2 * linux/fs/nfsd/nfs4state.c
4 * Copyright (c) 2001 The Regents of the University of Michigan.
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <kandros@umich.edu>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/param.h>
38 #include <linux/major.h>
39 #include <linux/slab.h>
41 #include <linux/sunrpc/svc.h>
42 #include <linux/nfsd/nfsd.h>
43 #include <linux/nfsd/cache.h>
44 #include <linux/mount.h>
45 #include <linux/workqueue.h>
46 #include <linux/smp_lock.h>
47 #include <linux/kthread.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfsd/state.h>
50 #include <linux/nfsd/xdr4.h>
51 #include <linux/namei.h>
52 #include <linux/mutex.h>
53 #include <linux/lockd/bind.h>
55 #define NFSDDBG_FACILITY NFSDDBG_PROC
58 static time_t lease_time
= 90; /* default lease time */
59 static time_t user_lease_time
= 90;
60 static time_t boot_time
;
61 static int in_grace
= 1;
62 static u32 current_clientid
= 1;
63 static u32 current_ownerid
= 1;
64 static u32 current_fileid
= 1;
65 static u32 current_delegid
= 1;
67 static stateid_t zerostateid
; /* bits all 0 */
68 static stateid_t onestateid
; /* bits all 1 */
70 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
71 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
73 /* forward declarations */
74 static struct nfs4_stateid
* find_stateid(stateid_t
*stid
, int flags
);
75 static struct nfs4_delegation
* find_delegation_stateid(struct inode
*ino
, stateid_t
*stid
);
76 static void release_stateid_lockowners(struct nfs4_stateid
*open_stp
);
77 static char user_recovery_dirname
[PATH_MAX
] = "/var/lib/nfs/v4recovery";
78 static void nfs4_set_recdir(char *recdir
);
83 * protects clientid_hashtbl[], clientstr_hashtbl[],
84 * unconfstr_hashtbl[], uncofid_hashtbl[].
86 static DEFINE_MUTEX(client_mutex
);
88 static struct kmem_cache
*stateowner_slab
= NULL
;
89 static struct kmem_cache
*file_slab
= NULL
;
90 static struct kmem_cache
*stateid_slab
= NULL
;
91 static struct kmem_cache
*deleg_slab
= NULL
;
96 mutex_lock(&client_mutex
);
100 nfs4_unlock_state(void)
102 mutex_unlock(&client_mutex
);
106 opaque_hashval(const void *ptr
, int nbytes
)
108 unsigned char *cptr
= (unsigned char *) ptr
;
118 /* forward declarations */
119 static void release_stateowner(struct nfs4_stateowner
*sop
);
120 static void release_stateid(struct nfs4_stateid
*stp
, int flags
);
126 /* recall_lock protects the del_recall_lru */
127 static DEFINE_SPINLOCK(recall_lock
);
128 static struct list_head del_recall_lru
;
131 free_nfs4_file(struct kref
*kref
)
133 struct nfs4_file
*fp
= container_of(kref
, struct nfs4_file
, fi_ref
);
134 list_del(&fp
->fi_hash
);
136 kmem_cache_free(file_slab
, fp
);
140 put_nfs4_file(struct nfs4_file
*fi
)
142 kref_put(&fi
->fi_ref
, free_nfs4_file
);
146 get_nfs4_file(struct nfs4_file
*fi
)
148 kref_get(&fi
->fi_ref
);
151 static int num_delegations
;
154 * Open owner state (share locks)
157 /* hash tables for nfs4_stateowner */
158 #define OWNER_HASH_BITS 8
159 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
160 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
162 #define ownerid_hashval(id) \
163 ((id) & OWNER_HASH_MASK)
164 #define ownerstr_hashval(clientid, ownername) \
165 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
167 static struct list_head ownerid_hashtbl
[OWNER_HASH_SIZE
];
168 static struct list_head ownerstr_hashtbl
[OWNER_HASH_SIZE
];
170 /* hash table for nfs4_file */
171 #define FILE_HASH_BITS 8
172 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
173 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
174 /* hash table for (open)nfs4_stateid */
175 #define STATEID_HASH_BITS 10
176 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
177 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
179 #define file_hashval(x) \
180 hash_ptr(x, FILE_HASH_BITS)
181 #define stateid_hashval(owner_id, file_id) \
182 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
184 static struct list_head file_hashtbl
[FILE_HASH_SIZE
];
185 static struct list_head stateid_hashtbl
[STATEID_HASH_SIZE
];
187 static struct nfs4_delegation
*
188 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_stateid
*stp
, struct svc_fh
*current_fh
, u32 type
)
190 struct nfs4_delegation
*dp
;
191 struct nfs4_file
*fp
= stp
->st_file
;
192 struct nfs4_callback
*cb
= &stp
->st_stateowner
->so_client
->cl_callback
;
194 dprintk("NFSD alloc_init_deleg\n");
195 if (num_delegations
> STATEID_HASH_SIZE
* 4)
197 dp
= kmem_cache_alloc(deleg_slab
, GFP_KERNEL
);
201 INIT_LIST_HEAD(&dp
->dl_perfile
);
202 INIT_LIST_HEAD(&dp
->dl_perclnt
);
203 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
208 get_file(stp
->st_vfs_file
);
209 dp
->dl_vfs_file
= stp
->st_vfs_file
;
211 dp
->dl_recall
.cbr_dp
= NULL
;
212 dp
->dl_recall
.cbr_ident
= cb
->cb_ident
;
213 dp
->dl_recall
.cbr_trunc
= 0;
214 dp
->dl_stateid
.si_boot
= boot_time
;
215 dp
->dl_stateid
.si_stateownerid
= current_delegid
++;
216 dp
->dl_stateid
.si_fileid
= 0;
217 dp
->dl_stateid
.si_generation
= 0;
218 dp
->dl_fhlen
= current_fh
->fh_handle
.fh_size
;
219 memcpy(dp
->dl_fhval
, ¤t_fh
->fh_handle
.fh_base
,
220 current_fh
->fh_handle
.fh_size
);
222 atomic_set(&dp
->dl_count
, 1);
223 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
224 list_add(&dp
->dl_perclnt
, &clp
->cl_delegations
);
229 nfs4_put_delegation(struct nfs4_delegation
*dp
)
231 if (atomic_dec_and_test(&dp
->dl_count
)) {
232 dprintk("NFSD: freeing dp %p\n",dp
);
233 put_nfs4_file(dp
->dl_file
);
234 kmem_cache_free(deleg_slab
, dp
);
239 /* Remove the associated file_lock first, then remove the delegation.
240 * lease_modify() is called to remove the FS_LEASE file_lock from
241 * the i_flock list, eventually calling nfsd's lock_manager
242 * fl_release_callback.
245 nfs4_close_delegation(struct nfs4_delegation
*dp
)
247 struct file
*filp
= dp
->dl_vfs_file
;
249 dprintk("NFSD: close_delegation dp %p\n",dp
);
250 dp
->dl_vfs_file
= NULL
;
251 /* The following nfsd_close may not actually close the file,
252 * but we want to remove the lease in any case. */
254 setlease(filp
, F_UNLCK
, &dp
->dl_flock
);
258 /* Called under the state lock. */
260 unhash_delegation(struct nfs4_delegation
*dp
)
262 list_del_init(&dp
->dl_perfile
);
263 list_del_init(&dp
->dl_perclnt
);
264 spin_lock(&recall_lock
);
265 list_del_init(&dp
->dl_recall_lru
);
266 spin_unlock(&recall_lock
);
267 nfs4_close_delegation(dp
);
268 nfs4_put_delegation(dp
);
275 /* Hash tables for nfs4_clientid state */
276 #define CLIENT_HASH_BITS 4
277 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
278 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
280 #define clientid_hashval(id) \
281 ((id) & CLIENT_HASH_MASK)
282 #define clientstr_hashval(name) \
283 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
285 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
286 * used in reboot/reset lease grace period processing
288 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
289 * setclientid_confirmed info.
291 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
294 * client_lru holds client queue ordered by nfs4_client.cl_time
297 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
298 * for last close replay.
300 static struct list_head reclaim_str_hashtbl
[CLIENT_HASH_SIZE
];
301 static int reclaim_str_hashtbl_size
= 0;
302 static struct list_head conf_id_hashtbl
[CLIENT_HASH_SIZE
];
303 static struct list_head conf_str_hashtbl
[CLIENT_HASH_SIZE
];
304 static struct list_head unconf_str_hashtbl
[CLIENT_HASH_SIZE
];
305 static struct list_head unconf_id_hashtbl
[CLIENT_HASH_SIZE
];
306 static struct list_head client_lru
;
307 static struct list_head close_lru
;
310 renew_client(struct nfs4_client
*clp
)
313 * Move client to the end to the LRU list.
315 dprintk("renewing client (clientid %08x/%08x)\n",
316 clp
->cl_clientid
.cl_boot
,
317 clp
->cl_clientid
.cl_id
);
318 list_move_tail(&clp
->cl_lru
, &client_lru
);
319 clp
->cl_time
= get_seconds();
322 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
324 STALE_CLIENTID(clientid_t
*clid
)
326 if (clid
->cl_boot
== boot_time
)
328 dprintk("NFSD stale clientid (%08x/%08x)\n",
329 clid
->cl_boot
, clid
->cl_id
);
334 * XXX Should we use a slab cache ?
335 * This type of memory management is somewhat inefficient, but we use it
336 * anyway since SETCLIENTID is not a common operation.
338 static inline struct nfs4_client
*
339 alloc_client(struct xdr_netobj name
)
341 struct nfs4_client
*clp
;
343 if ((clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
))!= NULL
) {
344 if ((clp
->cl_name
.data
= kmalloc(name
.len
, GFP_KERNEL
)) != NULL
) {
345 memcpy(clp
->cl_name
.data
, name
.data
, name
.len
);
346 clp
->cl_name
.len
= name
.len
;
357 free_client(struct nfs4_client
*clp
)
359 if (clp
->cl_cred
.cr_group_info
)
360 put_group_info(clp
->cl_cred
.cr_group_info
);
361 kfree(clp
->cl_name
.data
);
366 put_nfs4_client(struct nfs4_client
*clp
)
368 if (atomic_dec_and_test(&clp
->cl_count
))
373 shutdown_callback_client(struct nfs4_client
*clp
)
375 struct rpc_clnt
*clnt
= clp
->cl_callback
.cb_client
;
377 /* shutdown rpc client, ending any outstanding recall rpcs */
379 clp
->cl_callback
.cb_client
= NULL
;
380 rpc_shutdown_client(clnt
);
385 expire_client(struct nfs4_client
*clp
)
387 struct nfs4_stateowner
*sop
;
388 struct nfs4_delegation
*dp
;
389 struct list_head reaplist
;
391 dprintk("NFSD: expire_client cl_count %d\n",
392 atomic_read(&clp
->cl_count
));
394 shutdown_callback_client(clp
);
396 INIT_LIST_HEAD(&reaplist
);
397 spin_lock(&recall_lock
);
398 while (!list_empty(&clp
->cl_delegations
)) {
399 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
400 dprintk("NFSD: expire client. dp %p, fp %p\n", dp
,
402 list_del_init(&dp
->dl_perclnt
);
403 list_move(&dp
->dl_recall_lru
, &reaplist
);
405 spin_unlock(&recall_lock
);
406 while (!list_empty(&reaplist
)) {
407 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
408 list_del_init(&dp
->dl_recall_lru
);
409 unhash_delegation(dp
);
411 list_del(&clp
->cl_idhash
);
412 list_del(&clp
->cl_strhash
);
413 list_del(&clp
->cl_lru
);
414 while (!list_empty(&clp
->cl_openowners
)) {
415 sop
= list_entry(clp
->cl_openowners
.next
, struct nfs4_stateowner
, so_perclient
);
416 release_stateowner(sop
);
418 put_nfs4_client(clp
);
421 static struct nfs4_client
*
422 create_client(struct xdr_netobj name
, char *recdir
) {
423 struct nfs4_client
*clp
;
425 if (!(clp
= alloc_client(name
)))
427 memcpy(clp
->cl_recdir
, recdir
, HEXDIR_LEN
);
428 atomic_set(&clp
->cl_count
, 1);
429 atomic_set(&clp
->cl_callback
.cb_set
, 0);
430 INIT_LIST_HEAD(&clp
->cl_idhash
);
431 INIT_LIST_HEAD(&clp
->cl_strhash
);
432 INIT_LIST_HEAD(&clp
->cl_openowners
);
433 INIT_LIST_HEAD(&clp
->cl_delegations
);
434 INIT_LIST_HEAD(&clp
->cl_lru
);
440 copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
) {
441 memcpy(target
->cl_verifier
.data
, source
->data
, sizeof(target
->cl_verifier
.data
));
445 copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
) {
446 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
447 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
451 copy_cred(struct svc_cred
*target
, struct svc_cred
*source
) {
453 target
->cr_uid
= source
->cr_uid
;
454 target
->cr_gid
= source
->cr_gid
;
455 target
->cr_group_info
= source
->cr_group_info
;
456 get_group_info(target
->cr_group_info
);
460 same_name(const char *n1
, const char *n2
) {
461 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
465 cmp_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
) {
466 return(!memcmp(v1
->data
,v2
->data
,sizeof(v1
->data
)));
470 cmp_clid(clientid_t
* cl1
, clientid_t
* cl2
) {
471 return((cl1
->cl_boot
== cl2
->cl_boot
) &&
472 (cl1
->cl_id
== cl2
->cl_id
));
475 /* XXX what about NGROUP */
477 cmp_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
){
478 return(cr1
->cr_uid
== cr2
->cr_uid
);
483 gen_clid(struct nfs4_client
*clp
) {
484 clp
->cl_clientid
.cl_boot
= boot_time
;
485 clp
->cl_clientid
.cl_id
= current_clientid
++;
489 gen_confirm(struct nfs4_client
*clp
) {
494 p
= (u32
*)clp
->cl_confirm
.data
;
500 check_name(struct xdr_netobj name
) {
504 if (name
.len
> NFS4_OPAQUE_LIMIT
) {
505 printk("NFSD: check_name: name too long(%d)!\n", name
.len
);
512 add_to_unconfirmed(struct nfs4_client
*clp
, unsigned int strhashval
)
514 unsigned int idhashval
;
516 list_add(&clp
->cl_strhash
, &unconf_str_hashtbl
[strhashval
]);
517 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
518 list_add(&clp
->cl_idhash
, &unconf_id_hashtbl
[idhashval
]);
519 list_add_tail(&clp
->cl_lru
, &client_lru
);
520 clp
->cl_time
= get_seconds();
524 move_to_confirmed(struct nfs4_client
*clp
)
526 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
527 unsigned int strhashval
;
529 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
530 list_del_init(&clp
->cl_strhash
);
531 list_move(&clp
->cl_idhash
, &conf_id_hashtbl
[idhashval
]);
532 strhashval
= clientstr_hashval(clp
->cl_recdir
);
533 list_add(&clp
->cl_strhash
, &conf_str_hashtbl
[strhashval
]);
537 static struct nfs4_client
*
538 find_confirmed_client(clientid_t
*clid
)
540 struct nfs4_client
*clp
;
541 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
543 list_for_each_entry(clp
, &conf_id_hashtbl
[idhashval
], cl_idhash
) {
544 if (cmp_clid(&clp
->cl_clientid
, clid
))
550 static struct nfs4_client
*
551 find_unconfirmed_client(clientid_t
*clid
)
553 struct nfs4_client
*clp
;
554 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
556 list_for_each_entry(clp
, &unconf_id_hashtbl
[idhashval
], cl_idhash
) {
557 if (cmp_clid(&clp
->cl_clientid
, clid
))
563 static struct nfs4_client
*
564 find_confirmed_client_by_str(const char *dname
, unsigned int hashval
)
566 struct nfs4_client
*clp
;
568 list_for_each_entry(clp
, &conf_str_hashtbl
[hashval
], cl_strhash
) {
569 if (same_name(clp
->cl_recdir
, dname
))
575 static struct nfs4_client
*
576 find_unconfirmed_client_by_str(const char *dname
, unsigned int hashval
)
578 struct nfs4_client
*clp
;
580 list_for_each_entry(clp
, &unconf_str_hashtbl
[hashval
], cl_strhash
) {
581 if (same_name(clp
->cl_recdir
, dname
))
587 /* a helper function for parse_callback */
589 parse_octet(unsigned int *lenp
, char **addrp
)
591 unsigned int len
= *lenp
;
603 if ((c
< '0') || (c
> '9')) {
609 n
= (n
* 10) + (c
- '0');
620 /* parse and set the setclientid ipv4 callback address */
622 parse_ipv4(unsigned int addr_len
, char *addr_val
, unsigned int *cbaddrp
, unsigned short *cbportp
)
627 u32 addrlen
= addr_len
;
628 char *addr
= addr_val
;
633 for(i
= 4; i
> 0 ; i
--) {
634 if ((temp
= parse_octet(&addrlen
, &addr
)) < 0) {
637 cbaddr
|= (temp
<< shift
);
645 for(i
= 2; i
> 0 ; i
--) {
646 if ((temp
= parse_octet(&addrlen
, &addr
)) < 0) {
649 cbport
|= (temp
<< shift
);
658 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
)
660 struct nfs4_callback
*cb
= &clp
->cl_callback
;
662 /* Currently, we only support tcp for the callback channel */
663 if ((se
->se_callback_netid_len
!= 3) || memcmp((char *)se
->se_callback_netid_val
, "tcp", 3))
666 if ( !(parse_ipv4(se
->se_callback_addr_len
, se
->se_callback_addr_val
,
667 &cb
->cb_addr
, &cb
->cb_port
)))
669 cb
->cb_prog
= se
->se_callback_prog
;
670 cb
->cb_ident
= se
->se_callback_ident
;
673 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
674 "will not receive delegations\n",
675 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
681 * RFC 3010 has a complex implmentation description of processing a
682 * SETCLIENTID request consisting of 5 bullets, labeled as
683 * CASE0 - CASE4 below.
686 * callback information will be processed in a future patch
688 * an unconfirmed record is added when:
689 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
690 * CASE 1: confirmed record found with matching name, principal,
691 * verifier, and clientid.
692 * CASE 2: confirmed record found with matching name, principal,
693 * and there is no unconfirmed record with matching
696 * an unconfirmed record is replaced when:
697 * CASE 3: confirmed record found with matching name, principal,
698 * and an unconfirmed record is found with matching
699 * name, principal, and with clientid and
700 * confirm that does not match the confirmed record.
701 * CASE 4: there is no confirmed record with matching name and
702 * principal. there is an unconfirmed record with
703 * matching name, principal.
705 * an unconfirmed record is deleted when:
706 * CASE 1: an unconfirmed record that matches input name, verifier,
707 * and confirmed clientid.
708 * CASE 4: any unconfirmed records with matching name and principal
709 * that exist after an unconfirmed record has been replaced
710 * as described above.
714 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
715 struct nfsd4_setclientid
*setclid
)
717 struct sockaddr_in
*sin
= svc_addr_in(rqstp
);
718 struct xdr_netobj clname
= {
719 .len
= setclid
->se_namelen
,
720 .data
= setclid
->se_name
,
722 nfs4_verifier clverifier
= setclid
->se_verf
;
723 unsigned int strhashval
;
724 struct nfs4_client
*conf
, *unconf
, *new;
726 char dname
[HEXDIR_LEN
];
728 if (!check_name(clname
))
731 status
= nfs4_make_rec_clidname(dname
, &clname
);
736 * XXX The Duplicate Request Cache (DRC) has been checked (??)
737 * We get here on a DRC miss.
740 strhashval
= clientstr_hashval(dname
);
743 conf
= find_confirmed_client_by_str(dname
, strhashval
);
747 * clname match, confirmed, different principal
748 * or different ip_address
750 status
= nfserr_clid_inuse
;
751 if (!cmp_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)
752 || conf
->cl_addr
!= sin
->sin_addr
.s_addr
) {
753 dprintk("NFSD: setclientid: string in use by client"
754 "at %u.%u.%u.%u\n", NIPQUAD(conf
->cl_addr
));
758 unconf
= find_unconfirmed_client_by_str(dname
, strhashval
);
759 status
= nfserr_resource
;
763 * placed first, because it is the normal case.
766 expire_client(unconf
);
767 new = create_client(clname
, dname
);
770 copy_verf(new, &clverifier
);
771 new->cl_addr
= sin
->sin_addr
.s_addr
;
772 copy_cred(&new->cl_cred
,&rqstp
->rq_cred
);
775 gen_callback(new, setclid
);
776 add_to_unconfirmed(new, strhashval
);
777 } else if (cmp_verf(&conf
->cl_verifier
, &clverifier
)) {
780 * cl_name match, confirmed, principal match
781 * verifier match: probable callback update
783 * remove any unconfirmed nfs4_client with
784 * matching cl_name, cl_verifier, and cl_clientid
786 * create and insert an unconfirmed nfs4_client with same
787 * cl_name, cl_verifier, and cl_clientid as existing
788 * nfs4_client, but with the new callback info and a
792 /* Note this is removing unconfirmed {*x***},
793 * which is stronger than RFC recommended {vxc**}.
794 * This has the advantage that there is at most
795 * one {*x***} in either list at any time.
797 expire_client(unconf
);
799 new = create_client(clname
, dname
);
802 copy_verf(new,&conf
->cl_verifier
);
803 new->cl_addr
= sin
->sin_addr
.s_addr
;
804 copy_cred(&new->cl_cred
,&rqstp
->rq_cred
);
805 copy_clid(new, conf
);
807 gen_callback(new, setclid
);
808 add_to_unconfirmed(new,strhashval
);
809 } else if (!unconf
) {
812 * clname match, confirmed, principal match
813 * verfier does not match
814 * no unconfirmed. create a new unconfirmed nfs4_client
815 * using input clverifier, clname, and callback info
816 * and generate a new cl_clientid and cl_confirm.
818 new = create_client(clname
, dname
);
821 copy_verf(new,&clverifier
);
822 new->cl_addr
= sin
->sin_addr
.s_addr
;
823 copy_cred(&new->cl_cred
,&rqstp
->rq_cred
);
826 gen_callback(new, setclid
);
827 add_to_unconfirmed(new, strhashval
);
828 } else if (!cmp_verf(&conf
->cl_confirm
, &unconf
->cl_confirm
)) {
831 * confirmed found (name, principal match)
832 * confirmed verifier does not match input clverifier
834 * unconfirmed found (name match)
835 * confirmed->cl_confirm != unconfirmed->cl_confirm
837 * remove unconfirmed.
839 * create an unconfirmed nfs4_client
840 * with same cl_name as existing confirmed nfs4_client,
841 * but with new callback info, new cl_clientid,
842 * new cl_verifier and a new cl_confirm
844 expire_client(unconf
);
845 new = create_client(clname
, dname
);
848 copy_verf(new,&clverifier
);
849 new->cl_addr
= sin
->sin_addr
.s_addr
;
850 copy_cred(&new->cl_cred
,&rqstp
->rq_cred
);
853 gen_callback(new, setclid
);
854 add_to_unconfirmed(new, strhashval
);
856 /* No cases hit !!! */
857 status
= nfserr_inval
;
861 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
862 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
863 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
872 * RFC 3010 has a complex implmentation description of processing a
873 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
874 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
876 * NOTE: callback information will be processed here in a future patch
879 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
880 struct nfsd4_compound_state
*cstate
,
881 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
883 struct sockaddr_in
*sin
= svc_addr_in(rqstp
);
884 struct nfs4_client
*conf
, *unconf
;
885 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
886 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
889 if (STALE_CLIENTID(clid
))
890 return nfserr_stale_clientid
;
892 * XXX The Duplicate Request Cache (DRC) has been checked (??)
893 * We get here on a DRC miss.
898 conf
= find_confirmed_client(clid
);
899 unconf
= find_unconfirmed_client(clid
);
901 status
= nfserr_clid_inuse
;
902 if (conf
&& conf
->cl_addr
!= sin
->sin_addr
.s_addr
)
904 if (unconf
&& unconf
->cl_addr
!= sin
->sin_addr
.s_addr
)
907 if ((conf
&& unconf
) &&
908 (cmp_verf(&unconf
->cl_confirm
, &confirm
)) &&
909 (cmp_verf(&conf
->cl_verifier
, &unconf
->cl_verifier
)) &&
910 (same_name(conf
->cl_recdir
,unconf
->cl_recdir
)) &&
911 (!cmp_verf(&conf
->cl_confirm
, &unconf
->cl_confirm
))) {
913 * unconf record that matches input clientid and input confirm.
914 * conf record that matches input clientid.
915 * conf and unconf records match names, verifiers
917 if (!cmp_creds(&conf
->cl_cred
, &unconf
->cl_cred
))
918 status
= nfserr_clid_inuse
;
920 /* XXX: We just turn off callbacks until we can handle
921 * change request correctly. */
922 atomic_set(&conf
->cl_callback
.cb_set
, 0);
924 nfsd4_remove_clid_dir(unconf
);
925 expire_client(unconf
);
929 } else if ((conf
&& !unconf
) ||
931 (!cmp_verf(&conf
->cl_verifier
, &unconf
->cl_verifier
) ||
932 !same_name(conf
->cl_recdir
, unconf
->cl_recdir
)))) {
934 * conf record that matches input clientid.
935 * if unconf record matches input clientid, then
936 * unconf->cl_name or unconf->cl_verifier don't match the
939 if (!cmp_creds(&conf
->cl_cred
,&rqstp
->rq_cred
))
940 status
= nfserr_clid_inuse
;
943 } else if (!conf
&& unconf
944 && cmp_verf(&unconf
->cl_confirm
, &confirm
)) {
946 * conf record not found.
947 * unconf record found.
948 * unconf->cl_confirm matches input confirm
950 if (!cmp_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
)) {
951 status
= nfserr_clid_inuse
;
954 clientstr_hashval(unconf
->cl_recdir
);
955 conf
= find_confirmed_client_by_str(unconf
->cl_recdir
,
958 nfsd4_remove_clid_dir(conf
);
961 move_to_confirmed(unconf
);
965 } else if ((!conf
|| (conf
&& !cmp_verf(&conf
->cl_confirm
, &confirm
)))
966 && (!unconf
|| (unconf
&& !cmp_verf(&unconf
->cl_confirm
,
969 * conf record not found, or if conf, conf->cl_confirm does not
970 * match input confirm.
971 * unconf record not found, or if unconf, unconf->cl_confirm
972 * does not match input confirm.
974 status
= nfserr_stale_clientid
;
976 /* check that we have hit one of the cases...*/
977 status
= nfserr_clid_inuse
;
981 nfsd4_probe_callback(conf
);
986 /* OPEN Share state helper functions */
987 static inline struct nfs4_file
*
988 alloc_init_file(struct inode
*ino
)
990 struct nfs4_file
*fp
;
991 unsigned int hashval
= file_hashval(ino
);
993 fp
= kmem_cache_alloc(file_slab
, GFP_KERNEL
);
995 kref_init(&fp
->fi_ref
);
996 INIT_LIST_HEAD(&fp
->fi_hash
);
997 INIT_LIST_HEAD(&fp
->fi_stateids
);
998 INIT_LIST_HEAD(&fp
->fi_delegations
);
999 list_add(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
1000 fp
->fi_inode
= igrab(ino
);
1001 fp
->fi_id
= current_fileid
++;
1008 nfsd4_free_slab(struct kmem_cache
**slab
)
1012 kmem_cache_destroy(*slab
);
1017 nfsd4_free_slabs(void)
1019 nfsd4_free_slab(&stateowner_slab
);
1020 nfsd4_free_slab(&file_slab
);
1021 nfsd4_free_slab(&stateid_slab
);
1022 nfsd4_free_slab(&deleg_slab
);
1026 nfsd4_init_slabs(void)
1028 stateowner_slab
= kmem_cache_create("nfsd4_stateowners",
1029 sizeof(struct nfs4_stateowner
), 0, 0, NULL
, NULL
);
1030 if (stateowner_slab
== NULL
)
1032 file_slab
= kmem_cache_create("nfsd4_files",
1033 sizeof(struct nfs4_file
), 0, 0, NULL
, NULL
);
1034 if (file_slab
== NULL
)
1036 stateid_slab
= kmem_cache_create("nfsd4_stateids",
1037 sizeof(struct nfs4_stateid
), 0, 0, NULL
, NULL
);
1038 if (stateid_slab
== NULL
)
1040 deleg_slab
= kmem_cache_create("nfsd4_delegations",
1041 sizeof(struct nfs4_delegation
), 0, 0, NULL
, NULL
);
1042 if (deleg_slab
== NULL
)
1047 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1052 nfs4_free_stateowner(struct kref
*kref
)
1054 struct nfs4_stateowner
*sop
=
1055 container_of(kref
, struct nfs4_stateowner
, so_ref
);
1056 kfree(sop
->so_owner
.data
);
1057 kmem_cache_free(stateowner_slab
, sop
);
1060 static inline struct nfs4_stateowner
*
1061 alloc_stateowner(struct xdr_netobj
*owner
)
1063 struct nfs4_stateowner
*sop
;
1065 if ((sop
= kmem_cache_alloc(stateowner_slab
, GFP_KERNEL
))) {
1066 if ((sop
->so_owner
.data
= kmalloc(owner
->len
, GFP_KERNEL
))) {
1067 memcpy(sop
->so_owner
.data
, owner
->data
, owner
->len
);
1068 sop
->so_owner
.len
= owner
->len
;
1069 kref_init(&sop
->so_ref
);
1072 kmem_cache_free(stateowner_slab
, sop
);
1077 static struct nfs4_stateowner
*
1078 alloc_init_open_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfsd4_open
*open
) {
1079 struct nfs4_stateowner
*sop
;
1080 struct nfs4_replay
*rp
;
1081 unsigned int idhashval
;
1083 if (!(sop
= alloc_stateowner(&open
->op_owner
)))
1085 idhashval
= ownerid_hashval(current_ownerid
);
1086 INIT_LIST_HEAD(&sop
->so_idhash
);
1087 INIT_LIST_HEAD(&sop
->so_strhash
);
1088 INIT_LIST_HEAD(&sop
->so_perclient
);
1089 INIT_LIST_HEAD(&sop
->so_stateids
);
1090 INIT_LIST_HEAD(&sop
->so_perstateid
); /* not used */
1091 INIT_LIST_HEAD(&sop
->so_close_lru
);
1093 list_add(&sop
->so_idhash
, &ownerid_hashtbl
[idhashval
]);
1094 list_add(&sop
->so_strhash
, &ownerstr_hashtbl
[strhashval
]);
1095 list_add(&sop
->so_perclient
, &clp
->cl_openowners
);
1096 sop
->so_is_open_owner
= 1;
1097 sop
->so_id
= current_ownerid
++;
1098 sop
->so_client
= clp
;
1099 sop
->so_seqid
= open
->op_seqid
;
1100 sop
->so_confirmed
= 0;
1101 rp
= &sop
->so_replay
;
1102 rp
->rp_status
= nfserr_serverfault
;
1104 rp
->rp_buf
= rp
->rp_ibuf
;
1109 release_stateid_lockowners(struct nfs4_stateid
*open_stp
)
1111 struct nfs4_stateowner
*lock_sop
;
1113 while (!list_empty(&open_stp
->st_lockowners
)) {
1114 lock_sop
= list_entry(open_stp
->st_lockowners
.next
,
1115 struct nfs4_stateowner
, so_perstateid
);
1116 /* list_del(&open_stp->st_lockowners); */
1117 BUG_ON(lock_sop
->so_is_open_owner
);
1118 release_stateowner(lock_sop
);
1123 unhash_stateowner(struct nfs4_stateowner
*sop
)
1125 struct nfs4_stateid
*stp
;
1127 list_del(&sop
->so_idhash
);
1128 list_del(&sop
->so_strhash
);
1129 if (sop
->so_is_open_owner
)
1130 list_del(&sop
->so_perclient
);
1131 list_del(&sop
->so_perstateid
);
1132 while (!list_empty(&sop
->so_stateids
)) {
1133 stp
= list_entry(sop
->so_stateids
.next
,
1134 struct nfs4_stateid
, st_perstateowner
);
1135 if (sop
->so_is_open_owner
)
1136 release_stateid(stp
, OPEN_STATE
);
1138 release_stateid(stp
, LOCK_STATE
);
1143 release_stateowner(struct nfs4_stateowner
*sop
)
1145 unhash_stateowner(sop
);
1146 list_del(&sop
->so_close_lru
);
1147 nfs4_put_stateowner(sop
);
1151 init_stateid(struct nfs4_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
1152 struct nfs4_stateowner
*sop
= open
->op_stateowner
;
1153 unsigned int hashval
= stateid_hashval(sop
->so_id
, fp
->fi_id
);
1155 INIT_LIST_HEAD(&stp
->st_hash
);
1156 INIT_LIST_HEAD(&stp
->st_perstateowner
);
1157 INIT_LIST_HEAD(&stp
->st_lockowners
);
1158 INIT_LIST_HEAD(&stp
->st_perfile
);
1159 list_add(&stp
->st_hash
, &stateid_hashtbl
[hashval
]);
1160 list_add(&stp
->st_perstateowner
, &sop
->so_stateids
);
1161 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
1162 stp
->st_stateowner
= sop
;
1165 stp
->st_stateid
.si_boot
= boot_time
;
1166 stp
->st_stateid
.si_stateownerid
= sop
->so_id
;
1167 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
1168 stp
->st_stateid
.si_generation
= 0;
1169 stp
->st_access_bmap
= 0;
1170 stp
->st_deny_bmap
= 0;
1171 __set_bit(open
->op_share_access
, &stp
->st_access_bmap
);
1172 __set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
1173 stp
->st_openstp
= NULL
;
1177 release_stateid(struct nfs4_stateid
*stp
, int flags
)
1179 struct file
*filp
= stp
->st_vfs_file
;
1181 list_del(&stp
->st_hash
);
1182 list_del(&stp
->st_perfile
);
1183 list_del(&stp
->st_perstateowner
);
1184 if (flags
& OPEN_STATE
) {
1185 release_stateid_lockowners(stp
);
1186 stp
->st_vfs_file
= NULL
;
1188 } else if (flags
& LOCK_STATE
)
1189 locks_remove_posix(filp
, (fl_owner_t
) stp
->st_stateowner
);
1190 put_nfs4_file(stp
->st_file
);
1191 kmem_cache_free(stateid_slab
, stp
);
1195 move_to_close_lru(struct nfs4_stateowner
*sop
)
1197 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop
);
1199 list_move_tail(&sop
->so_close_lru
, &close_lru
);
1200 sop
->so_time
= get_seconds();
1204 cmp_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
, clientid_t
*clid
) {
1205 return ((sop
->so_owner
.len
== owner
->len
) &&
1206 !memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
1207 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
));
1210 static struct nfs4_stateowner
*
1211 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
)
1213 struct nfs4_stateowner
*so
= NULL
;
1215 list_for_each_entry(so
, &ownerstr_hashtbl
[hashval
], so_strhash
) {
1216 if (cmp_owner_str(so
, &open
->op_owner
, &open
->op_clientid
))
1222 /* search file_hashtbl[] for file */
1223 static struct nfs4_file
*
1224 find_file(struct inode
*ino
)
1226 unsigned int hashval
= file_hashval(ino
);
1227 struct nfs4_file
*fp
;
1229 list_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
1230 if (fp
->fi_inode
== ino
) {
1238 static int access_valid(u32 x
)
1240 return (x
> 0 && x
< 4);
1243 static int deny_valid(u32 x
)
1245 return (x
>= 0 && x
< 5);
1249 set_access(unsigned int *access
, unsigned long bmap
) {
1253 for (i
= 1; i
< 4; i
++) {
1254 if (test_bit(i
, &bmap
))
1260 set_deny(unsigned int *deny
, unsigned long bmap
) {
1264 for (i
= 0; i
< 4; i
++) {
1265 if (test_bit(i
, &bmap
))
1271 test_share(struct nfs4_stateid
*stp
, struct nfsd4_open
*open
) {
1272 unsigned int access
, deny
;
1274 set_access(&access
, stp
->st_access_bmap
);
1275 set_deny(&deny
, stp
->st_deny_bmap
);
1276 if ((access
& open
->op_share_deny
) || (deny
& open
->op_share_access
))
1282 * Called to check deny when READ with all zero stateid or
1283 * WRITE with all zero or all one stateid
1286 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
1288 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
1289 struct nfs4_file
*fp
;
1290 struct nfs4_stateid
*stp
;
1293 dprintk("NFSD: nfs4_share_conflict\n");
1295 fp
= find_file(ino
);
1298 ret
= nfserr_locked
;
1299 /* Search for conflicting share reservations */
1300 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
) {
1301 if (test_bit(deny_type
, &stp
->st_deny_bmap
) ||
1302 test_bit(NFS4_SHARE_DENY_BOTH
, &stp
->st_deny_bmap
))
1312 nfs4_file_downgrade(struct file
*filp
, unsigned int share_access
)
1314 if (share_access
& NFS4_SHARE_ACCESS_WRITE
) {
1315 put_write_access(filp
->f_path
.dentry
->d_inode
);
1316 filp
->f_mode
= (filp
->f_mode
| FMODE_READ
) & ~FMODE_WRITE
;
1321 * Recall a delegation
1324 do_recall(void *__dp
)
1326 struct nfs4_delegation
*dp
= __dp
;
1328 nfsd4_cb_recall(dp
);
1333 * Spawn a thread to perform a recall on the delegation represented
1334 * by the lease (file_lock)
1336 * Called from break_lease() with lock_kernel() held.
1337 * Note: we assume break_lease will only call this *once* for any given
1341 void nfsd_break_deleg_cb(struct file_lock
*fl
)
1343 struct nfs4_delegation
*dp
= (struct nfs4_delegation
*)fl
->fl_owner
;
1344 struct task_struct
*t
;
1346 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp
,fl
);
1350 /* We're assuming the state code never drops its reference
1351 * without first removing the lease. Since we're in this lease
1352 * callback (and since the lease code is serialized by the kernel
1353 * lock) we know the server hasn't removed the lease yet, we know
1354 * it's safe to take a reference: */
1355 atomic_inc(&dp
->dl_count
);
1357 spin_lock(&recall_lock
);
1358 list_add_tail(&dp
->dl_recall_lru
, &del_recall_lru
);
1359 spin_unlock(&recall_lock
);
1361 /* only place dl_time is set. protected by lock_kernel*/
1362 dp
->dl_time
= get_seconds();
1364 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1365 fl
->fl_break_time
= jiffies
+ NFSD_LEASE_TIME
* HZ
;
1367 t
= kthread_run(do_recall
, dp
, "%s", "nfs4_cb_recall");
1369 struct nfs4_client
*clp
= dp
->dl_client
;
1371 printk(KERN_INFO
"NFSD: Callback thread failed for "
1372 "for client (clientid %08x/%08x)\n",
1373 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1374 nfs4_put_delegation(dp
);
1379 * The file_lock is being reapd.
1381 * Called by locks_free_lock() with lock_kernel() held.
1384 void nfsd_release_deleg_cb(struct file_lock
*fl
)
1386 struct nfs4_delegation
*dp
= (struct nfs4_delegation
*)fl
->fl_owner
;
1388 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl
,dp
, atomic_read(&dp
->dl_count
));
1390 if (!(fl
->fl_flags
& FL_LEASE
) || !dp
)
1392 dp
->dl_flock
= NULL
;
1396 * Set the delegation file_lock back pointer.
1398 * Called from __setlease() with lock_kernel() held.
1401 void nfsd_copy_lock_deleg_cb(struct file_lock
*new, struct file_lock
*fl
)
1403 struct nfs4_delegation
*dp
= (struct nfs4_delegation
*)new->fl_owner
;
1405 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp
);
1412 * Called from __setlease() with lock_kernel() held
1415 int nfsd_same_client_deleg_cb(struct file_lock
*onlist
, struct file_lock
*try)
1417 struct nfs4_delegation
*onlistd
=
1418 (struct nfs4_delegation
*)onlist
->fl_owner
;
1419 struct nfs4_delegation
*tryd
=
1420 (struct nfs4_delegation
*)try->fl_owner
;
1422 if (onlist
->fl_lmops
!= try->fl_lmops
)
1425 return onlistd
->dl_client
== tryd
->dl_client
;
1430 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
1433 return lease_modify(onlist
, arg
);
1438 static struct lock_manager_operations nfsd_lease_mng_ops
= {
1439 .fl_break
= nfsd_break_deleg_cb
,
1440 .fl_release_private
= nfsd_release_deleg_cb
,
1441 .fl_copy_lock
= nfsd_copy_lock_deleg_cb
,
1442 .fl_mylease
= nfsd_same_client_deleg_cb
,
1443 .fl_change
= nfsd_change_deleg_cb
,
1448 nfsd4_process_open1(struct nfsd4_open
*open
)
1450 clientid_t
*clientid
= &open
->op_clientid
;
1451 struct nfs4_client
*clp
= NULL
;
1452 unsigned int strhashval
;
1453 struct nfs4_stateowner
*sop
= NULL
;
1455 if (!check_name(open
->op_owner
))
1456 return nfserr_inval
;
1458 if (STALE_CLIENTID(&open
->op_clientid
))
1459 return nfserr_stale_clientid
;
1461 strhashval
= ownerstr_hashval(clientid
->cl_id
, open
->op_owner
);
1462 sop
= find_openstateowner_str(strhashval
, open
);
1463 open
->op_stateowner
= sop
;
1465 /* Make sure the client's lease hasn't expired. */
1466 clp
= find_confirmed_client(clientid
);
1468 return nfserr_expired
;
1471 if (!sop
->so_confirmed
) {
1472 /* Replace unconfirmed owners without checking for replay. */
1473 clp
= sop
->so_client
;
1474 release_stateowner(sop
);
1475 open
->op_stateowner
= NULL
;
1478 if (open
->op_seqid
== sop
->so_seqid
- 1) {
1479 if (sop
->so_replay
.rp_buflen
)
1480 return nfserr_replay_me
;
1481 /* The original OPEN failed so spectacularly
1482 * that we don't even have replay data saved!
1483 * Therefore, we have no choice but to continue
1484 * processing this OPEN; presumably, we'll
1485 * fail again for the same reason.
1487 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1490 if (open
->op_seqid
!= sop
->so_seqid
)
1491 return nfserr_bad_seqid
;
1493 if (open
->op_stateowner
== NULL
) {
1494 sop
= alloc_init_open_stateowner(strhashval
, clp
, open
);
1496 return nfserr_resource
;
1497 open
->op_stateowner
= sop
;
1499 list_del_init(&sop
->so_close_lru
);
1500 renew_client(sop
->so_client
);
1504 static inline __be32
1505 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
1507 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
1508 return nfserr_openmode
;
1513 static struct nfs4_delegation
*
1514 find_delegation_file(struct nfs4_file
*fp
, stateid_t
*stid
)
1516 struct nfs4_delegation
*dp
;
1518 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
) {
1519 if (dp
->dl_stateid
.si_stateownerid
== stid
->si_stateownerid
)
1526 nfs4_check_deleg(struct nfs4_file
*fp
, struct nfsd4_open
*open
,
1527 struct nfs4_delegation
**dp
)
1530 __be32 status
= nfserr_bad_stateid
;
1532 *dp
= find_delegation_file(fp
, &open
->op_delegate_stateid
);
1535 flags
= open
->op_share_access
== NFS4_SHARE_ACCESS_READ
?
1536 RD_STATE
: WR_STATE
;
1537 status
= nfs4_check_delegmode(*dp
, flags
);
1541 if (open
->op_claim_type
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
)
1545 open
->op_stateowner
->so_confirmed
= 1;
1550 nfs4_check_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
, struct nfs4_stateid
**stpp
)
1552 struct nfs4_stateid
*local
;
1553 __be32 status
= nfserr_share_denied
;
1554 struct nfs4_stateowner
*sop
= open
->op_stateowner
;
1556 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
1557 /* ignore lock owners */
1558 if (local
->st_stateowner
->so_is_open_owner
== 0)
1560 /* remember if we have seen this open owner */
1561 if (local
->st_stateowner
== sop
)
1563 /* check for conflicting share reservations */
1564 if (!test_share(local
, open
))
1572 static inline struct nfs4_stateid
*
1573 nfs4_alloc_stateid(void)
1575 return kmem_cache_alloc(stateid_slab
, GFP_KERNEL
);
1579 nfs4_new_open(struct svc_rqst
*rqstp
, struct nfs4_stateid
**stpp
,
1580 struct nfs4_delegation
*dp
,
1581 struct svc_fh
*cur_fh
, int flags
)
1583 struct nfs4_stateid
*stp
;
1585 stp
= nfs4_alloc_stateid();
1587 return nfserr_resource
;
1590 get_file(dp
->dl_vfs_file
);
1591 stp
->st_vfs_file
= dp
->dl_vfs_file
;
1594 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, flags
,
1597 if (status
== nfserr_dropit
)
1598 status
= nfserr_jukebox
;
1599 kmem_cache_free(stateid_slab
, stp
);
1607 static inline __be32
1608 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
1609 struct nfsd4_open
*open
)
1611 struct iattr iattr
= {
1612 .ia_valid
= ATTR_SIZE
,
1615 if (!open
->op_truncate
)
1617 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
1618 return nfserr_inval
;
1619 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
1623 nfs4_upgrade_open(struct svc_rqst
*rqstp
, struct svc_fh
*cur_fh
, struct nfs4_stateid
*stp
, struct nfsd4_open
*open
)
1625 struct file
*filp
= stp
->st_vfs_file
;
1626 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1627 unsigned int share_access
, new_writer
;
1630 set_access(&share_access
, stp
->st_access_bmap
);
1631 new_writer
= (~share_access
) & open
->op_share_access
1632 & NFS4_SHARE_ACCESS_WRITE
;
1635 int err
= get_write_access(inode
);
1637 return nfserrno(err
);
1639 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
1642 put_write_access(inode
);
1645 /* remember the open */
1646 filp
->f_mode
|= open
->op_share_access
;
1647 set_bit(open
->op_share_access
, &stp
->st_access_bmap
);
1648 set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
1655 nfs4_set_claim_prev(struct nfsd4_open
*open
)
1657 open
->op_stateowner
->so_confirmed
= 1;
1658 open
->op_stateowner
->so_client
->cl_firststate
= 1;
1662 * Attempt to hand out a delegation.
1665 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
, struct nfs4_stateid
*stp
)
1667 struct nfs4_delegation
*dp
;
1668 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
1669 struct nfs4_callback
*cb
= &sop
->so_client
->cl_callback
;
1670 struct file_lock fl
, *flp
= &fl
;
1671 int status
, flag
= 0;
1673 flag
= NFS4_OPEN_DELEGATE_NONE
;
1674 open
->op_recall
= 0;
1675 switch (open
->op_claim_type
) {
1676 case NFS4_OPEN_CLAIM_PREVIOUS
:
1677 if (!atomic_read(&cb
->cb_set
))
1678 open
->op_recall
= 1;
1679 flag
= open
->op_delegate_type
;
1680 if (flag
== NFS4_OPEN_DELEGATE_NONE
)
1683 case NFS4_OPEN_CLAIM_NULL
:
1684 /* Let's not give out any delegations till everyone's
1685 * had the chance to reclaim theirs.... */
1686 if (nfs4_in_grace())
1688 if (!atomic_read(&cb
->cb_set
) || !sop
->so_confirmed
)
1690 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
1691 flag
= NFS4_OPEN_DELEGATE_WRITE
;
1693 flag
= NFS4_OPEN_DELEGATE_READ
;
1699 dp
= alloc_init_deleg(sop
->so_client
, stp
, fh
, flag
);
1701 flag
= NFS4_OPEN_DELEGATE_NONE
;
1704 locks_init_lock(&fl
);
1705 fl
.fl_lmops
= &nfsd_lease_mng_ops
;
1706 fl
.fl_flags
= FL_LEASE
;
1707 fl
.fl_end
= OFFSET_MAX
;
1708 fl
.fl_owner
= (fl_owner_t
)dp
;
1709 fl
.fl_file
= stp
->st_vfs_file
;
1710 fl
.fl_pid
= current
->tgid
;
1712 /* setlease checks to see if delegation should be handed out.
1713 * the lock_manager callbacks fl_mylease and fl_change are used
1715 if ((status
= setlease(stp
->st_vfs_file
,
1716 flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
, &flp
))) {
1717 dprintk("NFSD: setlease failed [%d], no delegation\n", status
);
1718 unhash_delegation(dp
);
1719 flag
= NFS4_OPEN_DELEGATE_NONE
;
1723 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stateid
, sizeof(dp
->dl_stateid
));
1725 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1726 dp
->dl_stateid
.si_boot
,
1727 dp
->dl_stateid
.si_stateownerid
,
1728 dp
->dl_stateid
.si_fileid
,
1729 dp
->dl_stateid
.si_generation
);
1731 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
1732 && flag
== NFS4_OPEN_DELEGATE_NONE
1733 && open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
)
1734 printk("NFSD: WARNING: refusing delegation reclaim\n");
1735 open
->op_delegate_type
= flag
;
1739 * called with nfs4_lock_state() held.
1742 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
1744 struct nfs4_file
*fp
= NULL
;
1745 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
1746 struct nfs4_stateid
*stp
= NULL
;
1747 struct nfs4_delegation
*dp
= NULL
;
1750 status
= nfserr_inval
;
1751 if (!access_valid(open
->op_share_access
)
1752 || !deny_valid(open
->op_share_deny
))
1755 * Lookup file; if found, lookup stateid and check open request,
1756 * and check for delegations in the process of being recalled.
1757 * If not found, create the nfs4_file struct
1759 fp
= find_file(ino
);
1761 if ((status
= nfs4_check_open(fp
, open
, &stp
)))
1763 status
= nfs4_check_deleg(fp
, open
, &dp
);
1767 status
= nfserr_bad_stateid
;
1768 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
)
1770 status
= nfserr_resource
;
1771 fp
= alloc_init_file(ino
);
1777 * OPEN the file, or upgrade an existing OPEN.
1778 * If truncate fails, the OPEN fails.
1781 /* Stateid was found, this is an OPEN upgrade */
1782 status
= nfs4_upgrade_open(rqstp
, current_fh
, stp
, open
);
1785 update_stateid(&stp
->st_stateid
);
1787 /* Stateid was not found, this is a new OPEN */
1789 if (open
->op_share_access
& NFS4_SHARE_ACCESS_READ
)
1791 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
1793 status
= nfs4_new_open(rqstp
, &stp
, dp
, current_fh
, flags
);
1796 init_stateid(stp
, fp
, open
);
1797 status
= nfsd4_truncate(rqstp
, current_fh
, open
);
1799 release_stateid(stp
, OPEN_STATE
);
1803 memcpy(&open
->op_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
1806 * Attempt to hand out a delegation. No error return, because the
1807 * OPEN succeeds even if we fail.
1809 nfs4_open_delegation(current_fh
, open
, stp
);
1813 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1814 stp
->st_stateid
.si_boot
, stp
->st_stateid
.si_stateownerid
,
1815 stp
->st_stateid
.si_fileid
, stp
->st_stateid
.si_generation
);
1819 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
1820 nfs4_set_claim_prev(open
);
1822 * To finish the open response, we just need to set the rflags.
1824 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
1825 if (!open
->op_stateowner
->so_confirmed
)
1826 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
1831 static struct workqueue_struct
*laundry_wq
;
1832 static void laundromat_main(struct work_struct
*);
1833 static DECLARE_DELAYED_WORK(laundromat_work
, laundromat_main
);
1836 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
1839 struct nfs4_client
*clp
;
1843 dprintk("process_renew(%08x/%08x): starting\n",
1844 clid
->cl_boot
, clid
->cl_id
);
1845 status
= nfserr_stale_clientid
;
1846 if (STALE_CLIENTID(clid
))
1848 clp
= find_confirmed_client(clid
);
1849 status
= nfserr_expired
;
1851 /* We assume the client took too long to RENEW. */
1852 dprintk("nfsd4_renew: clientid not found!\n");
1856 status
= nfserr_cb_path_down
;
1857 if (!list_empty(&clp
->cl_delegations
)
1858 && !atomic_read(&clp
->cl_callback
.cb_set
))
1862 nfs4_unlock_state();
1869 dprintk("NFSD: end of grace period\n");
1870 nfsd4_recdir_purge_old();
1875 nfs4_laundromat(void)
1877 struct nfs4_client
*clp
;
1878 struct nfs4_stateowner
*sop
;
1879 struct nfs4_delegation
*dp
;
1880 struct list_head
*pos
, *next
, reaplist
;
1881 time_t cutoff
= get_seconds() - NFSD_LEASE_TIME
;
1882 time_t t
, clientid_val
= NFSD_LEASE_TIME
;
1883 time_t u
, test_val
= NFSD_LEASE_TIME
;
1887 dprintk("NFSD: laundromat service - starting\n");
1890 list_for_each_safe(pos
, next
, &client_lru
) {
1891 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
1892 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
1893 t
= clp
->cl_time
- cutoff
;
1894 if (clientid_val
> t
)
1898 dprintk("NFSD: purging unused client (clientid %08x)\n",
1899 clp
->cl_clientid
.cl_id
);
1900 nfsd4_remove_clid_dir(clp
);
1903 INIT_LIST_HEAD(&reaplist
);
1904 spin_lock(&recall_lock
);
1905 list_for_each_safe(pos
, next
, &del_recall_lru
) {
1906 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
1907 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
1908 u
= dp
->dl_time
- cutoff
;
1913 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1915 list_move(&dp
->dl_recall_lru
, &reaplist
);
1917 spin_unlock(&recall_lock
);
1918 list_for_each_safe(pos
, next
, &reaplist
) {
1919 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
1920 list_del_init(&dp
->dl_recall_lru
);
1921 unhash_delegation(dp
);
1923 test_val
= NFSD_LEASE_TIME
;
1924 list_for_each_safe(pos
, next
, &close_lru
) {
1925 sop
= list_entry(pos
, struct nfs4_stateowner
, so_close_lru
);
1926 if (time_after((unsigned long)sop
->so_time
, (unsigned long)cutoff
)) {
1927 u
= sop
->so_time
- cutoff
;
1932 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1934 release_stateowner(sop
);
1936 if (clientid_val
< NFSD_LAUNDROMAT_MINTIMEOUT
)
1937 clientid_val
= NFSD_LAUNDROMAT_MINTIMEOUT
;
1938 nfs4_unlock_state();
1939 return clientid_val
;
1943 laundromat_main(struct work_struct
*not_used
)
1947 t
= nfs4_laundromat();
1948 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
1949 queue_delayed_work(laundry_wq
, &laundromat_work
, t
*HZ
);
1952 static struct nfs4_stateowner
*
1953 search_close_lru(u32 st_id
, int flags
)
1955 struct nfs4_stateowner
*local
= NULL
;
1957 if (flags
& CLOSE_STATE
) {
1958 list_for_each_entry(local
, &close_lru
, so_close_lru
) {
1959 if (local
->so_id
== st_id
)
1967 nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stateid
*stp
)
1969 return fhp
->fh_dentry
->d_inode
!= stp
->st_vfs_file
->f_path
.dentry
->d_inode
;
1973 STALE_STATEID(stateid_t
*stateid
)
1975 if (stateid
->si_boot
== boot_time
)
1977 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1978 stateid
->si_boot
, stateid
->si_stateownerid
, stateid
->si_fileid
,
1979 stateid
->si_generation
);
1984 access_permit_read(unsigned long access_bmap
)
1986 return test_bit(NFS4_SHARE_ACCESS_READ
, &access_bmap
) ||
1987 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
) ||
1988 test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
);
1992 access_permit_write(unsigned long access_bmap
)
1994 return test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
) ||
1995 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
);
1999 __be32
nfs4_check_openmode(struct nfs4_stateid
*stp
, int flags
)
2001 __be32 status
= nfserr_openmode
;
2003 if ((flags
& WR_STATE
) && (!access_permit_write(stp
->st_access_bmap
)))
2005 if ((flags
& RD_STATE
) && (!access_permit_read(stp
->st_access_bmap
)))
2012 static inline __be32
2013 check_special_stateids(svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
2015 /* Trying to call delegreturn with a special stateid? Yuch: */
2016 if (!(flags
& (RD_STATE
| WR_STATE
)))
2017 return nfserr_bad_stateid
;
2018 else if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
2020 else if (nfs4_in_grace()) {
2021 /* Answer in remaining cases depends on existance of
2022 * conflicting state; so we must wait out the grace period. */
2023 return nfserr_grace
;
2024 } else if (flags
& WR_STATE
)
2025 return nfs4_share_conflict(current_fh
,
2026 NFS4_SHARE_DENY_WRITE
);
2027 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2028 return nfs4_share_conflict(current_fh
,
2029 NFS4_SHARE_DENY_READ
);
2033 * Allow READ/WRITE during grace period on recovered state only for files
2034 * that are not able to provide mandatory locking.
2037 io_during_grace_disallowed(struct inode
*inode
, int flags
)
2039 return nfs4_in_grace() && (flags
& (RD_STATE
| WR_STATE
))
2040 && MANDATORY_LOCK(inode
);
2044 * Checks for stateid operations
2047 nfs4_preprocess_stateid_op(struct svc_fh
*current_fh
, stateid_t
*stateid
, int flags
, struct file
**filpp
)
2049 struct nfs4_stateid
*stp
= NULL
;
2050 struct nfs4_delegation
*dp
= NULL
;
2052 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2055 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2056 stateid
->si_boot
, stateid
->si_stateownerid
,
2057 stateid
->si_fileid
, stateid
->si_generation
);
2061 if (io_during_grace_disallowed(ino
, flags
))
2062 return nfserr_grace
;
2064 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
2065 return check_special_stateids(current_fh
, stateid
, flags
);
2068 status
= nfserr_stale_stateid
;
2069 if (STALE_STATEID(stateid
))
2073 status
= nfserr_bad_stateid
;
2074 if (!stateid
->si_fileid
) { /* delegation stateid */
2075 if(!(dp
= find_delegation_stateid(ino
, stateid
))) {
2076 dprintk("NFSD: delegation stateid not found\n");
2079 stidp
= &dp
->dl_stateid
;
2080 } else { /* open or lock stateid */
2081 if (!(stp
= find_stateid(stateid
, flags
))) {
2082 dprintk("NFSD: open or lock stateid not found\n");
2085 if ((flags
& CHECK_FH
) && nfs4_check_fh(current_fh
, stp
))
2087 if (!stp
->st_stateowner
->so_confirmed
)
2089 stidp
= &stp
->st_stateid
;
2091 if (stateid
->si_generation
> stidp
->si_generation
)
2095 status
= nfserr_old_stateid
;
2096 if (stateid
->si_generation
< stidp
->si_generation
)
2099 if ((status
= nfs4_check_openmode(stp
,flags
)))
2101 renew_client(stp
->st_stateowner
->so_client
);
2103 *filpp
= stp
->st_vfs_file
;
2105 if ((status
= nfs4_check_delegmode(dp
, flags
)))
2107 renew_client(dp
->dl_client
);
2108 if (flags
& DELEG_RET
)
2109 unhash_delegation(dp
);
2111 *filpp
= dp
->dl_vfs_file
;
2121 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
2122 RD_STATE
: WR_STATE
;
2126 * Checks for sequence id mutating operations.
2129 nfs4_preprocess_seqid_op(struct svc_fh
*current_fh
, u32 seqid
, stateid_t
*stateid
, int flags
, struct nfs4_stateowner
**sopp
, struct nfs4_stateid
**stpp
, struct nfsd4_lock
*lock
)
2131 struct nfs4_stateid
*stp
;
2132 struct nfs4_stateowner
*sop
;
2134 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2135 "stateid = (%08x/%08x/%08x/%08x)\n", seqid
,
2136 stateid
->si_boot
, stateid
->si_stateownerid
, stateid
->si_fileid
,
2137 stateid
->si_generation
);
2142 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
)) {
2143 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2144 return nfserr_bad_stateid
;
2147 if (STALE_STATEID(stateid
))
2148 return nfserr_stale_stateid
;
2150 * We return BAD_STATEID if filehandle doesn't match stateid,
2151 * the confirmed flag is incorrecly set, or the generation
2152 * number is incorrect.
2154 stp
= find_stateid(stateid
, flags
);
2157 * Also, we should make sure this isn't just the result of
2160 sop
= search_close_lru(stateid
->si_stateownerid
, flags
);
2162 return nfserr_bad_stateid
;
2168 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
2169 clientid_t
*lockclid
= &lock
->v
.new.clientid
;
2170 struct nfs4_client
*clp
= sop
->so_client
;
2174 lkflg
= setlkflg(lock
->lk_type
);
2176 if (lock
->lk_is_new
) {
2177 if (!sop
->so_is_open_owner
)
2178 return nfserr_bad_stateid
;
2179 if (!cmp_clid(&clp
->cl_clientid
, lockclid
))
2180 return nfserr_bad_stateid
;
2181 /* stp is the open stateid */
2182 status
= nfs4_check_openmode(stp
, lkflg
);
2186 /* stp is the lock stateid */
2187 status
= nfs4_check_openmode(stp
->st_openstp
, lkflg
);
2194 if ((flags
& CHECK_FH
) && nfs4_check_fh(current_fh
, stp
)) {
2195 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2196 return nfserr_bad_stateid
;
2200 *sopp
= sop
= stp
->st_stateowner
;
2203 * We now validate the seqid and stateid generation numbers.
2204 * For the moment, we ignore the possibility of
2205 * generation number wraparound.
2207 if (seqid
!= sop
->so_seqid
)
2210 if (sop
->so_confirmed
&& flags
& CONFIRM
) {
2211 printk("NFSD: preprocess_seqid_op: expected"
2212 " unconfirmed stateowner!\n");
2213 return nfserr_bad_stateid
;
2215 if (!sop
->so_confirmed
&& !(flags
& CONFIRM
)) {
2216 printk("NFSD: preprocess_seqid_op: stateowner not"
2217 " confirmed yet!\n");
2218 return nfserr_bad_stateid
;
2220 if (stateid
->si_generation
> stp
->st_stateid
.si_generation
) {
2221 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2222 return nfserr_bad_stateid
;
2225 if (stateid
->si_generation
< stp
->st_stateid
.si_generation
) {
2226 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2227 return nfserr_old_stateid
;
2229 renew_client(sop
->so_client
);
2233 if (seqid
== sop
->so_seqid
- 1) {
2234 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
2235 /* indicate replay to calling function */
2236 return nfserr_replay_me
;
2238 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2239 sop
->so_seqid
, seqid
);
2241 return nfserr_bad_seqid
;
2245 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2246 struct nfsd4_open_confirm
*oc
)
2249 struct nfs4_stateowner
*sop
;
2250 struct nfs4_stateid
*stp
;
2252 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2253 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
2254 cstate
->current_fh
.fh_dentry
->d_name
.name
);
2256 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
2262 if ((status
= nfs4_preprocess_seqid_op(&cstate
->current_fh
,
2263 oc
->oc_seqid
, &oc
->oc_req_stateid
,
2264 CHECK_FH
| CONFIRM
| OPEN_STATE
,
2265 &oc
->oc_stateowner
, &stp
, NULL
)))
2268 sop
= oc
->oc_stateowner
;
2269 sop
->so_confirmed
= 1;
2270 update_stateid(&stp
->st_stateid
);
2271 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2272 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2273 "stateid=(%08x/%08x/%08x/%08x)\n", oc
->oc_seqid
,
2274 stp
->st_stateid
.si_boot
,
2275 stp
->st_stateid
.si_stateownerid
,
2276 stp
->st_stateid
.si_fileid
,
2277 stp
->st_stateid
.si_generation
);
2279 nfsd4_create_clid_dir(sop
->so_client
);
2281 if (oc
->oc_stateowner
) {
2282 nfs4_get_stateowner(oc
->oc_stateowner
);
2283 cstate
->replay_owner
= oc
->oc_stateowner
;
2285 nfs4_unlock_state();
2291 * unset all bits in union bitmap (bmap) that
2292 * do not exist in share (from successful OPEN_DOWNGRADE)
2295 reset_union_bmap_access(unsigned long access
, unsigned long *bmap
)
2298 for (i
= 1; i
< 4; i
++) {
2299 if ((i
& access
) != i
)
2300 __clear_bit(i
, bmap
);
2305 reset_union_bmap_deny(unsigned long deny
, unsigned long *bmap
)
2308 for (i
= 0; i
< 4; i
++) {
2309 if ((i
& deny
) != i
)
2310 __clear_bit(i
, bmap
);
2315 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
2316 struct nfsd4_compound_state
*cstate
,
2317 struct nfsd4_open_downgrade
*od
)
2320 struct nfs4_stateid
*stp
;
2321 unsigned int share_access
;
2323 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2324 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
2325 cstate
->current_fh
.fh_dentry
->d_name
.name
);
2327 if (!access_valid(od
->od_share_access
)
2328 || !deny_valid(od
->od_share_deny
))
2329 return nfserr_inval
;
2332 if ((status
= nfs4_preprocess_seqid_op(&cstate
->current_fh
,
2335 CHECK_FH
| OPEN_STATE
,
2336 &od
->od_stateowner
, &stp
, NULL
)))
2339 status
= nfserr_inval
;
2340 if (!test_bit(od
->od_share_access
, &stp
->st_access_bmap
)) {
2341 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2342 stp
->st_access_bmap
, od
->od_share_access
);
2345 if (!test_bit(od
->od_share_deny
, &stp
->st_deny_bmap
)) {
2346 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2347 stp
->st_deny_bmap
, od
->od_share_deny
);
2350 set_access(&share_access
, stp
->st_access_bmap
);
2351 nfs4_file_downgrade(stp
->st_vfs_file
,
2352 share_access
& ~od
->od_share_access
);
2354 reset_union_bmap_access(od
->od_share_access
, &stp
->st_access_bmap
);
2355 reset_union_bmap_deny(od
->od_share_deny
, &stp
->st_deny_bmap
);
2357 update_stateid(&stp
->st_stateid
);
2358 memcpy(&od
->od_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2361 if (od
->od_stateowner
) {
2362 nfs4_get_stateowner(od
->od_stateowner
);
2363 cstate
->replay_owner
= od
->od_stateowner
;
2365 nfs4_unlock_state();
2370 * nfs4_unlock_state() called after encode
2373 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2374 struct nfsd4_close
*close
)
2377 struct nfs4_stateid
*stp
;
2379 dprintk("NFSD: nfsd4_close on file %.*s\n",
2380 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
2381 cstate
->current_fh
.fh_dentry
->d_name
.name
);
2384 /* check close_lru for replay */
2385 if ((status
= nfs4_preprocess_seqid_op(&cstate
->current_fh
,
2388 CHECK_FH
| OPEN_STATE
| CLOSE_STATE
,
2389 &close
->cl_stateowner
, &stp
, NULL
)))
2392 update_stateid(&stp
->st_stateid
);
2393 memcpy(&close
->cl_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2395 /* release_stateid() calls nfsd_close() if needed */
2396 release_stateid(stp
, OPEN_STATE
);
2398 /* place unused nfs4_stateowners on so_close_lru list to be
2399 * released by the laundromat service after the lease period
2400 * to enable us to handle CLOSE replay
2402 if (list_empty(&close
->cl_stateowner
->so_stateids
))
2403 move_to_close_lru(close
->cl_stateowner
);
2405 if (close
->cl_stateowner
) {
2406 nfs4_get_stateowner(close
->cl_stateowner
);
2407 cstate
->replay_owner
= close
->cl_stateowner
;
2409 nfs4_unlock_state();
2414 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2415 struct nfsd4_delegreturn
*dr
)
2419 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
2423 status
= nfs4_preprocess_stateid_op(&cstate
->current_fh
,
2424 &dr
->dr_stateid
, DELEG_RET
, NULL
);
2425 nfs4_unlock_state();
2432 * Lock owner state (byte-range locks)
2434 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2435 #define LOCK_HASH_BITS 8
2436 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2437 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2439 #define lockownerid_hashval(id) \
2440 ((id) & LOCK_HASH_MASK)
2442 static inline unsigned int
2443 lock_ownerstr_hashval(struct inode
*inode
, u32 cl_id
,
2444 struct xdr_netobj
*ownername
)
2446 return (file_hashval(inode
) + cl_id
2447 + opaque_hashval(ownername
->data
, ownername
->len
))
2451 static struct list_head lock_ownerid_hashtbl
[LOCK_HASH_SIZE
];
2452 static struct list_head lock_ownerstr_hashtbl
[LOCK_HASH_SIZE
];
2453 static struct list_head lockstateid_hashtbl
[STATEID_HASH_SIZE
];
2455 static struct nfs4_stateid
*
2456 find_stateid(stateid_t
*stid
, int flags
)
2458 struct nfs4_stateid
*local
= NULL
;
2459 u32 st_id
= stid
->si_stateownerid
;
2460 u32 f_id
= stid
->si_fileid
;
2461 unsigned int hashval
;
2463 dprintk("NFSD: find_stateid flags 0x%x\n",flags
);
2464 if ((flags
& LOCK_STATE
) || (flags
& RD_STATE
) || (flags
& WR_STATE
)) {
2465 hashval
= stateid_hashval(st_id
, f_id
);
2466 list_for_each_entry(local
, &lockstateid_hashtbl
[hashval
], st_hash
) {
2467 if ((local
->st_stateid
.si_stateownerid
== st_id
) &&
2468 (local
->st_stateid
.si_fileid
== f_id
))
2472 if ((flags
& OPEN_STATE
) || (flags
& RD_STATE
) || (flags
& WR_STATE
)) {
2473 hashval
= stateid_hashval(st_id
, f_id
);
2474 list_for_each_entry(local
, &stateid_hashtbl
[hashval
], st_hash
) {
2475 if ((local
->st_stateid
.si_stateownerid
== st_id
) &&
2476 (local
->st_stateid
.si_fileid
== f_id
))
2483 static struct nfs4_delegation
*
2484 find_delegation_stateid(struct inode
*ino
, stateid_t
*stid
)
2486 struct nfs4_file
*fp
;
2487 struct nfs4_delegation
*dl
;
2489 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2490 stid
->si_boot
, stid
->si_stateownerid
,
2491 stid
->si_fileid
, stid
->si_generation
);
2493 fp
= find_file(ino
);
2496 dl
= find_delegation_file(fp
, stid
);
2502 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2503 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2504 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2505 * locking, this prevents us from being completely protocol-compliant. The
2506 * real solution to this problem is to start using unsigned file offsets in
2507 * the VFS, but this is a very deep change!
2510 nfs4_transform_lock_offset(struct file_lock
*lock
)
2512 if (lock
->fl_start
< 0)
2513 lock
->fl_start
= OFFSET_MAX
;
2514 if (lock
->fl_end
< 0)
2515 lock
->fl_end
= OFFSET_MAX
;
2518 /* Hack!: For now, we're defining this just so we can use a pointer to it
2519 * as a unique cookie to identify our (NFSv4's) posix locks. */
2520 static struct lock_manager_operations nfsd_posix_mng_ops
= {
2524 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
2526 struct nfs4_stateowner
*sop
;
2529 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
2530 sop
= (struct nfs4_stateowner
*) fl
->fl_owner
;
2531 hval
= lockownerid_hashval(sop
->so_id
);
2532 kref_get(&sop
->so_ref
);
2534 deny
->ld_clientid
= sop
->so_client
->cl_clientid
;
2536 deny
->ld_sop
= NULL
;
2537 deny
->ld_clientid
.cl_boot
= 0;
2538 deny
->ld_clientid
.cl_id
= 0;
2540 deny
->ld_start
= fl
->fl_start
;
2541 deny
->ld_length
= ~(u64
)0;
2542 if (fl
->fl_end
!= ~(u64
)0)
2543 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
2544 deny
->ld_type
= NFS4_READ_LT
;
2545 if (fl
->fl_type
!= F_RDLCK
)
2546 deny
->ld_type
= NFS4_WRITE_LT
;
2549 static struct nfs4_stateowner
*
2550 find_lockstateowner_str(struct inode
*inode
, clientid_t
*clid
,
2551 struct xdr_netobj
*owner
)
2553 unsigned int hashval
= lock_ownerstr_hashval(inode
, clid
->cl_id
, owner
);
2554 struct nfs4_stateowner
*op
;
2556 list_for_each_entry(op
, &lock_ownerstr_hashtbl
[hashval
], so_strhash
) {
2557 if (cmp_owner_str(op
, owner
, clid
))
2564 * Alloc a lock owner structure.
2565 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2568 * strhashval = lock_ownerstr_hashval
2571 static struct nfs4_stateowner
*
2572 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
2573 struct nfs4_stateowner
*sop
;
2574 struct nfs4_replay
*rp
;
2575 unsigned int idhashval
;
2577 if (!(sop
= alloc_stateowner(&lock
->lk_new_owner
)))
2579 idhashval
= lockownerid_hashval(current_ownerid
);
2580 INIT_LIST_HEAD(&sop
->so_idhash
);
2581 INIT_LIST_HEAD(&sop
->so_strhash
);
2582 INIT_LIST_HEAD(&sop
->so_perclient
);
2583 INIT_LIST_HEAD(&sop
->so_stateids
);
2584 INIT_LIST_HEAD(&sop
->so_perstateid
);
2585 INIT_LIST_HEAD(&sop
->so_close_lru
); /* not used */
2587 list_add(&sop
->so_idhash
, &lock_ownerid_hashtbl
[idhashval
]);
2588 list_add(&sop
->so_strhash
, &lock_ownerstr_hashtbl
[strhashval
]);
2589 list_add(&sop
->so_perstateid
, &open_stp
->st_lockowners
);
2590 sop
->so_is_open_owner
= 0;
2591 sop
->so_id
= current_ownerid
++;
2592 sop
->so_client
= clp
;
2593 /* It is the openowner seqid that will be incremented in encode in the
2594 * case of new lockowners; so increment the lock seqid manually: */
2595 sop
->so_seqid
= lock
->lk_new_lock_seqid
+ 1;
2596 sop
->so_confirmed
= 1;
2597 rp
= &sop
->so_replay
;
2598 rp
->rp_status
= nfserr_serverfault
;
2600 rp
->rp_buf
= rp
->rp_ibuf
;
2604 static struct nfs4_stateid
*
2605 alloc_init_lock_stateid(struct nfs4_stateowner
*sop
, struct nfs4_file
*fp
, struct nfs4_stateid
*open_stp
)
2607 struct nfs4_stateid
*stp
;
2608 unsigned int hashval
= stateid_hashval(sop
->so_id
, fp
->fi_id
);
2610 stp
= nfs4_alloc_stateid();
2613 INIT_LIST_HEAD(&stp
->st_hash
);
2614 INIT_LIST_HEAD(&stp
->st_perfile
);
2615 INIT_LIST_HEAD(&stp
->st_perstateowner
);
2616 INIT_LIST_HEAD(&stp
->st_lockowners
); /* not used */
2617 list_add(&stp
->st_hash
, &lockstateid_hashtbl
[hashval
]);
2618 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2619 list_add(&stp
->st_perstateowner
, &sop
->so_stateids
);
2620 stp
->st_stateowner
= sop
;
2623 stp
->st_stateid
.si_boot
= boot_time
;
2624 stp
->st_stateid
.si_stateownerid
= sop
->so_id
;
2625 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
2626 stp
->st_stateid
.si_generation
= 0;
2627 stp
->st_vfs_file
= open_stp
->st_vfs_file
; /* FIXME refcount?? */
2628 stp
->st_access_bmap
= open_stp
->st_access_bmap
;
2629 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
2630 stp
->st_openstp
= open_stp
;
2637 check_lock_length(u64 offset
, u64 length
)
2639 return ((length
== 0) || ((length
!= ~(u64
)0) &&
2640 LOFF_OVERFLOW(offset
, length
)));
2647 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2648 struct nfsd4_lock
*lock
)
2650 struct nfs4_stateowner
*open_sop
= NULL
;
2651 struct nfs4_stateowner
*lock_sop
= NULL
;
2652 struct nfs4_stateid
*lock_stp
;
2654 struct file_lock file_lock
;
2655 struct file_lock conflock
;
2657 unsigned int strhashval
;
2661 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2662 (long long) lock
->lk_offset
,
2663 (long long) lock
->lk_length
);
2665 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
2666 return nfserr_inval
;
2668 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
2669 S_IFREG
, MAY_LOCK
))) {
2670 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2676 if (lock
->lk_is_new
) {
2678 * Client indicates that this is a new lockowner.
2679 * Use open owner and open stateid to create lock owner and
2682 struct nfs4_stateid
*open_stp
= NULL
;
2683 struct nfs4_file
*fp
;
2685 status
= nfserr_stale_clientid
;
2686 if (STALE_CLIENTID(&lock
->lk_new_clientid
))
2689 /* validate and update open stateid and open seqid */
2690 status
= nfs4_preprocess_seqid_op(&cstate
->current_fh
,
2691 lock
->lk_new_open_seqid
,
2692 &lock
->lk_new_open_stateid
,
2693 CHECK_FH
| OPEN_STATE
,
2694 &lock
->lk_replay_owner
, &open_stp
,
2698 open_sop
= lock
->lk_replay_owner
;
2699 /* create lockowner and lock stateid */
2700 fp
= open_stp
->st_file
;
2701 strhashval
= lock_ownerstr_hashval(fp
->fi_inode
,
2702 open_sop
->so_client
->cl_clientid
.cl_id
,
2703 &lock
->v
.new.owner
);
2704 /* XXX: Do we need to check for duplicate stateowners on
2705 * the same file, or should they just be allowed (and
2706 * create new stateids)? */
2707 status
= nfserr_resource
;
2708 lock_sop
= alloc_init_lock_stateowner(strhashval
,
2709 open_sop
->so_client
, open_stp
, lock
);
2710 if (lock_sop
== NULL
)
2712 lock_stp
= alloc_init_lock_stateid(lock_sop
, fp
, open_stp
);
2713 if (lock_stp
== NULL
)
2716 /* lock (lock owner + lock stateid) already exists */
2717 status
= nfs4_preprocess_seqid_op(&cstate
->current_fh
,
2718 lock
->lk_old_lock_seqid
,
2719 &lock
->lk_old_lock_stateid
,
2720 CHECK_FH
| LOCK_STATE
,
2721 &lock
->lk_replay_owner
, &lock_stp
, lock
);
2724 lock_sop
= lock
->lk_replay_owner
;
2726 /* lock->lk_replay_owner and lock_stp have been created or found */
2727 filp
= lock_stp
->st_vfs_file
;
2729 status
= nfserr_grace
;
2730 if (nfs4_in_grace() && !lock
->lk_reclaim
)
2732 status
= nfserr_no_grace
;
2733 if (!nfs4_in_grace() && lock
->lk_reclaim
)
2736 locks_init_lock(&file_lock
);
2737 switch (lock
->lk_type
) {
2740 file_lock
.fl_type
= F_RDLCK
;
2744 case NFS4_WRITEW_LT
:
2745 file_lock
.fl_type
= F_WRLCK
;
2749 status
= nfserr_inval
;
2752 file_lock
.fl_owner
= (fl_owner_t
)lock_sop
;
2753 file_lock
.fl_pid
= current
->tgid
;
2754 file_lock
.fl_file
= filp
;
2755 file_lock
.fl_flags
= FL_POSIX
;
2756 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
2758 file_lock
.fl_start
= lock
->lk_offset
;
2759 if ((lock
->lk_length
== ~(u64
)0) ||
2760 LOFF_OVERFLOW(lock
->lk_offset
, lock
->lk_length
))
2761 file_lock
.fl_end
= ~(u64
)0;
2763 file_lock
.fl_end
= lock
->lk_offset
+ lock
->lk_length
- 1;
2764 nfs4_transform_lock_offset(&file_lock
);
2767 * Try to lock the file in the VFS.
2768 * Note: locks.c uses the BKL to protect the inode's lock list.
2771 /* XXX?: Just to divert the locks_release_private at the start of
2772 * locks_copy_lock: */
2773 locks_init_lock(&conflock
);
2774 err
= vfs_lock_file(filp
, cmd
, &file_lock
, &conflock
);
2776 case 0: /* success! */
2777 update_stateid(&lock_stp
->st_stateid
);
2778 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stateid
,
2782 case (EAGAIN
): /* conflock holds conflicting lock */
2783 status
= nfserr_denied
;
2784 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2785 nfs4_set_lock_denied(&conflock
, &lock
->lk_denied
);
2788 status
= nfserr_deadlock
;
2791 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
2792 status
= nfserr_resource
;
2796 if (status
&& lock
->lk_is_new
&& lock_sop
)
2797 release_stateowner(lock_sop
);
2798 if (lock
->lk_replay_owner
) {
2799 nfs4_get_stateowner(lock
->lk_replay_owner
);
2800 cstate
->replay_owner
= lock
->lk_replay_owner
;
2802 nfs4_unlock_state();
2810 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2811 struct nfsd4_lockt
*lockt
)
2813 struct inode
*inode
;
2815 struct file_lock file_lock
;
2819 if (nfs4_in_grace())
2820 return nfserr_grace
;
2822 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
2823 return nfserr_inval
;
2825 lockt
->lt_stateowner
= NULL
;
2828 status
= nfserr_stale_clientid
;
2829 if (STALE_CLIENTID(&lockt
->lt_clientid
))
2832 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0))) {
2833 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2834 if (status
== nfserr_symlink
)
2835 status
= nfserr_inval
;
2839 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
2840 locks_init_lock(&file_lock
);
2841 switch (lockt
->lt_type
) {
2844 file_lock
.fl_type
= F_RDLCK
;
2847 case NFS4_WRITEW_LT
:
2848 file_lock
.fl_type
= F_WRLCK
;
2851 printk("NFSD: nfs4_lockt: bad lock type!\n");
2852 status
= nfserr_inval
;
2856 lockt
->lt_stateowner
= find_lockstateowner_str(inode
,
2857 &lockt
->lt_clientid
, &lockt
->lt_owner
);
2858 if (lockt
->lt_stateowner
)
2859 file_lock
.fl_owner
= (fl_owner_t
)lockt
->lt_stateowner
;
2860 file_lock
.fl_pid
= current
->tgid
;
2861 file_lock
.fl_flags
= FL_POSIX
;
2862 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
2864 file_lock
.fl_start
= lockt
->lt_offset
;
2865 if ((lockt
->lt_length
== ~(u64
)0) || LOFF_OVERFLOW(lockt
->lt_offset
, lockt
->lt_length
))
2866 file_lock
.fl_end
= ~(u64
)0;
2868 file_lock
.fl_end
= lockt
->lt_offset
+ lockt
->lt_length
- 1;
2870 nfs4_transform_lock_offset(&file_lock
);
2872 /* vfs_test_lock uses the struct file _only_ to resolve the inode.
2873 * since LOCKT doesn't require an OPEN, and therefore a struct
2874 * file may not exist, pass vfs_test_lock a struct file with
2875 * only the dentry:inode set.
2877 memset(&file
, 0, sizeof (struct file
));
2878 file
.f_path
.dentry
= cstate
->current_fh
.fh_dentry
;
2881 error
= vfs_test_lock(&file
, &file_lock
);
2883 status
= nfserrno(error
);
2886 if (file_lock
.fl_type
!= F_UNLCK
) {
2887 status
= nfserr_denied
;
2888 nfs4_set_lock_denied(&file_lock
, &lockt
->lt_denied
);
2891 nfs4_unlock_state();
2896 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2897 struct nfsd4_locku
*locku
)
2899 struct nfs4_stateid
*stp
;
2900 struct file
*filp
= NULL
;
2901 struct file_lock file_lock
;
2905 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2906 (long long) locku
->lu_offset
,
2907 (long long) locku
->lu_length
);
2909 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
2910 return nfserr_inval
;
2914 if ((status
= nfs4_preprocess_seqid_op(&cstate
->current_fh
,
2917 CHECK_FH
| LOCK_STATE
,
2918 &locku
->lu_stateowner
, &stp
, NULL
)))
2921 filp
= stp
->st_vfs_file
;
2923 locks_init_lock(&file_lock
);
2924 file_lock
.fl_type
= F_UNLCK
;
2925 file_lock
.fl_owner
= (fl_owner_t
) locku
->lu_stateowner
;
2926 file_lock
.fl_pid
= current
->tgid
;
2927 file_lock
.fl_file
= filp
;
2928 file_lock
.fl_flags
= FL_POSIX
;
2929 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
2930 file_lock
.fl_start
= locku
->lu_offset
;
2932 if ((locku
->lu_length
== ~(u64
)0) || LOFF_OVERFLOW(locku
->lu_offset
, locku
->lu_length
))
2933 file_lock
.fl_end
= ~(u64
)0;
2935 file_lock
.fl_end
= locku
->lu_offset
+ locku
->lu_length
- 1;
2936 nfs4_transform_lock_offset(&file_lock
);
2939 * Try to unlock the file in the VFS.
2941 err
= vfs_lock_file(filp
, F_SETLK
, &file_lock
, NULL
);
2943 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
2947 * OK, unlock succeeded; the only thing left to do is update the stateid.
2949 update_stateid(&stp
->st_stateid
);
2950 memcpy(&locku
->lu_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2953 if (locku
->lu_stateowner
) {
2954 nfs4_get_stateowner(locku
->lu_stateowner
);
2955 cstate
->replay_owner
= locku
->lu_stateowner
;
2957 nfs4_unlock_state();
2961 status
= nfserrno(err
);
2967 * 1: locks held by lockowner
2968 * 0: no locks held by lockowner
2971 check_for_locks(struct file
*filp
, struct nfs4_stateowner
*lowner
)
2973 struct file_lock
**flpp
;
2974 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2978 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
2979 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
2990 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
2991 struct nfsd4_compound_state
*cstate
,
2992 struct nfsd4_release_lockowner
*rlockowner
)
2994 clientid_t
*clid
= &rlockowner
->rl_clientid
;
2995 struct nfs4_stateowner
*sop
;
2996 struct nfs4_stateid
*stp
;
2997 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
2998 struct list_head matches
;
3002 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3003 clid
->cl_boot
, clid
->cl_id
);
3005 /* XXX check for lease expiration */
3007 status
= nfserr_stale_clientid
;
3008 if (STALE_CLIENTID(clid
))
3013 status
= nfserr_locks_held
;
3014 /* XXX: we're doing a linear search through all the lockowners.
3015 * Yipes! For now we'll just hope clients aren't really using
3016 * release_lockowner much, but eventually we have to fix these
3017 * data structures. */
3018 INIT_LIST_HEAD(&matches
);
3019 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
3020 list_for_each_entry(sop
, &lock_ownerid_hashtbl
[i
], so_idhash
) {
3021 if (!cmp_owner_str(sop
, owner
, clid
))
3023 list_for_each_entry(stp
, &sop
->so_stateids
,
3025 if (check_for_locks(stp
->st_vfs_file
, sop
))
3027 /* Note: so_perclient unused for lockowners,
3028 * so it's OK to fool with here. */
3029 list_add(&sop
->so_perclient
, &matches
);
3033 /* Clients probably won't expect us to return with some (but not all)
3034 * of the lockowner state released; so don't release any until all
3035 * have been checked. */
3037 while (!list_empty(&matches
)) {
3038 sop
= list_entry(matches
.next
, struct nfs4_stateowner
,
3040 /* unhash_stateowner deletes so_perclient only
3041 * for openowners. */
3042 list_del(&sop
->so_perclient
);
3043 release_stateowner(sop
);
3046 nfs4_unlock_state();
3050 static inline struct nfs4_client_reclaim
*
3053 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
3057 nfs4_has_reclaimed_state(const char *name
)
3059 unsigned int strhashval
= clientstr_hashval(name
);
3060 struct nfs4_client
*clp
;
3062 clp
= find_confirmed_client_by_str(name
, strhashval
);
3067 * failure => all reset bets are off, nfserr_no_grace...
3070 nfs4_client_to_reclaim(const char *name
)
3072 unsigned int strhashval
;
3073 struct nfs4_client_reclaim
*crp
= NULL
;
3075 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
3076 crp
= alloc_reclaim();
3079 strhashval
= clientstr_hashval(name
);
3080 INIT_LIST_HEAD(&crp
->cr_strhash
);
3081 list_add(&crp
->cr_strhash
, &reclaim_str_hashtbl
[strhashval
]);
3082 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
3083 reclaim_str_hashtbl_size
++;
3088 nfs4_release_reclaim(void)
3090 struct nfs4_client_reclaim
*crp
= NULL
;
3093 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
3094 while (!list_empty(&reclaim_str_hashtbl
[i
])) {
3095 crp
= list_entry(reclaim_str_hashtbl
[i
].next
,
3096 struct nfs4_client_reclaim
, cr_strhash
);
3097 list_del(&crp
->cr_strhash
);
3099 reclaim_str_hashtbl_size
--;
3102 BUG_ON(reclaim_str_hashtbl_size
);
3106 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3107 static struct nfs4_client_reclaim
*
3108 nfs4_find_reclaim_client(clientid_t
*clid
)
3110 unsigned int strhashval
;
3111 struct nfs4_client
*clp
;
3112 struct nfs4_client_reclaim
*crp
= NULL
;
3115 /* find clientid in conf_id_hashtbl */
3116 clp
= find_confirmed_client(clid
);
3120 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3121 clp
->cl_name
.len
, clp
->cl_name
.data
,
3124 /* find clp->cl_name in reclaim_str_hashtbl */
3125 strhashval
= clientstr_hashval(clp
->cl_recdir
);
3126 list_for_each_entry(crp
, &reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
3127 if (same_name(crp
->cr_recdir
, clp
->cl_recdir
)) {
3135 * Called from OPEN. Look for clientid in reclaim list.
3138 nfs4_check_open_reclaim(clientid_t
*clid
)
3140 return nfs4_find_reclaim_client(clid
) ? nfs_ok
: nfserr_reclaim_bad
;
3143 /* initialization to perform at module load time: */
3146 nfs4_state_init(void)
3150 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
3151 INIT_LIST_HEAD(&conf_id_hashtbl
[i
]);
3152 INIT_LIST_HEAD(&conf_str_hashtbl
[i
]);
3153 INIT_LIST_HEAD(&unconf_str_hashtbl
[i
]);
3154 INIT_LIST_HEAD(&unconf_id_hashtbl
[i
]);
3156 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
3157 INIT_LIST_HEAD(&file_hashtbl
[i
]);
3159 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++) {
3160 INIT_LIST_HEAD(&ownerstr_hashtbl
[i
]);
3161 INIT_LIST_HEAD(&ownerid_hashtbl
[i
]);
3163 for (i
= 0; i
< STATEID_HASH_SIZE
; i
++) {
3164 INIT_LIST_HEAD(&stateid_hashtbl
[i
]);
3165 INIT_LIST_HEAD(&lockstateid_hashtbl
[i
]);
3167 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
3168 INIT_LIST_HEAD(&lock_ownerid_hashtbl
[i
]);
3169 INIT_LIST_HEAD(&lock_ownerstr_hashtbl
[i
]);
3171 memset(&onestateid
, ~0, sizeof(stateid_t
));
3172 INIT_LIST_HEAD(&close_lru
);
3173 INIT_LIST_HEAD(&client_lru
);
3174 INIT_LIST_HEAD(&del_recall_lru
);
3175 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++)
3176 INIT_LIST_HEAD(&reclaim_str_hashtbl
[i
]);
3177 reclaim_str_hashtbl_size
= 0;
3181 nfsd4_load_reboot_recovery_data(void)
3186 nfsd4_init_recdir(user_recovery_dirname
);
3187 status
= nfsd4_recdir_load();
3188 nfs4_unlock_state();
3190 printk("NFSD: Failure reading reboot recovery data\n");
3193 /* initialization to perform when the nfsd service is started: */
3196 __nfs4_state_start(void)
3200 boot_time
= get_seconds();
3201 grace_time
= max(user_lease_time
, lease_time
);
3202 lease_time
= user_lease_time
;
3204 printk("NFSD: starting %ld-second grace period\n", grace_time
);
3205 laundry_wq
= create_singlethread_workqueue("nfsd4");
3206 queue_delayed_work(laundry_wq
, &laundromat_work
, grace_time
*HZ
);
3210 nfs4_state_start(void)
3216 status
= nfsd4_init_slabs();
3219 nfsd4_load_reboot_recovery_data();
3220 __nfs4_state_start();
3232 nfs4_lease_time(void)
3238 __nfs4_state_shutdown(void)
3241 struct nfs4_client
*clp
= NULL
;
3242 struct nfs4_delegation
*dp
= NULL
;
3243 struct list_head
*pos
, *next
, reaplist
;
3245 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
3246 while (!list_empty(&conf_id_hashtbl
[i
])) {
3247 clp
= list_entry(conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
3250 while (!list_empty(&unconf_str_hashtbl
[i
])) {
3251 clp
= list_entry(unconf_str_hashtbl
[i
].next
, struct nfs4_client
, cl_strhash
);
3255 INIT_LIST_HEAD(&reaplist
);
3256 spin_lock(&recall_lock
);
3257 list_for_each_safe(pos
, next
, &del_recall_lru
) {
3258 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3259 list_move(&dp
->dl_recall_lru
, &reaplist
);
3261 spin_unlock(&recall_lock
);
3262 list_for_each_safe(pos
, next
, &reaplist
) {
3263 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3264 list_del_init(&dp
->dl_recall_lru
);
3265 unhash_delegation(dp
);
3268 nfsd4_shutdown_recdir();
3273 nfs4_state_shutdown(void)
3275 cancel_rearming_delayed_workqueue(laundry_wq
, &laundromat_work
);
3276 destroy_workqueue(laundry_wq
);
3278 nfs4_release_reclaim();
3279 __nfs4_state_shutdown();
3281 nfs4_unlock_state();
3285 nfs4_set_recdir(char *recdir
)
3288 strcpy(user_recovery_dirname
, recdir
);
3289 nfs4_unlock_state();
3293 * Change the NFSv4 recovery directory to recdir.
3296 nfs4_reset_recoverydir(char *recdir
)
3299 struct nameidata nd
;
3301 status
= path_lookup(recdir
, LOOKUP_FOLLOW
, &nd
);
3305 if (S_ISDIR(nd
.dentry
->d_inode
->i_mode
)) {
3306 nfs4_set_recdir(recdir
);
3314 * Called when leasetime is changed.
3316 * The only way the protocol gives us to handle on-the-fly lease changes is to
3317 * simulate a reboot. Instead of doing that, we just wait till the next time
3318 * we start to register any changes in lease time. If the administrator
3319 * really wants to change the lease time *now*, they can go ahead and bring
3320 * nfsd down and then back up again after changing the lease time.
3323 nfs4_reset_lease(time_t leasetime
)
3326 user_lease_time
= leasetime
;