Merge tag 'jfs-3.10' of git://github.com/kleikamp/linux-shaggy
[deliverable/linux.git] / fs / nfsd / nfs4state.c
1 /*
2 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
4 *
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include <linux/file.h>
36 #include <linux/fs.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include "xdr4.h"
45 #include "vfs.h"
46 #include "current_stateid.h"
47
48 #include "netns.h"
49
50 #define NFSDDBG_FACILITY NFSDDBG_PROC
51
52 #define all_ones {{~0,~0},~0}
53 static const stateid_t one_stateid = {
54 .si_generation = ~0,
55 .si_opaque = all_ones,
56 };
57 static const stateid_t zero_stateid = {
58 /* all fields zero */
59 };
60 static const stateid_t currentstateid = {
61 .si_generation = 1,
62 };
63
64 static u64 current_sessionid = 1;
65
66 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
67 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
68 #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
69
70 /* forward declarations */
71 static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
72
73 /* Locking: */
74
75 /* Currently used for almost all code touching nfsv4 state: */
76 static DEFINE_MUTEX(client_mutex);
77
78 /*
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
82 */
83 static DEFINE_SPINLOCK(recall_lock);
84
85 static struct kmem_cache *openowner_slab = NULL;
86 static struct kmem_cache *lockowner_slab = NULL;
87 static struct kmem_cache *file_slab = NULL;
88 static struct kmem_cache *stateid_slab = NULL;
89 static struct kmem_cache *deleg_slab = NULL;
90
91 void
92 nfs4_lock_state(void)
93 {
94 mutex_lock(&client_mutex);
95 }
96
97 static void free_session(struct kref *);
98
99 /* Must be called under the client_lock */
100 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
101 {
102 kref_put(&ses->se_ref, free_session);
103 }
104
105 static void nfsd4_get_session(struct nfsd4_session *ses)
106 {
107 kref_get(&ses->se_ref);
108 }
109
110 void
111 nfs4_unlock_state(void)
112 {
113 mutex_unlock(&client_mutex);
114 }
115
116 static inline u32
117 opaque_hashval(const void *ptr, int nbytes)
118 {
119 unsigned char *cptr = (unsigned char *) ptr;
120
121 u32 x = 0;
122 while (nbytes--) {
123 x *= 37;
124 x += *cptr++;
125 }
126 return x;
127 }
128
129 static struct list_head del_recall_lru;
130
131 static void nfsd4_free_file(struct nfs4_file *f)
132 {
133 kmem_cache_free(file_slab, f);
134 }
135
136 static inline void
137 put_nfs4_file(struct nfs4_file *fi)
138 {
139 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
140 list_del(&fi->fi_hash);
141 spin_unlock(&recall_lock);
142 iput(fi->fi_inode);
143 nfsd4_free_file(fi);
144 }
145 }
146
147 static inline void
148 get_nfs4_file(struct nfs4_file *fi)
149 {
150 atomic_inc(&fi->fi_ref);
151 }
152
153 static int num_delegations;
154 unsigned long max_delegations;
155
156 /*
157 * Open owner state (share locks)
158 */
159
160 /* hash tables for lock and open owners */
161 #define OWNER_HASH_BITS 8
162 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
163 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
164
165 static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
166 {
167 unsigned int ret;
168
169 ret = opaque_hashval(ownername->data, ownername->len);
170 ret += clientid;
171 return ret & OWNER_HASH_MASK;
172 }
173
174 /* hash table for nfs4_file */
175 #define FILE_HASH_BITS 8
176 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
177
178 static unsigned int file_hashval(struct inode *ino)
179 {
180 /* XXX: why are we hashing on inode pointer, anyway? */
181 return hash_ptr(ino, FILE_HASH_BITS);
182 }
183
184 static struct list_head file_hashtbl[FILE_HASH_SIZE];
185
186 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
187 {
188 WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
189 atomic_inc(&fp->fi_access[oflag]);
190 }
191
192 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
193 {
194 if (oflag == O_RDWR) {
195 __nfs4_file_get_access(fp, O_RDONLY);
196 __nfs4_file_get_access(fp, O_WRONLY);
197 } else
198 __nfs4_file_get_access(fp, oflag);
199 }
200
201 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
202 {
203 if (fp->fi_fds[oflag]) {
204 fput(fp->fi_fds[oflag]);
205 fp->fi_fds[oflag] = NULL;
206 }
207 }
208
209 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
210 {
211 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
212 nfs4_file_put_fd(fp, oflag);
213 /*
214 * It's also safe to get rid of the RDWR open *if*
215 * we no longer have need of the other kind of access
216 * or if we already have the other kind of open:
217 */
218 if (fp->fi_fds[1-oflag]
219 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
220 nfs4_file_put_fd(fp, O_RDWR);
221 }
222 }
223
224 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
225 {
226 if (oflag == O_RDWR) {
227 __nfs4_file_put_access(fp, O_RDONLY);
228 __nfs4_file_put_access(fp, O_WRONLY);
229 } else
230 __nfs4_file_put_access(fp, oflag);
231 }
232
233 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
234 kmem_cache *slab)
235 {
236 struct idr *stateids = &cl->cl_stateids;
237 struct nfs4_stid *stid;
238 int new_id;
239
240 stid = kmem_cache_alloc(slab, GFP_KERNEL);
241 if (!stid)
242 return NULL;
243
244 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
245 if (new_id < 0)
246 goto out_free;
247 stid->sc_client = cl;
248 stid->sc_type = 0;
249 stid->sc_stateid.si_opaque.so_id = new_id;
250 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
251 /* Will be incremented before return to client: */
252 stid->sc_stateid.si_generation = 0;
253
254 /*
255 * It shouldn't be a problem to reuse an opaque stateid value.
256 * I don't think it is for 4.1. But with 4.0 I worry that, for
257 * example, a stray write retransmission could be accepted by
258 * the server when it should have been rejected. Therefore,
259 * adopt a trick from the sctp code to attempt to maximize the
260 * amount of time until an id is reused, by ensuring they always
261 * "increase" (mod INT_MAX):
262 */
263 return stid;
264 out_free:
265 kfree(stid);
266 return NULL;
267 }
268
269 static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
270 {
271 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
272 }
273
274 static struct nfs4_delegation *
275 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
276 {
277 struct nfs4_delegation *dp;
278 struct nfs4_file *fp = stp->st_file;
279
280 dprintk("NFSD alloc_init_deleg\n");
281 /*
282 * Major work on the lease subsystem (for example, to support
283 * calbacks on stat) will be required before we can support
284 * write delegations properly.
285 */
286 if (type != NFS4_OPEN_DELEGATE_READ)
287 return NULL;
288 if (fp->fi_had_conflict)
289 return NULL;
290 if (num_delegations > max_delegations)
291 return NULL;
292 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
293 if (dp == NULL)
294 return dp;
295 dp->dl_stid.sc_type = NFS4_DELEG_STID;
296 /*
297 * delegation seqid's are never incremented. The 4.1 special
298 * meaning of seqid 0 isn't meaningful, really, but let's avoid
299 * 0 anyway just for consistency and use 1:
300 */
301 dp->dl_stid.sc_stateid.si_generation = 1;
302 num_delegations++;
303 INIT_LIST_HEAD(&dp->dl_perfile);
304 INIT_LIST_HEAD(&dp->dl_perclnt);
305 INIT_LIST_HEAD(&dp->dl_recall_lru);
306 get_nfs4_file(fp);
307 dp->dl_file = fp;
308 dp->dl_type = type;
309 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
310 dp->dl_time = 0;
311 atomic_set(&dp->dl_count, 1);
312 nfsd4_init_callback(&dp->dl_recall);
313 return dp;
314 }
315
316 static void free_stid(struct nfs4_stid *s, struct kmem_cache *slab)
317 {
318 struct idr *stateids = &s->sc_client->cl_stateids;
319
320 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
321 kmem_cache_free(slab, s);
322 }
323
324 void
325 nfs4_put_delegation(struct nfs4_delegation *dp)
326 {
327 if (atomic_dec_and_test(&dp->dl_count)) {
328 dprintk("NFSD: freeing dp %p\n",dp);
329 put_nfs4_file(dp->dl_file);
330 free_stid(&dp->dl_stid, deleg_slab);
331 num_delegations--;
332 }
333 }
334
335 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
336 {
337 if (atomic_dec_and_test(&fp->fi_delegees)) {
338 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
339 fp->fi_lease = NULL;
340 fput(fp->fi_deleg_file);
341 fp->fi_deleg_file = NULL;
342 }
343 }
344
345 static void unhash_stid(struct nfs4_stid *s)
346 {
347 s->sc_type = 0;
348 }
349
350 /* Called under the state lock. */
351 static void
352 unhash_delegation(struct nfs4_delegation *dp)
353 {
354 unhash_stid(&dp->dl_stid);
355 list_del_init(&dp->dl_perclnt);
356 spin_lock(&recall_lock);
357 list_del_init(&dp->dl_perfile);
358 list_del_init(&dp->dl_recall_lru);
359 spin_unlock(&recall_lock);
360 nfs4_put_deleg_lease(dp->dl_file);
361 nfs4_put_delegation(dp);
362 }
363
364 /*
365 * SETCLIENTID state
366 */
367
368 static unsigned int clientid_hashval(u32 id)
369 {
370 return id & CLIENT_HASH_MASK;
371 }
372
373 static unsigned int clientstr_hashval(const char *name)
374 {
375 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
376 }
377
378 /*
379 * We store the NONE, READ, WRITE, and BOTH bits separately in the
380 * st_{access,deny}_bmap field of the stateid, in order to track not
381 * only what share bits are currently in force, but also what
382 * combinations of share bits previous opens have used. This allows us
383 * to enforce the recommendation of rfc 3530 14.2.19 that the server
384 * return an error if the client attempt to downgrade to a combination
385 * of share bits not explicable by closing some of its previous opens.
386 *
387 * XXX: This enforcement is actually incomplete, since we don't keep
388 * track of access/deny bit combinations; so, e.g., we allow:
389 *
390 * OPEN allow read, deny write
391 * OPEN allow both, deny none
392 * DOWNGRADE allow read, deny none
393 *
394 * which we should reject.
395 */
396 static unsigned int
397 bmap_to_share_mode(unsigned long bmap) {
398 int i;
399 unsigned int access = 0;
400
401 for (i = 1; i < 4; i++) {
402 if (test_bit(i, &bmap))
403 access |= i;
404 }
405 return access;
406 }
407
408 static bool
409 test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
410 unsigned int access, deny;
411
412 access = bmap_to_share_mode(stp->st_access_bmap);
413 deny = bmap_to_share_mode(stp->st_deny_bmap);
414 if ((access & open->op_share_deny) || (deny & open->op_share_access))
415 return false;
416 return true;
417 }
418
419 /* set share access for a given stateid */
420 static inline void
421 set_access(u32 access, struct nfs4_ol_stateid *stp)
422 {
423 __set_bit(access, &stp->st_access_bmap);
424 }
425
426 /* clear share access for a given stateid */
427 static inline void
428 clear_access(u32 access, struct nfs4_ol_stateid *stp)
429 {
430 __clear_bit(access, &stp->st_access_bmap);
431 }
432
433 /* test whether a given stateid has access */
434 static inline bool
435 test_access(u32 access, struct nfs4_ol_stateid *stp)
436 {
437 return test_bit(access, &stp->st_access_bmap);
438 }
439
440 /* set share deny for a given stateid */
441 static inline void
442 set_deny(u32 access, struct nfs4_ol_stateid *stp)
443 {
444 __set_bit(access, &stp->st_deny_bmap);
445 }
446
447 /* clear share deny for a given stateid */
448 static inline void
449 clear_deny(u32 access, struct nfs4_ol_stateid *stp)
450 {
451 __clear_bit(access, &stp->st_deny_bmap);
452 }
453
454 /* test whether a given stateid is denying specific access */
455 static inline bool
456 test_deny(u32 access, struct nfs4_ol_stateid *stp)
457 {
458 return test_bit(access, &stp->st_deny_bmap);
459 }
460
461 static int nfs4_access_to_omode(u32 access)
462 {
463 switch (access & NFS4_SHARE_ACCESS_BOTH) {
464 case NFS4_SHARE_ACCESS_READ:
465 return O_RDONLY;
466 case NFS4_SHARE_ACCESS_WRITE:
467 return O_WRONLY;
468 case NFS4_SHARE_ACCESS_BOTH:
469 return O_RDWR;
470 }
471 WARN_ON_ONCE(1);
472 return O_RDONLY;
473 }
474
475 /* release all access and file references for a given stateid */
476 static void
477 release_all_access(struct nfs4_ol_stateid *stp)
478 {
479 int i;
480
481 for (i = 1; i < 4; i++) {
482 if (test_access(i, stp))
483 nfs4_file_put_access(stp->st_file,
484 nfs4_access_to_omode(i));
485 clear_access(i, stp);
486 }
487 }
488
489 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
490 {
491 list_del(&stp->st_perfile);
492 list_del(&stp->st_perstateowner);
493 }
494
495 static void close_generic_stateid(struct nfs4_ol_stateid *stp)
496 {
497 release_all_access(stp);
498 put_nfs4_file(stp->st_file);
499 stp->st_file = NULL;
500 }
501
502 static void free_generic_stateid(struct nfs4_ol_stateid *stp)
503 {
504 free_stid(&stp->st_stid, stateid_slab);
505 }
506
507 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
508 {
509 struct file *file;
510
511 unhash_generic_stateid(stp);
512 unhash_stid(&stp->st_stid);
513 file = find_any_file(stp->st_file);
514 if (file)
515 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
516 close_generic_stateid(stp);
517 free_generic_stateid(stp);
518 }
519
520 static void unhash_lockowner(struct nfs4_lockowner *lo)
521 {
522 struct nfs4_ol_stateid *stp;
523
524 list_del(&lo->lo_owner.so_strhash);
525 list_del(&lo->lo_perstateid);
526 list_del(&lo->lo_owner_ino_hash);
527 while (!list_empty(&lo->lo_owner.so_stateids)) {
528 stp = list_first_entry(&lo->lo_owner.so_stateids,
529 struct nfs4_ol_stateid, st_perstateowner);
530 release_lock_stateid(stp);
531 }
532 }
533
534 static void release_lockowner(struct nfs4_lockowner *lo)
535 {
536 unhash_lockowner(lo);
537 nfs4_free_lockowner(lo);
538 }
539
540 static void
541 release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
542 {
543 struct nfs4_lockowner *lo;
544
545 while (!list_empty(&open_stp->st_lockowners)) {
546 lo = list_entry(open_stp->st_lockowners.next,
547 struct nfs4_lockowner, lo_perstateid);
548 release_lockowner(lo);
549 }
550 }
551
552 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
553 {
554 unhash_generic_stateid(stp);
555 release_stateid_lockowners(stp);
556 close_generic_stateid(stp);
557 }
558
559 static void release_open_stateid(struct nfs4_ol_stateid *stp)
560 {
561 unhash_open_stateid(stp);
562 unhash_stid(&stp->st_stid);
563 free_generic_stateid(stp);
564 }
565
566 static void unhash_openowner(struct nfs4_openowner *oo)
567 {
568 struct nfs4_ol_stateid *stp;
569
570 list_del(&oo->oo_owner.so_strhash);
571 list_del(&oo->oo_perclient);
572 while (!list_empty(&oo->oo_owner.so_stateids)) {
573 stp = list_first_entry(&oo->oo_owner.so_stateids,
574 struct nfs4_ol_stateid, st_perstateowner);
575 release_open_stateid(stp);
576 }
577 }
578
579 static void release_last_closed_stateid(struct nfs4_openowner *oo)
580 {
581 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
582
583 if (s) {
584 unhash_stid(&s->st_stid);
585 free_generic_stateid(s);
586 oo->oo_last_closed_stid = NULL;
587 }
588 }
589
590 static void release_openowner(struct nfs4_openowner *oo)
591 {
592 unhash_openowner(oo);
593 list_del(&oo->oo_close_lru);
594 release_last_closed_stateid(oo);
595 nfs4_free_openowner(oo);
596 }
597
598 static inline int
599 hash_sessionid(struct nfs4_sessionid *sessionid)
600 {
601 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
602
603 return sid->sequence % SESSION_HASH_SIZE;
604 }
605
606 #ifdef NFSD_DEBUG
607 static inline void
608 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
609 {
610 u32 *ptr = (u32 *)(&sessionid->data[0]);
611 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
612 }
613 #else
614 static inline void
615 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
616 {
617 }
618 #endif
619
620
621 static void
622 gen_sessionid(struct nfsd4_session *ses)
623 {
624 struct nfs4_client *clp = ses->se_client;
625 struct nfsd4_sessionid *sid;
626
627 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
628 sid->clientid = clp->cl_clientid;
629 sid->sequence = current_sessionid++;
630 sid->reserved = 0;
631 }
632
633 /*
634 * The protocol defines ca_maxresponssize_cached to include the size of
635 * the rpc header, but all we need to cache is the data starting after
636 * the end of the initial SEQUENCE operation--the rest we regenerate
637 * each time. Therefore we can advertise a ca_maxresponssize_cached
638 * value that is the number of bytes in our cache plus a few additional
639 * bytes. In order to stay on the safe side, and not promise more than
640 * we can cache, those additional bytes must be the minimum possible: 24
641 * bytes of rpc header (xid through accept state, with AUTH_NULL
642 * verifier), 12 for the compound header (with zero-length tag), and 44
643 * for the SEQUENCE op response:
644 */
645 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
646
647 static void
648 free_session_slots(struct nfsd4_session *ses)
649 {
650 int i;
651
652 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
653 kfree(ses->se_slots[i]);
654 }
655
656 /*
657 * We don't actually need to cache the rpc and session headers, so we
658 * can allocate a little less for each slot:
659 */
660 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
661 {
662 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
663 }
664
665 static int nfsd4_sanitize_slot_size(u32 size)
666 {
667 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
668 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
669
670 return size;
671 }
672
673 /*
674 * XXX: If we run out of reserved DRC memory we could (up to a point)
675 * re-negotiate active sessions and reduce their slot usage to make
676 * room for new connections. For now we just fail the create session.
677 */
678 static int nfsd4_get_drc_mem(int slotsize, u32 num)
679 {
680 int avail;
681
682 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
683
684 spin_lock(&nfsd_drc_lock);
685 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
686 nfsd_drc_max_mem - nfsd_drc_mem_used);
687 num = min_t(int, num, avail / slotsize);
688 nfsd_drc_mem_used += num * slotsize;
689 spin_unlock(&nfsd_drc_lock);
690
691 return num;
692 }
693
694 static void nfsd4_put_drc_mem(int slotsize, int num)
695 {
696 spin_lock(&nfsd_drc_lock);
697 nfsd_drc_mem_used -= slotsize * num;
698 spin_unlock(&nfsd_drc_lock);
699 }
700
701 static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
702 {
703 struct nfsd4_session *new;
704 int mem, i;
705
706 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
707 + sizeof(struct nfsd4_session) > PAGE_SIZE);
708 mem = numslots * sizeof(struct nfsd4_slot *);
709
710 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
711 if (!new)
712 return NULL;
713 /* allocate each struct nfsd4_slot and data cache in one piece */
714 for (i = 0; i < numslots; i++) {
715 mem = sizeof(struct nfsd4_slot) + slotsize;
716 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
717 if (!new->se_slots[i])
718 goto out_free;
719 }
720 return new;
721 out_free:
722 while (i--)
723 kfree(new->se_slots[i]);
724 kfree(new);
725 return NULL;
726 }
727
728 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new,
729 struct nfsd4_channel_attrs *req,
730 int numslots, int slotsize,
731 struct nfsd_net *nn)
732 {
733 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
734
735 new->maxreqs = numslots;
736 new->maxresp_cached = min_t(u32, req->maxresp_cached,
737 slotsize + NFSD_MIN_HDR_SEQ_SZ);
738 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
739 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
740 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
741 }
742
743 static void free_conn(struct nfsd4_conn *c)
744 {
745 svc_xprt_put(c->cn_xprt);
746 kfree(c);
747 }
748
749 static void nfsd4_conn_lost(struct svc_xpt_user *u)
750 {
751 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
752 struct nfs4_client *clp = c->cn_session->se_client;
753
754 spin_lock(&clp->cl_lock);
755 if (!list_empty(&c->cn_persession)) {
756 list_del(&c->cn_persession);
757 free_conn(c);
758 }
759 spin_unlock(&clp->cl_lock);
760 nfsd4_probe_callback(clp);
761 }
762
763 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
764 {
765 struct nfsd4_conn *conn;
766
767 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
768 if (!conn)
769 return NULL;
770 svc_xprt_get(rqstp->rq_xprt);
771 conn->cn_xprt = rqstp->rq_xprt;
772 conn->cn_flags = flags;
773 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
774 return conn;
775 }
776
777 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
778 {
779 conn->cn_session = ses;
780 list_add(&conn->cn_persession, &ses->se_conns);
781 }
782
783 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
784 {
785 struct nfs4_client *clp = ses->se_client;
786
787 spin_lock(&clp->cl_lock);
788 __nfsd4_hash_conn(conn, ses);
789 spin_unlock(&clp->cl_lock);
790 }
791
792 static int nfsd4_register_conn(struct nfsd4_conn *conn)
793 {
794 conn->cn_xpt_user.callback = nfsd4_conn_lost;
795 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
796 }
797
798 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
799 {
800 int ret;
801
802 nfsd4_hash_conn(conn, ses);
803 ret = nfsd4_register_conn(conn);
804 if (ret)
805 /* oops; xprt is already down: */
806 nfsd4_conn_lost(&conn->cn_xpt_user);
807 if (conn->cn_flags & NFS4_CDFC4_BACK) {
808 /* callback channel may be back up */
809 nfsd4_probe_callback(ses->se_client);
810 }
811 }
812
813 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
814 {
815 u32 dir = NFS4_CDFC4_FORE;
816
817 if (cses->flags & SESSION4_BACK_CHAN)
818 dir |= NFS4_CDFC4_BACK;
819 return alloc_conn(rqstp, dir);
820 }
821
822 /* must be called under client_lock */
823 static void nfsd4_del_conns(struct nfsd4_session *s)
824 {
825 struct nfs4_client *clp = s->se_client;
826 struct nfsd4_conn *c;
827
828 spin_lock(&clp->cl_lock);
829 while (!list_empty(&s->se_conns)) {
830 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
831 list_del_init(&c->cn_persession);
832 spin_unlock(&clp->cl_lock);
833
834 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
835 free_conn(c);
836
837 spin_lock(&clp->cl_lock);
838 }
839 spin_unlock(&clp->cl_lock);
840 }
841
842 static void __free_session(struct nfsd4_session *ses)
843 {
844 nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
845 free_session_slots(ses);
846 kfree(ses);
847 }
848
849 static void free_session(struct kref *kref)
850 {
851 struct nfsd4_session *ses;
852 struct nfsd_net *nn;
853
854 ses = container_of(kref, struct nfsd4_session, se_ref);
855 nn = net_generic(ses->se_client->net, nfsd_net_id);
856
857 lockdep_assert_held(&nn->client_lock);
858 nfsd4_del_conns(ses);
859 __free_session(ses);
860 }
861
862 void nfsd4_put_session(struct nfsd4_session *ses)
863 {
864 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
865
866 spin_lock(&nn->client_lock);
867 nfsd4_put_session_locked(ses);
868 spin_unlock(&nn->client_lock);
869 }
870
871 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan,
872 struct nfsd_net *nn)
873 {
874 struct nfsd4_session *new;
875 int numslots, slotsize;
876 /*
877 * Note decreasing slot size below client's request may
878 * make it difficult for client to function correctly, whereas
879 * decreasing the number of slots will (just?) affect
880 * performance. When short on memory we therefore prefer to
881 * decrease number of slots instead of their size.
882 */
883 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
884 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
885 if (numslots < 1)
886 return NULL;
887
888 new = __alloc_session(slotsize, numslots);
889 if (!new) {
890 nfsd4_put_drc_mem(slotsize, numslots);
891 return NULL;
892 }
893 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize, nn);
894 return new;
895 }
896
897 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
898 {
899 int idx;
900 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
901
902 new->se_client = clp;
903 gen_sessionid(new);
904
905 INIT_LIST_HEAD(&new->se_conns);
906
907 new->se_cb_seq_nr = 1;
908 new->se_flags = cses->flags;
909 new->se_cb_prog = cses->callback_prog;
910 new->se_cb_sec = cses->cb_sec;
911 kref_init(&new->se_ref);
912 idx = hash_sessionid(&new->se_sessionid);
913 spin_lock(&nn->client_lock);
914 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
915 spin_lock(&clp->cl_lock);
916 list_add(&new->se_perclnt, &clp->cl_sessions);
917 spin_unlock(&clp->cl_lock);
918 spin_unlock(&nn->client_lock);
919
920 if (cses->flags & SESSION4_BACK_CHAN) {
921 struct sockaddr *sa = svc_addr(rqstp);
922 /*
923 * This is a little silly; with sessions there's no real
924 * use for the callback address. Use the peer address
925 * as a reasonable default for now, but consider fixing
926 * the rpc client not to require an address in the
927 * future:
928 */
929 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
930 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
931 }
932 }
933
934 /* caller must hold client_lock */
935 static struct nfsd4_session *
936 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
937 {
938 struct nfsd4_session *elem;
939 int idx;
940 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
941
942 dump_sessionid(__func__, sessionid);
943 idx = hash_sessionid(sessionid);
944 /* Search in the appropriate list */
945 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
946 if (!memcmp(elem->se_sessionid.data, sessionid->data,
947 NFS4_MAX_SESSIONID_LEN)) {
948 return elem;
949 }
950 }
951
952 dprintk("%s: session not found\n", __func__);
953 return NULL;
954 }
955
956 /* caller must hold client_lock */
957 static void
958 unhash_session(struct nfsd4_session *ses)
959 {
960 list_del(&ses->se_hash);
961 spin_lock(&ses->se_client->cl_lock);
962 list_del(&ses->se_perclnt);
963 spin_unlock(&ses->se_client->cl_lock);
964 }
965
966 /* must be called under the client_lock */
967 static inline void
968 renew_client_locked(struct nfs4_client *clp)
969 {
970 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
971
972 if (is_client_expired(clp)) {
973 WARN_ON(1);
974 printk("%s: client (clientid %08x/%08x) already expired\n",
975 __func__,
976 clp->cl_clientid.cl_boot,
977 clp->cl_clientid.cl_id);
978 return;
979 }
980
981 dprintk("renewing client (clientid %08x/%08x)\n",
982 clp->cl_clientid.cl_boot,
983 clp->cl_clientid.cl_id);
984 list_move_tail(&clp->cl_lru, &nn->client_lru);
985 clp->cl_time = get_seconds();
986 }
987
988 static inline void
989 renew_client(struct nfs4_client *clp)
990 {
991 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
992
993 spin_lock(&nn->client_lock);
994 renew_client_locked(clp);
995 spin_unlock(&nn->client_lock);
996 }
997
998 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
999 static int
1000 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1001 {
1002 if (clid->cl_boot == nn->boot_time)
1003 return 0;
1004 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1005 clid->cl_boot, clid->cl_id, nn->boot_time);
1006 return 1;
1007 }
1008
1009 /*
1010 * XXX Should we use a slab cache ?
1011 * This type of memory management is somewhat inefficient, but we use it
1012 * anyway since SETCLIENTID is not a common operation.
1013 */
1014 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1015 {
1016 struct nfs4_client *clp;
1017
1018 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1019 if (clp == NULL)
1020 return NULL;
1021 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1022 if (clp->cl_name.data == NULL) {
1023 kfree(clp);
1024 return NULL;
1025 }
1026 clp->cl_name.len = name.len;
1027 return clp;
1028 }
1029
1030 static inline void
1031 free_client(struct nfs4_client *clp)
1032 {
1033 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1034
1035 lockdep_assert_held(&nn->client_lock);
1036 while (!list_empty(&clp->cl_sessions)) {
1037 struct nfsd4_session *ses;
1038 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1039 se_perclnt);
1040 list_del(&ses->se_perclnt);
1041 nfsd4_put_session_locked(ses);
1042 }
1043 free_svc_cred(&clp->cl_cred);
1044 kfree(clp->cl_name.data);
1045 idr_destroy(&clp->cl_stateids);
1046 kfree(clp);
1047 }
1048
1049 void
1050 release_session_client(struct nfsd4_session *session)
1051 {
1052 struct nfs4_client *clp = session->se_client;
1053 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1054
1055 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
1056 return;
1057 if (is_client_expired(clp)) {
1058 free_client(clp);
1059 session->se_client = NULL;
1060 } else
1061 renew_client_locked(clp);
1062 spin_unlock(&nn->client_lock);
1063 }
1064
1065 /* must be called under the client_lock */
1066 static inline void
1067 unhash_client_locked(struct nfs4_client *clp)
1068 {
1069 struct nfsd4_session *ses;
1070
1071 mark_client_expired(clp);
1072 list_del(&clp->cl_lru);
1073 spin_lock(&clp->cl_lock);
1074 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1075 list_del_init(&ses->se_hash);
1076 spin_unlock(&clp->cl_lock);
1077 }
1078
1079 static void
1080 destroy_client(struct nfs4_client *clp)
1081 {
1082 struct nfs4_openowner *oo;
1083 struct nfs4_delegation *dp;
1084 struct list_head reaplist;
1085 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1086
1087 INIT_LIST_HEAD(&reaplist);
1088 spin_lock(&recall_lock);
1089 while (!list_empty(&clp->cl_delegations)) {
1090 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1091 list_del_init(&dp->dl_perclnt);
1092 list_move(&dp->dl_recall_lru, &reaplist);
1093 }
1094 spin_unlock(&recall_lock);
1095 while (!list_empty(&reaplist)) {
1096 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1097 unhash_delegation(dp);
1098 }
1099 while (!list_empty(&clp->cl_openowners)) {
1100 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1101 release_openowner(oo);
1102 }
1103 nfsd4_shutdown_callback(clp);
1104 if (clp->cl_cb_conn.cb_xprt)
1105 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1106 list_del(&clp->cl_idhash);
1107 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1108 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1109 else
1110 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1111 spin_lock(&nn->client_lock);
1112 unhash_client_locked(clp);
1113 if (atomic_read(&clp->cl_refcount) == 0)
1114 free_client(clp);
1115 spin_unlock(&nn->client_lock);
1116 }
1117
1118 static void expire_client(struct nfs4_client *clp)
1119 {
1120 nfsd4_client_record_remove(clp);
1121 destroy_client(clp);
1122 }
1123
1124 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1125 {
1126 memcpy(target->cl_verifier.data, source->data,
1127 sizeof(target->cl_verifier.data));
1128 }
1129
1130 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1131 {
1132 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1133 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1134 }
1135
1136 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1137 {
1138 if (source->cr_principal) {
1139 target->cr_principal =
1140 kstrdup(source->cr_principal, GFP_KERNEL);
1141 if (target->cr_principal == NULL)
1142 return -ENOMEM;
1143 } else
1144 target->cr_principal = NULL;
1145 target->cr_flavor = source->cr_flavor;
1146 target->cr_uid = source->cr_uid;
1147 target->cr_gid = source->cr_gid;
1148 target->cr_group_info = source->cr_group_info;
1149 get_group_info(target->cr_group_info);
1150 return 0;
1151 }
1152
1153 static long long
1154 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1155 {
1156 long long res;
1157
1158 res = o1->len - o2->len;
1159 if (res)
1160 return res;
1161 return (long long)memcmp(o1->data, o2->data, o1->len);
1162 }
1163
1164 static int same_name(const char *n1, const char *n2)
1165 {
1166 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1167 }
1168
1169 static int
1170 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1171 {
1172 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1173 }
1174
1175 static int
1176 same_clid(clientid_t *cl1, clientid_t *cl2)
1177 {
1178 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1179 }
1180
1181 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1182 {
1183 int i;
1184
1185 if (g1->ngroups != g2->ngroups)
1186 return false;
1187 for (i=0; i<g1->ngroups; i++)
1188 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1189 return false;
1190 return true;
1191 }
1192
1193 /*
1194 * RFC 3530 language requires clid_inuse be returned when the
1195 * "principal" associated with a requests differs from that previously
1196 * used. We use uid, gid's, and gss principal string as our best
1197 * approximation. We also don't want to allow non-gss use of a client
1198 * established using gss: in theory cr_principal should catch that
1199 * change, but in practice cr_principal can be null even in the gss case
1200 * since gssd doesn't always pass down a principal string.
1201 */
1202 static bool is_gss_cred(struct svc_cred *cr)
1203 {
1204 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1205 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1206 }
1207
1208
1209 static bool
1210 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1211 {
1212 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1213 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1214 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1215 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1216 return false;
1217 if (cr1->cr_principal == cr2->cr_principal)
1218 return true;
1219 if (!cr1->cr_principal || !cr2->cr_principal)
1220 return false;
1221 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1222 }
1223
1224 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1225 {
1226 static u32 current_clientid = 1;
1227
1228 clp->cl_clientid.cl_boot = nn->boot_time;
1229 clp->cl_clientid.cl_id = current_clientid++;
1230 }
1231
1232 static void gen_confirm(struct nfs4_client *clp)
1233 {
1234 __be32 verf[2];
1235 static u32 i;
1236
1237 verf[0] = (__be32)get_seconds();
1238 verf[1] = (__be32)i++;
1239 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1240 }
1241
1242 static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
1243 {
1244 struct nfs4_stid *ret;
1245
1246 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1247 if (!ret || !ret->sc_type)
1248 return NULL;
1249 return ret;
1250 }
1251
1252 static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1253 {
1254 struct nfs4_stid *s;
1255
1256 s = find_stateid(cl, t);
1257 if (!s)
1258 return NULL;
1259 if (typemask & s->sc_type)
1260 return s;
1261 return NULL;
1262 }
1263
1264 static struct nfs4_client *create_client(struct xdr_netobj name,
1265 struct svc_rqst *rqstp, nfs4_verifier *verf)
1266 {
1267 struct nfs4_client *clp;
1268 struct sockaddr *sa = svc_addr(rqstp);
1269 int ret;
1270 struct net *net = SVC_NET(rqstp);
1271 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1272
1273 clp = alloc_client(name);
1274 if (clp == NULL)
1275 return NULL;
1276
1277 INIT_LIST_HEAD(&clp->cl_sessions);
1278 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1279 if (ret) {
1280 spin_lock(&nn->client_lock);
1281 free_client(clp);
1282 spin_unlock(&nn->client_lock);
1283 return NULL;
1284 }
1285 idr_init(&clp->cl_stateids);
1286 atomic_set(&clp->cl_refcount, 0);
1287 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1288 INIT_LIST_HEAD(&clp->cl_idhash);
1289 INIT_LIST_HEAD(&clp->cl_openowners);
1290 INIT_LIST_HEAD(&clp->cl_delegations);
1291 INIT_LIST_HEAD(&clp->cl_lru);
1292 INIT_LIST_HEAD(&clp->cl_callbacks);
1293 spin_lock_init(&clp->cl_lock);
1294 nfsd4_init_callback(&clp->cl_cb_null);
1295 clp->cl_time = get_seconds();
1296 clear_bit(0, &clp->cl_cb_slot_busy);
1297 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1298 copy_verf(clp, verf);
1299 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1300 gen_confirm(clp);
1301 clp->cl_cb_session = NULL;
1302 clp->net = net;
1303 return clp;
1304 }
1305
1306 static void
1307 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1308 {
1309 struct rb_node **new = &(root->rb_node), *parent = NULL;
1310 struct nfs4_client *clp;
1311
1312 while (*new) {
1313 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1314 parent = *new;
1315
1316 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1317 new = &((*new)->rb_left);
1318 else
1319 new = &((*new)->rb_right);
1320 }
1321
1322 rb_link_node(&new_clp->cl_namenode, parent, new);
1323 rb_insert_color(&new_clp->cl_namenode, root);
1324 }
1325
1326 static struct nfs4_client *
1327 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1328 {
1329 long long cmp;
1330 struct rb_node *node = root->rb_node;
1331 struct nfs4_client *clp;
1332
1333 while (node) {
1334 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1335 cmp = compare_blob(&clp->cl_name, name);
1336 if (cmp > 0)
1337 node = node->rb_left;
1338 else if (cmp < 0)
1339 node = node->rb_right;
1340 else
1341 return clp;
1342 }
1343 return NULL;
1344 }
1345
1346 static void
1347 add_to_unconfirmed(struct nfs4_client *clp)
1348 {
1349 unsigned int idhashval;
1350 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1351
1352 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1353 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1354 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1355 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1356 renew_client(clp);
1357 }
1358
1359 static void
1360 move_to_confirmed(struct nfs4_client *clp)
1361 {
1362 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1363 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1364
1365 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1366 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1367 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1368 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1369 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1370 renew_client(clp);
1371 }
1372
1373 static struct nfs4_client *
1374 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1375 {
1376 struct nfs4_client *clp;
1377 unsigned int idhashval = clientid_hashval(clid->cl_id);
1378
1379 list_for_each_entry(clp, &nn->conf_id_hashtbl[idhashval], cl_idhash) {
1380 if (same_clid(&clp->cl_clientid, clid)) {
1381 if ((bool)clp->cl_minorversion != sessions)
1382 return NULL;
1383 renew_client(clp);
1384 return clp;
1385 }
1386 }
1387 return NULL;
1388 }
1389
1390 static struct nfs4_client *
1391 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1392 {
1393 struct nfs4_client *clp;
1394 unsigned int idhashval = clientid_hashval(clid->cl_id);
1395
1396 list_for_each_entry(clp, &nn->unconf_id_hashtbl[idhashval], cl_idhash) {
1397 if (same_clid(&clp->cl_clientid, clid)) {
1398 if ((bool)clp->cl_minorversion != sessions)
1399 return NULL;
1400 return clp;
1401 }
1402 }
1403 return NULL;
1404 }
1405
1406 static bool clp_used_exchangeid(struct nfs4_client *clp)
1407 {
1408 return clp->cl_exchange_flags != 0;
1409 }
1410
1411 static struct nfs4_client *
1412 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1413 {
1414 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1415 }
1416
1417 static struct nfs4_client *
1418 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1419 {
1420 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1421 }
1422
1423 static void
1424 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1425 {
1426 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1427 struct sockaddr *sa = svc_addr(rqstp);
1428 u32 scopeid = rpc_get_scope_id(sa);
1429 unsigned short expected_family;
1430
1431 /* Currently, we only support tcp and tcp6 for the callback channel */
1432 if (se->se_callback_netid_len == 3 &&
1433 !memcmp(se->se_callback_netid_val, "tcp", 3))
1434 expected_family = AF_INET;
1435 else if (se->se_callback_netid_len == 4 &&
1436 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1437 expected_family = AF_INET6;
1438 else
1439 goto out_err;
1440
1441 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1442 se->se_callback_addr_len,
1443 (struct sockaddr *)&conn->cb_addr,
1444 sizeof(conn->cb_addr));
1445
1446 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1447 goto out_err;
1448
1449 if (conn->cb_addr.ss_family == AF_INET6)
1450 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1451
1452 conn->cb_prog = se->se_callback_prog;
1453 conn->cb_ident = se->se_callback_ident;
1454 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1455 return;
1456 out_err:
1457 conn->cb_addr.ss_family = AF_UNSPEC;
1458 conn->cb_addrlen = 0;
1459 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1460 "will not receive delegations\n",
1461 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1462
1463 return;
1464 }
1465
1466 /*
1467 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1468 */
1469 void
1470 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1471 {
1472 struct nfsd4_slot *slot = resp->cstate.slot;
1473 unsigned int base;
1474
1475 dprintk("--> %s slot %p\n", __func__, slot);
1476
1477 slot->sl_opcnt = resp->opcnt;
1478 slot->sl_status = resp->cstate.status;
1479
1480 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1481 if (nfsd4_not_cached(resp)) {
1482 slot->sl_datalen = 0;
1483 return;
1484 }
1485 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1486 base = (char *)resp->cstate.datap -
1487 (char *)resp->xbuf->head[0].iov_base;
1488 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1489 slot->sl_datalen))
1490 WARN("%s: sessions DRC could not cache compound\n", __func__);
1491 return;
1492 }
1493
1494 /*
1495 * Encode the replay sequence operation from the slot values.
1496 * If cachethis is FALSE encode the uncached rep error on the next
1497 * operation which sets resp->p and increments resp->opcnt for
1498 * nfs4svc_encode_compoundres.
1499 *
1500 */
1501 static __be32
1502 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1503 struct nfsd4_compoundres *resp)
1504 {
1505 struct nfsd4_op *op;
1506 struct nfsd4_slot *slot = resp->cstate.slot;
1507
1508 /* Encode the replayed sequence operation */
1509 op = &args->ops[resp->opcnt - 1];
1510 nfsd4_encode_operation(resp, op);
1511
1512 /* Return nfserr_retry_uncached_rep in next operation. */
1513 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1514 op = &args->ops[resp->opcnt++];
1515 op->status = nfserr_retry_uncached_rep;
1516 nfsd4_encode_operation(resp, op);
1517 }
1518 return op->status;
1519 }
1520
1521 /*
1522 * The sequence operation is not cached because we can use the slot and
1523 * session values.
1524 */
1525 __be32
1526 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1527 struct nfsd4_sequence *seq)
1528 {
1529 struct nfsd4_slot *slot = resp->cstate.slot;
1530 __be32 status;
1531
1532 dprintk("--> %s slot %p\n", __func__, slot);
1533
1534 /* Either returns 0 or nfserr_retry_uncached */
1535 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1536 if (status == nfserr_retry_uncached_rep)
1537 return status;
1538
1539 /* The sequence operation has been encoded, cstate->datap set. */
1540 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1541
1542 resp->opcnt = slot->sl_opcnt;
1543 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1544 status = slot->sl_status;
1545
1546 return status;
1547 }
1548
1549 /*
1550 * Set the exchange_id flags returned by the server.
1551 */
1552 static void
1553 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1554 {
1555 /* pNFS is not supported */
1556 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1557
1558 /* Referrals are supported, Migration is not. */
1559 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1560
1561 /* set the wire flags to return to client. */
1562 clid->flags = new->cl_exchange_flags;
1563 }
1564
1565 static bool client_has_state(struct nfs4_client *clp)
1566 {
1567 /*
1568 * Note clp->cl_openowners check isn't quite right: there's no
1569 * need to count owners without stateid's.
1570 *
1571 * Also note we should probably be using this in 4.0 case too.
1572 */
1573 return !list_empty(&clp->cl_openowners)
1574 || !list_empty(&clp->cl_delegations)
1575 || !list_empty(&clp->cl_sessions);
1576 }
1577
1578 __be32
1579 nfsd4_exchange_id(struct svc_rqst *rqstp,
1580 struct nfsd4_compound_state *cstate,
1581 struct nfsd4_exchange_id *exid)
1582 {
1583 struct nfs4_client *unconf, *conf, *new;
1584 __be32 status;
1585 char addr_str[INET6_ADDRSTRLEN];
1586 nfs4_verifier verf = exid->verifier;
1587 struct sockaddr *sa = svc_addr(rqstp);
1588 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
1589 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1590
1591 rpc_ntop(sa, addr_str, sizeof(addr_str));
1592 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1593 "ip_addr=%s flags %x, spa_how %d\n",
1594 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1595 addr_str, exid->flags, exid->spa_how);
1596
1597 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
1598 return nfserr_inval;
1599
1600 /* Currently only support SP4_NONE */
1601 switch (exid->spa_how) {
1602 case SP4_NONE:
1603 break;
1604 default: /* checked by xdr code */
1605 WARN_ON_ONCE(1);
1606 case SP4_SSV:
1607 case SP4_MACH_CRED:
1608 return nfserr_serverfault; /* no excuse :-/ */
1609 }
1610
1611 /* Cases below refer to rfc 5661 section 18.35.4: */
1612 nfs4_lock_state();
1613 conf = find_confirmed_client_by_name(&exid->clname, nn);
1614 if (conf) {
1615 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1616 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1617
1618 if (update) {
1619 if (!clp_used_exchangeid(conf)) { /* buggy client */
1620 status = nfserr_inval;
1621 goto out;
1622 }
1623 if (!creds_match) { /* case 9 */
1624 status = nfserr_perm;
1625 goto out;
1626 }
1627 if (!verfs_match) { /* case 8 */
1628 status = nfserr_not_same;
1629 goto out;
1630 }
1631 /* case 6 */
1632 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1633 new = conf;
1634 goto out_copy;
1635 }
1636 if (!creds_match) { /* case 3 */
1637 if (client_has_state(conf)) {
1638 status = nfserr_clid_inuse;
1639 goto out;
1640 }
1641 expire_client(conf);
1642 goto out_new;
1643 }
1644 if (verfs_match) { /* case 2 */
1645 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
1646 new = conf;
1647 goto out_copy;
1648 }
1649 /* case 5, client reboot */
1650 goto out_new;
1651 }
1652
1653 if (update) { /* case 7 */
1654 status = nfserr_noent;
1655 goto out;
1656 }
1657
1658 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
1659 if (unconf) /* case 4, possible retry or client restart */
1660 expire_client(unconf);
1661
1662 /* case 1 (normal case) */
1663 out_new:
1664 new = create_client(exid->clname, rqstp, &verf);
1665 if (new == NULL) {
1666 status = nfserr_jukebox;
1667 goto out;
1668 }
1669 new->cl_minorversion = 1;
1670
1671 gen_clid(new, nn);
1672 add_to_unconfirmed(new);
1673 out_copy:
1674 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1675 exid->clientid.cl_id = new->cl_clientid.cl_id;
1676
1677 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
1678 nfsd4_set_ex_flags(new, exid);
1679
1680 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1681 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1682 status = nfs_ok;
1683
1684 out:
1685 nfs4_unlock_state();
1686 return status;
1687 }
1688
1689 static __be32
1690 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1691 {
1692 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1693 slot_seqid);
1694
1695 /* The slot is in use, and no response has been sent. */
1696 if (slot_inuse) {
1697 if (seqid == slot_seqid)
1698 return nfserr_jukebox;
1699 else
1700 return nfserr_seq_misordered;
1701 }
1702 /* Note unsigned 32-bit arithmetic handles wraparound: */
1703 if (likely(seqid == slot_seqid + 1))
1704 return nfs_ok;
1705 if (seqid == slot_seqid)
1706 return nfserr_replay_cache;
1707 return nfserr_seq_misordered;
1708 }
1709
1710 /*
1711 * Cache the create session result into the create session single DRC
1712 * slot cache by saving the xdr structure. sl_seqid has been set.
1713 * Do this for solo or embedded create session operations.
1714 */
1715 static void
1716 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1717 struct nfsd4_clid_slot *slot, __be32 nfserr)
1718 {
1719 slot->sl_status = nfserr;
1720 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1721 }
1722
1723 static __be32
1724 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1725 struct nfsd4_clid_slot *slot)
1726 {
1727 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1728 return slot->sl_status;
1729 }
1730
1731 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1732 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1733 1 + /* MIN tag is length with zero, only length */ \
1734 3 + /* version, opcount, opcode */ \
1735 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1736 /* seqid, slotID, slotID, cache */ \
1737 4 ) * sizeof(__be32))
1738
1739 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1740 2 + /* verifier: AUTH_NULL, length 0 */\
1741 1 + /* status */ \
1742 1 + /* MIN tag is length with zero, only length */ \
1743 3 + /* opcount, opcode, opstatus*/ \
1744 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1745 /* seqid, slotID, slotID, slotID, status */ \
1746 5 ) * sizeof(__be32))
1747
1748 static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1749 {
1750 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1751 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1752 }
1753
1754 __be32
1755 nfsd4_create_session(struct svc_rqst *rqstp,
1756 struct nfsd4_compound_state *cstate,
1757 struct nfsd4_create_session *cr_ses)
1758 {
1759 struct sockaddr *sa = svc_addr(rqstp);
1760 struct nfs4_client *conf, *unconf;
1761 struct nfsd4_session *new;
1762 struct nfsd4_conn *conn;
1763 struct nfsd4_clid_slot *cs_slot = NULL;
1764 __be32 status = 0;
1765 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1766
1767 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1768 return nfserr_inval;
1769 if (check_forechannel_attrs(cr_ses->fore_channel))
1770 return nfserr_toosmall;
1771 new = alloc_session(&cr_ses->fore_channel, nn);
1772 if (!new)
1773 return nfserr_jukebox;
1774 status = nfserr_jukebox;
1775 conn = alloc_conn_from_crses(rqstp, cr_ses);
1776 if (!conn)
1777 goto out_free_session;
1778
1779 nfs4_lock_state();
1780 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
1781 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
1782
1783 if (conf) {
1784 cs_slot = &conf->cl_cs_slot;
1785 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1786 if (status == nfserr_replay_cache) {
1787 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1788 goto out_free_conn;
1789 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1790 status = nfserr_seq_misordered;
1791 goto out_free_conn;
1792 }
1793 } else if (unconf) {
1794 struct nfs4_client *old;
1795 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1796 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1797 status = nfserr_clid_inuse;
1798 goto out_free_conn;
1799 }
1800 cs_slot = &unconf->cl_cs_slot;
1801 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1802 if (status) {
1803 /* an unconfirmed replay returns misordered */
1804 status = nfserr_seq_misordered;
1805 goto out_free_conn;
1806 }
1807 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
1808 if (old)
1809 expire_client(old);
1810 move_to_confirmed(unconf);
1811 conf = unconf;
1812 } else {
1813 status = nfserr_stale_clientid;
1814 goto out_free_conn;
1815 }
1816 status = nfs_ok;
1817 /*
1818 * We do not support RDMA or persistent sessions
1819 */
1820 cr_ses->flags &= ~SESSION4_PERSIST;
1821 cr_ses->flags &= ~SESSION4_RDMA;
1822
1823 init_session(rqstp, new, conf, cr_ses);
1824 nfsd4_init_conn(rqstp, conn, new);
1825
1826 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1827 NFS4_MAX_SESSIONID_LEN);
1828 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1829 sizeof(struct nfsd4_channel_attrs));
1830 cs_slot->sl_seqid++;
1831 cr_ses->seqid = cs_slot->sl_seqid;
1832
1833 /* cache solo and embedded create sessions under the state lock */
1834 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1835 nfs4_unlock_state();
1836 out:
1837 dprintk("%s returns %d\n", __func__, ntohl(status));
1838 return status;
1839 out_free_conn:
1840 nfs4_unlock_state();
1841 free_conn(conn);
1842 out_free_session:
1843 __free_session(new);
1844 goto out;
1845 }
1846
1847 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1848 {
1849 switch (*dir) {
1850 case NFS4_CDFC4_FORE:
1851 case NFS4_CDFC4_BACK:
1852 return nfs_ok;
1853 case NFS4_CDFC4_FORE_OR_BOTH:
1854 case NFS4_CDFC4_BACK_OR_BOTH:
1855 *dir = NFS4_CDFC4_BOTH;
1856 return nfs_ok;
1857 };
1858 return nfserr_inval;
1859 }
1860
1861 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
1862 {
1863 struct nfsd4_session *session = cstate->session;
1864 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1865
1866 spin_lock(&nn->client_lock);
1867 session->se_cb_prog = bc->bc_cb_program;
1868 session->se_cb_sec = bc->bc_cb_sec;
1869 spin_unlock(&nn->client_lock);
1870
1871 nfsd4_probe_callback(session->se_client);
1872
1873 return nfs_ok;
1874 }
1875
1876 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1877 struct nfsd4_compound_state *cstate,
1878 struct nfsd4_bind_conn_to_session *bcts)
1879 {
1880 __be32 status;
1881 struct nfsd4_conn *conn;
1882 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1883
1884 if (!nfsd4_last_compound_op(rqstp))
1885 return nfserr_not_only_op;
1886 spin_lock(&nn->client_lock);
1887 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
1888 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1889 * client_lock iself: */
1890 if (cstate->session) {
1891 nfsd4_get_session(cstate->session);
1892 atomic_inc(&cstate->session->se_client->cl_refcount);
1893 }
1894 spin_unlock(&nn->client_lock);
1895 if (!cstate->session)
1896 return nfserr_badsession;
1897
1898 status = nfsd4_map_bcts_dir(&bcts->dir);
1899 if (status)
1900 return status;
1901 conn = alloc_conn(rqstp, bcts->dir);
1902 if (!conn)
1903 return nfserr_jukebox;
1904 nfsd4_init_conn(rqstp, conn, cstate->session);
1905 return nfs_ok;
1906 }
1907
1908 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1909 {
1910 if (!session)
1911 return 0;
1912 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1913 }
1914
1915 __be32
1916 nfsd4_destroy_session(struct svc_rqst *r,
1917 struct nfsd4_compound_state *cstate,
1918 struct nfsd4_destroy_session *sessionid)
1919 {
1920 struct nfsd4_session *ses;
1921 __be32 status = nfserr_badsession;
1922 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
1923
1924 /* Notes:
1925 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1926 * - Should we return nfserr_back_chan_busy if waiting for
1927 * callbacks on to-be-destroyed session?
1928 * - Do we need to clear any callback info from previous session?
1929 */
1930
1931 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1932 if (!nfsd4_last_compound_op(r))
1933 return nfserr_not_only_op;
1934 }
1935 dump_sessionid(__func__, &sessionid->sessionid);
1936 spin_lock(&nn->client_lock);
1937 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
1938 if (!ses) {
1939 spin_unlock(&nn->client_lock);
1940 goto out;
1941 }
1942
1943 unhash_session(ses);
1944 spin_unlock(&nn->client_lock);
1945
1946 nfs4_lock_state();
1947 nfsd4_probe_callback_sync(ses->se_client);
1948 nfs4_unlock_state();
1949
1950 spin_lock(&nn->client_lock);
1951 nfsd4_del_conns(ses);
1952 nfsd4_put_session_locked(ses);
1953 spin_unlock(&nn->client_lock);
1954 status = nfs_ok;
1955 out:
1956 dprintk("%s returns %d\n", __func__, ntohl(status));
1957 return status;
1958 }
1959
1960 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1961 {
1962 struct nfsd4_conn *c;
1963
1964 list_for_each_entry(c, &s->se_conns, cn_persession) {
1965 if (c->cn_xprt == xpt) {
1966 return c;
1967 }
1968 }
1969 return NULL;
1970 }
1971
1972 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1973 {
1974 struct nfs4_client *clp = ses->se_client;
1975 struct nfsd4_conn *c;
1976 int ret;
1977
1978 spin_lock(&clp->cl_lock);
1979 c = __nfsd4_find_conn(new->cn_xprt, ses);
1980 if (c) {
1981 spin_unlock(&clp->cl_lock);
1982 free_conn(new);
1983 return;
1984 }
1985 __nfsd4_hash_conn(new, ses);
1986 spin_unlock(&clp->cl_lock);
1987 ret = nfsd4_register_conn(new);
1988 if (ret)
1989 /* oops; xprt is already down: */
1990 nfsd4_conn_lost(&new->cn_xpt_user);
1991 return;
1992 }
1993
1994 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1995 {
1996 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1997
1998 return args->opcnt > session->se_fchannel.maxops;
1999 }
2000
2001 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2002 struct nfsd4_session *session)
2003 {
2004 struct xdr_buf *xb = &rqstp->rq_arg;
2005
2006 return xb->len > session->se_fchannel.maxreq_sz;
2007 }
2008
2009 __be32
2010 nfsd4_sequence(struct svc_rqst *rqstp,
2011 struct nfsd4_compound_state *cstate,
2012 struct nfsd4_sequence *seq)
2013 {
2014 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2015 struct nfsd4_session *session;
2016 struct nfsd4_slot *slot;
2017 struct nfsd4_conn *conn;
2018 __be32 status;
2019 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2020
2021 if (resp->opcnt != 1)
2022 return nfserr_sequence_pos;
2023
2024 /*
2025 * Will be either used or freed by nfsd4_sequence_check_conn
2026 * below.
2027 */
2028 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2029 if (!conn)
2030 return nfserr_jukebox;
2031
2032 spin_lock(&nn->client_lock);
2033 status = nfserr_badsession;
2034 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
2035 if (!session)
2036 goto out;
2037
2038 status = nfserr_too_many_ops;
2039 if (nfsd4_session_too_many_ops(rqstp, session))
2040 goto out;
2041
2042 status = nfserr_req_too_big;
2043 if (nfsd4_request_too_big(rqstp, session))
2044 goto out;
2045
2046 status = nfserr_badslot;
2047 if (seq->slotid >= session->se_fchannel.maxreqs)
2048 goto out;
2049
2050 slot = session->se_slots[seq->slotid];
2051 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2052
2053 /* We do not negotiate the number of slots yet, so set the
2054 * maxslots to the session maxreqs which is used to encode
2055 * sr_highest_slotid and the sr_target_slot id to maxslots */
2056 seq->maxslots = session->se_fchannel.maxreqs;
2057
2058 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2059 slot->sl_flags & NFSD4_SLOT_INUSE);
2060 if (status == nfserr_replay_cache) {
2061 status = nfserr_seq_misordered;
2062 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2063 goto out;
2064 cstate->slot = slot;
2065 cstate->session = session;
2066 /* Return the cached reply status and set cstate->status
2067 * for nfsd4_proc_compound processing */
2068 status = nfsd4_replay_cache_entry(resp, seq);
2069 cstate->status = nfserr_replay_cache;
2070 goto out;
2071 }
2072 if (status)
2073 goto out;
2074
2075 nfsd4_sequence_check_conn(conn, session);
2076 conn = NULL;
2077
2078 /* Success! bump slot seqid */
2079 slot->sl_seqid = seq->seqid;
2080 slot->sl_flags |= NFSD4_SLOT_INUSE;
2081 if (seq->cachethis)
2082 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2083 else
2084 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2085
2086 cstate->slot = slot;
2087 cstate->session = session;
2088
2089 out:
2090 /* Hold a session reference until done processing the compound. */
2091 if (cstate->session) {
2092 struct nfs4_client *clp = session->se_client;
2093
2094 nfsd4_get_session(cstate->session);
2095 atomic_inc(&clp->cl_refcount);
2096 switch (clp->cl_cb_state) {
2097 case NFSD4_CB_DOWN:
2098 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2099 break;
2100 case NFSD4_CB_FAULT:
2101 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2102 break;
2103 default:
2104 seq->status_flags = 0;
2105 }
2106 }
2107 kfree(conn);
2108 spin_unlock(&nn->client_lock);
2109 dprintk("%s: return %d\n", __func__, ntohl(status));
2110 return status;
2111 }
2112
2113 __be32
2114 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2115 {
2116 struct nfs4_client *conf, *unconf, *clp;
2117 __be32 status = 0;
2118 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2119
2120 nfs4_lock_state();
2121 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2122 conf = find_confirmed_client(&dc->clientid, true, nn);
2123
2124 if (conf) {
2125 clp = conf;
2126
2127 if (!is_client_expired(conf) && client_has_state(conf)) {
2128 status = nfserr_clientid_busy;
2129 goto out;
2130 }
2131
2132 /* rfc5661 18.50.3 */
2133 if (cstate->session && conf == cstate->session->se_client) {
2134 status = nfserr_clientid_busy;
2135 goto out;
2136 }
2137 } else if (unconf)
2138 clp = unconf;
2139 else {
2140 status = nfserr_stale_clientid;
2141 goto out;
2142 }
2143
2144 expire_client(clp);
2145 out:
2146 nfs4_unlock_state();
2147 dprintk("%s return %d\n", __func__, ntohl(status));
2148 return status;
2149 }
2150
2151 __be32
2152 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2153 {
2154 __be32 status = 0;
2155
2156 if (rc->rca_one_fs) {
2157 if (!cstate->current_fh.fh_dentry)
2158 return nfserr_nofilehandle;
2159 /*
2160 * We don't take advantage of the rca_one_fs case.
2161 * That's OK, it's optional, we can safely ignore it.
2162 */
2163 return nfs_ok;
2164 }
2165
2166 nfs4_lock_state();
2167 status = nfserr_complete_already;
2168 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2169 &cstate->session->se_client->cl_flags))
2170 goto out;
2171
2172 status = nfserr_stale_clientid;
2173 if (is_client_expired(cstate->session->se_client))
2174 /*
2175 * The following error isn't really legal.
2176 * But we only get here if the client just explicitly
2177 * destroyed the client. Surely it no longer cares what
2178 * error it gets back on an operation for the dead
2179 * client.
2180 */
2181 goto out;
2182
2183 status = nfs_ok;
2184 nfsd4_client_record_create(cstate->session->se_client);
2185 out:
2186 nfs4_unlock_state();
2187 return status;
2188 }
2189
2190 __be32
2191 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2192 struct nfsd4_setclientid *setclid)
2193 {
2194 struct xdr_netobj clname = setclid->se_name;
2195 nfs4_verifier clverifier = setclid->se_verf;
2196 struct nfs4_client *conf, *unconf, *new;
2197 __be32 status;
2198 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2199
2200 /* Cases below refer to rfc 3530 section 14.2.33: */
2201 nfs4_lock_state();
2202 conf = find_confirmed_client_by_name(&clname, nn);
2203 if (conf) {
2204 /* case 0: */
2205 status = nfserr_clid_inuse;
2206 if (clp_used_exchangeid(conf))
2207 goto out;
2208 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2209 char addr_str[INET6_ADDRSTRLEN];
2210 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2211 sizeof(addr_str));
2212 dprintk("NFSD: setclientid: string in use by client "
2213 "at %s\n", addr_str);
2214 goto out;
2215 }
2216 }
2217 unconf = find_unconfirmed_client_by_name(&clname, nn);
2218 if (unconf)
2219 expire_client(unconf);
2220 status = nfserr_jukebox;
2221 new = create_client(clname, rqstp, &clverifier);
2222 if (new == NULL)
2223 goto out;
2224 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2225 /* case 1: probable callback update */
2226 copy_clid(new, conf);
2227 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2228 gen_clid(new, nn);
2229 new->cl_minorversion = 0;
2230 gen_callback(new, setclid, rqstp);
2231 add_to_unconfirmed(new);
2232 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2233 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2234 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2235 status = nfs_ok;
2236 out:
2237 nfs4_unlock_state();
2238 return status;
2239 }
2240
2241
2242 __be32
2243 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2244 struct nfsd4_compound_state *cstate,
2245 struct nfsd4_setclientid_confirm *setclientid_confirm)
2246 {
2247 struct nfs4_client *conf, *unconf;
2248 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2249 clientid_t * clid = &setclientid_confirm->sc_clientid;
2250 __be32 status;
2251 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2252
2253 if (STALE_CLIENTID(clid, nn))
2254 return nfserr_stale_clientid;
2255 nfs4_lock_state();
2256
2257 conf = find_confirmed_client(clid, false, nn);
2258 unconf = find_unconfirmed_client(clid, false, nn);
2259 /*
2260 * We try hard to give out unique clientid's, so if we get an
2261 * attempt to confirm the same clientid with a different cred,
2262 * there's a bug somewhere. Let's charitably assume it's our
2263 * bug.
2264 */
2265 status = nfserr_serverfault;
2266 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2267 goto out;
2268 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2269 goto out;
2270 /* cases below refer to rfc 3530 section 14.2.34: */
2271 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2272 if (conf && !unconf) /* case 2: probable retransmit */
2273 status = nfs_ok;
2274 else /* case 4: client hasn't noticed we rebooted yet? */
2275 status = nfserr_stale_clientid;
2276 goto out;
2277 }
2278 status = nfs_ok;
2279 if (conf) { /* case 1: callback update */
2280 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2281 nfsd4_probe_callback(conf);
2282 expire_client(unconf);
2283 } else { /* case 3: normal case; new or rebooted client */
2284 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2285 if (conf)
2286 expire_client(conf);
2287 move_to_confirmed(unconf);
2288 nfsd4_probe_callback(unconf);
2289 }
2290 out:
2291 nfs4_unlock_state();
2292 return status;
2293 }
2294
2295 static struct nfs4_file *nfsd4_alloc_file(void)
2296 {
2297 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2298 }
2299
2300 /* OPEN Share state helper functions */
2301 static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2302 {
2303 unsigned int hashval = file_hashval(ino);
2304
2305 atomic_set(&fp->fi_ref, 1);
2306 INIT_LIST_HEAD(&fp->fi_hash);
2307 INIT_LIST_HEAD(&fp->fi_stateids);
2308 INIT_LIST_HEAD(&fp->fi_delegations);
2309 fp->fi_inode = igrab(ino);
2310 fp->fi_had_conflict = false;
2311 fp->fi_lease = NULL;
2312 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2313 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2314 spin_lock(&recall_lock);
2315 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2316 spin_unlock(&recall_lock);
2317 }
2318
2319 static void
2320 nfsd4_free_slab(struct kmem_cache **slab)
2321 {
2322 if (*slab == NULL)
2323 return;
2324 kmem_cache_destroy(*slab);
2325 *slab = NULL;
2326 }
2327
2328 void
2329 nfsd4_free_slabs(void)
2330 {
2331 nfsd4_free_slab(&openowner_slab);
2332 nfsd4_free_slab(&lockowner_slab);
2333 nfsd4_free_slab(&file_slab);
2334 nfsd4_free_slab(&stateid_slab);
2335 nfsd4_free_slab(&deleg_slab);
2336 }
2337
2338 int
2339 nfsd4_init_slabs(void)
2340 {
2341 openowner_slab = kmem_cache_create("nfsd4_openowners",
2342 sizeof(struct nfs4_openowner), 0, 0, NULL);
2343 if (openowner_slab == NULL)
2344 goto out_nomem;
2345 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2346 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2347 if (lockowner_slab == NULL)
2348 goto out_nomem;
2349 file_slab = kmem_cache_create("nfsd4_files",
2350 sizeof(struct nfs4_file), 0, 0, NULL);
2351 if (file_slab == NULL)
2352 goto out_nomem;
2353 stateid_slab = kmem_cache_create("nfsd4_stateids",
2354 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2355 if (stateid_slab == NULL)
2356 goto out_nomem;
2357 deleg_slab = kmem_cache_create("nfsd4_delegations",
2358 sizeof(struct nfs4_delegation), 0, 0, NULL);
2359 if (deleg_slab == NULL)
2360 goto out_nomem;
2361 return 0;
2362 out_nomem:
2363 nfsd4_free_slabs();
2364 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2365 return -ENOMEM;
2366 }
2367
2368 void nfs4_free_openowner(struct nfs4_openowner *oo)
2369 {
2370 kfree(oo->oo_owner.so_owner.data);
2371 kmem_cache_free(openowner_slab, oo);
2372 }
2373
2374 void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2375 {
2376 kfree(lo->lo_owner.so_owner.data);
2377 kmem_cache_free(lockowner_slab, lo);
2378 }
2379
2380 static void init_nfs4_replay(struct nfs4_replay *rp)
2381 {
2382 rp->rp_status = nfserr_serverfault;
2383 rp->rp_buflen = 0;
2384 rp->rp_buf = rp->rp_ibuf;
2385 }
2386
2387 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2388 {
2389 struct nfs4_stateowner *sop;
2390
2391 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2392 if (!sop)
2393 return NULL;
2394
2395 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2396 if (!sop->so_owner.data) {
2397 kmem_cache_free(slab, sop);
2398 return NULL;
2399 }
2400 sop->so_owner.len = owner->len;
2401
2402 INIT_LIST_HEAD(&sop->so_stateids);
2403 sop->so_client = clp;
2404 init_nfs4_replay(&sop->so_replay);
2405 return sop;
2406 }
2407
2408 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2409 {
2410 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2411
2412 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
2413 list_add(&oo->oo_perclient, &clp->cl_openowners);
2414 }
2415
2416 static struct nfs4_openowner *
2417 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2418 struct nfs4_openowner *oo;
2419
2420 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2421 if (!oo)
2422 return NULL;
2423 oo->oo_owner.so_is_open_owner = 1;
2424 oo->oo_owner.so_seqid = open->op_seqid;
2425 oo->oo_flags = NFS4_OO_NEW;
2426 oo->oo_time = 0;
2427 oo->oo_last_closed_stid = NULL;
2428 INIT_LIST_HEAD(&oo->oo_close_lru);
2429 hash_openowner(oo, clp, strhashval);
2430 return oo;
2431 }
2432
2433 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2434 struct nfs4_openowner *oo = open->op_openowner;
2435
2436 stp->st_stid.sc_type = NFS4_OPEN_STID;
2437 INIT_LIST_HEAD(&stp->st_lockowners);
2438 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2439 list_add(&stp->st_perfile, &fp->fi_stateids);
2440 stp->st_stateowner = &oo->oo_owner;
2441 get_nfs4_file(fp);
2442 stp->st_file = fp;
2443 stp->st_access_bmap = 0;
2444 stp->st_deny_bmap = 0;
2445 set_access(open->op_share_access, stp);
2446 set_deny(open->op_share_deny, stp);
2447 stp->st_openstp = NULL;
2448 }
2449
2450 static void
2451 move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
2452 {
2453 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2454
2455 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2456
2457 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
2458 oo->oo_time = get_seconds();
2459 }
2460
2461 static int
2462 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2463 clientid_t *clid)
2464 {
2465 return (sop->so_owner.len == owner->len) &&
2466 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2467 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2468 }
2469
2470 static struct nfs4_openowner *
2471 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2472 bool sessions, struct nfsd_net *nn)
2473 {
2474 struct nfs4_stateowner *so;
2475 struct nfs4_openowner *oo;
2476 struct nfs4_client *clp;
2477
2478 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
2479 if (!so->so_is_open_owner)
2480 continue;
2481 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2482 oo = openowner(so);
2483 clp = oo->oo_owner.so_client;
2484 if ((bool)clp->cl_minorversion != sessions)
2485 return NULL;
2486 renew_client(oo->oo_owner.so_client);
2487 return oo;
2488 }
2489 }
2490 return NULL;
2491 }
2492
2493 /* search file_hashtbl[] for file */
2494 static struct nfs4_file *
2495 find_file(struct inode *ino)
2496 {
2497 unsigned int hashval = file_hashval(ino);
2498 struct nfs4_file *fp;
2499
2500 spin_lock(&recall_lock);
2501 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2502 if (fp->fi_inode == ino) {
2503 get_nfs4_file(fp);
2504 spin_unlock(&recall_lock);
2505 return fp;
2506 }
2507 }
2508 spin_unlock(&recall_lock);
2509 return NULL;
2510 }
2511
2512 /*
2513 * Called to check deny when READ with all zero stateid or
2514 * WRITE with all zero or all one stateid
2515 */
2516 static __be32
2517 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2518 {
2519 struct inode *ino = current_fh->fh_dentry->d_inode;
2520 struct nfs4_file *fp;
2521 struct nfs4_ol_stateid *stp;
2522 __be32 ret;
2523
2524 dprintk("NFSD: nfs4_share_conflict\n");
2525
2526 fp = find_file(ino);
2527 if (!fp)
2528 return nfs_ok;
2529 ret = nfserr_locked;
2530 /* Search for conflicting share reservations */
2531 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2532 if (test_deny(deny_type, stp) ||
2533 test_deny(NFS4_SHARE_DENY_BOTH, stp))
2534 goto out;
2535 }
2536 ret = nfs_ok;
2537 out:
2538 put_nfs4_file(fp);
2539 return ret;
2540 }
2541
2542 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2543 {
2544 /* We're assuming the state code never drops its reference
2545 * without first removing the lease. Since we're in this lease
2546 * callback (and since the lease code is serialized by the kernel
2547 * lock) we know the server hasn't removed the lease yet, we know
2548 * it's safe to take a reference: */
2549 atomic_inc(&dp->dl_count);
2550
2551 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2552
2553 /* only place dl_time is set. protected by lock_flocks*/
2554 dp->dl_time = get_seconds();
2555
2556 nfsd4_cb_recall(dp);
2557 }
2558
2559 /* Called from break_lease() with lock_flocks() held. */
2560 static void nfsd_break_deleg_cb(struct file_lock *fl)
2561 {
2562 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2563 struct nfs4_delegation *dp;
2564
2565 if (!fp) {
2566 WARN(1, "(%p)->fl_owner NULL\n", fl);
2567 return;
2568 }
2569 if (fp->fi_had_conflict) {
2570 WARN(1, "duplicate break on %p\n", fp);
2571 return;
2572 }
2573 /*
2574 * We don't want the locks code to timeout the lease for us;
2575 * we'll remove it ourself if a delegation isn't returned
2576 * in time:
2577 */
2578 fl->fl_break_time = 0;
2579
2580 spin_lock(&recall_lock);
2581 fp->fi_had_conflict = true;
2582 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2583 nfsd_break_one_deleg(dp);
2584 spin_unlock(&recall_lock);
2585 }
2586
2587 static
2588 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2589 {
2590 if (arg & F_UNLCK)
2591 return lease_modify(onlist, arg);
2592 else
2593 return -EAGAIN;
2594 }
2595
2596 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2597 .lm_break = nfsd_break_deleg_cb,
2598 .lm_change = nfsd_change_deleg_cb,
2599 };
2600
2601 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2602 {
2603 if (nfsd4_has_session(cstate))
2604 return nfs_ok;
2605 if (seqid == so->so_seqid - 1)
2606 return nfserr_replay_me;
2607 if (seqid == so->so_seqid)
2608 return nfs_ok;
2609 return nfserr_bad_seqid;
2610 }
2611
2612 __be32
2613 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2614 struct nfsd4_open *open, struct nfsd_net *nn)
2615 {
2616 clientid_t *clientid = &open->op_clientid;
2617 struct nfs4_client *clp = NULL;
2618 unsigned int strhashval;
2619 struct nfs4_openowner *oo = NULL;
2620 __be32 status;
2621
2622 if (STALE_CLIENTID(&open->op_clientid, nn))
2623 return nfserr_stale_clientid;
2624 /*
2625 * In case we need it later, after we've already created the
2626 * file and don't want to risk a further failure:
2627 */
2628 open->op_file = nfsd4_alloc_file();
2629 if (open->op_file == NULL)
2630 return nfserr_jukebox;
2631
2632 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
2633 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
2634 open->op_openowner = oo;
2635 if (!oo) {
2636 clp = find_confirmed_client(clientid, cstate->minorversion,
2637 nn);
2638 if (clp == NULL)
2639 return nfserr_expired;
2640 goto new_owner;
2641 }
2642 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
2643 /* Replace unconfirmed owners without checking for replay. */
2644 clp = oo->oo_owner.so_client;
2645 release_openowner(oo);
2646 open->op_openowner = NULL;
2647 goto new_owner;
2648 }
2649 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2650 if (status)
2651 return status;
2652 clp = oo->oo_owner.so_client;
2653 goto alloc_stateid;
2654 new_owner:
2655 oo = alloc_init_open_stateowner(strhashval, clp, open);
2656 if (oo == NULL)
2657 return nfserr_jukebox;
2658 open->op_openowner = oo;
2659 alloc_stateid:
2660 open->op_stp = nfs4_alloc_stateid(clp);
2661 if (!open->op_stp)
2662 return nfserr_jukebox;
2663 return nfs_ok;
2664 }
2665
2666 static inline __be32
2667 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2668 {
2669 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2670 return nfserr_openmode;
2671 else
2672 return nfs_ok;
2673 }
2674
2675 static int share_access_to_flags(u32 share_access)
2676 {
2677 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2678 }
2679
2680 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
2681 {
2682 struct nfs4_stid *ret;
2683
2684 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
2685 if (!ret)
2686 return NULL;
2687 return delegstateid(ret);
2688 }
2689
2690 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2691 {
2692 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2693 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2694 }
2695
2696 static __be32
2697 nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
2698 struct nfs4_delegation **dp)
2699 {
2700 int flags;
2701 __be32 status = nfserr_bad_stateid;
2702
2703 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
2704 if (*dp == NULL)
2705 goto out;
2706 flags = share_access_to_flags(open->op_share_access);
2707 status = nfs4_check_delegmode(*dp, flags);
2708 if (status)
2709 *dp = NULL;
2710 out:
2711 if (!nfsd4_is_deleg_cur(open))
2712 return nfs_ok;
2713 if (status)
2714 return status;
2715 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2716 return nfs_ok;
2717 }
2718
2719 static __be32
2720 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
2721 {
2722 struct nfs4_ol_stateid *local;
2723 struct nfs4_openowner *oo = open->op_openowner;
2724
2725 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2726 /* ignore lock owners */
2727 if (local->st_stateowner->so_is_open_owner == 0)
2728 continue;
2729 /* remember if we have seen this open owner */
2730 if (local->st_stateowner == &oo->oo_owner)
2731 *stpp = local;
2732 /* check for conflicting share reservations */
2733 if (!test_share(local, open))
2734 return nfserr_share_denied;
2735 }
2736 return nfs_ok;
2737 }
2738
2739 static inline int nfs4_access_to_access(u32 nfs4_access)
2740 {
2741 int flags = 0;
2742
2743 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2744 flags |= NFSD_MAY_READ;
2745 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2746 flags |= NFSD_MAY_WRITE;
2747 return flags;
2748 }
2749
2750 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2751 struct svc_fh *cur_fh, struct nfsd4_open *open)
2752 {
2753 __be32 status;
2754 int oflag = nfs4_access_to_omode(open->op_share_access);
2755 int access = nfs4_access_to_access(open->op_share_access);
2756
2757 if (!fp->fi_fds[oflag]) {
2758 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2759 &fp->fi_fds[oflag]);
2760 if (status)
2761 return status;
2762 }
2763 nfs4_file_get_access(fp, oflag);
2764
2765 return nfs_ok;
2766 }
2767
2768 static inline __be32
2769 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2770 struct nfsd4_open *open)
2771 {
2772 struct iattr iattr = {
2773 .ia_valid = ATTR_SIZE,
2774 .ia_size = 0,
2775 };
2776 if (!open->op_truncate)
2777 return 0;
2778 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2779 return nfserr_inval;
2780 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2781 }
2782
2783 static __be32
2784 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
2785 {
2786 u32 op_share_access = open->op_share_access;
2787 bool new_access;
2788 __be32 status;
2789
2790 new_access = !test_access(op_share_access, stp);
2791 if (new_access) {
2792 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2793 if (status)
2794 return status;
2795 }
2796 status = nfsd4_truncate(rqstp, cur_fh, open);
2797 if (status) {
2798 if (new_access) {
2799 int oflag = nfs4_access_to_omode(op_share_access);
2800 nfs4_file_put_access(fp, oflag);
2801 }
2802 return status;
2803 }
2804 /* remember the open */
2805 set_access(op_share_access, stp);
2806 set_deny(open->op_share_deny, stp);
2807
2808 return nfs_ok;
2809 }
2810
2811
2812 static void
2813 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
2814 {
2815 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2816 }
2817
2818 /* Should we give out recallable state?: */
2819 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2820 {
2821 if (clp->cl_cb_state == NFSD4_CB_UP)
2822 return true;
2823 /*
2824 * In the sessions case, since we don't have to establish a
2825 * separate connection for callbacks, we assume it's OK
2826 * until we hear otherwise:
2827 */
2828 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2829 }
2830
2831 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2832 {
2833 struct file_lock *fl;
2834
2835 fl = locks_alloc_lock();
2836 if (!fl)
2837 return NULL;
2838 locks_init_lock(fl);
2839 fl->fl_lmops = &nfsd_lease_mng_ops;
2840 fl->fl_flags = FL_LEASE;
2841 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2842 fl->fl_end = OFFSET_MAX;
2843 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2844 fl->fl_pid = current->tgid;
2845 return fl;
2846 }
2847
2848 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2849 {
2850 struct nfs4_file *fp = dp->dl_file;
2851 struct file_lock *fl;
2852 int status;
2853
2854 fl = nfs4_alloc_init_lease(dp, flag);
2855 if (!fl)
2856 return -ENOMEM;
2857 fl->fl_file = find_readable_file(fp);
2858 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
2859 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2860 if (status) {
2861 list_del_init(&dp->dl_perclnt);
2862 locks_free_lock(fl);
2863 return -ENOMEM;
2864 }
2865 fp->fi_lease = fl;
2866 fp->fi_deleg_file = get_file(fl->fl_file);
2867 atomic_set(&fp->fi_delegees, 1);
2868 list_add(&dp->dl_perfile, &fp->fi_delegations);
2869 return 0;
2870 }
2871
2872 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2873 {
2874 struct nfs4_file *fp = dp->dl_file;
2875
2876 if (!fp->fi_lease)
2877 return nfs4_setlease(dp, flag);
2878 spin_lock(&recall_lock);
2879 if (fp->fi_had_conflict) {
2880 spin_unlock(&recall_lock);
2881 return -EAGAIN;
2882 }
2883 atomic_inc(&fp->fi_delegees);
2884 list_add(&dp->dl_perfile, &fp->fi_delegations);
2885 spin_unlock(&recall_lock);
2886 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
2887 return 0;
2888 }
2889
2890 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2891 {
2892 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2893 if (status == -EAGAIN)
2894 open->op_why_no_deleg = WND4_CONTENTION;
2895 else {
2896 open->op_why_no_deleg = WND4_RESOURCE;
2897 switch (open->op_deleg_want) {
2898 case NFS4_SHARE_WANT_READ_DELEG:
2899 case NFS4_SHARE_WANT_WRITE_DELEG:
2900 case NFS4_SHARE_WANT_ANY_DELEG:
2901 break;
2902 case NFS4_SHARE_WANT_CANCEL:
2903 open->op_why_no_deleg = WND4_CANCELLED;
2904 break;
2905 case NFS4_SHARE_WANT_NO_DELEG:
2906 WARN_ON_ONCE(1);
2907 }
2908 }
2909 }
2910
2911 /*
2912 * Attempt to hand out a delegation.
2913 */
2914 static void
2915 nfs4_open_delegation(struct net *net, struct svc_fh *fh,
2916 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
2917 {
2918 struct nfs4_delegation *dp;
2919 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
2920 int cb_up;
2921 int status = 0, flag = 0;
2922
2923 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
2924 flag = NFS4_OPEN_DELEGATE_NONE;
2925 open->op_recall = 0;
2926 switch (open->op_claim_type) {
2927 case NFS4_OPEN_CLAIM_PREVIOUS:
2928 if (!cb_up)
2929 open->op_recall = 1;
2930 flag = open->op_delegate_type;
2931 if (flag == NFS4_OPEN_DELEGATE_NONE)
2932 goto out;
2933 break;
2934 case NFS4_OPEN_CLAIM_NULL:
2935 /* Let's not give out any delegations till everyone's
2936 * had the chance to reclaim theirs.... */
2937 if (locks_in_grace(net))
2938 goto out;
2939 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
2940 goto out;
2941 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2942 flag = NFS4_OPEN_DELEGATE_WRITE;
2943 else
2944 flag = NFS4_OPEN_DELEGATE_READ;
2945 break;
2946 default:
2947 goto out;
2948 }
2949
2950 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
2951 if (dp == NULL)
2952 goto out_no_deleg;
2953 status = nfs4_set_delegation(dp, flag);
2954 if (status)
2955 goto out_free;
2956
2957 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
2958
2959 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2960 STATEID_VAL(&dp->dl_stid.sc_stateid));
2961 out:
2962 open->op_delegate_type = flag;
2963 if (flag == NFS4_OPEN_DELEGATE_NONE) {
2964 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
2965 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2966 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2967
2968 /* 4.1 client asking for a delegation? */
2969 if (open->op_deleg_want)
2970 nfsd4_open_deleg_none_ext(open, status);
2971 }
2972 return;
2973 out_free:
2974 unhash_stid(&dp->dl_stid);
2975 nfs4_put_delegation(dp);
2976 out_no_deleg:
2977 flag = NFS4_OPEN_DELEGATE_NONE;
2978 goto out;
2979 }
2980
2981 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
2982 struct nfs4_delegation *dp)
2983 {
2984 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
2985 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
2986 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2987 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
2988 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
2989 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
2990 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2991 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
2992 }
2993 /* Otherwise the client must be confused wanting a delegation
2994 * it already has, therefore we don't return
2995 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
2996 */
2997 }
2998
2999 /*
3000 * called with nfs4_lock_state() held.
3001 */
3002 __be32
3003 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3004 {
3005 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3006 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3007 struct nfs4_file *fp = NULL;
3008 struct inode *ino = current_fh->fh_dentry->d_inode;
3009 struct nfs4_ol_stateid *stp = NULL;
3010 struct nfs4_delegation *dp = NULL;
3011 __be32 status;
3012
3013 /*
3014 * Lookup file; if found, lookup stateid and check open request,
3015 * and check for delegations in the process of being recalled.
3016 * If not found, create the nfs4_file struct
3017 */
3018 fp = find_file(ino);
3019 if (fp) {
3020 if ((status = nfs4_check_open(fp, open, &stp)))
3021 goto out;
3022 status = nfs4_check_deleg(cl, fp, open, &dp);
3023 if (status)
3024 goto out;
3025 } else {
3026 status = nfserr_bad_stateid;
3027 if (nfsd4_is_deleg_cur(open))
3028 goto out;
3029 status = nfserr_jukebox;
3030 fp = open->op_file;
3031 open->op_file = NULL;
3032 nfsd4_init_file(fp, ino);
3033 }
3034
3035 /*
3036 * OPEN the file, or upgrade an existing OPEN.
3037 * If truncate fails, the OPEN fails.
3038 */
3039 if (stp) {
3040 /* Stateid was found, this is an OPEN upgrade */
3041 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3042 if (status)
3043 goto out;
3044 } else {
3045 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
3046 if (status)
3047 goto out;
3048 status = nfsd4_truncate(rqstp, current_fh, open);
3049 if (status)
3050 goto out;
3051 stp = open->op_stp;
3052 open->op_stp = NULL;
3053 init_open_stateid(stp, fp, open);
3054 }
3055 update_stateid(&stp->st_stid.sc_stateid);
3056 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3057
3058 if (nfsd4_has_session(&resp->cstate)) {
3059 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3060
3061 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3062 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3063 open->op_why_no_deleg = WND4_NOT_WANTED;
3064 goto nodeleg;
3065 }
3066 }
3067
3068 /*
3069 * Attempt to hand out a delegation. No error return, because the
3070 * OPEN succeeds even if we fail.
3071 */
3072 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
3073 nodeleg:
3074 status = nfs_ok;
3075
3076 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3077 STATEID_VAL(&stp->st_stid.sc_stateid));
3078 out:
3079 /* 4.1 client trying to upgrade/downgrade delegation? */
3080 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3081 open->op_deleg_want)
3082 nfsd4_deleg_xgrade_none_ext(open, dp);
3083
3084 if (fp)
3085 put_nfs4_file(fp);
3086 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3087 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3088 /*
3089 * To finish the open response, we just need to set the rflags.
3090 */
3091 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3092 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3093 !nfsd4_has_session(&resp->cstate))
3094 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3095
3096 return status;
3097 }
3098
3099 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3100 {
3101 if (open->op_openowner) {
3102 struct nfs4_openowner *oo = open->op_openowner;
3103
3104 if (!list_empty(&oo->oo_owner.so_stateids))
3105 list_del_init(&oo->oo_close_lru);
3106 if (oo->oo_flags & NFS4_OO_NEW) {
3107 if (status) {
3108 release_openowner(oo);
3109 open->op_openowner = NULL;
3110 } else
3111 oo->oo_flags &= ~NFS4_OO_NEW;
3112 }
3113 }
3114 if (open->op_file)
3115 nfsd4_free_file(open->op_file);
3116 if (open->op_stp)
3117 free_generic_stateid(open->op_stp);
3118 }
3119
3120 static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
3121 {
3122 struct nfs4_client *found;
3123
3124 if (STALE_CLIENTID(clid, nn))
3125 return nfserr_stale_clientid;
3126 found = find_confirmed_client(clid, session, nn);
3127 if (clp)
3128 *clp = found;
3129 return found ? nfs_ok : nfserr_expired;
3130 }
3131
3132 __be32
3133 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3134 clientid_t *clid)
3135 {
3136 struct nfs4_client *clp;
3137 __be32 status;
3138 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3139
3140 nfs4_lock_state();
3141 dprintk("process_renew(%08x/%08x): starting\n",
3142 clid->cl_boot, clid->cl_id);
3143 status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
3144 if (status)
3145 goto out;
3146 status = nfserr_cb_path_down;
3147 if (!list_empty(&clp->cl_delegations)
3148 && clp->cl_cb_state != NFSD4_CB_UP)
3149 goto out;
3150 status = nfs_ok;
3151 out:
3152 nfs4_unlock_state();
3153 return status;
3154 }
3155
3156 static void
3157 nfsd4_end_grace(struct nfsd_net *nn)
3158 {
3159 /* do nothing if grace period already ended */
3160 if (nn->grace_ended)
3161 return;
3162
3163 dprintk("NFSD: end of grace period\n");
3164 nn->grace_ended = true;
3165 nfsd4_record_grace_done(nn, nn->boot_time);
3166 locks_end_grace(&nn->nfsd4_manager);
3167 /*
3168 * Now that every NFSv4 client has had the chance to recover and
3169 * to see the (possibly new, possibly shorter) lease time, we
3170 * can safely set the next grace time to the current lease time:
3171 */
3172 nn->nfsd4_grace = nn->nfsd4_lease;
3173 }
3174
3175 static time_t
3176 nfs4_laundromat(struct nfsd_net *nn)
3177 {
3178 struct nfs4_client *clp;
3179 struct nfs4_openowner *oo;
3180 struct nfs4_delegation *dp;
3181 struct list_head *pos, *next, reaplist;
3182 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3183 time_t t, clientid_val = nn->nfsd4_lease;
3184 time_t u, test_val = nn->nfsd4_lease;
3185
3186 nfs4_lock_state();
3187
3188 dprintk("NFSD: laundromat service - starting\n");
3189 nfsd4_end_grace(nn);
3190 INIT_LIST_HEAD(&reaplist);
3191 spin_lock(&nn->client_lock);
3192 list_for_each_safe(pos, next, &nn->client_lru) {
3193 clp = list_entry(pos, struct nfs4_client, cl_lru);
3194 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3195 t = clp->cl_time - cutoff;
3196 if (clientid_val > t)
3197 clientid_val = t;
3198 break;
3199 }
3200 if (atomic_read(&clp->cl_refcount)) {
3201 dprintk("NFSD: client in use (clientid %08x)\n",
3202 clp->cl_clientid.cl_id);
3203 continue;
3204 }
3205 unhash_client_locked(clp);
3206 list_add(&clp->cl_lru, &reaplist);
3207 }
3208 spin_unlock(&nn->client_lock);
3209 list_for_each_safe(pos, next, &reaplist) {
3210 clp = list_entry(pos, struct nfs4_client, cl_lru);
3211 dprintk("NFSD: purging unused client (clientid %08x)\n",
3212 clp->cl_clientid.cl_id);
3213 expire_client(clp);
3214 }
3215 spin_lock(&recall_lock);
3216 list_for_each_safe(pos, next, &del_recall_lru) {
3217 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3218 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3219 continue;
3220 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3221 u = dp->dl_time - cutoff;
3222 if (test_val > u)
3223 test_val = u;
3224 break;
3225 }
3226 list_move(&dp->dl_recall_lru, &reaplist);
3227 }
3228 spin_unlock(&recall_lock);
3229 list_for_each_safe(pos, next, &reaplist) {
3230 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3231 unhash_delegation(dp);
3232 }
3233 test_val = nn->nfsd4_lease;
3234 list_for_each_safe(pos, next, &nn->close_lru) {
3235 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3236 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3237 u = oo->oo_time - cutoff;
3238 if (test_val > u)
3239 test_val = u;
3240 break;
3241 }
3242 release_openowner(oo);
3243 }
3244 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3245 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3246 nfs4_unlock_state();
3247 return clientid_val;
3248 }
3249
3250 static struct workqueue_struct *laundry_wq;
3251 static void laundromat_main(struct work_struct *);
3252
3253 static void
3254 laundromat_main(struct work_struct *laundry)
3255 {
3256 time_t t;
3257 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3258 work);
3259 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3260 laundromat_work);
3261
3262 t = nfs4_laundromat(nn);
3263 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3264 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
3265 }
3266
3267 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3268 {
3269 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3270 return nfserr_bad_stateid;
3271 return nfs_ok;
3272 }
3273
3274 static int
3275 STALE_STATEID(stateid_t *stateid, struct nfsd_net *nn)
3276 {
3277 if (stateid->si_opaque.so_clid.cl_boot == nn->boot_time)
3278 return 0;
3279 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
3280 STATEID_VAL(stateid));
3281 return 1;
3282 }
3283
3284 static inline int
3285 access_permit_read(struct nfs4_ol_stateid *stp)
3286 {
3287 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3288 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3289 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
3290 }
3291
3292 static inline int
3293 access_permit_write(struct nfs4_ol_stateid *stp)
3294 {
3295 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3296 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
3297 }
3298
3299 static
3300 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
3301 {
3302 __be32 status = nfserr_openmode;
3303
3304 /* For lock stateid's, we test the parent open, not the lock: */
3305 if (stp->st_openstp)
3306 stp = stp->st_openstp;
3307 if ((flags & WR_STATE) && !access_permit_write(stp))
3308 goto out;
3309 if ((flags & RD_STATE) && !access_permit_read(stp))
3310 goto out;
3311 status = nfs_ok;
3312 out:
3313 return status;
3314 }
3315
3316 static inline __be32
3317 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
3318 {
3319 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3320 return nfs_ok;
3321 else if (locks_in_grace(net)) {
3322 /* Answer in remaining cases depends on existence of
3323 * conflicting state; so we must wait out the grace period. */
3324 return nfserr_grace;
3325 } else if (flags & WR_STATE)
3326 return nfs4_share_conflict(current_fh,
3327 NFS4_SHARE_DENY_WRITE);
3328 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3329 return nfs4_share_conflict(current_fh,
3330 NFS4_SHARE_DENY_READ);
3331 }
3332
3333 /*
3334 * Allow READ/WRITE during grace period on recovered state only for files
3335 * that are not able to provide mandatory locking.
3336 */
3337 static inline int
3338 grace_disallows_io(struct net *net, struct inode *inode)
3339 {
3340 return locks_in_grace(net) && mandatory_lock(inode);
3341 }
3342
3343 /* Returns true iff a is later than b: */
3344 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3345 {
3346 return (s32)a->si_generation - (s32)b->si_generation > 0;
3347 }
3348
3349 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3350 {
3351 /*
3352 * When sessions are used the stateid generation number is ignored
3353 * when it is zero.
3354 */
3355 if (has_session && in->si_generation == 0)
3356 return nfs_ok;
3357
3358 if (in->si_generation == ref->si_generation)
3359 return nfs_ok;
3360
3361 /* If the client sends us a stateid from the future, it's buggy: */
3362 if (stateid_generation_after(in, ref))
3363 return nfserr_bad_stateid;
3364 /*
3365 * However, we could see a stateid from the past, even from a
3366 * non-buggy client. For example, if the client sends a lock
3367 * while some IO is outstanding, the lock may bump si_generation
3368 * while the IO is still in flight. The client could avoid that
3369 * situation by waiting for responses on all the IO requests,
3370 * but better performance may result in retrying IO that
3371 * receives an old_stateid error if requests are rarely
3372 * reordered in flight:
3373 */
3374 return nfserr_old_stateid;
3375 }
3376
3377 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
3378 {
3379 struct nfs4_stid *s;
3380 struct nfs4_ol_stateid *ols;
3381 __be32 status;
3382
3383 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3384 return nfserr_bad_stateid;
3385 /* Client debugging aid. */
3386 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3387 char addr_str[INET6_ADDRSTRLEN];
3388 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3389 sizeof(addr_str));
3390 pr_warn_ratelimited("NFSD: client %s testing state ID "
3391 "with incorrect client ID\n", addr_str);
3392 return nfserr_bad_stateid;
3393 }
3394 s = find_stateid(cl, stateid);
3395 if (!s)
3396 return nfserr_bad_stateid;
3397 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
3398 if (status)
3399 return status;
3400 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3401 return nfs_ok;
3402 ols = openlockstateid(s);
3403 if (ols->st_stateowner->so_is_open_owner
3404 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
3405 return nfserr_bad_stateid;
3406 return nfs_ok;
3407 }
3408
3409 static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3410 struct nfs4_stid **s, bool sessions,
3411 struct nfsd_net *nn)
3412 {
3413 struct nfs4_client *cl;
3414
3415 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3416 return nfserr_bad_stateid;
3417 if (STALE_STATEID(stateid, nn))
3418 return nfserr_stale_stateid;
3419 cl = find_confirmed_client(&stateid->si_opaque.so_clid, sessions, nn);
3420 if (!cl)
3421 return nfserr_expired;
3422 *s = find_stateid_by_type(cl, stateid, typemask);
3423 if (!*s)
3424 return nfserr_bad_stateid;
3425 return nfs_ok;
3426
3427 }
3428
3429 /*
3430 * Checks for stateid operations
3431 */
3432 __be32
3433 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
3434 stateid_t *stateid, int flags, struct file **filpp)
3435 {
3436 struct nfs4_stid *s;
3437 struct nfs4_ol_stateid *stp = NULL;
3438 struct nfs4_delegation *dp = NULL;
3439 struct svc_fh *current_fh = &cstate->current_fh;
3440 struct inode *ino = current_fh->fh_dentry->d_inode;
3441 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3442 __be32 status;
3443
3444 if (filpp)
3445 *filpp = NULL;
3446
3447 if (grace_disallows_io(net, ino))
3448 return nfserr_grace;
3449
3450 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3451 return check_special_stateids(net, current_fh, stateid, flags);
3452
3453 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3454 &s, cstate->minorversion, nn);
3455 if (status)
3456 return status;
3457 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3458 if (status)
3459 goto out;
3460 switch (s->sc_type) {
3461 case NFS4_DELEG_STID:
3462 dp = delegstateid(s);
3463 status = nfs4_check_delegmode(dp, flags);
3464 if (status)
3465 goto out;
3466 if (filpp) {
3467 *filpp = dp->dl_file->fi_deleg_file;
3468 if (!*filpp) {
3469 WARN_ON_ONCE(1);
3470 status = nfserr_serverfault;
3471 goto out;
3472 }
3473 }
3474 break;
3475 case NFS4_OPEN_STID:
3476 case NFS4_LOCK_STID:
3477 stp = openlockstateid(s);
3478 status = nfs4_check_fh(current_fh, stp);
3479 if (status)
3480 goto out;
3481 if (stp->st_stateowner->so_is_open_owner
3482 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
3483 goto out;
3484 status = nfs4_check_openmode(stp, flags);
3485 if (status)
3486 goto out;
3487 if (filpp) {
3488 if (flags & RD_STATE)
3489 *filpp = find_readable_file(stp->st_file);
3490 else
3491 *filpp = find_writeable_file(stp->st_file);
3492 }
3493 break;
3494 default:
3495 return nfserr_bad_stateid;
3496 }
3497 status = nfs_ok;
3498 out:
3499 return status;
3500 }
3501
3502 static __be32
3503 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
3504 {
3505 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
3506 return nfserr_locks_held;
3507 release_lock_stateid(stp);
3508 return nfs_ok;
3509 }
3510
3511 /*
3512 * Test if the stateid is valid
3513 */
3514 __be32
3515 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3516 struct nfsd4_test_stateid *test_stateid)
3517 {
3518 struct nfsd4_test_stateid_id *stateid;
3519 struct nfs4_client *cl = cstate->session->se_client;
3520
3521 nfs4_lock_state();
3522 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
3523 stateid->ts_id_status =
3524 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
3525 nfs4_unlock_state();
3526
3527 return nfs_ok;
3528 }
3529
3530 __be32
3531 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3532 struct nfsd4_free_stateid *free_stateid)
3533 {
3534 stateid_t *stateid = &free_stateid->fr_stateid;
3535 struct nfs4_stid *s;
3536 struct nfs4_client *cl = cstate->session->se_client;
3537 __be32 ret = nfserr_bad_stateid;
3538
3539 nfs4_lock_state();
3540 s = find_stateid(cl, stateid);
3541 if (!s)
3542 goto out;
3543 switch (s->sc_type) {
3544 case NFS4_DELEG_STID:
3545 ret = nfserr_locks_held;
3546 goto out;
3547 case NFS4_OPEN_STID:
3548 case NFS4_LOCK_STID:
3549 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3550 if (ret)
3551 goto out;
3552 if (s->sc_type == NFS4_LOCK_STID)
3553 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3554 else
3555 ret = nfserr_locks_held;
3556 break;
3557 default:
3558 ret = nfserr_bad_stateid;
3559 }
3560 out:
3561 nfs4_unlock_state();
3562 return ret;
3563 }
3564
3565 static inline int
3566 setlkflg (int type)
3567 {
3568 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3569 RD_STATE : WR_STATE;
3570 }
3571
3572 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
3573 {
3574 struct svc_fh *current_fh = &cstate->current_fh;
3575 struct nfs4_stateowner *sop = stp->st_stateowner;
3576 __be32 status;
3577
3578 status = nfsd4_check_seqid(cstate, sop, seqid);
3579 if (status)
3580 return status;
3581 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3582 /*
3583 * "Closed" stateid's exist *only* to return
3584 * nfserr_replay_me from the previous step.
3585 */
3586 return nfserr_bad_stateid;
3587 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3588 if (status)
3589 return status;
3590 return nfs4_check_fh(current_fh, stp);
3591 }
3592
3593 /*
3594 * Checks for sequence id mutating operations.
3595 */
3596 static __be32
3597 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3598 stateid_t *stateid, char typemask,
3599 struct nfs4_ol_stateid **stpp,
3600 struct nfsd_net *nn)
3601 {
3602 __be32 status;
3603 struct nfs4_stid *s;
3604
3605 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3606 seqid, STATEID_VAL(stateid));
3607
3608 *stpp = NULL;
3609 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3610 cstate->minorversion, nn);
3611 if (status)
3612 return status;
3613 *stpp = openlockstateid(s);
3614 cstate->replay_owner = (*stpp)->st_stateowner;
3615
3616 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3617 }
3618
3619 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3620 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
3621 {
3622 __be32 status;
3623 struct nfs4_openowner *oo;
3624
3625 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
3626 NFS4_OPEN_STID, stpp, nn);
3627 if (status)
3628 return status;
3629 oo = openowner((*stpp)->st_stateowner);
3630 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
3631 return nfserr_bad_stateid;
3632 return nfs_ok;
3633 }
3634
3635 __be32
3636 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3637 struct nfsd4_open_confirm *oc)
3638 {
3639 __be32 status;
3640 struct nfs4_openowner *oo;
3641 struct nfs4_ol_stateid *stp;
3642 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3643
3644 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3645 (int)cstate->current_fh.fh_dentry->d_name.len,
3646 cstate->current_fh.fh_dentry->d_name.name);
3647
3648 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3649 if (status)
3650 return status;
3651
3652 nfs4_lock_state();
3653
3654 status = nfs4_preprocess_seqid_op(cstate,
3655 oc->oc_seqid, &oc->oc_req_stateid,
3656 NFS4_OPEN_STID, &stp, nn);
3657 if (status)
3658 goto out;
3659 oo = openowner(stp->st_stateowner);
3660 status = nfserr_bad_stateid;
3661 if (oo->oo_flags & NFS4_OO_CONFIRMED)
3662 goto out;
3663 oo->oo_flags |= NFS4_OO_CONFIRMED;
3664 update_stateid(&stp->st_stid.sc_stateid);
3665 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3666 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3667 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
3668
3669 nfsd4_client_record_create(oo->oo_owner.so_client);
3670 status = nfs_ok;
3671 out:
3672 if (!cstate->replay_owner)
3673 nfs4_unlock_state();
3674 return status;
3675 }
3676
3677 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
3678 {
3679 if (!test_access(access, stp))
3680 return;
3681 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
3682 clear_access(access, stp);
3683 }
3684
3685 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3686 {
3687 switch (to_access) {
3688 case NFS4_SHARE_ACCESS_READ:
3689 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3690 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3691 break;
3692 case NFS4_SHARE_ACCESS_WRITE:
3693 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3694 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3695 break;
3696 case NFS4_SHARE_ACCESS_BOTH:
3697 break;
3698 default:
3699 WARN_ON_ONCE(1);
3700 }
3701 }
3702
3703 static void
3704 reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
3705 {
3706 int i;
3707 for (i = 0; i < 4; i++) {
3708 if ((i & deny) != i)
3709 clear_deny(i, stp);
3710 }
3711 }
3712
3713 __be32
3714 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3715 struct nfsd4_compound_state *cstate,
3716 struct nfsd4_open_downgrade *od)
3717 {
3718 __be32 status;
3719 struct nfs4_ol_stateid *stp;
3720 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3721
3722 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3723 (int)cstate->current_fh.fh_dentry->d_name.len,
3724 cstate->current_fh.fh_dentry->d_name.name);
3725
3726 /* We don't yet support WANT bits: */
3727 if (od->od_deleg_want)
3728 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3729 od->od_deleg_want);
3730
3731 nfs4_lock_state();
3732 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3733 &od->od_stateid, &stp, nn);
3734 if (status)
3735 goto out;
3736 status = nfserr_inval;
3737 if (!test_access(od->od_share_access, stp)) {
3738 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
3739 stp->st_access_bmap, od->od_share_access);
3740 goto out;
3741 }
3742 if (!test_deny(od->od_share_deny, stp)) {
3743 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3744 stp->st_deny_bmap, od->od_share_deny);
3745 goto out;
3746 }
3747 nfs4_stateid_downgrade(stp, od->od_share_access);
3748
3749 reset_union_bmap_deny(od->od_share_deny, stp);
3750
3751 update_stateid(&stp->st_stid.sc_stateid);
3752 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3753 status = nfs_ok;
3754 out:
3755 if (!cstate->replay_owner)
3756 nfs4_unlock_state();
3757 return status;
3758 }
3759
3760 void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3761 {
3762 struct nfs4_openowner *oo;
3763 struct nfs4_ol_stateid *s;
3764
3765 if (!so->so_is_open_owner)
3766 return;
3767 oo = openowner(so);
3768 s = oo->oo_last_closed_stid;
3769 if (!s)
3770 return;
3771 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3772 /* Release the last_closed_stid on the next seqid bump: */
3773 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3774 return;
3775 }
3776 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
3777 release_last_closed_stateid(oo);
3778 }
3779
3780 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3781 {
3782 unhash_open_stateid(s);
3783 s->st_stid.sc_type = NFS4_CLOSED_STID;
3784 }
3785
3786 /*
3787 * nfs4_unlock_state() called after encode
3788 */
3789 __be32
3790 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3791 struct nfsd4_close *close)
3792 {
3793 __be32 status;
3794 struct nfs4_openowner *oo;
3795 struct nfs4_ol_stateid *stp;
3796 struct net *net = SVC_NET(rqstp);
3797 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3798
3799 dprintk("NFSD: nfsd4_close on file %.*s\n",
3800 (int)cstate->current_fh.fh_dentry->d_name.len,
3801 cstate->current_fh.fh_dentry->d_name.name);
3802
3803 nfs4_lock_state();
3804 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3805 &close->cl_stateid,
3806 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3807 &stp, nn);
3808 if (status)
3809 goto out;
3810 oo = openowner(stp->st_stateowner);
3811 status = nfs_ok;
3812 update_stateid(&stp->st_stid.sc_stateid);
3813 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3814
3815 nfsd4_close_open_stateid(stp);
3816 release_last_closed_stateid(oo);
3817 oo->oo_last_closed_stid = stp;
3818
3819 if (list_empty(&oo->oo_owner.so_stateids)) {
3820 if (cstate->minorversion) {
3821 release_openowner(oo);
3822 cstate->replay_owner = NULL;
3823 } else {
3824 /*
3825 * In the 4.0 case we need to keep the owners around a
3826 * little while to handle CLOSE replay.
3827 */
3828 if (list_empty(&oo->oo_owner.so_stateids))
3829 move_to_close_lru(oo, SVC_NET(rqstp));
3830 }
3831 }
3832 out:
3833 if (!cstate->replay_owner)
3834 nfs4_unlock_state();
3835 return status;
3836 }
3837
3838 __be32
3839 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3840 struct nfsd4_delegreturn *dr)
3841 {
3842 struct nfs4_delegation *dp;
3843 stateid_t *stateid = &dr->dr_stateid;
3844 struct nfs4_stid *s;
3845 __be32 status;
3846 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3847
3848 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3849 return status;
3850
3851 nfs4_lock_state();
3852 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
3853 cstate->minorversion, nn);
3854 if (status)
3855 goto out;
3856 dp = delegstateid(s);
3857 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
3858 if (status)
3859 goto out;
3860
3861 unhash_delegation(dp);
3862 out:
3863 nfs4_unlock_state();
3864
3865 return status;
3866 }
3867
3868
3869 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3870
3871 #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
3872
3873 static inline u64
3874 end_offset(u64 start, u64 len)
3875 {
3876 u64 end;
3877
3878 end = start + len;
3879 return end >= start ? end: NFS4_MAX_UINT64;
3880 }
3881
3882 /* last octet in a range */
3883 static inline u64
3884 last_byte_offset(u64 start, u64 len)
3885 {
3886 u64 end;
3887
3888 WARN_ON_ONCE(!len);
3889 end = start + len;
3890 return end > start ? end - 1: NFS4_MAX_UINT64;
3891 }
3892
3893 static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
3894 {
3895 return (file_hashval(inode) + cl_id
3896 + opaque_hashval(ownername->data, ownername->len))
3897 & LOCKOWNER_INO_HASH_MASK;
3898 }
3899
3900 /*
3901 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3902 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3903 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3904 * locking, this prevents us from being completely protocol-compliant. The
3905 * real solution to this problem is to start using unsigned file offsets in
3906 * the VFS, but this is a very deep change!
3907 */
3908 static inline void
3909 nfs4_transform_lock_offset(struct file_lock *lock)
3910 {
3911 if (lock->fl_start < 0)
3912 lock->fl_start = OFFSET_MAX;
3913 if (lock->fl_end < 0)
3914 lock->fl_end = OFFSET_MAX;
3915 }
3916
3917 /* Hack!: For now, we're defining this just so we can use a pointer to it
3918 * as a unique cookie to identify our (NFSv4's) posix locks. */
3919 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3920 };
3921
3922 static inline void
3923 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3924 {
3925 struct nfs4_lockowner *lo;
3926
3927 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3928 lo = (struct nfs4_lockowner *) fl->fl_owner;
3929 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3930 lo->lo_owner.so_owner.len, GFP_KERNEL);
3931 if (!deny->ld_owner.data)
3932 /* We just don't care that much */
3933 goto nevermind;
3934 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3935 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
3936 } else {
3937 nevermind:
3938 deny->ld_owner.len = 0;
3939 deny->ld_owner.data = NULL;
3940 deny->ld_clientid.cl_boot = 0;
3941 deny->ld_clientid.cl_id = 0;
3942 }
3943 deny->ld_start = fl->fl_start;
3944 deny->ld_length = NFS4_MAX_UINT64;
3945 if (fl->fl_end != NFS4_MAX_UINT64)
3946 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3947 deny->ld_type = NFS4_READ_LT;
3948 if (fl->fl_type != F_RDLCK)
3949 deny->ld_type = NFS4_WRITE_LT;
3950 }
3951
3952 static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3953 {
3954 struct nfs4_ol_stateid *lst;
3955
3956 if (!same_owner_str(&lo->lo_owner, owner, clid))
3957 return false;
3958 lst = list_first_entry(&lo->lo_owner.so_stateids,
3959 struct nfs4_ol_stateid, st_perstateowner);
3960 return lst->st_file->fi_inode == inode;
3961 }
3962
3963 static struct nfs4_lockowner *
3964 find_lockowner_str(struct inode *inode, clientid_t *clid,
3965 struct xdr_netobj *owner, struct nfsd_net *nn)
3966 {
3967 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
3968 struct nfs4_lockowner *lo;
3969
3970 list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
3971 if (same_lockowner_ino(lo, inode, clid, owner))
3972 return lo;
3973 }
3974 return NULL;
3975 }
3976
3977 static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
3978 {
3979 struct inode *inode = open_stp->st_file->fi_inode;
3980 unsigned int inohash = lockowner_ino_hashval(inode,
3981 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
3982 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3983
3984 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
3985 list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
3986 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
3987 }
3988
3989 /*
3990 * Alloc a lock owner structure.
3991 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3992 * occurred.
3993 *
3994 * strhashval = ownerstr_hashval
3995 */
3996
3997 static struct nfs4_lockowner *
3998 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
3999 struct nfs4_lockowner *lo;
4000
4001 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4002 if (!lo)
4003 return NULL;
4004 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4005 lo->lo_owner.so_is_open_owner = 0;
4006 /* It is the openowner seqid that will be incremented in encode in the
4007 * case of new lockowners; so increment the lock seqid manually: */
4008 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4009 hash_lockowner(lo, strhashval, clp, open_stp);
4010 return lo;
4011 }
4012
4013 static struct nfs4_ol_stateid *
4014 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
4015 {
4016 struct nfs4_ol_stateid *stp;
4017 struct nfs4_client *clp = lo->lo_owner.so_client;
4018
4019 stp = nfs4_alloc_stateid(clp);
4020 if (stp == NULL)
4021 return NULL;
4022 stp->st_stid.sc_type = NFS4_LOCK_STID;
4023 list_add(&stp->st_perfile, &fp->fi_stateids);
4024 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4025 stp->st_stateowner = &lo->lo_owner;
4026 get_nfs4_file(fp);
4027 stp->st_file = fp;
4028 stp->st_access_bmap = 0;
4029 stp->st_deny_bmap = open_stp->st_deny_bmap;
4030 stp->st_openstp = open_stp;
4031 return stp;
4032 }
4033
4034 static int
4035 check_lock_length(u64 offset, u64 length)
4036 {
4037 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4038 LOFF_OVERFLOW(offset, length)));
4039 }
4040
4041 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4042 {
4043 struct nfs4_file *fp = lock_stp->st_file;
4044 int oflag = nfs4_access_to_omode(access);
4045
4046 if (test_access(access, lock_stp))
4047 return;
4048 nfs4_file_get_access(fp, oflag);
4049 set_access(access, lock_stp);
4050 }
4051
4052 static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
4053 {
4054 struct nfs4_file *fi = ost->st_file;
4055 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4056 struct nfs4_client *cl = oo->oo_owner.so_client;
4057 struct nfs4_lockowner *lo;
4058 unsigned int strhashval;
4059 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4060
4061 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
4062 &lock->v.new.owner, nn);
4063 if (lo) {
4064 if (!cstate->minorversion)
4065 return nfserr_bad_seqid;
4066 /* XXX: a lockowner always has exactly one stateid: */
4067 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4068 struct nfs4_ol_stateid, st_perstateowner);
4069 return nfs_ok;
4070 }
4071 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4072 &lock->v.new.owner);
4073 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4074 if (lo == NULL)
4075 return nfserr_jukebox;
4076 *lst = alloc_init_lock_stateid(lo, fi, ost);
4077 if (*lst == NULL) {
4078 release_lockowner(lo);
4079 return nfserr_jukebox;
4080 }
4081 *new = true;
4082 return nfs_ok;
4083 }
4084
4085 /*
4086 * LOCK operation
4087 */
4088 __be32
4089 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4090 struct nfsd4_lock *lock)
4091 {
4092 struct nfs4_openowner *open_sop = NULL;
4093 struct nfs4_lockowner *lock_sop = NULL;
4094 struct nfs4_ol_stateid *lock_stp;
4095 struct file *filp = NULL;
4096 struct file_lock *file_lock = NULL;
4097 struct file_lock *conflock = NULL;
4098 __be32 status = 0;
4099 bool new_state = false;
4100 int lkflg;
4101 int err;
4102 struct net *net = SVC_NET(rqstp);
4103 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4104
4105 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4106 (long long) lock->lk_offset,
4107 (long long) lock->lk_length);
4108
4109 if (check_lock_length(lock->lk_offset, lock->lk_length))
4110 return nfserr_inval;
4111
4112 if ((status = fh_verify(rqstp, &cstate->current_fh,
4113 S_IFREG, NFSD_MAY_LOCK))) {
4114 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4115 return status;
4116 }
4117
4118 nfs4_lock_state();
4119
4120 if (lock->lk_is_new) {
4121 struct nfs4_ol_stateid *open_stp = NULL;
4122
4123 if (nfsd4_has_session(cstate))
4124 /* See rfc 5661 18.10.3: given clientid is ignored: */
4125 memcpy(&lock->v.new.clientid,
4126 &cstate->session->se_client->cl_clientid,
4127 sizeof(clientid_t));
4128
4129 status = nfserr_stale_clientid;
4130 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
4131 goto out;
4132
4133 /* validate and update open stateid and open seqid */
4134 status = nfs4_preprocess_confirmed_seqid_op(cstate,
4135 lock->lk_new_open_seqid,
4136 &lock->lk_new_open_stateid,
4137 &open_stp, nn);
4138 if (status)
4139 goto out;
4140 open_sop = openowner(open_stp->st_stateowner);
4141 status = nfserr_bad_stateid;
4142 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
4143 &lock->v.new.clientid))
4144 goto out;
4145 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4146 &lock_stp, &new_state);
4147 } else
4148 status = nfs4_preprocess_seqid_op(cstate,
4149 lock->lk_old_lock_seqid,
4150 &lock->lk_old_lock_stateid,
4151 NFS4_LOCK_STID, &lock_stp, nn);
4152 if (status)
4153 goto out;
4154 lock_sop = lockowner(lock_stp->st_stateowner);
4155
4156 lkflg = setlkflg(lock->lk_type);
4157 status = nfs4_check_openmode(lock_stp, lkflg);
4158 if (status)
4159 goto out;
4160
4161 status = nfserr_grace;
4162 if (locks_in_grace(net) && !lock->lk_reclaim)
4163 goto out;
4164 status = nfserr_no_grace;
4165 if (!locks_in_grace(net) && lock->lk_reclaim)
4166 goto out;
4167
4168 file_lock = locks_alloc_lock();
4169 if (!file_lock) {
4170 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4171 status = nfserr_jukebox;
4172 goto out;
4173 }
4174
4175 locks_init_lock(file_lock);
4176 switch (lock->lk_type) {
4177 case NFS4_READ_LT:
4178 case NFS4_READW_LT:
4179 filp = find_readable_file(lock_stp->st_file);
4180 if (filp)
4181 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4182 file_lock->fl_type = F_RDLCK;
4183 break;
4184 case NFS4_WRITE_LT:
4185 case NFS4_WRITEW_LT:
4186 filp = find_writeable_file(lock_stp->st_file);
4187 if (filp)
4188 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4189 file_lock->fl_type = F_WRLCK;
4190 break;
4191 default:
4192 status = nfserr_inval;
4193 goto out;
4194 }
4195 if (!filp) {
4196 status = nfserr_openmode;
4197 goto out;
4198 }
4199 file_lock->fl_owner = (fl_owner_t)lock_sop;
4200 file_lock->fl_pid = current->tgid;
4201 file_lock->fl_file = filp;
4202 file_lock->fl_flags = FL_POSIX;
4203 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4204 file_lock->fl_start = lock->lk_offset;
4205 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4206 nfs4_transform_lock_offset(file_lock);
4207
4208 conflock = locks_alloc_lock();
4209 if (!conflock) {
4210 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4211 status = nfserr_jukebox;
4212 goto out;
4213 }
4214
4215 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
4216 switch (-err) {
4217 case 0: /* success! */
4218 update_stateid(&lock_stp->st_stid.sc_stateid);
4219 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
4220 sizeof(stateid_t));
4221 status = 0;
4222 break;
4223 case (EAGAIN): /* conflock holds conflicting lock */
4224 status = nfserr_denied;
4225 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4226 nfs4_set_lock_denied(conflock, &lock->lk_denied);
4227 break;
4228 case (EDEADLK):
4229 status = nfserr_deadlock;
4230 break;
4231 default:
4232 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4233 status = nfserrno(err);
4234 break;
4235 }
4236 out:
4237 if (status && new_state)
4238 release_lockowner(lock_sop);
4239 if (!cstate->replay_owner)
4240 nfs4_unlock_state();
4241 if (file_lock)
4242 locks_free_lock(file_lock);
4243 if (conflock)
4244 locks_free_lock(conflock);
4245 return status;
4246 }
4247
4248 /*
4249 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4250 * so we do a temporary open here just to get an open file to pass to
4251 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4252 * inode operation.)
4253 */
4254 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4255 {
4256 struct file *file;
4257 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4258 if (!err) {
4259 err = nfserrno(vfs_test_lock(file, lock));
4260 nfsd_close(file);
4261 }
4262 return err;
4263 }
4264
4265 /*
4266 * LOCKT operation
4267 */
4268 __be32
4269 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4270 struct nfsd4_lockt *lockt)
4271 {
4272 struct inode *inode;
4273 struct file_lock *file_lock = NULL;
4274 struct nfs4_lockowner *lo;
4275 __be32 status;
4276 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4277
4278 if (locks_in_grace(SVC_NET(rqstp)))
4279 return nfserr_grace;
4280
4281 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4282 return nfserr_inval;
4283
4284 nfs4_lock_state();
4285
4286 if (!nfsd4_has_session(cstate)) {
4287 status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
4288 if (status)
4289 goto out;
4290 }
4291
4292 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4293 goto out;
4294
4295 inode = cstate->current_fh.fh_dentry->d_inode;
4296 file_lock = locks_alloc_lock();
4297 if (!file_lock) {
4298 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4299 status = nfserr_jukebox;
4300 goto out;
4301 }
4302 locks_init_lock(file_lock);
4303 switch (lockt->lt_type) {
4304 case NFS4_READ_LT:
4305 case NFS4_READW_LT:
4306 file_lock->fl_type = F_RDLCK;
4307 break;
4308 case NFS4_WRITE_LT:
4309 case NFS4_WRITEW_LT:
4310 file_lock->fl_type = F_WRLCK;
4311 break;
4312 default:
4313 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4314 status = nfserr_inval;
4315 goto out;
4316 }
4317
4318 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
4319 if (lo)
4320 file_lock->fl_owner = (fl_owner_t)lo;
4321 file_lock->fl_pid = current->tgid;
4322 file_lock->fl_flags = FL_POSIX;
4323
4324 file_lock->fl_start = lockt->lt_offset;
4325 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4326
4327 nfs4_transform_lock_offset(file_lock);
4328
4329 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
4330 if (status)
4331 goto out;
4332
4333 if (file_lock->fl_type != F_UNLCK) {
4334 status = nfserr_denied;
4335 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
4336 }
4337 out:
4338 nfs4_unlock_state();
4339 if (file_lock)
4340 locks_free_lock(file_lock);
4341 return status;
4342 }
4343
4344 __be32
4345 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4346 struct nfsd4_locku *locku)
4347 {
4348 struct nfs4_ol_stateid *stp;
4349 struct file *filp = NULL;
4350 struct file_lock *file_lock = NULL;
4351 __be32 status;
4352 int err;
4353 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4354
4355 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4356 (long long) locku->lu_offset,
4357 (long long) locku->lu_length);
4358
4359 if (check_lock_length(locku->lu_offset, locku->lu_length))
4360 return nfserr_inval;
4361
4362 nfs4_lock_state();
4363
4364 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
4365 &locku->lu_stateid, NFS4_LOCK_STID,
4366 &stp, nn);
4367 if (status)
4368 goto out;
4369 filp = find_any_file(stp->st_file);
4370 if (!filp) {
4371 status = nfserr_lock_range;
4372 goto out;
4373 }
4374 file_lock = locks_alloc_lock();
4375 if (!file_lock) {
4376 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4377 status = nfserr_jukebox;
4378 goto out;
4379 }
4380 locks_init_lock(file_lock);
4381 file_lock->fl_type = F_UNLCK;
4382 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4383 file_lock->fl_pid = current->tgid;
4384 file_lock->fl_file = filp;
4385 file_lock->fl_flags = FL_POSIX;
4386 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4387 file_lock->fl_start = locku->lu_offset;
4388
4389 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4390 locku->lu_length);
4391 nfs4_transform_lock_offset(file_lock);
4392
4393 /*
4394 * Try to unlock the file in the VFS.
4395 */
4396 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
4397 if (err) {
4398 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4399 goto out_nfserr;
4400 }
4401 /*
4402 * OK, unlock succeeded; the only thing left to do is update the stateid.
4403 */
4404 update_stateid(&stp->st_stid.sc_stateid);
4405 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4406
4407 out:
4408 if (!cstate->replay_owner)
4409 nfs4_unlock_state();
4410 if (file_lock)
4411 locks_free_lock(file_lock);
4412 return status;
4413
4414 out_nfserr:
4415 status = nfserrno(err);
4416 goto out;
4417 }
4418
4419 /*
4420 * returns
4421 * 1: locks held by lockowner
4422 * 0: no locks held by lockowner
4423 */
4424 static int
4425 check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
4426 {
4427 struct file_lock **flpp;
4428 struct inode *inode = filp->fi_inode;
4429 int status = 0;
4430
4431 lock_flocks();
4432 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4433 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4434 status = 1;
4435 goto out;
4436 }
4437 }
4438 out:
4439 unlock_flocks();
4440 return status;
4441 }
4442
4443 __be32
4444 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4445 struct nfsd4_compound_state *cstate,
4446 struct nfsd4_release_lockowner *rlockowner)
4447 {
4448 clientid_t *clid = &rlockowner->rl_clientid;
4449 struct nfs4_stateowner *sop;
4450 struct nfs4_lockowner *lo;
4451 struct nfs4_ol_stateid *stp;
4452 struct xdr_netobj *owner = &rlockowner->rl_owner;
4453 struct list_head matches;
4454 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
4455 __be32 status;
4456 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4457
4458 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4459 clid->cl_boot, clid->cl_id);
4460
4461 nfs4_lock_state();
4462
4463 status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
4464 if (status)
4465 goto out;
4466
4467 status = nfserr_locks_held;
4468 INIT_LIST_HEAD(&matches);
4469
4470 list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
4471 if (sop->so_is_open_owner)
4472 continue;
4473 if (!same_owner_str(sop, owner, clid))
4474 continue;
4475 list_for_each_entry(stp, &sop->so_stateids,
4476 st_perstateowner) {
4477 lo = lockowner(sop);
4478 if (check_for_locks(stp->st_file, lo))
4479 goto out;
4480 list_add(&lo->lo_list, &matches);
4481 }
4482 }
4483 /* Clients probably won't expect us to return with some (but not all)
4484 * of the lockowner state released; so don't release any until all
4485 * have been checked. */
4486 status = nfs_ok;
4487 while (!list_empty(&matches)) {
4488 lo = list_entry(matches.next, struct nfs4_lockowner,
4489 lo_list);
4490 /* unhash_stateowner deletes so_perclient only
4491 * for openowners. */
4492 list_del(&lo->lo_list);
4493 release_lockowner(lo);
4494 }
4495 out:
4496 nfs4_unlock_state();
4497 return status;
4498 }
4499
4500 static inline struct nfs4_client_reclaim *
4501 alloc_reclaim(void)
4502 {
4503 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4504 }
4505
4506 bool
4507 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
4508 {
4509 struct nfs4_client_reclaim *crp;
4510
4511 crp = nfsd4_find_reclaim_client(name, nn);
4512 return (crp && crp->cr_clp);
4513 }
4514
4515 /*
4516 * failure => all reset bets are off, nfserr_no_grace...
4517 */
4518 struct nfs4_client_reclaim *
4519 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
4520 {
4521 unsigned int strhashval;
4522 struct nfs4_client_reclaim *crp;
4523
4524 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4525 crp = alloc_reclaim();
4526 if (crp) {
4527 strhashval = clientstr_hashval(name);
4528 INIT_LIST_HEAD(&crp->cr_strhash);
4529 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
4530 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4531 crp->cr_clp = NULL;
4532 nn->reclaim_str_hashtbl_size++;
4533 }
4534 return crp;
4535 }
4536
4537 void
4538 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
4539 {
4540 list_del(&crp->cr_strhash);
4541 kfree(crp);
4542 nn->reclaim_str_hashtbl_size--;
4543 }
4544
4545 void
4546 nfs4_release_reclaim(struct nfsd_net *nn)
4547 {
4548 struct nfs4_client_reclaim *crp = NULL;
4549 int i;
4550
4551 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4552 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4553 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
4554 struct nfs4_client_reclaim, cr_strhash);
4555 nfs4_remove_reclaim_record(crp, nn);
4556 }
4557 }
4558 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
4559 }
4560
4561 /*
4562 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4563 struct nfs4_client_reclaim *
4564 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
4565 {
4566 unsigned int strhashval;
4567 struct nfs4_client_reclaim *crp = NULL;
4568
4569 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
4570
4571 strhashval = clientstr_hashval(recdir);
4572 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
4573 if (same_name(crp->cr_recdir, recdir)) {
4574 return crp;
4575 }
4576 }
4577 return NULL;
4578 }
4579
4580 /*
4581 * Called from OPEN. Look for clientid in reclaim list.
4582 */
4583 __be32
4584 nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
4585 {
4586 struct nfs4_client *clp;
4587
4588 /* find clientid in conf_id_hashtbl */
4589 clp = find_confirmed_client(clid, sessions, nn);
4590 if (clp == NULL)
4591 return nfserr_reclaim_bad;
4592
4593 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
4594 }
4595
4596 #ifdef CONFIG_NFSD_FAULT_INJECTION
4597
4598 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4599 {
4600 expire_client(clp);
4601 return 1;
4602 }
4603
4604 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
4605 {
4606 char buf[INET6_ADDRSTRLEN];
4607 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
4608 printk(KERN_INFO "NFS Client: %s\n", buf);
4609 return 1;
4610 }
4611
4612 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
4613 const char *type)
4614 {
4615 char buf[INET6_ADDRSTRLEN];
4616 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
4617 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
4618 }
4619
4620 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
4621 {
4622 struct nfs4_openowner *oop;
4623 struct nfs4_lockowner *lop, *lo_next;
4624 struct nfs4_ol_stateid *stp, *st_next;
4625 u64 count = 0;
4626
4627 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
4628 list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
4629 list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
4630 if (func)
4631 func(lop);
4632 if (++count == max)
4633 return count;
4634 }
4635 }
4636 }
4637
4638 return count;
4639 }
4640
4641 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4642 {
4643 return nfsd_foreach_client_lock(clp, max, release_lockowner);
4644 }
4645
4646 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
4647 {
4648 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
4649 nfsd_print_count(clp, count, "locked files");
4650 return count;
4651 }
4652
4653 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
4654 {
4655 struct nfs4_openowner *oop, *next;
4656 u64 count = 0;
4657
4658 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
4659 if (func)
4660 func(oop);
4661 if (++count == max)
4662 break;
4663 }
4664
4665 return count;
4666 }
4667
4668 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
4669 {
4670 return nfsd_foreach_client_open(clp, max, release_openowner);
4671 }
4672
4673 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
4674 {
4675 u64 count = nfsd_foreach_client_open(clp, max, NULL);
4676 nfsd_print_count(clp, count, "open files");
4677 return count;
4678 }
4679
4680 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
4681 struct list_head *victims)
4682 {
4683 struct nfs4_delegation *dp, *next;
4684 u64 count = 0;
4685
4686 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
4687 if (victims)
4688 list_move(&dp->dl_recall_lru, victims);
4689 if (++count == max)
4690 break;
4691 }
4692 return count;
4693 }
4694
4695 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
4696 {
4697 struct nfs4_delegation *dp, *next;
4698 LIST_HEAD(victims);
4699 u64 count;
4700
4701 spin_lock(&recall_lock);
4702 count = nfsd_find_all_delegations(clp, max, &victims);
4703 spin_unlock(&recall_lock);
4704
4705 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4706 unhash_delegation(dp);
4707
4708 return count;
4709 }
4710
4711 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
4712 {
4713 struct nfs4_delegation *dp, *next;
4714 LIST_HEAD(victims);
4715 u64 count;
4716
4717 spin_lock(&recall_lock);
4718 count = nfsd_find_all_delegations(clp, max, &victims);
4719 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4720 nfsd_break_one_deleg(dp);
4721 spin_unlock(&recall_lock);
4722
4723 return count;
4724 }
4725
4726 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
4727 {
4728 u64 count = 0;
4729
4730 spin_lock(&recall_lock);
4731 count = nfsd_find_all_delegations(clp, max, NULL);
4732 spin_unlock(&recall_lock);
4733
4734 nfsd_print_count(clp, count, "delegations");
4735 return count;
4736 }
4737
4738 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
4739 {
4740 struct nfs4_client *clp, *next;
4741 u64 count = 0;
4742 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4743
4744 if (!nfsd_netns_ready(nn))
4745 return 0;
4746
4747 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
4748 count += func(clp, max - count);
4749 if ((max != 0) && (count >= max))
4750 break;
4751 }
4752
4753 return count;
4754 }
4755
4756 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
4757 {
4758 struct nfs4_client *clp;
4759 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4760
4761 if (!nfsd_netns_ready(nn))
4762 return NULL;
4763
4764 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
4765 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
4766 return clp;
4767 }
4768 return NULL;
4769 }
4770
4771 #endif /* CONFIG_NFSD_FAULT_INJECTION */
4772
4773 /* initialization to perform at module load time: */
4774
4775 void
4776 nfs4_state_init(void)
4777 {
4778 int i;
4779
4780 for (i = 0; i < FILE_HASH_SIZE; i++) {
4781 INIT_LIST_HEAD(&file_hashtbl[i]);
4782 }
4783 INIT_LIST_HEAD(&del_recall_lru);
4784 }
4785
4786 /*
4787 * Since the lifetime of a delegation isn't limited to that of an open, a
4788 * client may quite reasonably hang on to a delegation as long as it has
4789 * the inode cached. This becomes an obvious problem the first time a
4790 * client's inode cache approaches the size of the server's total memory.
4791 *
4792 * For now we avoid this problem by imposing a hard limit on the number
4793 * of delegations, which varies according to the server's memory size.
4794 */
4795 static void
4796 set_max_delegations(void)
4797 {
4798 /*
4799 * Allow at most 4 delegations per megabyte of RAM. Quick
4800 * estimates suggest that in the worst case (where every delegation
4801 * is for a different inode), a delegation could take about 1.5K,
4802 * giving a worst case usage of about 6% of memory.
4803 */
4804 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4805 }
4806
4807 static int nfs4_state_create_net(struct net *net)
4808 {
4809 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4810 int i;
4811
4812 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4813 CLIENT_HASH_SIZE, GFP_KERNEL);
4814 if (!nn->conf_id_hashtbl)
4815 goto err;
4816 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4817 CLIENT_HASH_SIZE, GFP_KERNEL);
4818 if (!nn->unconf_id_hashtbl)
4819 goto err_unconf_id;
4820 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
4821 OWNER_HASH_SIZE, GFP_KERNEL);
4822 if (!nn->ownerstr_hashtbl)
4823 goto err_ownerstr;
4824 nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
4825 LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
4826 if (!nn->lockowner_ino_hashtbl)
4827 goto err_lockowner_ino;
4828 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
4829 SESSION_HASH_SIZE, GFP_KERNEL);
4830 if (!nn->sessionid_hashtbl)
4831 goto err_sessionid;
4832
4833 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4834 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
4835 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
4836 }
4837 for (i = 0; i < OWNER_HASH_SIZE; i++)
4838 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
4839 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4840 INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
4841 for (i = 0; i < SESSION_HASH_SIZE; i++)
4842 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
4843 nn->conf_name_tree = RB_ROOT;
4844 nn->unconf_name_tree = RB_ROOT;
4845 INIT_LIST_HEAD(&nn->client_lru);
4846 INIT_LIST_HEAD(&nn->close_lru);
4847 spin_lock_init(&nn->client_lock);
4848
4849 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
4850 get_net(net);
4851
4852 return 0;
4853
4854 err_sessionid:
4855 kfree(nn->lockowner_ino_hashtbl);
4856 err_lockowner_ino:
4857 kfree(nn->ownerstr_hashtbl);
4858 err_ownerstr:
4859 kfree(nn->unconf_id_hashtbl);
4860 err_unconf_id:
4861 kfree(nn->conf_id_hashtbl);
4862 err:
4863 return -ENOMEM;
4864 }
4865
4866 static void
4867 nfs4_state_destroy_net(struct net *net)
4868 {
4869 int i;
4870 struct nfs4_client *clp = NULL;
4871 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4872 struct rb_node *node, *tmp;
4873
4874 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4875 while (!list_empty(&nn->conf_id_hashtbl[i])) {
4876 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4877 destroy_client(clp);
4878 }
4879 }
4880
4881 node = rb_first(&nn->unconf_name_tree);
4882 while (node != NULL) {
4883 tmp = node;
4884 node = rb_next(tmp);
4885 clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
4886 rb_erase(tmp, &nn->unconf_name_tree);
4887 destroy_client(clp);
4888 }
4889
4890 kfree(nn->sessionid_hashtbl);
4891 kfree(nn->lockowner_ino_hashtbl);
4892 kfree(nn->ownerstr_hashtbl);
4893 kfree(nn->unconf_id_hashtbl);
4894 kfree(nn->conf_id_hashtbl);
4895 put_net(net);
4896 }
4897
4898 int
4899 nfs4_state_start_net(struct net *net)
4900 {
4901 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4902 int ret;
4903
4904 ret = nfs4_state_create_net(net);
4905 if (ret)
4906 return ret;
4907 nfsd4_client_tracking_init(net);
4908 nn->boot_time = get_seconds();
4909 locks_start_grace(net, &nn->nfsd4_manager);
4910 nn->grace_ended = false;
4911 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
4912 nn->nfsd4_grace, net);
4913 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
4914 return 0;
4915 }
4916
4917 /* initialization to perform when the nfsd service is started: */
4918
4919 int
4920 nfs4_state_start(void)
4921 {
4922 int ret;
4923
4924 ret = set_callback_cred();
4925 if (ret)
4926 return -ENOMEM;
4927 laundry_wq = create_singlethread_workqueue("nfsd4");
4928 if (laundry_wq == NULL) {
4929 ret = -ENOMEM;
4930 goto out_recovery;
4931 }
4932 ret = nfsd4_create_callback_queue();
4933 if (ret)
4934 goto out_free_laundry;
4935
4936 set_max_delegations();
4937
4938 return 0;
4939
4940 out_free_laundry:
4941 destroy_workqueue(laundry_wq);
4942 out_recovery:
4943 return ret;
4944 }
4945
4946 /* should be called with the state lock held */
4947 void
4948 nfs4_state_shutdown_net(struct net *net)
4949 {
4950 struct nfs4_delegation *dp = NULL;
4951 struct list_head *pos, *next, reaplist;
4952 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4953
4954 cancel_delayed_work_sync(&nn->laundromat_work);
4955 locks_end_grace(&nn->nfsd4_manager);
4956
4957 INIT_LIST_HEAD(&reaplist);
4958 spin_lock(&recall_lock);
4959 list_for_each_safe(pos, next, &del_recall_lru) {
4960 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4961 if (dp->dl_stid.sc_client->net != net)
4962 continue;
4963 list_move(&dp->dl_recall_lru, &reaplist);
4964 }
4965 spin_unlock(&recall_lock);
4966 list_for_each_safe(pos, next, &reaplist) {
4967 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4968 unhash_delegation(dp);
4969 }
4970
4971 nfsd4_client_tracking_exit(net);
4972 nfs4_state_destroy_net(net);
4973 }
4974
4975 void
4976 nfs4_state_shutdown(void)
4977 {
4978 destroy_workqueue(laundry_wq);
4979 nfsd4_destroy_callback_queue();
4980 }
4981
4982 static void
4983 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
4984 {
4985 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
4986 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
4987 }
4988
4989 static void
4990 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
4991 {
4992 if (cstate->minorversion) {
4993 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
4994 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
4995 }
4996 }
4997
4998 void
4999 clear_current_stateid(struct nfsd4_compound_state *cstate)
5000 {
5001 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5002 }
5003
5004 /*
5005 * functions to set current state id
5006 */
5007 void
5008 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5009 {
5010 put_stateid(cstate, &odp->od_stateid);
5011 }
5012
5013 void
5014 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5015 {
5016 put_stateid(cstate, &open->op_stateid);
5017 }
5018
5019 void
5020 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5021 {
5022 put_stateid(cstate, &close->cl_stateid);
5023 }
5024
5025 void
5026 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5027 {
5028 put_stateid(cstate, &lock->lk_resp_stateid);
5029 }
5030
5031 /*
5032 * functions to consume current state id
5033 */
5034
5035 void
5036 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5037 {
5038 get_stateid(cstate, &odp->od_stateid);
5039 }
5040
5041 void
5042 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5043 {
5044 get_stateid(cstate, &drp->dr_stateid);
5045 }
5046
5047 void
5048 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5049 {
5050 get_stateid(cstate, &fsp->fr_stateid);
5051 }
5052
5053 void
5054 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5055 {
5056 get_stateid(cstate, &setattr->sa_stateid);
5057 }
5058
5059 void
5060 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5061 {
5062 get_stateid(cstate, &close->cl_stateid);
5063 }
5064
5065 void
5066 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5067 {
5068 get_stateid(cstate, &locku->lu_stateid);
5069 }
5070
5071 void
5072 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5073 {
5074 get_stateid(cstate, &read->rd_stateid);
5075 }
5076
5077 void
5078 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5079 {
5080 get_stateid(cstate, &write->wr_stateid);
5081 }
This page took 0.139399 seconds and 5 git commands to generate.