blob: 678f3be88ac04e7cf319e1819835f8a394b53291 [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>
Marc Eshelfd85b812006-11-28 16:26:41 -050053#include <linux/lockd/bind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define NFSDDBG_FACILITY NFSDDBG_PROC
56
57/* Globals */
58static time_t lease_time = 90; /* default lease time */
NeilBrownd99a05a2005-06-23 22:03:21 -070059static time_t user_lease_time = 90;
NeilBrownfd39ca92005-06-23 22:04:03 -070060static time_t boot_time;
NeilBrowna76b4312005-06-23 22:04:01 -070061static int in_grace = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static u32 current_clientid = 1;
63static u32 current_ownerid = 1;
64static u32 current_fileid = 1;
65static u32 current_delegid = 1;
66static u32 nfs4_init;
NeilBrownfd39ca92005-06-23 22:04:03 -070067static stateid_t zerostateid; /* bits all 0 */
68static stateid_t onestateid; /* bits all 1 */
69
70#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
71#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* forward declarations */
NeilBrownfd39ca92005-06-23 22:04:03 -070074static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
76static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
NeilBrown0964a3d2005-06-23 22:04:32 -070077static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
78static void nfs4_set_recdir(char *recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* Locking:
81 *
Ingo Molnar353ab6e2006-03-26 01:37:12 -080082 * client_mutex:
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * protects clientid_hashtbl[], clientstr_hashtbl[],
84 * unconfstr_hashtbl[], uncofid_hashtbl[].
85 */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080086static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Christoph Lametere18b8902006-12-06 20:33:20 -080088static struct kmem_cache *stateowner_slab = NULL;
89static struct kmem_cache *file_slab = NULL;
90static struct kmem_cache *stateid_slab = NULL;
91static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093void
94nfs4_lock_state(void)
95{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080096 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99void
100nfs4_unlock_state(void)
101{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800102 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105static inline u32
106opaque_hashval(const void *ptr, int nbytes)
107{
108 unsigned char *cptr = (unsigned char *) ptr;
109
110 u32 x = 0;
111 while (nbytes--) {
112 x *= 37;
113 x += *cptr++;
114 }
115 return x;
116}
117
118/* forward declarations */
119static void release_stateowner(struct nfs4_stateowner *sop);
120static void release_stateid(struct nfs4_stateid *stp, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/*
123 * Delegation state
124 */
125
126/* recall_lock protects the del_recall_lru */
Ingo Molnar34af9462006-06-27 02:53:55 -0700127static DEFINE_SPINLOCK(recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static struct list_head del_recall_lru;
129
NeilBrown13cd2182005-06-23 22:03:10 -0700130static void
131free_nfs4_file(struct kref *kref)
132{
133 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
134 list_del(&fp->fi_hash);
135 iput(fp->fi_inode);
136 kmem_cache_free(file_slab, fp);
137}
138
139static inline void
140put_nfs4_file(struct nfs4_file *fi)
141{
142 kref_put(&fi->fi_ref, free_nfs4_file);
143}
144
145static inline void
146get_nfs4_file(struct nfs4_file *fi)
147{
148 kref_get(&fi->fi_ref);
149}
150
NeilBrownef0f3392006-04-10 22:55:41 -0700151static int num_delegations;
152
153/*
154 * Open owner state (share locks)
155 */
156
157/* hash tables for nfs4_stateowner */
158#define OWNER_HASH_BITS 8
159#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
160#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
161
162#define ownerid_hashval(id) \
163 ((id) & OWNER_HASH_MASK)
164#define ownerstr_hashval(clientid, ownername) \
165 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
166
167static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
168static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
169
170/* hash table for nfs4_file */
171#define FILE_HASH_BITS 8
172#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
173#define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
174/* hash table for (open)nfs4_stateid */
175#define STATEID_HASH_BITS 10
176#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
177#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
178
179#define file_hashval(x) \
180 hash_ptr(x, FILE_HASH_BITS)
181#define stateid_hashval(owner_id, file_id) \
182 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
183
184static struct list_head file_hashtbl[FILE_HASH_SIZE];
185static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static struct nfs4_delegation *
188alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
189{
190 struct nfs4_delegation *dp;
191 struct nfs4_file *fp = stp->st_file;
192 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
193
194 dprintk("NFSD alloc_init_deleg\n");
NeilBrownef0f3392006-04-10 22:55:41 -0700195 if (num_delegations > STATEID_HASH_SIZE * 4)
196 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700197 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
198 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700200 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700201 INIT_LIST_HEAD(&dp->dl_perfile);
202 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 INIT_LIST_HEAD(&dp->dl_recall_lru);
204 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700205 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 dp->dl_file = fp;
207 dp->dl_flock = NULL;
208 get_file(stp->st_vfs_file);
209 dp->dl_vfs_file = stp->st_vfs_file;
210 dp->dl_type = type;
211 dp->dl_recall.cbr_dp = NULL;
212 dp->dl_recall.cbr_ident = cb->cb_ident;
213 dp->dl_recall.cbr_trunc = 0;
214 dp->dl_stateid.si_boot = boot_time;
215 dp->dl_stateid.si_stateownerid = current_delegid++;
216 dp->dl_stateid.si_fileid = 0;
217 dp->dl_stateid.si_generation = 0;
218 dp->dl_fhlen = current_fh->fh_handle.fh_size;
219 memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
220 current_fh->fh_handle.fh_size);
221 dp->dl_time = 0;
222 atomic_set(&dp->dl_count, 1);
NeilBrownea1da632005-06-23 22:04:17 -0700223 list_add(&dp->dl_perfile, &fp->fi_delegations);
224 list_add(&dp->dl_perclnt, &clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return dp;
226}
227
228void
229nfs4_put_delegation(struct nfs4_delegation *dp)
230{
231 if (atomic_dec_and_test(&dp->dl_count)) {
232 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700233 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700234 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700235 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237}
238
239/* Remove the associated file_lock first, then remove the delegation.
240 * lease_modify() is called to remove the FS_LEASE file_lock from
241 * the i_flock list, eventually calling nfsd's lock_manager
242 * fl_release_callback.
243 */
244static void
245nfs4_close_delegation(struct nfs4_delegation *dp)
246{
247 struct file *filp = dp->dl_vfs_file;
248
249 dprintk("NFSD: close_delegation dp %p\n",dp);
250 dp->dl_vfs_file = NULL;
251 /* The following nfsd_close may not actually close the file,
252 * but we want to remove the lease in any case. */
NeilBrownc9071322005-04-16 15:26:38 -0700253 if (dp->dl_flock)
254 setlease(filp, F_UNLCK, &dp->dl_flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/* Called under the state lock. */
259static void
260unhash_delegation(struct nfs4_delegation *dp)
261{
NeilBrownea1da632005-06-23 22:04:17 -0700262 list_del_init(&dp->dl_perfile);
263 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 spin_lock(&recall_lock);
265 list_del_init(&dp->dl_recall_lru);
266 spin_unlock(&recall_lock);
267 nfs4_close_delegation(dp);
268 nfs4_put_delegation(dp);
269}
270
271/*
272 * SETCLIENTID state
273 */
274
275/* Hash tables for nfs4_clientid state */
276#define CLIENT_HASH_BITS 4
277#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
278#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
279
280#define clientid_hashval(id) \
281 ((id) & CLIENT_HASH_MASK)
NeilBrowna55370a2005-06-23 22:03:52 -0700282#define clientstr_hashval(name) \
283 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/*
285 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
286 * used in reboot/reset lease grace period processing
287 *
288 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
289 * setclientid_confirmed info.
290 *
291 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
292 * setclientid info.
293 *
294 * client_lru holds client queue ordered by nfs4_client.cl_time
295 * for lease renewal.
296 *
297 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
298 * for last close replay.
299 */
300static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
301static int reclaim_str_hashtbl_size = 0;
302static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
303static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
304static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
305static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
306static struct list_head client_lru;
307static struct list_head close_lru;
308
309static inline void
310renew_client(struct nfs4_client *clp)
311{
312 /*
313 * Move client to the end to the LRU list.
314 */
315 dprintk("renewing client (clientid %08x/%08x)\n",
316 clp->cl_clientid.cl_boot,
317 clp->cl_clientid.cl_id);
318 list_move_tail(&clp->cl_lru, &client_lru);
319 clp->cl_time = get_seconds();
320}
321
322/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
323static int
324STALE_CLIENTID(clientid_t *clid)
325{
326 if (clid->cl_boot == boot_time)
327 return 0;
328 dprintk("NFSD stale clientid (%08x/%08x)\n",
329 clid->cl_boot, clid->cl_id);
330 return 1;
331}
332
333/*
334 * XXX Should we use a slab cache ?
335 * This type of memory management is somewhat inefficient, but we use it
336 * anyway since SETCLIENTID is not a common operation.
337 */
338static inline struct nfs4_client *
339alloc_client(struct xdr_netobj name)
340{
341 struct nfs4_client *clp;
342
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700343 if ((clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
345 memcpy(clp->cl_name.data, name.data, name.len);
346 clp->cl_name.len = name.len;
347 }
348 else {
349 kfree(clp);
350 clp = NULL;
351 }
352 }
353 return clp;
354}
355
356static inline void
357free_client(struct nfs4_client *clp)
358{
359 if (clp->cl_cred.cr_group_info)
360 put_group_info(clp->cl_cred.cr_group_info);
361 kfree(clp->cl_name.data);
362 kfree(clp);
363}
364
365void
366put_nfs4_client(struct nfs4_client *clp)
367{
368 if (atomic_dec_and_test(&clp->cl_count))
369 free_client(clp);
370}
371
372static void
NeilBrown4e2fd492006-04-10 22:55:39 -0700373shutdown_callback_client(struct nfs4_client *clp)
374{
375 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
376
377 /* shutdown rpc client, ending any outstanding recall rpcs */
378 if (clnt) {
379 clp->cl_callback.cb_client = NULL;
380 rpc_shutdown_client(clnt);
381 rpciod_down();
382 }
383}
384
385static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386expire_client(struct nfs4_client *clp)
387{
388 struct nfs4_stateowner *sop;
389 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct list_head reaplist;
391
392 dprintk("NFSD: expire_client cl_count %d\n",
393 atomic_read(&clp->cl_count));
394
NeilBrown4e2fd492006-04-10 22:55:39 -0700395 shutdown_callback_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 INIT_LIST_HEAD(&reaplist);
398 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700399 while (!list_empty(&clp->cl_delegations)) {
400 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
402 dp->dl_flock);
NeilBrownea1da632005-06-23 22:04:17 -0700403 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 list_move(&dp->dl_recall_lru, &reaplist);
405 }
406 spin_unlock(&recall_lock);
407 while (!list_empty(&reaplist)) {
408 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
409 list_del_init(&dp->dl_recall_lru);
410 unhash_delegation(dp);
411 }
412 list_del(&clp->cl_idhash);
413 list_del(&clp->cl_strhash);
414 list_del(&clp->cl_lru);
NeilBrownea1da632005-06-23 22:04:17 -0700415 while (!list_empty(&clp->cl_openowners)) {
416 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 release_stateowner(sop);
418 }
419 put_nfs4_client(clp);
420}
421
422static struct nfs4_client *
NeilBrowna55370a2005-06-23 22:03:52 -0700423create_client(struct xdr_netobj name, char *recdir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct nfs4_client *clp;
425
426 if (!(clp = alloc_client(name)))
427 goto out;
NeilBrowna55370a2005-06-23 22:03:52 -0700428 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 atomic_set(&clp->cl_count, 1);
430 atomic_set(&clp->cl_callback.cb_set, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 INIT_LIST_HEAD(&clp->cl_idhash);
432 INIT_LIST_HEAD(&clp->cl_strhash);
NeilBrownea1da632005-06-23 22:04:17 -0700433 INIT_LIST_HEAD(&clp->cl_openowners);
434 INIT_LIST_HEAD(&clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 INIT_LIST_HEAD(&clp->cl_lru);
436out:
437 return clp;
438}
439
440static void
441copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
442 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
443}
444
445static void
446copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
447 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
448 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
449}
450
451static void
452copy_cred(struct svc_cred *target, struct svc_cred *source) {
453
454 target->cr_uid = source->cr_uid;
455 target->cr_gid = source->cr_gid;
456 target->cr_group_info = source->cr_group_info;
457 get_group_info(target->cr_group_info);
458}
459
NeilBrowna55370a2005-06-23 22:03:52 -0700460static inline int
461same_name(const char *n1, const char *n2) {
462 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465static int
466cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
467 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
468}
469
470static int
471cmp_clid(clientid_t * cl1, clientid_t * cl2) {
472 return((cl1->cl_boot == cl2->cl_boot) &&
473 (cl1->cl_id == cl2->cl_id));
474}
475
476/* XXX what about NGROUP */
477static int
478cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
479 return(cr1->cr_uid == cr2->cr_uid);
480
481}
482
483static void
484gen_clid(struct nfs4_client *clp) {
485 clp->cl_clientid.cl_boot = boot_time;
486 clp->cl_clientid.cl_id = current_clientid++;
487}
488
489static void
490gen_confirm(struct nfs4_client *clp) {
491 struct timespec tv;
492 u32 * p;
493
494 tv = CURRENT_TIME;
495 p = (u32 *)clp->cl_confirm.data;
496 *p++ = tv.tv_sec;
497 *p++ = tv.tv_nsec;
498}
499
500static int
501check_name(struct xdr_netobj name) {
502
503 if (name.len == 0)
504 return 0;
505 if (name.len > NFS4_OPAQUE_LIMIT) {
506 printk("NFSD: check_name: name too long(%d)!\n", name.len);
507 return 0;
508 }
509 return 1;
510}
511
NeilBrownfd39ca92005-06-23 22:04:03 -0700512static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
514{
515 unsigned int idhashval;
516
517 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
518 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
519 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
520 list_add_tail(&clp->cl_lru, &client_lru);
521 clp->cl_time = get_seconds();
522}
523
NeilBrownfd39ca92005-06-23 22:04:03 -0700524static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525move_to_confirmed(struct nfs4_client *clp)
526{
527 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
528 unsigned int strhashval;
529
530 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
531 list_del_init(&clp->cl_strhash);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700532 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -0700533 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
535 renew_client(clp);
536}
537
538static struct nfs4_client *
539find_confirmed_client(clientid_t *clid)
540{
541 struct nfs4_client *clp;
542 unsigned int idhashval = clientid_hashval(clid->cl_id);
543
544 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
545 if (cmp_clid(&clp->cl_clientid, clid))
546 return clp;
547 }
548 return NULL;
549}
550
551static struct nfs4_client *
552find_unconfirmed_client(clientid_t *clid)
553{
554 struct nfs4_client *clp;
555 unsigned int idhashval = clientid_hashval(clid->cl_id);
556
557 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
558 if (cmp_clid(&clp->cl_clientid, clid))
559 return clp;
560 }
561 return NULL;
562}
563
NeilBrown28ce6052005-06-23 22:03:56 -0700564static struct nfs4_client *
565find_confirmed_client_by_str(const char *dname, unsigned int hashval)
566{
567 struct nfs4_client *clp;
568
569 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
570 if (same_name(clp->cl_recdir, dname))
571 return clp;
572 }
573 return NULL;
574}
575
576static struct nfs4_client *
577find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
578{
579 struct nfs4_client *clp;
580
581 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
582 if (same_name(clp->cl_recdir, dname))
583 return clp;
584 }
585 return NULL;
586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/* a helper function for parse_callback */
589static int
590parse_octet(unsigned int *lenp, char **addrp)
591{
592 unsigned int len = *lenp;
593 char *p = *addrp;
594 int n = -1;
595 char c;
596
597 for (;;) {
598 if (!len)
599 break;
600 len--;
601 c = *p++;
602 if (c == '.')
603 break;
604 if ((c < '0') || (c > '9')) {
605 n = -1;
606 break;
607 }
608 if (n < 0)
609 n = 0;
610 n = (n * 10) + (c - '0');
611 if (n > 255) {
612 n = -1;
613 break;
614 }
615 }
616 *lenp = len;
617 *addrp = p;
618 return n;
619}
620
621/* parse and set the setclientid ipv4 callback address */
NeilBrownfd39ca92005-06-23 22:04:03 -0700622static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
624{
625 int temp = 0;
626 u32 cbaddr = 0;
627 u16 cbport = 0;
628 u32 addrlen = addr_len;
629 char *addr = addr_val;
630 int i, shift;
631
632 /* ipaddress */
633 shift = 24;
634 for(i = 4; i > 0 ; i--) {
635 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
636 return 0;
637 }
638 cbaddr |= (temp << shift);
639 if (shift > 0)
640 shift -= 8;
641 }
642 *cbaddrp = cbaddr;
643
644 /* port */
645 shift = 8;
646 for(i = 2; i > 0 ; i--) {
647 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
648 return 0;
649 }
650 cbport |= (temp << shift);
651 if (shift > 0)
652 shift -= 8;
653 }
654 *cbportp = cbport;
655 return 1;
656}
657
NeilBrownfd39ca92005-06-23 22:04:03 -0700658static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
660{
661 struct nfs4_callback *cb = &clp->cl_callback;
662
663 /* Currently, we only support tcp for the callback channel */
664 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
665 goto out_err;
666
667 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
668 &cb->cb_addr, &cb->cb_port)))
669 goto out_err;
670 cb->cb_prog = se->se_callback_prog;
671 cb->cb_ident = se->se_callback_ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return;
673out_err:
Neil Brown849823c2005-09-13 01:25:36 -0700674 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 "will not receive delegations\n",
676 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return;
679}
680
681/*
682 * RFC 3010 has a complex implmentation description of processing a
683 * SETCLIENTID request consisting of 5 bullets, labeled as
684 * CASE0 - CASE4 below.
685 *
686 * NOTES:
687 * callback information will be processed in a future patch
688 *
689 * an unconfirmed record is added when:
690 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
691 * CASE 1: confirmed record found with matching name, principal,
692 * verifier, and clientid.
693 * CASE 2: confirmed record found with matching name, principal,
694 * and there is no unconfirmed record with matching
695 * name and principal
696 *
697 * an unconfirmed record is replaced when:
698 * CASE 3: confirmed record found with matching name, principal,
699 * and an unconfirmed record is found with matching
700 * name, principal, and with clientid and
701 * confirm that does not match the confirmed record.
702 * CASE 4: there is no confirmed record with matching name and
703 * principal. there is an unconfirmed record with
704 * matching name, principal.
705 *
706 * an unconfirmed record is deleted when:
707 * CASE 1: an unconfirmed record that matches input name, verifier,
708 * and confirmed clientid.
709 * CASE 4: any unconfirmed records with matching name and principal
710 * that exist after an unconfirmed record has been replaced
711 * as described above.
712 *
713 */
Al Virob37ad282006-10-19 23:28:59 -0700714__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800715nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
716 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Chuck Lever27459f02007-02-12 00:53:34 -0800718 struct sockaddr_in *sin = svc_addr_in(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 struct xdr_netobj clname = {
720 .len = setclid->se_namelen,
721 .data = setclid->se_name,
722 };
723 nfs4_verifier clverifier = setclid->se_verf;
724 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -0700725 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -0700726 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -0700727 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -0700730 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
NeilBrowna55370a2005-06-23 22:03:52 -0700732 status = nfs4_make_rec_clidname(dname, &clname);
733 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -0700734 return status;
NeilBrowna55370a2005-06-23 22:03:52 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /*
737 * XXX The Duplicate Request Cache (DRC) has been checked (??)
738 * We get here on a DRC miss.
739 */
740
NeilBrowna55370a2005-06-23 22:03:52 -0700741 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 nfs4_lock_state();
NeilBrown28ce6052005-06-23 22:03:56 -0700744 conf = find_confirmed_client_by_str(dname, strhashval);
745 if (conf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /*
747 * CASE 0:
748 * clname match, confirmed, different principal
749 * or different ip_address
750 */
751 status = nfserr_clid_inuse;
NeilBrown28ce6052005-06-23 22:03:56 -0700752 if (!cmp_creds(&conf->cl_cred, &rqstp->rq_cred)
Chuck Lever27459f02007-02-12 00:53:34 -0800753 || conf->cl_addr != sin->sin_addr.s_addr) {
Bruce Fields21315ed2007-03-26 21:32:09 -0800754 dprintk("NFSD: setclientid: string in use by client"
755 "at %u.%u.%u.%u\n", NIPQUAD(conf->cl_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 goto out;
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
NeilBrown28ce6052005-06-23 22:03:56 -0700759 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 status = nfserr_resource;
761 if (!conf) {
762 /*
763 * CASE 4:
764 * placed first, because it is the normal case.
765 */
766 if (unconf)
767 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700768 new = create_client(clname, dname);
769 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto out;
771 copy_verf(new, &clverifier);
Chuck Lever27459f02007-02-12 00:53:34 -0800772 new->cl_addr = sin->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 copy_cred(&new->cl_cred,&rqstp->rq_cred);
774 gen_clid(new);
775 gen_confirm(new);
776 gen_callback(new, setclid);
777 add_to_unconfirmed(new, strhashval);
778 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
779 /*
780 * CASE 1:
781 * cl_name match, confirmed, principal match
782 * verifier match: probable callback update
783 *
784 * remove any unconfirmed nfs4_client with
785 * matching cl_name, cl_verifier, and cl_clientid
786 *
787 * create and insert an unconfirmed nfs4_client with same
788 * cl_name, cl_verifier, and cl_clientid as existing
789 * nfs4_client, but with the new callback info and a
790 * new cl_confirm
791 */
NeilBrown31f4a6c2005-06-23 22:04:06 -0700792 if (unconf) {
793 /* Note this is removing unconfirmed {*x***},
794 * which is stronger than RFC recommended {vxc**}.
795 * This has the advantage that there is at most
796 * one {*x***} in either list at any time.
797 */
798 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
NeilBrowna55370a2005-06-23 22:03:52 -0700800 new = create_client(clname, dname);
801 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto out;
803 copy_verf(new,&conf->cl_verifier);
Chuck Lever27459f02007-02-12 00:53:34 -0800804 new->cl_addr = sin->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 copy_cred(&new->cl_cred,&rqstp->rq_cred);
806 copy_clid(new, conf);
807 gen_confirm(new);
808 gen_callback(new, setclid);
809 add_to_unconfirmed(new,strhashval);
810 } else if (!unconf) {
811 /*
812 * CASE 2:
813 * clname match, confirmed, principal match
814 * verfier does not match
815 * no unconfirmed. create a new unconfirmed nfs4_client
816 * using input clverifier, clname, and callback info
817 * and generate a new cl_clientid and cl_confirm.
818 */
NeilBrowna55370a2005-06-23 22:03:52 -0700819 new = create_client(clname, dname);
820 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto out;
822 copy_verf(new,&clverifier);
Chuck Lever27459f02007-02-12 00:53:34 -0800823 new->cl_addr = sin->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 copy_cred(&new->cl_cred,&rqstp->rq_cred);
825 gen_clid(new);
826 gen_confirm(new);
827 gen_callback(new, setclid);
828 add_to_unconfirmed(new, strhashval);
829 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
830 /*
831 * CASE3:
832 * confirmed found (name, principal match)
833 * confirmed verifier does not match input clverifier
834 *
835 * unconfirmed found (name match)
836 * confirmed->cl_confirm != unconfirmed->cl_confirm
837 *
838 * remove unconfirmed.
839 *
840 * create an unconfirmed nfs4_client
841 * with same cl_name as existing confirmed nfs4_client,
842 * but with new callback info, new cl_clientid,
843 * new cl_verifier and a new cl_confirm
844 */
845 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700846 new = create_client(clname, dname);
847 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 goto out;
849 copy_verf(new,&clverifier);
Chuck Lever27459f02007-02-12 00:53:34 -0800850 new->cl_addr = sin->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 copy_cred(&new->cl_cred,&rqstp->rq_cred);
852 gen_clid(new);
853 gen_confirm(new);
854 gen_callback(new, setclid);
855 add_to_unconfirmed(new, strhashval);
856 } else {
857 /* No cases hit !!! */
858 status = nfserr_inval;
859 goto out;
860
861 }
862 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
863 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
864 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
865 status = nfs_ok;
866out:
867 nfs4_unlock_state();
868 return status;
869}
870
871
872/*
873 * RFC 3010 has a complex implmentation description of processing a
874 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
875 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
876 *
877 * NOTE: callback information will be processed here in a future patch
878 */
Al Virob37ad282006-10-19 23:28:59 -0700879__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800880nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
881 struct nfsd4_compound_state *cstate,
882 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Chuck Lever27459f02007-02-12 00:53:34 -0800884 struct sockaddr_in *sin = svc_addr_in(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -0700885 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
887 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -0700888 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 if (STALE_CLIENTID(clid))
891 return nfserr_stale_clientid;
892 /*
893 * XXX The Duplicate Request Cache (DRC) has been checked (??)
894 * We get here on a DRC miss.
895 */
896
897 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -0700898
899 conf = find_confirmed_client(clid);
900 unconf = find_unconfirmed_client(clid);
901
902 status = nfserr_clid_inuse;
Chuck Lever27459f02007-02-12 00:53:34 -0800903 if (conf && conf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -0700904 goto out;
Chuck Lever27459f02007-02-12 00:53:34 -0800905 if (unconf && unconf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -0700906 goto out;
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if ((conf && unconf) &&
909 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
910 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
NeilBrowna55370a2005-06-23 22:03:52 -0700911 (same_name(conf->cl_recdir,unconf->cl_recdir)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
NeilBrown7c79f732005-06-23 22:04:13 -0700913 /* CASE 1:
914 * unconf record that matches input clientid and input confirm.
915 * conf record that matches input clientid.
916 * conf and unconf records match names, verifiers
917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
919 status = nfserr_clid_inuse;
920 else {
NeilBrown1a69c172005-06-23 22:04:08 -0700921 /* XXX: We just turn off callbacks until we can handle
922 * change request correctly. */
NeilBrowncb36d632005-06-23 22:04:23 -0700923 atomic_set(&conf->cl_callback.cb_set, 0);
NeilBrown21ab45a2005-06-23 22:04:14 -0700924 gen_confirm(conf);
NeilBrown46309022005-07-07 17:59:10 -0700925 nfsd4_remove_clid_dir(unconf);
NeilBrown1a69c172005-06-23 22:04:08 -0700926 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
NeilBrown7c79f732005-06-23 22:04:13 -0700930 } else if ((conf && !unconf) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 ((conf && unconf) &&
932 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
NeilBrowna55370a2005-06-23 22:03:52 -0700933 !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
NeilBrown7c79f732005-06-23 22:04:13 -0700934 /* CASE 2:
935 * conf record that matches input clientid.
936 * if unconf record matches input clientid, then
937 * unconf->cl_name or unconf->cl_verifier don't match the
938 * conf record.
939 */
NeilBrown21ab45a2005-06-23 22:04:14 -0700940 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -0700942 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -0700944 } else if (!conf && unconf
945 && cmp_verf(&unconf->cl_confirm, &confirm)) {
946 /* CASE 3:
947 * conf record not found.
948 * unconf record found.
949 * unconf->cl_confirm matches input confirm
950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
952 status = nfserr_clid_inuse;
953 } else {
NeilBrown1a69c172005-06-23 22:04:08 -0700954 unsigned int hash =
955 clientstr_hashval(unconf->cl_recdir);
956 conf = find_confirmed_client_by_str(unconf->cl_recdir,
957 hash);
958 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -0700959 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -0700960 expire_client(conf);
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -0700963 conf = unconf;
NeilBrown1a69c172005-06-23 22:04:08 -0700964 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
NeilBrown7c79f732005-06-23 22:04:13 -0700966 } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm)))
967 && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm,
968 &confirm)))) {
969 /* CASE 4:
970 * conf record not found, or if conf, conf->cl_confirm does not
971 * match input confirm.
972 * unconf record not found, or if unconf, unconf->cl_confirm
973 * does not match input confirm.
974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -0700976 } else {
NeilBrown08e89872005-06-23 22:04:11 -0700977 /* check that we have hit one of the cases...*/
978 status = nfserr_clid_inuse;
979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980out:
981 if (!status)
NeilBrown21ab45a2005-06-23 22:04:14 -0700982 nfsd4_probe_callback(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 nfs4_unlock_state();
984 return status;
985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987/* OPEN Share state helper functions */
988static inline struct nfs4_file *
989alloc_init_file(struct inode *ino)
990{
991 struct nfs4_file *fp;
992 unsigned int hashval = file_hashval(ino);
993
NeilBrowne60d4392005-06-23 22:03:01 -0700994 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
995 if (fp) {
NeilBrown13cd2182005-06-23 22:03:10 -0700996 kref_init(&fp->fi_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -0700998 INIT_LIST_HEAD(&fp->fi_stateids);
999 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
1001 fp->fi_inode = igrab(ino);
1002 fp->fi_id = current_fileid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return fp;
1004 }
1005 return NULL;
1006}
1007
1008static void
Christoph Lametere18b8902006-12-06 20:33:20 -08001009nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07001010{
NeilBrowne60d4392005-06-23 22:03:01 -07001011 if (*slab == NULL)
1012 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001013 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001014 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07001015}
1016
1017static void
1018nfsd4_free_slabs(void)
1019{
1020 nfsd4_free_slab(&stateowner_slab);
1021 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07001022 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001023 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026static int
1027nfsd4_init_slabs(void)
1028{
1029 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1030 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001031 if (stateowner_slab == NULL)
1032 goto out_nomem;
1033 file_slab = kmem_cache_create("nfsd4_files",
1034 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1035 if (file_slab == NULL)
1036 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07001037 stateid_slab = kmem_cache_create("nfsd4_stateids",
1038 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1039 if (stateid_slab == NULL)
1040 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07001041 deleg_slab = kmem_cache_create("nfsd4_delegations",
1042 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1043 if (deleg_slab == NULL)
1044 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07001046out_nomem:
1047 nfsd4_free_slabs();
1048 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1049 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052void
1053nfs4_free_stateowner(struct kref *kref)
1054{
1055 struct nfs4_stateowner *sop =
1056 container_of(kref, struct nfs4_stateowner, so_ref);
1057 kfree(sop->so_owner.data);
1058 kmem_cache_free(stateowner_slab, sop);
1059}
1060
1061static inline struct nfs4_stateowner *
1062alloc_stateowner(struct xdr_netobj *owner)
1063{
1064 struct nfs4_stateowner *sop;
1065
1066 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1067 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1068 memcpy(sop->so_owner.data, owner->data, owner->len);
1069 sop->so_owner.len = owner->len;
1070 kref_init(&sop->so_ref);
1071 return sop;
1072 }
1073 kmem_cache_free(stateowner_slab, sop);
1074 }
1075 return NULL;
1076}
1077
1078static struct nfs4_stateowner *
1079alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1080 struct nfs4_stateowner *sop;
1081 struct nfs4_replay *rp;
1082 unsigned int idhashval;
1083
1084 if (!(sop = alloc_stateowner(&open->op_owner)))
1085 return NULL;
1086 idhashval = ownerid_hashval(current_ownerid);
1087 INIT_LIST_HEAD(&sop->so_idhash);
1088 INIT_LIST_HEAD(&sop->so_strhash);
1089 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001090 INIT_LIST_HEAD(&sop->so_stateids);
1091 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 INIT_LIST_HEAD(&sop->so_close_lru);
1093 sop->so_time = 0;
1094 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1095 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001096 list_add(&sop->so_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 sop->so_is_open_owner = 1;
1098 sop->so_id = current_ownerid++;
1099 sop->so_client = clp;
1100 sop->so_seqid = open->op_seqid;
1101 sop->so_confirmed = 0;
1102 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08001103 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 rp->rp_buflen = 0;
1105 rp->rp_buf = rp->rp_ibuf;
1106 return sop;
1107}
1108
1109static void
1110release_stateid_lockowners(struct nfs4_stateid *open_stp)
1111{
1112 struct nfs4_stateowner *lock_sop;
1113
NeilBrownea1da632005-06-23 22:04:17 -07001114 while (!list_empty(&open_stp->st_lockowners)) {
1115 lock_sop = list_entry(open_stp->st_lockowners.next,
1116 struct nfs4_stateowner, so_perstateid);
1117 /* list_del(&open_stp->st_lockowners); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 BUG_ON(lock_sop->so_is_open_owner);
1119 release_stateowner(lock_sop);
1120 }
1121}
1122
1123static void
1124unhash_stateowner(struct nfs4_stateowner *sop)
1125{
1126 struct nfs4_stateid *stp;
1127
1128 list_del(&sop->so_idhash);
1129 list_del(&sop->so_strhash);
NeilBrown6fa305d2005-06-23 22:03:06 -07001130 if (sop->so_is_open_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 list_del(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001132 list_del(&sop->so_perstateid);
1133 while (!list_empty(&sop->so_stateids)) {
1134 stp = list_entry(sop->so_stateids.next,
1135 struct nfs4_stateid, st_perstateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (sop->so_is_open_owner)
1137 release_stateid(stp, OPEN_STATE);
1138 else
1139 release_stateid(stp, LOCK_STATE);
1140 }
1141}
1142
1143static void
1144release_stateowner(struct nfs4_stateowner *sop)
1145{
1146 unhash_stateowner(sop);
1147 list_del(&sop->so_close_lru);
1148 nfs4_put_stateowner(sop);
1149}
1150
1151static inline void
1152init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1153 struct nfs4_stateowner *sop = open->op_stateowner;
1154 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1155
1156 INIT_LIST_HEAD(&stp->st_hash);
NeilBrownea1da632005-06-23 22:04:17 -07001157 INIT_LIST_HEAD(&stp->st_perstateowner);
1158 INIT_LIST_HEAD(&stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 INIT_LIST_HEAD(&stp->st_perfile);
1160 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001161 list_add(&stp->st_perstateowner, &sop->so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07001162 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07001164 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 stp->st_file = fp;
1166 stp->st_stateid.si_boot = boot_time;
1167 stp->st_stateid.si_stateownerid = sop->so_id;
1168 stp->st_stateid.si_fileid = fp->fi_id;
1169 stp->st_stateid.si_generation = 0;
1170 stp->st_access_bmap = 0;
1171 stp->st_deny_bmap = 0;
1172 __set_bit(open->op_share_access, &stp->st_access_bmap);
1173 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07001174 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177static void
1178release_stateid(struct nfs4_stateid *stp, int flags)
1179{
1180 struct file *filp = stp->st_vfs_file;
1181
1182 list_del(&stp->st_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 list_del(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07001184 list_del(&stp->st_perstateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (flags & OPEN_STATE) {
1186 release_stateid_lockowners(stp);
1187 stp->st_vfs_file = NULL;
1188 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 } else if (flags & LOCK_STATE)
1190 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
NeilBrown13cd2182005-06-23 22:03:10 -07001191 put_nfs4_file(stp->st_file);
NeilBrown5ac049a2005-06-23 22:03:03 -07001192 kmem_cache_free(stateid_slab, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
NeilBrownfd39ca92005-06-23 22:04:03 -07001195static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196move_to_close_lru(struct nfs4_stateowner *sop)
1197{
1198 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1199
NeilBrown358dd552006-04-10 22:55:42 -07001200 list_move_tail(&sop->so_close_lru, &close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 sop->so_time = get_seconds();
1202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204static int
1205cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1206 return ((sop->so_owner.len == owner->len) &&
1207 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1208 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1209}
1210
1211static struct nfs4_stateowner *
1212find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1213{
1214 struct nfs4_stateowner *so = NULL;
1215
1216 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1217 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1218 return so;
1219 }
1220 return NULL;
1221}
1222
1223/* search file_hashtbl[] for file */
1224static struct nfs4_file *
1225find_file(struct inode *ino)
1226{
1227 unsigned int hashval = file_hashval(ino);
1228 struct nfs4_file *fp;
1229
1230 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07001231 if (fp->fi_inode == ino) {
1232 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07001234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236 return NULL;
1237}
1238
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001239static int access_valid(u32 x)
1240{
1241 return (x > 0 && x < 4);
1242}
1243
1244static int deny_valid(u32 x)
1245{
1246 return (x >= 0 && x < 5);
1247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
NeilBrownfd39ca92005-06-23 22:04:03 -07001249static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250set_access(unsigned int *access, unsigned long bmap) {
1251 int i;
1252
1253 *access = 0;
1254 for (i = 1; i < 4; i++) {
1255 if (test_bit(i, &bmap))
1256 *access |= i;
1257 }
1258}
1259
NeilBrownfd39ca92005-06-23 22:04:03 -07001260static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261set_deny(unsigned int *deny, unsigned long bmap) {
1262 int i;
1263
1264 *deny = 0;
1265 for (i = 0; i < 4; i++) {
1266 if (test_bit(i, &bmap))
1267 *deny |= i ;
1268 }
1269}
1270
1271static int
1272test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1273 unsigned int access, deny;
1274
1275 set_access(&access, stp->st_access_bmap);
1276 set_deny(&deny, stp->st_deny_bmap);
1277 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1278 return 0;
1279 return 1;
1280}
1281
1282/*
1283 * Called to check deny when READ with all zero stateid or
1284 * WRITE with all zero or all one stateid
1285 */
Al Virob37ad282006-10-19 23:28:59 -07001286static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1288{
1289 struct inode *ino = current_fh->fh_dentry->d_inode;
1290 struct nfs4_file *fp;
1291 struct nfs4_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07001292 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 dprintk("NFSD: nfs4_share_conflict\n");
1295
1296 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07001297 if (!fp)
1298 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07001299 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07001301 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1302 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1303 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1304 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
NeilBrown13cd2182005-06-23 22:03:10 -07001306 ret = nfs_ok;
1307out:
1308 put_nfs4_file(fp);
1309 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312static inline void
1313nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1314{
1315 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001316 put_write_access(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1318 }
1319}
1320
1321/*
1322 * Recall a delegation
1323 */
1324static int
1325do_recall(void *__dp)
1326{
1327 struct nfs4_delegation *dp = __dp;
1328
1329 daemonize("nfsv4-recall");
1330
1331 nfsd4_cb_recall(dp);
1332 return 0;
1333}
1334
1335/*
1336 * Spawn a thread to perform a recall on the delegation represented
1337 * by the lease (file_lock)
1338 *
1339 * Called from break_lease() with lock_kernel() held.
1340 * Note: we assume break_lease will only call this *once* for any given
1341 * lease.
1342 */
1343static
1344void nfsd_break_deleg_cb(struct file_lock *fl)
1345{
1346 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1347 struct task_struct *t;
1348
1349 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1350 if (!dp)
1351 return;
1352
1353 /* We're assuming the state code never drops its reference
1354 * without first removing the lease. Since we're in this lease
1355 * callback (and since the lease code is serialized by the kernel
1356 * lock) we know the server hasn't removed the lease yet, we know
1357 * it's safe to take a reference: */
1358 atomic_inc(&dp->dl_count);
1359
1360 spin_lock(&recall_lock);
1361 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1362 spin_unlock(&recall_lock);
1363
1364 /* only place dl_time is set. protected by lock_kernel*/
1365 dp->dl_time = get_seconds();
1366
1367 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1368 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1369
1370 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1371 if (IS_ERR(t)) {
1372 struct nfs4_client *clp = dp->dl_client;
1373
1374 printk(KERN_INFO "NFSD: Callback thread failed for "
1375 "for client (clientid %08x/%08x)\n",
1376 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1377 nfs4_put_delegation(dp);
1378 }
1379}
1380
1381/*
1382 * The file_lock is being reapd.
1383 *
1384 * Called by locks_free_lock() with lock_kernel() held.
1385 */
1386static
1387void nfsd_release_deleg_cb(struct file_lock *fl)
1388{
1389 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1390
1391 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1392
1393 if (!(fl->fl_flags & FL_LEASE) || !dp)
1394 return;
1395 dp->dl_flock = NULL;
1396}
1397
1398/*
1399 * Set the delegation file_lock back pointer.
1400 *
1401 * Called from __setlease() with lock_kernel() held.
1402 */
1403static
1404void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1405{
1406 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1407
1408 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1409 if (!dp)
1410 return;
1411 dp->dl_flock = new;
1412}
1413
1414/*
1415 * Called from __setlease() with lock_kernel() held
1416 */
1417static
1418int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1419{
1420 struct nfs4_delegation *onlistd =
1421 (struct nfs4_delegation *)onlist->fl_owner;
1422 struct nfs4_delegation *tryd =
1423 (struct nfs4_delegation *)try->fl_owner;
1424
1425 if (onlist->fl_lmops != try->fl_lmops)
1426 return 0;
1427
1428 return onlistd->dl_client == tryd->dl_client;
1429}
1430
1431
1432static
1433int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1434{
1435 if (arg & F_UNLCK)
1436 return lease_modify(onlist, arg);
1437 else
1438 return -EAGAIN;
1439}
1440
NeilBrownfd39ca92005-06-23 22:04:03 -07001441static struct lock_manager_operations nfsd_lease_mng_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 .fl_break = nfsd_break_deleg_cb,
1443 .fl_release_private = nfsd_release_deleg_cb,
1444 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1445 .fl_mylease = nfsd_same_client_deleg_cb,
1446 .fl_change = nfsd_change_deleg_cb,
1447};
1448
1449
Al Virob37ad282006-10-19 23:28:59 -07001450__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451nfsd4_process_open1(struct nfsd4_open *open)
1452{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 clientid_t *clientid = &open->op_clientid;
1454 struct nfs4_client *clp = NULL;
1455 unsigned int strhashval;
1456 struct nfs4_stateowner *sop = NULL;
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001459 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 if (STALE_CLIENTID(&open->op_clientid))
1462 return nfserr_stale_clientid;
1463
1464 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1465 sop = find_openstateowner_str(strhashval, open);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001466 open->op_stateowner = sop;
1467 if (!sop) {
1468 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 clp = find_confirmed_client(clientid);
1470 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001471 return nfserr_expired;
1472 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001474 if (!sop->so_confirmed) {
1475 /* Replace unconfirmed owners without checking for replay. */
1476 clp = sop->so_client;
1477 release_stateowner(sop);
1478 open->op_stateowner = NULL;
1479 goto renew;
1480 }
1481 if (open->op_seqid == sop->so_seqid - 1) {
1482 if (sop->so_replay.rp_buflen)
Al Viroa90b0612006-10-19 23:29:03 -07001483 return nfserr_replay_me;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001484 /* The original OPEN failed so spectacularly
1485 * that we don't even have replay data saved!
1486 * Therefore, we have no choice but to continue
1487 * processing this OPEN; presumably, we'll
1488 * fail again for the same reason.
1489 */
1490 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1491 goto renew;
1492 }
1493 if (open->op_seqid != sop->so_seqid)
1494 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495renew:
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001496 if (open->op_stateowner == NULL) {
1497 sop = alloc_init_open_stateowner(strhashval, clp, open);
1498 if (sop == NULL)
1499 return nfserr_resource;
1500 open->op_stateowner = sop;
1501 }
1502 list_del_init(&sop->so_close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 renew_client(sop->so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001504 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Al Virob37ad282006-10-19 23:28:59 -07001507static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07001508nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1509{
1510 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1511 return nfserr_openmode;
1512 else
1513 return nfs_ok;
1514}
1515
NeilBrown52f4fb42005-06-23 22:02:49 -07001516static struct nfs4_delegation *
1517find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1518{
1519 struct nfs4_delegation *dp;
1520
NeilBrownea1da632005-06-23 22:04:17 -07001521 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
NeilBrown52f4fb42005-06-23 22:02:49 -07001522 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1523 return dp;
1524 }
1525 return NULL;
1526}
1527
Al Virob37ad282006-10-19 23:28:59 -07001528static __be32
NeilBrown567d9822005-06-23 22:02:53 -07001529nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1530 struct nfs4_delegation **dp)
1531{
1532 int flags;
Al Virob37ad282006-10-19 23:28:59 -07001533 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07001534
1535 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1536 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07001537 goto out;
NeilBrown567d9822005-06-23 22:02:53 -07001538 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1539 RD_STATE : WR_STATE;
1540 status = nfs4_check_delegmode(*dp, flags);
1541 if (status)
1542 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001543out:
1544 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1545 return nfs_ok;
1546 if (status)
1547 return status;
1548 open->op_stateowner->so_confirmed = 1;
1549 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07001550}
1551
Al Virob37ad282006-10-19 23:28:59 -07001552static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1554{
1555 struct nfs4_stateid *local;
Al Virob37ad282006-10-19 23:28:59 -07001556 __be32 status = nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 struct nfs4_stateowner *sop = open->op_stateowner;
1558
NeilBrown8beefa22005-06-23 22:03:08 -07001559 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* ignore lock owners */
1561 if (local->st_stateowner->so_is_open_owner == 0)
1562 continue;
1563 /* remember if we have seen this open owner */
1564 if (local->st_stateowner == sop)
1565 *stpp = local;
1566 /* check for conflicting share reservations */
1567 if (!test_share(local, open))
1568 goto out;
1569 }
1570 status = 0;
1571out:
1572 return status;
1573}
1574
NeilBrown5ac049a2005-06-23 22:03:03 -07001575static inline struct nfs4_stateid *
1576nfs4_alloc_stateid(void)
1577{
1578 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1579}
1580
Al Virob37ad282006-10-19 23:28:59 -07001581static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
NeilBrown567d9822005-06-23 22:02:53 -07001583 struct nfs4_delegation *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct svc_fh *cur_fh, int flags)
1585{
1586 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
NeilBrown5ac049a2005-06-23 22:03:03 -07001588 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (stp == NULL)
1590 return nfserr_resource;
1591
NeilBrown567d9822005-06-23 22:02:53 -07001592 if (dp) {
1593 get_file(dp->dl_vfs_file);
1594 stp->st_vfs_file = dp->dl_vfs_file;
1595 } else {
Al Virob37ad282006-10-19 23:28:59 -07001596 __be32 status;
NeilBrown567d9822005-06-23 22:02:53 -07001597 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1598 &stp->st_vfs_file);
1599 if (status) {
1600 if (status == nfserr_dropit)
1601 status = nfserr_jukebox;
NeilBrown5ac049a2005-06-23 22:03:03 -07001602 kmem_cache_free(stateid_slab, stp);
NeilBrown567d9822005-06-23 22:02:53 -07001603 return status;
1604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 *stpp = stp;
1607 return 0;
1608}
1609
Al Virob37ad282006-10-19 23:28:59 -07001610static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1612 struct nfsd4_open *open)
1613{
1614 struct iattr iattr = {
1615 .ia_valid = ATTR_SIZE,
1616 .ia_size = 0,
1617 };
1618 if (!open->op_truncate)
1619 return 0;
1620 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08001621 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1623}
1624
Al Virob37ad282006-10-19 23:28:59 -07001625static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1627{
1628 struct file *filp = stp->st_vfs_file;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001629 struct inode *inode = filp->f_path.dentry->d_inode;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001630 unsigned int share_access, new_writer;
Al Virob37ad282006-10-19 23:28:59 -07001631 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 set_access(&share_access, stp->st_access_bmap);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001634 new_writer = (~share_access) & open->op_share_access
1635 & NFS4_SHARE_ACCESS_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001637 if (new_writer) {
Al Virob37ad282006-10-19 23:28:59 -07001638 int err = get_write_access(inode);
1639 if (err)
1640 return nfserrno(err);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 status = nfsd4_truncate(rqstp, cur_fh, open);
1643 if (status) {
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001644 if (new_writer)
1645 put_write_access(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return status;
1647 }
1648 /* remember the open */
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001649 filp->f_mode |= open->op_share_access;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 set_bit(open->op_share_access, &stp->st_access_bmap);
1651 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1652
1653 return nfs_ok;
1654}
1655
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657static void
NeilBrown37515172005-07-07 17:59:16 -07001658nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
NeilBrown37515172005-07-07 17:59:16 -07001660 open->op_stateowner->so_confirmed = 1;
1661 open->op_stateowner->so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
1664/*
1665 * Attempt to hand out a delegation.
1666 */
1667static void
1668nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1669{
1670 struct nfs4_delegation *dp;
1671 struct nfs4_stateowner *sop = stp->st_stateowner;
1672 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1673 struct file_lock fl, *flp = &fl;
1674 int status, flag = 0;
1675
1676 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07001677 open->op_recall = 0;
1678 switch (open->op_claim_type) {
1679 case NFS4_OPEN_CLAIM_PREVIOUS:
1680 if (!atomic_read(&cb->cb_set))
1681 open->op_recall = 1;
1682 flag = open->op_delegate_type;
1683 if (flag == NFS4_OPEN_DELEGATE_NONE)
1684 goto out;
1685 break;
1686 case NFS4_OPEN_CLAIM_NULL:
1687 /* Let's not give out any delegations till everyone's
1688 * had the chance to reclaim theirs.... */
1689 if (nfs4_in_grace())
1690 goto out;
1691 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1692 goto out;
1693 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1694 flag = NFS4_OPEN_DELEGATE_WRITE;
1695 else
1696 flag = NFS4_OPEN_DELEGATE_READ;
1697 break;
1698 default:
1699 goto out;
1700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1703 if (dp == NULL) {
1704 flag = NFS4_OPEN_DELEGATE_NONE;
1705 goto out;
1706 }
1707 locks_init_lock(&fl);
1708 fl.fl_lmops = &nfsd_lease_mng_ops;
1709 fl.fl_flags = FL_LEASE;
1710 fl.fl_end = OFFSET_MAX;
1711 fl.fl_owner = (fl_owner_t)dp;
1712 fl.fl_file = stp->st_vfs_file;
1713 fl.fl_pid = current->tgid;
1714
1715 /* setlease checks to see if delegation should be handed out.
1716 * the lock_manager callbacks fl_mylease and fl_change are used
1717 */
1718 if ((status = setlease(stp->st_vfs_file,
1719 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1720 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
NeilBrownc9071322005-04-16 15:26:38 -07001721 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 flag = NFS4_OPEN_DELEGATE_NONE;
1723 goto out;
1724 }
1725
1726 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1727
1728 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1729 dp->dl_stateid.si_boot,
1730 dp->dl_stateid.si_stateownerid,
1731 dp->dl_stateid.si_fileid,
1732 dp->dl_stateid.si_generation);
1733out:
NeilBrown7b190fe2005-06-23 22:03:23 -07001734 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1735 && flag == NFS4_OPEN_DELEGATE_NONE
1736 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1737 printk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 open->op_delegate_type = flag;
1739}
1740
1741/*
1742 * called with nfs4_lock_state() held.
1743 */
Al Virob37ad282006-10-19 23:28:59 -07001744__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1746{
1747 struct nfs4_file *fp = NULL;
1748 struct inode *ino = current_fh->fh_dentry->d_inode;
1749 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07001750 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07001751 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 status = nfserr_inval;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001754 if (!access_valid(open->op_share_access)
1755 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 goto out;
1757 /*
1758 * Lookup file; if found, lookup stateid and check open request,
1759 * and check for delegations in the process of being recalled.
1760 * If not found, create the nfs4_file struct
1761 */
1762 fp = find_file(ino);
1763 if (fp) {
1764 if ((status = nfs4_check_open(fp, open, &stp)))
1765 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001766 status = nfs4_check_deleg(fp, open, &dp);
1767 if (status)
1768 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07001770 status = nfserr_bad_stateid;
1771 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1772 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 status = nfserr_resource;
1774 fp = alloc_init_file(ino);
1775 if (fp == NULL)
1776 goto out;
1777 }
1778
1779 /*
1780 * OPEN the file, or upgrade an existing OPEN.
1781 * If truncate fails, the OPEN fails.
1782 */
1783 if (stp) {
1784 /* Stateid was found, this is an OPEN upgrade */
1785 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1786 if (status)
1787 goto out;
NeilBrown444c2c02005-07-07 17:59:22 -07001788 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 } else {
1790 /* Stateid was not found, this is a new OPEN */
1791 int flags = 0;
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -07001792 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
1793 flags |= MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -07001795 flags |= MAY_WRITE;
NeilBrown567d9822005-06-23 22:02:53 -07001796 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1797 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto out;
1799 init_stateid(stp, fp, open);
1800 status = nfsd4_truncate(rqstp, current_fh, open);
1801 if (status) {
1802 release_stateid(stp, OPEN_STATE);
1803 goto out;
1804 }
1805 }
1806 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1807
1808 /*
1809 * Attempt to hand out a delegation. No error return, because the
1810 * OPEN succeeds even if we fail.
1811 */
1812 nfs4_open_delegation(current_fh, open, stp);
1813
1814 status = nfs_ok;
1815
1816 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1817 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1818 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1819out:
NeilBrown13cd2182005-06-23 22:03:10 -07001820 if (fp)
1821 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07001822 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1823 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /*
1825 * To finish the open response, we just need to set the rflags.
1826 */
1827 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1828 if (!open->op_stateowner->so_confirmed)
1829 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1830
1831 return status;
1832}
1833
NeilBrown58da2822005-06-23 22:03:19 -07001834static struct workqueue_struct *laundry_wq;
David Howellsc4028952006-11-22 14:57:56 +00001835static void laundromat_main(struct work_struct *);
1836static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Al Virob37ad282006-10-19 23:28:59 -07001838__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001839nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1840 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07001843 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 nfs4_lock_state();
1846 dprintk("process_renew(%08x/%08x): starting\n",
1847 clid->cl_boot, clid->cl_id);
1848 status = nfserr_stale_clientid;
1849 if (STALE_CLIENTID(clid))
1850 goto out;
1851 clp = find_confirmed_client(clid);
1852 status = nfserr_expired;
1853 if (clp == NULL) {
1854 /* We assume the client took too long to RENEW. */
1855 dprintk("nfsd4_renew: clientid not found!\n");
1856 goto out;
1857 }
1858 renew_client(clp);
1859 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07001860 if (!list_empty(&clp->cl_delegations)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 && !atomic_read(&clp->cl_callback.cb_set))
1862 goto out;
1863 status = nfs_ok;
1864out:
1865 nfs4_unlock_state();
1866 return status;
1867}
1868
NeilBrowna76b4312005-06-23 22:04:01 -07001869static void
1870end_grace(void)
1871{
1872 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07001873 nfsd4_recdir_purge_old();
NeilBrowna76b4312005-06-23 22:04:01 -07001874 in_grace = 0;
1875}
1876
NeilBrownfd39ca92005-06-23 22:04:03 -07001877static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878nfs4_laundromat(void)
1879{
1880 struct nfs4_client *clp;
1881 struct nfs4_stateowner *sop;
1882 struct nfs4_delegation *dp;
1883 struct list_head *pos, *next, reaplist;
1884 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1885 time_t t, clientid_val = NFSD_LEASE_TIME;
1886 time_t u, test_val = NFSD_LEASE_TIME;
1887
1888 nfs4_lock_state();
1889
1890 dprintk("NFSD: laundromat service - starting\n");
NeilBrowna76b4312005-06-23 22:04:01 -07001891 if (in_grace)
1892 end_grace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 list_for_each_safe(pos, next, &client_lru) {
1894 clp = list_entry(pos, struct nfs4_client, cl_lru);
1895 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1896 t = clp->cl_time - cutoff;
1897 if (clientid_val > t)
1898 clientid_val = t;
1899 break;
1900 }
1901 dprintk("NFSD: purging unused client (clientid %08x)\n",
1902 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07001903 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 expire_client(clp);
1905 }
1906 INIT_LIST_HEAD(&reaplist);
1907 spin_lock(&recall_lock);
1908 list_for_each_safe(pos, next, &del_recall_lru) {
1909 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1910 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1911 u = dp->dl_time - cutoff;
1912 if (test_val > u)
1913 test_val = u;
1914 break;
1915 }
1916 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1917 dp, dp->dl_flock);
1918 list_move(&dp->dl_recall_lru, &reaplist);
1919 }
1920 spin_unlock(&recall_lock);
1921 list_for_each_safe(pos, next, &reaplist) {
1922 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1923 list_del_init(&dp->dl_recall_lru);
1924 unhash_delegation(dp);
1925 }
1926 test_val = NFSD_LEASE_TIME;
1927 list_for_each_safe(pos, next, &close_lru) {
1928 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1929 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1930 u = sop->so_time - cutoff;
1931 if (test_val > u)
1932 test_val = u;
1933 break;
1934 }
1935 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1936 sop->so_id);
NeilBrown358dd552006-04-10 22:55:42 -07001937 release_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1940 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1941 nfs4_unlock_state();
1942 return clientid_val;
1943}
1944
1945void
David Howellsc4028952006-11-22 14:57:56 +00001946laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 time_t t;
1949
1950 t = nfs4_laundromat();
1951 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07001952 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
NeilBrownfd39ca92005-06-23 22:04:03 -07001955static struct nfs4_stateowner *
NeilBrownf8816512005-07-07 17:59:25 -07001956search_close_lru(u32 st_id, int flags)
1957{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 struct nfs4_stateowner *local = NULL;
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (flags & CLOSE_STATE) {
1961 list_for_each_entry(local, &close_lru, so_close_lru) {
1962 if (local->so_id == st_id)
1963 return local;
1964 }
1965 }
1966 return NULL;
1967}
1968
1969static inline int
1970nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1971{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001972 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
1975static int
1976STALE_STATEID(stateid_t *stateid)
1977{
1978 if (stateid->si_boot == boot_time)
1979 return 0;
Neil Brown849823c2005-09-13 01:25:36 -07001980 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1982 stateid->si_generation);
1983 return 1;
1984}
1985
1986static inline int
1987access_permit_read(unsigned long access_bmap)
1988{
1989 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1990 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1991 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1992}
1993
1994static inline int
1995access_permit_write(unsigned long access_bmap)
1996{
1997 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1998 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
1999}
2000
2001static
Al Virob37ad282006-10-19 23:28:59 -07002002__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Al Virob37ad282006-10-19 23:28:59 -07002004 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2007 goto out;
2008 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2009 goto out;
2010 status = nfs_ok;
2011out:
2012 return status;
2013}
2014
Al Virob37ad282006-10-19 23:28:59 -07002015static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2017{
2018 /* Trying to call delegreturn with a special stateid? Yuch: */
2019 if (!(flags & (RD_STATE | WR_STATE)))
2020 return nfserr_bad_stateid;
2021 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2022 return nfs_ok;
2023 else if (nfs4_in_grace()) {
2024 /* Answer in remaining cases depends on existance of
2025 * conflicting state; so we must wait out the grace period. */
2026 return nfserr_grace;
2027 } else if (flags & WR_STATE)
2028 return nfs4_share_conflict(current_fh,
2029 NFS4_SHARE_DENY_WRITE);
2030 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2031 return nfs4_share_conflict(current_fh,
2032 NFS4_SHARE_DENY_READ);
2033}
2034
2035/*
2036 * Allow READ/WRITE during grace period on recovered state only for files
2037 * that are not able to provide mandatory locking.
2038 */
2039static inline int
2040io_during_grace_disallowed(struct inode *inode, int flags)
2041{
2042 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2043 && MANDATORY_LOCK(inode);
2044}
2045
2046/*
2047* Checks for stateid operations
2048*/
Al Virob37ad282006-10-19 23:28:59 -07002049__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2051{
2052 struct nfs4_stateid *stp = NULL;
2053 struct nfs4_delegation *dp = NULL;
2054 stateid_t *stidp;
2055 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07002056 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2059 stateid->si_boot, stateid->si_stateownerid,
2060 stateid->si_fileid, stateid->si_generation);
2061 if (filpp)
2062 *filpp = NULL;
2063
2064 if (io_during_grace_disallowed(ino, flags))
2065 return nfserr_grace;
2066
2067 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2068 return check_special_stateids(current_fh, stateid, flags);
2069
2070 /* STALE STATEID */
2071 status = nfserr_stale_stateid;
2072 if (STALE_STATEID(stateid))
2073 goto out;
2074
2075 /* BAD STATEID */
2076 status = nfserr_bad_stateid;
2077 if (!stateid->si_fileid) { /* delegation stateid */
2078 if(!(dp = find_delegation_stateid(ino, stateid))) {
2079 dprintk("NFSD: delegation stateid not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 goto out;
2081 }
2082 stidp = &dp->dl_stateid;
2083 } else { /* open or lock stateid */
2084 if (!(stp = find_stateid(stateid, flags))) {
2085 dprintk("NFSD: open or lock stateid not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 goto out;
2087 }
2088 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2089 goto out;
2090 if (!stp->st_stateowner->so_confirmed)
2091 goto out;
2092 stidp = &stp->st_stateid;
2093 }
2094 if (stateid->si_generation > stidp->si_generation)
2095 goto out;
2096
2097 /* OLD STATEID */
2098 status = nfserr_old_stateid;
2099 if (stateid->si_generation < stidp->si_generation)
2100 goto out;
2101 if (stp) {
2102 if ((status = nfs4_check_openmode(stp,flags)))
2103 goto out;
2104 renew_client(stp->st_stateowner->so_client);
2105 if (filpp)
2106 *filpp = stp->st_vfs_file;
2107 } else if (dp) {
2108 if ((status = nfs4_check_delegmode(dp, flags)))
2109 goto out;
2110 renew_client(dp->dl_client);
2111 if (flags & DELEG_RET)
2112 unhash_delegation(dp);
2113 if (filpp)
2114 *filpp = dp->dl_vfs_file;
2115 }
2116 status = nfs_ok;
2117out:
2118 return status;
2119}
2120
NeilBrown4c4cd222005-07-07 17:59:27 -07002121static inline int
2122setlkflg (int type)
2123{
2124 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2125 RD_STATE : WR_STATE;
2126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128/*
2129 * Checks for sequence id mutating operations.
2130 */
Al Virob37ad282006-10-19 23:28:59 -07002131static __be32
NeilBrown4c4cd222005-07-07 17:59:27 -07002132nfs4_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 -07002133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 struct nfs4_stateid *stp;
2135 struct nfs4_stateowner *sop;
2136
2137 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2138 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2139 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2140 stateid->si_generation);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 *stpp = NULL;
2143 *sopp = NULL;
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2146 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002147 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (STALE_STATEID(stateid))
NeilBrown3a4f98b2005-07-07 17:59:26 -07002151 return nfserr_stale_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /*
2153 * We return BAD_STATEID if filehandle doesn't match stateid,
2154 * the confirmed flag is incorrecly set, or the generation
2155 * number is incorrect.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 */
NeilBrownf8816512005-07-07 17:59:25 -07002157 stp = find_stateid(stateid, flags);
2158 if (stp == NULL) {
2159 /*
2160 * Also, we should make sure this isn't just the result of
2161 * a replayed close:
2162 */
2163 sop = search_close_lru(stateid->si_stateownerid, flags);
2164 if (sop == NULL)
2165 return nfserr_bad_stateid;
2166 *sopp = sop;
2167 goto check_replay;
2168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
NeilBrown4c4cd222005-07-07 17:59:27 -07002170 if (lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 struct nfs4_stateowner *sop = stp->st_stateowner;
NeilBrown4c4cd222005-07-07 17:59:27 -07002172 clientid_t *lockclid = &lock->v.new.clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 struct nfs4_client *clp = sop->so_client;
NeilBrown4c4cd222005-07-07 17:59:27 -07002174 int lkflg = 0;
Al Virob37ad282006-10-19 23:28:59 -07002175 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
NeilBrown4c4cd222005-07-07 17:59:27 -07002177 lkflg = setlkflg(lock->lk_type);
2178
2179 if (lock->lk_is_new) {
2180 if (!sop->so_is_open_owner)
2181 return nfserr_bad_stateid;
2182 if (!cmp_clid(&clp->cl_clientid, lockclid))
2183 return nfserr_bad_stateid;
2184 /* stp is the open stateid */
2185 status = nfs4_check_openmode(stp, lkflg);
2186 if (status)
2187 return status;
2188 } else {
2189 /* stp is the lock stateid */
2190 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2191 if (status)
2192 return status;
2193 }
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196
2197 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2198 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002199 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201
2202 *stpp = stp;
2203 *sopp = sop = stp->st_stateowner;
2204
2205 /*
2206 * We now validate the seqid and stateid generation numbers.
2207 * For the moment, we ignore the possibility of
2208 * generation number wraparound.
2209 */
NeilBrownbd9aac52005-07-07 17:59:19 -07002210 if (seqid != sop->so_seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 goto check_replay;
2212
NeilBrown3a4f98b2005-07-07 17:59:26 -07002213 if (sop->so_confirmed && flags & CONFIRM) {
2214 printk("NFSD: preprocess_seqid_op: expected"
2215 " unconfirmed stateowner!\n");
2216 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07002218 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2219 printk("NFSD: preprocess_seqid_op: stateowner not"
2220 " confirmed yet!\n");
2221 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223 if (stateid->si_generation > stp->st_stateid.si_generation) {
2224 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002225 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (stateid->si_generation < stp->st_stateid.si_generation) {
2229 printk("NFSD: preprocess_seqid_op: old stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002230 return nfserr_old_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
NeilBrown52fd0042005-07-07 17:59:24 -07002232 renew_client(sop->so_client);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002233 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235check_replay:
NeilBrownbd9aac52005-07-07 17:59:19 -07002236 if (seqid == sop->so_seqid - 1) {
Neil Brown849823c2005-09-13 01:25:36 -07002237 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /* indicate replay to calling function */
Al Viroa90b0612006-10-19 23:29:03 -07002239 return nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07002241 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2242 sop->so_seqid, seqid);
2243 *sopp = NULL;
2244 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Al Virob37ad282006-10-19 23:28:59 -07002247__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002248nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002249 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Al Virob37ad282006-10-19 23:28:59 -07002251 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 struct nfs4_stateowner *sop;
2253 struct nfs4_stateid *stp;
2254
2255 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002256 (int)cstate->current_fh.fh_dentry->d_name.len,
2257 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002259 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07002260 if (status)
2261 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 nfs4_lock_state();
2264
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002265 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2266 oc->oc_seqid, &oc->oc_req_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 CHECK_FH | CONFIRM | OPEN_STATE,
2268 &oc->oc_stateowner, &stp, NULL)))
2269 goto out;
2270
2271 sop = oc->oc_stateowner;
2272 sop->so_confirmed = 1;
2273 update_stateid(&stp->st_stateid);
2274 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2275 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2276 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2277 stp->st_stateid.si_boot,
2278 stp->st_stateid.si_stateownerid,
2279 stp->st_stateid.si_fileid,
2280 stp->st_stateid.si_generation);
NeilBrownc7b9a452005-06-23 22:04:30 -07002281
2282 nfsd4_create_clid_dir(sop->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283out:
Neil Brownf2327d92005-09-13 01:25:37 -07002284 if (oc->oc_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 nfs4_get_stateowner(oc->oc_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002286 cstate->replay_owner = oc->oc_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 nfs4_unlock_state();
2289 return status;
2290}
2291
2292
2293/*
2294 * unset all bits in union bitmap (bmap) that
2295 * do not exist in share (from successful OPEN_DOWNGRADE)
2296 */
2297static void
2298reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2299{
2300 int i;
2301 for (i = 1; i < 4; i++) {
2302 if ((i & access) != i)
2303 __clear_bit(i, bmap);
2304 }
2305}
2306
2307static void
2308reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2309{
2310 int i;
2311 for (i = 0; i < 4; i++) {
2312 if ((i & deny) != i)
2313 __clear_bit(i, bmap);
2314 }
2315}
2316
Al Virob37ad282006-10-19 23:28:59 -07002317__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002318nfsd4_open_downgrade(struct svc_rqst *rqstp,
2319 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002320 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Al Virob37ad282006-10-19 23:28:59 -07002322 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 struct nfs4_stateid *stp;
2324 unsigned int share_access;
2325
2326 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002327 (int)cstate->current_fh.fh_dentry->d_name.len,
2328 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002330 if (!access_valid(od->od_share_access)
2331 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return nfserr_inval;
2333
2334 nfs4_lock_state();
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002335 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2336 od->od_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 &od->od_stateid,
2338 CHECK_FH | OPEN_STATE,
2339 &od->od_stateowner, &stp, NULL)))
2340 goto out;
2341
2342 status = nfserr_inval;
2343 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2344 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2345 stp->st_access_bmap, od->od_share_access);
2346 goto out;
2347 }
2348 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2349 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2350 stp->st_deny_bmap, od->od_share_deny);
2351 goto out;
2352 }
2353 set_access(&share_access, stp->st_access_bmap);
2354 nfs4_file_downgrade(stp->st_vfs_file,
2355 share_access & ~od->od_share_access);
2356
2357 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2358 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2359
2360 update_stateid(&stp->st_stateid);
2361 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2362 status = nfs_ok;
2363out:
Neil Brownf2327d92005-09-13 01:25:37 -07002364 if (od->od_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 nfs4_get_stateowner(od->od_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002366 cstate->replay_owner = od->od_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 nfs4_unlock_state();
2369 return status;
2370}
2371
2372/*
2373 * nfs4_unlock_state() called after encode
2374 */
Al Virob37ad282006-10-19 23:28:59 -07002375__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002376nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002377 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
Al Virob37ad282006-10-19 23:28:59 -07002379 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 struct nfs4_stateid *stp;
2381
2382 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002383 (int)cstate->current_fh.fh_dentry->d_name.len,
2384 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 nfs4_lock_state();
2387 /* check close_lru for replay */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002388 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2389 close->cl_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 &close->cl_stateid,
2391 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2392 &close->cl_stateowner, &stp, NULL)))
2393 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 status = nfs_ok;
2395 update_stateid(&stp->st_stateid);
2396 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2397
J. Bruce Fields04ef5952006-01-18 17:43:21 -08002398 /* release_stateid() calls nfsd_close() if needed */
2399 release_stateid(stp, OPEN_STATE);
2400
2401 /* place unused nfs4_stateowners on so_close_lru list to be
2402 * released by the laundromat service after the lease period
2403 * to enable us to handle CLOSE replay
2404 */
2405 if (list_empty(&close->cl_stateowner->so_stateids))
2406 move_to_close_lru(close->cl_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407out:
Neil Brownf2327d92005-09-13 01:25:37 -07002408 if (close->cl_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 nfs4_get_stateowner(close->cl_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002410 cstate->replay_owner = close->cl_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 nfs4_unlock_state();
2413 return status;
2414}
2415
Al Virob37ad282006-10-19 23:28:59 -07002416__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002417nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2418 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Al Virob37ad282006-10-19 23:28:59 -07002420 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002422 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 goto out;
2424
2425 nfs4_lock_state();
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002426 status = nfs4_preprocess_stateid_op(&cstate->current_fh,
2427 &dr->dr_stateid, DELEG_RET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 nfs4_unlock_state();
2429out:
2430 return status;
2431}
2432
2433
2434/*
2435 * Lock owner state (byte-range locks)
2436 */
2437#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2438#define LOCK_HASH_BITS 8
2439#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2440#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2441
2442#define lockownerid_hashval(id) \
2443 ((id) & LOCK_HASH_MASK)
2444
2445static inline unsigned int
2446lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2447 struct xdr_netobj *ownername)
2448{
2449 return (file_hashval(inode) + cl_id
2450 + opaque_hashval(ownername->data, ownername->len))
2451 & LOCK_HASH_MASK;
2452}
2453
2454static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2455static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2456static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2457
NeilBrownfd39ca92005-06-23 22:04:03 -07002458static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459find_stateid(stateid_t *stid, int flags)
2460{
2461 struct nfs4_stateid *local = NULL;
2462 u32 st_id = stid->si_stateownerid;
2463 u32 f_id = stid->si_fileid;
2464 unsigned int hashval;
2465
2466 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2467 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2468 hashval = stateid_hashval(st_id, f_id);
2469 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2470 if ((local->st_stateid.si_stateownerid == st_id) &&
2471 (local->st_stateid.si_fileid == f_id))
2472 return local;
2473 }
2474 }
2475 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2476 hashval = stateid_hashval(st_id, f_id);
2477 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2478 if ((local->st_stateid.si_stateownerid == st_id) &&
2479 (local->st_stateid.si_fileid == f_id))
2480 return local;
2481 }
Neil Brown849823c2005-09-13 01:25:36 -07002482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return NULL;
2484}
2485
2486static struct nfs4_delegation *
2487find_delegation_stateid(struct inode *ino, stateid_t *stid)
2488{
NeilBrown13cd2182005-06-23 22:03:10 -07002489 struct nfs4_file *fp;
2490 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2493 stid->si_boot, stid->si_stateownerid,
2494 stid->si_fileid, stid->si_generation);
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002497 if (!fp)
2498 return NULL;
2499 dl = find_delegation_file(fp, stid);
2500 put_nfs4_file(fp);
2501 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
2504/*
2505 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2506 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2507 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2508 * locking, this prevents us from being completely protocol-compliant. The
2509 * real solution to this problem is to start using unsigned file offsets in
2510 * the VFS, but this is a very deep change!
2511 */
2512static inline void
2513nfs4_transform_lock_offset(struct file_lock *lock)
2514{
2515 if (lock->fl_start < 0)
2516 lock->fl_start = OFFSET_MAX;
2517 if (lock->fl_end < 0)
2518 lock->fl_end = OFFSET_MAX;
2519}
2520
NeilBrownd5b90262006-04-10 22:55:22 -07002521/* Hack!: For now, we're defining this just so we can use a pointer to it
2522 * as a unique cookie to identify our (NFSv4's) posix locks. */
Adrian Bunke465a772006-04-10 22:55:23 -07002523static struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07002524};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526static inline void
2527nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2528{
NeilBrownd5b90262006-04-10 22:55:22 -07002529 struct nfs4_stateowner *sop;
2530 unsigned int hval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
NeilBrownd5b90262006-04-10 22:55:22 -07002532 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2533 sop = (struct nfs4_stateowner *) fl->fl_owner;
2534 hval = lockownerid_hashval(sop->so_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 kref_get(&sop->so_ref);
2536 deny->ld_sop = sop;
2537 deny->ld_clientid = sop->so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07002538 } else {
2539 deny->ld_sop = NULL;
2540 deny->ld_clientid.cl_boot = 0;
2541 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
2543 deny->ld_start = fl->fl_start;
2544 deny->ld_length = ~(u64)0;
2545 if (fl->fl_end != ~(u64)0)
2546 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2547 deny->ld_type = NFS4_READ_LT;
2548 if (fl->fl_type != F_RDLCK)
2549 deny->ld_type = NFS4_WRITE_LT;
2550}
2551
2552static struct nfs4_stateowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2554 struct xdr_netobj *owner)
2555{
2556 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2557 struct nfs4_stateowner *op;
2558
2559 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2560 if (cmp_owner_str(op, owner, clid))
2561 return op;
2562 }
2563 return NULL;
2564}
2565
2566/*
2567 * Alloc a lock owner structure.
2568 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2569 * occured.
2570 *
2571 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 */
2573
2574static struct nfs4_stateowner *
2575alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2576 struct nfs4_stateowner *sop;
2577 struct nfs4_replay *rp;
2578 unsigned int idhashval;
2579
2580 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2581 return NULL;
2582 idhashval = lockownerid_hashval(current_ownerid);
2583 INIT_LIST_HEAD(&sop->so_idhash);
2584 INIT_LIST_HEAD(&sop->so_strhash);
2585 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07002586 INIT_LIST_HEAD(&sop->so_stateids);
2587 INIT_LIST_HEAD(&sop->so_perstateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2589 sop->so_time = 0;
2590 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2591 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07002592 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 sop->so_is_open_owner = 0;
2594 sop->so_id = current_ownerid++;
2595 sop->so_client = clp;
Neil Brownb59e3c02005-09-13 01:25:38 -07002596 /* It is the openowner seqid that will be incremented in encode in the
2597 * case of new lockowners; so increment the lock seqid manually: */
2598 sop->so_seqid = lock->lk_new_lock_seqid + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 sop->so_confirmed = 1;
2600 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08002601 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 rp->rp_buflen = 0;
2603 rp->rp_buf = rp->rp_ibuf;
2604 return sop;
2605}
2606
NeilBrownfd39ca92005-06-23 22:04:03 -07002607static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2609{
2610 struct nfs4_stateid *stp;
2611 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2612
NeilBrown5ac049a2005-06-23 22:03:03 -07002613 stp = nfs4_alloc_stateid();
2614 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 goto out;
2616 INIT_LIST_HEAD(&stp->st_hash);
2617 INIT_LIST_HEAD(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07002618 INIT_LIST_HEAD(&stp->st_perstateowner);
2619 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07002621 list_add(&stp->st_perfile, &fp->fi_stateids);
NeilBrownea1da632005-06-23 22:04:17 -07002622 list_add(&stp->st_perstateowner, &sop->so_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07002624 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 stp->st_file = fp;
2626 stp->st_stateid.si_boot = boot_time;
2627 stp->st_stateid.si_stateownerid = sop->so_id;
2628 stp->st_stateid.si_fileid = fp->fi_id;
2629 stp->st_stateid.si_generation = 0;
2630 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2631 stp->st_access_bmap = open_stp->st_access_bmap;
2632 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07002633 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635out:
2636 return stp;
2637}
2638
NeilBrownfd39ca92005-06-23 22:04:03 -07002639static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640check_lock_length(u64 offset, u64 length)
2641{
2642 return ((length == 0) || ((length != ~(u64)0) &&
2643 LOFF_OVERFLOW(offset, length)));
2644}
2645
2646/*
2647 * LOCK operation
2648 */
Al Virob37ad282006-10-19 23:28:59 -07002649__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002650nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002651 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
NeilBrown3e9e3db2005-06-23 22:04:20 -07002653 struct nfs4_stateowner *open_sop = NULL;
Neil Brownb59e3c02005-09-13 01:25:38 -07002654 struct nfs4_stateowner *lock_sop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 struct nfs4_stateid *lock_stp;
2656 struct file *filp;
2657 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05002658 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07002659 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 unsigned int strhashval;
Marc Eshel150b3932007-01-18 16:15:35 -05002661 unsigned int cmd;
Al Virob8dd7b92006-10-19 23:29:01 -07002662 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2665 (long long) lock->lk_offset,
2666 (long long) lock->lk_length);
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (check_lock_length(lock->lk_offset, lock->lk_length))
2669 return nfserr_inval;
2670
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002671 if ((status = fh_verify(rqstp, &cstate->current_fh,
2672 S_IFREG, MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08002673 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2674 return status;
2675 }
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 nfs4_lock_state();
2678
2679 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07002680 /*
2681 * Client indicates that this is a new lockowner.
2682 * Use open owner and open stateid to create lock owner and
2683 * lock stateid.
2684 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 struct nfs4_stateid *open_stp = NULL;
2686 struct nfs4_file *fp;
2687
2688 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002689 if (STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 /* validate and update open stateid and open seqid */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002693 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 lock->lk_new_open_seqid,
2695 &lock->lk_new_open_stateid,
2696 CHECK_FH | OPEN_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002697 &lock->lk_replay_owner, &open_stp,
Neil Brownb59e3c02005-09-13 01:25:38 -07002698 lock);
NeilBrown37515172005-07-07 17:59:16 -07002699 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002701 open_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 /* create lockowner and lock stateid */
2703 fp = open_stp->st_file;
2704 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2705 open_sop->so_client->cl_clientid.cl_id,
2706 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07002707 /* XXX: Do we need to check for duplicate stateowners on
2708 * the same file, or should they just be allowed (and
2709 * create new stateids)? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 status = nfserr_resource;
Neil Brownb59e3c02005-09-13 01:25:38 -07002711 lock_sop = alloc_init_lock_stateowner(strhashval,
2712 open_sop->so_client, open_stp, lock);
2713 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07002715 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08002716 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 } else {
2719 /* lock (lock owner + lock stateid) already exists */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002720 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 lock->lk_old_lock_seqid,
2722 &lock->lk_old_lock_stateid,
2723 CHECK_FH | LOCK_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002724 &lock->lk_replay_owner, &lock_stp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (status)
2726 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002727 lock_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
J. Bruce Fields3a655882006-01-18 17:43:19 -08002729 /* lock->lk_replay_owner and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 filp = lock_stp->st_vfs_file;
2731
NeilBrown0dd395d2005-07-07 17:59:15 -07002732 status = nfserr_grace;
2733 if (nfs4_in_grace() && !lock->lk_reclaim)
2734 goto out;
2735 status = nfserr_no_grace;
2736 if (!nfs4_in_grace() && lock->lk_reclaim)
2737 goto out;
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 locks_init_lock(&file_lock);
2740 switch (lock->lk_type) {
2741 case NFS4_READ_LT:
2742 case NFS4_READW_LT:
2743 file_lock.fl_type = F_RDLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05002744 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 break;
2746 case NFS4_WRITE_LT:
2747 case NFS4_WRITEW_LT:
2748 file_lock.fl_type = F_WRLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05002749 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 break;
2751 default:
2752 status = nfserr_inval;
2753 goto out;
2754 }
Neil Brownb59e3c02005-09-13 01:25:38 -07002755 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 file_lock.fl_pid = current->tgid;
2757 file_lock.fl_file = filp;
2758 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002759 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 file_lock.fl_start = lock->lk_offset;
2762 if ((lock->lk_length == ~(u64)0) ||
2763 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2764 file_lock.fl_end = ~(u64)0;
2765 else
2766 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2767 nfs4_transform_lock_offset(&file_lock);
2768
2769 /*
2770 * Try to lock the file in the VFS.
2771 * Note: locks.c uses the BKL to protect the inode's lock list.
2772 */
2773
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002774 /* XXX?: Just to divert the locks_release_private at the start of
2775 * locks_copy_lock: */
Marc Eshel150b3932007-01-18 16:15:35 -05002776 locks_init_lock(&conflock);
Marc Eshelfd85b812006-11-28 16:26:41 -05002777 err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07002778 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 case 0: /* success! */
2780 update_stateid(&lock_stp->st_stateid);
2781 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2782 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07002783 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002784 break;
2785 case (EAGAIN): /* conflock holds conflicting lock */
2786 status = nfserr_denied;
2787 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2788 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 case (EDEADLK):
2791 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05002794 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002795 status = nfserr_resource;
2796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08002799 if (status && lock->lk_is_new && lock_sop)
2800 release_stateowner(lock_sop);
J. Bruce Fields3a655882006-01-18 17:43:19 -08002801 if (lock->lk_replay_owner) {
2802 nfs4_get_stateowner(lock->lk_replay_owner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002803 cstate->replay_owner = lock->lk_replay_owner;
Neil Brownf2327d92005-09-13 01:25:37 -07002804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 nfs4_unlock_state();
2806 return status;
2807}
2808
2809/*
2810 * LOCKT operation
2811 */
Al Virob37ad282006-10-19 23:28:59 -07002812__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002813nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2814 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
2816 struct inode *inode;
2817 struct file file;
2818 struct file_lock file_lock;
Marc Eshelfd85b812006-11-28 16:26:41 -05002819 int error;
Al Virob37ad282006-10-19 23:28:59 -07002820 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 if (nfs4_in_grace())
2823 return nfserr_grace;
2824
2825 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2826 return nfserr_inval;
2827
2828 lockt->lt_stateowner = NULL;
2829 nfs4_lock_state();
2830
2831 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002832 if (STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002835 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
Neil Brown849823c2005-09-13 01:25:36 -07002836 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (status == nfserr_symlink)
2838 status = nfserr_inval;
2839 goto out;
2840 }
2841
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002842 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 locks_init_lock(&file_lock);
2844 switch (lockt->lt_type) {
2845 case NFS4_READ_LT:
2846 case NFS4_READW_LT:
2847 file_lock.fl_type = F_RDLCK;
2848 break;
2849 case NFS4_WRITE_LT:
2850 case NFS4_WRITEW_LT:
2851 file_lock.fl_type = F_WRLCK;
2852 break;
2853 default:
2854 printk("NFSD: nfs4_lockt: bad lock type!\n");
2855 status = nfserr_inval;
2856 goto out;
2857 }
2858
2859 lockt->lt_stateowner = find_lockstateowner_str(inode,
2860 &lockt->lt_clientid, &lockt->lt_owner);
2861 if (lockt->lt_stateowner)
2862 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2863 file_lock.fl_pid = current->tgid;
2864 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002865 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
2867 file_lock.fl_start = lockt->lt_offset;
2868 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2869 file_lock.fl_end = ~(u64)0;
2870 else
2871 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2872
2873 nfs4_transform_lock_offset(&file_lock);
2874
Marc Eshelfd85b812006-11-28 16:26:41 -05002875 /* vfs_test_lock uses the struct file _only_ to resolve the inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 * since LOCKT doesn't require an OPEN, and therefore a struct
Marc Eshelfd85b812006-11-28 16:26:41 -05002877 * file may not exist, pass vfs_test_lock a struct file with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 * only the dentry:inode set.
2879 */
2880 memset(&file, 0, sizeof (struct file));
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002881 file.f_path.dentry = cstate->current_fh.fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 status = nfs_ok;
Marc Eshelfd85b812006-11-28 16:26:41 -05002884 error = vfs_test_lock(&file, &file_lock);
2885 if (error) {
2886 status = nfserrno(error);
2887 goto out;
2888 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002889 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002891 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
2893out:
2894 nfs4_unlock_state();
2895 return status;
2896}
2897
Al Virob37ad282006-10-19 23:28:59 -07002898__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002899nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002900 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 struct nfs4_stateid *stp;
2903 struct file *filp = NULL;
2904 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07002905 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002906 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2909 (long long) locku->lu_offset,
2910 (long long) locku->lu_length);
2911
2912 if (check_lock_length(locku->lu_offset, locku->lu_length))
2913 return nfserr_inval;
2914
2915 nfs4_lock_state();
2916
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002917 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 locku->lu_seqid,
2919 &locku->lu_stateid,
2920 CHECK_FH | LOCK_STATE,
2921 &locku->lu_stateowner, &stp, NULL)))
2922 goto out;
2923
2924 filp = stp->st_vfs_file;
2925 BUG_ON(!filp);
2926 locks_init_lock(&file_lock);
2927 file_lock.fl_type = F_UNLCK;
2928 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2929 file_lock.fl_pid = current->tgid;
2930 file_lock.fl_file = filp;
2931 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002932 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 file_lock.fl_start = locku->lu_offset;
2934
2935 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2936 file_lock.fl_end = ~(u64)0;
2937 else
2938 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2939 nfs4_transform_lock_offset(&file_lock);
2940
2941 /*
2942 * Try to unlock the file in the VFS.
2943 */
Marc Eshelfd85b812006-11-28 16:26:41 -05002944 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07002945 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05002946 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 goto out_nfserr;
2948 }
2949 /*
2950 * OK, unlock succeeded; the only thing left to do is update the stateid.
2951 */
2952 update_stateid(&stp->st_stateid);
2953 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2954
2955out:
Neil Brownf2327d92005-09-13 01:25:37 -07002956 if (locku->lu_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 nfs4_get_stateowner(locku->lu_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002958 cstate->replay_owner = locku->lu_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 nfs4_unlock_state();
2961 return status;
2962
2963out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002964 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 goto out;
2966}
2967
2968/*
2969 * returns
2970 * 1: locks held by lockowner
2971 * 0: no locks held by lockowner
2972 */
2973static int
2974check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2975{
2976 struct file_lock **flpp;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08002977 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 int status = 0;
2979
2980 lock_kernel();
2981 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08002982 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 status = 1;
2984 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08002985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987out:
2988 unlock_kernel();
2989 return status;
2990}
2991
Al Virob37ad282006-10-19 23:28:59 -07002992__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002993nfsd4_release_lockowner(struct svc_rqst *rqstp,
2994 struct nfsd4_compound_state *cstate,
2995 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
2997 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07002998 struct nfs4_stateowner *sop;
2999 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07003001 struct list_head matches;
3002 int i;
Al Virob37ad282006-10-19 23:28:59 -07003003 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3006 clid->cl_boot, clid->cl_id);
3007
3008 /* XXX check for lease expiration */
3009
3010 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07003011 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 nfs4_lock_state();
3015
NeilBrown3e9e3db2005-06-23 22:04:20 -07003016 status = nfserr_locks_held;
3017 /* XXX: we're doing a linear search through all the lockowners.
3018 * Yipes! For now we'll just hope clients aren't really using
3019 * release_lockowner much, but eventually we have to fix these
3020 * data structures. */
3021 INIT_LIST_HEAD(&matches);
3022 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3023 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
3024 if (!cmp_owner_str(sop, owner, clid))
3025 continue;
3026 list_for_each_entry(stp, &sop->so_stateids,
3027 st_perstateowner) {
3028 if (check_for_locks(stp->st_vfs_file, sop))
3029 goto out;
3030 /* Note: so_perclient unused for lockowners,
3031 * so it's OK to fool with here. */
3032 list_add(&sop->so_perclient, &matches);
3033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07003035 }
3036 /* Clients probably won't expect us to return with some (but not all)
3037 * of the lockowner state released; so don't release any until all
3038 * have been checked. */
3039 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07003040 while (!list_empty(&matches)) {
3041 sop = list_entry(matches.next, struct nfs4_stateowner,
3042 so_perclient);
3043 /* unhash_stateowner deletes so_perclient only
3044 * for openowners. */
3045 list_del(&sop->so_perclient);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003046 release_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
3048out:
3049 nfs4_unlock_state();
3050 return status;
3051}
3052
3053static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07003054alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
NeilBrowna55370a2005-06-23 22:03:52 -07003056 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
NeilBrownc7b9a452005-06-23 22:04:30 -07003059int
3060nfs4_has_reclaimed_state(const char *name)
3061{
3062 unsigned int strhashval = clientstr_hashval(name);
3063 struct nfs4_client *clp;
3064
3065 clp = find_confirmed_client_by_str(name, strhashval);
3066 return clp ? 1 : 0;
3067}
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069/*
3070 * failure => all reset bets are off, nfserr_no_grace...
3071 */
NeilBrown190e4fb2005-06-23 22:04:25 -07003072int
3073nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
3075 unsigned int strhashval;
3076 struct nfs4_client_reclaim *crp = NULL;
3077
NeilBrowna55370a2005-06-23 22:03:52 -07003078 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3079 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 if (!crp)
3081 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07003082 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 INIT_LIST_HEAD(&crp->cr_strhash);
3084 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07003085 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 reclaim_str_hashtbl_size++;
3087 return 1;
3088}
3089
3090static void
3091nfs4_release_reclaim(void)
3092{
3093 struct nfs4_client_reclaim *crp = NULL;
3094 int i;
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3097 while (!list_empty(&reclaim_str_hashtbl[i])) {
3098 crp = list_entry(reclaim_str_hashtbl[i].next,
3099 struct nfs4_client_reclaim, cr_strhash);
3100 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 kfree(crp);
3102 reclaim_str_hashtbl_size--;
3103 }
3104 }
3105 BUG_ON(reclaim_str_hashtbl_size);
3106}
3107
3108/*
3109 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07003110static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111nfs4_find_reclaim_client(clientid_t *clid)
3112{
3113 unsigned int strhashval;
3114 struct nfs4_client *clp;
3115 struct nfs4_client_reclaim *crp = NULL;
3116
3117
3118 /* find clientid in conf_id_hashtbl */
3119 clp = find_confirmed_client(clid);
3120 if (clp == NULL)
3121 return NULL;
3122
NeilBrowna55370a2005-06-23 22:03:52 -07003123 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3124 clp->cl_name.len, clp->cl_name.data,
3125 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07003128 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07003130 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return crp;
3132 }
3133 }
3134 return NULL;
3135}
3136
3137/*
3138* Called from OPEN. Look for clientid in reclaim list.
3139*/
Al Virob37ad282006-10-19 23:28:59 -07003140__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141nfs4_check_open_reclaim(clientid_t *clid)
3142{
NeilBrowndfc83562005-06-23 22:03:16 -07003143 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
NeilBrownac4d8ff22005-06-23 22:03:30 -07003146/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
NeilBrownac4d8ff22005-06-23 22:03:30 -07003148void
3149nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
3151 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3154 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3155 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3156 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3157 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3158 }
3159 for (i = 0; i < FILE_HASH_SIZE; i++) {
3160 INIT_LIST_HEAD(&file_hashtbl[i]);
3161 }
3162 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3163 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3164 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3165 }
3166 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3167 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3168 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3169 }
3170 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3171 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3172 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 INIT_LIST_HEAD(&close_lru);
3176 INIT_LIST_HEAD(&client_lru);
3177 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07003178 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3179 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3180 reclaim_str_hashtbl_size = 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003181}
3182
NeilBrown190e4fb2005-06-23 22:04:25 -07003183static void
3184nfsd4_load_reboot_recovery_data(void)
3185{
3186 int status;
3187
NeilBrown0964a3d2005-06-23 22:04:32 -07003188 nfs4_lock_state();
3189 nfsd4_init_recdir(user_recovery_dirname);
NeilBrown190e4fb2005-06-23 22:04:25 -07003190 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07003191 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07003192 if (status)
3193 printk("NFSD: Failure reading reboot recovery data\n");
3194}
3195
NeilBrownac4d8ff22005-06-23 22:03:30 -07003196/* initialization to perform when the nfsd service is started: */
3197
3198static void
3199__nfs4_state_start(void)
3200{
3201 time_t grace_time;
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 boot_time = get_seconds();
NeilBrownd99a05a2005-06-23 22:03:21 -07003204 grace_time = max(user_lease_time, lease_time);
3205 lease_time = user_lease_time;
NeilBrowna76b4312005-06-23 22:04:01 -07003206 in_grace = 1;
NeilBrownd99a05a2005-06-23 22:03:21 -07003207 printk("NFSD: starting %ld-second grace period\n", grace_time);
NeilBrown58da2822005-06-23 22:03:19 -07003208 laundry_wq = create_singlethread_workqueue("nfsd4");
NeilBrowna76b4312005-06-23 22:04:01 -07003209 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
3212int
NeilBrown76a35502005-06-23 22:03:26 -07003213nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
3215 int status;
3216
3217 if (nfs4_init)
3218 return 0;
3219 status = nfsd4_init_slabs();
3220 if (status)
3221 return status;
NeilBrown190e4fb2005-06-23 22:04:25 -07003222 nfsd4_load_reboot_recovery_data();
NeilBrown76a35502005-06-23 22:03:26 -07003223 __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 nfs4_init = 1;
3225 return 0;
3226}
3227
3228int
3229nfs4_in_grace(void)
3230{
NeilBrowna76b4312005-06-23 22:04:01 -07003231 return in_grace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
3233
3234time_t
3235nfs4_lease_time(void)
3236{
3237 return lease_time;
3238}
3239
3240static void
3241__nfs4_state_shutdown(void)
3242{
3243 int i;
3244 struct nfs4_client *clp = NULL;
3245 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 struct list_head *pos, *next, reaplist;
3247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3249 while (!list_empty(&conf_id_hashtbl[i])) {
3250 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3251 expire_client(clp);
3252 }
3253 while (!list_empty(&unconf_str_hashtbl[i])) {
3254 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3255 expire_client(clp);
3256 }
3257 }
3258 INIT_LIST_HEAD(&reaplist);
3259 spin_lock(&recall_lock);
3260 list_for_each_safe(pos, next, &del_recall_lru) {
3261 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3262 list_move(&dp->dl_recall_lru, &reaplist);
3263 }
3264 spin_unlock(&recall_lock);
3265 list_for_each_safe(pos, next, &reaplist) {
3266 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3267 list_del_init(&dp->dl_recall_lru);
3268 unhash_delegation(dp);
3269 }
3270
NeilBrown190e4fb2005-06-23 22:04:25 -07003271 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 nfs4_init = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
3275void
3276nfs4_state_shutdown(void)
3277{
NeilBrown5e8d5c22006-04-10 22:55:37 -07003278 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3279 destroy_workqueue(laundry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 nfs4_lock_state();
3281 nfs4_release_reclaim();
3282 __nfs4_state_shutdown();
3283 nfsd4_free_slabs();
3284 nfs4_unlock_state();
3285}
3286
NeilBrown0964a3d2005-06-23 22:04:32 -07003287static void
3288nfs4_set_recdir(char *recdir)
3289{
3290 nfs4_lock_state();
3291 strcpy(user_recovery_dirname, recdir);
3292 nfs4_unlock_state();
3293}
3294
3295/*
3296 * Change the NFSv4 recovery directory to recdir.
3297 */
3298int
3299nfs4_reset_recoverydir(char *recdir)
3300{
3301 int status;
3302 struct nameidata nd;
3303
3304 status = path_lookup(recdir, LOOKUP_FOLLOW, &nd);
3305 if (status)
3306 return status;
3307 status = -ENOTDIR;
3308 if (S_ISDIR(nd.dentry->d_inode->i_mode)) {
3309 nfs4_set_recdir(recdir);
3310 status = 0;
3311 }
3312 path_release(&nd);
3313 return status;
3314}
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316/*
3317 * Called when leasetime is changed.
3318 *
NeilBrownd99a05a2005-06-23 22:03:21 -07003319 * The only way the protocol gives us to handle on-the-fly lease changes is to
3320 * simulate a reboot. Instead of doing that, we just wait till the next time
3321 * we start to register any changes in lease time. If the administrator
3322 * really wants to change the lease time *now*, they can go ahead and bring
3323 * nfsd down and then back up again after changing the lease time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 */
3325void
3326nfs4_reset_lease(time_t leasetime)
3327{
NeilBrownd99a05a2005-06-23 22:03:21 -07003328 lock_kernel();
3329 user_lease_time = leasetime;
3330 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}