~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nfsd/nfs4state.c

Version: ~ [ linux-5.6-rc1 ] ~ [ linux-5.5.2 ] ~ [ linux-5.4.17 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.102 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.170 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.213 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.213 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp