2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
35 #include <linux/file.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>
46 #define NFSDDBG_FACILITY NFSDDBG_PROC
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;
59 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
60 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
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 char user_recovery_dirname
[PATH_MAX
] = "/var/lib/nfs/v4recovery";
68 static void nfs4_set_recdir(char *recdir
);
69 static int check_for_locks(struct nfs4_file
*filp
, struct nfs4_stateowner
*lowner
);
73 /* Currently used for almost all code touching nfsv4 state: */
74 static DEFINE_MUTEX(client_mutex
);
77 * Currently used for the del_recall_lru and file hash table. In an
78 * effort to decrease the scope of the client_mutex, this spinlock may
79 * eventually cover more:
81 static DEFINE_SPINLOCK(recall_lock
);
83 static struct kmem_cache
*stateowner_slab
= NULL
;
84 static struct kmem_cache
*file_slab
= NULL
;
85 static struct kmem_cache
*stateid_slab
= NULL
;
86 static struct kmem_cache
*deleg_slab
= NULL
;
91 mutex_lock(&client_mutex
);
95 nfs4_unlock_state(void)
97 mutex_unlock(&client_mutex
);
101 opaque_hashval(const void *ptr
, int nbytes
)
103 unsigned char *cptr
= (unsigned char *) ptr
;
113 static struct list_head del_recall_lru
;
116 put_nfs4_file(struct nfs4_file
*fi
)
118 if (atomic_dec_and_lock(&fi
->fi_ref
, &recall_lock
)) {
119 list_del(&fi
->fi_hash
);
120 spin_unlock(&recall_lock
);
122 kmem_cache_free(file_slab
, fi
);
127 get_nfs4_file(struct nfs4_file
*fi
)
129 atomic_inc(&fi
->fi_ref
);
132 static int num_delegations
;
133 unsigned int max_delegations
;
136 * Open owner state (share locks)
139 /* hash tables for nfs4_stateowner */
140 #define OWNER_HASH_BITS 8
141 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
142 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
144 #define ownerid_hashval(id) \
145 ((id) & OWNER_HASH_MASK)
146 #define ownerstr_hashval(clientid, ownername) \
147 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
149 static struct list_head ownerid_hashtbl
[OWNER_HASH_SIZE
];
150 static struct list_head ownerstr_hashtbl
[OWNER_HASH_SIZE
];
152 /* hash table for nfs4_file */
153 #define FILE_HASH_BITS 8
154 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
156 /* hash table for (open)nfs4_stateid */
157 #define STATEID_HASH_BITS 10
158 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
159 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
161 #define file_hashval(x) \
162 hash_ptr(x, FILE_HASH_BITS)
163 #define stateid_hashval(owner_id, file_id) \
164 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
166 static struct list_head file_hashtbl
[FILE_HASH_SIZE
];
167 static struct list_head stateid_hashtbl
[STATEID_HASH_SIZE
];
169 static void __nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
171 BUG_ON(!(fp
->fi_fds
[oflag
] || fp
->fi_fds
[O_RDWR
]));
172 atomic_inc(&fp
->fi_access
[oflag
]);
175 static void nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
177 if (oflag
== O_RDWR
) {
178 __nfs4_file_get_access(fp
, O_RDONLY
);
179 __nfs4_file_get_access(fp
, O_WRONLY
);
181 __nfs4_file_get_access(fp
, oflag
);
184 static void nfs4_file_put_fd(struct nfs4_file
*fp
, int oflag
)
186 if (fp
->fi_fds
[oflag
]) {
187 fput(fp
->fi_fds
[oflag
]);
188 fp
->fi_fds
[oflag
] = NULL
;
192 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
194 if (atomic_dec_and_test(&fp
->fi_access
[oflag
])) {
195 nfs4_file_put_fd(fp
, oflag
);
197 * It's also safe to get rid of the RDWR open *if*
198 * we no longer have need of the other kind of access
199 * or if we already have the other kind of open:
201 if (fp
->fi_fds
[1-oflag
]
202 || atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
203 nfs4_file_put_fd(fp
, O_RDWR
);
207 static void nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
209 if (oflag
== O_RDWR
) {
210 __nfs4_file_put_access(fp
, O_RDONLY
);
211 __nfs4_file_put_access(fp
, O_WRONLY
);
213 __nfs4_file_put_access(fp
, oflag
);
216 static struct nfs4_delegation
*
217 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_stateid
*stp
, struct svc_fh
*current_fh
, u32 type
)
219 struct nfs4_delegation
*dp
;
220 struct nfs4_file
*fp
= stp
->st_file
;
222 dprintk("NFSD alloc_init_deleg\n");
224 * Major work on the lease subsystem (for example, to support
225 * calbacks on stat) will be required before we can support
226 * write delegations properly.
228 if (type
!= NFS4_OPEN_DELEGATE_READ
)
230 if (fp
->fi_had_conflict
)
232 if (num_delegations
> max_delegations
)
234 dp
= kmem_cache_alloc(deleg_slab
, GFP_KERNEL
);
238 INIT_LIST_HEAD(&dp
->dl_perfile
);
239 INIT_LIST_HEAD(&dp
->dl_perclnt
);
240 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
245 dp
->dl_stateid
.si_boot
= boot_time
;
246 dp
->dl_stateid
.si_stateownerid
= current_delegid
++;
247 dp
->dl_stateid
.si_fileid
= 0;
248 dp
->dl_stateid
.si_generation
= 0;
249 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
251 atomic_set(&dp
->dl_count
, 1);
252 INIT_WORK(&dp
->dl_recall
.cb_work
, nfsd4_do_callback_rpc
);
257 nfs4_put_delegation(struct nfs4_delegation
*dp
)
259 if (atomic_dec_and_test(&dp
->dl_count
)) {
260 dprintk("NFSD: freeing dp %p\n",dp
);
261 put_nfs4_file(dp
->dl_file
);
262 kmem_cache_free(deleg_slab
, dp
);
267 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
269 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
270 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
272 fput(fp
->fi_deleg_file
);
273 fp
->fi_deleg_file
= NULL
;
277 /* Called under the state lock. */
279 unhash_delegation(struct nfs4_delegation
*dp
)
281 list_del_init(&dp
->dl_perclnt
);
282 spin_lock(&recall_lock
);
283 list_del_init(&dp
->dl_perfile
);
284 list_del_init(&dp
->dl_recall_lru
);
285 spin_unlock(&recall_lock
);
286 nfs4_put_deleg_lease(dp
->dl_file
);
287 nfs4_put_delegation(dp
);
294 /* client_lock protects the client lru list and session hash table */
295 static DEFINE_SPINLOCK(client_lock
);
297 /* Hash tables for nfs4_clientid state */
298 #define CLIENT_HASH_BITS 4
299 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
300 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
302 #define clientid_hashval(id) \
303 ((id) & CLIENT_HASH_MASK)
304 #define clientstr_hashval(name) \
305 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
307 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
308 * used in reboot/reset lease grace period processing
310 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
311 * setclientid_confirmed info.
313 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
316 * client_lru holds client queue ordered by nfs4_client.cl_time
319 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
320 * for last close replay.
322 static struct list_head reclaim_str_hashtbl
[CLIENT_HASH_SIZE
];
323 static int reclaim_str_hashtbl_size
= 0;
324 static struct list_head conf_id_hashtbl
[CLIENT_HASH_SIZE
];
325 static struct list_head conf_str_hashtbl
[CLIENT_HASH_SIZE
];
326 static struct list_head unconf_str_hashtbl
[CLIENT_HASH_SIZE
];
327 static struct list_head unconf_id_hashtbl
[CLIENT_HASH_SIZE
];
328 static struct list_head client_lru
;
329 static struct list_head close_lru
;
332 * We store the NONE, READ, WRITE, and BOTH bits separately in the
333 * st_{access,deny}_bmap field of the stateid, in order to track not
334 * only what share bits are currently in force, but also what
335 * combinations of share bits previous opens have used. This allows us
336 * to enforce the recommendation of rfc 3530 14.2.19 that the server
337 * return an error if the client attempt to downgrade to a combination
338 * of share bits not explicable by closing some of its previous opens.
340 * XXX: This enforcement is actually incomplete, since we don't keep
341 * track of access/deny bit combinations; so, e.g., we allow:
343 * OPEN allow read, deny write
344 * OPEN allow both, deny none
345 * DOWNGRADE allow read, deny none
347 * which we should reject.
350 set_access(unsigned int *access
, unsigned long bmap
) {
354 for (i
= 1; i
< 4; i
++) {
355 if (test_bit(i
, &bmap
))
361 set_deny(unsigned int *deny
, unsigned long bmap
) {
365 for (i
= 0; i
< 4; i
++) {
366 if (test_bit(i
, &bmap
))
372 test_share(struct nfs4_stateid
*stp
, struct nfsd4_open
*open
) {
373 unsigned int access
, deny
;
375 set_access(&access
, stp
->st_access_bmap
);
376 set_deny(&deny
, stp
->st_deny_bmap
);
377 if ((access
& open
->op_share_deny
) || (deny
& open
->op_share_access
))
382 static int nfs4_access_to_omode(u32 access
)
384 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
385 case NFS4_SHARE_ACCESS_READ
:
387 case NFS4_SHARE_ACCESS_WRITE
:
389 case NFS4_SHARE_ACCESS_BOTH
:
395 static void unhash_generic_stateid(struct nfs4_stateid
*stp
)
397 list_del(&stp
->st_hash
);
398 list_del(&stp
->st_perfile
);
399 list_del(&stp
->st_perstateowner
);
402 static void free_generic_stateid(struct nfs4_stateid
*stp
)
406 if (stp
->st_access_bmap
) {
407 for (i
= 1; i
< 4; i
++) {
408 if (test_bit(i
, &stp
->st_access_bmap
))
409 nfs4_file_put_access(stp
->st_file
,
410 nfs4_access_to_omode(i
));
413 put_nfs4_file(stp
->st_file
);
414 kmem_cache_free(stateid_slab
, stp
);
417 static void release_lock_stateid(struct nfs4_stateid
*stp
)
421 unhash_generic_stateid(stp
);
422 file
= find_any_file(stp
->st_file
);
424 locks_remove_posix(file
, (fl_owner_t
)stp
->st_stateowner
);
425 free_generic_stateid(stp
);
428 static void unhash_lockowner(struct nfs4_stateowner
*sop
)
430 struct nfs4_stateid
*stp
;
432 list_del(&sop
->so_idhash
);
433 list_del(&sop
->so_strhash
);
434 list_del(&sop
->so_perstateid
);
435 while (!list_empty(&sop
->so_stateids
)) {
436 stp
= list_first_entry(&sop
->so_stateids
,
437 struct nfs4_stateid
, st_perstateowner
);
438 release_lock_stateid(stp
);
442 static void release_lockowner(struct nfs4_stateowner
*sop
)
444 unhash_lockowner(sop
);
445 nfs4_put_stateowner(sop
);
449 release_stateid_lockowners(struct nfs4_stateid
*open_stp
)
451 struct nfs4_stateowner
*lock_sop
;
453 while (!list_empty(&open_stp
->st_lockowners
)) {
454 lock_sop
= list_entry(open_stp
->st_lockowners
.next
,
455 struct nfs4_stateowner
, so_perstateid
);
456 /* list_del(&open_stp->st_lockowners); */
457 BUG_ON(lock_sop
->so_is_open_owner
);
458 release_lockowner(lock_sop
);
462 static void release_open_stateid(struct nfs4_stateid
*stp
)
464 unhash_generic_stateid(stp
);
465 release_stateid_lockowners(stp
);
466 free_generic_stateid(stp
);
469 static void unhash_openowner(struct nfs4_stateowner
*sop
)
471 struct nfs4_stateid
*stp
;
473 list_del(&sop
->so_idhash
);
474 list_del(&sop
->so_strhash
);
475 list_del(&sop
->so_perclient
);
476 list_del(&sop
->so_perstateid
); /* XXX: necessary? */
477 while (!list_empty(&sop
->so_stateids
)) {
478 stp
= list_first_entry(&sop
->so_stateids
,
479 struct nfs4_stateid
, st_perstateowner
);
480 release_open_stateid(stp
);
484 static void release_openowner(struct nfs4_stateowner
*sop
)
486 unhash_openowner(sop
);
487 list_del(&sop
->so_close_lru
);
488 nfs4_put_stateowner(sop
);
491 #define SESSION_HASH_SIZE 512
492 static struct list_head sessionid_hashtbl
[SESSION_HASH_SIZE
];
495 hash_sessionid(struct nfs4_sessionid
*sessionid
)
497 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
499 return sid
->sequence
% SESSION_HASH_SIZE
;
503 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
505 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
506 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
510 gen_sessionid(struct nfsd4_session
*ses
)
512 struct nfs4_client
*clp
= ses
->se_client
;
513 struct nfsd4_sessionid
*sid
;
515 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
516 sid
->clientid
= clp
->cl_clientid
;
517 sid
->sequence
= current_sessionid
++;
522 * The protocol defines ca_maxresponssize_cached to include the size of
523 * the rpc header, but all we need to cache is the data starting after
524 * the end of the initial SEQUENCE operation--the rest we regenerate
525 * each time. Therefore we can advertise a ca_maxresponssize_cached
526 * value that is the number of bytes in our cache plus a few additional
527 * bytes. In order to stay on the safe side, and not promise more than
528 * we can cache, those additional bytes must be the minimum possible: 24
529 * bytes of rpc header (xid through accept state, with AUTH_NULL
530 * verifier), 12 for the compound header (with zero-length tag), and 44
531 * for the SEQUENCE op response:
533 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
536 free_session_slots(struct nfsd4_session
*ses
)
540 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
541 kfree(ses
->se_slots
[i
]);
545 * We don't actually need to cache the rpc and session headers, so we
546 * can allocate a little less for each slot:
548 static inline int slot_bytes(struct nfsd4_channel_attrs
*ca
)
550 return ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
553 static int nfsd4_sanitize_slot_size(u32 size
)
555 size
-= NFSD_MIN_HDR_SEQ_SZ
; /* We don't cache the rpc header */
556 size
= min_t(u32
, size
, NFSD_SLOT_CACHE_SIZE
);
562 * XXX: If we run out of reserved DRC memory we could (up to a point)
563 * re-negotiate active sessions and reduce their slot usage to make
564 * rooom for new connections. For now we just fail the create session.
566 static int nfsd4_get_drc_mem(int slotsize
, u32 num
)
570 num
= min_t(u32
, num
, NFSD_MAX_SLOTS_PER_SESSION
);
572 spin_lock(&nfsd_drc_lock
);
573 avail
= min_t(int, NFSD_MAX_MEM_PER_SESSION
,
574 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
575 num
= min_t(int, num
, avail
/ slotsize
);
576 nfsd_drc_mem_used
+= num
* slotsize
;
577 spin_unlock(&nfsd_drc_lock
);
582 static void nfsd4_put_drc_mem(int slotsize
, int num
)
584 spin_lock(&nfsd_drc_lock
);
585 nfsd_drc_mem_used
-= slotsize
* num
;
586 spin_unlock(&nfsd_drc_lock
);
589 static struct nfsd4_session
*alloc_session(int slotsize
, int numslots
)
591 struct nfsd4_session
*new;
594 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
595 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
596 mem
= numslots
* sizeof(struct nfsd4_slot
*);
598 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
601 /* allocate each struct nfsd4_slot and data cache in one piece */
602 for (i
= 0; i
< numslots
; i
++) {
603 mem
= sizeof(struct nfsd4_slot
) + slotsize
;
604 new->se_slots
[i
] = kzalloc(mem
, GFP_KERNEL
);
605 if (!new->se_slots
[i
])
611 kfree(new->se_slots
[i
]);
616 static void init_forechannel_attrs(struct nfsd4_channel_attrs
*new, struct nfsd4_channel_attrs
*req
, int numslots
, int slotsize
)
618 u32 maxrpc
= nfsd_serv
->sv_max_mesg
;
620 new->maxreqs
= numslots
;
621 new->maxresp_cached
= min_t(u32
, req
->maxresp_cached
,
622 slotsize
+ NFSD_MIN_HDR_SEQ_SZ
);
623 new->maxreq_sz
= min_t(u32
, req
->maxreq_sz
, maxrpc
);
624 new->maxresp_sz
= min_t(u32
, req
->maxresp_sz
, maxrpc
);
625 new->maxops
= min_t(u32
, req
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
628 static void free_conn(struct nfsd4_conn
*c
)
630 svc_xprt_put(c
->cn_xprt
);
634 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
636 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
637 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
639 spin_lock(&clp
->cl_lock
);
640 if (!list_empty(&c
->cn_persession
)) {
641 list_del(&c
->cn_persession
);
644 spin_unlock(&clp
->cl_lock
);
645 nfsd4_probe_callback(clp
);
648 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
650 struct nfsd4_conn
*conn
;
652 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
655 svc_xprt_get(rqstp
->rq_xprt
);
656 conn
->cn_xprt
= rqstp
->rq_xprt
;
657 conn
->cn_flags
= flags
;
658 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
662 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
664 conn
->cn_session
= ses
;
665 list_add(&conn
->cn_persession
, &ses
->se_conns
);
668 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
670 struct nfs4_client
*clp
= ses
->se_client
;
672 spin_lock(&clp
->cl_lock
);
673 __nfsd4_hash_conn(conn
, ses
);
674 spin_unlock(&clp
->cl_lock
);
677 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
679 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
680 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
683 static __be32
nfsd4_new_conn(struct svc_rqst
*rqstp
, struct nfsd4_session
*ses
, u32 dir
)
685 struct nfsd4_conn
*conn
;
688 conn
= alloc_conn(rqstp
, dir
);
690 return nfserr_jukebox
;
691 nfsd4_hash_conn(conn
, ses
);
692 ret
= nfsd4_register_conn(conn
);
694 /* oops; xprt is already down: */
695 nfsd4_conn_lost(&conn
->cn_xpt_user
);
699 static __be32
nfsd4_new_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_session
*ses
)
701 u32 dir
= NFS4_CDFC4_FORE
;
703 if (ses
->se_flags
& SESSION4_BACK_CHAN
)
704 dir
|= NFS4_CDFC4_BACK
;
706 return nfsd4_new_conn(rqstp
, ses
, dir
);
709 /* must be called under client_lock */
710 static void nfsd4_del_conns(struct nfsd4_session
*s
)
712 struct nfs4_client
*clp
= s
->se_client
;
713 struct nfsd4_conn
*c
;
715 spin_lock(&clp
->cl_lock
);
716 while (!list_empty(&s
->se_conns
)) {
717 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
718 list_del_init(&c
->cn_persession
);
719 spin_unlock(&clp
->cl_lock
);
721 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
724 spin_lock(&clp
->cl_lock
);
726 spin_unlock(&clp
->cl_lock
);
729 void free_session(struct kref
*kref
)
731 struct nfsd4_session
*ses
;
734 ses
= container_of(kref
, struct nfsd4_session
, se_ref
);
735 nfsd4_del_conns(ses
);
736 spin_lock(&nfsd_drc_lock
);
737 mem
= ses
->se_fchannel
.maxreqs
* slot_bytes(&ses
->se_fchannel
);
738 nfsd_drc_mem_used
-= mem
;
739 spin_unlock(&nfsd_drc_lock
);
740 free_session_slots(ses
);
744 static struct nfsd4_session
*alloc_init_session(struct svc_rqst
*rqstp
, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
746 struct nfsd4_session
*new;
747 struct nfsd4_channel_attrs
*fchan
= &cses
->fore_channel
;
748 int numslots
, slotsize
;
753 * Note decreasing slot size below client's request may
754 * make it difficult for client to function correctly, whereas
755 * decreasing the number of slots will (just?) affect
756 * performance. When short on memory we therefore prefer to
757 * decrease number of slots instead of their size.
759 slotsize
= nfsd4_sanitize_slot_size(fchan
->maxresp_cached
);
760 numslots
= nfsd4_get_drc_mem(slotsize
, fchan
->maxreqs
);
764 new = alloc_session(slotsize
, numslots
);
766 nfsd4_put_drc_mem(slotsize
, fchan
->maxreqs
);
769 init_forechannel_attrs(&new->se_fchannel
, fchan
, numslots
, slotsize
);
771 new->se_client
= clp
;
774 INIT_LIST_HEAD(&new->se_conns
);
776 new->se_cb_seq_nr
= 1;
777 new->se_flags
= cses
->flags
;
778 new->se_cb_prog
= cses
->callback_prog
;
779 kref_init(&new->se_ref
);
780 idx
= hash_sessionid(&new->se_sessionid
);
781 spin_lock(&client_lock
);
782 list_add(&new->se_hash
, &sessionid_hashtbl
[idx
]);
783 spin_lock(&clp
->cl_lock
);
784 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
785 spin_unlock(&clp
->cl_lock
);
786 spin_unlock(&client_lock
);
788 status
= nfsd4_new_conn_from_crses(rqstp
, new);
789 /* whoops: benny points out, status is ignored! (err, or bogus) */
791 free_session(&new->se_ref
);
794 if (cses
->flags
& SESSION4_BACK_CHAN
) {
795 struct sockaddr
*sa
= svc_addr(rqstp
);
797 * This is a little silly; with sessions there's no real
798 * use for the callback address. Use the peer address
799 * as a reasonable default for now, but consider fixing
800 * the rpc client not to require an address in the
803 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
804 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
806 nfsd4_probe_callback(clp
);
810 /* caller must hold client_lock */
811 static struct nfsd4_session
*
812 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
)
814 struct nfsd4_session
*elem
;
817 dump_sessionid(__func__
, sessionid
);
818 idx
= hash_sessionid(sessionid
);
819 /* Search in the appropriate list */
820 list_for_each_entry(elem
, &sessionid_hashtbl
[idx
], se_hash
) {
821 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
822 NFS4_MAX_SESSIONID_LEN
)) {
827 dprintk("%s: session not found\n", __func__
);
831 /* caller must hold client_lock */
833 unhash_session(struct nfsd4_session
*ses
)
835 list_del(&ses
->se_hash
);
836 spin_lock(&ses
->se_client
->cl_lock
);
837 list_del(&ses
->se_perclnt
);
838 spin_unlock(&ses
->se_client
->cl_lock
);
841 /* must be called under the client_lock */
843 renew_client_locked(struct nfs4_client
*clp
)
845 if (is_client_expired(clp
)) {
846 dprintk("%s: client (clientid %08x/%08x) already expired\n",
848 clp
->cl_clientid
.cl_boot
,
849 clp
->cl_clientid
.cl_id
);
854 * Move client to the end to the LRU list.
856 dprintk("renewing client (clientid %08x/%08x)\n",
857 clp
->cl_clientid
.cl_boot
,
858 clp
->cl_clientid
.cl_id
);
859 list_move_tail(&clp
->cl_lru
, &client_lru
);
860 clp
->cl_time
= get_seconds();
864 renew_client(struct nfs4_client
*clp
)
866 spin_lock(&client_lock
);
867 renew_client_locked(clp
);
868 spin_unlock(&client_lock
);
871 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
873 STALE_CLIENTID(clientid_t
*clid
)
875 if (clid
->cl_boot
== boot_time
)
877 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
878 clid
->cl_boot
, clid
->cl_id
, boot_time
);
883 * XXX Should we use a slab cache ?
884 * This type of memory management is somewhat inefficient, but we use it
885 * anyway since SETCLIENTID is not a common operation.
887 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
889 struct nfs4_client
*clp
;
891 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
894 clp
->cl_name
.data
= kmalloc(name
.len
, GFP_KERNEL
);
895 if (clp
->cl_name
.data
== NULL
) {
899 memcpy(clp
->cl_name
.data
, name
.data
, name
.len
);
900 clp
->cl_name
.len
= name
.len
;
905 free_client(struct nfs4_client
*clp
)
907 while (!list_empty(&clp
->cl_sessions
)) {
908 struct nfsd4_session
*ses
;
909 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
911 list_del(&ses
->se_perclnt
);
912 nfsd4_put_session(ses
);
914 if (clp
->cl_cred
.cr_group_info
)
915 put_group_info(clp
->cl_cred
.cr_group_info
);
916 kfree(clp
->cl_principal
);
917 kfree(clp
->cl_name
.data
);
922 release_session_client(struct nfsd4_session
*session
)
924 struct nfs4_client
*clp
= session
->se_client
;
926 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &client_lock
))
928 if (is_client_expired(clp
)) {
930 session
->se_client
= NULL
;
932 renew_client_locked(clp
);
933 spin_unlock(&client_lock
);
936 /* must be called under the client_lock */
938 unhash_client_locked(struct nfs4_client
*clp
)
940 struct nfsd4_session
*ses
;
942 mark_client_expired(clp
);
943 list_del(&clp
->cl_lru
);
944 spin_lock(&clp
->cl_lock
);
945 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
946 list_del_init(&ses
->se_hash
);
947 spin_unlock(&clp
->cl_lock
);
951 expire_client(struct nfs4_client
*clp
)
953 struct nfs4_stateowner
*sop
;
954 struct nfs4_delegation
*dp
;
955 struct list_head reaplist
;
957 INIT_LIST_HEAD(&reaplist
);
958 spin_lock(&recall_lock
);
959 while (!list_empty(&clp
->cl_delegations
)) {
960 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
961 list_del_init(&dp
->dl_perclnt
);
962 list_move(&dp
->dl_recall_lru
, &reaplist
);
964 spin_unlock(&recall_lock
);
965 while (!list_empty(&reaplist
)) {
966 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
967 list_del_init(&dp
->dl_recall_lru
);
968 unhash_delegation(dp
);
970 while (!list_empty(&clp
->cl_openowners
)) {
971 sop
= list_entry(clp
->cl_openowners
.next
, struct nfs4_stateowner
, so_perclient
);
972 release_openowner(sop
);
974 nfsd4_shutdown_callback(clp
);
975 if (clp
->cl_cb_conn
.cb_xprt
)
976 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
977 list_del(&clp
->cl_idhash
);
978 list_del(&clp
->cl_strhash
);
979 spin_lock(&client_lock
);
980 unhash_client_locked(clp
);
981 if (atomic_read(&clp
->cl_refcount
) == 0)
983 spin_unlock(&client_lock
);
986 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
988 memcpy(target
->cl_verifier
.data
, source
->data
,
989 sizeof(target
->cl_verifier
.data
));
992 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
994 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
995 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
998 static void copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1000 target
->cr_uid
= source
->cr_uid
;
1001 target
->cr_gid
= source
->cr_gid
;
1002 target
->cr_group_info
= source
->cr_group_info
;
1003 get_group_info(target
->cr_group_info
);
1006 static int same_name(const char *n1
, const char *n2
)
1008 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1012 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1014 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1018 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1020 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1023 /* XXX what about NGROUP */
1025 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1027 return cr1
->cr_uid
== cr2
->cr_uid
;
1030 static void gen_clid(struct nfs4_client
*clp
)
1032 static u32 current_clientid
= 1;
1034 clp
->cl_clientid
.cl_boot
= boot_time
;
1035 clp
->cl_clientid
.cl_id
= current_clientid
++;
1038 static void gen_confirm(struct nfs4_client
*clp
)
1043 p
= (u32
*)clp
->cl_confirm
.data
;
1044 *p
++ = get_seconds();
1048 static struct nfs4_client
*create_client(struct xdr_netobj name
, char *recdir
,
1049 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1051 struct nfs4_client
*clp
;
1052 struct sockaddr
*sa
= svc_addr(rqstp
);
1055 clp
= alloc_client(name
);
1059 INIT_LIST_HEAD(&clp
->cl_sessions
);
1061 princ
= svc_gss_principal(rqstp
);
1063 clp
->cl_principal
= kstrdup(princ
, GFP_KERNEL
);
1064 if (clp
->cl_principal
== NULL
) {
1070 memcpy(clp
->cl_recdir
, recdir
, HEXDIR_LEN
);
1071 atomic_set(&clp
->cl_refcount
, 0);
1072 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1073 INIT_LIST_HEAD(&clp
->cl_idhash
);
1074 INIT_LIST_HEAD(&clp
->cl_strhash
);
1075 INIT_LIST_HEAD(&clp
->cl_openowners
);
1076 INIT_LIST_HEAD(&clp
->cl_delegations
);
1077 INIT_LIST_HEAD(&clp
->cl_lru
);
1078 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1079 spin_lock_init(&clp
->cl_lock
);
1080 INIT_WORK(&clp
->cl_cb_null
.cb_work
, nfsd4_do_callback_rpc
);
1081 clp
->cl_time
= get_seconds();
1082 clear_bit(0, &clp
->cl_cb_slot_busy
);
1083 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1084 copy_verf(clp
, verf
);
1085 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1086 clp
->cl_flavor
= rqstp
->rq_flavor
;
1087 copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1089 clp
->cl_cb_session
= NULL
;
1093 static int check_name(struct xdr_netobj name
)
1097 if (name
.len
> NFS4_OPAQUE_LIMIT
) {
1098 dprintk("NFSD: check_name: name too long(%d)!\n", name
.len
);
1105 add_to_unconfirmed(struct nfs4_client
*clp
, unsigned int strhashval
)
1107 unsigned int idhashval
;
1109 list_add(&clp
->cl_strhash
, &unconf_str_hashtbl
[strhashval
]);
1110 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1111 list_add(&clp
->cl_idhash
, &unconf_id_hashtbl
[idhashval
]);
1116 move_to_confirmed(struct nfs4_client
*clp
)
1118 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1119 unsigned int strhashval
;
1121 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1122 list_move(&clp
->cl_idhash
, &conf_id_hashtbl
[idhashval
]);
1123 strhashval
= clientstr_hashval(clp
->cl_recdir
);
1124 list_move(&clp
->cl_strhash
, &conf_str_hashtbl
[strhashval
]);
1128 static struct nfs4_client
*
1129 find_confirmed_client(clientid_t
*clid
)
1131 struct nfs4_client
*clp
;
1132 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1134 list_for_each_entry(clp
, &conf_id_hashtbl
[idhashval
], cl_idhash
) {
1135 if (same_clid(&clp
->cl_clientid
, clid
))
1141 static struct nfs4_client
*
1142 find_unconfirmed_client(clientid_t
*clid
)
1144 struct nfs4_client
*clp
;
1145 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1147 list_for_each_entry(clp
, &unconf_id_hashtbl
[idhashval
], cl_idhash
) {
1148 if (same_clid(&clp
->cl_clientid
, clid
))
1154 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1156 return clp
->cl_exchange_flags
!= 0;
1159 static struct nfs4_client
*
1160 find_confirmed_client_by_str(const char *dname
, unsigned int hashval
)
1162 struct nfs4_client
*clp
;
1164 list_for_each_entry(clp
, &conf_str_hashtbl
[hashval
], cl_strhash
) {
1165 if (same_name(clp
->cl_recdir
, dname
))
1171 static struct nfs4_client
*
1172 find_unconfirmed_client_by_str(const char *dname
, unsigned int hashval
)
1174 struct nfs4_client
*clp
;
1176 list_for_each_entry(clp
, &unconf_str_hashtbl
[hashval
], cl_strhash
) {
1177 if (same_name(clp
->cl_recdir
, dname
))
1183 static void rpc_svcaddr2sockaddr(struct sockaddr
*sa
, unsigned short family
, union svc_addr_u
*svcaddr
)
1187 ((struct sockaddr_in
*)sa
)->sin_family
= AF_INET
;
1188 ((struct sockaddr_in
*)sa
)->sin_addr
= svcaddr
->addr
;
1191 ((struct sockaddr_in6
*)sa
)->sin6_family
= AF_INET6
;
1192 ((struct sockaddr_in6
*)sa
)->sin6_addr
= svcaddr
->addr6
;
1198 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1200 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1201 struct sockaddr
*sa
= svc_addr(rqstp
);
1202 u32 scopeid
= rpc_get_scope_id(sa
);
1203 unsigned short expected_family
;
1205 /* Currently, we only support tcp and tcp6 for the callback channel */
1206 if (se
->se_callback_netid_len
== 3 &&
1207 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1208 expected_family
= AF_INET
;
1209 else if (se
->se_callback_netid_len
== 4 &&
1210 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1211 expected_family
= AF_INET6
;
1215 conn
->cb_addrlen
= rpc_uaddr2sockaddr(se
->se_callback_addr_val
,
1216 se
->se_callback_addr_len
,
1217 (struct sockaddr
*)&conn
->cb_addr
,
1218 sizeof(conn
->cb_addr
));
1220 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1223 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1224 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1226 conn
->cb_prog
= se
->se_callback_prog
;
1227 conn
->cb_ident
= se
->se_callback_ident
;
1228 rpc_svcaddr2sockaddr((struct sockaddr
*)&conn
->cb_saddr
, expected_family
, &rqstp
->rq_daddr
);
1231 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1232 conn
->cb_addrlen
= 0;
1233 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1234 "will not receive delegations\n",
1235 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1241 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1244 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1246 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1249 dprintk("--> %s slot %p\n", __func__
, slot
);
1251 slot
->sl_opcnt
= resp
->opcnt
;
1252 slot
->sl_status
= resp
->cstate
.status
;
1254 if (nfsd4_not_cached(resp
)) {
1255 slot
->sl_datalen
= 0;
1258 slot
->sl_datalen
= (char *)resp
->p
- (char *)resp
->cstate
.datap
;
1259 base
= (char *)resp
->cstate
.datap
-
1260 (char *)resp
->xbuf
->head
[0].iov_base
;
1261 if (read_bytes_from_xdr_buf(resp
->xbuf
, base
, slot
->sl_data
,
1263 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1268 * Encode the replay sequence operation from the slot values.
1269 * If cachethis is FALSE encode the uncached rep error on the next
1270 * operation which sets resp->p and increments resp->opcnt for
1271 * nfs4svc_encode_compoundres.
1275 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1276 struct nfsd4_compoundres
*resp
)
1278 struct nfsd4_op
*op
;
1279 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1281 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__
,
1282 resp
->opcnt
, resp
->cstate
.slot
->sl_cachethis
);
1284 /* Encode the replayed sequence operation */
1285 op
= &args
->ops
[resp
->opcnt
- 1];
1286 nfsd4_encode_operation(resp
, op
);
1288 /* Return nfserr_retry_uncached_rep in next operation. */
1289 if (args
->opcnt
> 1 && slot
->sl_cachethis
== 0) {
1290 op
= &args
->ops
[resp
->opcnt
++];
1291 op
->status
= nfserr_retry_uncached_rep
;
1292 nfsd4_encode_operation(resp
, op
);
1298 * The sequence operation is not cached because we can use the slot and
1302 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1303 struct nfsd4_sequence
*seq
)
1305 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1308 dprintk("--> %s slot %p\n", __func__
, slot
);
1310 /* Either returns 0 or nfserr_retry_uncached */
1311 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1312 if (status
== nfserr_retry_uncached_rep
)
1315 /* The sequence operation has been encoded, cstate->datap set. */
1316 memcpy(resp
->cstate
.datap
, slot
->sl_data
, slot
->sl_datalen
);
1318 resp
->opcnt
= slot
->sl_opcnt
;
1319 resp
->p
= resp
->cstate
.datap
+ XDR_QUADLEN(slot
->sl_datalen
);
1320 status
= slot
->sl_status
;
1326 * Set the exchange_id flags returned by the server.
1329 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1331 /* pNFS is not supported */
1332 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1334 /* Referrals are supported, Migration is not. */
1335 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1337 /* set the wire flags to return to client. */
1338 clid
->flags
= new->cl_exchange_flags
;
1342 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1343 struct nfsd4_compound_state
*cstate
,
1344 struct nfsd4_exchange_id
*exid
)
1346 struct nfs4_client
*unconf
, *conf
, *new;
1348 unsigned int strhashval
;
1349 char dname
[HEXDIR_LEN
];
1350 char addr_str
[INET6_ADDRSTRLEN
];
1351 nfs4_verifier verf
= exid
->verifier
;
1352 struct sockaddr
*sa
= svc_addr(rqstp
);
1354 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
1355 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1356 "ip_addr=%s flags %x, spa_how %d\n",
1357 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
1358 addr_str
, exid
->flags
, exid
->spa_how
);
1360 if (!check_name(exid
->clname
) || (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
))
1361 return nfserr_inval
;
1363 /* Currently only support SP4_NONE */
1364 switch (exid
->spa_how
) {
1368 return nfserr_serverfault
;
1370 BUG(); /* checked by xdr code */
1372 return nfserr_serverfault
; /* no excuse :-/ */
1375 status
= nfs4_make_rec_clidname(dname
, &exid
->clname
);
1380 strhashval
= clientstr_hashval(dname
);
1385 conf
= find_confirmed_client_by_str(dname
, strhashval
);
1387 if (!clp_used_exchangeid(conf
)) {
1388 status
= nfserr_clid_inuse
; /* XXX: ? */
1391 if (!same_verf(&verf
, &conf
->cl_verifier
)) {
1392 /* 18.35.4 case 8 */
1393 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1394 status
= nfserr_not_same
;
1397 /* Client reboot: destroy old state */
1398 expire_client(conf
);
1401 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
1402 /* 18.35.4 case 9 */
1403 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1404 status
= nfserr_perm
;
1407 expire_client(conf
);
1411 * Set bit when the owner id and verifier map to an already
1412 * confirmed client id (18.35.3).
1414 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1417 * Falling into 18.35.4 case 2, possible router replay.
1418 * Leave confirmed record intact and return same result.
1420 copy_verf(conf
, &verf
);
1425 /* 18.35.4 case 7 */
1426 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1427 status
= nfserr_noent
;
1431 unconf
= find_unconfirmed_client_by_str(dname
, strhashval
);
1434 * Possible retry or client restart. Per 18.35.4 case 4,
1435 * a new unconfirmed record should be generated regardless
1436 * of whether any properties have changed.
1438 expire_client(unconf
);
1443 new = create_client(exid
->clname
, dname
, rqstp
, &verf
);
1445 status
= nfserr_jukebox
;
1450 add_to_unconfirmed(new, strhashval
);
1452 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
1453 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
1456 nfsd4_set_ex_flags(new, exid
);
1458 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1459 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
1463 nfs4_unlock_state();
1465 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status
));
1470 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
1472 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
1475 /* The slot is in use, and no response has been sent. */
1477 if (seqid
== slot_seqid
)
1478 return nfserr_jukebox
;
1480 return nfserr_seq_misordered
;
1483 if (likely(seqid
== slot_seqid
+ 1))
1486 if (seqid
== slot_seqid
)
1487 return nfserr_replay_cache
;
1489 if (seqid
== 1 && (slot_seqid
+ 1) == 0)
1491 /* Misordered replay or misordered new request */
1492 return nfserr_seq_misordered
;
1496 * Cache the create session result into the create session single DRC
1497 * slot cache by saving the xdr structure. sl_seqid has been set.
1498 * Do this for solo or embedded create session operations.
1501 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
1502 struct nfsd4_clid_slot
*slot
, int nfserr
)
1504 slot
->sl_status
= nfserr
;
1505 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
1509 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
1510 struct nfsd4_clid_slot
*slot
)
1512 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
1513 return slot
->sl_status
;
1516 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1517 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1518 1 + /* MIN tag is length with zero, only length */ \
1519 3 + /* version, opcount, opcode */ \
1520 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1521 /* seqid, slotID, slotID, cache */ \
1522 4 ) * sizeof(__be32))
1524 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1525 2 + /* verifier: AUTH_NULL, length 0 */\
1527 1 + /* MIN tag is length with zero, only length */ \
1528 3 + /* opcount, opcode, opstatus*/ \
1529 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1530 /* seqid, slotID, slotID, slotID, status */ \
1531 5 ) * sizeof(__be32))
1533 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs fchannel
)
1535 return fchannel
.maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
1536 || fchannel
.maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
;
1540 nfsd4_create_session(struct svc_rqst
*rqstp
,
1541 struct nfsd4_compound_state
*cstate
,
1542 struct nfsd4_create_session
*cr_ses
)
1544 struct sockaddr
*sa
= svc_addr(rqstp
);
1545 struct nfs4_client
*conf
, *unconf
;
1546 struct nfsd4_session
*new;
1547 struct nfsd4_clid_slot
*cs_slot
= NULL
;
1548 bool confirm_me
= false;
1551 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
1552 return nfserr_inval
;
1555 unconf
= find_unconfirmed_client(&cr_ses
->clientid
);
1556 conf
= find_confirmed_client(&cr_ses
->clientid
);
1559 cs_slot
= &conf
->cl_cs_slot
;
1560 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1561 if (status
== nfserr_replay_cache
) {
1562 dprintk("Got a create_session replay! seqid= %d\n",
1564 /* Return the cached reply status */
1565 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
1567 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
1568 status
= nfserr_seq_misordered
;
1569 dprintk("Sequence misordered!\n");
1570 dprintk("Expected seqid= %d but got seqid= %d\n",
1571 cs_slot
->sl_seqid
, cr_ses
->seqid
);
1574 } else if (unconf
) {
1575 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
1576 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
1577 status
= nfserr_clid_inuse
;
1581 cs_slot
= &unconf
->cl_cs_slot
;
1582 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1584 /* an unconfirmed replay returns misordered */
1585 status
= nfserr_seq_misordered
;
1592 status
= nfserr_stale_clientid
;
1597 * XXX: we should probably set this at creation time, and check
1598 * for consistent minorversion use throughout:
1600 conf
->cl_minorversion
= 1;
1602 * We do not support RDMA or persistent sessions
1604 cr_ses
->flags
&= ~SESSION4_PERSIST
;
1605 cr_ses
->flags
&= ~SESSION4_RDMA
;
1607 status
= nfserr_toosmall
;
1608 if (check_forechannel_attrs(cr_ses
->fore_channel
))
1611 status
= nfserr_jukebox
;
1612 new = alloc_init_session(rqstp
, conf
, cr_ses
);
1616 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
1617 NFS4_MAX_SESSIONID_LEN
);
1618 memcpy(&cr_ses
->fore_channel
, &new->se_fchannel
,
1619 sizeof(struct nfsd4_channel_attrs
));
1620 cs_slot
->sl_seqid
++;
1621 cr_ses
->seqid
= cs_slot
->sl_seqid
;
1623 /* cache solo and embedded create sessions under the state lock */
1624 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
1626 move_to_confirmed(conf
);
1628 nfs4_unlock_state();
1629 dprintk("%s returns %d\n", __func__
, ntohl(status
));
1633 static bool nfsd4_last_compound_op(struct svc_rqst
*rqstp
)
1635 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
1636 struct nfsd4_compoundargs
*argp
= rqstp
->rq_argp
;
1638 return argp
->opcnt
== resp
->opcnt
;
1641 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
1644 case NFS4_CDFC4_FORE
:
1645 case NFS4_CDFC4_BACK
:
1647 case NFS4_CDFC4_FORE_OR_BOTH
:
1648 case NFS4_CDFC4_BACK_OR_BOTH
:
1649 *dir
= NFS4_CDFC4_BOTH
;
1652 return nfserr_inval
;
1655 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
1656 struct nfsd4_compound_state
*cstate
,
1657 struct nfsd4_bind_conn_to_session
*bcts
)
1661 if (!nfsd4_last_compound_op(rqstp
))
1662 return nfserr_not_only_op
;
1663 spin_lock(&client_lock
);
1664 cstate
->session
= find_in_sessionid_hashtbl(&bcts
->sessionid
);
1665 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1666 * client_lock iself: */
1667 if (cstate
->session
) {
1668 nfsd4_get_session(cstate
->session
);
1669 atomic_inc(&cstate
->session
->se_client
->cl_refcount
);
1671 spin_unlock(&client_lock
);
1672 if (!cstate
->session
)
1673 return nfserr_badsession
;
1675 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
1677 nfsd4_new_conn(rqstp
, cstate
->session
, bcts
->dir
);
1681 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
1685 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
1689 nfsd4_destroy_session(struct svc_rqst
*r
,
1690 struct nfsd4_compound_state
*cstate
,
1691 struct nfsd4_destroy_session
*sessionid
)
1693 struct nfsd4_session
*ses
;
1694 u32 status
= nfserr_badsession
;
1697 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1698 * - Should we return nfserr_back_chan_busy if waiting for
1699 * callbacks on to-be-destroyed session?
1700 * - Do we need to clear any callback info from previous session?
1703 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
1704 if (!nfsd4_last_compound_op(r
))
1705 return nfserr_not_only_op
;
1707 dump_sessionid(__func__
, &sessionid
->sessionid
);
1708 spin_lock(&client_lock
);
1709 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
);
1711 spin_unlock(&client_lock
);
1715 unhash_session(ses
);
1716 spin_unlock(&client_lock
);
1719 nfsd4_probe_callback_sync(ses
->se_client
);
1720 nfs4_unlock_state();
1722 nfsd4_del_conns(ses
);
1724 nfsd4_put_session(ses
);
1727 dprintk("%s returns %d\n", __func__
, ntohl(status
));
1731 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
1733 struct nfsd4_conn
*c
;
1735 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
1736 if (c
->cn_xprt
== xpt
) {
1743 static void nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
1745 struct nfs4_client
*clp
= ses
->se_client
;
1746 struct nfsd4_conn
*c
;
1749 spin_lock(&clp
->cl_lock
);
1750 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
1752 spin_unlock(&clp
->cl_lock
);
1756 __nfsd4_hash_conn(new, ses
);
1757 spin_unlock(&clp
->cl_lock
);
1758 ret
= nfsd4_register_conn(new);
1760 /* oops; xprt is already down: */
1761 nfsd4_conn_lost(&new->cn_xpt_user
);
1765 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
1767 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
1769 return args
->opcnt
> session
->se_fchannel
.maxops
;
1772 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
1773 struct nfsd4_session
*session
)
1775 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
1777 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
1781 nfsd4_sequence(struct svc_rqst
*rqstp
,
1782 struct nfsd4_compound_state
*cstate
,
1783 struct nfsd4_sequence
*seq
)
1785 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
1786 struct nfsd4_session
*session
;
1787 struct nfsd4_slot
*slot
;
1788 struct nfsd4_conn
*conn
;
1791 if (resp
->opcnt
!= 1)
1792 return nfserr_sequence_pos
;
1795 * Will be either used or freed by nfsd4_sequence_check_conn
1798 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
1800 return nfserr_jukebox
;
1802 spin_lock(&client_lock
);
1803 status
= nfserr_badsession
;
1804 session
= find_in_sessionid_hashtbl(&seq
->sessionid
);
1808 status
= nfserr_too_many_ops
;
1809 if (nfsd4_session_too_many_ops(rqstp
, session
))
1812 status
= nfserr_req_too_big
;
1813 if (nfsd4_request_too_big(rqstp
, session
))
1816 status
= nfserr_badslot
;
1817 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
1820 slot
= session
->se_slots
[seq
->slotid
];
1821 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
1823 /* We do not negotiate the number of slots yet, so set the
1824 * maxslots to the session maxreqs which is used to encode
1825 * sr_highest_slotid and the sr_target_slot id to maxslots */
1826 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
1828 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
, slot
->sl_inuse
);
1829 if (status
== nfserr_replay_cache
) {
1830 cstate
->slot
= slot
;
1831 cstate
->session
= session
;
1832 /* Return the cached reply status and set cstate->status
1833 * for nfsd4_proc_compound processing */
1834 status
= nfsd4_replay_cache_entry(resp
, seq
);
1835 cstate
->status
= nfserr_replay_cache
;
1841 nfsd4_sequence_check_conn(conn
, session
);
1844 /* Success! bump slot seqid */
1845 slot
->sl_inuse
= true;
1846 slot
->sl_seqid
= seq
->seqid
;
1847 slot
->sl_cachethis
= seq
->cachethis
;
1849 cstate
->slot
= slot
;
1850 cstate
->session
= session
;
1853 /* Hold a session reference until done processing the compound. */
1854 if (cstate
->session
) {
1855 struct nfs4_client
*clp
= session
->se_client
;
1857 nfsd4_get_session(cstate
->session
);
1858 atomic_inc(&clp
->cl_refcount
);
1859 if (clp
->cl_cb_state
== NFSD4_CB_DOWN
)
1860 seq
->status_flags
|= SEQ4_STATUS_CB_PATH_DOWN
;
1863 spin_unlock(&client_lock
);
1864 dprintk("%s: return %d\n", __func__
, ntohl(status
));
1869 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
1873 if (rc
->rca_one_fs
) {
1874 if (!cstate
->current_fh
.fh_dentry
)
1875 return nfserr_nofilehandle
;
1877 * We don't take advantage of the rca_one_fs case.
1878 * That's OK, it's optional, we can safely ignore it.
1884 status
= nfserr_complete_already
;
1885 if (cstate
->session
->se_client
->cl_firststate
)
1888 status
= nfserr_stale_clientid
;
1889 if (is_client_expired(cstate
->session
->se_client
))
1891 * The following error isn't really legal.
1892 * But we only get here if the client just explicitly
1893 * destroyed the client. Surely it no longer cares what
1894 * error it gets back on an operation for the dead
1900 nfsd4_create_clid_dir(cstate
->session
->se_client
);
1902 nfs4_unlock_state();
1907 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
1908 struct nfsd4_setclientid
*setclid
)
1910 struct xdr_netobj clname
= {
1911 .len
= setclid
->se_namelen
,
1912 .data
= setclid
->se_name
,
1914 nfs4_verifier clverifier
= setclid
->se_verf
;
1915 unsigned int strhashval
;
1916 struct nfs4_client
*conf
, *unconf
, *new;
1918 char dname
[HEXDIR_LEN
];
1920 if (!check_name(clname
))
1921 return nfserr_inval
;
1923 status
= nfs4_make_rec_clidname(dname
, &clname
);
1928 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1929 * We get here on a DRC miss.
1932 strhashval
= clientstr_hashval(dname
);
1935 conf
= find_confirmed_client_by_str(dname
, strhashval
);
1937 /* RFC 3530 14.2.33 CASE 0: */
1938 status
= nfserr_clid_inuse
;
1939 if (clp_used_exchangeid(conf
))
1941 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
1942 char addr_str
[INET6_ADDRSTRLEN
];
1943 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
1945 dprintk("NFSD: setclientid: string in use by client "
1946 "at %s\n", addr_str
);
1951 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1952 * has a description of SETCLIENTID request processing consisting
1953 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1955 unconf
= find_unconfirmed_client_by_str(dname
, strhashval
);
1956 status
= nfserr_jukebox
;
1959 * RFC 3530 14.2.33 CASE 4:
1960 * placed first, because it is the normal case
1963 expire_client(unconf
);
1964 new = create_client(clname
, dname
, rqstp
, &clverifier
);
1968 } else if (same_verf(&conf
->cl_verifier
, &clverifier
)) {
1970 * RFC 3530 14.2.33 CASE 1:
1971 * probable callback update
1974 /* Note this is removing unconfirmed {*x***},
1975 * which is stronger than RFC recommended {vxc**}.
1976 * This has the advantage that there is at most
1977 * one {*x***} in either list at any time.
1979 expire_client(unconf
);
1981 new = create_client(clname
, dname
, rqstp
, &clverifier
);
1984 copy_clid(new, conf
);
1985 } else if (!unconf
) {
1987 * RFC 3530 14.2.33 CASE 2:
1988 * probable client reboot; state will be removed if
1991 new = create_client(clname
, dname
, rqstp
, &clverifier
);
1997 * RFC 3530 14.2.33 CASE 3:
1998 * probable client reboot; state will be removed if
2001 expire_client(unconf
);
2002 new = create_client(clname
, dname
, rqstp
, &clverifier
);
2008 * XXX: we should probably set this at creation time, and check
2009 * for consistent minorversion use throughout:
2011 new->cl_minorversion
= 0;
2012 gen_callback(new, setclid
, rqstp
);
2013 add_to_unconfirmed(new, strhashval
);
2014 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2015 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2016 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2019 nfs4_unlock_state();
2025 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2026 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2027 * bullets, labeled as CASE1 - CASE4 below.
2030 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2031 struct nfsd4_compound_state
*cstate
,
2032 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2034 struct sockaddr
*sa
= svc_addr(rqstp
);
2035 struct nfs4_client
*conf
, *unconf
;
2036 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2037 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2040 if (STALE_CLIENTID(clid
))
2041 return nfserr_stale_clientid
;
2043 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2044 * We get here on a DRC miss.
2049 conf
= find_confirmed_client(clid
);
2050 unconf
= find_unconfirmed_client(clid
);
2052 status
= nfserr_clid_inuse
;
2053 if (conf
&& !rpc_cmp_addr((struct sockaddr
*) &conf
->cl_addr
, sa
))
2055 if (unconf
&& !rpc_cmp_addr((struct sockaddr
*) &unconf
->cl_addr
, sa
))
2059 * section 14.2.34 of RFC 3530 has a description of
2060 * SETCLIENTID_CONFIRM request processing consisting
2061 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2063 if (conf
&& unconf
&& same_verf(&confirm
, &unconf
->cl_confirm
)) {
2065 * RFC 3530 14.2.34 CASE 1:
2068 if (!same_creds(&conf
->cl_cred
, &unconf
->cl_cred
))
2069 status
= nfserr_clid_inuse
;
2071 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
2072 nfsd4_probe_callback(conf
);
2073 expire_client(unconf
);
2077 } else if (conf
&& !unconf
) {
2079 * RFC 3530 14.2.34 CASE 2:
2080 * probable retransmitted request; play it safe and
2083 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
2084 status
= nfserr_clid_inuse
;
2087 } else if (!conf
&& unconf
2088 && same_verf(&unconf
->cl_confirm
, &confirm
)) {
2090 * RFC 3530 14.2.34 CASE 3:
2091 * Normal case; new or rebooted client:
2093 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
)) {
2094 status
= nfserr_clid_inuse
;
2097 clientstr_hashval(unconf
->cl_recdir
);
2098 conf
= find_confirmed_client_by_str(unconf
->cl_recdir
,
2101 nfsd4_remove_clid_dir(conf
);
2102 expire_client(conf
);
2104 move_to_confirmed(unconf
);
2106 nfsd4_probe_callback(conf
);
2109 } else if ((!conf
|| (conf
&& !same_verf(&conf
->cl_confirm
, &confirm
)))
2110 && (!unconf
|| (unconf
&& !same_verf(&unconf
->cl_confirm
,
2113 * RFC 3530 14.2.34 CASE 4:
2114 * Client probably hasn't noticed that we rebooted yet.
2116 status
= nfserr_stale_clientid
;
2118 /* check that we have hit one of the cases...*/
2119 status
= nfserr_clid_inuse
;
2122 nfs4_unlock_state();
2126 /* OPEN Share state helper functions */
2127 static inline struct nfs4_file
*
2128 alloc_init_file(struct inode
*ino
)
2130 struct nfs4_file
*fp
;
2131 unsigned int hashval
= file_hashval(ino
);
2133 fp
= kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2135 atomic_set(&fp
->fi_ref
, 1);
2136 INIT_LIST_HEAD(&fp
->fi_hash
);
2137 INIT_LIST_HEAD(&fp
->fi_stateids
);
2138 INIT_LIST_HEAD(&fp
->fi_delegations
);
2139 fp
->fi_inode
= igrab(ino
);
2140 fp
->fi_id
= current_fileid
++;
2141 fp
->fi_had_conflict
= false;
2142 fp
->fi_lease
= NULL
;
2143 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
2144 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
2145 spin_lock(&recall_lock
);
2146 list_add(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
2147 spin_unlock(&recall_lock
);
2154 nfsd4_free_slab(struct kmem_cache
**slab
)
2158 kmem_cache_destroy(*slab
);
2163 nfsd4_free_slabs(void)
2165 nfsd4_free_slab(&stateowner_slab
);
2166 nfsd4_free_slab(&file_slab
);
2167 nfsd4_free_slab(&stateid_slab
);
2168 nfsd4_free_slab(&deleg_slab
);
2172 nfsd4_init_slabs(void)
2174 stateowner_slab
= kmem_cache_create("nfsd4_stateowners",
2175 sizeof(struct nfs4_stateowner
), 0, 0, NULL
);
2176 if (stateowner_slab
== NULL
)
2178 file_slab
= kmem_cache_create("nfsd4_files",
2179 sizeof(struct nfs4_file
), 0, 0, NULL
);
2180 if (file_slab
== NULL
)
2182 stateid_slab
= kmem_cache_create("nfsd4_stateids",
2183 sizeof(struct nfs4_stateid
), 0, 0, NULL
);
2184 if (stateid_slab
== NULL
)
2186 deleg_slab
= kmem_cache_create("nfsd4_delegations",
2187 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
2188 if (deleg_slab
== NULL
)
2193 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2198 nfs4_free_stateowner(struct kref
*kref
)
2200 struct nfs4_stateowner
*sop
=
2201 container_of(kref
, struct nfs4_stateowner
, so_ref
);
2202 kfree(sop
->so_owner
.data
);
2203 kmem_cache_free(stateowner_slab
, sop
);
2206 static inline struct nfs4_stateowner
*
2207 alloc_stateowner(struct xdr_netobj
*owner
)
2209 struct nfs4_stateowner
*sop
;
2211 if ((sop
= kmem_cache_alloc(stateowner_slab
, GFP_KERNEL
))) {
2212 if ((sop
->so_owner
.data
= kmalloc(owner
->len
, GFP_KERNEL
))) {
2213 memcpy(sop
->so_owner
.data
, owner
->data
, owner
->len
);
2214 sop
->so_owner
.len
= owner
->len
;
2215 kref_init(&sop
->so_ref
);
2218 kmem_cache_free(stateowner_slab
, sop
);
2223 static struct nfs4_stateowner
*
2224 alloc_init_open_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfsd4_open
*open
) {
2225 struct nfs4_stateowner
*sop
;
2226 struct nfs4_replay
*rp
;
2227 unsigned int idhashval
;
2229 if (!(sop
= alloc_stateowner(&open
->op_owner
)))
2231 idhashval
= ownerid_hashval(current_ownerid
);
2232 INIT_LIST_HEAD(&sop
->so_idhash
);
2233 INIT_LIST_HEAD(&sop
->so_strhash
);
2234 INIT_LIST_HEAD(&sop
->so_perclient
);
2235 INIT_LIST_HEAD(&sop
->so_stateids
);
2236 INIT_LIST_HEAD(&sop
->so_perstateid
); /* not used */
2237 INIT_LIST_HEAD(&sop
->so_close_lru
);
2239 list_add(&sop
->so_idhash
, &ownerid_hashtbl
[idhashval
]);
2240 list_add(&sop
->so_strhash
, &ownerstr_hashtbl
[strhashval
]);
2241 list_add(&sop
->so_perclient
, &clp
->cl_openowners
);
2242 sop
->so_is_open_owner
= 1;
2243 sop
->so_id
= current_ownerid
++;
2244 sop
->so_client
= clp
;
2245 sop
->so_seqid
= open
->op_seqid
;
2246 sop
->so_confirmed
= 0;
2247 rp
= &sop
->so_replay
;
2248 rp
->rp_status
= nfserr_serverfault
;
2250 rp
->rp_buf
= rp
->rp_ibuf
;
2255 init_stateid(struct nfs4_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
2256 struct nfs4_stateowner
*sop
= open
->op_stateowner
;
2257 unsigned int hashval
= stateid_hashval(sop
->so_id
, fp
->fi_id
);
2259 INIT_LIST_HEAD(&stp
->st_hash
);
2260 INIT_LIST_HEAD(&stp
->st_perstateowner
);
2261 INIT_LIST_HEAD(&stp
->st_lockowners
);
2262 INIT_LIST_HEAD(&stp
->st_perfile
);
2263 list_add(&stp
->st_hash
, &stateid_hashtbl
[hashval
]);
2264 list_add(&stp
->st_perstateowner
, &sop
->so_stateids
);
2265 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2266 stp
->st_stateowner
= sop
;
2269 stp
->st_stateid
.si_boot
= boot_time
;
2270 stp
->st_stateid
.si_stateownerid
= sop
->so_id
;
2271 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
2272 stp
->st_stateid
.si_generation
= 0;
2273 stp
->st_access_bmap
= 0;
2274 stp
->st_deny_bmap
= 0;
2275 __set_bit(open
->op_share_access
& ~NFS4_SHARE_WANT_MASK
,
2276 &stp
->st_access_bmap
);
2277 __set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
2278 stp
->st_openstp
= NULL
;
2282 move_to_close_lru(struct nfs4_stateowner
*sop
)
2284 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop
);
2286 list_move_tail(&sop
->so_close_lru
, &close_lru
);
2287 sop
->so_time
= get_seconds();
2291 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
2294 return (sop
->so_owner
.len
== owner
->len
) &&
2295 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
2296 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
);
2299 static struct nfs4_stateowner
*
2300 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
)
2302 struct nfs4_stateowner
*so
= NULL
;
2304 list_for_each_entry(so
, &ownerstr_hashtbl
[hashval
], so_strhash
) {
2305 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
))
2311 /* search file_hashtbl[] for file */
2312 static struct nfs4_file
*
2313 find_file(struct inode
*ino
)
2315 unsigned int hashval
= file_hashval(ino
);
2316 struct nfs4_file
*fp
;
2318 spin_lock(&recall_lock
);
2319 list_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
2320 if (fp
->fi_inode
== ino
) {
2322 spin_unlock(&recall_lock
);
2326 spin_unlock(&recall_lock
);
2330 static inline int access_valid(u32 x
, u32 minorversion
)
2332 if ((x
& NFS4_SHARE_ACCESS_MASK
) < NFS4_SHARE_ACCESS_READ
)
2334 if ((x
& NFS4_SHARE_ACCESS_MASK
) > NFS4_SHARE_ACCESS_BOTH
)
2336 x
&= ~NFS4_SHARE_ACCESS_MASK
;
2337 if (minorversion
&& x
) {
2338 if ((x
& NFS4_SHARE_WANT_MASK
) > NFS4_SHARE_WANT_CANCEL
)
2340 if ((x
& NFS4_SHARE_WHEN_MASK
) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED
)
2342 x
&= ~(NFS4_SHARE_WANT_MASK
| NFS4_SHARE_WHEN_MASK
);
2349 static inline int deny_valid(u32 x
)
2351 /* Note: unlike access bits, deny bits may be zero. */
2352 return x
<= NFS4_SHARE_DENY_BOTH
;
2356 * Called to check deny when READ with all zero stateid or
2357 * WRITE with all zero or all one stateid
2360 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
2362 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2363 struct nfs4_file
*fp
;
2364 struct nfs4_stateid
*stp
;
2367 dprintk("NFSD: nfs4_share_conflict\n");
2369 fp
= find_file(ino
);
2372 ret
= nfserr_locked
;
2373 /* Search for conflicting share reservations */
2374 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
) {
2375 if (test_bit(deny_type
, &stp
->st_deny_bmap
) ||
2376 test_bit(NFS4_SHARE_DENY_BOTH
, &stp
->st_deny_bmap
))
2385 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
2387 /* We're assuming the state code never drops its reference
2388 * without first removing the lease. Since we're in this lease
2389 * callback (and since the lease code is serialized by the kernel
2390 * lock) we know the server hasn't removed the lease yet, we know
2391 * it's safe to take a reference: */
2392 atomic_inc(&dp
->dl_count
);
2394 list_add_tail(&dp
->dl_recall_lru
, &del_recall_lru
);
2396 /* only place dl_time is set. protected by lock_flocks*/
2397 dp
->dl_time
= get_seconds();
2399 nfsd4_cb_recall(dp
);
2402 /* Called from break_lease() with lock_flocks() held. */
2403 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
2405 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
2406 struct nfs4_delegation
*dp
;
2409 /* We assume break_lease is only called once per lease: */
2410 BUG_ON(fp
->fi_had_conflict
);
2412 * We don't want the locks code to timeout the lease for us;
2413 * we'll remove it ourself if a delegation isn't returned
2416 fl
->fl_break_time
= 0;
2418 spin_lock(&recall_lock
);
2419 fp
->fi_had_conflict
= true;
2420 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2421 nfsd_break_one_deleg(dp
);
2422 spin_unlock(&recall_lock
);
2426 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
2429 return lease_modify(onlist
, arg
);
2434 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
2435 .lm_break
= nfsd_break_deleg_cb
,
2436 .lm_change
= nfsd_change_deleg_cb
,
2441 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
2442 struct nfsd4_open
*open
)
2444 clientid_t
*clientid
= &open
->op_clientid
;
2445 struct nfs4_client
*clp
= NULL
;
2446 unsigned int strhashval
;
2447 struct nfs4_stateowner
*sop
= NULL
;
2449 if (!check_name(open
->op_owner
))
2450 return nfserr_inval
;
2452 if (STALE_CLIENTID(&open
->op_clientid
))
2453 return nfserr_stale_clientid
;
2455 strhashval
= ownerstr_hashval(clientid
->cl_id
, open
->op_owner
);
2456 sop
= find_openstateowner_str(strhashval
, open
);
2457 open
->op_stateowner
= sop
;
2459 /* Make sure the client's lease hasn't expired. */
2460 clp
= find_confirmed_client(clientid
);
2462 return nfserr_expired
;
2465 /* When sessions are used, skip open sequenceid processing */
2466 if (nfsd4_has_session(cstate
))
2468 if (!sop
->so_confirmed
) {
2469 /* Replace unconfirmed owners without checking for replay. */
2470 clp
= sop
->so_client
;
2471 release_openowner(sop
);
2472 open
->op_stateowner
= NULL
;
2475 if (open
->op_seqid
== sop
->so_seqid
- 1) {
2476 if (sop
->so_replay
.rp_buflen
)
2477 return nfserr_replay_me
;
2478 /* The original OPEN failed so spectacularly
2479 * that we don't even have replay data saved!
2480 * Therefore, we have no choice but to continue
2481 * processing this OPEN; presumably, we'll
2482 * fail again for the same reason.
2484 dprintk("nfsd4_process_open1: replay with no replay cache\n");
2487 if (open
->op_seqid
!= sop
->so_seqid
)
2488 return nfserr_bad_seqid
;
2490 if (open
->op_stateowner
== NULL
) {
2491 sop
= alloc_init_open_stateowner(strhashval
, clp
, open
);
2493 return nfserr_jukebox
;
2494 open
->op_stateowner
= sop
;
2496 list_del_init(&sop
->so_close_lru
);
2497 renew_client(sop
->so_client
);
2501 static inline __be32
2502 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
2504 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
2505 return nfserr_openmode
;
2510 static struct nfs4_delegation
*
2511 find_delegation_file(struct nfs4_file
*fp
, stateid_t
*stid
)
2513 struct nfs4_delegation
*dp
;
2515 spin_lock(&recall_lock
);
2516 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2517 if (dp
->dl_stateid
.si_stateownerid
== stid
->si_stateownerid
) {
2518 spin_unlock(&recall_lock
);
2521 spin_unlock(&recall_lock
);
2525 static int share_access_to_flags(u32 share_access
)
2527 share_access
&= ~NFS4_SHARE_WANT_MASK
;
2529 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
2533 nfs4_check_deleg(struct nfs4_file
*fp
, struct nfsd4_open
*open
,
2534 struct nfs4_delegation
**dp
)
2537 __be32 status
= nfserr_bad_stateid
;
2539 *dp
= find_delegation_file(fp
, &open
->op_delegate_stateid
);
2542 flags
= share_access_to_flags(open
->op_share_access
);
2543 status
= nfs4_check_delegmode(*dp
, flags
);
2547 if (open
->op_claim_type
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2551 open
->op_stateowner
->so_confirmed
= 1;
2556 nfs4_check_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
, struct nfs4_stateid
**stpp
)
2558 struct nfs4_stateid
*local
;
2559 __be32 status
= nfserr_share_denied
;
2560 struct nfs4_stateowner
*sop
= open
->op_stateowner
;
2562 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
2563 /* ignore lock owners */
2564 if (local
->st_stateowner
->so_is_open_owner
== 0)
2566 /* remember if we have seen this open owner */
2567 if (local
->st_stateowner
== sop
)
2569 /* check for conflicting share reservations */
2570 if (!test_share(local
, open
))
2578 static inline struct nfs4_stateid
*
2579 nfs4_alloc_stateid(void)
2581 return kmem_cache_alloc(stateid_slab
, GFP_KERNEL
);
2584 static inline int nfs4_access_to_access(u32 nfs4_access
)
2588 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
2589 flags
|= NFSD_MAY_READ
;
2590 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
2591 flags
|= NFSD_MAY_WRITE
;
2595 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
2596 struct svc_fh
*cur_fh
, struct nfsd4_open
*open
)
2599 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
2600 int access
= nfs4_access_to_access(open
->op_share_access
);
2602 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2603 * allowing it to break the lease and return EAGAIN leaves the
2604 * client unable to make progress in returning the delegation */
2605 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2606 access
|= NFSD_MAY_NOT_BREAK_LEASE
;
2608 if (!fp
->fi_fds
[oflag
]) {
2609 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
,
2610 &fp
->fi_fds
[oflag
]);
2614 nfs4_file_get_access(fp
, oflag
);
2620 nfs4_new_open(struct svc_rqst
*rqstp
, struct nfs4_stateid
**stpp
,
2621 struct nfs4_file
*fp
, struct svc_fh
*cur_fh
,
2622 struct nfsd4_open
*open
)
2624 struct nfs4_stateid
*stp
;
2627 stp
= nfs4_alloc_stateid();
2629 return nfserr_jukebox
;
2631 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
2633 kmem_cache_free(stateid_slab
, stp
);
2640 static inline __be32
2641 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
2642 struct nfsd4_open
*open
)
2644 struct iattr iattr
= {
2645 .ia_valid
= ATTR_SIZE
,
2648 if (!open
->op_truncate
)
2650 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
2651 return nfserr_inval
;
2652 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
2656 nfs4_upgrade_open(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
, struct svc_fh
*cur_fh
, struct nfs4_stateid
*stp
, struct nfsd4_open
*open
)
2658 u32 op_share_access
= open
->op_share_access
& ~NFS4_SHARE_WANT_MASK
;
2662 new_access
= !test_bit(op_share_access
, &stp
->st_access_bmap
);
2664 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
2668 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
2671 int oflag
= nfs4_access_to_omode(op_share_access
);
2672 nfs4_file_put_access(fp
, oflag
);
2676 /* remember the open */
2677 __set_bit(op_share_access
, &stp
->st_access_bmap
);
2678 __set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
2685 nfs4_set_claim_prev(struct nfsd4_open
*open
)
2687 open
->op_stateowner
->so_confirmed
= 1;
2688 open
->op_stateowner
->so_client
->cl_firststate
= 1;
2691 /* Should we give out recallable state?: */
2692 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
2694 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
2697 * In the sessions case, since we don't have to establish a
2698 * separate connection for callbacks, we assume it's OK
2699 * until we hear otherwise:
2701 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
2704 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_delegation
*dp
, int flag
)
2706 struct file_lock
*fl
;
2708 fl
= locks_alloc_lock();
2711 locks_init_lock(fl
);
2712 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
2713 fl
->fl_flags
= FL_LEASE
;
2714 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
2715 fl
->fl_end
= OFFSET_MAX
;
2716 fl
->fl_owner
= (fl_owner_t
)(dp
->dl_file
);
2717 fl
->fl_pid
= current
->tgid
;
2721 static int nfs4_setlease(struct nfs4_delegation
*dp
, int flag
)
2723 struct nfs4_file
*fp
= dp
->dl_file
;
2724 struct file_lock
*fl
;
2727 fl
= nfs4_alloc_init_lease(dp
, flag
);
2730 fl
->fl_file
= find_readable_file(fp
);
2731 list_add(&dp
->dl_perclnt
, &dp
->dl_client
->cl_delegations
);
2732 status
= vfs_setlease(fl
->fl_file
, fl
->fl_type
, &fl
);
2734 list_del_init(&dp
->dl_perclnt
);
2735 locks_free_lock(fl
);
2739 fp
->fi_deleg_file
= fl
->fl_file
;
2740 get_file(fp
->fi_deleg_file
);
2741 atomic_set(&fp
->fi_delegees
, 1);
2742 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
2746 static int nfs4_set_delegation(struct nfs4_delegation
*dp
, int flag
)
2748 struct nfs4_file
*fp
= dp
->dl_file
;
2751 return nfs4_setlease(dp
, flag
);
2752 spin_lock(&recall_lock
);
2753 if (fp
->fi_had_conflict
) {
2754 spin_unlock(&recall_lock
);
2757 atomic_inc(&fp
->fi_delegees
);
2758 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
2759 spin_unlock(&recall_lock
);
2760 list_add(&dp
->dl_perclnt
, &dp
->dl_client
->cl_delegations
);
2765 * Attempt to hand out a delegation.
2768 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
, struct nfs4_stateid
*stp
)
2770 struct nfs4_delegation
*dp
;
2771 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
2773 int status
, flag
= 0;
2775 cb_up
= nfsd4_cb_channel_good(sop
->so_client
);
2776 flag
= NFS4_OPEN_DELEGATE_NONE
;
2777 open
->op_recall
= 0;
2778 switch (open
->op_claim_type
) {
2779 case NFS4_OPEN_CLAIM_PREVIOUS
:
2781 open
->op_recall
= 1;
2782 flag
= open
->op_delegate_type
;
2783 if (flag
== NFS4_OPEN_DELEGATE_NONE
)
2786 case NFS4_OPEN_CLAIM_NULL
:
2787 /* Let's not give out any delegations till everyone's
2788 * had the chance to reclaim theirs.... */
2789 if (locks_in_grace())
2791 if (!cb_up
|| !sop
->so_confirmed
)
2793 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
2794 flag
= NFS4_OPEN_DELEGATE_WRITE
;
2796 flag
= NFS4_OPEN_DELEGATE_READ
;
2802 dp
= alloc_init_deleg(sop
->so_client
, stp
, fh
, flag
);
2805 status
= nfs4_set_delegation(dp
, flag
);
2809 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stateid
, sizeof(dp
->dl_stateid
));
2811 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
2812 STATEID_VAL(&dp
->dl_stateid
));
2814 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
2815 && flag
== NFS4_OPEN_DELEGATE_NONE
2816 && open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
)
2817 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2818 open
->op_delegate_type
= flag
;
2821 nfs4_put_delegation(dp
);
2823 flag
= NFS4_OPEN_DELEGATE_NONE
;
2828 * called with nfs4_lock_state() held.
2831 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
2833 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2834 struct nfs4_file
*fp
= NULL
;
2835 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2836 struct nfs4_stateid
*stp
= NULL
;
2837 struct nfs4_delegation
*dp
= NULL
;
2840 status
= nfserr_inval
;
2841 if (!access_valid(open
->op_share_access
, resp
->cstate
.minorversion
)
2842 || !deny_valid(open
->op_share_deny
))
2845 * Lookup file; if found, lookup stateid and check open request,
2846 * and check for delegations in the process of being recalled.
2847 * If not found, create the nfs4_file struct
2849 fp
= find_file(ino
);
2851 if ((status
= nfs4_check_open(fp
, open
, &stp
)))
2853 status
= nfs4_check_deleg(fp
, open
, &dp
);
2857 status
= nfserr_bad_stateid
;
2858 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2860 status
= nfserr_jukebox
;
2861 fp
= alloc_init_file(ino
);
2867 * OPEN the file, or upgrade an existing OPEN.
2868 * If truncate fails, the OPEN fails.
2871 /* Stateid was found, this is an OPEN upgrade */
2872 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
2875 update_stateid(&stp
->st_stateid
);
2877 status
= nfs4_new_open(rqstp
, &stp
, fp
, current_fh
, open
);
2880 init_stateid(stp
, fp
, open
);
2881 status
= nfsd4_truncate(rqstp
, current_fh
, open
);
2883 release_open_stateid(stp
);
2886 if (nfsd4_has_session(&resp
->cstate
))
2887 update_stateid(&stp
->st_stateid
);
2889 memcpy(&open
->op_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2891 if (nfsd4_has_session(&resp
->cstate
))
2892 open
->op_stateowner
->so_confirmed
= 1;
2895 * Attempt to hand out a delegation. No error return, because the
2896 * OPEN succeeds even if we fail.
2898 nfs4_open_delegation(current_fh
, open
, stp
);
2902 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
2903 STATEID_VAL(&stp
->st_stateid
));
2907 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
2908 nfs4_set_claim_prev(open
);
2910 * To finish the open response, we just need to set the rflags.
2912 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
2913 if (!open
->op_stateowner
->so_confirmed
&&
2914 !nfsd4_has_session(&resp
->cstate
))
2915 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
2921 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2924 struct nfs4_client
*clp
;
2928 dprintk("process_renew(%08x/%08x): starting\n",
2929 clid
->cl_boot
, clid
->cl_id
);
2930 status
= nfserr_stale_clientid
;
2931 if (STALE_CLIENTID(clid
))
2933 clp
= find_confirmed_client(clid
);
2934 status
= nfserr_expired
;
2936 /* We assume the client took too long to RENEW. */
2937 dprintk("nfsd4_renew: clientid not found!\n");
2941 status
= nfserr_cb_path_down
;
2942 if (!list_empty(&clp
->cl_delegations
)
2943 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
2947 nfs4_unlock_state();
2951 static struct lock_manager nfsd4_manager
= {
2955 nfsd4_end_grace(void)
2957 dprintk("NFSD: end of grace period\n");
2958 nfsd4_recdir_purge_old();
2959 locks_end_grace(&nfsd4_manager
);
2961 * Now that every NFSv4 client has had the chance to recover and
2962 * to see the (possibly new, possibly shorter) lease time, we
2963 * can safely set the next grace time to the current lease time:
2965 nfsd4_grace
= nfsd4_lease
;
2969 nfs4_laundromat(void)
2971 struct nfs4_client
*clp
;
2972 struct nfs4_stateowner
*sop
;
2973 struct nfs4_delegation
*dp
;
2974 struct list_head
*pos
, *next
, reaplist
;
2975 time_t cutoff
= get_seconds() - nfsd4_lease
;
2976 time_t t
, clientid_val
= nfsd4_lease
;
2977 time_t u
, test_val
= nfsd4_lease
;
2981 dprintk("NFSD: laundromat service - starting\n");
2982 if (locks_in_grace())
2984 INIT_LIST_HEAD(&reaplist
);
2985 spin_lock(&client_lock
);
2986 list_for_each_safe(pos
, next
, &client_lru
) {
2987 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
2988 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
2989 t
= clp
->cl_time
- cutoff
;
2990 if (clientid_val
> t
)
2994 if (atomic_read(&clp
->cl_refcount
)) {
2995 dprintk("NFSD: client in use (clientid %08x)\n",
2996 clp
->cl_clientid
.cl_id
);
2999 unhash_client_locked(clp
);
3000 list_add(&clp
->cl_lru
, &reaplist
);
3002 spin_unlock(&client_lock
);
3003 list_for_each_safe(pos
, next
, &reaplist
) {
3004 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3005 dprintk("NFSD: purging unused client (clientid %08x)\n",
3006 clp
->cl_clientid
.cl_id
);
3007 nfsd4_remove_clid_dir(clp
);
3010 spin_lock(&recall_lock
);
3011 list_for_each_safe(pos
, next
, &del_recall_lru
) {
3012 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3013 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
3014 u
= dp
->dl_time
- cutoff
;
3019 list_move(&dp
->dl_recall_lru
, &reaplist
);
3021 spin_unlock(&recall_lock
);
3022 list_for_each_safe(pos
, next
, &reaplist
) {
3023 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3024 list_del_init(&dp
->dl_recall_lru
);
3025 unhash_delegation(dp
);
3027 test_val
= nfsd4_lease
;
3028 list_for_each_safe(pos
, next
, &close_lru
) {
3029 sop
= list_entry(pos
, struct nfs4_stateowner
, so_close_lru
);
3030 if (time_after((unsigned long)sop
->so_time
, (unsigned long)cutoff
)) {
3031 u
= sop
->so_time
- cutoff
;
3036 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
3038 release_openowner(sop
);
3040 if (clientid_val
< NFSD_LAUNDROMAT_MINTIMEOUT
)
3041 clientid_val
= NFSD_LAUNDROMAT_MINTIMEOUT
;
3042 nfs4_unlock_state();
3043 return clientid_val
;
3046 static struct workqueue_struct
*laundry_wq
;
3047 static void laundromat_main(struct work_struct
*);
3048 static DECLARE_DELAYED_WORK(laundromat_work
, laundromat_main
);
3051 laundromat_main(struct work_struct
*not_used
)
3055 t
= nfs4_laundromat();
3056 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
3057 queue_delayed_work(laundry_wq
, &laundromat_work
, t
*HZ
);
3060 static struct nfs4_stateowner
*
3061 search_close_lru(u32 st_id
, int flags
)
3063 struct nfs4_stateowner
*local
= NULL
;
3065 if (flags
& CLOSE_STATE
) {
3066 list_for_each_entry(local
, &close_lru
, so_close_lru
) {
3067 if (local
->so_id
== st_id
)
3075 nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stateid
*stp
)
3077 return fhp
->fh_dentry
->d_inode
!= stp
->st_file
->fi_inode
;
3081 STALE_STATEID(stateid_t
*stateid
)
3083 if (stateid
->si_boot
== boot_time
)
3085 dprintk("NFSD: stale stateid " STATEID_FMT
"!\n",
3086 STATEID_VAL(stateid
));
3091 access_permit_read(unsigned long access_bmap
)
3093 return test_bit(NFS4_SHARE_ACCESS_READ
, &access_bmap
) ||
3094 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
) ||
3095 test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
);
3099 access_permit_write(unsigned long access_bmap
)
3101 return test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
) ||
3102 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
);
3106 __be32
nfs4_check_openmode(struct nfs4_stateid
*stp
, int flags
)
3108 __be32 status
= nfserr_openmode
;
3110 /* For lock stateid's, we test the parent open, not the lock: */
3111 if (stp
->st_openstp
)
3112 stp
= stp
->st_openstp
;
3113 if ((flags
& WR_STATE
) && (!access_permit_write(stp
->st_access_bmap
)))
3115 if ((flags
& RD_STATE
) && (!access_permit_read(stp
->st_access_bmap
)))
3122 static inline __be32
3123 check_special_stateids(svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3125 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
3127 else if (locks_in_grace()) {
3128 /* Answer in remaining cases depends on existence of
3129 * conflicting state; so we must wait out the grace period. */
3130 return nfserr_grace
;
3131 } else if (flags
& WR_STATE
)
3132 return nfs4_share_conflict(current_fh
,
3133 NFS4_SHARE_DENY_WRITE
);
3134 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3135 return nfs4_share_conflict(current_fh
,
3136 NFS4_SHARE_DENY_READ
);
3140 * Allow READ/WRITE during grace period on recovered state only for files
3141 * that are not able to provide mandatory locking.
3144 grace_disallows_io(struct inode
*inode
)
3146 return locks_in_grace() && mandatory_lock(inode
);
3149 static int check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, int flags
)
3152 * When sessions are used the stateid generation number is ignored
3155 if ((flags
& HAS_SESSION
) && in
->si_generation
== 0)
3158 /* If the client sends us a stateid from the future, it's buggy: */
3159 if (in
->si_generation
> ref
->si_generation
)
3160 return nfserr_bad_stateid
;
3162 * The following, however, can happen. For example, if the
3163 * client sends an open and some IO at the same time, the open
3164 * may bump si_generation while the IO is still in flight.
3165 * Thanks to hard links and renames, the client never knows what
3166 * file an open will affect. So it could avoid that situation
3167 * only by serializing all opens and IO from the same open
3168 * owner. To recover from the old_stateid error, the client
3169 * will just have to retry the IO:
3171 if (in
->si_generation
< ref
->si_generation
)
3172 return nfserr_old_stateid
;
3177 static int is_delegation_stateid(stateid_t
*stateid
)
3179 return stateid
->si_fileid
== 0;
3182 static int is_open_stateid(struct nfs4_stateid
*stateid
)
3184 return stateid
->st_openstp
== NULL
;
3187 __be32
nfs4_validate_stateid(stateid_t
*stateid
, int flags
)
3189 struct nfs4_stateid
*stp
= NULL
;
3190 __be32 status
= nfserr_stale_stateid
;
3192 if (STALE_STATEID(stateid
))
3195 status
= nfserr_expired
;
3196 stp
= search_for_stateid(stateid
);
3199 status
= nfserr_bad_stateid
;
3201 if (!stp
->st_stateowner
->so_confirmed
)
3204 status
= check_stateid_generation(stateid
, &stp
->st_stateid
, flags
);
3214 * Checks for stateid operations
3217 nfs4_preprocess_stateid_op(struct nfsd4_compound_state
*cstate
,
3218 stateid_t
*stateid
, int flags
, struct file
**filpp
)
3220 struct nfs4_stateid
*stp
= NULL
;
3221 struct nfs4_delegation
*dp
= NULL
;
3222 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3223 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3229 if (grace_disallows_io(ino
))
3230 return nfserr_grace
;
3232 if (nfsd4_has_session(cstate
))
3233 flags
|= HAS_SESSION
;
3235 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3236 return check_special_stateids(current_fh
, stateid
, flags
);
3238 status
= nfserr_stale_stateid
;
3239 if (STALE_STATEID(stateid
))
3243 * We assume that any stateid that has the current boot time,
3244 * but that we can't find, is expired:
3246 status
= nfserr_expired
;
3247 if (is_delegation_stateid(stateid
)) {
3248 dp
= find_delegation_stateid(ino
, stateid
);
3251 status
= check_stateid_generation(stateid
, &dp
->dl_stateid
,
3255 status
= nfs4_check_delegmode(dp
, flags
);
3258 renew_client(dp
->dl_client
);
3260 *filpp
= dp
->dl_file
->fi_deleg_file
;
3263 } else { /* open or lock stateid */
3264 stp
= find_stateid(stateid
, flags
);
3267 status
= nfserr_bad_stateid
;
3268 if (nfs4_check_fh(current_fh
, stp
))
3270 if (!stp
->st_stateowner
->so_confirmed
)
3272 status
= check_stateid_generation(stateid
, &stp
->st_stateid
,
3276 status
= nfs4_check_openmode(stp
, flags
);
3279 renew_client(stp
->st_stateowner
->so_client
);
3281 if (flags
& RD_STATE
)
3282 *filpp
= find_readable_file(stp
->st_file
);
3284 *filpp
= find_writeable_file(stp
->st_file
);
3293 nfsd4_free_delegation_stateid(stateid_t
*stateid
)
3295 struct nfs4_delegation
*dp
= search_for_delegation(stateid
);
3297 return nfserr_locks_held
;
3298 return nfserr_bad_stateid
;
3302 nfsd4_free_lock_stateid(struct nfs4_stateid
*stp
)
3304 if (check_for_locks(stp
->st_file
, stp
->st_stateowner
))
3305 return nfserr_locks_held
;
3306 release_lock_stateid(stp
);
3311 * Test if the stateid is valid
3314 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3315 struct nfsd4_test_stateid
*test_stateid
)
3317 test_stateid
->ts_has_session
= nfsd4_has_session(cstate
);
3325 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3326 struct nfsd4_free_stateid
*free_stateid
)
3328 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
3329 struct nfs4_stateid
*stp
;
3333 if (is_delegation_stateid(stateid
)) {
3334 ret
= nfsd4_free_delegation_stateid(stateid
);
3338 stp
= search_for_stateid(stateid
);
3340 ret
= nfserr_bad_stateid
;
3343 if (stateid
->si_generation
!= 0) {
3344 if (stateid
->si_generation
< stp
->st_stateid
.si_generation
) {
3345 ret
= nfserr_old_stateid
;
3348 if (stateid
->si_generation
> stp
->st_stateid
.si_generation
) {
3349 ret
= nfserr_bad_stateid
;
3354 if (is_open_stateid(stp
)) {
3355 ret
= nfserr_locks_held
;
3358 ret
= nfsd4_free_lock_stateid(stp
);
3363 nfs4_unlock_state();
3370 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
3371 RD_STATE
: WR_STATE
;
3375 * Checks for sequence id mutating operations.
3378 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
3379 stateid_t
*stateid
, int flags
,
3380 struct nfs4_stateowner
**sopp
,
3381 struct nfs4_stateid
**stpp
, struct nfsd4_lock
*lock
)
3383 struct nfs4_stateid
*stp
;
3384 struct nfs4_stateowner
*sop
;
3385 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3388 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
3389 seqid
, STATEID_VAL(stateid
));
3394 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
)) {
3395 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
3396 return nfserr_bad_stateid
;
3399 if (STALE_STATEID(stateid
))
3400 return nfserr_stale_stateid
;
3402 if (nfsd4_has_session(cstate
))
3403 flags
|= HAS_SESSION
;
3406 * We return BAD_STATEID if filehandle doesn't match stateid,
3407 * the confirmed flag is incorrecly set, or the generation
3408 * number is incorrect.
3410 stp
= find_stateid(stateid
, flags
);
3413 * Also, we should make sure this isn't just the result of
3416 sop
= search_close_lru(stateid
->si_stateownerid
, flags
);
3417 /* It's not stale; let's assume it's expired: */
3419 return nfserr_expired
;
3425 *sopp
= sop
= stp
->st_stateowner
;
3428 clientid_t
*lockclid
= &lock
->v
.new.clientid
;
3429 struct nfs4_client
*clp
= sop
->so_client
;
3433 lkflg
= setlkflg(lock
->lk_type
);
3435 if (lock
->lk_is_new
) {
3436 if (!sop
->so_is_open_owner
)
3437 return nfserr_bad_stateid
;
3438 if (!(flags
& HAS_SESSION
) &&
3439 !same_clid(&clp
->cl_clientid
, lockclid
))
3440 return nfserr_bad_stateid
;
3441 /* stp is the open stateid */
3442 status
= nfs4_check_openmode(stp
, lkflg
);
3446 /* stp is the lock stateid */
3447 status
= nfs4_check_openmode(stp
->st_openstp
, lkflg
);
3453 if (nfs4_check_fh(current_fh
, stp
)) {
3454 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
3455 return nfserr_bad_stateid
;
3459 * We now validate the seqid and stateid generation numbers.
3460 * For the moment, we ignore the possibility of
3461 * generation number wraparound.
3463 if (!(flags
& HAS_SESSION
) && seqid
!= sop
->so_seqid
)
3466 if (sop
->so_confirmed
&& flags
& CONFIRM
) {
3467 dprintk("NFSD: preprocess_seqid_op: expected"
3468 " unconfirmed stateowner!\n");
3469 return nfserr_bad_stateid
;
3471 if (!sop
->so_confirmed
&& !(flags
& CONFIRM
)) {
3472 dprintk("NFSD: preprocess_seqid_op: stateowner not"
3473 " confirmed yet!\n");
3474 return nfserr_bad_stateid
;
3476 status
= check_stateid_generation(stateid
, &stp
->st_stateid
, flags
);
3479 renew_client(sop
->so_client
);
3483 if (seqid
== sop
->so_seqid
- 1) {
3484 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
3485 /* indicate replay to calling function */
3486 return nfserr_replay_me
;
3488 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
3489 sop
->so_seqid
, seqid
);
3491 return nfserr_bad_seqid
;
3495 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3496 struct nfsd4_open_confirm
*oc
)
3499 struct nfs4_stateowner
*sop
;
3500 struct nfs4_stateid
*stp
;
3502 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3503 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3504 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3506 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
3512 if ((status
= nfs4_preprocess_seqid_op(cstate
,
3513 oc
->oc_seqid
, &oc
->oc_req_stateid
,
3514 CONFIRM
| OPEN_STATE
,
3515 &oc
->oc_stateowner
, &stp
, NULL
)))
3518 sop
= oc
->oc_stateowner
;
3519 sop
->so_confirmed
= 1;
3520 update_stateid(&stp
->st_stateid
);
3521 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3522 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
3523 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stateid
));
3525 nfsd4_create_clid_dir(sop
->so_client
);
3527 if (oc
->oc_stateowner
) {
3528 nfs4_get_stateowner(oc
->oc_stateowner
);
3529 cstate
->replay_owner
= oc
->oc_stateowner
;
3531 nfs4_unlock_state();
3535 static inline void nfs4_file_downgrade(struct nfs4_stateid
*stp
, unsigned int to_access
)
3539 for (i
= 1; i
< 4; i
++) {
3540 if (test_bit(i
, &stp
->st_access_bmap
)
3541 && ((i
& to_access
) != i
)) {
3542 nfs4_file_put_access(stp
->st_file
, nfs4_access_to_omode(i
));
3543 __clear_bit(i
, &stp
->st_access_bmap
);
3549 reset_union_bmap_deny(unsigned long deny
, unsigned long *bmap
)
3552 for (i
= 0; i
< 4; i
++) {
3553 if ((i
& deny
) != i
)
3554 __clear_bit(i
, bmap
);
3559 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
3560 struct nfsd4_compound_state
*cstate
,
3561 struct nfsd4_open_downgrade
*od
)
3564 struct nfs4_stateid
*stp
;
3566 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3567 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3568 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3570 if (!access_valid(od
->od_share_access
, cstate
->minorversion
)
3571 || !deny_valid(od
->od_share_deny
))
3572 return nfserr_inval
;
3573 /* We don't yet support WANT bits: */
3574 od
->od_share_access
&= NFS4_SHARE_ACCESS_MASK
;
3577 if ((status
= nfs4_preprocess_seqid_op(cstate
,
3581 &od
->od_stateowner
, &stp
, NULL
)))
3584 status
= nfserr_inval
;
3585 if (!test_bit(od
->od_share_access
, &stp
->st_access_bmap
)) {
3586 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3587 stp
->st_access_bmap
, od
->od_share_access
);
3590 if (!test_bit(od
->od_share_deny
, &stp
->st_deny_bmap
)) {
3591 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3592 stp
->st_deny_bmap
, od
->od_share_deny
);
3595 nfs4_file_downgrade(stp
, od
->od_share_access
);
3597 reset_union_bmap_deny(od
->od_share_deny
, &stp
->st_deny_bmap
);
3599 update_stateid(&stp
->st_stateid
);
3600 memcpy(&od
->od_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3603 if (od
->od_stateowner
) {
3604 nfs4_get_stateowner(od
->od_stateowner
);
3605 cstate
->replay_owner
= od
->od_stateowner
;
3607 nfs4_unlock_state();
3612 * nfs4_unlock_state() called after encode
3615 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3616 struct nfsd4_close
*close
)
3619 struct nfs4_stateid
*stp
;
3621 dprintk("NFSD: nfsd4_close on file %.*s\n",
3622 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3623 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3626 /* check close_lru for replay */
3627 if ((status
= nfs4_preprocess_seqid_op(cstate
,
3630 OPEN_STATE
| CLOSE_STATE
,
3631 &close
->cl_stateowner
, &stp
, NULL
)))
3634 update_stateid(&stp
->st_stateid
);
3635 memcpy(&close
->cl_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3637 /* release_stateid() calls nfsd_close() if needed */
3638 release_open_stateid(stp
);
3640 /* place unused nfs4_stateowners on so_close_lru list to be
3641 * released by the laundromat service after the lease period
3642 * to enable us to handle CLOSE replay
3644 if (list_empty(&close
->cl_stateowner
->so_stateids
))
3645 move_to_close_lru(close
->cl_stateowner
);
3647 if (close
->cl_stateowner
) {
3648 nfs4_get_stateowner(close
->cl_stateowner
);
3649 cstate
->replay_owner
= close
->cl_stateowner
;
3651 nfs4_unlock_state();
3656 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3657 struct nfsd4_delegreturn
*dr
)
3659 struct nfs4_delegation
*dp
;
3660 stateid_t
*stateid
= &dr
->dr_stateid
;
3661 struct inode
*inode
;
3665 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
3667 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
3669 if (nfsd4_has_session(cstate
))
3670 flags
|= HAS_SESSION
;
3672 status
= nfserr_bad_stateid
;
3673 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3675 status
= nfserr_stale_stateid
;
3676 if (STALE_STATEID(stateid
))
3678 status
= nfserr_bad_stateid
;
3679 if (!is_delegation_stateid(stateid
))
3681 status
= nfserr_expired
;
3682 dp
= find_delegation_stateid(inode
, stateid
);
3685 status
= check_stateid_generation(stateid
, &dp
->dl_stateid
, flags
);
3688 renew_client(dp
->dl_client
);
3690 unhash_delegation(dp
);
3692 nfs4_unlock_state();
3699 * Lock owner state (byte-range locks)
3701 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3702 #define LOCK_HASH_BITS 8
3703 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3704 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3707 end_offset(u64 start
, u64 len
)
3712 return end
>= start
? end
: NFS4_MAX_UINT64
;
3715 /* last octet in a range */
3717 last_byte_offset(u64 start
, u64 len
)
3723 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
3726 #define lockownerid_hashval(id) \
3727 ((id) & LOCK_HASH_MASK)
3729 static inline unsigned int
3730 lock_ownerstr_hashval(struct inode
*inode
, u32 cl_id
,
3731 struct xdr_netobj
*ownername
)
3733 return (file_hashval(inode
) + cl_id
3734 + opaque_hashval(ownername
->data
, ownername
->len
))
3738 static struct list_head lock_ownerid_hashtbl
[LOCK_HASH_SIZE
];
3739 static struct list_head lock_ownerstr_hashtbl
[LOCK_HASH_SIZE
];
3740 static struct list_head lockstateid_hashtbl
[STATEID_HASH_SIZE
];
3743 same_stateid(stateid_t
*id_one
, stateid_t
*id_two
)
3745 if (id_one
->si_stateownerid
!= id_two
->si_stateownerid
)
3747 return id_one
->si_fileid
== id_two
->si_fileid
;
3750 static struct nfs4_stateid
*
3751 find_stateid(stateid_t
*stid
, int flags
)
3753 struct nfs4_stateid
*local
;
3754 u32 st_id
= stid
->si_stateownerid
;
3755 u32 f_id
= stid
->si_fileid
;
3756 unsigned int hashval
;
3758 dprintk("NFSD: find_stateid flags 0x%x\n",flags
);
3759 if (flags
& (LOCK_STATE
| RD_STATE
| WR_STATE
)) {
3760 hashval
= stateid_hashval(st_id
, f_id
);
3761 list_for_each_entry(local
, &lockstateid_hashtbl
[hashval
], st_hash
) {
3762 if ((local
->st_stateid
.si_stateownerid
== st_id
) &&
3763 (local
->st_stateid
.si_fileid
== f_id
))
3768 if (flags
& (OPEN_STATE
| RD_STATE
| WR_STATE
)) {
3769 hashval
= stateid_hashval(st_id
, f_id
);
3770 list_for_each_entry(local
, &stateid_hashtbl
[hashval
], st_hash
) {
3771 if ((local
->st_stateid
.si_stateownerid
== st_id
) &&
3772 (local
->st_stateid
.si_fileid
== f_id
))
3779 static struct nfs4_stateid
*
3780 search_for_stateid(stateid_t
*stid
)
3782 struct nfs4_stateid
*local
;
3783 unsigned int hashval
= stateid_hashval(stid
->si_stateownerid
, stid
->si_fileid
);
3785 list_for_each_entry(local
, &lockstateid_hashtbl
[hashval
], st_hash
) {
3786 if (same_stateid(&local
->st_stateid
, stid
))
3790 list_for_each_entry(local
, &stateid_hashtbl
[hashval
], st_hash
) {
3791 if (same_stateid(&local
->st_stateid
, stid
))
3797 static struct nfs4_delegation
*
3798 search_for_delegation(stateid_t
*stid
)
3800 struct nfs4_file
*fp
;
3801 struct nfs4_delegation
*dp
;
3802 struct list_head
*pos
;
3805 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
3806 list_for_each_entry(fp
, &file_hashtbl
[i
], fi_hash
) {
3807 list_for_each(pos
, &fp
->fi_delegations
) {
3808 dp
= list_entry(pos
, struct nfs4_delegation
, dl_perfile
);
3809 if (same_stateid(&dp
->dl_stateid
, stid
))
3817 static struct nfs4_delegation
*
3818 find_delegation_stateid(struct inode
*ino
, stateid_t
*stid
)
3820 struct nfs4_file
*fp
;
3821 struct nfs4_delegation
*dl
;
3823 dprintk("NFSD: %s: stateid=" STATEID_FMT
"\n", __func__
,
3826 fp
= find_file(ino
);
3829 dl
= find_delegation_file(fp
, stid
);
3835 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3836 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3837 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3838 * locking, this prevents us from being completely protocol-compliant. The
3839 * real solution to this problem is to start using unsigned file offsets in
3840 * the VFS, but this is a very deep change!
3843 nfs4_transform_lock_offset(struct file_lock
*lock
)
3845 if (lock
->fl_start
< 0)
3846 lock
->fl_start
= OFFSET_MAX
;
3847 if (lock
->fl_end
< 0)
3848 lock
->fl_end
= OFFSET_MAX
;
3851 /* Hack!: For now, we're defining this just so we can use a pointer to it
3852 * as a unique cookie to identify our (NFSv4's) posix locks. */
3853 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
3857 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
3859 struct nfs4_stateowner
*sop
;
3861 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
3862 sop
= (struct nfs4_stateowner
*) fl
->fl_owner
;
3863 kref_get(&sop
->so_ref
);
3865 deny
->ld_clientid
= sop
->so_client
->cl_clientid
;
3867 deny
->ld_sop
= NULL
;
3868 deny
->ld_clientid
.cl_boot
= 0;
3869 deny
->ld_clientid
.cl_id
= 0;
3871 deny
->ld_start
= fl
->fl_start
;
3872 deny
->ld_length
= NFS4_MAX_UINT64
;
3873 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
3874 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
3875 deny
->ld_type
= NFS4_READ_LT
;
3876 if (fl
->fl_type
!= F_RDLCK
)
3877 deny
->ld_type
= NFS4_WRITE_LT
;
3880 static struct nfs4_stateowner
*
3881 find_lockstateowner_str(struct inode
*inode
, clientid_t
*clid
,
3882 struct xdr_netobj
*owner
)
3884 unsigned int hashval
= lock_ownerstr_hashval(inode
, clid
->cl_id
, owner
);
3885 struct nfs4_stateowner
*op
;
3887 list_for_each_entry(op
, &lock_ownerstr_hashtbl
[hashval
], so_strhash
) {
3888 if (same_owner_str(op
, owner
, clid
))
3895 * Alloc a lock owner structure.
3896 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3899 * strhashval = lock_ownerstr_hashval
3902 static struct nfs4_stateowner
*
3903 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
3904 struct nfs4_stateowner
*sop
;
3905 struct nfs4_replay
*rp
;
3906 unsigned int idhashval
;
3908 if (!(sop
= alloc_stateowner(&lock
->lk_new_owner
)))
3910 idhashval
= lockownerid_hashval(current_ownerid
);
3911 INIT_LIST_HEAD(&sop
->so_idhash
);
3912 INIT_LIST_HEAD(&sop
->so_strhash
);
3913 INIT_LIST_HEAD(&sop
->so_perclient
);
3914 INIT_LIST_HEAD(&sop
->so_stateids
);
3915 INIT_LIST_HEAD(&sop
->so_perstateid
);
3916 INIT_LIST_HEAD(&sop
->so_close_lru
); /* not used */
3918 list_add(&sop
->so_idhash
, &lock_ownerid_hashtbl
[idhashval
]);
3919 list_add(&sop
->so_strhash
, &lock_ownerstr_hashtbl
[strhashval
]);
3920 list_add(&sop
->so_perstateid
, &open_stp
->st_lockowners
);
3921 sop
->so_is_open_owner
= 0;
3922 sop
->so_id
= current_ownerid
++;
3923 sop
->so_client
= clp
;
3924 /* It is the openowner seqid that will be incremented in encode in the
3925 * case of new lockowners; so increment the lock seqid manually: */
3926 sop
->so_seqid
= lock
->lk_new_lock_seqid
+ 1;
3927 sop
->so_confirmed
= 1;
3928 rp
= &sop
->so_replay
;
3929 rp
->rp_status
= nfserr_serverfault
;
3931 rp
->rp_buf
= rp
->rp_ibuf
;
3935 static struct nfs4_stateid
*
3936 alloc_init_lock_stateid(struct nfs4_stateowner
*sop
, struct nfs4_file
*fp
, struct nfs4_stateid
*open_stp
)
3938 struct nfs4_stateid
*stp
;
3939 unsigned int hashval
= stateid_hashval(sop
->so_id
, fp
->fi_id
);
3941 stp
= nfs4_alloc_stateid();
3944 INIT_LIST_HEAD(&stp
->st_hash
);
3945 INIT_LIST_HEAD(&stp
->st_perfile
);
3946 INIT_LIST_HEAD(&stp
->st_perstateowner
);
3947 INIT_LIST_HEAD(&stp
->st_lockowners
); /* not used */
3948 list_add(&stp
->st_hash
, &lockstateid_hashtbl
[hashval
]);
3949 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
3950 list_add(&stp
->st_perstateowner
, &sop
->so_stateids
);
3951 stp
->st_stateowner
= sop
;
3954 stp
->st_stateid
.si_boot
= boot_time
;
3955 stp
->st_stateid
.si_stateownerid
= sop
->so_id
;
3956 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
3957 stp
->st_stateid
.si_generation
= 0;
3958 stp
->st_access_bmap
= 0;
3959 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
3960 stp
->st_openstp
= open_stp
;
3967 check_lock_length(u64 offset
, u64 length
)
3969 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
3970 LOFF_OVERFLOW(offset
, length
)));
3973 static void get_lock_access(struct nfs4_stateid
*lock_stp
, u32 access
)
3975 struct nfs4_file
*fp
= lock_stp
->st_file
;
3976 int oflag
= nfs4_access_to_omode(access
);
3978 if (test_bit(access
, &lock_stp
->st_access_bmap
))
3980 nfs4_file_get_access(fp
, oflag
);
3981 __set_bit(access
, &lock_stp
->st_access_bmap
);
3988 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3989 struct nfsd4_lock
*lock
)
3991 struct nfs4_stateowner
*open_sop
= NULL
;
3992 struct nfs4_stateowner
*lock_sop
= NULL
;
3993 struct nfs4_stateid
*lock_stp
;
3994 struct nfs4_file
*fp
;
3995 struct file
*filp
= NULL
;
3996 struct file_lock file_lock
;
3997 struct file_lock conflock
;
3999 unsigned int strhashval
;
4002 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4003 (long long) lock
->lk_offset
,
4004 (long long) lock
->lk_length
);
4006 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
4007 return nfserr_inval
;
4009 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
4010 S_IFREG
, NFSD_MAY_LOCK
))) {
4011 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4017 if (lock
->lk_is_new
) {
4019 * Client indicates that this is a new lockowner.
4020 * Use open owner and open stateid to create lock owner and
4023 struct nfs4_stateid
*open_stp
= NULL
;
4025 status
= nfserr_stale_clientid
;
4026 if (!nfsd4_has_session(cstate
) &&
4027 STALE_CLIENTID(&lock
->lk_new_clientid
))
4030 /* validate and update open stateid and open seqid */
4031 status
= nfs4_preprocess_seqid_op(cstate
,
4032 lock
->lk_new_open_seqid
,
4033 &lock
->lk_new_open_stateid
,
4035 &lock
->lk_replay_owner
, &open_stp
,
4039 open_sop
= lock
->lk_replay_owner
;
4040 /* create lockowner and lock stateid */
4041 fp
= open_stp
->st_file
;
4042 strhashval
= lock_ownerstr_hashval(fp
->fi_inode
,
4043 open_sop
->so_client
->cl_clientid
.cl_id
,
4044 &lock
->v
.new.owner
);
4045 /* XXX: Do we need to check for duplicate stateowners on
4046 * the same file, or should they just be allowed (and
4047 * create new stateids)? */
4048 status
= nfserr_jukebox
;
4049 lock_sop
= alloc_init_lock_stateowner(strhashval
,
4050 open_sop
->so_client
, open_stp
, lock
);
4051 if (lock_sop
== NULL
)
4053 lock_stp
= alloc_init_lock_stateid(lock_sop
, fp
, open_stp
);
4054 if (lock_stp
== NULL
)
4057 /* lock (lock owner + lock stateid) already exists */
4058 status
= nfs4_preprocess_seqid_op(cstate
,
4059 lock
->lk_old_lock_seqid
,
4060 &lock
->lk_old_lock_stateid
,
4062 &lock
->lk_replay_owner
, &lock_stp
, lock
);
4065 lock_sop
= lock
->lk_replay_owner
;
4066 fp
= lock_stp
->st_file
;
4068 /* lock->lk_replay_owner and lock_stp have been created or found */
4070 status
= nfserr_grace
;
4071 if (locks_in_grace() && !lock
->lk_reclaim
)
4073 status
= nfserr_no_grace
;
4074 if (!locks_in_grace() && lock
->lk_reclaim
)
4077 locks_init_lock(&file_lock
);
4078 switch (lock
->lk_type
) {
4081 filp
= find_readable_file(lock_stp
->st_file
);
4083 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4084 file_lock
.fl_type
= F_RDLCK
;
4087 case NFS4_WRITEW_LT
:
4088 filp
= find_writeable_file(lock_stp
->st_file
);
4090 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4091 file_lock
.fl_type
= F_WRLCK
;
4094 status
= nfserr_inval
;
4098 status
= nfserr_openmode
;
4101 file_lock
.fl_owner
= (fl_owner_t
)lock_sop
;
4102 file_lock
.fl_pid
= current
->tgid
;
4103 file_lock
.fl_file
= filp
;
4104 file_lock
.fl_flags
= FL_POSIX
;
4105 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
4107 file_lock
.fl_start
= lock
->lk_offset
;
4108 file_lock
.fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
4109 nfs4_transform_lock_offset(&file_lock
);
4112 * Try to lock the file in the VFS.
4113 * Note: locks.c uses the BKL to protect the inode's lock list.
4116 err
= vfs_lock_file(filp
, F_SETLK
, &file_lock
, &conflock
);
4118 case 0: /* success! */
4119 update_stateid(&lock_stp
->st_stateid
);
4120 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stateid
,
4124 case (EAGAIN
): /* conflock holds conflicting lock */
4125 status
= nfserr_denied
;
4126 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4127 nfs4_set_lock_denied(&conflock
, &lock
->lk_denied
);
4130 status
= nfserr_deadlock
;
4133 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4134 status
= nfserrno(err
);
4138 if (status
&& lock
->lk_is_new
&& lock_sop
)
4139 release_lockowner(lock_sop
);
4140 if (lock
->lk_replay_owner
) {
4141 nfs4_get_stateowner(lock
->lk_replay_owner
);
4142 cstate
->replay_owner
= lock
->lk_replay_owner
;
4144 nfs4_unlock_state();
4149 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4150 * so we do a temporary open here just to get an open file to pass to
4151 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4154 static int nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4159 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4162 err
= vfs_test_lock(file
, lock
);
4171 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4172 struct nfsd4_lockt
*lockt
)
4174 struct inode
*inode
;
4175 struct file_lock file_lock
;
4179 if (locks_in_grace())
4180 return nfserr_grace
;
4182 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4183 return nfserr_inval
;
4185 lockt
->lt_stateowner
= NULL
;
4188 status
= nfserr_stale_clientid
;
4189 if (!nfsd4_has_session(cstate
) && STALE_CLIENTID(&lockt
->lt_clientid
))
4192 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0))) {
4193 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
4194 if (status
== nfserr_symlink
)
4195 status
= nfserr_inval
;
4199 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
4200 locks_init_lock(&file_lock
);
4201 switch (lockt
->lt_type
) {
4204 file_lock
.fl_type
= F_RDLCK
;
4207 case NFS4_WRITEW_LT
:
4208 file_lock
.fl_type
= F_WRLCK
;
4211 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4212 status
= nfserr_inval
;
4216 lockt
->lt_stateowner
= find_lockstateowner_str(inode
,
4217 &lockt
->lt_clientid
, &lockt
->lt_owner
);
4218 if (lockt
->lt_stateowner
)
4219 file_lock
.fl_owner
= (fl_owner_t
)lockt
->lt_stateowner
;
4220 file_lock
.fl_pid
= current
->tgid
;
4221 file_lock
.fl_flags
= FL_POSIX
;
4223 file_lock
.fl_start
= lockt
->lt_offset
;
4224 file_lock
.fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
4226 nfs4_transform_lock_offset(&file_lock
);
4229 error
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, &file_lock
);
4231 status
= nfserrno(error
);
4234 if (file_lock
.fl_type
!= F_UNLCK
) {
4235 status
= nfserr_denied
;
4236 nfs4_set_lock_denied(&file_lock
, &lockt
->lt_denied
);
4239 nfs4_unlock_state();
4244 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4245 struct nfsd4_locku
*locku
)
4247 struct nfs4_stateid
*stp
;
4248 struct file
*filp
= NULL
;
4249 struct file_lock file_lock
;
4253 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4254 (long long) locku
->lu_offset
,
4255 (long long) locku
->lu_length
);
4257 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
4258 return nfserr_inval
;
4262 if ((status
= nfs4_preprocess_seqid_op(cstate
,
4266 &locku
->lu_stateowner
, &stp
, NULL
)))
4269 filp
= find_any_file(stp
->st_file
);
4271 status
= nfserr_lock_range
;
4275 locks_init_lock(&file_lock
);
4276 file_lock
.fl_type
= F_UNLCK
;
4277 file_lock
.fl_owner
= (fl_owner_t
) locku
->lu_stateowner
;
4278 file_lock
.fl_pid
= current
->tgid
;
4279 file_lock
.fl_file
= filp
;
4280 file_lock
.fl_flags
= FL_POSIX
;
4281 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
4282 file_lock
.fl_start
= locku
->lu_offset
;
4284 file_lock
.fl_end
= last_byte_offset(locku
->lu_offset
, locku
->lu_length
);
4285 nfs4_transform_lock_offset(&file_lock
);
4288 * Try to unlock the file in the VFS.
4290 err
= vfs_lock_file(filp
, F_SETLK
, &file_lock
, NULL
);
4292 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4296 * OK, unlock succeeded; the only thing left to do is update the stateid.
4298 update_stateid(&stp
->st_stateid
);
4299 memcpy(&locku
->lu_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
4302 if (locku
->lu_stateowner
) {
4303 nfs4_get_stateowner(locku
->lu_stateowner
);
4304 cstate
->replay_owner
= locku
->lu_stateowner
;
4306 nfs4_unlock_state();
4310 status
= nfserrno(err
);
4316 * 1: locks held by lockowner
4317 * 0: no locks held by lockowner
4320 check_for_locks(struct nfs4_file
*filp
, struct nfs4_stateowner
*lowner
)
4322 struct file_lock
**flpp
;
4323 struct inode
*inode
= filp
->fi_inode
;
4327 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
4328 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
4339 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
4340 struct nfsd4_compound_state
*cstate
,
4341 struct nfsd4_release_lockowner
*rlockowner
)
4343 clientid_t
*clid
= &rlockowner
->rl_clientid
;
4344 struct nfs4_stateowner
*sop
;
4345 struct nfs4_stateid
*stp
;
4346 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
4347 struct list_head matches
;
4351 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4352 clid
->cl_boot
, clid
->cl_id
);
4354 /* XXX check for lease expiration */
4356 status
= nfserr_stale_clientid
;
4357 if (STALE_CLIENTID(clid
))
4362 status
= nfserr_locks_held
;
4363 /* XXX: we're doing a linear search through all the lockowners.
4364 * Yipes! For now we'll just hope clients aren't really using
4365 * release_lockowner much, but eventually we have to fix these
4366 * data structures. */
4367 INIT_LIST_HEAD(&matches
);
4368 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
4369 list_for_each_entry(sop
, &lock_ownerid_hashtbl
[i
], so_idhash
) {
4370 if (!same_owner_str(sop
, owner
, clid
))
4372 list_for_each_entry(stp
, &sop
->so_stateids
,
4374 if (check_for_locks(stp
->st_file
, sop
))
4376 /* Note: so_perclient unused for lockowners,
4377 * so it's OK to fool with here. */
4378 list_add(&sop
->so_perclient
, &matches
);
4382 /* Clients probably won't expect us to return with some (but not all)
4383 * of the lockowner state released; so don't release any until all
4384 * have been checked. */
4386 while (!list_empty(&matches
)) {
4387 sop
= list_entry(matches
.next
, struct nfs4_stateowner
,
4389 /* unhash_stateowner deletes so_perclient only
4390 * for openowners. */
4391 list_del(&sop
->so_perclient
);
4392 release_lockowner(sop
);
4395 nfs4_unlock_state();
4399 static inline struct nfs4_client_reclaim
*
4402 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
4406 nfs4_has_reclaimed_state(const char *name
, bool use_exchange_id
)
4408 unsigned int strhashval
= clientstr_hashval(name
);
4409 struct nfs4_client
*clp
;
4411 clp
= find_confirmed_client_by_str(name
, strhashval
);
4416 * failure => all reset bets are off, nfserr_no_grace...
4419 nfs4_client_to_reclaim(const char *name
)
4421 unsigned int strhashval
;
4422 struct nfs4_client_reclaim
*crp
= NULL
;
4424 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
4425 crp
= alloc_reclaim();
4428 strhashval
= clientstr_hashval(name
);
4429 INIT_LIST_HEAD(&crp
->cr_strhash
);
4430 list_add(&crp
->cr_strhash
, &reclaim_str_hashtbl
[strhashval
]);
4431 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
4432 reclaim_str_hashtbl_size
++;
4437 nfs4_release_reclaim(void)
4439 struct nfs4_client_reclaim
*crp
= NULL
;
4442 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4443 while (!list_empty(&reclaim_str_hashtbl
[i
])) {
4444 crp
= list_entry(reclaim_str_hashtbl
[i
].next
,
4445 struct nfs4_client_reclaim
, cr_strhash
);
4446 list_del(&crp
->cr_strhash
);
4448 reclaim_str_hashtbl_size
--;
4451 BUG_ON(reclaim_str_hashtbl_size
);
4455 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4456 static struct nfs4_client_reclaim
*
4457 nfs4_find_reclaim_client(clientid_t
*clid
)
4459 unsigned int strhashval
;
4460 struct nfs4_client
*clp
;
4461 struct nfs4_client_reclaim
*crp
= NULL
;
4464 /* find clientid in conf_id_hashtbl */
4465 clp
= find_confirmed_client(clid
);
4469 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4470 clp
->cl_name
.len
, clp
->cl_name
.data
,
4473 /* find clp->cl_name in reclaim_str_hashtbl */
4474 strhashval
= clientstr_hashval(clp
->cl_recdir
);
4475 list_for_each_entry(crp
, &reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
4476 if (same_name(crp
->cr_recdir
, clp
->cl_recdir
)) {
4484 * Called from OPEN. Look for clientid in reclaim list.
4487 nfs4_check_open_reclaim(clientid_t
*clid
)
4489 return nfs4_find_reclaim_client(clid
) ? nfs_ok
: nfserr_reclaim_bad
;
4492 /* initialization to perform at module load time: */
4495 nfs4_state_init(void)
4499 status
= nfsd4_init_slabs();
4502 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4503 INIT_LIST_HEAD(&conf_id_hashtbl
[i
]);
4504 INIT_LIST_HEAD(&conf_str_hashtbl
[i
]);
4505 INIT_LIST_HEAD(&unconf_str_hashtbl
[i
]);
4506 INIT_LIST_HEAD(&unconf_id_hashtbl
[i
]);
4507 INIT_LIST_HEAD(&reclaim_str_hashtbl
[i
]);
4509 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
4510 INIT_LIST_HEAD(&sessionid_hashtbl
[i
]);
4511 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
4512 INIT_LIST_HEAD(&file_hashtbl
[i
]);
4514 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++) {
4515 INIT_LIST_HEAD(&ownerstr_hashtbl
[i
]);
4516 INIT_LIST_HEAD(&ownerid_hashtbl
[i
]);
4518 for (i
= 0; i
< STATEID_HASH_SIZE
; i
++) {
4519 INIT_LIST_HEAD(&stateid_hashtbl
[i
]);
4520 INIT_LIST_HEAD(&lockstateid_hashtbl
[i
]);
4522 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
4523 INIT_LIST_HEAD(&lock_ownerid_hashtbl
[i
]);
4524 INIT_LIST_HEAD(&lock_ownerstr_hashtbl
[i
]);
4526 memset(&onestateid
, ~0, sizeof(stateid_t
));
4527 INIT_LIST_HEAD(&close_lru
);
4528 INIT_LIST_HEAD(&client_lru
);
4529 INIT_LIST_HEAD(&del_recall_lru
);
4530 reclaim_str_hashtbl_size
= 0;
4535 nfsd4_load_reboot_recovery_data(void)
4540 nfsd4_init_recdir(user_recovery_dirname
);
4541 status
= nfsd4_recdir_load();
4542 nfs4_unlock_state();
4544 printk("NFSD: Failure reading reboot recovery data\n");
4548 * Since the lifetime of a delegation isn't limited to that of an open, a
4549 * client may quite reasonably hang on to a delegation as long as it has
4550 * the inode cached. This becomes an obvious problem the first time a
4551 * client's inode cache approaches the size of the server's total memory.
4553 * For now we avoid this problem by imposing a hard limit on the number
4554 * of delegations, which varies according to the server's memory size.
4557 set_max_delegations(void)
4560 * Allow at most 4 delegations per megabyte of RAM. Quick
4561 * estimates suggest that in the worst case (where every delegation
4562 * is for a different inode), a delegation could take about 1.5K,
4563 * giving a worst case usage of about 6% of memory.
4565 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
4568 /* initialization to perform when the nfsd service is started: */
4571 __nfs4_state_start(void)
4575 boot_time
= get_seconds();
4576 locks_start_grace(&nfsd4_manager
);
4577 printk(KERN_INFO
"NFSD: starting %ld-second grace period\n",
4579 ret
= set_callback_cred();
4582 laundry_wq
= create_singlethread_workqueue("nfsd4");
4583 if (laundry_wq
== NULL
)
4585 ret
= nfsd4_create_callback_queue();
4587 goto out_free_laundry
;
4588 queue_delayed_work(laundry_wq
, &laundromat_work
, nfsd4_grace
* HZ
);
4589 set_max_delegations();
4592 destroy_workqueue(laundry_wq
);
4597 nfs4_state_start(void)
4599 nfsd4_load_reboot_recovery_data();
4600 return __nfs4_state_start();
4604 __nfs4_state_shutdown(void)
4607 struct nfs4_client
*clp
= NULL
;
4608 struct nfs4_delegation
*dp
= NULL
;
4609 struct list_head
*pos
, *next
, reaplist
;
4611 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4612 while (!list_empty(&conf_id_hashtbl
[i
])) {
4613 clp
= list_entry(conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
4616 while (!list_empty(&unconf_str_hashtbl
[i
])) {
4617 clp
= list_entry(unconf_str_hashtbl
[i
].next
, struct nfs4_client
, cl_strhash
);
4621 INIT_LIST_HEAD(&reaplist
);
4622 spin_lock(&recall_lock
);
4623 list_for_each_safe(pos
, next
, &del_recall_lru
) {
4624 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4625 list_move(&dp
->dl_recall_lru
, &reaplist
);
4627 spin_unlock(&recall_lock
);
4628 list_for_each_safe(pos
, next
, &reaplist
) {
4629 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4630 list_del_init(&dp
->dl_recall_lru
);
4631 unhash_delegation(dp
);
4634 nfsd4_shutdown_recdir();
4638 nfs4_state_shutdown(void)
4640 cancel_delayed_work_sync(&laundromat_work
);
4641 destroy_workqueue(laundry_wq
);
4642 locks_end_grace(&nfsd4_manager
);
4644 nfs4_release_reclaim();
4645 __nfs4_state_shutdown();
4646 nfs4_unlock_state();
4647 nfsd4_destroy_callback_queue();
4651 * user_recovery_dirname is protected by the nfsd_mutex since it's only
4652 * accessed when nfsd is starting.
4655 nfs4_set_recdir(char *recdir
)
4657 strcpy(user_recovery_dirname
, recdir
);
4661 * Change the NFSv4 recovery directory to recdir.
4664 nfs4_reset_recoverydir(char *recdir
)
4669 status
= kern_path(recdir
, LOOKUP_FOLLOW
, &path
);
4673 if (S_ISDIR(path
.dentry
->d_inode
->i_mode
)) {
4674 nfs4_set_recdir(recdir
);
4682 nfs4_recoverydir(void)
4684 return user_recovery_dirname
;