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