blob: e42c7a0eb6facfd28fbf5acc16d903ebbda776b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2* linux/fs/nfsd/nfs4state.c
3*
4* Copyright (c) 2001 The Regents of the University of Michigan.
5* All rights reserved.
6*
7* Kendrick Smith <kmsmith@umich.edu>
8* Andy Adamson <kandros@umich.edu>
9*
10* Redistribution and use in source and binary forms, with or without
11* modification, are permitted provided that the following conditions
12* are met:
13*
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.
22*
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.
34*
35*/
36
37#include <linux/param.h>
38#include <linux/major.h>
39#include <linux/slab.h>
40
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>
NeilBrown0964a3d2005-06-23 22:04:32 -070051#include <linux/namei.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080052#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define NFSDDBG_FACILITY NFSDDBG_PROC
55
56/* Globals */
57static time_t lease_time = 90; /* default lease time */
NeilBrownd99a05ad2005-06-23 22:03:21 -070058static time_t user_lease_time = 90;
NeilBrownfd39ca92005-06-23 22:04:03 -070059static time_t boot_time;
NeilBrowna76b4312005-06-23 22:04:01 -070060static int in_grace = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static u32 current_clientid = 1;
62static u32 current_ownerid = 1;
63static u32 current_fileid = 1;
64static u32 current_delegid = 1;
65static u32 nfs4_init;
NeilBrownfd39ca92005-06-23 22:04:03 -070066static stateid_t zerostateid; /* bits all 0 */
67static stateid_t onestateid; /* bits all 1 */
68
69#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
70#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
NeilBrownfd39ca92005-06-23 22:04:03 -070073static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
75static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
NeilBrown0964a3d2005-06-23 22:04:32 -070076static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
77static void nfs4_set_recdir(char *recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* Locking:
80 *
Ingo Molnar353ab6e2006-03-26 01:37:12 -080081 * client_mutex:
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * protects clientid_hashtbl[], clientstr_hashtbl[],
83 * unconfstr_hashtbl[], uncofid_hashtbl[].
84 */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080085static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Christoph Lametere18b8902006-12-06 20:33:20 -080087static struct kmem_cache *stateowner_slab = NULL;
88static struct kmem_cache *file_slab = NULL;
89static struct kmem_cache *stateid_slab = NULL;
90static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092void
93nfs4_lock_state(void)
94{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080095 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98void
99nfs4_unlock_state(void)
100{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800101 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104static inline u32
105opaque_hashval(const void *ptr, int nbytes)
106{
107 unsigned char *cptr = (unsigned char *) ptr;
108
109 u32 x = 0;
110 while (nbytes--) {
111 x *= 37;
112 x += *cptr++;
113 }
114 return x;
115}
116
117/* forward declarations */
118static void release_stateowner(struct nfs4_stateowner *sop);
119static void release_stateid(struct nfs4_stateid *stp, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/*
122 * Delegation state
123 */
124
125/* recall_lock protects the del_recall_lru */
Ingo Molnar34af9462006-06-27 02:53:55 -0700126static DEFINE_SPINLOCK(recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static struct list_head del_recall_lru;
128
NeilBrown13cd2182005-06-23 22:03:10 -0700129static void
130free_nfs4_file(struct kref *kref)
131{
132 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
133 list_del(&fp->fi_hash);
134 iput(fp->fi_inode);
135 kmem_cache_free(file_slab, fp);
136}
137
138static inline void
139put_nfs4_file(struct nfs4_file *fi)
140{
141 kref_put(&fi->fi_ref, free_nfs4_file);
142}
143
144static inline void
145get_nfs4_file(struct nfs4_file *fi)
146{
147 kref_get(&fi->fi_ref);
148}
149
NeilBrownef0f3392006-04-10 22:55:41 -0700150static int num_delegations;
151
152/*
153 * Open owner state (share locks)
154 */
155
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)
160
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)
165
166static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
167static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
168
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)
177
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)
182
183static struct list_head file_hashtbl[FILE_HASH_SIZE];
184static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static struct nfs4_delegation *
187alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
188{
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;
192
193 dprintk("NFSD alloc_init_deleg\n");
NeilBrownef0f3392006-04-10 22:55:41 -0700194 if (num_delegations > STATEID_HASH_SIZE * 4)
195 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700196 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
197 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700199 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700200 INIT_LIST_HEAD(&dp->dl_perfile);
201 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 INIT_LIST_HEAD(&dp->dl_recall_lru);
203 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700204 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 dp->dl_file = fp;
206 dp->dl_flock = NULL;
207 get_file(stp->st_vfs_file);
208 dp->dl_vfs_file = stp->st_vfs_file;
209 dp->dl_type = type;
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, &current_fh->fh_handle.fh_base,
219 current_fh->fh_handle.fh_size);
220 dp->dl_time = 0;
221 atomic_set(&dp->dl_count, 1);
NeilBrownea1da632005-06-23 22:04:17 -0700222 list_add(&dp->dl_perfile, &fp->fi_delegations);
223 list_add(&dp->dl_perclnt, &clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return dp;
225}
226
227void
228nfs4_put_delegation(struct nfs4_delegation *dp)
229{
230 if (atomic_dec_and_test(&dp->dl_count)) {
231 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700232 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700233 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700234 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236}
237
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.
242 */
243static void
244nfs4_close_delegation(struct nfs4_delegation *dp)
245{
246 struct file *filp = dp->dl_vfs_file;
247
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. */
NeilBrownc9071322005-04-16 15:26:38 -0700252 if (dp->dl_flock)
253 setlease(filp, F_UNLCK, &dp->dl_flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257/* Called under the state lock. */
258static void
259unhash_delegation(struct nfs4_delegation *dp)
260{
NeilBrownea1da632005-06-23 22:04:17 -0700261 list_del_init(&dp->dl_perfile);
262 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 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);
268}
269
270/*
271 * SETCLIENTID state
272 */
273
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)
278
279#define clientid_hashval(id) \
280 ((id) & CLIENT_HASH_MASK)
NeilBrowna55370a2005-06-23 22:03:52 -0700281#define clientstr_hashval(name) \
282 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/*
284 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
285 * used in reboot/reset lease grace period processing
286 *
287 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
288 * setclientid_confirmed info.
289 *
290 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
291 * setclientid info.
292 *
293 * client_lru holds client queue ordered by nfs4_client.cl_time
294 * for lease renewal.
295 *
296 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
297 * for last close replay.
298 */
299static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
300static int reclaim_str_hashtbl_size = 0;
301static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
302static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
303static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
304static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
305static struct list_head client_lru;
306static struct list_head close_lru;
307
308static inline void
309renew_client(struct nfs4_client *clp)
310{
311 /*
312 * Move client to the end to the LRU list.
313 */
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();
319}
320
321/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
322static int
323STALE_CLIENTID(clientid_t *clid)
324{
325 if (clid->cl_boot == boot_time)
326 return 0;
327 dprintk("NFSD stale clientid (%08x/%08x)\n",
328 clid->cl_boot, clid->cl_id);
329 return 1;
330}
331
332/*
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.
336 */
337static inline struct nfs4_client *
338alloc_client(struct xdr_netobj name)
339{
340 struct nfs4_client *clp;
341
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700342 if ((clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 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;
346 }
347 else {
348 kfree(clp);
349 clp = NULL;
350 }
351 }
352 return clp;
353}
354
355static inline void
356free_client(struct nfs4_client *clp)
357{
358 if (clp->cl_cred.cr_group_info)
359 put_group_info(clp->cl_cred.cr_group_info);
360 kfree(clp->cl_name.data);
361 kfree(clp);
362}
363
364void
365put_nfs4_client(struct nfs4_client *clp)
366{
367 if (atomic_dec_and_test(&clp->cl_count))
368 free_client(clp);
369}
370
371static void
NeilBrown4e2fd492006-04-10 22:55:39 -0700372shutdown_callback_client(struct nfs4_client *clp)
373{
374 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
375
376 /* shutdown rpc client, ending any outstanding recall rpcs */
377 if (clnt) {
378 clp->cl_callback.cb_client = NULL;
379 rpc_shutdown_client(clnt);
380 rpciod_down();
381 }
382}
383
384static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385expire_client(struct nfs4_client *clp)
386{
387 struct nfs4_stateowner *sop;
388 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct list_head reaplist;
390
391 dprintk("NFSD: expire_client cl_count %d\n",
392 atomic_read(&clp->cl_count));
393
NeilBrown4e2fd492006-04-10 22:55:39 -0700394 shutdown_callback_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 INIT_LIST_HEAD(&reaplist);
397 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700398 while (!list_empty(&clp->cl_delegations)) {
399 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
401 dp->dl_flock);
NeilBrownea1da632005-06-23 22:04:17 -0700402 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 list_move(&dp->dl_recall_lru, &reaplist);
404 }
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);
410 }
411 list_del(&clp->cl_idhash);
412 list_del(&clp->cl_strhash);
413 list_del(&clp->cl_lru);
NeilBrownea1da632005-06-23 22:04:17 -0700414 while (!list_empty(&clp->cl_openowners)) {
415 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 release_stateowner(sop);
417 }
418 put_nfs4_client(clp);
419}
420
421static struct nfs4_client *
NeilBrowna55370a2005-06-23 22:03:52 -0700422create_client(struct xdr_netobj name, char *recdir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct nfs4_client *clp;
424
425 if (!(clp = alloc_client(name)))
426 goto out;
NeilBrowna55370a2005-06-23 22:03:52 -0700427 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 atomic_set(&clp->cl_count, 1);
429 atomic_set(&clp->cl_callback.cb_set, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 INIT_LIST_HEAD(&clp->cl_idhash);
431 INIT_LIST_HEAD(&clp->cl_strhash);
NeilBrownea1da632005-06-23 22:04:17 -0700432 INIT_LIST_HEAD(&clp->cl_openowners);
433 INIT_LIST_HEAD(&clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 INIT_LIST_HEAD(&clp->cl_lru);
435out:
436 return clp;
437}
438
439static void
440copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
441 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
442}
443
444static void
445copy_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;
448}
449
450static void
451copy_cred(struct svc_cred *target, struct svc_cred *source) {
452
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);
457}
458
NeilBrowna55370a2005-06-23 22:03:52 -0700459static inline int
460same_name(const char *n1, const char *n2) {
461 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464static int
465cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
466 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
467}
468
469static int
470cmp_clid(clientid_t * cl1, clientid_t * cl2) {
471 return((cl1->cl_boot == cl2->cl_boot) &&
472 (cl1->cl_id == cl2->cl_id));
473}
474
475/* XXX what about NGROUP */
476static int
477cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
478 return(cr1->cr_uid == cr2->cr_uid);
479
480}
481
482static void
483gen_clid(struct nfs4_client *clp) {
484 clp->cl_clientid.cl_boot = boot_time;
485 clp->cl_clientid.cl_id = current_clientid++;
486}
487
488static void
489gen_confirm(struct nfs4_client *clp) {
490 struct timespec tv;
491 u32 * p;
492
493 tv = CURRENT_TIME;
494 p = (u32 *)clp->cl_confirm.data;
495 *p++ = tv.tv_sec;
496 *p++ = tv.tv_nsec;
497}
498
499static int
500check_name(struct xdr_netobj name) {
501
502 if (name.len == 0)
503 return 0;
504 if (name.len > NFS4_OPAQUE_LIMIT) {
505 printk("NFSD: check_name: name too long(%d)!\n", name.len);
506 return 0;
507 }
508 return 1;
509}
510
NeilBrownfd39ca92005-06-23 22:04:03 -0700511static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
513{
514 unsigned int idhashval;
515
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();
521}
522
NeilBrownfd39ca92005-06-23 22:04:03 -0700523static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524move_to_confirmed(struct nfs4_client *clp)
525{
526 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
527 unsigned int strhashval;
528
529 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
530 list_del_init(&clp->cl_strhash);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700531 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -0700532 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
534 renew_client(clp);
535}
536
537static struct nfs4_client *
538find_confirmed_client(clientid_t *clid)
539{
540 struct nfs4_client *clp;
541 unsigned int idhashval = clientid_hashval(clid->cl_id);
542
543 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
544 if (cmp_clid(&clp->cl_clientid, clid))
545 return clp;
546 }
547 return NULL;
548}
549
550static struct nfs4_client *
551find_unconfirmed_client(clientid_t *clid)
552{
553 struct nfs4_client *clp;
554 unsigned int idhashval = clientid_hashval(clid->cl_id);
555
556 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
557 if (cmp_clid(&clp->cl_clientid, clid))
558 return clp;
559 }
560 return NULL;
561}
562
NeilBrown28ce6052005-06-23 22:03:56 -0700563static struct nfs4_client *
564find_confirmed_client_by_str(const char *dname, unsigned int hashval)
565{
566 struct nfs4_client *clp;
567
568 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
569 if (same_name(clp->cl_recdir, dname))
570 return clp;
571 }
572 return NULL;
573}
574
575static struct nfs4_client *
576find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
577{
578 struct nfs4_client *clp;
579
580 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
581 if (same_name(clp->cl_recdir, dname))
582 return clp;
583 }
584 return NULL;
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/* a helper function for parse_callback */
588static int
589parse_octet(unsigned int *lenp, char **addrp)
590{
591 unsigned int len = *lenp;
592 char *p = *addrp;
593 int n = -1;
594 char c;
595
596 for (;;) {
597 if (!len)
598 break;
599 len--;
600 c = *p++;
601 if (c == '.')
602 break;
603 if ((c < '0') || (c > '9')) {
604 n = -1;
605 break;
606 }
607 if (n < 0)
608 n = 0;
609 n = (n * 10) + (c - '0');
610 if (n > 255) {
611 n = -1;
612 break;
613 }
614 }
615 *lenp = len;
616 *addrp = p;
617 return n;
618}
619
620/* parse and set the setclientid ipv4 callback address */
NeilBrownfd39ca92005-06-23 22:04:03 -0700621static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
623{
624 int temp = 0;
625 u32 cbaddr = 0;
626 u16 cbport = 0;
627 u32 addrlen = addr_len;
628 char *addr = addr_val;
629 int i, shift;
630
631 /* ipaddress */
632 shift = 24;
633 for(i = 4; i > 0 ; i--) {
634 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
635 return 0;
636 }
637 cbaddr |= (temp << shift);
638 if (shift > 0)
639 shift -= 8;
640 }
641 *cbaddrp = cbaddr;
642
643 /* port */
644 shift = 8;
645 for(i = 2; i > 0 ; i--) {
646 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
647 return 0;
648 }
649 cbport |= (temp << shift);
650 if (shift > 0)
651 shift -= 8;
652 }
653 *cbportp = cbport;
654 return 1;
655}
656
NeilBrownfd39ca92005-06-23 22:04:03 -0700657static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
659{
660 struct nfs4_callback *cb = &clp->cl_callback;
661
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))
664 goto out_err;
665
666 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
667 &cb->cb_addr, &cb->cb_port)))
668 goto out_err;
669 cb->cb_prog = se->se_callback_prog;
670 cb->cb_ident = se->se_callback_ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return;
672out_err:
Neil Brown849823c2005-09-13 01:25:36 -0700673 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 "will not receive delegations\n",
675 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return;
678}
679
680/*
681 * RFC 3010 has a complex implmentation description of processing a
682 * SETCLIENTID request consisting of 5 bullets, labeled as
683 * CASE0 - CASE4 below.
684 *
685 * NOTES:
686 * callback information will be processed in a future patch
687 *
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
694 * name and principal
695 *
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.
704 *
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.
711 *
712 */
Al Virob37ad282006-10-19 23:28:59 -0700713__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800714nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
715 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Chuck Lever27459f02007-02-12 00:53:34 -0800717 struct sockaddr_in *sin = svc_addr_in(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct xdr_netobj clname = {
719 .len = setclid->se_namelen,
720 .data = setclid->se_name,
721 };
722 nfs4_verifier clverifier = setclid->se_verf;
723 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -0700724 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -0700725 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -0700726 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -0700729 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
NeilBrowna55370a2005-06-23 22:03:52 -0700731 status = nfs4_make_rec_clidname(dname, &clname);
732 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -0700733 return status;
NeilBrowna55370a2005-06-23 22:03:52 -0700734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /*
736 * XXX The Duplicate Request Cache (DRC) has been checked (??)
737 * We get here on a DRC miss.
738 */
739
NeilBrowna55370a2005-06-23 22:03:52 -0700740 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 nfs4_lock_state();
NeilBrown28ce6052005-06-23 22:03:56 -0700743 conf = find_confirmed_client_by_str(dname, strhashval);
744 if (conf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /*
746 * CASE 0:
747 * clname match, confirmed, different principal
748 * or different ip_address
749 */
750 status = nfserr_clid_inuse;
NeilBrown28ce6052005-06-23 22:03:56 -0700751 if (!cmp_creds(&conf->cl_cred, &rqstp->rq_cred)
Chuck Lever27459f02007-02-12 00:53:34 -0800752 || conf->cl_addr != sin->sin_addr.s_addr) {
Bruce Fields21315ed2007-03-26 21:32:09 -0800753 dprintk("NFSD: setclientid: string in use by client"
754 "at %u.%u.%u.%u\n", NIPQUAD(conf->cl_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 goto out;
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
NeilBrown28ce6052005-06-23 22:03:56 -0700758 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 status = nfserr_resource;
760 if (!conf) {
761 /*
762 * CASE 4:
763 * placed first, because it is the normal case.
764 */
765 if (unconf)
766 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700767 new = create_client(clname, dname);
768 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 goto out;
770 copy_verf(new, &clverifier);
Chuck Lever27459f02007-02-12 00:53:34 -0800771 new->cl_addr = sin->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 copy_cred(&new->cl_cred,&rqstp->rq_cred);
773 gen_clid(new);
774 gen_confirm(new);
775 gen_callback(new, setclid);
776 add_to_unconfirmed(new, strhashval);
777 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
778 /*
779 * CASE 1:
780 * cl_name match, confirmed, principal match
781 * verifier match: probable callback update
782 *
783 * remove any unconfirmed nfs4_client with
784 * matching cl_name, cl_verifier, and cl_clientid
785 *
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
789 * new cl_confirm
790 */
NeilBrown31f4a6c2005-06-23 22:04:06 -0700791 if (unconf) {
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.
796 */
797 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
NeilBrowna55370a2005-06-23 22:03:52 -0700799 new = create_client(clname, dname);
800 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto out;
802 copy_verf(new,&conf->cl_verifier);
Chuck Lever27459f02007-02-12 00:53:34 -0800803 new->cl_addr = sin->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 copy_cred(&new->cl_cred,&rqstp->rq_cred);
805 copy_clid(new, conf);
806 gen_confirm(new);
807 gen_callback(new, setclid);
808 add_to_unconfirmed(new,strhashval);
809 } else if (!unconf) {
810 /*
811 * CASE 2:
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.
817 */
NeilBrowna55370a2005-06-23 22:03:52 -0700818 new = create_client(clname, dname);
819 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 goto out;
821 copy_verf(new,&clverifier);
Chuck Lever27459f02007-02-12 00:53:34 -0800822 new->cl_addr = sin->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 copy_cred(&new->cl_cred,&rqstp->rq_cred);
824 gen_clid(new);
825 gen_confirm(new);
826 gen_callback(new, setclid);
827 add_to_unconfirmed(new, strhashval);
828 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
829 /*
830 * CASE3:
831 * confirmed found (name, principal match)
832 * confirmed verifier does not match input clverifier
833 *
834 * unconfirmed found (name match)
835 * confirmed->cl_confirm != unconfirmed->cl_confirm
836 *
837 * remove unconfirmed.
838 *
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
843 */
844 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700845 new = create_client(clname, dname);
846 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto out;
848 copy_verf(new,&clverifier);
Chuck Lever27459f02007-02-12 00:53:34 -0800849 new->cl_addr = sin->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 copy_cred(&new->cl_cred,&rqstp->rq_cred);
851 gen_clid(new);
852 gen_confirm(new);
853 gen_callback(new, setclid);
854 add_to_unconfirmed(new, strhashval);
855 } else {
856 /* No cases hit !!! */
857 status = nfserr_inval;
858 goto out;
859
860 }
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));
864 status = nfs_ok;
865out:
866 nfs4_unlock_state();
867 return status;
868}
869
870
871/*
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.
875 *
876 * NOTE: callback information will be processed here in a future patch
877 */
Al Virob37ad282006-10-19 23:28:59 -0700878__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800879nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
880 struct nfsd4_compound_state *cstate,
881 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Chuck Lever27459f02007-02-12 00:53:34 -0800883 struct sockaddr_in *sin = svc_addr_in(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -0700884 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
886 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -0700887 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (STALE_CLIENTID(clid))
890 return nfserr_stale_clientid;
891 /*
892 * XXX The Duplicate Request Cache (DRC) has been checked (??)
893 * We get here on a DRC miss.
894 */
895
896 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -0700897
898 conf = find_confirmed_client(clid);
899 unconf = find_unconfirmed_client(clid);
900
901 status = nfserr_clid_inuse;
Chuck Lever27459f02007-02-12 00:53:34 -0800902 if (conf && conf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -0700903 goto out;
Chuck Lever27459f02007-02-12 00:53:34 -0800904 if (unconf && unconf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -0700905 goto out;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if ((conf && unconf) &&
908 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
909 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
NeilBrowna55370a2005-06-23 22:03:52 -0700910 (same_name(conf->cl_recdir,unconf->cl_recdir)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
NeilBrown7c79f732005-06-23 22:04:13 -0700912 /* CASE 1:
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
916 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
918 status = nfserr_clid_inuse;
919 else {
NeilBrown1a69c172005-06-23 22:04:08 -0700920 /* XXX: We just turn off callbacks until we can handle
921 * change request correctly. */
NeilBrowncb36d632005-06-23 22:04:23 -0700922 atomic_set(&conf->cl_callback.cb_set, 0);
NeilBrown21ab45a2005-06-23 22:04:14 -0700923 gen_confirm(conf);
NeilBrown46309022005-07-07 17:59:10 -0700924 nfsd4_remove_clid_dir(unconf);
NeilBrown1a69c172005-06-23 22:04:08 -0700925 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
NeilBrown7c79f732005-06-23 22:04:13 -0700929 } else if ((conf && !unconf) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 ((conf && unconf) &&
931 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
NeilBrowna55370a2005-06-23 22:03:52 -0700932 !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
NeilBrown7c79f732005-06-23 22:04:13 -0700933 /* CASE 2:
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
937 * conf record.
938 */
NeilBrown21ab45a2005-06-23 22:04:14 -0700939 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -0700941 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -0700943 } else if (!conf && unconf
944 && cmp_verf(&unconf->cl_confirm, &confirm)) {
945 /* CASE 3:
946 * conf record not found.
947 * unconf record found.
948 * unconf->cl_confirm matches input confirm
949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
951 status = nfserr_clid_inuse;
952 } else {
NeilBrown1a69c172005-06-23 22:04:08 -0700953 unsigned int hash =
954 clientstr_hashval(unconf->cl_recdir);
955 conf = find_confirmed_client_by_str(unconf->cl_recdir,
956 hash);
957 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -0700958 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -0700959 expire_client(conf);
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -0700962 conf = unconf;
NeilBrown1a69c172005-06-23 22:04:08 -0700963 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
NeilBrown7c79f732005-06-23 22:04:13 -0700965 } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm)))
966 && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm,
967 &confirm)))) {
968 /* CASE 4:
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.
973 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -0700975 } else {
NeilBrown08e89872005-06-23 22:04:11 -0700976 /* check that we have hit one of the cases...*/
977 status = nfserr_clid_inuse;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979out:
980 if (!status)
NeilBrown21ab45a2005-06-23 22:04:14 -0700981 nfsd4_probe_callback(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 nfs4_unlock_state();
983 return status;
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/* OPEN Share state helper functions */
987static inline struct nfs4_file *
988alloc_init_file(struct inode *ino)
989{
990 struct nfs4_file *fp;
991 unsigned int hashval = file_hashval(ino);
992
NeilBrowne60d4392005-06-23 22:03:01 -0700993 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
994 if (fp) {
NeilBrown13cd2182005-06-23 22:03:10 -0700995 kref_init(&fp->fi_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -0700997 INIT_LIST_HEAD(&fp->fi_stateids);
998 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
1000 fp->fi_inode = igrab(ino);
1001 fp->fi_id = current_fileid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return fp;
1003 }
1004 return NULL;
1005}
1006
1007static void
Christoph Lametere18b8902006-12-06 20:33:20 -08001008nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07001009{
NeilBrowne60d4392005-06-23 22:03:01 -07001010 if (*slab == NULL)
1011 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001012 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001013 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07001014}
1015
1016static void
1017nfsd4_free_slabs(void)
1018{
1019 nfsd4_free_slab(&stateowner_slab);
1020 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07001021 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001022 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025static int
1026nfsd4_init_slabs(void)
1027{
1028 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1029 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001030 if (stateowner_slab == NULL)
1031 goto out_nomem;
1032 file_slab = kmem_cache_create("nfsd4_files",
1033 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1034 if (file_slab == NULL)
1035 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07001036 stateid_slab = kmem_cache_create("nfsd4_stateids",
1037 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1038 if (stateid_slab == NULL)
1039 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07001040 deleg_slab = kmem_cache_create("nfsd4_delegations",
1041 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1042 if (deleg_slab == NULL)
1043 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07001045out_nomem:
1046 nfsd4_free_slabs();
1047 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1048 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051void
1052nfs4_free_stateowner(struct kref *kref)
1053{
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);
1058}
1059
1060static inline struct nfs4_stateowner *
1061alloc_stateowner(struct xdr_netobj *owner)
1062{
1063 struct nfs4_stateowner *sop;
1064
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);
1070 return sop;
1071 }
1072 kmem_cache_free(stateowner_slab, sop);
1073 }
1074 return NULL;
1075}
1076
1077static struct nfs4_stateowner *
1078alloc_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;
1082
1083 if (!(sop = alloc_stateowner(&open->op_owner)))
1084 return NULL;
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);
NeilBrownea1da632005-06-23 22:04:17 -07001089 INIT_LIST_HEAD(&sop->so_stateids);
1090 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 INIT_LIST_HEAD(&sop->so_close_lru);
1092 sop->so_time = 0;
1093 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1094 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001095 list_add(&sop->so_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 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;
Al Virode1ae282006-01-18 17:43:47 -08001102 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 rp->rp_buflen = 0;
1104 rp->rp_buf = rp->rp_ibuf;
1105 return sop;
1106}
1107
1108static void
1109release_stateid_lockowners(struct nfs4_stateid *open_stp)
1110{
1111 struct nfs4_stateowner *lock_sop;
1112
NeilBrownea1da632005-06-23 22:04:17 -07001113 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); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 BUG_ON(lock_sop->so_is_open_owner);
1118 release_stateowner(lock_sop);
1119 }
1120}
1121
1122static void
1123unhash_stateowner(struct nfs4_stateowner *sop)
1124{
1125 struct nfs4_stateid *stp;
1126
1127 list_del(&sop->so_idhash);
1128 list_del(&sop->so_strhash);
NeilBrown6fa305d2005-06-23 22:03:06 -07001129 if (sop->so_is_open_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 list_del(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001131 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (sop->so_is_open_owner)
1136 release_stateid(stp, OPEN_STATE);
1137 else
1138 release_stateid(stp, LOCK_STATE);
1139 }
1140}
1141
1142static void
1143release_stateowner(struct nfs4_stateowner *sop)
1144{
1145 unhash_stateowner(sop);
1146 list_del(&sop->so_close_lru);
1147 nfs4_put_stateowner(sop);
1148}
1149
1150static inline void
1151init_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);
1154
1155 INIT_LIST_HEAD(&stp->st_hash);
NeilBrownea1da632005-06-23 22:04:17 -07001156 INIT_LIST_HEAD(&stp->st_perstateowner);
1157 INIT_LIST_HEAD(&stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 INIT_LIST_HEAD(&stp->st_perfile);
1159 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001160 list_add(&stp->st_perstateowner, &sop->so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07001161 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07001163 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 stp->st_file = fp;
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);
NeilBrown4c4cd222005-07-07 17:59:27 -07001173 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176static void
1177release_stateid(struct nfs4_stateid *stp, int flags)
1178{
1179 struct file *filp = stp->st_vfs_file;
1180
1181 list_del(&stp->st_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 list_del(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07001183 list_del(&stp->st_perstateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (flags & OPEN_STATE) {
1185 release_stateid_lockowners(stp);
1186 stp->st_vfs_file = NULL;
1187 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 } else if (flags & LOCK_STATE)
1189 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
NeilBrown13cd2182005-06-23 22:03:10 -07001190 put_nfs4_file(stp->st_file);
NeilBrown5ac049a2005-06-23 22:03:03 -07001191 kmem_cache_free(stateid_slab, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
NeilBrownfd39ca92005-06-23 22:04:03 -07001194static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195move_to_close_lru(struct nfs4_stateowner *sop)
1196{
1197 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1198
NeilBrown358dd552006-04-10 22:55:42 -07001199 list_move_tail(&sop->so_close_lru, &close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 sop->so_time = get_seconds();
1201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203static int
1204cmp_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));
1208}
1209
1210static struct nfs4_stateowner *
1211find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1212{
1213 struct nfs4_stateowner *so = NULL;
1214
1215 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1216 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1217 return so;
1218 }
1219 return NULL;
1220}
1221
1222/* search file_hashtbl[] for file */
1223static struct nfs4_file *
1224find_file(struct inode *ino)
1225{
1226 unsigned int hashval = file_hashval(ino);
1227 struct nfs4_file *fp;
1228
1229 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07001230 if (fp->fi_inode == ino) {
1231 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 return NULL;
1236}
1237
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001238static int access_valid(u32 x)
1239{
1240 return (x > 0 && x < 4);
1241}
1242
1243static int deny_valid(u32 x)
1244{
1245 return (x >= 0 && x < 5);
1246}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
NeilBrownfd39ca92005-06-23 22:04:03 -07001248static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249set_access(unsigned int *access, unsigned long bmap) {
1250 int i;
1251
1252 *access = 0;
1253 for (i = 1; i < 4; i++) {
1254 if (test_bit(i, &bmap))
1255 *access |= i;
1256 }
1257}
1258
NeilBrownfd39ca92005-06-23 22:04:03 -07001259static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260set_deny(unsigned int *deny, unsigned long bmap) {
1261 int i;
1262
1263 *deny = 0;
1264 for (i = 0; i < 4; i++) {
1265 if (test_bit(i, &bmap))
1266 *deny |= i ;
1267 }
1268}
1269
1270static int
1271test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1272 unsigned int access, deny;
1273
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))
1277 return 0;
1278 return 1;
1279}
1280
1281/*
1282 * Called to check deny when READ with all zero stateid or
1283 * WRITE with all zero or all one stateid
1284 */
Al Virob37ad282006-10-19 23:28:59 -07001285static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1287{
1288 struct inode *ino = current_fh->fh_dentry->d_inode;
1289 struct nfs4_file *fp;
1290 struct nfs4_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07001291 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 dprintk("NFSD: nfs4_share_conflict\n");
1294
1295 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07001296 if (!fp)
1297 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07001298 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07001300 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))
1303 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
NeilBrown13cd2182005-06-23 22:03:10 -07001305 ret = nfs_ok;
1306out:
1307 put_nfs4_file(fp);
1308 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311static inline void
1312nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1313{
1314 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001315 put_write_access(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1317 }
1318}
1319
1320/*
1321 * Recall a delegation
1322 */
1323static int
1324do_recall(void *__dp)
1325{
1326 struct nfs4_delegation *dp = __dp;
1327
1328 daemonize("nfsv4-recall");
1329
1330 nfsd4_cb_recall(dp);
1331 return 0;
1332}
1333
1334/*
1335 * Spawn a thread to perform a recall on the delegation represented
1336 * by the lease (file_lock)
1337 *
1338 * Called from break_lease() with lock_kernel() held.
1339 * Note: we assume break_lease will only call this *once* for any given
1340 * lease.
1341 */
1342static
1343void nfsd_break_deleg_cb(struct file_lock *fl)
1344{
1345 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1346 struct task_struct *t;
1347
1348 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1349 if (!dp)
1350 return;
1351
1352 /* We're assuming the state code never drops its reference
1353 * without first removing the lease. Since we're in this lease
1354 * callback (and since the lease code is serialized by the kernel
1355 * lock) we know the server hasn't removed the lease yet, we know
1356 * it's safe to take a reference: */
1357 atomic_inc(&dp->dl_count);
1358
1359 spin_lock(&recall_lock);
1360 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1361 spin_unlock(&recall_lock);
1362
1363 /* only place dl_time is set. protected by lock_kernel*/
1364 dp->dl_time = get_seconds();
1365
1366 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1367 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1368
1369 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1370 if (IS_ERR(t)) {
1371 struct nfs4_client *clp = dp->dl_client;
1372
1373 printk(KERN_INFO "NFSD: Callback thread failed for "
1374 "for client (clientid %08x/%08x)\n",
1375 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1376 nfs4_put_delegation(dp);
1377 }
1378}
1379
1380/*
1381 * The file_lock is being reapd.
1382 *
1383 * Called by locks_free_lock() with lock_kernel() held.
1384 */
1385static
1386void nfsd_release_deleg_cb(struct file_lock *fl)
1387{
1388 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1389
1390 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1391
1392 if (!(fl->fl_flags & FL_LEASE) || !dp)
1393 return;
1394 dp->dl_flock = NULL;
1395}
1396
1397/*
1398 * Set the delegation file_lock back pointer.
1399 *
1400 * Called from __setlease() with lock_kernel() held.
1401 */
1402static
1403void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1404{
1405 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1406
1407 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1408 if (!dp)
1409 return;
1410 dp->dl_flock = new;
1411}
1412
1413/*
1414 * Called from __setlease() with lock_kernel() held
1415 */
1416static
1417int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1418{
1419 struct nfs4_delegation *onlistd =
1420 (struct nfs4_delegation *)onlist->fl_owner;
1421 struct nfs4_delegation *tryd =
1422 (struct nfs4_delegation *)try->fl_owner;
1423
1424 if (onlist->fl_lmops != try->fl_lmops)
1425 return 0;
1426
1427 return onlistd->dl_client == tryd->dl_client;
1428}
1429
1430
1431static
1432int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1433{
1434 if (arg & F_UNLCK)
1435 return lease_modify(onlist, arg);
1436 else
1437 return -EAGAIN;
1438}
1439
NeilBrownfd39ca92005-06-23 22:04:03 -07001440static struct lock_manager_operations nfsd_lease_mng_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 .fl_break = nfsd_break_deleg_cb,
1442 .fl_release_private = nfsd_release_deleg_cb,
1443 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1444 .fl_mylease = nfsd_same_client_deleg_cb,
1445 .fl_change = nfsd_change_deleg_cb,
1446};
1447
1448
Al Virob37ad282006-10-19 23:28:59 -07001449__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450nfsd4_process_open1(struct nfsd4_open *open)
1451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 clientid_t *clientid = &open->op_clientid;
1453 struct nfs4_client *clp = NULL;
1454 unsigned int strhashval;
1455 struct nfs4_stateowner *sop = NULL;
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001458 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 if (STALE_CLIENTID(&open->op_clientid))
1461 return nfserr_stale_clientid;
1462
1463 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1464 sop = find_openstateowner_str(strhashval, open);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001465 open->op_stateowner = sop;
1466 if (!sop) {
1467 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 clp = find_confirmed_client(clientid);
1469 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001470 return nfserr_expired;
1471 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001473 if (!sop->so_confirmed) {
1474 /* Replace unconfirmed owners without checking for replay. */
1475 clp = sop->so_client;
1476 release_stateowner(sop);
1477 open->op_stateowner = NULL;
1478 goto renew;
1479 }
1480 if (open->op_seqid == sop->so_seqid - 1) {
1481 if (sop->so_replay.rp_buflen)
Al Viroa90b0612006-10-19 23:29:03 -07001482 return nfserr_replay_me;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001483 /* The original OPEN failed so spectacularly
1484 * that we don't even have replay data saved!
1485 * Therefore, we have no choice but to continue
1486 * processing this OPEN; presumably, we'll
1487 * fail again for the same reason.
1488 */
1489 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1490 goto renew;
1491 }
1492 if (open->op_seqid != sop->so_seqid)
1493 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494renew:
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001495 if (open->op_stateowner == NULL) {
1496 sop = alloc_init_open_stateowner(strhashval, clp, open);
1497 if (sop == NULL)
1498 return nfserr_resource;
1499 open->op_stateowner = sop;
1500 }
1501 list_del_init(&sop->so_close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 renew_client(sop->so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001503 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Al Virob37ad282006-10-19 23:28:59 -07001506static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07001507nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1508{
1509 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1510 return nfserr_openmode;
1511 else
1512 return nfs_ok;
1513}
1514
NeilBrown52f4fb42005-06-23 22:02:49 -07001515static struct nfs4_delegation *
1516find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1517{
1518 struct nfs4_delegation *dp;
1519
NeilBrownea1da632005-06-23 22:04:17 -07001520 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
NeilBrown52f4fb42005-06-23 22:02:49 -07001521 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1522 return dp;
1523 }
1524 return NULL;
1525}
1526
Al Virob37ad282006-10-19 23:28:59 -07001527static __be32
NeilBrown567d9822005-06-23 22:02:53 -07001528nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1529 struct nfs4_delegation **dp)
1530{
1531 int flags;
Al Virob37ad282006-10-19 23:28:59 -07001532 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07001533
1534 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1535 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07001536 goto out;
NeilBrown567d9822005-06-23 22:02:53 -07001537 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1538 RD_STATE : WR_STATE;
1539 status = nfs4_check_delegmode(*dp, flags);
1540 if (status)
1541 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001542out:
1543 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1544 return nfs_ok;
1545 if (status)
1546 return status;
1547 open->op_stateowner->so_confirmed = 1;
1548 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07001549}
1550
Al Virob37ad282006-10-19 23:28:59 -07001551static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1553{
1554 struct nfs4_stateid *local;
Al Virob37ad282006-10-19 23:28:59 -07001555 __be32 status = nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct nfs4_stateowner *sop = open->op_stateowner;
1557
NeilBrown8beefa22005-06-23 22:03:08 -07001558 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /* ignore lock owners */
1560 if (local->st_stateowner->so_is_open_owner == 0)
1561 continue;
1562 /* remember if we have seen this open owner */
1563 if (local->st_stateowner == sop)
1564 *stpp = local;
1565 /* check for conflicting share reservations */
1566 if (!test_share(local, open))
1567 goto out;
1568 }
1569 status = 0;
1570out:
1571 return status;
1572}
1573
NeilBrown5ac049a2005-06-23 22:03:03 -07001574static inline struct nfs4_stateid *
1575nfs4_alloc_stateid(void)
1576{
1577 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1578}
1579
Al Virob37ad282006-10-19 23:28:59 -07001580static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
NeilBrown567d9822005-06-23 22:02:53 -07001582 struct nfs4_delegation *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct svc_fh *cur_fh, int flags)
1584{
1585 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
NeilBrown5ac049a2005-06-23 22:03:03 -07001587 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (stp == NULL)
1589 return nfserr_resource;
1590
NeilBrown567d9822005-06-23 22:02:53 -07001591 if (dp) {
1592 get_file(dp->dl_vfs_file);
1593 stp->st_vfs_file = dp->dl_vfs_file;
1594 } else {
Al Virob37ad282006-10-19 23:28:59 -07001595 __be32 status;
NeilBrown567d9822005-06-23 22:02:53 -07001596 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1597 &stp->st_vfs_file);
1598 if (status) {
1599 if (status == nfserr_dropit)
1600 status = nfserr_jukebox;
NeilBrown5ac049a2005-06-23 22:03:03 -07001601 kmem_cache_free(stateid_slab, stp);
NeilBrown567d9822005-06-23 22:02:53 -07001602 return status;
1603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 *stpp = stp;
1606 return 0;
1607}
1608
Al Virob37ad282006-10-19 23:28:59 -07001609static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1611 struct nfsd4_open *open)
1612{
1613 struct iattr iattr = {
1614 .ia_valid = ATTR_SIZE,
1615 .ia_size = 0,
1616 };
1617 if (!open->op_truncate)
1618 return 0;
1619 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08001620 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1622}
1623
Al Virob37ad282006-10-19 23:28:59 -07001624static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1626{
1627 struct file *filp = stp->st_vfs_file;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001628 struct inode *inode = filp->f_path.dentry->d_inode;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001629 unsigned int share_access, new_writer;
Al Virob37ad282006-10-19 23:28:59 -07001630 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 set_access(&share_access, stp->st_access_bmap);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001633 new_writer = (~share_access) & open->op_share_access
1634 & NFS4_SHARE_ACCESS_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001636 if (new_writer) {
Al Virob37ad282006-10-19 23:28:59 -07001637 int err = get_write_access(inode);
1638 if (err)
1639 return nfserrno(err);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 status = nfsd4_truncate(rqstp, cur_fh, open);
1642 if (status) {
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001643 if (new_writer)
1644 put_write_access(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return status;
1646 }
1647 /* remember the open */
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001648 filp->f_mode |= open->op_share_access;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 set_bit(open->op_share_access, &stp->st_access_bmap);
1650 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1651
1652 return nfs_ok;
1653}
1654
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656static void
NeilBrown37515172005-07-07 17:59:16 -07001657nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
NeilBrown37515172005-07-07 17:59:16 -07001659 open->op_stateowner->so_confirmed = 1;
1660 open->op_stateowner->so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
1663/*
1664 * Attempt to hand out a delegation.
1665 */
1666static void
1667nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1668{
1669 struct nfs4_delegation *dp;
1670 struct nfs4_stateowner *sop = stp->st_stateowner;
1671 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1672 struct file_lock fl, *flp = &fl;
1673 int status, flag = 0;
1674
1675 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07001676 open->op_recall = 0;
1677 switch (open->op_claim_type) {
1678 case NFS4_OPEN_CLAIM_PREVIOUS:
1679 if (!atomic_read(&cb->cb_set))
1680 open->op_recall = 1;
1681 flag = open->op_delegate_type;
1682 if (flag == NFS4_OPEN_DELEGATE_NONE)
1683 goto out;
1684 break;
1685 case NFS4_OPEN_CLAIM_NULL:
1686 /* Let's not give out any delegations till everyone's
1687 * had the chance to reclaim theirs.... */
1688 if (nfs4_in_grace())
1689 goto out;
1690 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1691 goto out;
1692 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1693 flag = NFS4_OPEN_DELEGATE_WRITE;
1694 else
1695 flag = NFS4_OPEN_DELEGATE_READ;
1696 break;
1697 default:
1698 goto out;
1699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1702 if (dp == NULL) {
1703 flag = NFS4_OPEN_DELEGATE_NONE;
1704 goto out;
1705 }
1706 locks_init_lock(&fl);
1707 fl.fl_lmops = &nfsd_lease_mng_ops;
1708 fl.fl_flags = FL_LEASE;
1709 fl.fl_end = OFFSET_MAX;
1710 fl.fl_owner = (fl_owner_t)dp;
1711 fl.fl_file = stp->st_vfs_file;
1712 fl.fl_pid = current->tgid;
1713
1714 /* setlease checks to see if delegation should be handed out.
1715 * the lock_manager callbacks fl_mylease and fl_change are used
1716 */
1717 if ((status = setlease(stp->st_vfs_file,
1718 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1719 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
NeilBrownc9071322005-04-16 15:26:38 -07001720 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 flag = NFS4_OPEN_DELEGATE_NONE;
1722 goto out;
1723 }
1724
1725 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1726
1727 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1728 dp->dl_stateid.si_boot,
1729 dp->dl_stateid.si_stateownerid,
1730 dp->dl_stateid.si_fileid,
1731 dp->dl_stateid.si_generation);
1732out:
NeilBrown7b190fe2005-06-23 22:03:23 -07001733 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1734 && flag == NFS4_OPEN_DELEGATE_NONE
1735 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1736 printk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 open->op_delegate_type = flag;
1738}
1739
1740/*
1741 * called with nfs4_lock_state() held.
1742 */
Al Virob37ad282006-10-19 23:28:59 -07001743__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1745{
1746 struct nfs4_file *fp = NULL;
1747 struct inode *ino = current_fh->fh_dentry->d_inode;
1748 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07001749 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07001750 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 status = nfserr_inval;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001753 if (!access_valid(open->op_share_access)
1754 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 goto out;
1756 /*
1757 * Lookup file; if found, lookup stateid and check open request,
1758 * and check for delegations in the process of being recalled.
1759 * If not found, create the nfs4_file struct
1760 */
1761 fp = find_file(ino);
1762 if (fp) {
1763 if ((status = nfs4_check_open(fp, open, &stp)))
1764 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001765 status = nfs4_check_deleg(fp, open, &dp);
1766 if (status)
1767 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07001769 status = nfserr_bad_stateid;
1770 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 status = nfserr_resource;
1773 fp = alloc_init_file(ino);
1774 if (fp == NULL)
1775 goto out;
1776 }
1777
1778 /*
1779 * OPEN the file, or upgrade an existing OPEN.
1780 * If truncate fails, the OPEN fails.
1781 */
1782 if (stp) {
1783 /* Stateid was found, this is an OPEN upgrade */
1784 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1785 if (status)
1786 goto out;
NeilBrown444c2c02005-07-07 17:59:22 -07001787 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 } else {
1789 /* Stateid was not found, this is a new OPEN */
1790 int flags = 0;
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -07001791 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
1792 flags |= MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -07001794 flags |= MAY_WRITE;
NeilBrown567d9822005-06-23 22:02:53 -07001795 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1796 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 goto out;
1798 init_stateid(stp, fp, open);
1799 status = nfsd4_truncate(rqstp, current_fh, open);
1800 if (status) {
1801 release_stateid(stp, OPEN_STATE);
1802 goto out;
1803 }
1804 }
1805 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1806
1807 /*
1808 * Attempt to hand out a delegation. No error return, because the
1809 * OPEN succeeds even if we fail.
1810 */
1811 nfs4_open_delegation(current_fh, open, stp);
1812
1813 status = nfs_ok;
1814
1815 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1816 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1817 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1818out:
NeilBrown13cd2182005-06-23 22:03:10 -07001819 if (fp)
1820 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07001821 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1822 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /*
1824 * To finish the open response, we just need to set the rflags.
1825 */
1826 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1827 if (!open->op_stateowner->so_confirmed)
1828 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1829
1830 return status;
1831}
1832
NeilBrown58da2822005-06-23 22:03:19 -07001833static struct workqueue_struct *laundry_wq;
David Howellsc4028952006-11-22 14:57:56 +00001834static void laundromat_main(struct work_struct *);
1835static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Al Virob37ad282006-10-19 23:28:59 -07001837__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001838nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1839 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07001842 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 nfs4_lock_state();
1845 dprintk("process_renew(%08x/%08x): starting\n",
1846 clid->cl_boot, clid->cl_id);
1847 status = nfserr_stale_clientid;
1848 if (STALE_CLIENTID(clid))
1849 goto out;
1850 clp = find_confirmed_client(clid);
1851 status = nfserr_expired;
1852 if (clp == NULL) {
1853 /* We assume the client took too long to RENEW. */
1854 dprintk("nfsd4_renew: clientid not found!\n");
1855 goto out;
1856 }
1857 renew_client(clp);
1858 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07001859 if (!list_empty(&clp->cl_delegations)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 && !atomic_read(&clp->cl_callback.cb_set))
1861 goto out;
1862 status = nfs_ok;
1863out:
1864 nfs4_unlock_state();
1865 return status;
1866}
1867
NeilBrowna76b4312005-06-23 22:04:01 -07001868static void
1869end_grace(void)
1870{
1871 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07001872 nfsd4_recdir_purge_old();
NeilBrowna76b4312005-06-23 22:04:01 -07001873 in_grace = 0;
1874}
1875
NeilBrownfd39ca92005-06-23 22:04:03 -07001876static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877nfs4_laundromat(void)
1878{
1879 struct nfs4_client *clp;
1880 struct nfs4_stateowner *sop;
1881 struct nfs4_delegation *dp;
1882 struct list_head *pos, *next, reaplist;
1883 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1884 time_t t, clientid_val = NFSD_LEASE_TIME;
1885 time_t u, test_val = NFSD_LEASE_TIME;
1886
1887 nfs4_lock_state();
1888
1889 dprintk("NFSD: laundromat service - starting\n");
NeilBrowna76b4312005-06-23 22:04:01 -07001890 if (in_grace)
1891 end_grace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 list_for_each_safe(pos, next, &client_lru) {
1893 clp = list_entry(pos, struct nfs4_client, cl_lru);
1894 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1895 t = clp->cl_time - cutoff;
1896 if (clientid_val > t)
1897 clientid_val = t;
1898 break;
1899 }
1900 dprintk("NFSD: purging unused client (clientid %08x)\n",
1901 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07001902 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 expire_client(clp);
1904 }
1905 INIT_LIST_HEAD(&reaplist);
1906 spin_lock(&recall_lock);
1907 list_for_each_safe(pos, next, &del_recall_lru) {
1908 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1909 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1910 u = dp->dl_time - cutoff;
1911 if (test_val > u)
1912 test_val = u;
1913 break;
1914 }
1915 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1916 dp, dp->dl_flock);
1917 list_move(&dp->dl_recall_lru, &reaplist);
1918 }
1919 spin_unlock(&recall_lock);
1920 list_for_each_safe(pos, next, &reaplist) {
1921 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1922 list_del_init(&dp->dl_recall_lru);
1923 unhash_delegation(dp);
1924 }
1925 test_val = NFSD_LEASE_TIME;
1926 list_for_each_safe(pos, next, &close_lru) {
1927 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1928 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1929 u = sop->so_time - cutoff;
1930 if (test_val > u)
1931 test_val = u;
1932 break;
1933 }
1934 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1935 sop->so_id);
NeilBrown358dd552006-04-10 22:55:42 -07001936 release_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1939 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1940 nfs4_unlock_state();
1941 return clientid_val;
1942}
1943
1944void
David Howellsc4028952006-11-22 14:57:56 +00001945laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 time_t t;
1948
1949 t = nfs4_laundromat();
1950 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07001951 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
NeilBrownfd39ca92005-06-23 22:04:03 -07001954static struct nfs4_stateowner *
NeilBrownf8816512005-07-07 17:59:25 -07001955search_close_lru(u32 st_id, int flags)
1956{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 struct nfs4_stateowner *local = NULL;
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (flags & CLOSE_STATE) {
1960 list_for_each_entry(local, &close_lru, so_close_lru) {
1961 if (local->so_id == st_id)
1962 return local;
1963 }
1964 }
1965 return NULL;
1966}
1967
1968static inline int
1969nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1970{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001971 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974static int
1975STALE_STATEID(stateid_t *stateid)
1976{
1977 if (stateid->si_boot == boot_time)
1978 return 0;
Neil Brown849823c2005-09-13 01:25:36 -07001979 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1981 stateid->si_generation);
1982 return 1;
1983}
1984
1985static inline int
1986access_permit_read(unsigned long access_bmap)
1987{
1988 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1989 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1990 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1991}
1992
1993static inline int
1994access_permit_write(unsigned long access_bmap)
1995{
1996 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1997 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
1998}
1999
2000static
Al Virob37ad282006-10-19 23:28:59 -07002001__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Al Virob37ad282006-10-19 23:28:59 -07002003 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2006 goto out;
2007 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2008 goto out;
2009 status = nfs_ok;
2010out:
2011 return status;
2012}
2013
Al Virob37ad282006-10-19 23:28:59 -07002014static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2016{
2017 /* Trying to call delegreturn with a special stateid? Yuch: */
2018 if (!(flags & (RD_STATE | WR_STATE)))
2019 return nfserr_bad_stateid;
2020 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2021 return nfs_ok;
2022 else if (nfs4_in_grace()) {
2023 /* Answer in remaining cases depends on existance of
2024 * conflicting state; so we must wait out the grace period. */
2025 return nfserr_grace;
2026 } else if (flags & WR_STATE)
2027 return nfs4_share_conflict(current_fh,
2028 NFS4_SHARE_DENY_WRITE);
2029 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2030 return nfs4_share_conflict(current_fh,
2031 NFS4_SHARE_DENY_READ);
2032}
2033
2034/*
2035 * Allow READ/WRITE during grace period on recovered state only for files
2036 * that are not able to provide mandatory locking.
2037 */
2038static inline int
2039io_during_grace_disallowed(struct inode *inode, int flags)
2040{
2041 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2042 && MANDATORY_LOCK(inode);
2043}
2044
2045/*
2046* Checks for stateid operations
2047*/
Al Virob37ad282006-10-19 23:28:59 -07002048__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2050{
2051 struct nfs4_stateid *stp = NULL;
2052 struct nfs4_delegation *dp = NULL;
2053 stateid_t *stidp;
2054 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07002055 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2058 stateid->si_boot, stateid->si_stateownerid,
2059 stateid->si_fileid, stateid->si_generation);
2060 if (filpp)
2061 *filpp = NULL;
2062
2063 if (io_during_grace_disallowed(ino, flags))
2064 return nfserr_grace;
2065
2066 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2067 return check_special_stateids(current_fh, stateid, flags);
2068
2069 /* STALE STATEID */
2070 status = nfserr_stale_stateid;
2071 if (STALE_STATEID(stateid))
2072 goto out;
2073
2074 /* BAD STATEID */
2075 status = nfserr_bad_stateid;
2076 if (!stateid->si_fileid) { /* delegation stateid */
2077 if(!(dp = find_delegation_stateid(ino, stateid))) {
2078 dprintk("NFSD: delegation stateid not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 goto out;
2080 }
2081 stidp = &dp->dl_stateid;
2082 } else { /* open or lock stateid */
2083 if (!(stp = find_stateid(stateid, flags))) {
2084 dprintk("NFSD: open or lock stateid not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 goto out;
2086 }
2087 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2088 goto out;
2089 if (!stp->st_stateowner->so_confirmed)
2090 goto out;
2091 stidp = &stp->st_stateid;
2092 }
2093 if (stateid->si_generation > stidp->si_generation)
2094 goto out;
2095
2096 /* OLD STATEID */
2097 status = nfserr_old_stateid;
2098 if (stateid->si_generation < stidp->si_generation)
2099 goto out;
2100 if (stp) {
2101 if ((status = nfs4_check_openmode(stp,flags)))
2102 goto out;
2103 renew_client(stp->st_stateowner->so_client);
2104 if (filpp)
2105 *filpp = stp->st_vfs_file;
2106 } else if (dp) {
2107 if ((status = nfs4_check_delegmode(dp, flags)))
2108 goto out;
2109 renew_client(dp->dl_client);
2110 if (flags & DELEG_RET)
2111 unhash_delegation(dp);
2112 if (filpp)
2113 *filpp = dp->dl_vfs_file;
2114 }
2115 status = nfs_ok;
2116out:
2117 return status;
2118}
2119
NeilBrown4c4cd222005-07-07 17:59:27 -07002120static inline int
2121setlkflg (int type)
2122{
2123 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2124 RD_STATE : WR_STATE;
2125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127/*
2128 * Checks for sequence id mutating operations.
2129 */
Al Virob37ad282006-10-19 23:28:59 -07002130static __be32
NeilBrown4c4cd222005-07-07 17:59:27 -07002131nfs4_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 struct nfs4_stateid *stp;
2134 struct nfs4_stateowner *sop;
2135
2136 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2137 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2138 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2139 stateid->si_generation);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 *stpp = NULL;
2142 *sopp = NULL;
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2145 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002146 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (STALE_STATEID(stateid))
NeilBrown3a4f98b2005-07-07 17:59:26 -07002150 return nfserr_stale_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /*
2152 * We return BAD_STATEID if filehandle doesn't match stateid,
2153 * the confirmed flag is incorrecly set, or the generation
2154 * number is incorrect.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 */
NeilBrownf8816512005-07-07 17:59:25 -07002156 stp = find_stateid(stateid, flags);
2157 if (stp == NULL) {
2158 /*
2159 * Also, we should make sure this isn't just the result of
2160 * a replayed close:
2161 */
2162 sop = search_close_lru(stateid->si_stateownerid, flags);
2163 if (sop == NULL)
2164 return nfserr_bad_stateid;
2165 *sopp = sop;
2166 goto check_replay;
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
NeilBrown4c4cd222005-07-07 17:59:27 -07002169 if (lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 struct nfs4_stateowner *sop = stp->st_stateowner;
NeilBrown4c4cd222005-07-07 17:59:27 -07002171 clientid_t *lockclid = &lock->v.new.clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 struct nfs4_client *clp = sop->so_client;
NeilBrown4c4cd222005-07-07 17:59:27 -07002173 int lkflg = 0;
Al Virob37ad282006-10-19 23:28:59 -07002174 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
NeilBrown4c4cd222005-07-07 17:59:27 -07002176 lkflg = setlkflg(lock->lk_type);
2177
2178 if (lock->lk_is_new) {
2179 if (!sop->so_is_open_owner)
2180 return nfserr_bad_stateid;
2181 if (!cmp_clid(&clp->cl_clientid, lockclid))
2182 return nfserr_bad_stateid;
2183 /* stp is the open stateid */
2184 status = nfs4_check_openmode(stp, lkflg);
2185 if (status)
2186 return status;
2187 } else {
2188 /* stp is the lock stateid */
2189 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2190 if (status)
2191 return status;
2192 }
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195
2196 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2197 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002198 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200
2201 *stpp = stp;
2202 *sopp = sop = stp->st_stateowner;
2203
2204 /*
2205 * We now validate the seqid and stateid generation numbers.
2206 * For the moment, we ignore the possibility of
2207 * generation number wraparound.
2208 */
NeilBrownbd9aac52005-07-07 17:59:19 -07002209 if (seqid != sop->so_seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 goto check_replay;
2211
NeilBrown3a4f98b2005-07-07 17:59:26 -07002212 if (sop->so_confirmed && flags & CONFIRM) {
2213 printk("NFSD: preprocess_seqid_op: expected"
2214 " unconfirmed stateowner!\n");
2215 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07002217 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2218 printk("NFSD: preprocess_seqid_op: stateowner not"
2219 " confirmed yet!\n");
2220 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222 if (stateid->si_generation > stp->st_stateid.si_generation) {
2223 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002224 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (stateid->si_generation < stp->st_stateid.si_generation) {
2228 printk("NFSD: preprocess_seqid_op: old stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002229 return nfserr_old_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
NeilBrown52fd0042005-07-07 17:59:24 -07002231 renew_client(sop->so_client);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002232 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234check_replay:
NeilBrownbd9aac52005-07-07 17:59:19 -07002235 if (seqid == sop->so_seqid - 1) {
Neil Brown849823c2005-09-13 01:25:36 -07002236 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /* indicate replay to calling function */
Al Viroa90b0612006-10-19 23:29:03 -07002238 return nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07002240 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2241 sop->so_seqid, seqid);
2242 *sopp = NULL;
2243 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Al Virob37ad282006-10-19 23:28:59 -07002246__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002247nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002248 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Al Virob37ad282006-10-19 23:28:59 -07002250 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 struct nfs4_stateowner *sop;
2252 struct nfs4_stateid *stp;
2253
2254 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002255 (int)cstate->current_fh.fh_dentry->d_name.len,
2256 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002258 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07002259 if (status)
2260 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 nfs4_lock_state();
2263
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002264 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2265 oc->oc_seqid, &oc->oc_req_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 CHECK_FH | CONFIRM | OPEN_STATE,
2267 &oc->oc_stateowner, &stp, NULL)))
2268 goto out;
2269
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);
NeilBrownc7b9a452005-06-23 22:04:30 -07002280
2281 nfsd4_create_clid_dir(sop->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282out:
Neil Brownf2327d92005-09-13 01:25:37 -07002283 if (oc->oc_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 nfs4_get_stateowner(oc->oc_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002285 cstate->replay_owner = oc->oc_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 nfs4_unlock_state();
2288 return status;
2289}
2290
2291
2292/*
2293 * unset all bits in union bitmap (bmap) that
2294 * do not exist in share (from successful OPEN_DOWNGRADE)
2295 */
2296static void
2297reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2298{
2299 int i;
2300 for (i = 1; i < 4; i++) {
2301 if ((i & access) != i)
2302 __clear_bit(i, bmap);
2303 }
2304}
2305
2306static void
2307reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2308{
2309 int i;
2310 for (i = 0; i < 4; i++) {
2311 if ((i & deny) != i)
2312 __clear_bit(i, bmap);
2313 }
2314}
2315
Al Virob37ad282006-10-19 23:28:59 -07002316__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002317nfsd4_open_downgrade(struct svc_rqst *rqstp,
2318 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002319 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Al Virob37ad282006-10-19 23:28:59 -07002321 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 struct nfs4_stateid *stp;
2323 unsigned int share_access;
2324
2325 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002326 (int)cstate->current_fh.fh_dentry->d_name.len,
2327 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002329 if (!access_valid(od->od_share_access)
2330 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return nfserr_inval;
2332
2333 nfs4_lock_state();
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002334 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2335 od->od_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 &od->od_stateid,
2337 CHECK_FH | OPEN_STATE,
2338 &od->od_stateowner, &stp, NULL)))
2339 goto out;
2340
2341 status = nfserr_inval;
2342 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2343 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2344 stp->st_access_bmap, od->od_share_access);
2345 goto out;
2346 }
2347 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2348 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2349 stp->st_deny_bmap, od->od_share_deny);
2350 goto out;
2351 }
2352 set_access(&share_access, stp->st_access_bmap);
2353 nfs4_file_downgrade(stp->st_vfs_file,
2354 share_access & ~od->od_share_access);
2355
2356 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2357 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2358
2359 update_stateid(&stp->st_stateid);
2360 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2361 status = nfs_ok;
2362out:
Neil Brownf2327d92005-09-13 01:25:37 -07002363 if (od->od_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 nfs4_get_stateowner(od->od_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002365 cstate->replay_owner = od->od_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 nfs4_unlock_state();
2368 return status;
2369}
2370
2371/*
2372 * nfs4_unlock_state() called after encode
2373 */
Al Virob37ad282006-10-19 23:28:59 -07002374__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002375nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002376 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Al Virob37ad282006-10-19 23:28:59 -07002378 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 struct nfs4_stateid *stp;
2380
2381 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002382 (int)cstate->current_fh.fh_dentry->d_name.len,
2383 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 nfs4_lock_state();
2386 /* check close_lru for replay */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002387 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2388 close->cl_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 &close->cl_stateid,
2390 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2391 &close->cl_stateowner, &stp, NULL)))
2392 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 status = nfs_ok;
2394 update_stateid(&stp->st_stateid);
2395 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2396
J. Bruce Fields04ef5952006-01-18 17:43:21 -08002397 /* release_stateid() calls nfsd_close() if needed */
2398 release_stateid(stp, OPEN_STATE);
2399
2400 /* place unused nfs4_stateowners on so_close_lru list to be
2401 * released by the laundromat service after the lease period
2402 * to enable us to handle CLOSE replay
2403 */
2404 if (list_empty(&close->cl_stateowner->so_stateids))
2405 move_to_close_lru(close->cl_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406out:
Neil Brownf2327d92005-09-13 01:25:37 -07002407 if (close->cl_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 nfs4_get_stateowner(close->cl_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002409 cstate->replay_owner = close->cl_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 nfs4_unlock_state();
2412 return status;
2413}
2414
Al Virob37ad282006-10-19 23:28:59 -07002415__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002416nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2417 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Al Virob37ad282006-10-19 23:28:59 -07002419 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002421 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 goto out;
2423
2424 nfs4_lock_state();
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002425 status = nfs4_preprocess_stateid_op(&cstate->current_fh,
2426 &dr->dr_stateid, DELEG_RET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 nfs4_unlock_state();
2428out:
2429 return status;
2430}
2431
2432
2433/*
2434 * Lock owner state (byte-range locks)
2435 */
2436#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2437#define LOCK_HASH_BITS 8
2438#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2439#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2440
2441#define lockownerid_hashval(id) \
2442 ((id) & LOCK_HASH_MASK)
2443
2444static inline unsigned int
2445lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2446 struct xdr_netobj *ownername)
2447{
2448 return (file_hashval(inode) + cl_id
2449 + opaque_hashval(ownername->data, ownername->len))
2450 & LOCK_HASH_MASK;
2451}
2452
2453static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2454static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2455static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2456
NeilBrownfd39ca92005-06-23 22:04:03 -07002457static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458find_stateid(stateid_t *stid, int flags)
2459{
2460 struct nfs4_stateid *local = NULL;
2461 u32 st_id = stid->si_stateownerid;
2462 u32 f_id = stid->si_fileid;
2463 unsigned int hashval;
2464
2465 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2466 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2467 hashval = stateid_hashval(st_id, f_id);
2468 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2469 if ((local->st_stateid.si_stateownerid == st_id) &&
2470 (local->st_stateid.si_fileid == f_id))
2471 return local;
2472 }
2473 }
2474 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2475 hashval = stateid_hashval(st_id, f_id);
2476 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2477 if ((local->st_stateid.si_stateownerid == st_id) &&
2478 (local->st_stateid.si_fileid == f_id))
2479 return local;
2480 }
Neil Brown849823c2005-09-13 01:25:36 -07002481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return NULL;
2483}
2484
2485static struct nfs4_delegation *
2486find_delegation_stateid(struct inode *ino, stateid_t *stid)
2487{
NeilBrown13cd2182005-06-23 22:03:10 -07002488 struct nfs4_file *fp;
2489 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2492 stid->si_boot, stid->si_stateownerid,
2493 stid->si_fileid, stid->si_generation);
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002496 if (!fp)
2497 return NULL;
2498 dl = find_delegation_file(fp, stid);
2499 put_nfs4_file(fp);
2500 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
2503/*
2504 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2505 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2506 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2507 * locking, this prevents us from being completely protocol-compliant. The
2508 * real solution to this problem is to start using unsigned file offsets in
2509 * the VFS, but this is a very deep change!
2510 */
2511static inline void
2512nfs4_transform_lock_offset(struct file_lock *lock)
2513{
2514 if (lock->fl_start < 0)
2515 lock->fl_start = OFFSET_MAX;
2516 if (lock->fl_end < 0)
2517 lock->fl_end = OFFSET_MAX;
2518}
2519
NeilBrownd5b90262006-04-10 22:55:22 -07002520/* Hack!: For now, we're defining this just so we can use a pointer to it
2521 * as a unique cookie to identify our (NFSv4's) posix locks. */
Adrian Bunke465a772006-04-10 22:55:23 -07002522static struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07002523};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525static inline void
2526nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2527{
NeilBrownd5b90262006-04-10 22:55:22 -07002528 struct nfs4_stateowner *sop;
2529 unsigned int hval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
NeilBrownd5b90262006-04-10 22:55:22 -07002531 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2532 sop = (struct nfs4_stateowner *) fl->fl_owner;
2533 hval = lockownerid_hashval(sop->so_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 kref_get(&sop->so_ref);
2535 deny->ld_sop = sop;
2536 deny->ld_clientid = sop->so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07002537 } else {
2538 deny->ld_sop = NULL;
2539 deny->ld_clientid.cl_boot = 0;
2540 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
2542 deny->ld_start = fl->fl_start;
2543 deny->ld_length = ~(u64)0;
2544 if (fl->fl_end != ~(u64)0)
2545 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2546 deny->ld_type = NFS4_READ_LT;
2547 if (fl->fl_type != F_RDLCK)
2548 deny->ld_type = NFS4_WRITE_LT;
2549}
2550
2551static struct nfs4_stateowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2553 struct xdr_netobj *owner)
2554{
2555 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2556 struct nfs4_stateowner *op;
2557
2558 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2559 if (cmp_owner_str(op, owner, clid))
2560 return op;
2561 }
2562 return NULL;
2563}
2564
2565/*
2566 * Alloc a lock owner structure.
2567 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2568 * occured.
2569 *
2570 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 */
2572
2573static struct nfs4_stateowner *
2574alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2575 struct nfs4_stateowner *sop;
2576 struct nfs4_replay *rp;
2577 unsigned int idhashval;
2578
2579 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2580 return NULL;
2581 idhashval = lockownerid_hashval(current_ownerid);
2582 INIT_LIST_HEAD(&sop->so_idhash);
2583 INIT_LIST_HEAD(&sop->so_strhash);
2584 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07002585 INIT_LIST_HEAD(&sop->so_stateids);
2586 INIT_LIST_HEAD(&sop->so_perstateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2588 sop->so_time = 0;
2589 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2590 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07002591 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 sop->so_is_open_owner = 0;
2593 sop->so_id = current_ownerid++;
2594 sop->so_client = clp;
Neil Brownb59e3c02005-09-13 01:25:38 -07002595 /* It is the openowner seqid that will be incremented in encode in the
2596 * case of new lockowners; so increment the lock seqid manually: */
2597 sop->so_seqid = lock->lk_new_lock_seqid + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 sop->so_confirmed = 1;
2599 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08002600 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 rp->rp_buflen = 0;
2602 rp->rp_buf = rp->rp_ibuf;
2603 return sop;
2604}
2605
NeilBrownfd39ca92005-06-23 22:04:03 -07002606static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2608{
2609 struct nfs4_stateid *stp;
2610 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2611
NeilBrown5ac049a2005-06-23 22:03:03 -07002612 stp = nfs4_alloc_stateid();
2613 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 goto out;
2615 INIT_LIST_HEAD(&stp->st_hash);
2616 INIT_LIST_HEAD(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07002617 INIT_LIST_HEAD(&stp->st_perstateowner);
2618 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07002620 list_add(&stp->st_perfile, &fp->fi_stateids);
NeilBrownea1da632005-06-23 22:04:17 -07002621 list_add(&stp->st_perstateowner, &sop->so_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07002623 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 stp->st_file = fp;
2625 stp->st_stateid.si_boot = boot_time;
2626 stp->st_stateid.si_stateownerid = sop->so_id;
2627 stp->st_stateid.si_fileid = fp->fi_id;
2628 stp->st_stateid.si_generation = 0;
2629 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2630 stp->st_access_bmap = open_stp->st_access_bmap;
2631 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07002632 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634out:
2635 return stp;
2636}
2637
NeilBrownfd39ca92005-06-23 22:04:03 -07002638static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639check_lock_length(u64 offset, u64 length)
2640{
2641 return ((length == 0) || ((length != ~(u64)0) &&
2642 LOFF_OVERFLOW(offset, length)));
2643}
2644
2645/*
2646 * LOCK operation
2647 */
Al Virob37ad282006-10-19 23:28:59 -07002648__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002649nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002650 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
NeilBrown3e9e3db2005-06-23 22:04:20 -07002652 struct nfs4_stateowner *open_sop = NULL;
Neil Brownb59e3c02005-09-13 01:25:38 -07002653 struct nfs4_stateowner *lock_sop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 struct nfs4_stateid *lock_stp;
2655 struct file *filp;
2656 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05002657 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07002658 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 unsigned int strhashval;
Al Virob8dd7b92006-10-19 23:29:01 -07002660 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2663 (long long) lock->lk_offset,
2664 (long long) lock->lk_length);
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (check_lock_length(lock->lk_offset, lock->lk_length))
2667 return nfserr_inval;
2668
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002669 if ((status = fh_verify(rqstp, &cstate->current_fh,
2670 S_IFREG, MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08002671 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2672 return status;
2673 }
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 nfs4_lock_state();
2676
2677 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07002678 /*
2679 * Client indicates that this is a new lockowner.
2680 * Use open owner and open stateid to create lock owner and
2681 * lock stateid.
2682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 struct nfs4_stateid *open_stp = NULL;
2684 struct nfs4_file *fp;
2685
2686 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002687 if (STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 /* validate and update open stateid and open seqid */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002691 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 lock->lk_new_open_seqid,
2693 &lock->lk_new_open_stateid,
2694 CHECK_FH | OPEN_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002695 &lock->lk_replay_owner, &open_stp,
Neil Brownb59e3c02005-09-13 01:25:38 -07002696 lock);
NeilBrown37515172005-07-07 17:59:16 -07002697 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002699 open_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /* create lockowner and lock stateid */
2701 fp = open_stp->st_file;
2702 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2703 open_sop->so_client->cl_clientid.cl_id,
2704 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07002705 /* XXX: Do we need to check for duplicate stateowners on
2706 * the same file, or should they just be allowed (and
2707 * create new stateids)? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 status = nfserr_resource;
Neil Brownb59e3c02005-09-13 01:25:38 -07002709 lock_sop = alloc_init_lock_stateowner(strhashval,
2710 open_sop->so_client, open_stp, lock);
2711 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07002713 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08002714 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 } else {
2717 /* lock (lock owner + lock stateid) already exists */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002718 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 lock->lk_old_lock_seqid,
2720 &lock->lk_old_lock_stateid,
2721 CHECK_FH | LOCK_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002722 &lock->lk_replay_owner, &lock_stp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (status)
2724 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002725 lock_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 }
J. Bruce Fields3a655882006-01-18 17:43:19 -08002727 /* lock->lk_replay_owner and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 filp = lock_stp->st_vfs_file;
2729
NeilBrown0dd395dc2005-07-07 17:59:15 -07002730 status = nfserr_grace;
2731 if (nfs4_in_grace() && !lock->lk_reclaim)
2732 goto out;
2733 status = nfserr_no_grace;
2734 if (!nfs4_in_grace() && lock->lk_reclaim)
2735 goto out;
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 locks_init_lock(&file_lock);
2738 switch (lock->lk_type) {
2739 case NFS4_READ_LT:
2740 case NFS4_READW_LT:
2741 file_lock.fl_type = F_RDLCK;
2742 break;
2743 case NFS4_WRITE_LT:
2744 case NFS4_WRITEW_LT:
2745 file_lock.fl_type = F_WRLCK;
2746 break;
2747 default:
2748 status = nfserr_inval;
2749 goto out;
2750 }
Neil Brownb59e3c02005-09-13 01:25:38 -07002751 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 file_lock.fl_pid = current->tgid;
2753 file_lock.fl_file = filp;
2754 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002755 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 file_lock.fl_start = lock->lk_offset;
2758 if ((lock->lk_length == ~(u64)0) ||
2759 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2760 file_lock.fl_end = ~(u64)0;
2761 else
2762 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2763 nfs4_transform_lock_offset(&file_lock);
2764
2765 /*
2766 * Try to lock the file in the VFS.
2767 * Note: locks.c uses the BKL to protect the inode's lock list.
2768 */
2769
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002770 /* XXX?: Just to divert the locks_release_private at the start of
2771 * locks_copy_lock: */
2772 conflock.fl_ops = NULL;
2773 conflock.fl_lmops = NULL;
Al Virob8dd7b92006-10-19 23:29:01 -07002774 err = posix_lock_file_conf(filp, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07002775 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 case 0: /* success! */
2777 update_stateid(&lock_stp->st_stateid);
2778 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2779 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07002780 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002781 break;
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);
2786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 case (EDEADLK):
2788 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 default:
Al Virob8dd7b92006-10-19 23:29:01 -07002791 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf() failed! status %d\n",err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002792 status = nfserr_resource;
2793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08002796 if (status && lock->lk_is_new && lock_sop)
2797 release_stateowner(lock_sop);
J. Bruce Fields3a655882006-01-18 17:43:19 -08002798 if (lock->lk_replay_owner) {
2799 nfs4_get_stateowner(lock->lk_replay_owner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002800 cstate->replay_owner = lock->lk_replay_owner;
Neil Brownf2327d92005-09-13 01:25:37 -07002801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 nfs4_unlock_state();
2803 return status;
2804}
2805
2806/*
2807 * LOCKT operation
2808 */
Al Virob37ad282006-10-19 23:28:59 -07002809__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002810nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2811 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
2813 struct inode *inode;
2814 struct file file;
2815 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07002816 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 if (nfs4_in_grace())
2819 return nfserr_grace;
2820
2821 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2822 return nfserr_inval;
2823
2824 lockt->lt_stateowner = NULL;
2825 nfs4_lock_state();
2826
2827 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002828 if (STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002831 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
Neil Brown849823c2005-09-13 01:25:36 -07002832 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (status == nfserr_symlink)
2834 status = nfserr_inval;
2835 goto out;
2836 }
2837
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002838 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 locks_init_lock(&file_lock);
2840 switch (lockt->lt_type) {
2841 case NFS4_READ_LT:
2842 case NFS4_READW_LT:
2843 file_lock.fl_type = F_RDLCK;
2844 break;
2845 case NFS4_WRITE_LT:
2846 case NFS4_WRITEW_LT:
2847 file_lock.fl_type = F_WRLCK;
2848 break;
2849 default:
2850 printk("NFSD: nfs4_lockt: bad lock type!\n");
2851 status = nfserr_inval;
2852 goto out;
2853 }
2854
2855 lockt->lt_stateowner = find_lockstateowner_str(inode,
2856 &lockt->lt_clientid, &lockt->lt_owner);
2857 if (lockt->lt_stateowner)
2858 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2859 file_lock.fl_pid = current->tgid;
2860 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002861 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 file_lock.fl_start = lockt->lt_offset;
2864 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2865 file_lock.fl_end = ~(u64)0;
2866 else
2867 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2868
2869 nfs4_transform_lock_offset(&file_lock);
2870
2871 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2872 * since LOCKT doesn't require an OPEN, and therefore a struct
2873 * file may not exist, pass posix_test_lock a struct file with
2874 * only the dentry:inode set.
2875 */
2876 memset(&file, 0, sizeof (struct file));
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002877 file.f_path.dentry = cstate->current_fh.fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879 status = nfs_ok;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002880 posix_test_lock(&file, &file_lock);
2881 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002883 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
2885out:
2886 nfs4_unlock_state();
2887 return status;
2888}
2889
Al Virob37ad282006-10-19 23:28:59 -07002890__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002891nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002892 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
2894 struct nfs4_stateid *stp;
2895 struct file *filp = NULL;
2896 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07002897 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002898 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
2900 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2901 (long long) locku->lu_offset,
2902 (long long) locku->lu_length);
2903
2904 if (check_lock_length(locku->lu_offset, locku->lu_length))
2905 return nfserr_inval;
2906
2907 nfs4_lock_state();
2908
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002909 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 locku->lu_seqid,
2911 &locku->lu_stateid,
2912 CHECK_FH | LOCK_STATE,
2913 &locku->lu_stateowner, &stp, NULL)))
2914 goto out;
2915
2916 filp = stp->st_vfs_file;
2917 BUG_ON(!filp);
2918 locks_init_lock(&file_lock);
2919 file_lock.fl_type = F_UNLCK;
2920 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2921 file_lock.fl_pid = current->tgid;
2922 file_lock.fl_file = filp;
2923 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002924 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 file_lock.fl_start = locku->lu_offset;
2926
2927 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2928 file_lock.fl_end = ~(u64)0;
2929 else
2930 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2931 nfs4_transform_lock_offset(&file_lock);
2932
2933 /*
2934 * Try to unlock the file in the VFS.
2935 */
Al Virob8dd7b92006-10-19 23:29:01 -07002936 err = posix_lock_file(filp, &file_lock);
2937 if (err) {
Neil Brown849823c2005-09-13 01:25:36 -07002938 dprintk("NFSD: nfs4_locku: posix_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 goto out_nfserr;
2940 }
2941 /*
2942 * OK, unlock succeeded; the only thing left to do is update the stateid.
2943 */
2944 update_stateid(&stp->st_stateid);
2945 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2946
2947out:
Neil Brownf2327d92005-09-13 01:25:37 -07002948 if (locku->lu_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 nfs4_get_stateowner(locku->lu_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002950 cstate->replay_owner = locku->lu_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 nfs4_unlock_state();
2953 return status;
2954
2955out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002956 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 goto out;
2958}
2959
2960/*
2961 * returns
2962 * 1: locks held by lockowner
2963 * 0: no locks held by lockowner
2964 */
2965static int
2966check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2967{
2968 struct file_lock **flpp;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08002969 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 int status = 0;
2971
2972 lock_kernel();
2973 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08002974 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 status = 1;
2976 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08002977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
2979out:
2980 unlock_kernel();
2981 return status;
2982}
2983
Al Virob37ad282006-10-19 23:28:59 -07002984__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002985nfsd4_release_lockowner(struct svc_rqst *rqstp,
2986 struct nfsd4_compound_state *cstate,
2987 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
2989 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07002990 struct nfs4_stateowner *sop;
2991 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07002993 struct list_head matches;
2994 int i;
Al Virob37ad282006-10-19 23:28:59 -07002995 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2998 clid->cl_boot, clid->cl_id);
2999
3000 /* XXX check for lease expiration */
3001
3002 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07003003 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
3006 nfs4_lock_state();
3007
NeilBrown3e9e3db2005-06-23 22:04:20 -07003008 status = nfserr_locks_held;
3009 /* XXX: we're doing a linear search through all the lockowners.
3010 * Yipes! For now we'll just hope clients aren't really using
3011 * release_lockowner much, but eventually we have to fix these
3012 * data structures. */
3013 INIT_LIST_HEAD(&matches);
3014 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3015 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
3016 if (!cmp_owner_str(sop, owner, clid))
3017 continue;
3018 list_for_each_entry(stp, &sop->so_stateids,
3019 st_perstateowner) {
3020 if (check_for_locks(stp->st_vfs_file, sop))
3021 goto out;
3022 /* Note: so_perclient unused for lockowners,
3023 * so it's OK to fool with here. */
3024 list_add(&sop->so_perclient, &matches);
3025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07003027 }
3028 /* Clients probably won't expect us to return with some (but not all)
3029 * of the lockowner state released; so don't release any until all
3030 * have been checked. */
3031 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07003032 while (!list_empty(&matches)) {
3033 sop = list_entry(matches.next, struct nfs4_stateowner,
3034 so_perclient);
3035 /* unhash_stateowner deletes so_perclient only
3036 * for openowners. */
3037 list_del(&sop->so_perclient);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003038 release_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 }
3040out:
3041 nfs4_unlock_state();
3042 return status;
3043}
3044
3045static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07003046alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
NeilBrowna55370a2005-06-23 22:03:52 -07003048 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
NeilBrownc7b9a452005-06-23 22:04:30 -07003051int
3052nfs4_has_reclaimed_state(const char *name)
3053{
3054 unsigned int strhashval = clientstr_hashval(name);
3055 struct nfs4_client *clp;
3056
3057 clp = find_confirmed_client_by_str(name, strhashval);
3058 return clp ? 1 : 0;
3059}
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061/*
3062 * failure => all reset bets are off, nfserr_no_grace...
3063 */
NeilBrown190e4fb2005-06-23 22:04:25 -07003064int
3065nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
3067 unsigned int strhashval;
3068 struct nfs4_client_reclaim *crp = NULL;
3069
NeilBrowna55370a2005-06-23 22:03:52 -07003070 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3071 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if (!crp)
3073 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07003074 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 INIT_LIST_HEAD(&crp->cr_strhash);
3076 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07003077 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 reclaim_str_hashtbl_size++;
3079 return 1;
3080}
3081
3082static void
3083nfs4_release_reclaim(void)
3084{
3085 struct nfs4_client_reclaim *crp = NULL;
3086 int i;
3087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3089 while (!list_empty(&reclaim_str_hashtbl[i])) {
3090 crp = list_entry(reclaim_str_hashtbl[i].next,
3091 struct nfs4_client_reclaim, cr_strhash);
3092 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 kfree(crp);
3094 reclaim_str_hashtbl_size--;
3095 }
3096 }
3097 BUG_ON(reclaim_str_hashtbl_size);
3098}
3099
3100/*
3101 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07003102static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103nfs4_find_reclaim_client(clientid_t *clid)
3104{
3105 unsigned int strhashval;
3106 struct nfs4_client *clp;
3107 struct nfs4_client_reclaim *crp = NULL;
3108
3109
3110 /* find clientid in conf_id_hashtbl */
3111 clp = find_confirmed_client(clid);
3112 if (clp == NULL)
3113 return NULL;
3114
NeilBrowna55370a2005-06-23 22:03:52 -07003115 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3116 clp->cl_name.len, clp->cl_name.data,
3117 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07003120 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07003122 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return crp;
3124 }
3125 }
3126 return NULL;
3127}
3128
3129/*
3130* Called from OPEN. Look for clientid in reclaim list.
3131*/
Al Virob37ad282006-10-19 23:28:59 -07003132__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133nfs4_check_open_reclaim(clientid_t *clid)
3134{
NeilBrowndfc83562005-06-23 22:03:16 -07003135 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
NeilBrownac4d8ff22005-06-23 22:03:30 -07003138/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
NeilBrownac4d8ff22005-06-23 22:03:30 -07003140void
3141nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
3143 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3146 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3147 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3148 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3149 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3150 }
3151 for (i = 0; i < FILE_HASH_SIZE; i++) {
3152 INIT_LIST_HEAD(&file_hashtbl[i]);
3153 }
3154 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3155 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3156 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3157 }
3158 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3159 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3160 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3161 }
3162 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3163 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3164 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 INIT_LIST_HEAD(&close_lru);
3168 INIT_LIST_HEAD(&client_lru);
3169 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07003170 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3171 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3172 reclaim_str_hashtbl_size = 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003173}
3174
NeilBrown190e4fb2005-06-23 22:04:25 -07003175static void
3176nfsd4_load_reboot_recovery_data(void)
3177{
3178 int status;
3179
NeilBrown0964a3d2005-06-23 22:04:32 -07003180 nfs4_lock_state();
3181 nfsd4_init_recdir(user_recovery_dirname);
NeilBrown190e4fb2005-06-23 22:04:25 -07003182 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07003183 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07003184 if (status)
3185 printk("NFSD: Failure reading reboot recovery data\n");
3186}
3187
NeilBrownac4d8ff22005-06-23 22:03:30 -07003188/* initialization to perform when the nfsd service is started: */
3189
3190static void
3191__nfs4_state_start(void)
3192{
3193 time_t grace_time;
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 boot_time = get_seconds();
NeilBrownd99a05ad2005-06-23 22:03:21 -07003196 grace_time = max(user_lease_time, lease_time);
3197 lease_time = user_lease_time;
NeilBrowna76b4312005-06-23 22:04:01 -07003198 in_grace = 1;
NeilBrownd99a05ad2005-06-23 22:03:21 -07003199 printk("NFSD: starting %ld-second grace period\n", grace_time);
NeilBrown58da2822005-06-23 22:03:19 -07003200 laundry_wq = create_singlethread_workqueue("nfsd4");
NeilBrowna76b4312005-06-23 22:04:01 -07003201 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203
3204int
NeilBrown76a35502005-06-23 22:03:26 -07003205nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
3207 int status;
3208
3209 if (nfs4_init)
3210 return 0;
3211 status = nfsd4_init_slabs();
3212 if (status)
3213 return status;
NeilBrown190e4fb2005-06-23 22:04:25 -07003214 nfsd4_load_reboot_recovery_data();
NeilBrown76a35502005-06-23 22:03:26 -07003215 __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 nfs4_init = 1;
3217 return 0;
3218}
3219
3220int
3221nfs4_in_grace(void)
3222{
NeilBrowna76b4312005-06-23 22:04:01 -07003223 return in_grace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
3226time_t
3227nfs4_lease_time(void)
3228{
3229 return lease_time;
3230}
3231
3232static void
3233__nfs4_state_shutdown(void)
3234{
3235 int i;
3236 struct nfs4_client *clp = NULL;
3237 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 struct list_head *pos, *next, reaplist;
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3241 while (!list_empty(&conf_id_hashtbl[i])) {
3242 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3243 expire_client(clp);
3244 }
3245 while (!list_empty(&unconf_str_hashtbl[i])) {
3246 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3247 expire_client(clp);
3248 }
3249 }
3250 INIT_LIST_HEAD(&reaplist);
3251 spin_lock(&recall_lock);
3252 list_for_each_safe(pos, next, &del_recall_lru) {
3253 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3254 list_move(&dp->dl_recall_lru, &reaplist);
3255 }
3256 spin_unlock(&recall_lock);
3257 list_for_each_safe(pos, next, &reaplist) {
3258 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3259 list_del_init(&dp->dl_recall_lru);
3260 unhash_delegation(dp);
3261 }
3262
NeilBrown190e4fb2005-06-23 22:04:25 -07003263 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 nfs4_init = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266
3267void
3268nfs4_state_shutdown(void)
3269{
NeilBrown5e8d5c22006-04-10 22:55:37 -07003270 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3271 destroy_workqueue(laundry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 nfs4_lock_state();
3273 nfs4_release_reclaim();
3274 __nfs4_state_shutdown();
3275 nfsd4_free_slabs();
3276 nfs4_unlock_state();
3277}
3278
NeilBrown0964a3d2005-06-23 22:04:32 -07003279static void
3280nfs4_set_recdir(char *recdir)
3281{
3282 nfs4_lock_state();
3283 strcpy(user_recovery_dirname, recdir);
3284 nfs4_unlock_state();
3285}
3286
3287/*
3288 * Change the NFSv4 recovery directory to recdir.
3289 */
3290int
3291nfs4_reset_recoverydir(char *recdir)
3292{
3293 int status;
3294 struct nameidata nd;
3295
3296 status = path_lookup(recdir, LOOKUP_FOLLOW, &nd);
3297 if (status)
3298 return status;
3299 status = -ENOTDIR;
3300 if (S_ISDIR(nd.dentry->d_inode->i_mode)) {
3301 nfs4_set_recdir(recdir);
3302 status = 0;
3303 }
3304 path_release(&nd);
3305 return status;
3306}
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308/*
3309 * Called when leasetime is changed.
3310 *
NeilBrownd99a05ad2005-06-23 22:03:21 -07003311 * The only way the protocol gives us to handle on-the-fly lease changes is to
3312 * simulate a reboot. Instead of doing that, we just wait till the next time
3313 * we start to register any changes in lease time. If the administrator
3314 * really wants to change the lease time *now*, they can go ahead and bring
3315 * nfsd down and then back up again after changing the lease time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 */
3317void
3318nfs4_reset_lease(time_t leasetime)
3319{
NeilBrownd99a05ad2005-06-23 22:03:21 -07003320 lock_kernel();
3321 user_lease_time = leasetime;
3322 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}