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>
54 #define NFSDDBG_FACILITY NFSDDBG_PROC
57 static time_t lease_time
= 90; /* default lease time */
58 static time_t user_lease_time
= 90;
59 static time_t boot_time
;
60 static int in_grace
= 1;
61 static u32 current_clientid
= 1;
62 static u32 current_ownerid
= 1;
63 static u32 current_fileid
= 1;
64 static u32 current_delegid
= 1;
66 static stateid_t zerostateid
; /* bits all 0 */
67 static stateid_t onestateid
; /* bits all 1 */
69 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
70 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
72 /* forward declarations */
73 static struct nfs4_stateid
* find_stateid(stateid_t
*stid
, int flags
);
74 static struct nfs4_delegation
* find_delegation_stateid(struct inode
*ino
, stateid_t
*stid
);
75 static void release_stateid_lockowners(struct nfs4_stateid
*open_stp
);
76 static char user_recovery_dirname
[PATH_MAX
] = "/var/lib/nfs/v4recovery";
77 static void nfs4_set_recdir(char *recdir
);
82 * protects clientid_hashtbl[], clientstr_hashtbl[],
83 * unconfstr_hashtbl[], uncofid_hashtbl[].
85 static DEFINE_MUTEX(client_mutex
);
87 static kmem_cache_t
*stateowner_slab
= NULL
;
88 static kmem_cache_t
*file_slab
= NULL
;
89 static kmem_cache_t
*stateid_slab
= NULL
;
90 static kmem_cache_t
*deleg_slab
= NULL
;
95 mutex_lock(&client_mutex
);
99 nfs4_unlock_state(void)
101 mutex_unlock(&client_mutex
);
105 opaque_hashval(const void *ptr
, int nbytes
)
107 unsigned char *cptr
= (unsigned char *) ptr
;
117 /* forward declarations */
118 static void release_stateowner(struct nfs4_stateowner
*sop
);
119 static void release_stateid(struct nfs4_stateid
*stp
, int flags
);
125 /* recall_lock protects the del_recall_lru */
126 static DEFINE_SPINLOCK(recall_lock
);
127 static struct list_head del_recall_lru
;
130 free_nfs4_file(struct kref
*kref
)
132 struct nfs4_file
*fp
= container_of(kref
, struct nfs4_file
, fi_ref
);
133 list_del(&fp
->fi_hash
);
135 kmem_cache_free(file_slab
, fp
);
139 put_nfs4_file(struct nfs4_file
*fi
)
141 kref_put(&fi
->fi_ref
, free_nfs4_file
);
145 get_nfs4_file(struct nfs4_file
*fi
)
147 kref_get(&fi
->fi_ref
);
150 static int num_delegations
;
153 * Open owner state (share locks)
156 /* hash tables for nfs4_stateowner */
157 #define OWNER_HASH_BITS 8
158 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
159 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
161 #define ownerid_hashval(id) \
162 ((id) & OWNER_HASH_MASK)
163 #define ownerstr_hashval(clientid, ownername) \
164 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
166 static struct list_head ownerid_hashtbl
[OWNER_HASH_SIZE
];
167 static struct list_head ownerstr_hashtbl
[OWNER_HASH_SIZE
];
169 /* hash table for nfs4_file */
170 #define FILE_HASH_BITS 8
171 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
172 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
173 /* hash table for (open)nfs4_stateid */
174 #define STATEID_HASH_BITS 10
175 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
176 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
178 #define file_hashval(x) \
179 hash_ptr(x, FILE_HASH_BITS)
180 #define stateid_hashval(owner_id, file_id) \
181 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
183 static struct list_head file_hashtbl
[FILE_HASH_SIZE
];
184 static struct list_head stateid_hashtbl
[STATEID_HASH_SIZE
];
186 static struct nfs4_delegation
*
187 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_stateid
*stp
, struct svc_fh
*current_fh
, u32 type
)
189 struct nfs4_delegation
*dp
;
190 struct nfs4_file
*fp
= stp
->st_file
;
191 struct nfs4_callback
*cb
= &stp
->st_stateowner
->so_client
->cl_callback
;
193 dprintk("NFSD alloc_init_deleg\n");
194 if (num_delegations
> STATEID_HASH_SIZE
* 4)
196 dp
= kmem_cache_alloc(deleg_slab
, GFP_KERNEL
);
200 INIT_LIST_HEAD(&dp
->dl_perfile
);
201 INIT_LIST_HEAD(&dp
->dl_perclnt
);
202 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
207 get_file(stp
->st_vfs_file
);
208 dp
->dl_vfs_file
= stp
->st_vfs_file
;
210 dp
->dl_recall
.cbr_dp
= NULL
;
211 dp
->dl_recall
.cbr_ident
= cb
->cb_ident
;
212 dp
->dl_recall
.cbr_trunc
= 0;
213 dp
->dl_stateid
.si_boot
= boot_time
;
214 dp
->dl_stateid
.si_stateownerid
= current_delegid
++;
215 dp
->dl_stateid
.si_fileid
= 0;
216 dp
->dl_stateid
.si_generation
= 0;
217 dp
->dl_fhlen
= current_fh
->fh_handle
.fh_size
;
218 memcpy(dp
->dl_fhval
, ¤t_fh
->fh_handle
.fh_base
,
219 current_fh
->fh_handle
.fh_size
);
221 atomic_set(&dp
->dl_count
, 1);
222 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
223 list_add(&dp
->dl_perclnt
, &clp
->cl_delegations
);
228 nfs4_put_delegation(struct nfs4_delegation
*dp
)
230 if (atomic_dec_and_test(&dp
->dl_count
)) {
231 dprintk("NFSD: freeing dp %p\n",dp
);
232 put_nfs4_file(dp
->dl_file
);
233 kmem_cache_free(deleg_slab
, dp
);
238 /* Remove the associated file_lock first, then remove the delegation.
239 * lease_modify() is called to remove the FS_LEASE file_lock from
240 * the i_flock list, eventually calling nfsd's lock_manager
241 * fl_release_callback.
244 nfs4_close_delegation(struct nfs4_delegation
*dp
)
246 struct file
*filp
= dp
->dl_vfs_file
;
248 dprintk("NFSD: close_delegation dp %p\n",dp
);
249 dp
->dl_vfs_file
= NULL
;
250 /* The following nfsd_close may not actually close the file,
251 * but we want to remove the lease in any case. */
253 setlease(filp
, F_UNLCK
, &dp
->dl_flock
);
257 /* Called under the state lock. */
259 unhash_delegation(struct nfs4_delegation
*dp
)
261 list_del_init(&dp
->dl_perfile
);
262 list_del_init(&dp
->dl_perclnt
);
263 spin_lock(&recall_lock
);
264 list_del_init(&dp
->dl_recall_lru
);
265 spin_unlock(&recall_lock
);
266 nfs4_close_delegation(dp
);
267 nfs4_put_delegation(dp
);
274 /* Hash tables for nfs4_clientid state */
275 #define CLIENT_HASH_BITS 4
276 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
277 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
279 #define clientid_hashval(id) \
280 ((id) & CLIENT_HASH_MASK)
281 #define clientstr_hashval(name) \
282 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
284 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
285 * used in reboot/reset lease grace period processing
287 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
288 * setclientid_confirmed info.
290 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
293 * client_lru holds client queue ordered by nfs4_client.cl_time
296 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
297 * for last close replay.
299 static struct list_head reclaim_str_hashtbl
[CLIENT_HASH_SIZE
];
300 static int reclaim_str_hashtbl_size
= 0;
301 static struct list_head conf_id_hashtbl
[CLIENT_HASH_SIZE
];
302 static struct list_head conf_str_hashtbl
[CLIENT_HASH_SIZE
];
303 static struct list_head unconf_str_hashtbl
[CLIENT_HASH_SIZE
];
304 static struct list_head unconf_id_hashtbl
[CLIENT_HASH_SIZE
];
305 static struct list_head client_lru
;
306 static struct list_head close_lru
;
309 renew_client(struct nfs4_client
*clp
)
312 * Move client to the end to the LRU list.
314 dprintk("renewing client (clientid %08x/%08x)\n",
315 clp
->cl_clientid
.cl_boot
,
316 clp
->cl_clientid
.cl_id
);
317 list_move_tail(&clp
->cl_lru
, &client_lru
);
318 clp
->cl_time
= get_seconds();
321 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
323 STALE_CLIENTID(clientid_t
*clid
)
325 if (clid
->cl_boot
== boot_time
)
327 dprintk("NFSD stale clientid (%08x/%08x)\n",
328 clid
->cl_boot
, clid
->cl_id
);
333 * XXX Should we use a slab cache ?
334 * This type of memory management is somewhat inefficient, but we use it
335 * anyway since SETCLIENTID is not a common operation.
337 static inline struct nfs4_client
*
338 alloc_client(struct xdr_netobj name
)
340 struct nfs4_client
*clp
;
342 if ((clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
))!= NULL
) {
343 if ((clp
->cl_name
.data
= kmalloc(name
.len
, GFP_KERNEL
)) != NULL
) {
344 memcpy(clp
->cl_name
.data
, name
.data
, name
.len
);
345 clp
->cl_name
.len
= name
.len
;
356 free_client(struct nfs4_client
*clp
)
358 if (clp
->cl_cred
.cr_group_info
)
359 put_group_info(clp
->cl_cred
.cr_group_info
);
360 kfree(clp
->cl_name
.data
);
365 put_nfs4_client(struct nfs4_client
*clp
)
367 if (atomic_dec_and_test(&clp
->cl_count
))
372 shutdown_callback_client(struct nfs4_client
*clp
)
374 struct rpc_clnt
*clnt
= clp
->cl_callback
.cb_client
;
376 /* shutdown rpc client, ending any outstanding recall rpcs */
378 clp
->cl_callback
.cb_client
= NULL
;
379 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_setclientid
*setclid
)
716 u32 ip_addr
= rqstp
->rq_addr
.sin_addr
.s_addr
;
717 struct xdr_netobj clname
= {
718 .len
= setclid
->se_namelen
,
719 .data
= setclid
->se_name
,
721 nfs4_verifier clverifier
= setclid
->se_verf
;
722 unsigned int strhashval
;
723 struct nfs4_client
*conf
, *unconf
, *new;
725 char dname
[HEXDIR_LEN
];
727 if (!check_name(clname
))
730 status
= nfs4_make_rec_clidname(dname
, &clname
);
735 * XXX The Duplicate Request Cache (DRC) has been checked (??)
736 * We get here on a DRC miss.
739 strhashval
= clientstr_hashval(dname
);
742 conf
= find_confirmed_client_by_str(dname
, strhashval
);
746 * clname match, confirmed, different principal
747 * or different ip_address
749 status
= nfserr_clid_inuse
;
750 if (!cmp_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)
751 || conf
->cl_addr
!= ip_addr
) {
752 printk("NFSD: setclientid: string in use by client"
753 "(clientid %08x/%08x)\n",
754 conf
->cl_clientid
.cl_boot
, conf
->cl_clientid
.cl_id
);
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
= ip_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
= ip_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
= ip_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
= ip_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
, struct nfsd4_setclientid_confirm
*setclientid_confirm
)
881 u32 ip_addr
= rqstp
->rq_addr
.sin_addr
.s_addr
;
882 struct nfs4_client
*conf
, *unconf
;
883 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
884 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
887 if (STALE_CLIENTID(clid
))
888 return nfserr_stale_clientid
;
890 * XXX The Duplicate Request Cache (DRC) has been checked (??)
891 * We get here on a DRC miss.
896 conf
= find_confirmed_client(clid
);
897 unconf
= find_unconfirmed_client(clid
);
899 status
= nfserr_clid_inuse
;
900 if (conf
&& conf
->cl_addr
!= ip_addr
)
902 if (unconf
&& unconf
->cl_addr
!= ip_addr
)
905 if ((conf
&& unconf
) &&
906 (cmp_verf(&unconf
->cl_confirm
, &confirm
)) &&
907 (cmp_verf(&conf
->cl_verifier
, &unconf
->cl_verifier
)) &&
908 (same_name(conf
->cl_recdir
,unconf
->cl_recdir
)) &&
909 (!cmp_verf(&conf
->cl_confirm
, &unconf
->cl_confirm
))) {
911 * unconf record that matches input clientid and input confirm.
912 * conf record that matches input clientid.
913 * conf and unconf records match names, verifiers
915 if (!cmp_creds(&conf
->cl_cred
, &unconf
->cl_cred
))
916 status
= nfserr_clid_inuse
;
918 /* XXX: We just turn off callbacks until we can handle
919 * change request correctly. */
920 atomic_set(&conf
->cl_callback
.cb_set
, 0);
922 nfsd4_remove_clid_dir(unconf
);
923 expire_client(unconf
);
927 } else if ((conf
&& !unconf
) ||
929 (!cmp_verf(&conf
->cl_verifier
, &unconf
->cl_verifier
) ||
930 !same_name(conf
->cl_recdir
, unconf
->cl_recdir
)))) {
932 * conf record that matches input clientid.
933 * if unconf record matches input clientid, then
934 * unconf->cl_name or unconf->cl_verifier don't match the
937 if (!cmp_creds(&conf
->cl_cred
,&rqstp
->rq_cred
))
938 status
= nfserr_clid_inuse
;
941 } else if (!conf
&& unconf
942 && cmp_verf(&unconf
->cl_confirm
, &confirm
)) {
944 * conf record not found.
945 * unconf record found.
946 * unconf->cl_confirm matches input confirm
948 if (!cmp_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
)) {
949 status
= nfserr_clid_inuse
;
952 clientstr_hashval(unconf
->cl_recdir
);
953 conf
= find_confirmed_client_by_str(unconf
->cl_recdir
,
956 nfsd4_remove_clid_dir(conf
);
959 move_to_confirmed(unconf
);
963 } else if ((!conf
|| (conf
&& !cmp_verf(&conf
->cl_confirm
, &confirm
)))
964 && (!unconf
|| (unconf
&& !cmp_verf(&unconf
->cl_confirm
,
967 * conf record not found, or if conf, conf->cl_confirm does not
968 * match input confirm.
969 * unconf record not found, or if unconf, unconf->cl_confirm
970 * does not match input confirm.
972 status
= nfserr_stale_clientid
;
974 /* check that we have hit one of the cases...*/
975 status
= nfserr_clid_inuse
;
979 nfsd4_probe_callback(conf
);
984 /* OPEN Share state helper functions */
985 static inline struct nfs4_file
*
986 alloc_init_file(struct inode
*ino
)
988 struct nfs4_file
*fp
;
989 unsigned int hashval
= file_hashval(ino
);
991 fp
= kmem_cache_alloc(file_slab
, GFP_KERNEL
);
993 kref_init(&fp
->fi_ref
);
994 INIT_LIST_HEAD(&fp
->fi_hash
);
995 INIT_LIST_HEAD(&fp
->fi_stateids
);
996 INIT_LIST_HEAD(&fp
->fi_delegations
);
997 list_add(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
998 fp
->fi_inode
= igrab(ino
);
999 fp
->fi_id
= current_fileid
++;
1006 nfsd4_free_slab(kmem_cache_t
**slab
)
1012 status
= kmem_cache_destroy(*slab
);
1018 nfsd4_free_slabs(void)
1020 nfsd4_free_slab(&stateowner_slab
);
1021 nfsd4_free_slab(&file_slab
);
1022 nfsd4_free_slab(&stateid_slab
);
1023 nfsd4_free_slab(&deleg_slab
);
1027 nfsd4_init_slabs(void)
1029 stateowner_slab
= kmem_cache_create("nfsd4_stateowners",
1030 sizeof(struct nfs4_stateowner
), 0, 0, NULL
, NULL
);
1031 if (stateowner_slab
== NULL
)
1033 file_slab
= kmem_cache_create("nfsd4_files",
1034 sizeof(struct nfs4_file
), 0, 0, NULL
, NULL
);
1035 if (file_slab
== NULL
)
1037 stateid_slab
= kmem_cache_create("nfsd4_stateids",
1038 sizeof(struct nfs4_stateid
), 0, 0, NULL
, NULL
);
1039 if (stateid_slab
== NULL
)
1041 deleg_slab
= kmem_cache_create("nfsd4_delegations",
1042 sizeof(struct nfs4_delegation
), 0, 0, NULL
, NULL
);
1043 if (deleg_slab
== NULL
)
1048 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1053 nfs4_free_stateowner(struct kref
*kref
)
1055 struct nfs4_stateowner
*sop
=
1056 container_of(kref
, struct nfs4_stateowner
, so_ref
);
1057 kfree(sop
->so_owner
.data
);
1058 kmem_cache_free(stateowner_slab
, sop
);
1061 static inline struct nfs4_stateowner
*
1062 alloc_stateowner(struct xdr_netobj
*owner
)
1064 struct nfs4_stateowner
*sop
;
1066 if ((sop
= kmem_cache_alloc(stateowner_slab
, GFP_KERNEL
))) {
1067 if ((sop
->so_owner
.data
= kmalloc(owner
->len
, GFP_KERNEL
))) {
1068 memcpy(sop
->so_owner
.data
, owner
->data
, owner
->len
);
1069 sop
->so_owner
.len
= owner
->len
;
1070 kref_init(&sop
->so_ref
);
1073 kmem_cache_free(stateowner_slab
, sop
);
1078 static struct nfs4_stateowner
*
1079 alloc_init_open_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfsd4_open
*open
) {
1080 struct nfs4_stateowner
*sop
;
1081 struct nfs4_replay
*rp
;
1082 unsigned int idhashval
;
1084 if (!(sop
= alloc_stateowner(&open
->op_owner
)))
1086 idhashval
= ownerid_hashval(current_ownerid
);
1087 INIT_LIST_HEAD(&sop
->so_idhash
);
1088 INIT_LIST_HEAD(&sop
->so_strhash
);
1089 INIT_LIST_HEAD(&sop
->so_perclient
);
1090 INIT_LIST_HEAD(&sop
->so_stateids
);
1091 INIT_LIST_HEAD(&sop
->so_perstateid
); /* not used */
1092 INIT_LIST_HEAD(&sop
->so_close_lru
);
1094 list_add(&sop
->so_idhash
, &ownerid_hashtbl
[idhashval
]);
1095 list_add(&sop
->so_strhash
, &ownerstr_hashtbl
[strhashval
]);
1096 list_add(&sop
->so_perclient
, &clp
->cl_openowners
);
1097 sop
->so_is_open_owner
= 1;
1098 sop
->so_id
= current_ownerid
++;
1099 sop
->so_client
= clp
;
1100 sop
->so_seqid
= open
->op_seqid
;
1101 sop
->so_confirmed
= 0;
1102 rp
= &sop
->so_replay
;
1103 rp
->rp_status
= nfserr_serverfault
;
1105 rp
->rp_buf
= rp
->rp_ibuf
;
1110 release_stateid_lockowners(struct nfs4_stateid
*open_stp
)
1112 struct nfs4_stateowner
*lock_sop
;
1114 while (!list_empty(&open_stp
->st_lockowners
)) {
1115 lock_sop
= list_entry(open_stp
->st_lockowners
.next
,
1116 struct nfs4_stateowner
, so_perstateid
);
1117 /* list_del(&open_stp->st_lockowners); */
1118 BUG_ON(lock_sop
->so_is_open_owner
);
1119 release_stateowner(lock_sop
);
1124 unhash_stateowner(struct nfs4_stateowner
*sop
)
1126 struct nfs4_stateid
*stp
;
1128 list_del(&sop
->so_idhash
);
1129 list_del(&sop
->so_strhash
);
1130 if (sop
->so_is_open_owner
)
1131 list_del(&sop
->so_perclient
);
1132 list_del(&sop
->so_perstateid
);
1133 while (!list_empty(&sop
->so_stateids
)) {
1134 stp
= list_entry(sop
->so_stateids
.next
,
1135 struct nfs4_stateid
, st_perstateowner
);
1136 if (sop
->so_is_open_owner
)
1137 release_stateid(stp
, OPEN_STATE
);
1139 release_stateid(stp
, LOCK_STATE
);
1144 release_stateowner(struct nfs4_stateowner
*sop
)
1146 unhash_stateowner(sop
);
1147 list_del(&sop
->so_close_lru
);
1148 nfs4_put_stateowner(sop
);
1152 init_stateid(struct nfs4_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
1153 struct nfs4_stateowner
*sop
= open
->op_stateowner
;
1154 unsigned int hashval
= stateid_hashval(sop
->so_id
, fp
->fi_id
);
1156 INIT_LIST_HEAD(&stp
->st_hash
);
1157 INIT_LIST_HEAD(&stp
->st_perstateowner
);
1158 INIT_LIST_HEAD(&stp
->st_lockowners
);
1159 INIT_LIST_HEAD(&stp
->st_perfile
);
1160 list_add(&stp
->st_hash
, &stateid_hashtbl
[hashval
]);
1161 list_add(&stp
->st_perstateowner
, &sop
->so_stateids
);
1162 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
1163 stp
->st_stateowner
= sop
;
1166 stp
->st_stateid
.si_boot
= boot_time
;
1167 stp
->st_stateid
.si_stateownerid
= sop
->so_id
;
1168 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
1169 stp
->st_stateid
.si_generation
= 0;
1170 stp
->st_access_bmap
= 0;
1171 stp
->st_deny_bmap
= 0;
1172 __set_bit(open
->op_share_access
, &stp
->st_access_bmap
);
1173 __set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
1174 stp
->st_openstp
= NULL
;
1178 release_stateid(struct nfs4_stateid
*stp
, int flags
)
1180 struct file
*filp
= stp
->st_vfs_file
;
1182 list_del(&stp
->st_hash
);
1183 list_del(&stp
->st_perfile
);
1184 list_del(&stp
->st_perstateowner
);
1185 if (flags
& OPEN_STATE
) {
1186 release_stateid_lockowners(stp
);
1187 stp
->st_vfs_file
= NULL
;
1189 } else if (flags
& LOCK_STATE
)
1190 locks_remove_posix(filp
, (fl_owner_t
) stp
->st_stateowner
);
1191 put_nfs4_file(stp
->st_file
);
1192 kmem_cache_free(stateid_slab
, stp
);
1196 move_to_close_lru(struct nfs4_stateowner
*sop
)
1198 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop
);
1200 list_move_tail(&sop
->so_close_lru
, &close_lru
);
1201 sop
->so_time
= get_seconds();
1205 cmp_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
, clientid_t
*clid
) {
1206 return ((sop
->so_owner
.len
== owner
->len
) &&
1207 !memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
1208 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
));
1211 static struct nfs4_stateowner
*
1212 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
)
1214 struct nfs4_stateowner
*so
= NULL
;
1216 list_for_each_entry(so
, &ownerstr_hashtbl
[hashval
], so_strhash
) {
1217 if (cmp_owner_str(so
, &open
->op_owner
, &open
->op_clientid
))
1223 /* search file_hashtbl[] for file */
1224 static struct nfs4_file
*
1225 find_file(struct inode
*ino
)
1227 unsigned int hashval
= file_hashval(ino
);
1228 struct nfs4_file
*fp
;
1230 list_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
1231 if (fp
->fi_inode
== ino
) {
1239 static int access_valid(u32 x
)
1241 return (x
> 0 && x
< 4);
1244 static int deny_valid(u32 x
)
1246 return (x
>= 0 && x
< 5);
1250 set_access(unsigned int *access
, unsigned long bmap
) {
1254 for (i
= 1; i
< 4; i
++) {
1255 if (test_bit(i
, &bmap
))
1261 set_deny(unsigned int *deny
, unsigned long bmap
) {
1265 for (i
= 0; i
< 4; i
++) {
1266 if (test_bit(i
, &bmap
))
1272 test_share(struct nfs4_stateid
*stp
, struct nfsd4_open
*open
) {
1273 unsigned int access
, deny
;
1275 set_access(&access
, stp
->st_access_bmap
);
1276 set_deny(&deny
, stp
->st_deny_bmap
);
1277 if ((access
& open
->op_share_deny
) || (deny
& open
->op_share_access
))
1283 * Called to check deny when READ with all zero stateid or
1284 * WRITE with all zero or all one stateid
1287 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
1289 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
1290 struct nfs4_file
*fp
;
1291 struct nfs4_stateid
*stp
;
1294 dprintk("NFSD: nfs4_share_conflict\n");
1296 fp
= find_file(ino
);
1299 ret
= nfserr_locked
;
1300 /* Search for conflicting share reservations */
1301 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
) {
1302 if (test_bit(deny_type
, &stp
->st_deny_bmap
) ||
1303 test_bit(NFS4_SHARE_DENY_BOTH
, &stp
->st_deny_bmap
))
1313 nfs4_file_downgrade(struct file
*filp
, unsigned int share_access
)
1315 if (share_access
& NFS4_SHARE_ACCESS_WRITE
) {
1316 put_write_access(filp
->f_dentry
->d_inode
);
1317 filp
->f_mode
= (filp
->f_mode
| FMODE_READ
) & ~FMODE_WRITE
;
1322 * Recall a delegation
1325 do_recall(void *__dp
)
1327 struct nfs4_delegation
*dp
= __dp
;
1329 daemonize("nfsv4-recall");
1331 nfsd4_cb_recall(dp
);
1336 * Spawn a thread to perform a recall on the delegation represented
1337 * by the lease (file_lock)
1339 * Called from break_lease() with lock_kernel() held.
1340 * Note: we assume break_lease will only call this *once* for any given
1344 void nfsd_break_deleg_cb(struct file_lock
*fl
)
1346 struct nfs4_delegation
*dp
= (struct nfs4_delegation
*)fl
->fl_owner
;
1347 struct task_struct
*t
;
1349 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp
,fl
);
1353 /* We're assuming the state code never drops its reference
1354 * without first removing the lease. Since we're in this lease
1355 * callback (and since the lease code is serialized by the kernel
1356 * lock) we know the server hasn't removed the lease yet, we know
1357 * it's safe to take a reference: */
1358 atomic_inc(&dp
->dl_count
);
1360 spin_lock(&recall_lock
);
1361 list_add_tail(&dp
->dl_recall_lru
, &del_recall_lru
);
1362 spin_unlock(&recall_lock
);
1364 /* only place dl_time is set. protected by lock_kernel*/
1365 dp
->dl_time
= get_seconds();
1367 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1368 fl
->fl_break_time
= jiffies
+ NFSD_LEASE_TIME
* HZ
;
1370 t
= kthread_run(do_recall
, dp
, "%s", "nfs4_cb_recall");
1372 struct nfs4_client
*clp
= dp
->dl_client
;
1374 printk(KERN_INFO
"NFSD: Callback thread failed for "
1375 "for client (clientid %08x/%08x)\n",
1376 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1377 nfs4_put_delegation(dp
);
1382 * The file_lock is being reapd.
1384 * Called by locks_free_lock() with lock_kernel() held.
1387 void nfsd_release_deleg_cb(struct file_lock
*fl
)
1389 struct nfs4_delegation
*dp
= (struct nfs4_delegation
*)fl
->fl_owner
;
1391 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl
,dp
, atomic_read(&dp
->dl_count
));
1393 if (!(fl
->fl_flags
& FL_LEASE
) || !dp
)
1395 dp
->dl_flock
= NULL
;
1399 * Set the delegation file_lock back pointer.
1401 * Called from __setlease() with lock_kernel() held.
1404 void nfsd_copy_lock_deleg_cb(struct file_lock
*new, struct file_lock
*fl
)
1406 struct nfs4_delegation
*dp
= (struct nfs4_delegation
*)new->fl_owner
;
1408 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp
);
1415 * Called from __setlease() with lock_kernel() held
1418 int nfsd_same_client_deleg_cb(struct file_lock
*onlist
, struct file_lock
*try)
1420 struct nfs4_delegation
*onlistd
=
1421 (struct nfs4_delegation
*)onlist
->fl_owner
;
1422 struct nfs4_delegation
*tryd
=
1423 (struct nfs4_delegation
*)try->fl_owner
;
1425 if (onlist
->fl_lmops
!= try->fl_lmops
)
1428 return onlistd
->dl_client
== tryd
->dl_client
;
1433 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
1436 return lease_modify(onlist
, arg
);
1441 static struct lock_manager_operations nfsd_lease_mng_ops
= {
1442 .fl_break
= nfsd_break_deleg_cb
,
1443 .fl_release_private
= nfsd_release_deleg_cb
,
1444 .fl_copy_lock
= nfsd_copy_lock_deleg_cb
,
1445 .fl_mylease
= nfsd_same_client_deleg_cb
,
1446 .fl_change
= nfsd_change_deleg_cb
,
1451 nfsd4_process_open1(struct nfsd4_open
*open
)
1453 clientid_t
*clientid
= &open
->op_clientid
;
1454 struct nfs4_client
*clp
= NULL
;
1455 unsigned int strhashval
;
1456 struct nfs4_stateowner
*sop
= NULL
;
1458 if (!check_name(open
->op_owner
))
1459 return nfserr_inval
;
1461 if (STALE_CLIENTID(&open
->op_clientid
))
1462 return nfserr_stale_clientid
;
1464 strhashval
= ownerstr_hashval(clientid
->cl_id
, open
->op_owner
);
1465 sop
= find_openstateowner_str(strhashval
, open
);
1466 open
->op_stateowner
= sop
;
1468 /* Make sure the client's lease hasn't expired. */
1469 clp
= find_confirmed_client(clientid
);
1471 return nfserr_expired
;
1474 if (!sop
->so_confirmed
) {
1475 /* Replace unconfirmed owners without checking for replay. */
1476 clp
= sop
->so_client
;
1477 release_stateowner(sop
);
1478 open
->op_stateowner
= NULL
;
1481 if (open
->op_seqid
== sop
->so_seqid
- 1) {
1482 if (sop
->so_replay
.rp_buflen
)
1483 return NFSERR_REPLAY_ME
;
1484 /* The original OPEN failed so spectacularly
1485 * that we don't even have replay data saved!
1486 * Therefore, we have no choice but to continue
1487 * processing this OPEN; presumably, we'll
1488 * fail again for the same reason.
1490 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1493 if (open
->op_seqid
!= sop
->so_seqid
)
1494 return nfserr_bad_seqid
;
1496 if (open
->op_stateowner
== NULL
) {
1497 sop
= alloc_init_open_stateowner(strhashval
, clp
, open
);
1499 return nfserr_resource
;
1500 open
->op_stateowner
= sop
;
1502 list_del_init(&sop
->so_close_lru
);
1503 renew_client(sop
->so_client
);
1508 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
1510 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
1511 return nfserr_openmode
;
1516 static struct nfs4_delegation
*
1517 find_delegation_file(struct nfs4_file
*fp
, stateid_t
*stid
)
1519 struct nfs4_delegation
*dp
;
1521 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
) {
1522 if (dp
->dl_stateid
.si_stateownerid
== stid
->si_stateownerid
)
1529 nfs4_check_deleg(struct nfs4_file
*fp
, struct nfsd4_open
*open
,
1530 struct nfs4_delegation
**dp
)
1533 int status
= nfserr_bad_stateid
;
1535 *dp
= find_delegation_file(fp
, &open
->op_delegate_stateid
);
1538 flags
= open
->op_share_access
== NFS4_SHARE_ACCESS_READ
?
1539 RD_STATE
: WR_STATE
;
1540 status
= nfs4_check_delegmode(*dp
, flags
);
1544 if (open
->op_claim_type
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
)
1548 open
->op_stateowner
->so_confirmed
= 1;
1553 nfs4_check_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
, struct nfs4_stateid
**stpp
)
1555 struct nfs4_stateid
*local
;
1556 int status
= nfserr_share_denied
;
1557 struct nfs4_stateowner
*sop
= open
->op_stateowner
;
1559 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
1560 /* ignore lock owners */
1561 if (local
->st_stateowner
->so_is_open_owner
== 0)
1563 /* remember if we have seen this open owner */
1564 if (local
->st_stateowner
== sop
)
1566 /* check for conflicting share reservations */
1567 if (!test_share(local
, open
))
1575 static inline struct nfs4_stateid
*
1576 nfs4_alloc_stateid(void)
1578 return kmem_cache_alloc(stateid_slab
, GFP_KERNEL
);
1582 nfs4_new_open(struct svc_rqst
*rqstp
, struct nfs4_stateid
**stpp
,
1583 struct nfs4_delegation
*dp
,
1584 struct svc_fh
*cur_fh
, int flags
)
1586 struct nfs4_stateid
*stp
;
1588 stp
= nfs4_alloc_stateid();
1590 return nfserr_resource
;
1593 get_file(dp
->dl_vfs_file
);
1594 stp
->st_vfs_file
= dp
->dl_vfs_file
;
1597 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, flags
,
1600 if (status
== nfserr_dropit
)
1601 status
= nfserr_jukebox
;
1602 kmem_cache_free(stateid_slab
, stp
);
1611 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
1612 struct nfsd4_open
*open
)
1614 struct iattr iattr
= {
1615 .ia_valid
= ATTR_SIZE
,
1618 if (!open
->op_truncate
)
1620 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
1621 return nfserr_inval
;
1622 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
1626 nfs4_upgrade_open(struct svc_rqst
*rqstp
, struct svc_fh
*cur_fh
, struct nfs4_stateid
*stp
, struct nfsd4_open
*open
)
1628 struct file
*filp
= stp
->st_vfs_file
;
1629 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1630 unsigned int share_access
, new_writer
;
1633 set_access(&share_access
, stp
->st_access_bmap
);
1634 new_writer
= (~share_access
) & open
->op_share_access
1635 & NFS4_SHARE_ACCESS_WRITE
;
1638 status
= get_write_access(inode
);
1640 return nfserrno(status
);
1642 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
1645 put_write_access(inode
);
1648 /* remember the open */
1649 filp
->f_mode
|= open
->op_share_access
;
1650 set_bit(open
->op_share_access
, &stp
->st_access_bmap
);
1651 set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
1658 nfs4_set_claim_prev(struct nfsd4_open
*open
)
1660 open
->op_stateowner
->so_confirmed
= 1;
1661 open
->op_stateowner
->so_client
->cl_firststate
= 1;
1665 * Attempt to hand out a delegation.
1668 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
, struct nfs4_stateid
*stp
)
1670 struct nfs4_delegation
*dp
;
1671 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
1672 struct nfs4_callback
*cb
= &sop
->so_client
->cl_callback
;
1673 struct file_lock fl
, *flp
= &fl
;
1674 int status
, flag
= 0;
1676 flag
= NFS4_OPEN_DELEGATE_NONE
;
1677 open
->op_recall
= 0;
1678 switch (open
->op_claim_type
) {
1679 case NFS4_OPEN_CLAIM_PREVIOUS
:
1680 if (!atomic_read(&cb
->cb_set
))
1681 open
->op_recall
= 1;
1682 flag
= open
->op_delegate_type
;
1683 if (flag
== NFS4_OPEN_DELEGATE_NONE
)
1686 case NFS4_OPEN_CLAIM_NULL
:
1687 /* Let's not give out any delegations till everyone's
1688 * had the chance to reclaim theirs.... */
1689 if (nfs4_in_grace())
1691 if (!atomic_read(&cb
->cb_set
) || !sop
->so_confirmed
)
1693 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
1694 flag
= NFS4_OPEN_DELEGATE_WRITE
;
1696 flag
= NFS4_OPEN_DELEGATE_READ
;
1702 dp
= alloc_init_deleg(sop
->so_client
, stp
, fh
, flag
);
1704 flag
= NFS4_OPEN_DELEGATE_NONE
;
1707 locks_init_lock(&fl
);
1708 fl
.fl_lmops
= &nfsd_lease_mng_ops
;
1709 fl
.fl_flags
= FL_LEASE
;
1710 fl
.fl_end
= OFFSET_MAX
;
1711 fl
.fl_owner
= (fl_owner_t
)dp
;
1712 fl
.fl_file
= stp
->st_vfs_file
;
1713 fl
.fl_pid
= current
->tgid
;
1715 /* setlease checks to see if delegation should be handed out.
1716 * the lock_manager callbacks fl_mylease and fl_change are used
1718 if ((status
= setlease(stp
->st_vfs_file
,
1719 flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
, &flp
))) {
1720 dprintk("NFSD: setlease failed [%d], no delegation\n", status
);
1721 unhash_delegation(dp
);
1722 flag
= NFS4_OPEN_DELEGATE_NONE
;
1726 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stateid
, sizeof(dp
->dl_stateid
));
1728 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1729 dp
->dl_stateid
.si_boot
,
1730 dp
->dl_stateid
.si_stateownerid
,
1731 dp
->dl_stateid
.si_fileid
,
1732 dp
->dl_stateid
.si_generation
);
1734 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
1735 && flag
== NFS4_OPEN_DELEGATE_NONE
1736 && open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
)
1737 printk("NFSD: WARNING: refusing delegation reclaim\n");
1738 open
->op_delegate_type
= flag
;
1742 * called with nfs4_lock_state() held.
1745 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
1747 struct nfs4_file
*fp
= NULL
;
1748 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
1749 struct nfs4_stateid
*stp
= NULL
;
1750 struct nfs4_delegation
*dp
= NULL
;
1753 status
= nfserr_inval
;
1754 if (!access_valid(open
->op_share_access
)
1755 || !deny_valid(open
->op_share_deny
))
1758 * Lookup file; if found, lookup stateid and check open request,
1759 * and check for delegations in the process of being recalled.
1760 * If not found, create the nfs4_file struct
1762 fp
= find_file(ino
);
1764 if ((status
= nfs4_check_open(fp
, open
, &stp
)))
1766 status
= nfs4_check_deleg(fp
, open
, &dp
);
1770 status
= nfserr_bad_stateid
;
1771 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
)
1773 status
= nfserr_resource
;
1774 fp
= alloc_init_file(ino
);
1780 * OPEN the file, or upgrade an existing OPEN.
1781 * If truncate fails, the OPEN fails.
1784 /* Stateid was found, this is an OPEN upgrade */
1785 status
= nfs4_upgrade_open(rqstp
, current_fh
, stp
, open
);
1788 update_stateid(&stp
->st_stateid
);
1790 /* Stateid was not found, this is a new OPEN */
1792 if (open
->op_share_access
& NFS4_SHARE_ACCESS_READ
)
1794 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
1796 status
= nfs4_new_open(rqstp
, &stp
, dp
, current_fh
, flags
);
1799 init_stateid(stp
, fp
, open
);
1800 status
= nfsd4_truncate(rqstp
, current_fh
, open
);
1802 release_stateid(stp
, OPEN_STATE
);
1806 memcpy(&open
->op_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
1809 * Attempt to hand out a delegation. No error return, because the
1810 * OPEN succeeds even if we fail.
1812 nfs4_open_delegation(current_fh
, open
, stp
);
1816 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1817 stp
->st_stateid
.si_boot
, stp
->st_stateid
.si_stateownerid
,
1818 stp
->st_stateid
.si_fileid
, stp
->st_stateid
.si_generation
);
1822 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
1823 nfs4_set_claim_prev(open
);
1825 * To finish the open response, we just need to set the rflags.
1827 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
1828 if (!open
->op_stateowner
->so_confirmed
)
1829 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
1834 static struct workqueue_struct
*laundry_wq
;
1835 static struct work_struct laundromat_work
;
1836 static void laundromat_main(void *);
1837 static DECLARE_WORK(laundromat_work
, laundromat_main
, NULL
);
1840 nfsd4_renew(clientid_t
*clid
)
1842 struct nfs4_client
*clp
;
1846 dprintk("process_renew(%08x/%08x): starting\n",
1847 clid
->cl_boot
, clid
->cl_id
);
1848 status
= nfserr_stale_clientid
;
1849 if (STALE_CLIENTID(clid
))
1851 clp
= find_confirmed_client(clid
);
1852 status
= nfserr_expired
;
1854 /* We assume the client took too long to RENEW. */
1855 dprintk("nfsd4_renew: clientid not found!\n");
1859 status
= nfserr_cb_path_down
;
1860 if (!list_empty(&clp
->cl_delegations
)
1861 && !atomic_read(&clp
->cl_callback
.cb_set
))
1865 nfs4_unlock_state();
1872 dprintk("NFSD: end of grace period\n");
1873 nfsd4_recdir_purge_old();
1878 nfs4_laundromat(void)
1880 struct nfs4_client
*clp
;
1881 struct nfs4_stateowner
*sop
;
1882 struct nfs4_delegation
*dp
;
1883 struct list_head
*pos
, *next
, reaplist
;
1884 time_t cutoff
= get_seconds() - NFSD_LEASE_TIME
;
1885 time_t t
, clientid_val
= NFSD_LEASE_TIME
;
1886 time_t u
, test_val
= NFSD_LEASE_TIME
;
1890 dprintk("NFSD: laundromat service - starting\n");
1893 list_for_each_safe(pos
, next
, &client_lru
) {
1894 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
1895 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
1896 t
= clp
->cl_time
- cutoff
;
1897 if (clientid_val
> t
)
1901 dprintk("NFSD: purging unused client (clientid %08x)\n",
1902 clp
->cl_clientid
.cl_id
);
1903 nfsd4_remove_clid_dir(clp
);
1906 INIT_LIST_HEAD(&reaplist
);
1907 spin_lock(&recall_lock
);
1908 list_for_each_safe(pos
, next
, &del_recall_lru
) {
1909 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
1910 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
1911 u
= dp
->dl_time
- cutoff
;
1916 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1918 list_move(&dp
->dl_recall_lru
, &reaplist
);
1920 spin_unlock(&recall_lock
);
1921 list_for_each_safe(pos
, next
, &reaplist
) {
1922 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
1923 list_del_init(&dp
->dl_recall_lru
);
1924 unhash_delegation(dp
);
1926 test_val
= NFSD_LEASE_TIME
;
1927 list_for_each_safe(pos
, next
, &close_lru
) {
1928 sop
= list_entry(pos
, struct nfs4_stateowner
, so_close_lru
);
1929 if (time_after((unsigned long)sop
->so_time
, (unsigned long)cutoff
)) {
1930 u
= sop
->so_time
- cutoff
;
1935 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1937 release_stateowner(sop
);
1939 if (clientid_val
< NFSD_LAUNDROMAT_MINTIMEOUT
)
1940 clientid_val
= NFSD_LAUNDROMAT_MINTIMEOUT
;
1941 nfs4_unlock_state();
1942 return clientid_val
;
1946 laundromat_main(void *not_used
)
1950 t
= nfs4_laundromat();
1951 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
1952 queue_delayed_work(laundry_wq
, &laundromat_work
, t
*HZ
);
1955 static struct nfs4_stateowner
*
1956 search_close_lru(u32 st_id
, int flags
)
1958 struct nfs4_stateowner
*local
= NULL
;
1960 if (flags
& CLOSE_STATE
) {
1961 list_for_each_entry(local
, &close_lru
, so_close_lru
) {
1962 if (local
->so_id
== st_id
)
1970 nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stateid
*stp
)
1972 return fhp
->fh_dentry
->d_inode
!= stp
->st_vfs_file
->f_dentry
->d_inode
;
1976 STALE_STATEID(stateid_t
*stateid
)
1978 if (stateid
->si_boot
== boot_time
)
1980 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1981 stateid
->si_boot
, stateid
->si_stateownerid
, stateid
->si_fileid
,
1982 stateid
->si_generation
);
1987 access_permit_read(unsigned long access_bmap
)
1989 return test_bit(NFS4_SHARE_ACCESS_READ
, &access_bmap
) ||
1990 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
) ||
1991 test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
);
1995 access_permit_write(unsigned long access_bmap
)
1997 return test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
) ||
1998 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
);
2002 int nfs4_check_openmode(struct nfs4_stateid
*stp
, int flags
)
2004 int status
= nfserr_openmode
;
2006 if ((flags
& WR_STATE
) && (!access_permit_write(stp
->st_access_bmap
)))
2008 if ((flags
& RD_STATE
) && (!access_permit_read(stp
->st_access_bmap
)))
2016 check_special_stateids(svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
2018 /* Trying to call delegreturn with a special stateid? Yuch: */
2019 if (!(flags
& (RD_STATE
| WR_STATE
)))
2020 return nfserr_bad_stateid
;
2021 else if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
2023 else if (nfs4_in_grace()) {
2024 /* Answer in remaining cases depends on existance of
2025 * conflicting state; so we must wait out the grace period. */
2026 return nfserr_grace
;
2027 } else if (flags
& WR_STATE
)
2028 return nfs4_share_conflict(current_fh
,
2029 NFS4_SHARE_DENY_WRITE
);
2030 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2031 return nfs4_share_conflict(current_fh
,
2032 NFS4_SHARE_DENY_READ
);
2036 * Allow READ/WRITE during grace period on recovered state only for files
2037 * that are not able to provide mandatory locking.
2040 io_during_grace_disallowed(struct inode
*inode
, int flags
)
2042 return nfs4_in_grace() && (flags
& (RD_STATE
| WR_STATE
))
2043 && MANDATORY_LOCK(inode
);
2047 * Checks for stateid operations
2050 nfs4_preprocess_stateid_op(struct svc_fh
*current_fh
, stateid_t
*stateid
, int flags
, struct file
**filpp
)
2052 struct nfs4_stateid
*stp
= NULL
;
2053 struct nfs4_delegation
*dp
= NULL
;
2055 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2058 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2059 stateid
->si_boot
, stateid
->si_stateownerid
,
2060 stateid
->si_fileid
, stateid
->si_generation
);
2064 if (io_during_grace_disallowed(ino
, flags
))
2065 return nfserr_grace
;
2067 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
2068 return check_special_stateids(current_fh
, stateid
, flags
);
2071 status
= nfserr_stale_stateid
;
2072 if (STALE_STATEID(stateid
))
2076 status
= nfserr_bad_stateid
;
2077 if (!stateid
->si_fileid
) { /* delegation stateid */
2078 if(!(dp
= find_delegation_stateid(ino
, stateid
))) {
2079 dprintk("NFSD: delegation stateid not found\n");
2082 stidp
= &dp
->dl_stateid
;
2083 } else { /* open or lock stateid */
2084 if (!(stp
= find_stateid(stateid
, flags
))) {
2085 dprintk("NFSD: open or lock stateid not found\n");
2088 if ((flags
& CHECK_FH
) && nfs4_check_fh(current_fh
, stp
))
2090 if (!stp
->st_stateowner
->so_confirmed
)
2092 stidp
= &stp
->st_stateid
;
2094 if (stateid
->si_generation
> stidp
->si_generation
)
2098 status
= nfserr_old_stateid
;
2099 if (stateid
->si_generation
< stidp
->si_generation
)
2102 if ((status
= nfs4_check_openmode(stp
,flags
)))
2104 renew_client(stp
->st_stateowner
->so_client
);
2106 *filpp
= stp
->st_vfs_file
;
2108 if ((status
= nfs4_check_delegmode(dp
, flags
)))
2110 renew_client(dp
->dl_client
);
2111 if (flags
& DELEG_RET
)
2112 unhash_delegation(dp
);
2114 *filpp
= dp
->dl_vfs_file
;
2124 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
2125 RD_STATE
: WR_STATE
;
2129 * Checks for sequence id mutating operations.
2132 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
)
2134 struct nfs4_stateid
*stp
;
2135 struct nfs4_stateowner
*sop
;
2137 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2138 "stateid = (%08x/%08x/%08x/%08x)\n", seqid
,
2139 stateid
->si_boot
, stateid
->si_stateownerid
, stateid
->si_fileid
,
2140 stateid
->si_generation
);
2145 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
)) {
2146 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2147 return nfserr_bad_stateid
;
2150 if (STALE_STATEID(stateid
))
2151 return nfserr_stale_stateid
;
2153 * We return BAD_STATEID if filehandle doesn't match stateid,
2154 * the confirmed flag is incorrecly set, or the generation
2155 * number is incorrect.
2157 stp
= find_stateid(stateid
, flags
);
2160 * Also, we should make sure this isn't just the result of
2163 sop
= search_close_lru(stateid
->si_stateownerid
, flags
);
2165 return nfserr_bad_stateid
;
2171 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
2172 clientid_t
*lockclid
= &lock
->v
.new.clientid
;
2173 struct nfs4_client
*clp
= sop
->so_client
;
2177 lkflg
= setlkflg(lock
->lk_type
);
2179 if (lock
->lk_is_new
) {
2180 if (!sop
->so_is_open_owner
)
2181 return nfserr_bad_stateid
;
2182 if (!cmp_clid(&clp
->cl_clientid
, lockclid
))
2183 return nfserr_bad_stateid
;
2184 /* stp is the open stateid */
2185 status
= nfs4_check_openmode(stp
, lkflg
);
2189 /* stp is the lock stateid */
2190 status
= nfs4_check_openmode(stp
->st_openstp
, lkflg
);
2197 if ((flags
& CHECK_FH
) && nfs4_check_fh(current_fh
, stp
)) {
2198 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2199 return nfserr_bad_stateid
;
2203 *sopp
= sop
= stp
->st_stateowner
;
2206 * We now validate the seqid and stateid generation numbers.
2207 * For the moment, we ignore the possibility of
2208 * generation number wraparound.
2210 if (seqid
!= sop
->so_seqid
)
2213 if (sop
->so_confirmed
&& flags
& CONFIRM
) {
2214 printk("NFSD: preprocess_seqid_op: expected"
2215 " unconfirmed stateowner!\n");
2216 return nfserr_bad_stateid
;
2218 if (!sop
->so_confirmed
&& !(flags
& CONFIRM
)) {
2219 printk("NFSD: preprocess_seqid_op: stateowner not"
2220 " confirmed yet!\n");
2221 return nfserr_bad_stateid
;
2223 if (stateid
->si_generation
> stp
->st_stateid
.si_generation
) {
2224 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2225 return nfserr_bad_stateid
;
2228 if (stateid
->si_generation
< stp
->st_stateid
.si_generation
) {
2229 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2230 return nfserr_old_stateid
;
2232 renew_client(sop
->so_client
);
2236 if (seqid
== sop
->so_seqid
- 1) {
2237 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
2238 /* indicate replay to calling function */
2239 return NFSERR_REPLAY_ME
;
2241 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2242 sop
->so_seqid
, seqid
);
2244 return nfserr_bad_seqid
;
2248 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open_confirm
*oc
, struct nfs4_stateowner
**replay_owner
)
2251 struct nfs4_stateowner
*sop
;
2252 struct nfs4_stateid
*stp
;
2254 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2255 (int)current_fh
->fh_dentry
->d_name
.len
,
2256 current_fh
->fh_dentry
->d_name
.name
);
2258 status
= fh_verify(rqstp
, current_fh
, S_IFREG
, 0);
2264 if ((status
= nfs4_preprocess_seqid_op(current_fh
, oc
->oc_seqid
,
2265 &oc
->oc_req_stateid
,
2266 CHECK_FH
| CONFIRM
| OPEN_STATE
,
2267 &oc
->oc_stateowner
, &stp
, NULL
)))
2270 sop
= oc
->oc_stateowner
;
2271 sop
->so_confirmed
= 1;
2272 update_stateid(&stp
->st_stateid
);
2273 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2274 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2275 "stateid=(%08x/%08x/%08x/%08x)\n", oc
->oc_seqid
,
2276 stp
->st_stateid
.si_boot
,
2277 stp
->st_stateid
.si_stateownerid
,
2278 stp
->st_stateid
.si_fileid
,
2279 stp
->st_stateid
.si_generation
);
2281 nfsd4_create_clid_dir(sop
->so_client
);
2283 if (oc
->oc_stateowner
) {
2284 nfs4_get_stateowner(oc
->oc_stateowner
);
2285 *replay_owner
= oc
->oc_stateowner
;
2287 nfs4_unlock_state();
2293 * unset all bits in union bitmap (bmap) that
2294 * do not exist in share (from successful OPEN_DOWNGRADE)
2297 reset_union_bmap_access(unsigned long access
, unsigned long *bmap
)
2300 for (i
= 1; i
< 4; i
++) {
2301 if ((i
& access
) != i
)
2302 __clear_bit(i
, bmap
);
2307 reset_union_bmap_deny(unsigned long deny
, unsigned long *bmap
)
2310 for (i
= 0; i
< 4; i
++) {
2311 if ((i
& deny
) != i
)
2312 __clear_bit(i
, bmap
);
2317 nfsd4_open_downgrade(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open_downgrade
*od
, struct nfs4_stateowner
**replay_owner
)
2320 struct nfs4_stateid
*stp
;
2321 unsigned int share_access
;
2323 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2324 (int)current_fh
->fh_dentry
->d_name
.len
,
2325 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(current_fh
, od
->od_seqid
,
2334 CHECK_FH
| OPEN_STATE
,
2335 &od
->od_stateowner
, &stp
, NULL
)))
2338 status
= nfserr_inval
;
2339 if (!test_bit(od
->od_share_access
, &stp
->st_access_bmap
)) {
2340 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2341 stp
->st_access_bmap
, od
->od_share_access
);
2344 if (!test_bit(od
->od_share_deny
, &stp
->st_deny_bmap
)) {
2345 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2346 stp
->st_deny_bmap
, od
->od_share_deny
);
2349 set_access(&share_access
, stp
->st_access_bmap
);
2350 nfs4_file_downgrade(stp
->st_vfs_file
,
2351 share_access
& ~od
->od_share_access
);
2353 reset_union_bmap_access(od
->od_share_access
, &stp
->st_access_bmap
);
2354 reset_union_bmap_deny(od
->od_share_deny
, &stp
->st_deny_bmap
);
2356 update_stateid(&stp
->st_stateid
);
2357 memcpy(&od
->od_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2360 if (od
->od_stateowner
) {
2361 nfs4_get_stateowner(od
->od_stateowner
);
2362 *replay_owner
= od
->od_stateowner
;
2364 nfs4_unlock_state();
2369 * nfs4_unlock_state() called after encode
2372 nfsd4_close(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_close
*close
, struct nfs4_stateowner
**replay_owner
)
2375 struct nfs4_stateid
*stp
;
2377 dprintk("NFSD: nfsd4_close on file %.*s\n",
2378 (int)current_fh
->fh_dentry
->d_name
.len
,
2379 current_fh
->fh_dentry
->d_name
.name
);
2382 /* check close_lru for replay */
2383 if ((status
= nfs4_preprocess_seqid_op(current_fh
, close
->cl_seqid
,
2385 CHECK_FH
| OPEN_STATE
| CLOSE_STATE
,
2386 &close
->cl_stateowner
, &stp
, NULL
)))
2389 update_stateid(&stp
->st_stateid
);
2390 memcpy(&close
->cl_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2392 /* release_stateid() calls nfsd_close() if needed */
2393 release_stateid(stp
, OPEN_STATE
);
2395 /* place unused nfs4_stateowners on so_close_lru list to be
2396 * released by the laundromat service after the lease period
2397 * to enable us to handle CLOSE replay
2399 if (list_empty(&close
->cl_stateowner
->so_stateids
))
2400 move_to_close_lru(close
->cl_stateowner
);
2402 if (close
->cl_stateowner
) {
2403 nfs4_get_stateowner(close
->cl_stateowner
);
2404 *replay_owner
= close
->cl_stateowner
;
2406 nfs4_unlock_state();
2411 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_delegreturn
*dr
)
2415 if ((status
= fh_verify(rqstp
, current_fh
, S_IFREG
, 0)))
2419 status
= nfs4_preprocess_stateid_op(current_fh
, &dr
->dr_stateid
, DELEG_RET
, NULL
);
2420 nfs4_unlock_state();
2427 * Lock owner state (byte-range locks)
2429 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2430 #define LOCK_HASH_BITS 8
2431 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2432 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2434 #define lockownerid_hashval(id) \
2435 ((id) & LOCK_HASH_MASK)
2437 static inline unsigned int
2438 lock_ownerstr_hashval(struct inode
*inode
, u32 cl_id
,
2439 struct xdr_netobj
*ownername
)
2441 return (file_hashval(inode
) + cl_id
2442 + opaque_hashval(ownername
->data
, ownername
->len
))
2446 static struct list_head lock_ownerid_hashtbl
[LOCK_HASH_SIZE
];
2447 static struct list_head lock_ownerstr_hashtbl
[LOCK_HASH_SIZE
];
2448 static struct list_head lockstateid_hashtbl
[STATEID_HASH_SIZE
];
2450 static struct nfs4_stateid
*
2451 find_stateid(stateid_t
*stid
, int flags
)
2453 struct nfs4_stateid
*local
= NULL
;
2454 u32 st_id
= stid
->si_stateownerid
;
2455 u32 f_id
= stid
->si_fileid
;
2456 unsigned int hashval
;
2458 dprintk("NFSD: find_stateid flags 0x%x\n",flags
);
2459 if ((flags
& LOCK_STATE
) || (flags
& RD_STATE
) || (flags
& WR_STATE
)) {
2460 hashval
= stateid_hashval(st_id
, f_id
);
2461 list_for_each_entry(local
, &lockstateid_hashtbl
[hashval
], st_hash
) {
2462 if ((local
->st_stateid
.si_stateownerid
== st_id
) &&
2463 (local
->st_stateid
.si_fileid
== f_id
))
2467 if ((flags
& OPEN_STATE
) || (flags
& RD_STATE
) || (flags
& WR_STATE
)) {
2468 hashval
= stateid_hashval(st_id
, f_id
);
2469 list_for_each_entry(local
, &stateid_hashtbl
[hashval
], st_hash
) {
2470 if ((local
->st_stateid
.si_stateownerid
== st_id
) &&
2471 (local
->st_stateid
.si_fileid
== f_id
))
2478 static struct nfs4_delegation
*
2479 find_delegation_stateid(struct inode
*ino
, stateid_t
*stid
)
2481 struct nfs4_file
*fp
;
2482 struct nfs4_delegation
*dl
;
2484 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2485 stid
->si_boot
, stid
->si_stateownerid
,
2486 stid
->si_fileid
, stid
->si_generation
);
2488 fp
= find_file(ino
);
2491 dl
= find_delegation_file(fp
, stid
);
2497 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2498 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2499 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2500 * locking, this prevents us from being completely protocol-compliant. The
2501 * real solution to this problem is to start using unsigned file offsets in
2502 * the VFS, but this is a very deep change!
2505 nfs4_transform_lock_offset(struct file_lock
*lock
)
2507 if (lock
->fl_start
< 0)
2508 lock
->fl_start
= OFFSET_MAX
;
2509 if (lock
->fl_end
< 0)
2510 lock
->fl_end
= OFFSET_MAX
;
2513 /* Hack!: For now, we're defining this just so we can use a pointer to it
2514 * as a unique cookie to identify our (NFSv4's) posix locks. */
2515 static struct lock_manager_operations nfsd_posix_mng_ops
= {
2519 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
2521 struct nfs4_stateowner
*sop
;
2524 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
2525 sop
= (struct nfs4_stateowner
*) fl
->fl_owner
;
2526 hval
= lockownerid_hashval(sop
->so_id
);
2527 kref_get(&sop
->so_ref
);
2529 deny
->ld_clientid
= sop
->so_client
->cl_clientid
;
2531 deny
->ld_sop
= NULL
;
2532 deny
->ld_clientid
.cl_boot
= 0;
2533 deny
->ld_clientid
.cl_id
= 0;
2535 deny
->ld_start
= fl
->fl_start
;
2536 deny
->ld_length
= ~(u64
)0;
2537 if (fl
->fl_end
!= ~(u64
)0)
2538 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
2539 deny
->ld_type
= NFS4_READ_LT
;
2540 if (fl
->fl_type
!= F_RDLCK
)
2541 deny
->ld_type
= NFS4_WRITE_LT
;
2544 static struct nfs4_stateowner
*
2545 find_lockstateowner_str(struct inode
*inode
, clientid_t
*clid
,
2546 struct xdr_netobj
*owner
)
2548 unsigned int hashval
= lock_ownerstr_hashval(inode
, clid
->cl_id
, owner
);
2549 struct nfs4_stateowner
*op
;
2551 list_for_each_entry(op
, &lock_ownerstr_hashtbl
[hashval
], so_strhash
) {
2552 if (cmp_owner_str(op
, owner
, clid
))
2559 * Alloc a lock owner structure.
2560 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2563 * strhashval = lock_ownerstr_hashval
2566 static struct nfs4_stateowner
*
2567 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
2568 struct nfs4_stateowner
*sop
;
2569 struct nfs4_replay
*rp
;
2570 unsigned int idhashval
;
2572 if (!(sop
= alloc_stateowner(&lock
->lk_new_owner
)))
2574 idhashval
= lockownerid_hashval(current_ownerid
);
2575 INIT_LIST_HEAD(&sop
->so_idhash
);
2576 INIT_LIST_HEAD(&sop
->so_strhash
);
2577 INIT_LIST_HEAD(&sop
->so_perclient
);
2578 INIT_LIST_HEAD(&sop
->so_stateids
);
2579 INIT_LIST_HEAD(&sop
->so_perstateid
);
2580 INIT_LIST_HEAD(&sop
->so_close_lru
); /* not used */
2582 list_add(&sop
->so_idhash
, &lock_ownerid_hashtbl
[idhashval
]);
2583 list_add(&sop
->so_strhash
, &lock_ownerstr_hashtbl
[strhashval
]);
2584 list_add(&sop
->so_perstateid
, &open_stp
->st_lockowners
);
2585 sop
->so_is_open_owner
= 0;
2586 sop
->so_id
= current_ownerid
++;
2587 sop
->so_client
= clp
;
2588 /* It is the openowner seqid that will be incremented in encode in the
2589 * case of new lockowners; so increment the lock seqid manually: */
2590 sop
->so_seqid
= lock
->lk_new_lock_seqid
+ 1;
2591 sop
->so_confirmed
= 1;
2592 rp
= &sop
->so_replay
;
2593 rp
->rp_status
= nfserr_serverfault
;
2595 rp
->rp_buf
= rp
->rp_ibuf
;
2599 static struct nfs4_stateid
*
2600 alloc_init_lock_stateid(struct nfs4_stateowner
*sop
, struct nfs4_file
*fp
, struct nfs4_stateid
*open_stp
)
2602 struct nfs4_stateid
*stp
;
2603 unsigned int hashval
= stateid_hashval(sop
->so_id
, fp
->fi_id
);
2605 stp
= nfs4_alloc_stateid();
2608 INIT_LIST_HEAD(&stp
->st_hash
);
2609 INIT_LIST_HEAD(&stp
->st_perfile
);
2610 INIT_LIST_HEAD(&stp
->st_perstateowner
);
2611 INIT_LIST_HEAD(&stp
->st_lockowners
); /* not used */
2612 list_add(&stp
->st_hash
, &lockstateid_hashtbl
[hashval
]);
2613 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2614 list_add(&stp
->st_perstateowner
, &sop
->so_stateids
);
2615 stp
->st_stateowner
= sop
;
2618 stp
->st_stateid
.si_boot
= boot_time
;
2619 stp
->st_stateid
.si_stateownerid
= sop
->so_id
;
2620 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
2621 stp
->st_stateid
.si_generation
= 0;
2622 stp
->st_vfs_file
= open_stp
->st_vfs_file
; /* FIXME refcount?? */
2623 stp
->st_access_bmap
= open_stp
->st_access_bmap
;
2624 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
2625 stp
->st_openstp
= open_stp
;
2632 check_lock_length(u64 offset
, u64 length
)
2634 return ((length
== 0) || ((length
!= ~(u64
)0) &&
2635 LOFF_OVERFLOW(offset
, length
)));
2642 nfsd4_lock(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_lock
*lock
, struct nfs4_stateowner
**replay_owner
)
2644 struct nfs4_stateowner
*open_sop
= NULL
;
2645 struct nfs4_stateowner
*lock_sop
= NULL
;
2646 struct nfs4_stateid
*lock_stp
;
2648 struct file_lock file_lock
;
2649 struct file_lock conflock
;
2651 unsigned int strhashval
;
2653 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2654 (long long) lock
->lk_offset
,
2655 (long long) lock
->lk_length
);
2657 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
2658 return nfserr_inval
;
2660 if ((status
= fh_verify(rqstp
, current_fh
, S_IFREG
, MAY_LOCK
))) {
2661 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2667 if (lock
->lk_is_new
) {
2669 * Client indicates that this is a new lockowner.
2670 * Use open owner and open stateid to create lock owner and
2673 struct nfs4_stateid
*open_stp
= NULL
;
2674 struct nfs4_file
*fp
;
2676 status
= nfserr_stale_clientid
;
2677 if (STALE_CLIENTID(&lock
->lk_new_clientid
))
2680 /* validate and update open stateid and open seqid */
2681 status
= nfs4_preprocess_seqid_op(current_fh
,
2682 lock
->lk_new_open_seqid
,
2683 &lock
->lk_new_open_stateid
,
2684 CHECK_FH
| OPEN_STATE
,
2685 &lock
->lk_replay_owner
, &open_stp
,
2689 open_sop
= lock
->lk_replay_owner
;
2690 /* create lockowner and lock stateid */
2691 fp
= open_stp
->st_file
;
2692 strhashval
= lock_ownerstr_hashval(fp
->fi_inode
,
2693 open_sop
->so_client
->cl_clientid
.cl_id
,
2694 &lock
->v
.new.owner
);
2695 /* XXX: Do we need to check for duplicate stateowners on
2696 * the same file, or should they just be allowed (and
2697 * create new stateids)? */
2698 status
= nfserr_resource
;
2699 lock_sop
= alloc_init_lock_stateowner(strhashval
,
2700 open_sop
->so_client
, open_stp
, lock
);
2701 if (lock_sop
== NULL
)
2703 lock_stp
= alloc_init_lock_stateid(lock_sop
, fp
, open_stp
);
2704 if (lock_stp
== NULL
)
2707 /* lock (lock owner + lock stateid) already exists */
2708 status
= nfs4_preprocess_seqid_op(current_fh
,
2709 lock
->lk_old_lock_seqid
,
2710 &lock
->lk_old_lock_stateid
,
2711 CHECK_FH
| LOCK_STATE
,
2712 &lock
->lk_replay_owner
, &lock_stp
, lock
);
2715 lock_sop
= lock
->lk_replay_owner
;
2717 /* lock->lk_replay_owner and lock_stp have been created or found */
2718 filp
= lock_stp
->st_vfs_file
;
2720 status
= nfserr_grace
;
2721 if (nfs4_in_grace() && !lock
->lk_reclaim
)
2723 status
= nfserr_no_grace
;
2724 if (!nfs4_in_grace() && lock
->lk_reclaim
)
2727 locks_init_lock(&file_lock
);
2728 switch (lock
->lk_type
) {
2731 file_lock
.fl_type
= F_RDLCK
;
2734 case NFS4_WRITEW_LT
:
2735 file_lock
.fl_type
= F_WRLCK
;
2738 status
= nfserr_inval
;
2741 file_lock
.fl_owner
= (fl_owner_t
)lock_sop
;
2742 file_lock
.fl_pid
= current
->tgid
;
2743 file_lock
.fl_file
= filp
;
2744 file_lock
.fl_flags
= FL_POSIX
;
2745 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
2747 file_lock
.fl_start
= lock
->lk_offset
;
2748 if ((lock
->lk_length
== ~(u64
)0) ||
2749 LOFF_OVERFLOW(lock
->lk_offset
, lock
->lk_length
))
2750 file_lock
.fl_end
= ~(u64
)0;
2752 file_lock
.fl_end
= lock
->lk_offset
+ lock
->lk_length
- 1;
2753 nfs4_transform_lock_offset(&file_lock
);
2756 * Try to lock the file in the VFS.
2757 * Note: locks.c uses the BKL to protect the inode's lock list.
2760 /* XXX?: Just to divert the locks_release_private at the start of
2761 * locks_copy_lock: */
2762 conflock
.fl_ops
= NULL
;
2763 conflock
.fl_lmops
= NULL
;
2764 status
= posix_lock_file_conf(filp
, &file_lock
, &conflock
);
2765 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf status %d\n",status
);
2767 case 0: /* success! */
2768 update_stateid(&lock_stp
->st_stateid
);
2769 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stateid
,
2772 case (EAGAIN
): /* conflock holds conflicting lock */
2773 status
= nfserr_denied
;
2774 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2775 nfs4_set_lock_denied(&conflock
, &lock
->lk_denied
);
2778 status
= nfserr_deadlock
;
2781 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf() failed! status %d\n",status
);
2782 status
= nfserr_resource
;
2786 if (status
&& lock
->lk_is_new
&& lock_sop
)
2787 release_stateowner(lock_sop
);
2788 if (lock
->lk_replay_owner
) {
2789 nfs4_get_stateowner(lock
->lk_replay_owner
);
2790 *replay_owner
= lock
->lk_replay_owner
;
2792 nfs4_unlock_state();
2800 nfsd4_lockt(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_lockt
*lockt
)
2802 struct inode
*inode
;
2804 struct file_lock file_lock
;
2805 struct file_lock conflock
;
2808 if (nfs4_in_grace())
2809 return nfserr_grace
;
2811 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
2812 return nfserr_inval
;
2814 lockt
->lt_stateowner
= NULL
;
2817 status
= nfserr_stale_clientid
;
2818 if (STALE_CLIENTID(&lockt
->lt_clientid
))
2821 if ((status
= fh_verify(rqstp
, current_fh
, S_IFREG
, 0))) {
2822 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2823 if (status
== nfserr_symlink
)
2824 status
= nfserr_inval
;
2828 inode
= current_fh
->fh_dentry
->d_inode
;
2829 locks_init_lock(&file_lock
);
2830 switch (lockt
->lt_type
) {
2833 file_lock
.fl_type
= F_RDLCK
;
2836 case NFS4_WRITEW_LT
:
2837 file_lock
.fl_type
= F_WRLCK
;
2840 printk("NFSD: nfs4_lockt: bad lock type!\n");
2841 status
= nfserr_inval
;
2845 lockt
->lt_stateowner
= find_lockstateowner_str(inode
,
2846 &lockt
->lt_clientid
, &lockt
->lt_owner
);
2847 if (lockt
->lt_stateowner
)
2848 file_lock
.fl_owner
= (fl_owner_t
)lockt
->lt_stateowner
;
2849 file_lock
.fl_pid
= current
->tgid
;
2850 file_lock
.fl_flags
= FL_POSIX
;
2851 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
2853 file_lock
.fl_start
= lockt
->lt_offset
;
2854 if ((lockt
->lt_length
== ~(u64
)0) || LOFF_OVERFLOW(lockt
->lt_offset
, lockt
->lt_length
))
2855 file_lock
.fl_end
= ~(u64
)0;
2857 file_lock
.fl_end
= lockt
->lt_offset
+ lockt
->lt_length
- 1;
2859 nfs4_transform_lock_offset(&file_lock
);
2861 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2862 * since LOCKT doesn't require an OPEN, and therefore a struct
2863 * file may not exist, pass posix_test_lock a struct file with
2864 * only the dentry:inode set.
2866 memset(&file
, 0, sizeof (struct file
));
2867 file
.f_dentry
= current_fh
->fh_dentry
;
2870 if (posix_test_lock(&file
, &file_lock
, &conflock
)) {
2871 status
= nfserr_denied
;
2872 nfs4_set_lock_denied(&conflock
, &lockt
->lt_denied
);
2875 nfs4_unlock_state();
2880 nfsd4_locku(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_locku
*locku
, struct nfs4_stateowner
**replay_owner
)
2882 struct nfs4_stateid
*stp
;
2883 struct file
*filp
= NULL
;
2884 struct file_lock file_lock
;
2887 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2888 (long long) locku
->lu_offset
,
2889 (long long) locku
->lu_length
);
2891 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
2892 return nfserr_inval
;
2896 if ((status
= nfs4_preprocess_seqid_op(current_fh
,
2899 CHECK_FH
| LOCK_STATE
,
2900 &locku
->lu_stateowner
, &stp
, NULL
)))
2903 filp
= stp
->st_vfs_file
;
2905 locks_init_lock(&file_lock
);
2906 file_lock
.fl_type
= F_UNLCK
;
2907 file_lock
.fl_owner
= (fl_owner_t
) locku
->lu_stateowner
;
2908 file_lock
.fl_pid
= current
->tgid
;
2909 file_lock
.fl_file
= filp
;
2910 file_lock
.fl_flags
= FL_POSIX
;
2911 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
2912 file_lock
.fl_start
= locku
->lu_offset
;
2914 if ((locku
->lu_length
== ~(u64
)0) || LOFF_OVERFLOW(locku
->lu_offset
, locku
->lu_length
))
2915 file_lock
.fl_end
= ~(u64
)0;
2917 file_lock
.fl_end
= locku
->lu_offset
+ locku
->lu_length
- 1;
2918 nfs4_transform_lock_offset(&file_lock
);
2921 * Try to unlock the file in the VFS.
2923 status
= posix_lock_file(filp
, &file_lock
);
2925 dprintk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2929 * OK, unlock succeeded; the only thing left to do is update the stateid.
2931 update_stateid(&stp
->st_stateid
);
2932 memcpy(&locku
->lu_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2935 if (locku
->lu_stateowner
) {
2936 nfs4_get_stateowner(locku
->lu_stateowner
);
2937 *replay_owner
= locku
->lu_stateowner
;
2939 nfs4_unlock_state();
2943 status
= nfserrno(status
);
2949 * 1: locks held by lockowner
2950 * 0: no locks held by lockowner
2953 check_for_locks(struct file
*filp
, struct nfs4_stateowner
*lowner
)
2955 struct file_lock
**flpp
;
2956 struct inode
*inode
= filp
->f_dentry
->d_inode
;
2960 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
2961 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
2972 nfsd4_release_lockowner(struct svc_rqst
*rqstp
, struct nfsd4_release_lockowner
*rlockowner
)
2974 clientid_t
*clid
= &rlockowner
->rl_clientid
;
2975 struct nfs4_stateowner
*sop
;
2976 struct nfs4_stateid
*stp
;
2977 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
2978 struct list_head matches
;
2982 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2983 clid
->cl_boot
, clid
->cl_id
);
2985 /* XXX check for lease expiration */
2987 status
= nfserr_stale_clientid
;
2988 if (STALE_CLIENTID(clid
))
2993 status
= nfserr_locks_held
;
2994 /* XXX: we're doing a linear search through all the lockowners.
2995 * Yipes! For now we'll just hope clients aren't really using
2996 * release_lockowner much, but eventually we have to fix these
2997 * data structures. */
2998 INIT_LIST_HEAD(&matches
);
2999 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
3000 list_for_each_entry(sop
, &lock_ownerid_hashtbl
[i
], so_idhash
) {
3001 if (!cmp_owner_str(sop
, owner
, clid
))
3003 list_for_each_entry(stp
, &sop
->so_stateids
,
3005 if (check_for_locks(stp
->st_vfs_file
, sop
))
3007 /* Note: so_perclient unused for lockowners,
3008 * so it's OK to fool with here. */
3009 list_add(&sop
->so_perclient
, &matches
);
3013 /* Clients probably won't expect us to return with some (but not all)
3014 * of the lockowner state released; so don't release any until all
3015 * have been checked. */
3017 while (!list_empty(&matches
)) {
3018 sop
= list_entry(matches
.next
, struct nfs4_stateowner
,
3020 /* unhash_stateowner deletes so_perclient only
3021 * for openowners. */
3022 list_del(&sop
->so_perclient
);
3023 release_stateowner(sop
);
3026 nfs4_unlock_state();
3030 static inline struct nfs4_client_reclaim
*
3033 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
3037 nfs4_has_reclaimed_state(const char *name
)
3039 unsigned int strhashval
= clientstr_hashval(name
);
3040 struct nfs4_client
*clp
;
3042 clp
= find_confirmed_client_by_str(name
, strhashval
);
3047 * failure => all reset bets are off, nfserr_no_grace...
3050 nfs4_client_to_reclaim(const char *name
)
3052 unsigned int strhashval
;
3053 struct nfs4_client_reclaim
*crp
= NULL
;
3055 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
3056 crp
= alloc_reclaim();
3059 strhashval
= clientstr_hashval(name
);
3060 INIT_LIST_HEAD(&crp
->cr_strhash
);
3061 list_add(&crp
->cr_strhash
, &reclaim_str_hashtbl
[strhashval
]);
3062 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
3063 reclaim_str_hashtbl_size
++;
3068 nfs4_release_reclaim(void)
3070 struct nfs4_client_reclaim
*crp
= NULL
;
3073 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
3074 while (!list_empty(&reclaim_str_hashtbl
[i
])) {
3075 crp
= list_entry(reclaim_str_hashtbl
[i
].next
,
3076 struct nfs4_client_reclaim
, cr_strhash
);
3077 list_del(&crp
->cr_strhash
);
3079 reclaim_str_hashtbl_size
--;
3082 BUG_ON(reclaim_str_hashtbl_size
);
3086 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3087 static struct nfs4_client_reclaim
*
3088 nfs4_find_reclaim_client(clientid_t
*clid
)
3090 unsigned int strhashval
;
3091 struct nfs4_client
*clp
;
3092 struct nfs4_client_reclaim
*crp
= NULL
;
3095 /* find clientid in conf_id_hashtbl */
3096 clp
= find_confirmed_client(clid
);
3100 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3101 clp
->cl_name
.len
, clp
->cl_name
.data
,
3104 /* find clp->cl_name in reclaim_str_hashtbl */
3105 strhashval
= clientstr_hashval(clp
->cl_recdir
);
3106 list_for_each_entry(crp
, &reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
3107 if (same_name(crp
->cr_recdir
, clp
->cl_recdir
)) {
3115 * Called from OPEN. Look for clientid in reclaim list.
3118 nfs4_check_open_reclaim(clientid_t
*clid
)
3120 return nfs4_find_reclaim_client(clid
) ? nfs_ok
: nfserr_reclaim_bad
;
3123 /* initialization to perform at module load time: */
3126 nfs4_state_init(void)
3130 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
3131 INIT_LIST_HEAD(&conf_id_hashtbl
[i
]);
3132 INIT_LIST_HEAD(&conf_str_hashtbl
[i
]);
3133 INIT_LIST_HEAD(&unconf_str_hashtbl
[i
]);
3134 INIT_LIST_HEAD(&unconf_id_hashtbl
[i
]);
3136 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
3137 INIT_LIST_HEAD(&file_hashtbl
[i
]);
3139 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++) {
3140 INIT_LIST_HEAD(&ownerstr_hashtbl
[i
]);
3141 INIT_LIST_HEAD(&ownerid_hashtbl
[i
]);
3143 for (i
= 0; i
< STATEID_HASH_SIZE
; i
++) {
3144 INIT_LIST_HEAD(&stateid_hashtbl
[i
]);
3145 INIT_LIST_HEAD(&lockstateid_hashtbl
[i
]);
3147 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
3148 INIT_LIST_HEAD(&lock_ownerid_hashtbl
[i
]);
3149 INIT_LIST_HEAD(&lock_ownerstr_hashtbl
[i
]);
3151 memset(&onestateid
, ~0, sizeof(stateid_t
));
3152 INIT_LIST_HEAD(&close_lru
);
3153 INIT_LIST_HEAD(&client_lru
);
3154 INIT_LIST_HEAD(&del_recall_lru
);
3155 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++)
3156 INIT_LIST_HEAD(&reclaim_str_hashtbl
[i
]);
3157 reclaim_str_hashtbl_size
= 0;
3161 nfsd4_load_reboot_recovery_data(void)
3166 nfsd4_init_recdir(user_recovery_dirname
);
3167 status
= nfsd4_recdir_load();
3168 nfs4_unlock_state();
3170 printk("NFSD: Failure reading reboot recovery data\n");
3173 /* initialization to perform when the nfsd service is started: */
3176 __nfs4_state_start(void)
3180 boot_time
= get_seconds();
3181 grace_time
= max(user_lease_time
, lease_time
);
3182 lease_time
= user_lease_time
;
3184 printk("NFSD: starting %ld-second grace period\n", grace_time
);
3185 laundry_wq
= create_singlethread_workqueue("nfsd4");
3186 queue_delayed_work(laundry_wq
, &laundromat_work
, grace_time
*HZ
);
3190 nfs4_state_start(void)
3196 status
= nfsd4_init_slabs();
3199 nfsd4_load_reboot_recovery_data();
3200 __nfs4_state_start();
3212 nfs4_lease_time(void)
3218 __nfs4_state_shutdown(void)
3221 struct nfs4_client
*clp
= NULL
;
3222 struct nfs4_delegation
*dp
= NULL
;
3223 struct list_head
*pos
, *next
, reaplist
;
3225 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
3226 while (!list_empty(&conf_id_hashtbl
[i
])) {
3227 clp
= list_entry(conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
3230 while (!list_empty(&unconf_str_hashtbl
[i
])) {
3231 clp
= list_entry(unconf_str_hashtbl
[i
].next
, struct nfs4_client
, cl_strhash
);
3235 INIT_LIST_HEAD(&reaplist
);
3236 spin_lock(&recall_lock
);
3237 list_for_each_safe(pos
, next
, &del_recall_lru
) {
3238 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3239 list_move(&dp
->dl_recall_lru
, &reaplist
);
3241 spin_unlock(&recall_lock
);
3242 list_for_each_safe(pos
, next
, &reaplist
) {
3243 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3244 list_del_init(&dp
->dl_recall_lru
);
3245 unhash_delegation(dp
);
3248 cancel_delayed_work(&laundromat_work
);
3249 nfsd4_shutdown_recdir();
3254 nfs4_state_shutdown(void)
3256 cancel_rearming_delayed_workqueue(laundry_wq
, &laundromat_work
);
3257 destroy_workqueue(laundry_wq
);
3259 nfs4_release_reclaim();
3260 __nfs4_state_shutdown();
3262 nfs4_unlock_state();
3266 nfs4_set_recdir(char *recdir
)
3269 strcpy(user_recovery_dirname
, recdir
);
3270 nfs4_unlock_state();
3274 * Change the NFSv4 recovery directory to recdir.
3277 nfs4_reset_recoverydir(char *recdir
)
3280 struct nameidata nd
;
3282 status
= path_lookup(recdir
, LOOKUP_FOLLOW
, &nd
);
3286 if (S_ISDIR(nd
.dentry
->d_inode
->i_mode
)) {
3287 nfs4_set_recdir(recdir
);
3295 * Called when leasetime is changed.
3297 * The only way the protocol gives us to handle on-the-fly lease changes is to
3298 * simulate a reboot. Instead of doing that, we just wait till the next time
3299 * we start to register any changes in lease time. If the administrator
3300 * really wants to change the lease time *now*, they can go ahead and bring
3301 * nfsd down and then back up again after changing the lease time.
3304 nfs4_reset_lease(time_t leasetime
)
3307 user_lease_time
= leasetime
;