blob: 96c7578cbe1e82c16d2b720ef4e25e91d546187f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2* linux/fs/nfsd/nfs4state.c
3*
4* Copyright (c) 2001 The Regents of the University of Michigan.
5* All rights reserved.
6*
7* Kendrick Smith <kmsmith@umich.edu>
8* Andy Adamson <kandros@umich.edu>
9*
10* Redistribution and use in source and binary forms, with or without
11* modification, are permitted provided that the following conditions
12* are met:
13*
14* 1. Redistributions of source code must retain the above copyright
15* notice, this list of conditions and the following disclaimer.
16* 2. Redistributions in binary form must reproduce the above copyright
17* notice, this list of conditions and the following disclaimer in the
18* documentation and/or other materials provided with the distribution.
19* 3. Neither the name of the University nor the names of its
20* contributors may be used to endorse or promote products derived
21* from this software without specific prior written permission.
22*
23* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*
35*/
36
37#include <linux/param.h>
38#include <linux/major.h>
39#include <linux/slab.h>
40
41#include <linux/sunrpc/svc.h>
42#include <linux/nfsd/nfsd.h>
43#include <linux/nfsd/cache.h>
44#include <linux/mount.h>
45#include <linux/workqueue.h>
46#include <linux/smp_lock.h>
47#include <linux/kthread.h>
48#include <linux/nfs4.h>
49#include <linux/nfsd/state.h>
50#include <linux/nfsd/xdr4.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070051#include <linux/namei.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080052#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define NFSDDBG_FACILITY NFSDDBG_PROC
55
56/* Globals */
57static time_t lease_time = 90; /* default lease time */
NeilBrownd99a05a2005-06-23 22:03:21 -070058static time_t user_lease_time = 90;
NeilBrownfd39ca92005-06-23 22:04:03 -070059static time_t boot_time;
NeilBrowna76b4312005-06-23 22:04:01 -070060static int in_grace = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static u32 current_clientid = 1;
62static u32 current_ownerid = 1;
63static u32 current_fileid = 1;
64static u32 current_delegid = 1;
65static u32 nfs4_init;
NeilBrownfd39ca92005-06-23 22:04:03 -070066static stateid_t zerostateid; /* bits all 0 */
67static stateid_t onestateid; /* bits all 1 */
68
69#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
70#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
NeilBrownfd39ca92005-06-23 22:04:03 -070073static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
75static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
NeilBrown0964a3d2005-06-23 22:04:32 -070076static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
77static void nfs4_set_recdir(char *recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* Locking:
80 *
Ingo Molnar353ab6e2006-03-26 01:37:12 -080081 * client_mutex:
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * protects clientid_hashtbl[], clientstr_hashtbl[],
83 * unconfstr_hashtbl[], uncofid_hashtbl[].
84 */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080085static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
NeilBrownfd39ca92005-06-23 22:04:03 -070087static kmem_cache_t *stateowner_slab = NULL;
88static kmem_cache_t *file_slab = NULL;
89static kmem_cache_t *stateid_slab = NULL;
90static kmem_cache_t *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092void
93nfs4_lock_state(void)
94{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080095 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98void
99nfs4_unlock_state(void)
100{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800101 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104static inline u32
105opaque_hashval(const void *ptr, int nbytes)
106{
107 unsigned char *cptr = (unsigned char *) ptr;
108
109 u32 x = 0;
110 while (nbytes--) {
111 x *= 37;
112 x += *cptr++;
113 }
114 return x;
115}
116
117/* forward declarations */
118static void release_stateowner(struct nfs4_stateowner *sop);
119static void release_stateid(struct nfs4_stateid *stp, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/*
122 * Delegation state
123 */
124
125/* recall_lock protects the del_recall_lru */
NeilBrownfd39ca92005-06-23 22:04:03 -0700126static spinlock_t recall_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static struct list_head del_recall_lru;
128
NeilBrown13cd2182005-06-23 22:03:10 -0700129static void
130free_nfs4_file(struct kref *kref)
131{
132 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
133 list_del(&fp->fi_hash);
134 iput(fp->fi_inode);
135 kmem_cache_free(file_slab, fp);
136}
137
138static inline void
139put_nfs4_file(struct nfs4_file *fi)
140{
141 kref_put(&fi->fi_ref, free_nfs4_file);
142}
143
144static inline void
145get_nfs4_file(struct nfs4_file *fi)
146{
147 kref_get(&fi->fi_ref);
148}
149
NeilBrownef0f3392006-04-10 22:55:41 -0700150static int num_delegations;
151
152/*
153 * Open owner state (share locks)
154 */
155
156/* hash tables for nfs4_stateowner */
157#define OWNER_HASH_BITS 8
158#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
159#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
160
161#define ownerid_hashval(id) \
162 ((id) & OWNER_HASH_MASK)
163#define ownerstr_hashval(clientid, ownername) \
164 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
165
166static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
167static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
168
169/* hash table for nfs4_file */
170#define FILE_HASH_BITS 8
171#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
172#define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
173/* hash table for (open)nfs4_stateid */
174#define STATEID_HASH_BITS 10
175#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
176#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
177
178#define file_hashval(x) \
179 hash_ptr(x, FILE_HASH_BITS)
180#define stateid_hashval(owner_id, file_id) \
181 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
182
183static struct list_head file_hashtbl[FILE_HASH_SIZE];
184static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static struct nfs4_delegation *
187alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
188{
189 struct nfs4_delegation *dp;
190 struct nfs4_file *fp = stp->st_file;
191 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
192
193 dprintk("NFSD alloc_init_deleg\n");
NeilBrownef0f3392006-04-10 22:55:41 -0700194 if (num_delegations > STATEID_HASH_SIZE * 4)
195 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700196 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
197 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700199 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700200 INIT_LIST_HEAD(&dp->dl_perfile);
201 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 INIT_LIST_HEAD(&dp->dl_recall_lru);
203 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700204 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 dp->dl_file = fp;
206 dp->dl_flock = NULL;
207 get_file(stp->st_vfs_file);
208 dp->dl_vfs_file = stp->st_vfs_file;
209 dp->dl_type = type;
210 dp->dl_recall.cbr_dp = NULL;
211 dp->dl_recall.cbr_ident = cb->cb_ident;
212 dp->dl_recall.cbr_trunc = 0;
213 dp->dl_stateid.si_boot = boot_time;
214 dp->dl_stateid.si_stateownerid = current_delegid++;
215 dp->dl_stateid.si_fileid = 0;
216 dp->dl_stateid.si_generation = 0;
217 dp->dl_fhlen = current_fh->fh_handle.fh_size;
218 memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
219 current_fh->fh_handle.fh_size);
220 dp->dl_time = 0;
221 atomic_set(&dp->dl_count, 1);
NeilBrownea1da632005-06-23 22:04:17 -0700222 list_add(&dp->dl_perfile, &fp->fi_delegations);
223 list_add(&dp->dl_perclnt, &clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return dp;
225}
226
227void
228nfs4_put_delegation(struct nfs4_delegation *dp)
229{
230 if (atomic_dec_and_test(&dp->dl_count)) {
231 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700232 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700233 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700234 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236}
237
238/* Remove the associated file_lock first, then remove the delegation.
239 * lease_modify() is called to remove the FS_LEASE file_lock from
240 * the i_flock list, eventually calling nfsd's lock_manager
241 * fl_release_callback.
242 */
243static void
244nfs4_close_delegation(struct nfs4_delegation *dp)
245{
246 struct file *filp = dp->dl_vfs_file;
247
248 dprintk("NFSD: close_delegation dp %p\n",dp);
249 dp->dl_vfs_file = NULL;
250 /* The following nfsd_close may not actually close the file,
251 * but we want to remove the lease in any case. */
NeilBrownc9071322005-04-16 15:26:38 -0700252 if (dp->dl_flock)
253 setlease(filp, F_UNLCK, &dp->dl_flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257/* Called under the state lock. */
258static void
259unhash_delegation(struct nfs4_delegation *dp)
260{
NeilBrownea1da632005-06-23 22:04:17 -0700261 list_del_init(&dp->dl_perfile);
262 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 spin_lock(&recall_lock);
264 list_del_init(&dp->dl_recall_lru);
265 spin_unlock(&recall_lock);
266 nfs4_close_delegation(dp);
267 nfs4_put_delegation(dp);
268}
269
270/*
271 * SETCLIENTID state
272 */
273
274/* Hash tables for nfs4_clientid state */
275#define CLIENT_HASH_BITS 4
276#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
277#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
278
279#define clientid_hashval(id) \
280 ((id) & CLIENT_HASH_MASK)
NeilBrowna55370a2005-06-23 22:03:52 -0700281#define clientstr_hashval(name) \
282 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/*
284 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
285 * used in reboot/reset lease grace period processing
286 *
287 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
288 * setclientid_confirmed info.
289 *
290 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
291 * setclientid info.
292 *
293 * client_lru holds client queue ordered by nfs4_client.cl_time
294 * for lease renewal.
295 *
296 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
297 * for last close replay.
298 */
299static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
300static int reclaim_str_hashtbl_size = 0;
301static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
302static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
303static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
304static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
305static struct list_head client_lru;
306static struct list_head close_lru;
307
308static inline void
309renew_client(struct nfs4_client *clp)
310{
311 /*
312 * Move client to the end to the LRU list.
313 */
314 dprintk("renewing client (clientid %08x/%08x)\n",
315 clp->cl_clientid.cl_boot,
316 clp->cl_clientid.cl_id);
317 list_move_tail(&clp->cl_lru, &client_lru);
318 clp->cl_time = get_seconds();
319}
320
321/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
322static int
323STALE_CLIENTID(clientid_t *clid)
324{
325 if (clid->cl_boot == boot_time)
326 return 0;
327 dprintk("NFSD stale clientid (%08x/%08x)\n",
328 clid->cl_boot, clid->cl_id);
329 return 1;
330}
331
332/*
333 * XXX Should we use a slab cache ?
334 * This type of memory management is somewhat inefficient, but we use it
335 * anyway since SETCLIENTID is not a common operation.
336 */
337static inline struct nfs4_client *
338alloc_client(struct xdr_netobj name)
339{
340 struct nfs4_client *clp;
341
342 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
343 memset(clp, 0, sizeof(*clp));
344 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
345 memcpy(clp->cl_name.data, name.data, name.len);
346 clp->cl_name.len = name.len;
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);
532 list_del_init(&clp->cl_idhash);
533 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -0700534 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
536 renew_client(clp);
537}
538
539static struct nfs4_client *
540find_confirmed_client(clientid_t *clid)
541{
542 struct nfs4_client *clp;
543 unsigned int idhashval = clientid_hashval(clid->cl_id);
544
545 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
546 if (cmp_clid(&clp->cl_clientid, clid))
547 return clp;
548 }
549 return NULL;
550}
551
552static struct nfs4_client *
553find_unconfirmed_client(clientid_t *clid)
554{
555 struct nfs4_client *clp;
556 unsigned int idhashval = clientid_hashval(clid->cl_id);
557
558 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
559 if (cmp_clid(&clp->cl_clientid, clid))
560 return clp;
561 }
562 return NULL;
563}
564
NeilBrown28ce6052005-06-23 22:03:56 -0700565static struct nfs4_client *
566find_confirmed_client_by_str(const char *dname, unsigned int hashval)
567{
568 struct nfs4_client *clp;
569
570 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
571 if (same_name(clp->cl_recdir, dname))
572 return clp;
573 }
574 return NULL;
575}
576
577static struct nfs4_client *
578find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
579{
580 struct nfs4_client *clp;
581
582 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
583 if (same_name(clp->cl_recdir, dname))
584 return clp;
585 }
586 return NULL;
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/* a helper function for parse_callback */
590static int
591parse_octet(unsigned int *lenp, char **addrp)
592{
593 unsigned int len = *lenp;
594 char *p = *addrp;
595 int n = -1;
596 char c;
597
598 for (;;) {
599 if (!len)
600 break;
601 len--;
602 c = *p++;
603 if (c == '.')
604 break;
605 if ((c < '0') || (c > '9')) {
606 n = -1;
607 break;
608 }
609 if (n < 0)
610 n = 0;
611 n = (n * 10) + (c - '0');
612 if (n > 255) {
613 n = -1;
614 break;
615 }
616 }
617 *lenp = len;
618 *addrp = p;
619 return n;
620}
621
622/* parse and set the setclientid ipv4 callback address */
NeilBrownfd39ca92005-06-23 22:04:03 -0700623static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
625{
626 int temp = 0;
627 u32 cbaddr = 0;
628 u16 cbport = 0;
629 u32 addrlen = addr_len;
630 char *addr = addr_val;
631 int i, shift;
632
633 /* ipaddress */
634 shift = 24;
635 for(i = 4; i > 0 ; i--) {
636 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
637 return 0;
638 }
639 cbaddr |= (temp << shift);
640 if (shift > 0)
641 shift -= 8;
642 }
643 *cbaddrp = cbaddr;
644
645 /* port */
646 shift = 8;
647 for(i = 2; i > 0 ; i--) {
648 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
649 return 0;
650 }
651 cbport |= (temp << shift);
652 if (shift > 0)
653 shift -= 8;
654 }
655 *cbportp = cbport;
656 return 1;
657}
658
NeilBrownfd39ca92005-06-23 22:04:03 -0700659static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
661{
662 struct nfs4_callback *cb = &clp->cl_callback;
663
664 /* Currently, we only support tcp for the callback channel */
665 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
666 goto out_err;
667
668 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
669 &cb->cb_addr, &cb->cb_port)))
670 goto out_err;
671 cb->cb_prog = se->se_callback_prog;
672 cb->cb_ident = se->se_callback_ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return;
674out_err:
Neil Brown849823c2005-09-13 01:25:36 -0700675 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 "will not receive delegations\n",
677 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return;
680}
681
682/*
683 * RFC 3010 has a complex implmentation description of processing a
684 * SETCLIENTID request consisting of 5 bullets, labeled as
685 * CASE0 - CASE4 below.
686 *
687 * NOTES:
688 * callback information will be processed in a future patch
689 *
690 * an unconfirmed record is added when:
691 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
692 * CASE 1: confirmed record found with matching name, principal,
693 * verifier, and clientid.
694 * CASE 2: confirmed record found with matching name, principal,
695 * and there is no unconfirmed record with matching
696 * name and principal
697 *
698 * an unconfirmed record is replaced when:
699 * CASE 3: confirmed record found with matching name, principal,
700 * and an unconfirmed record is found with matching
701 * name, principal, and with clientid and
702 * confirm that does not match the confirmed record.
703 * CASE 4: there is no confirmed record with matching name and
704 * principal. there is an unconfirmed record with
705 * matching name, principal.
706 *
707 * an unconfirmed record is deleted when:
708 * CASE 1: an unconfirmed record that matches input name, verifier,
709 * and confirmed clientid.
710 * CASE 4: any unconfirmed records with matching name and principal
711 * that exist after an unconfirmed record has been replaced
712 * as described above.
713 *
714 */
715int
716nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
717{
718 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
719 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int 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)
753 || conf->cl_addr != ip_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 printk("NFSD: setclientid: string in use by client"
755 "(clientid %08x/%08x)\n",
NeilBrown28ce6052005-06-23 22:03:56 -0700756 conf->cl_clientid.cl_boot, conf->cl_clientid.cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto out;
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
NeilBrown28ce6052005-06-23 22:03:56 -0700760 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 status = nfserr_resource;
762 if (!conf) {
763 /*
764 * CASE 4:
765 * placed first, because it is the normal case.
766 */
767 if (unconf)
768 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700769 new = create_client(clname, dname);
770 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 goto out;
772 copy_verf(new, &clverifier);
773 new->cl_addr = ip_addr;
774 copy_cred(&new->cl_cred,&rqstp->rq_cred);
775 gen_clid(new);
776 gen_confirm(new);
777 gen_callback(new, setclid);
778 add_to_unconfirmed(new, strhashval);
779 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
780 /*
781 * CASE 1:
782 * cl_name match, confirmed, principal match
783 * verifier match: probable callback update
784 *
785 * remove any unconfirmed nfs4_client with
786 * matching cl_name, cl_verifier, and cl_clientid
787 *
788 * create and insert an unconfirmed nfs4_client with same
789 * cl_name, cl_verifier, and cl_clientid as existing
790 * nfs4_client, but with the new callback info and a
791 * new cl_confirm
792 */
NeilBrown31f4a6c2005-06-23 22:04:06 -0700793 if (unconf) {
794 /* Note this is removing unconfirmed {*x***},
795 * which is stronger than RFC recommended {vxc**}.
796 * This has the advantage that there is at most
797 * one {*x***} in either list at any time.
798 */
799 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
NeilBrowna55370a2005-06-23 22:03:52 -0700801 new = create_client(clname, dname);
802 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto out;
804 copy_verf(new,&conf->cl_verifier);
805 new->cl_addr = ip_addr;
806 copy_cred(&new->cl_cred,&rqstp->rq_cred);
807 copy_clid(new, conf);
808 gen_confirm(new);
809 gen_callback(new, setclid);
810 add_to_unconfirmed(new,strhashval);
811 } else if (!unconf) {
812 /*
813 * CASE 2:
814 * clname match, confirmed, principal match
815 * verfier does not match
816 * no unconfirmed. create a new unconfirmed nfs4_client
817 * using input clverifier, clname, and callback info
818 * and generate a new cl_clientid and cl_confirm.
819 */
NeilBrowna55370a2005-06-23 22:03:52 -0700820 new = create_client(clname, dname);
821 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 goto out;
823 copy_verf(new,&clverifier);
824 new->cl_addr = ip_addr;
825 copy_cred(&new->cl_cred,&rqstp->rq_cred);
826 gen_clid(new);
827 gen_confirm(new);
828 gen_callback(new, setclid);
829 add_to_unconfirmed(new, strhashval);
830 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
831 /*
832 * CASE3:
833 * confirmed found (name, principal match)
834 * confirmed verifier does not match input clverifier
835 *
836 * unconfirmed found (name match)
837 * confirmed->cl_confirm != unconfirmed->cl_confirm
838 *
839 * remove unconfirmed.
840 *
841 * create an unconfirmed nfs4_client
842 * with same cl_name as existing confirmed nfs4_client,
843 * but with new callback info, new cl_clientid,
844 * new cl_verifier and a new cl_confirm
845 */
846 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700847 new = create_client(clname, dname);
848 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 goto out;
850 copy_verf(new,&clverifier);
851 new->cl_addr = ip_addr;
852 copy_cred(&new->cl_cred,&rqstp->rq_cred);
853 gen_clid(new);
854 gen_confirm(new);
855 gen_callback(new, setclid);
856 add_to_unconfirmed(new, strhashval);
857 } else {
858 /* No cases hit !!! */
859 status = nfserr_inval;
860 goto out;
861
862 }
863 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
864 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
865 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
866 status = nfs_ok;
867out:
868 nfs4_unlock_state();
869 return status;
870}
871
872
873/*
874 * RFC 3010 has a complex implmentation description of processing a
875 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
876 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
877 *
878 * NOTE: callback information will be processed here in a future patch
879 */
880int
881nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
882{
883 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
NeilBrown21ab45a2005-06-23 22:04:14 -0700884 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
886 clientid_t * clid = &setclientid_confirm->sc_clientid;
887 int status;
888
889 if (STALE_CLIENTID(clid))
890 return nfserr_stale_clientid;
891 /*
892 * XXX The Duplicate Request Cache (DRC) has been checked (??)
893 * We get here on a DRC miss.
894 */
895
896 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -0700897
898 conf = find_confirmed_client(clid);
899 unconf = find_unconfirmed_client(clid);
900
901 status = nfserr_clid_inuse;
902 if (conf && conf->cl_addr != ip_addr)
903 goto out;
904 if (unconf && unconf->cl_addr != ip_addr)
905 goto out;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if ((conf && unconf) &&
908 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
909 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
NeilBrowna55370a2005-06-23 22:03:52 -0700910 (same_name(conf->cl_recdir,unconf->cl_recdir)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
NeilBrown7c79f732005-06-23 22:04:13 -0700912 /* CASE 1:
913 * unconf record that matches input clientid and input confirm.
914 * conf record that matches input clientid.
915 * conf and unconf records match names, verifiers
916 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
918 status = nfserr_clid_inuse;
919 else {
NeilBrown1a69c172005-06-23 22:04:08 -0700920 /* XXX: We just turn off callbacks until we can handle
921 * change request correctly. */
NeilBrowncb36d632005-06-23 22:04:23 -0700922 atomic_set(&conf->cl_callback.cb_set, 0);
NeilBrown21ab45a2005-06-23 22:04:14 -0700923 gen_confirm(conf);
NeilBrown46309022005-07-07 17:59:10 -0700924 nfsd4_remove_clid_dir(unconf);
NeilBrown1a69c172005-06-23 22:04:08 -0700925 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
NeilBrown7c79f732005-06-23 22:04:13 -0700929 } else if ((conf && !unconf) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 ((conf && unconf) &&
931 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
NeilBrowna55370a2005-06-23 22:03:52 -0700932 !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
NeilBrown7c79f732005-06-23 22:04:13 -0700933 /* CASE 2:
934 * conf record that matches input clientid.
935 * if unconf record matches input clientid, then
936 * unconf->cl_name or unconf->cl_verifier don't match the
937 * conf record.
938 */
NeilBrown21ab45a2005-06-23 22:04:14 -0700939 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -0700941 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -0700943 } else if (!conf && unconf
944 && cmp_verf(&unconf->cl_confirm, &confirm)) {
945 /* CASE 3:
946 * conf record not found.
947 * unconf record found.
948 * unconf->cl_confirm matches input confirm
949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
951 status = nfserr_clid_inuse;
952 } else {
NeilBrown1a69c172005-06-23 22:04:08 -0700953 unsigned int hash =
954 clientstr_hashval(unconf->cl_recdir);
955 conf = find_confirmed_client_by_str(unconf->cl_recdir,
956 hash);
957 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -0700958 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -0700959 expire_client(conf);
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -0700962 conf = unconf;
NeilBrown1a69c172005-06-23 22:04:08 -0700963 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
NeilBrown7c79f732005-06-23 22:04:13 -0700965 } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm)))
966 && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm,
967 &confirm)))) {
968 /* CASE 4:
969 * conf record not found, or if conf, conf->cl_confirm does not
970 * match input confirm.
971 * unconf record not found, or if unconf, unconf->cl_confirm
972 * does not match input confirm.
973 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -0700975 } else {
NeilBrown08e89872005-06-23 22:04:11 -0700976 /* check that we have hit one of the cases...*/
977 status = nfserr_clid_inuse;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979out:
980 if (!status)
NeilBrown21ab45a2005-06-23 22:04:14 -0700981 nfsd4_probe_callback(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 nfs4_unlock_state();
983 return status;
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/* OPEN Share state helper functions */
987static inline struct nfs4_file *
988alloc_init_file(struct inode *ino)
989{
990 struct nfs4_file *fp;
991 unsigned int hashval = file_hashval(ino);
992
NeilBrowne60d4392005-06-23 22:03:01 -0700993 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
994 if (fp) {
NeilBrown13cd2182005-06-23 22:03:10 -0700995 kref_init(&fp->fi_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -0700997 INIT_LIST_HEAD(&fp->fi_stateids);
998 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
1000 fp->fi_inode = igrab(ino);
1001 fp->fi_id = current_fileid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return fp;
1003 }
1004 return NULL;
1005}
1006
1007static void
NeilBrowne60d4392005-06-23 22:03:01 -07001008nfsd4_free_slab(kmem_cache_t **slab)
1009{
1010 int status;
1011
1012 if (*slab == NULL)
1013 return;
1014 status = kmem_cache_destroy(*slab);
1015 *slab = NULL;
1016 WARN_ON(status);
1017}
1018
1019static void
1020nfsd4_free_slabs(void)
1021{
1022 nfsd4_free_slab(&stateowner_slab);
1023 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07001024 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001025 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001026}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028static int
1029nfsd4_init_slabs(void)
1030{
1031 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1032 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001033 if (stateowner_slab == NULL)
1034 goto out_nomem;
1035 file_slab = kmem_cache_create("nfsd4_files",
1036 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1037 if (file_slab == NULL)
1038 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07001039 stateid_slab = kmem_cache_create("nfsd4_stateids",
1040 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1041 if (stateid_slab == NULL)
1042 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07001043 deleg_slab = kmem_cache_create("nfsd4_delegations",
1044 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1045 if (deleg_slab == NULL)
1046 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07001048out_nomem:
1049 nfsd4_free_slabs();
1050 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1051 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054void
1055nfs4_free_stateowner(struct kref *kref)
1056{
1057 struct nfs4_stateowner *sop =
1058 container_of(kref, struct nfs4_stateowner, so_ref);
1059 kfree(sop->so_owner.data);
1060 kmem_cache_free(stateowner_slab, sop);
1061}
1062
1063static inline struct nfs4_stateowner *
1064alloc_stateowner(struct xdr_netobj *owner)
1065{
1066 struct nfs4_stateowner *sop;
1067
1068 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1069 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1070 memcpy(sop->so_owner.data, owner->data, owner->len);
1071 sop->so_owner.len = owner->len;
1072 kref_init(&sop->so_ref);
1073 return sop;
1074 }
1075 kmem_cache_free(stateowner_slab, sop);
1076 }
1077 return NULL;
1078}
1079
1080static struct nfs4_stateowner *
1081alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1082 struct nfs4_stateowner *sop;
1083 struct nfs4_replay *rp;
1084 unsigned int idhashval;
1085
1086 if (!(sop = alloc_stateowner(&open->op_owner)))
1087 return NULL;
1088 idhashval = ownerid_hashval(current_ownerid);
1089 INIT_LIST_HEAD(&sop->so_idhash);
1090 INIT_LIST_HEAD(&sop->so_strhash);
1091 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001092 INIT_LIST_HEAD(&sop->so_stateids);
1093 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 INIT_LIST_HEAD(&sop->so_close_lru);
1095 sop->so_time = 0;
1096 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1097 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001098 list_add(&sop->so_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 sop->so_is_open_owner = 1;
1100 sop->so_id = current_ownerid++;
1101 sop->so_client = clp;
1102 sop->so_seqid = open->op_seqid;
1103 sop->so_confirmed = 0;
1104 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08001105 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 rp->rp_buflen = 0;
1107 rp->rp_buf = rp->rp_ibuf;
1108 return sop;
1109}
1110
1111static void
1112release_stateid_lockowners(struct nfs4_stateid *open_stp)
1113{
1114 struct nfs4_stateowner *lock_sop;
1115
NeilBrownea1da632005-06-23 22:04:17 -07001116 while (!list_empty(&open_stp->st_lockowners)) {
1117 lock_sop = list_entry(open_stp->st_lockowners.next,
1118 struct nfs4_stateowner, so_perstateid);
1119 /* list_del(&open_stp->st_lockowners); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 BUG_ON(lock_sop->so_is_open_owner);
1121 release_stateowner(lock_sop);
1122 }
1123}
1124
1125static void
1126unhash_stateowner(struct nfs4_stateowner *sop)
1127{
1128 struct nfs4_stateid *stp;
1129
1130 list_del(&sop->so_idhash);
1131 list_del(&sop->so_strhash);
NeilBrown6fa305d2005-06-23 22:03:06 -07001132 if (sop->so_is_open_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 list_del(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001134 list_del(&sop->so_perstateid);
1135 while (!list_empty(&sop->so_stateids)) {
1136 stp = list_entry(sop->so_stateids.next,
1137 struct nfs4_stateid, st_perstateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (sop->so_is_open_owner)
1139 release_stateid(stp, OPEN_STATE);
1140 else
1141 release_stateid(stp, LOCK_STATE);
1142 }
1143}
1144
1145static void
1146release_stateowner(struct nfs4_stateowner *sop)
1147{
1148 unhash_stateowner(sop);
1149 list_del(&sop->so_close_lru);
1150 nfs4_put_stateowner(sop);
1151}
1152
1153static inline void
1154init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1155 struct nfs4_stateowner *sop = open->op_stateowner;
1156 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1157
1158 INIT_LIST_HEAD(&stp->st_hash);
NeilBrownea1da632005-06-23 22:04:17 -07001159 INIT_LIST_HEAD(&stp->st_perstateowner);
1160 INIT_LIST_HEAD(&stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 INIT_LIST_HEAD(&stp->st_perfile);
1162 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001163 list_add(&stp->st_perstateowner, &sop->so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07001164 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07001166 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 stp->st_file = fp;
1168 stp->st_stateid.si_boot = boot_time;
1169 stp->st_stateid.si_stateownerid = sop->so_id;
1170 stp->st_stateid.si_fileid = fp->fi_id;
1171 stp->st_stateid.si_generation = 0;
1172 stp->st_access_bmap = 0;
1173 stp->st_deny_bmap = 0;
1174 __set_bit(open->op_share_access, &stp->st_access_bmap);
1175 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07001176 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179static void
1180release_stateid(struct nfs4_stateid *stp, int flags)
1181{
1182 struct file *filp = stp->st_vfs_file;
1183
1184 list_del(&stp->st_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 list_del(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07001186 list_del(&stp->st_perstateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (flags & OPEN_STATE) {
1188 release_stateid_lockowners(stp);
1189 stp->st_vfs_file = NULL;
1190 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 } else if (flags & LOCK_STATE)
1192 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
NeilBrown13cd2182005-06-23 22:03:10 -07001193 put_nfs4_file(stp->st_file);
NeilBrown5ac049a2005-06-23 22:03:03 -07001194 kmem_cache_free(stateid_slab, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
NeilBrownfd39ca92005-06-23 22:04:03 -07001197static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198move_to_close_lru(struct nfs4_stateowner *sop)
1199{
1200 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1201
NeilBrown358dd552006-04-10 22:55:42 -07001202 list_move_tail(&sop->so_close_lru, &close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 sop->so_time = get_seconds();
1204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206static int
1207cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1208 return ((sop->so_owner.len == owner->len) &&
1209 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1210 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1211}
1212
1213static struct nfs4_stateowner *
1214find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1215{
1216 struct nfs4_stateowner *so = NULL;
1217
1218 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1219 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1220 return so;
1221 }
1222 return NULL;
1223}
1224
1225/* search file_hashtbl[] for file */
1226static struct nfs4_file *
1227find_file(struct inode *ino)
1228{
1229 unsigned int hashval = file_hashval(ino);
1230 struct nfs4_file *fp;
1231
1232 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07001233 if (fp->fi_inode == ino) {
1234 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07001236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238 return NULL;
1239}
1240
1241#define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1242#define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1243
NeilBrownfd39ca92005-06-23 22:04:03 -07001244static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245set_access(unsigned int *access, unsigned long bmap) {
1246 int i;
1247
1248 *access = 0;
1249 for (i = 1; i < 4; i++) {
1250 if (test_bit(i, &bmap))
1251 *access |= i;
1252 }
1253}
1254
NeilBrownfd39ca92005-06-23 22:04:03 -07001255static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256set_deny(unsigned int *deny, unsigned long bmap) {
1257 int i;
1258
1259 *deny = 0;
1260 for (i = 0; i < 4; i++) {
1261 if (test_bit(i, &bmap))
1262 *deny |= i ;
1263 }
1264}
1265
1266static int
1267test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1268 unsigned int access, deny;
1269
1270 set_access(&access, stp->st_access_bmap);
1271 set_deny(&deny, stp->st_deny_bmap);
1272 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1273 return 0;
1274 return 1;
1275}
1276
1277/*
1278 * Called to check deny when READ with all zero stateid or
1279 * WRITE with all zero or all one stateid
1280 */
NeilBrownfd39ca92005-06-23 22:04:03 -07001281static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1283{
1284 struct inode *ino = current_fh->fh_dentry->d_inode;
1285 struct nfs4_file *fp;
1286 struct nfs4_stateid *stp;
NeilBrown13cd2182005-06-23 22:03:10 -07001287 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 dprintk("NFSD: nfs4_share_conflict\n");
1290
1291 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07001292 if (!fp)
1293 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07001294 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07001296 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1297 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1298 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
NeilBrown13cd2182005-06-23 22:03:10 -07001301 ret = nfs_ok;
1302out:
1303 put_nfs4_file(fp);
1304 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307static inline void
1308nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1309{
1310 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1311 put_write_access(filp->f_dentry->d_inode);
1312 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1313 }
1314}
1315
1316/*
1317 * Recall a delegation
1318 */
1319static int
1320do_recall(void *__dp)
1321{
1322 struct nfs4_delegation *dp = __dp;
1323
1324 daemonize("nfsv4-recall");
1325
1326 nfsd4_cb_recall(dp);
1327 return 0;
1328}
1329
1330/*
1331 * Spawn a thread to perform a recall on the delegation represented
1332 * by the lease (file_lock)
1333 *
1334 * Called from break_lease() with lock_kernel() held.
1335 * Note: we assume break_lease will only call this *once* for any given
1336 * lease.
1337 */
1338static
1339void nfsd_break_deleg_cb(struct file_lock *fl)
1340{
1341 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1342 struct task_struct *t;
1343
1344 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1345 if (!dp)
1346 return;
1347
1348 /* We're assuming the state code never drops its reference
1349 * without first removing the lease. Since we're in this lease
1350 * callback (and since the lease code is serialized by the kernel
1351 * lock) we know the server hasn't removed the lease yet, we know
1352 * it's safe to take a reference: */
1353 atomic_inc(&dp->dl_count);
1354
1355 spin_lock(&recall_lock);
1356 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1357 spin_unlock(&recall_lock);
1358
1359 /* only place dl_time is set. protected by lock_kernel*/
1360 dp->dl_time = get_seconds();
1361
1362 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1363 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1364
1365 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1366 if (IS_ERR(t)) {
1367 struct nfs4_client *clp = dp->dl_client;
1368
1369 printk(KERN_INFO "NFSD: Callback thread failed for "
1370 "for client (clientid %08x/%08x)\n",
1371 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1372 nfs4_put_delegation(dp);
1373 }
1374}
1375
1376/*
1377 * The file_lock is being reapd.
1378 *
1379 * Called by locks_free_lock() with lock_kernel() held.
1380 */
1381static
1382void nfsd_release_deleg_cb(struct file_lock *fl)
1383{
1384 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1385
1386 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1387
1388 if (!(fl->fl_flags & FL_LEASE) || !dp)
1389 return;
1390 dp->dl_flock = NULL;
1391}
1392
1393/*
1394 * Set the delegation file_lock back pointer.
1395 *
1396 * Called from __setlease() with lock_kernel() held.
1397 */
1398static
1399void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1400{
1401 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1402
1403 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1404 if (!dp)
1405 return;
1406 dp->dl_flock = new;
1407}
1408
1409/*
1410 * Called from __setlease() with lock_kernel() held
1411 */
1412static
1413int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1414{
1415 struct nfs4_delegation *onlistd =
1416 (struct nfs4_delegation *)onlist->fl_owner;
1417 struct nfs4_delegation *tryd =
1418 (struct nfs4_delegation *)try->fl_owner;
1419
1420 if (onlist->fl_lmops != try->fl_lmops)
1421 return 0;
1422
1423 return onlistd->dl_client == tryd->dl_client;
1424}
1425
1426
1427static
1428int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1429{
1430 if (arg & F_UNLCK)
1431 return lease_modify(onlist, arg);
1432 else
1433 return -EAGAIN;
1434}
1435
NeilBrownfd39ca92005-06-23 22:04:03 -07001436static struct lock_manager_operations nfsd_lease_mng_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 .fl_break = nfsd_break_deleg_cb,
1438 .fl_release_private = nfsd_release_deleg_cb,
1439 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1440 .fl_mylease = nfsd_same_client_deleg_cb,
1441 .fl_change = nfsd_change_deleg_cb,
1442};
1443
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445int
1446nfsd4_process_open1(struct nfsd4_open *open)
1447{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 clientid_t *clientid = &open->op_clientid;
1449 struct nfs4_client *clp = NULL;
1450 unsigned int strhashval;
1451 struct nfs4_stateowner *sop = NULL;
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001454 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 if (STALE_CLIENTID(&open->op_clientid))
1457 return nfserr_stale_clientid;
1458
1459 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1460 sop = find_openstateowner_str(strhashval, open);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001461 open->op_stateowner = sop;
1462 if (!sop) {
1463 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 clp = find_confirmed_client(clientid);
1465 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001466 return nfserr_expired;
1467 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001469 if (!sop->so_confirmed) {
1470 /* Replace unconfirmed owners without checking for replay. */
1471 clp = sop->so_client;
1472 release_stateowner(sop);
1473 open->op_stateowner = NULL;
1474 goto renew;
1475 }
1476 if (open->op_seqid == sop->so_seqid - 1) {
1477 if (sop->so_replay.rp_buflen)
1478 return NFSERR_REPLAY_ME;
1479 /* The original OPEN failed so spectacularly
1480 * that we don't even have replay data saved!
1481 * Therefore, we have no choice but to continue
1482 * processing this OPEN; presumably, we'll
1483 * fail again for the same reason.
1484 */
1485 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1486 goto renew;
1487 }
1488 if (open->op_seqid != sop->so_seqid)
1489 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490renew:
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001491 if (open->op_stateowner == NULL) {
1492 sop = alloc_init_open_stateowner(strhashval, clp, open);
1493 if (sop == NULL)
1494 return nfserr_resource;
1495 open->op_stateowner = sop;
1496 }
1497 list_del_init(&sop->so_close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 renew_client(sop->so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001499 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
NeilBrown4a6e43e2005-06-23 22:02:50 -07001502static inline int
1503nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1504{
1505 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1506 return nfserr_openmode;
1507 else
1508 return nfs_ok;
1509}
1510
NeilBrown52f4fb42005-06-23 22:02:49 -07001511static struct nfs4_delegation *
1512find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1513{
1514 struct nfs4_delegation *dp;
1515
NeilBrownea1da632005-06-23 22:04:17 -07001516 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
NeilBrown52f4fb42005-06-23 22:02:49 -07001517 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1518 return dp;
1519 }
1520 return NULL;
1521}
1522
NeilBrownc44c5ee2005-06-23 22:02:54 -07001523static int
NeilBrown567d9822005-06-23 22:02:53 -07001524nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1525 struct nfs4_delegation **dp)
1526{
1527 int flags;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001528 int status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07001529
1530 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1531 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07001532 goto out;
NeilBrown567d9822005-06-23 22:02:53 -07001533 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1534 RD_STATE : WR_STATE;
1535 status = nfs4_check_delegmode(*dp, flags);
1536 if (status)
1537 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001538out:
1539 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1540 return nfs_ok;
1541 if (status)
1542 return status;
1543 open->op_stateowner->so_confirmed = 1;
1544 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07001545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547static int
1548nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1549{
1550 struct nfs4_stateid *local;
1551 int status = nfserr_share_denied;
1552 struct nfs4_stateowner *sop = open->op_stateowner;
1553
NeilBrown8beefa22005-06-23 22:03:08 -07001554 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /* ignore lock owners */
1556 if (local->st_stateowner->so_is_open_owner == 0)
1557 continue;
1558 /* remember if we have seen this open owner */
1559 if (local->st_stateowner == sop)
1560 *stpp = local;
1561 /* check for conflicting share reservations */
1562 if (!test_share(local, open))
1563 goto out;
1564 }
1565 status = 0;
1566out:
1567 return status;
1568}
1569
NeilBrown5ac049a2005-06-23 22:03:03 -07001570static inline struct nfs4_stateid *
1571nfs4_alloc_stateid(void)
1572{
1573 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576static int
1577nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
NeilBrown567d9822005-06-23 22:02:53 -07001578 struct nfs4_delegation *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct svc_fh *cur_fh, int flags)
1580{
1581 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
NeilBrown5ac049a2005-06-23 22:03:03 -07001583 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (stp == NULL)
1585 return nfserr_resource;
1586
NeilBrown567d9822005-06-23 22:02:53 -07001587 if (dp) {
1588 get_file(dp->dl_vfs_file);
1589 stp->st_vfs_file = dp->dl_vfs_file;
1590 } else {
1591 int status;
1592 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1593 &stp->st_vfs_file);
1594 if (status) {
1595 if (status == nfserr_dropit)
1596 status = nfserr_jukebox;
NeilBrown5ac049a2005-06-23 22:03:03 -07001597 kmem_cache_free(stateid_slab, stp);
NeilBrown567d9822005-06-23 22:02:53 -07001598 return status;
1599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 *stpp = stp;
1602 return 0;
1603}
1604
1605static inline int
1606nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1607 struct nfsd4_open *open)
1608{
1609 struct iattr iattr = {
1610 .ia_valid = ATTR_SIZE,
1611 .ia_size = 0,
1612 };
1613 if (!open->op_truncate)
1614 return 0;
1615 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08001616 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1618}
1619
1620static int
1621nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1622{
1623 struct file *filp = stp->st_vfs_file;
1624 struct inode *inode = filp->f_dentry->d_inode;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001625 unsigned int share_access, new_writer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int status;
1627
1628 set_access(&share_access, stp->st_access_bmap);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001629 new_writer = (~share_access) & open->op_share_access
1630 & NFS4_SHARE_ACCESS_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001632 if (new_writer) {
1633 status = get_write_access(inode);
1634 if (status)
1635 return nfserrno(status);
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 status = nfsd4_truncate(rqstp, cur_fh, open);
1638 if (status) {
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001639 if (new_writer)
1640 put_write_access(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return status;
1642 }
1643 /* remember the open */
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001644 filp->f_mode |= open->op_share_access;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 set_bit(open->op_share_access, &stp->st_access_bmap);
1646 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1647
1648 return nfs_ok;
1649}
1650
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652static void
NeilBrown37515172005-07-07 17:59:16 -07001653nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
NeilBrown37515172005-07-07 17:59:16 -07001655 open->op_stateowner->so_confirmed = 1;
1656 open->op_stateowner->so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659/*
1660 * Attempt to hand out a delegation.
1661 */
1662static void
1663nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1664{
1665 struct nfs4_delegation *dp;
1666 struct nfs4_stateowner *sop = stp->st_stateowner;
1667 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1668 struct file_lock fl, *flp = &fl;
1669 int status, flag = 0;
1670
1671 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07001672 open->op_recall = 0;
1673 switch (open->op_claim_type) {
1674 case NFS4_OPEN_CLAIM_PREVIOUS:
1675 if (!atomic_read(&cb->cb_set))
1676 open->op_recall = 1;
1677 flag = open->op_delegate_type;
1678 if (flag == NFS4_OPEN_DELEGATE_NONE)
1679 goto out;
1680 break;
1681 case NFS4_OPEN_CLAIM_NULL:
1682 /* Let's not give out any delegations till everyone's
1683 * had the chance to reclaim theirs.... */
1684 if (nfs4_in_grace())
1685 goto out;
1686 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1687 goto out;
1688 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1689 flag = NFS4_OPEN_DELEGATE_WRITE;
1690 else
1691 flag = NFS4_OPEN_DELEGATE_READ;
1692 break;
1693 default:
1694 goto out;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1698 if (dp == NULL) {
1699 flag = NFS4_OPEN_DELEGATE_NONE;
1700 goto out;
1701 }
1702 locks_init_lock(&fl);
1703 fl.fl_lmops = &nfsd_lease_mng_ops;
1704 fl.fl_flags = FL_LEASE;
1705 fl.fl_end = OFFSET_MAX;
1706 fl.fl_owner = (fl_owner_t)dp;
1707 fl.fl_file = stp->st_vfs_file;
1708 fl.fl_pid = current->tgid;
1709
1710 /* setlease checks to see if delegation should be handed out.
1711 * the lock_manager callbacks fl_mylease and fl_change are used
1712 */
1713 if ((status = setlease(stp->st_vfs_file,
1714 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1715 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
NeilBrownc9071322005-04-16 15:26:38 -07001716 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 flag = NFS4_OPEN_DELEGATE_NONE;
1718 goto out;
1719 }
1720
1721 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1722
1723 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1724 dp->dl_stateid.si_boot,
1725 dp->dl_stateid.si_stateownerid,
1726 dp->dl_stateid.si_fileid,
1727 dp->dl_stateid.si_generation);
1728out:
NeilBrown7b190fe2005-06-23 22:03:23 -07001729 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1730 && flag == NFS4_OPEN_DELEGATE_NONE
1731 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1732 printk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 open->op_delegate_type = flag;
1734}
1735
1736/*
1737 * called with nfs4_lock_state() held.
1738 */
1739int
1740nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1741{
1742 struct nfs4_file *fp = NULL;
1743 struct inode *ino = current_fh->fh_dentry->d_inode;
1744 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07001745 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int status;
1747
1748 status = nfserr_inval;
1749 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1750 goto out;
1751 /*
1752 * Lookup file; if found, lookup stateid and check open request,
1753 * and check for delegations in the process of being recalled.
1754 * If not found, create the nfs4_file struct
1755 */
1756 fp = find_file(ino);
1757 if (fp) {
1758 if ((status = nfs4_check_open(fp, open, &stp)))
1759 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001760 status = nfs4_check_deleg(fp, open, &dp);
1761 if (status)
1762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07001764 status = nfserr_bad_stateid;
1765 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 status = nfserr_resource;
1768 fp = alloc_init_file(ino);
1769 if (fp == NULL)
1770 goto out;
1771 }
1772
1773 /*
1774 * OPEN the file, or upgrade an existing OPEN.
1775 * If truncate fails, the OPEN fails.
1776 */
1777 if (stp) {
1778 /* Stateid was found, this is an OPEN upgrade */
1779 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1780 if (status)
1781 goto out;
NeilBrown444c2c02005-07-07 17:59:22 -07001782 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 } else {
1784 /* Stateid was not found, this is a new OPEN */
1785 int flags = 0;
1786 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1787 flags = MAY_WRITE;
1788 else
1789 flags = MAY_READ;
NeilBrown567d9822005-06-23 22:02:53 -07001790 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1791 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto out;
1793 init_stateid(stp, fp, open);
1794 status = nfsd4_truncate(rqstp, current_fh, open);
1795 if (status) {
1796 release_stateid(stp, OPEN_STATE);
1797 goto out;
1798 }
1799 }
1800 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1801
1802 /*
1803 * Attempt to hand out a delegation. No error return, because the
1804 * OPEN succeeds even if we fail.
1805 */
1806 nfs4_open_delegation(current_fh, open, stp);
1807
1808 status = nfs_ok;
1809
1810 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1811 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1812 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1813out:
NeilBrown13cd2182005-06-23 22:03:10 -07001814 if (fp)
1815 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07001816 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1817 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /*
1819 * To finish the open response, we just need to set the rflags.
1820 */
1821 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1822 if (!open->op_stateowner->so_confirmed)
1823 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1824
1825 return status;
1826}
1827
NeilBrown58da2822005-06-23 22:03:19 -07001828static struct workqueue_struct *laundry_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829static struct work_struct laundromat_work;
1830static void laundromat_main(void *);
1831static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1832
1833int
1834nfsd4_renew(clientid_t *clid)
1835{
1836 struct nfs4_client *clp;
1837 int status;
1838
1839 nfs4_lock_state();
1840 dprintk("process_renew(%08x/%08x): starting\n",
1841 clid->cl_boot, clid->cl_id);
1842 status = nfserr_stale_clientid;
1843 if (STALE_CLIENTID(clid))
1844 goto out;
1845 clp = find_confirmed_client(clid);
1846 status = nfserr_expired;
1847 if (clp == NULL) {
1848 /* We assume the client took too long to RENEW. */
1849 dprintk("nfsd4_renew: clientid not found!\n");
1850 goto out;
1851 }
1852 renew_client(clp);
1853 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07001854 if (!list_empty(&clp->cl_delegations)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 && !atomic_read(&clp->cl_callback.cb_set))
1856 goto out;
1857 status = nfs_ok;
1858out:
1859 nfs4_unlock_state();
1860 return status;
1861}
1862
NeilBrowna76b4312005-06-23 22:04:01 -07001863static void
1864end_grace(void)
1865{
1866 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07001867 nfsd4_recdir_purge_old();
NeilBrowna76b4312005-06-23 22:04:01 -07001868 in_grace = 0;
1869}
1870
NeilBrownfd39ca92005-06-23 22:04:03 -07001871static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872nfs4_laundromat(void)
1873{
1874 struct nfs4_client *clp;
1875 struct nfs4_stateowner *sop;
1876 struct nfs4_delegation *dp;
1877 struct list_head *pos, *next, reaplist;
1878 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1879 time_t t, clientid_val = NFSD_LEASE_TIME;
1880 time_t u, test_val = NFSD_LEASE_TIME;
1881
1882 nfs4_lock_state();
1883
1884 dprintk("NFSD: laundromat service - starting\n");
NeilBrowna76b4312005-06-23 22:04:01 -07001885 if (in_grace)
1886 end_grace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 list_for_each_safe(pos, next, &client_lru) {
1888 clp = list_entry(pos, struct nfs4_client, cl_lru);
1889 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1890 t = clp->cl_time - cutoff;
1891 if (clientid_val > t)
1892 clientid_val = t;
1893 break;
1894 }
1895 dprintk("NFSD: purging unused client (clientid %08x)\n",
1896 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07001897 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 expire_client(clp);
1899 }
1900 INIT_LIST_HEAD(&reaplist);
1901 spin_lock(&recall_lock);
1902 list_for_each_safe(pos, next, &del_recall_lru) {
1903 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1904 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1905 u = dp->dl_time - cutoff;
1906 if (test_val > u)
1907 test_val = u;
1908 break;
1909 }
1910 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1911 dp, dp->dl_flock);
1912 list_move(&dp->dl_recall_lru, &reaplist);
1913 }
1914 spin_unlock(&recall_lock);
1915 list_for_each_safe(pos, next, &reaplist) {
1916 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1917 list_del_init(&dp->dl_recall_lru);
1918 unhash_delegation(dp);
1919 }
1920 test_val = NFSD_LEASE_TIME;
1921 list_for_each_safe(pos, next, &close_lru) {
1922 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1923 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1924 u = sop->so_time - cutoff;
1925 if (test_val > u)
1926 test_val = u;
1927 break;
1928 }
1929 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1930 sop->so_id);
NeilBrown358dd552006-04-10 22:55:42 -07001931 release_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1934 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1935 nfs4_unlock_state();
1936 return clientid_val;
1937}
1938
1939void
1940laundromat_main(void *not_used)
1941{
1942 time_t t;
1943
1944 t = nfs4_laundromat();
1945 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07001946 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
NeilBrownfd39ca92005-06-23 22:04:03 -07001949static struct nfs4_stateowner *
NeilBrownf8816512005-07-07 17:59:25 -07001950search_close_lru(u32 st_id, int flags)
1951{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct nfs4_stateowner *local = NULL;
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (flags & CLOSE_STATE) {
1955 list_for_each_entry(local, &close_lru, so_close_lru) {
1956 if (local->so_id == st_id)
1957 return local;
1958 }
1959 }
1960 return NULL;
1961}
1962
1963static inline int
1964nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1965{
1966 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1967}
1968
1969static int
1970STALE_STATEID(stateid_t *stateid)
1971{
1972 if (stateid->si_boot == boot_time)
1973 return 0;
Neil Brown849823c2005-09-13 01:25:36 -07001974 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1976 stateid->si_generation);
1977 return 1;
1978}
1979
1980static inline int
1981access_permit_read(unsigned long access_bmap)
1982{
1983 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1984 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1985 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1986}
1987
1988static inline int
1989access_permit_write(unsigned long access_bmap)
1990{
1991 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1992 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
1993}
1994
1995static
1996int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
1997{
1998 int status = nfserr_openmode;
1999
2000 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2001 goto out;
2002 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2003 goto out;
2004 status = nfs_ok;
2005out:
2006 return status;
2007}
2008
2009static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2011{
2012 /* Trying to call delegreturn with a special stateid? Yuch: */
2013 if (!(flags & (RD_STATE | WR_STATE)))
2014 return nfserr_bad_stateid;
2015 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2016 return nfs_ok;
2017 else if (nfs4_in_grace()) {
2018 /* Answer in remaining cases depends on existance of
2019 * conflicting state; so we must wait out the grace period. */
2020 return nfserr_grace;
2021 } else if (flags & WR_STATE)
2022 return nfs4_share_conflict(current_fh,
2023 NFS4_SHARE_DENY_WRITE);
2024 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2025 return nfs4_share_conflict(current_fh,
2026 NFS4_SHARE_DENY_READ);
2027}
2028
2029/*
2030 * Allow READ/WRITE during grace period on recovered state only for files
2031 * that are not able to provide mandatory locking.
2032 */
2033static inline int
2034io_during_grace_disallowed(struct inode *inode, int flags)
2035{
2036 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2037 && MANDATORY_LOCK(inode);
2038}
2039
2040/*
2041* Checks for stateid operations
2042*/
2043int
2044nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2045{
2046 struct nfs4_stateid *stp = NULL;
2047 struct nfs4_delegation *dp = NULL;
2048 stateid_t *stidp;
2049 struct inode *ino = current_fh->fh_dentry->d_inode;
2050 int status;
2051
2052 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2053 stateid->si_boot, stateid->si_stateownerid,
2054 stateid->si_fileid, stateid->si_generation);
2055 if (filpp)
2056 *filpp = NULL;
2057
2058 if (io_during_grace_disallowed(ino, flags))
2059 return nfserr_grace;
2060
2061 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2062 return check_special_stateids(current_fh, stateid, flags);
2063
2064 /* STALE STATEID */
2065 status = nfserr_stale_stateid;
2066 if (STALE_STATEID(stateid))
2067 goto out;
2068
2069 /* BAD STATEID */
2070 status = nfserr_bad_stateid;
2071 if (!stateid->si_fileid) { /* delegation stateid */
2072 if(!(dp = find_delegation_stateid(ino, stateid))) {
2073 dprintk("NFSD: delegation stateid not found\n");
2074 if (nfs4_in_grace())
2075 status = nfserr_grace;
2076 goto out;
2077 }
2078 stidp = &dp->dl_stateid;
2079 } else { /* open or lock stateid */
2080 if (!(stp = find_stateid(stateid, flags))) {
2081 dprintk("NFSD: open or lock stateid not found\n");
2082 if (nfs4_in_grace())
2083 status = nfserr_grace;
2084 goto out;
2085 }
2086 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2087 goto out;
2088 if (!stp->st_stateowner->so_confirmed)
2089 goto out;
2090 stidp = &stp->st_stateid;
2091 }
2092 if (stateid->si_generation > stidp->si_generation)
2093 goto out;
2094
2095 /* OLD STATEID */
2096 status = nfserr_old_stateid;
2097 if (stateid->si_generation < stidp->si_generation)
2098 goto out;
2099 if (stp) {
2100 if ((status = nfs4_check_openmode(stp,flags)))
2101 goto out;
2102 renew_client(stp->st_stateowner->so_client);
2103 if (filpp)
2104 *filpp = stp->st_vfs_file;
2105 } else if (dp) {
2106 if ((status = nfs4_check_delegmode(dp, flags)))
2107 goto out;
2108 renew_client(dp->dl_client);
2109 if (flags & DELEG_RET)
2110 unhash_delegation(dp);
2111 if (filpp)
2112 *filpp = dp->dl_vfs_file;
2113 }
2114 status = nfs_ok;
2115out:
2116 return status;
2117}
2118
NeilBrown4c4cd222005-07-07 17:59:27 -07002119static inline int
2120setlkflg (int type)
2121{
2122 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2123 RD_STATE : WR_STATE;
2124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126/*
2127 * Checks for sequence id mutating operations.
2128 */
NeilBrownfd39ca92005-06-23 22:04:03 -07002129static int
NeilBrown4c4cd222005-07-07 17:59:27 -07002130nfs4_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 -07002131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 struct nfs4_stateid *stp;
2133 struct nfs4_stateowner *sop;
2134
2135 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2136 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2137 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2138 stateid->si_generation);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 *stpp = NULL;
2141 *sopp = NULL;
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2144 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002145 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (STALE_STATEID(stateid))
NeilBrown3a4f98b2005-07-07 17:59:26 -07002149 return nfserr_stale_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /*
2151 * We return BAD_STATEID if filehandle doesn't match stateid,
2152 * the confirmed flag is incorrecly set, or the generation
2153 * number is incorrect.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 */
NeilBrownf8816512005-07-07 17:59:25 -07002155 stp = find_stateid(stateid, flags);
2156 if (stp == NULL) {
2157 /*
2158 * Also, we should make sure this isn't just the result of
2159 * a replayed close:
2160 */
2161 sop = search_close_lru(stateid->si_stateownerid, flags);
2162 if (sop == NULL)
2163 return nfserr_bad_stateid;
2164 *sopp = sop;
2165 goto check_replay;
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
NeilBrown4c4cd222005-07-07 17:59:27 -07002168 if (lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 struct nfs4_stateowner *sop = stp->st_stateowner;
NeilBrown4c4cd222005-07-07 17:59:27 -07002170 clientid_t *lockclid = &lock->v.new.clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 struct nfs4_client *clp = sop->so_client;
NeilBrown4c4cd222005-07-07 17:59:27 -07002172 int lkflg = 0;
2173 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
NeilBrown4c4cd222005-07-07 17:59:27 -07002175 lkflg = setlkflg(lock->lk_type);
2176
2177 if (lock->lk_is_new) {
2178 if (!sop->so_is_open_owner)
2179 return nfserr_bad_stateid;
2180 if (!cmp_clid(&clp->cl_clientid, lockclid))
2181 return nfserr_bad_stateid;
2182 /* stp is the open stateid */
2183 status = nfs4_check_openmode(stp, lkflg);
2184 if (status)
2185 return status;
2186 } else {
2187 /* stp is the lock stateid */
2188 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2189 if (status)
2190 return status;
2191 }
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194
2195 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2196 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002197 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199
2200 *stpp = stp;
2201 *sopp = sop = stp->st_stateowner;
2202
2203 /*
2204 * We now validate the seqid and stateid generation numbers.
2205 * For the moment, we ignore the possibility of
2206 * generation number wraparound.
2207 */
NeilBrownbd9aac52005-07-07 17:59:19 -07002208 if (seqid != sop->so_seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 goto check_replay;
2210
NeilBrown3a4f98b2005-07-07 17:59:26 -07002211 if (sop->so_confirmed && flags & CONFIRM) {
2212 printk("NFSD: preprocess_seqid_op: expected"
2213 " unconfirmed stateowner!\n");
2214 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07002216 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2217 printk("NFSD: preprocess_seqid_op: stateowner not"
2218 " confirmed yet!\n");
2219 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221 if (stateid->si_generation > stp->st_stateid.si_generation) {
2222 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002223 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (stateid->si_generation < stp->st_stateid.si_generation) {
2227 printk("NFSD: preprocess_seqid_op: old stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002228 return nfserr_old_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
NeilBrown52fd0042005-07-07 17:59:24 -07002230 renew_client(sop->so_client);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002231 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233check_replay:
NeilBrownbd9aac52005-07-07 17:59:19 -07002234 if (seqid == sop->so_seqid - 1) {
Neil Brown849823c2005-09-13 01:25:36 -07002235 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 /* indicate replay to calling function */
NeilBrown3a4f98b2005-07-07 17:59:26 -07002237 return NFSERR_REPLAY_ME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07002239 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2240 sop->so_seqid, seqid);
2241 *sopp = NULL;
2242 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
2245int
Neil Brownf2327d92005-09-13 01:25:37 -07002246nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc, struct nfs4_stateowner **replay_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 int status;
2249 struct nfs4_stateowner *sop;
2250 struct nfs4_stateid *stp;
2251
2252 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2253 (int)current_fh->fh_dentry->d_name.len,
2254 current_fh->fh_dentry->d_name.name);
2255
2256 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2257 goto out;
2258
2259 nfs4_lock_state();
2260
2261 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2262 &oc->oc_req_stateid,
2263 CHECK_FH | CONFIRM | OPEN_STATE,
2264 &oc->oc_stateowner, &stp, NULL)))
2265 goto out;
2266
2267 sop = oc->oc_stateowner;
2268 sop->so_confirmed = 1;
2269 update_stateid(&stp->st_stateid);
2270 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2271 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2272 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2273 stp->st_stateid.si_boot,
2274 stp->st_stateid.si_stateownerid,
2275 stp->st_stateid.si_fileid,
2276 stp->st_stateid.si_generation);
NeilBrownc7b9a452005-06-23 22:04:30 -07002277
2278 nfsd4_create_clid_dir(sop->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279out:
Neil Brownf2327d92005-09-13 01:25:37 -07002280 if (oc->oc_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 nfs4_get_stateowner(oc->oc_stateowner);
Neil Brownf2327d92005-09-13 01:25:37 -07002282 *replay_owner = oc->oc_stateowner;
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 nfs4_unlock_state();
2285 return status;
2286}
2287
2288
2289/*
2290 * unset all bits in union bitmap (bmap) that
2291 * do not exist in share (from successful OPEN_DOWNGRADE)
2292 */
2293static void
2294reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2295{
2296 int i;
2297 for (i = 1; i < 4; i++) {
2298 if ((i & access) != i)
2299 __clear_bit(i, bmap);
2300 }
2301}
2302
2303static void
2304reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2305{
2306 int i;
2307 for (i = 0; i < 4; i++) {
2308 if ((i & deny) != i)
2309 __clear_bit(i, bmap);
2310 }
2311}
2312
2313int
Neil Brownf2327d92005-09-13 01:25:37 -07002314nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od, struct nfs4_stateowner **replay_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
2316 int status;
2317 struct nfs4_stateid *stp;
2318 unsigned int share_access;
2319
2320 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2321 (int)current_fh->fh_dentry->d_name.len,
2322 current_fh->fh_dentry->d_name.name);
2323
2324 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2325 return nfserr_inval;
2326
2327 nfs4_lock_state();
2328 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2329 &od->od_stateid,
2330 CHECK_FH | OPEN_STATE,
2331 &od->od_stateowner, &stp, NULL)))
2332 goto out;
2333
2334 status = nfserr_inval;
2335 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2336 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2337 stp->st_access_bmap, od->od_share_access);
2338 goto out;
2339 }
2340 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2341 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2342 stp->st_deny_bmap, od->od_share_deny);
2343 goto out;
2344 }
2345 set_access(&share_access, stp->st_access_bmap);
2346 nfs4_file_downgrade(stp->st_vfs_file,
2347 share_access & ~od->od_share_access);
2348
2349 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2350 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2351
2352 update_stateid(&stp->st_stateid);
2353 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2354 status = nfs_ok;
2355out:
Neil Brownf2327d92005-09-13 01:25:37 -07002356 if (od->od_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 nfs4_get_stateowner(od->od_stateowner);
Neil Brownf2327d92005-09-13 01:25:37 -07002358 *replay_owner = od->od_stateowner;
2359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 nfs4_unlock_state();
2361 return status;
2362}
2363
2364/*
2365 * nfs4_unlock_state() called after encode
2366 */
2367int
Neil Brownf2327d92005-09-13 01:25:37 -07002368nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close, struct nfs4_stateowner **replay_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
2370 int status;
2371 struct nfs4_stateid *stp;
2372
2373 dprintk("NFSD: nfsd4_close on file %.*s\n",
2374 (int)current_fh->fh_dentry->d_name.len,
2375 current_fh->fh_dentry->d_name.name);
2376
2377 nfs4_lock_state();
2378 /* check close_lru for replay */
2379 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2380 &close->cl_stateid,
2381 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2382 &close->cl_stateowner, &stp, NULL)))
2383 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 status = nfs_ok;
2385 update_stateid(&stp->st_stateid);
2386 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2387
J. Bruce Fields04ef5952006-01-18 17:43:21 -08002388 /* release_stateid() calls nfsd_close() if needed */
2389 release_stateid(stp, OPEN_STATE);
2390
2391 /* place unused nfs4_stateowners on so_close_lru list to be
2392 * released by the laundromat service after the lease period
2393 * to enable us to handle CLOSE replay
2394 */
2395 if (list_empty(&close->cl_stateowner->so_stateids))
2396 move_to_close_lru(close->cl_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397out:
Neil Brownf2327d92005-09-13 01:25:37 -07002398 if (close->cl_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 nfs4_get_stateowner(close->cl_stateowner);
Neil Brownf2327d92005-09-13 01:25:37 -07002400 *replay_owner = close->cl_stateowner;
2401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 nfs4_unlock_state();
2403 return status;
2404}
2405
2406int
2407nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2408{
2409 int status;
2410
2411 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2412 goto out;
2413
2414 nfs4_lock_state();
2415 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2416 nfs4_unlock_state();
2417out:
2418 return status;
2419}
2420
2421
2422/*
2423 * Lock owner state (byte-range locks)
2424 */
2425#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2426#define LOCK_HASH_BITS 8
2427#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2428#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2429
2430#define lockownerid_hashval(id) \
2431 ((id) & LOCK_HASH_MASK)
2432
2433static inline unsigned int
2434lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2435 struct xdr_netobj *ownername)
2436{
2437 return (file_hashval(inode) + cl_id
2438 + opaque_hashval(ownername->data, ownername->len))
2439 & LOCK_HASH_MASK;
2440}
2441
2442static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2443static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2444static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2445
NeilBrownfd39ca92005-06-23 22:04:03 -07002446static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447find_stateid(stateid_t *stid, int flags)
2448{
2449 struct nfs4_stateid *local = NULL;
2450 u32 st_id = stid->si_stateownerid;
2451 u32 f_id = stid->si_fileid;
2452 unsigned int hashval;
2453
2454 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2455 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2456 hashval = stateid_hashval(st_id, f_id);
2457 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2458 if ((local->st_stateid.si_stateownerid == st_id) &&
2459 (local->st_stateid.si_fileid == f_id))
2460 return local;
2461 }
2462 }
2463 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2464 hashval = stateid_hashval(st_id, f_id);
2465 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2466 if ((local->st_stateid.si_stateownerid == st_id) &&
2467 (local->st_stateid.si_fileid == f_id))
2468 return local;
2469 }
Neil Brown849823c2005-09-13 01:25:36 -07002470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return NULL;
2472}
2473
2474static struct nfs4_delegation *
2475find_delegation_stateid(struct inode *ino, stateid_t *stid)
2476{
NeilBrown13cd2182005-06-23 22:03:10 -07002477 struct nfs4_file *fp;
2478 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2481 stid->si_boot, stid->si_stateownerid,
2482 stid->si_fileid, stid->si_generation);
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002485 if (!fp)
2486 return NULL;
2487 dl = find_delegation_file(fp, stid);
2488 put_nfs4_file(fp);
2489 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
2492/*
2493 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2494 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2495 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2496 * locking, this prevents us from being completely protocol-compliant. The
2497 * real solution to this problem is to start using unsigned file offsets in
2498 * the VFS, but this is a very deep change!
2499 */
2500static inline void
2501nfs4_transform_lock_offset(struct file_lock *lock)
2502{
2503 if (lock->fl_start < 0)
2504 lock->fl_start = OFFSET_MAX;
2505 if (lock->fl_end < 0)
2506 lock->fl_end = OFFSET_MAX;
2507}
2508
NeilBrownd5b90262006-04-10 22:55:22 -07002509/* Hack!: For now, we're defining this just so we can use a pointer to it
2510 * as a unique cookie to identify our (NFSv4's) posix locks. */
Adrian Bunke465a772006-04-10 22:55:23 -07002511static struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07002512};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514static inline void
2515nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2516{
NeilBrownd5b90262006-04-10 22:55:22 -07002517 struct nfs4_stateowner *sop;
2518 unsigned int hval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
NeilBrownd5b90262006-04-10 22:55:22 -07002520 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2521 sop = (struct nfs4_stateowner *) fl->fl_owner;
2522 hval = lockownerid_hashval(sop->so_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 kref_get(&sop->so_ref);
2524 deny->ld_sop = sop;
2525 deny->ld_clientid = sop->so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07002526 } else {
2527 deny->ld_sop = NULL;
2528 deny->ld_clientid.cl_boot = 0;
2529 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531 deny->ld_start = fl->fl_start;
2532 deny->ld_length = ~(u64)0;
2533 if (fl->fl_end != ~(u64)0)
2534 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2535 deny->ld_type = NFS4_READ_LT;
2536 if (fl->fl_type != F_RDLCK)
2537 deny->ld_type = NFS4_WRITE_LT;
2538}
2539
2540static struct nfs4_stateowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2542 struct xdr_netobj *owner)
2543{
2544 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2545 struct nfs4_stateowner *op;
2546
2547 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2548 if (cmp_owner_str(op, owner, clid))
2549 return op;
2550 }
2551 return NULL;
2552}
2553
2554/*
2555 * Alloc a lock owner structure.
2556 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2557 * occured.
2558 *
2559 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 */
2561
2562static struct nfs4_stateowner *
2563alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2564 struct nfs4_stateowner *sop;
2565 struct nfs4_replay *rp;
2566 unsigned int idhashval;
2567
2568 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2569 return NULL;
2570 idhashval = lockownerid_hashval(current_ownerid);
2571 INIT_LIST_HEAD(&sop->so_idhash);
2572 INIT_LIST_HEAD(&sop->so_strhash);
2573 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07002574 INIT_LIST_HEAD(&sop->so_stateids);
2575 INIT_LIST_HEAD(&sop->so_perstateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2577 sop->so_time = 0;
2578 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2579 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07002580 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 sop->so_is_open_owner = 0;
2582 sop->so_id = current_ownerid++;
2583 sop->so_client = clp;
Neil Brownb59e3c02005-09-13 01:25:38 -07002584 /* It is the openowner seqid that will be incremented in encode in the
2585 * case of new lockowners; so increment the lock seqid manually: */
2586 sop->so_seqid = lock->lk_new_lock_seqid + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 sop->so_confirmed = 1;
2588 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08002589 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 rp->rp_buflen = 0;
2591 rp->rp_buf = rp->rp_ibuf;
2592 return sop;
2593}
2594
NeilBrownfd39ca92005-06-23 22:04:03 -07002595static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2597{
2598 struct nfs4_stateid *stp;
2599 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2600
NeilBrown5ac049a2005-06-23 22:03:03 -07002601 stp = nfs4_alloc_stateid();
2602 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 goto out;
2604 INIT_LIST_HEAD(&stp->st_hash);
2605 INIT_LIST_HEAD(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07002606 INIT_LIST_HEAD(&stp->st_perstateowner);
2607 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07002609 list_add(&stp->st_perfile, &fp->fi_stateids);
NeilBrownea1da632005-06-23 22:04:17 -07002610 list_add(&stp->st_perstateowner, &sop->so_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07002612 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 stp->st_file = fp;
2614 stp->st_stateid.si_boot = boot_time;
2615 stp->st_stateid.si_stateownerid = sop->so_id;
2616 stp->st_stateid.si_fileid = fp->fi_id;
2617 stp->st_stateid.si_generation = 0;
2618 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2619 stp->st_access_bmap = open_stp->st_access_bmap;
2620 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07002621 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623out:
2624 return stp;
2625}
2626
NeilBrownfd39ca92005-06-23 22:04:03 -07002627static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628check_lock_length(u64 offset, u64 length)
2629{
2630 return ((length == 0) || ((length != ~(u64)0) &&
2631 LOFF_OVERFLOW(offset, length)));
2632}
2633
2634/*
2635 * LOCK operation
2636 */
2637int
Neil Brownf2327d92005-09-13 01:25:37 -07002638nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock, struct nfs4_stateowner **replay_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
NeilBrown3e9e3db2005-06-23 22:04:20 -07002640 struct nfs4_stateowner *open_sop = NULL;
Neil Brownb59e3c02005-09-13 01:25:38 -07002641 struct nfs4_stateowner *lock_sop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 struct nfs4_stateid *lock_stp;
2643 struct file *filp;
2644 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05002645 struct file_lock conflock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 int status = 0;
2647 unsigned int strhashval;
2648
2649 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2650 (long long) lock->lk_offset,
2651 (long long) lock->lk_length);
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (check_lock_length(lock->lk_offset, lock->lk_length))
2654 return nfserr_inval;
2655
Andy Adamsona6f6ef22006-01-18 17:43:17 -08002656 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2657 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2658 return status;
2659 }
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 nfs4_lock_state();
2662
2663 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07002664 /*
2665 * Client indicates that this is a new lockowner.
2666 * Use open owner and open stateid to create lock owner and
2667 * lock stateid.
2668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 struct nfs4_stateid *open_stp = NULL;
2670 struct nfs4_file *fp;
2671
2672 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002673 if (STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 /* validate and update open stateid and open seqid */
2677 status = nfs4_preprocess_seqid_op(current_fh,
2678 lock->lk_new_open_seqid,
2679 &lock->lk_new_open_stateid,
2680 CHECK_FH | OPEN_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002681 &lock->lk_replay_owner, &open_stp,
Neil Brownb59e3c02005-09-13 01:25:38 -07002682 lock);
NeilBrown37515172005-07-07 17:59:16 -07002683 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002685 open_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 /* create lockowner and lock stateid */
2687 fp = open_stp->st_file;
2688 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2689 open_sop->so_client->cl_clientid.cl_id,
2690 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07002691 /* XXX: Do we need to check for duplicate stateowners on
2692 * the same file, or should they just be allowed (and
2693 * create new stateids)? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 status = nfserr_resource;
Neil Brownb59e3c02005-09-13 01:25:38 -07002695 lock_sop = alloc_init_lock_stateowner(strhashval,
2696 open_sop->so_client, open_stp, lock);
2697 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07002699 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08002700 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 } else {
2703 /* lock (lock owner + lock stateid) already exists */
2704 status = nfs4_preprocess_seqid_op(current_fh,
2705 lock->lk_old_lock_seqid,
2706 &lock->lk_old_lock_stateid,
2707 CHECK_FH | LOCK_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002708 &lock->lk_replay_owner, &lock_stp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (status)
2710 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002711 lock_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
J. Bruce Fields3a655882006-01-18 17:43:19 -08002713 /* lock->lk_replay_owner and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 filp = lock_stp->st_vfs_file;
2715
NeilBrown0dd395d2005-07-07 17:59:15 -07002716 status = nfserr_grace;
2717 if (nfs4_in_grace() && !lock->lk_reclaim)
2718 goto out;
2719 status = nfserr_no_grace;
2720 if (!nfs4_in_grace() && lock->lk_reclaim)
2721 goto out;
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 locks_init_lock(&file_lock);
2724 switch (lock->lk_type) {
2725 case NFS4_READ_LT:
2726 case NFS4_READW_LT:
2727 file_lock.fl_type = F_RDLCK;
2728 break;
2729 case NFS4_WRITE_LT:
2730 case NFS4_WRITEW_LT:
2731 file_lock.fl_type = F_WRLCK;
2732 break;
2733 default:
2734 status = nfserr_inval;
2735 goto out;
2736 }
Neil Brownb59e3c02005-09-13 01:25:38 -07002737 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 file_lock.fl_pid = current->tgid;
2739 file_lock.fl_file = filp;
2740 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002741 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743 file_lock.fl_start = lock->lk_offset;
2744 if ((lock->lk_length == ~(u64)0) ||
2745 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2746 file_lock.fl_end = ~(u64)0;
2747 else
2748 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2749 nfs4_transform_lock_offset(&file_lock);
2750
2751 /*
2752 * Try to lock the file in the VFS.
2753 * Note: locks.c uses the BKL to protect the inode's lock list.
2754 */
2755
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002756 /* XXX?: Just to divert the locks_release_private at the start of
2757 * locks_copy_lock: */
2758 conflock.fl_ops = NULL;
2759 conflock.fl_lmops = NULL;
2760 status = posix_lock_file_conf(filp, &file_lock, &conflock);
2761 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf status %d\n",status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 switch (-status) {
2763 case 0: /* success! */
2764 update_stateid(&lock_stp->st_stateid);
2765 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2766 sizeof(stateid_t));
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002767 break;
2768 case (EAGAIN): /* conflock holds conflicting lock */
2769 status = nfserr_denied;
2770 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2771 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 case (EDEADLK):
2774 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 default:
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002777 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf() failed! status %d\n",status);
2778 status = nfserr_resource;
2779 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08002782 if (status && lock->lk_is_new && lock_sop)
2783 release_stateowner(lock_sop);
J. Bruce Fields3a655882006-01-18 17:43:19 -08002784 if (lock->lk_replay_owner) {
2785 nfs4_get_stateowner(lock->lk_replay_owner);
2786 *replay_owner = lock->lk_replay_owner;
Neil Brownf2327d92005-09-13 01:25:37 -07002787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 nfs4_unlock_state();
2789 return status;
2790}
2791
2792/*
2793 * LOCKT operation
2794 */
2795int
2796nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2797{
2798 struct inode *inode;
2799 struct file file;
2800 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05002801 struct file_lock conflock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 int status;
2803
2804 if (nfs4_in_grace())
2805 return nfserr_grace;
2806
2807 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2808 return nfserr_inval;
2809
2810 lockt->lt_stateowner = NULL;
2811 nfs4_lock_state();
2812
2813 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002814 if (STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
Neil Brown849823c2005-09-13 01:25:36 -07002818 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 if (status == nfserr_symlink)
2820 status = nfserr_inval;
2821 goto out;
2822 }
2823
2824 inode = current_fh->fh_dentry->d_inode;
2825 locks_init_lock(&file_lock);
2826 switch (lockt->lt_type) {
2827 case NFS4_READ_LT:
2828 case NFS4_READW_LT:
2829 file_lock.fl_type = F_RDLCK;
2830 break;
2831 case NFS4_WRITE_LT:
2832 case NFS4_WRITEW_LT:
2833 file_lock.fl_type = F_WRLCK;
2834 break;
2835 default:
2836 printk("NFSD: nfs4_lockt: bad lock type!\n");
2837 status = nfserr_inval;
2838 goto out;
2839 }
2840
2841 lockt->lt_stateowner = find_lockstateowner_str(inode,
2842 &lockt->lt_clientid, &lockt->lt_owner);
2843 if (lockt->lt_stateowner)
2844 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2845 file_lock.fl_pid = current->tgid;
2846 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002847 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 file_lock.fl_start = lockt->lt_offset;
2850 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2851 file_lock.fl_end = ~(u64)0;
2852 else
2853 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2854
2855 nfs4_transform_lock_offset(&file_lock);
2856
2857 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2858 * since LOCKT doesn't require an OPEN, and therefore a struct
2859 * file may not exist, pass posix_test_lock a struct file with
2860 * only the dentry:inode set.
2861 */
2862 memset(&file, 0, sizeof (struct file));
2863 file.f_dentry = current_fh->fh_dentry;
2864
2865 status = nfs_ok;
Andy Adamson8dc7c312006-03-20 13:44:26 -05002866 if (posix_test_lock(&file, &file_lock, &conflock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 status = nfserr_denied;
Andy Adamson8dc7c312006-03-20 13:44:26 -05002868 nfs4_set_lock_denied(&conflock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
2870out:
2871 nfs4_unlock_state();
2872 return status;
2873}
2874
2875int
Neil Brownf2327d92005-09-13 01:25:37 -07002876nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku, struct nfs4_stateowner **replay_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
2878 struct nfs4_stateid *stp;
2879 struct file *filp = NULL;
2880 struct file_lock file_lock;
2881 int status;
2882
2883 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2884 (long long) locku->lu_offset,
2885 (long long) locku->lu_length);
2886
2887 if (check_lock_length(locku->lu_offset, locku->lu_length))
2888 return nfserr_inval;
2889
2890 nfs4_lock_state();
2891
2892 if ((status = nfs4_preprocess_seqid_op(current_fh,
2893 locku->lu_seqid,
2894 &locku->lu_stateid,
2895 CHECK_FH | LOCK_STATE,
2896 &locku->lu_stateowner, &stp, NULL)))
2897 goto out;
2898
2899 filp = stp->st_vfs_file;
2900 BUG_ON(!filp);
2901 locks_init_lock(&file_lock);
2902 file_lock.fl_type = F_UNLCK;
2903 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2904 file_lock.fl_pid = current->tgid;
2905 file_lock.fl_file = filp;
2906 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002907 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 file_lock.fl_start = locku->lu_offset;
2909
2910 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2911 file_lock.fl_end = ~(u64)0;
2912 else
2913 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2914 nfs4_transform_lock_offset(&file_lock);
2915
2916 /*
2917 * Try to unlock the file in the VFS.
2918 */
2919 status = posix_lock_file(filp, &file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 if (status) {
Neil Brown849823c2005-09-13 01:25:36 -07002921 dprintk("NFSD: nfs4_locku: posix_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 goto out_nfserr;
2923 }
2924 /*
2925 * OK, unlock succeeded; the only thing left to do is update the stateid.
2926 */
2927 update_stateid(&stp->st_stateid);
2928 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2929
2930out:
Neil Brownf2327d92005-09-13 01:25:37 -07002931 if (locku->lu_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 nfs4_get_stateowner(locku->lu_stateowner);
Neil Brownf2327d92005-09-13 01:25:37 -07002933 *replay_owner = locku->lu_stateowner;
2934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 nfs4_unlock_state();
2936 return status;
2937
2938out_nfserr:
2939 status = nfserrno(status);
2940 goto out;
2941}
2942
2943/*
2944 * returns
2945 * 1: locks held by lockowner
2946 * 0: no locks held by lockowner
2947 */
2948static int
2949check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2950{
2951 struct file_lock **flpp;
2952 struct inode *inode = filp->f_dentry->d_inode;
2953 int status = 0;
2954
2955 lock_kernel();
2956 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08002957 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 status = 1;
2959 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08002960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
2962out:
2963 unlock_kernel();
2964 return status;
2965}
2966
2967int
2968nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
2969{
2970 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07002971 struct nfs4_stateowner *sop;
2972 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07002974 struct list_head matches;
2975 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 int status;
2977
2978 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2979 clid->cl_boot, clid->cl_id);
2980
2981 /* XXX check for lease expiration */
2982
2983 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002984 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 nfs4_lock_state();
2988
NeilBrown3e9e3db2005-06-23 22:04:20 -07002989 status = nfserr_locks_held;
2990 /* XXX: we're doing a linear search through all the lockowners.
2991 * Yipes! For now we'll just hope clients aren't really using
2992 * release_lockowner much, but eventually we have to fix these
2993 * data structures. */
2994 INIT_LIST_HEAD(&matches);
2995 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2996 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
2997 if (!cmp_owner_str(sop, owner, clid))
2998 continue;
2999 list_for_each_entry(stp, &sop->so_stateids,
3000 st_perstateowner) {
3001 if (check_for_locks(stp->st_vfs_file, sop))
3002 goto out;
3003 /* Note: so_perclient unused for lockowners,
3004 * so it's OK to fool with here. */
3005 list_add(&sop->so_perclient, &matches);
3006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07003008 }
3009 /* Clients probably won't expect us to return with some (but not all)
3010 * of the lockowner state released; so don't release any until all
3011 * have been checked. */
3012 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07003013 while (!list_empty(&matches)) {
3014 sop = list_entry(matches.next, struct nfs4_stateowner,
3015 so_perclient);
3016 /* unhash_stateowner deletes so_perclient only
3017 * for openowners. */
3018 list_del(&sop->so_perclient);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003019 release_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021out:
3022 nfs4_unlock_state();
3023 return status;
3024}
3025
3026static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07003027alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
NeilBrowna55370a2005-06-23 22:03:52 -07003029 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
NeilBrownc7b9a452005-06-23 22:04:30 -07003032int
3033nfs4_has_reclaimed_state(const char *name)
3034{
3035 unsigned int strhashval = clientstr_hashval(name);
3036 struct nfs4_client *clp;
3037
3038 clp = find_confirmed_client_by_str(name, strhashval);
3039 return clp ? 1 : 0;
3040}
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042/*
3043 * failure => all reset bets are off, nfserr_no_grace...
3044 */
NeilBrown190e4fb2005-06-23 22:04:25 -07003045int
3046nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
3048 unsigned int strhashval;
3049 struct nfs4_client_reclaim *crp = NULL;
3050
NeilBrowna55370a2005-06-23 22:03:52 -07003051 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3052 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (!crp)
3054 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07003055 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 INIT_LIST_HEAD(&crp->cr_strhash);
3057 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07003058 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 reclaim_str_hashtbl_size++;
3060 return 1;
3061}
3062
3063static void
3064nfs4_release_reclaim(void)
3065{
3066 struct nfs4_client_reclaim *crp = NULL;
3067 int i;
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3070 while (!list_empty(&reclaim_str_hashtbl[i])) {
3071 crp = list_entry(reclaim_str_hashtbl[i].next,
3072 struct nfs4_client_reclaim, cr_strhash);
3073 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 kfree(crp);
3075 reclaim_str_hashtbl_size--;
3076 }
3077 }
3078 BUG_ON(reclaim_str_hashtbl_size);
3079}
3080
3081/*
3082 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07003083static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084nfs4_find_reclaim_client(clientid_t *clid)
3085{
3086 unsigned int strhashval;
3087 struct nfs4_client *clp;
3088 struct nfs4_client_reclaim *crp = NULL;
3089
3090
3091 /* find clientid in conf_id_hashtbl */
3092 clp = find_confirmed_client(clid);
3093 if (clp == NULL)
3094 return NULL;
3095
NeilBrowna55370a2005-06-23 22:03:52 -07003096 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3097 clp->cl_name.len, clp->cl_name.data,
3098 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07003101 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07003103 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return crp;
3105 }
3106 }
3107 return NULL;
3108}
3109
3110/*
3111* Called from OPEN. Look for clientid in reclaim list.
3112*/
3113int
3114nfs4_check_open_reclaim(clientid_t *clid)
3115{
NeilBrowndfc83562005-06-23 22:03:16 -07003116 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
NeilBrownac4d8ff22005-06-23 22:03:30 -07003119/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
NeilBrownac4d8ff22005-06-23 22:03:30 -07003121void
3122nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
3124 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3127 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3128 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3129 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3130 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3131 }
3132 for (i = 0; i < FILE_HASH_SIZE; i++) {
3133 INIT_LIST_HEAD(&file_hashtbl[i]);
3134 }
3135 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3136 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3137 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3138 }
3139 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3140 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3141 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3142 }
3143 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3144 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3145 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 INIT_LIST_HEAD(&close_lru);
3149 INIT_LIST_HEAD(&client_lru);
3150 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07003151 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3152 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3153 reclaim_str_hashtbl_size = 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003154}
3155
NeilBrown190e4fb2005-06-23 22:04:25 -07003156static void
3157nfsd4_load_reboot_recovery_data(void)
3158{
3159 int status;
3160
NeilBrown0964a3d2005-06-23 22:04:32 -07003161 nfs4_lock_state();
3162 nfsd4_init_recdir(user_recovery_dirname);
NeilBrown190e4fb2005-06-23 22:04:25 -07003163 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07003164 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07003165 if (status)
3166 printk("NFSD: Failure reading reboot recovery data\n");
3167}
3168
NeilBrownac4d8ff22005-06-23 22:03:30 -07003169/* initialization to perform when the nfsd service is started: */
3170
3171static void
3172__nfs4_state_start(void)
3173{
3174 time_t grace_time;
3175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 boot_time = get_seconds();
NeilBrownd99a05a2005-06-23 22:03:21 -07003177 grace_time = max(user_lease_time, lease_time);
3178 lease_time = user_lease_time;
NeilBrowna76b4312005-06-23 22:04:01 -07003179 in_grace = 1;
NeilBrownd99a05a2005-06-23 22:03:21 -07003180 printk("NFSD: starting %ld-second grace period\n", grace_time);
NeilBrown58da2822005-06-23 22:03:19 -07003181 laundry_wq = create_singlethread_workqueue("nfsd4");
NeilBrowna76b4312005-06-23 22:04:01 -07003182 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
3185int
NeilBrown76a35502005-06-23 22:03:26 -07003186nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
3188 int status;
3189
3190 if (nfs4_init)
3191 return 0;
3192 status = nfsd4_init_slabs();
3193 if (status)
3194 return status;
NeilBrown190e4fb2005-06-23 22:04:25 -07003195 nfsd4_load_reboot_recovery_data();
NeilBrown76a35502005-06-23 22:03:26 -07003196 __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 nfs4_init = 1;
3198 return 0;
3199}
3200
3201int
3202nfs4_in_grace(void)
3203{
NeilBrowna76b4312005-06-23 22:04:01 -07003204 return in_grace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
3207time_t
3208nfs4_lease_time(void)
3209{
3210 return lease_time;
3211}
3212
3213static void
3214__nfs4_state_shutdown(void)
3215{
3216 int i;
3217 struct nfs4_client *clp = NULL;
3218 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 struct list_head *pos, *next, reaplist;
3220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3222 while (!list_empty(&conf_id_hashtbl[i])) {
3223 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3224 expire_client(clp);
3225 }
3226 while (!list_empty(&unconf_str_hashtbl[i])) {
3227 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3228 expire_client(clp);
3229 }
3230 }
3231 INIT_LIST_HEAD(&reaplist);
3232 spin_lock(&recall_lock);
3233 list_for_each_safe(pos, next, &del_recall_lru) {
3234 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3235 list_move(&dp->dl_recall_lru, &reaplist);
3236 }
3237 spin_unlock(&recall_lock);
3238 list_for_each_safe(pos, next, &reaplist) {
3239 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3240 list_del_init(&dp->dl_recall_lru);
3241 unhash_delegation(dp);
3242 }
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 cancel_delayed_work(&laundromat_work);
NeilBrown190e4fb2005-06-23 22:04:25 -07003245 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 nfs4_init = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
3249void
3250nfs4_state_shutdown(void)
3251{
NeilBrown5e8d5c22006-04-10 22:55:37 -07003252 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3253 destroy_workqueue(laundry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 nfs4_lock_state();
3255 nfs4_release_reclaim();
3256 __nfs4_state_shutdown();
3257 nfsd4_free_slabs();
3258 nfs4_unlock_state();
3259}
3260
NeilBrown0964a3d2005-06-23 22:04:32 -07003261static void
3262nfs4_set_recdir(char *recdir)
3263{
3264 nfs4_lock_state();
3265 strcpy(user_recovery_dirname, recdir);
3266 nfs4_unlock_state();
3267}
3268
3269/*
3270 * Change the NFSv4 recovery directory to recdir.
3271 */
3272int
3273nfs4_reset_recoverydir(char *recdir)
3274{
3275 int status;
3276 struct nameidata nd;
3277
3278 status = path_lookup(recdir, LOOKUP_FOLLOW, &nd);
3279 if (status)
3280 return status;
3281 status = -ENOTDIR;
3282 if (S_ISDIR(nd.dentry->d_inode->i_mode)) {
3283 nfs4_set_recdir(recdir);
3284 status = 0;
3285 }
3286 path_release(&nd);
3287 return status;
3288}
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290/*
3291 * Called when leasetime is changed.
3292 *
NeilBrownd99a05a2005-06-23 22:03:21 -07003293 * The only way the protocol gives us to handle on-the-fly lease changes is to
3294 * simulate a reboot. Instead of doing that, we just wait till the next time
3295 * we start to register any changes in lease time. If the administrator
3296 * really wants to change the lease time *now*, they can go ahead and bring
3297 * nfsd down and then back up again after changing the lease time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 */
3299void
3300nfs4_reset_lease(time_t leasetime)
3301{
NeilBrownd99a05a2005-06-23 22:03:21 -07003302 lock_kernel();
3303 user_lease_time = leasetime;
3304 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305}