blob: b6f60f48e94b4038473faf04b766ce706e60a9d4 [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>
Dave Hansenaceaf782008-02-15 14:37:31 -080044#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mount.h>
46#include <linux/workqueue.h>
47#include <linux/smp_lock.h>
48#include <linux/kthread.h>
49#include <linux/nfs4.h>
50#include <linux/nfsd/state.h>
51#include <linux/nfsd/xdr4.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070052#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070053#include <linux/swap.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080054#include <linux/mutex.h>
Marc Eshelfd85b812006-11-28 16:26:41 -050055#include <linux/lockd/bind.h>
Marc Eshel9a8db972007-07-17 04:04:35 -070056#include <linux/module.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050057#include <linux/sunrpc/svcauth_gss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define NFSDDBG_FACILITY NFSDDBG_PROC
60
61/* Globals */
62static time_t lease_time = 90; /* default lease time */
NeilBrownd99a05a2005-06-23 22:03:21 -070063static time_t user_lease_time = 90;
NeilBrownfd39ca92005-06-23 22:04:03 -070064static time_t boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static u32 current_ownerid = 1;
66static u32 current_fileid = 1;
67static u32 current_delegid = 1;
68static u32 nfs4_init;
NeilBrownfd39ca92005-06-23 22:04:03 -070069static stateid_t zerostateid; /* bits all 0 */
70static stateid_t onestateid; /* bits all 1 */
71
72#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
73#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* forward declarations */
NeilBrownfd39ca92005-06-23 22:04:03 -070076static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
78static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
NeilBrown0964a3d2005-06-23 22:04:32 -070079static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
80static void nfs4_set_recdir(char *recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* Locking:
83 *
Ingo Molnar353ab6e2006-03-26 01:37:12 -080084 * client_mutex:
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * protects clientid_hashtbl[], clientstr_hashtbl[],
86 * unconfstr_hashtbl[], uncofid_hashtbl[].
87 */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080088static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Christoph Lametere18b8902006-12-06 20:33:20 -080090static struct kmem_cache *stateowner_slab = NULL;
91static struct kmem_cache *file_slab = NULL;
92static struct kmem_cache *stateid_slab = NULL;
93static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095void
96nfs4_lock_state(void)
97{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080098 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101void
102nfs4_unlock_state(void)
103{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800104 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107static inline u32
108opaque_hashval(const void *ptr, int nbytes)
109{
110 unsigned char *cptr = (unsigned char *) ptr;
111
112 u32 x = 0;
113 while (nbytes--) {
114 x *= 37;
115 x += *cptr++;
116 }
117 return x;
118}
119
120/* forward declarations */
121static void release_stateowner(struct nfs4_stateowner *sop);
122static void release_stateid(struct nfs4_stateid *stp, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/*
125 * Delegation state
126 */
127
128/* recall_lock protects the del_recall_lru */
Ingo Molnar34af9462006-06-27 02:53:55 -0700129static DEFINE_SPINLOCK(recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static struct list_head del_recall_lru;
131
NeilBrown13cd2182005-06-23 22:03:10 -0700132static void
133free_nfs4_file(struct kref *kref)
134{
135 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
136 list_del(&fp->fi_hash);
137 iput(fp->fi_inode);
138 kmem_cache_free(file_slab, fp);
139}
140
141static inline void
142put_nfs4_file(struct nfs4_file *fi)
143{
144 kref_put(&fi->fi_ref, free_nfs4_file);
145}
146
147static inline void
148get_nfs4_file(struct nfs4_file *fi)
149{
150 kref_get(&fi->fi_ref);
151}
152
NeilBrownef0f3392006-04-10 22:55:41 -0700153static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700154unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700155
156/*
157 * Open owner state (share locks)
158 */
159
160/* hash tables for nfs4_stateowner */
161#define OWNER_HASH_BITS 8
162#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
163#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
164
165#define ownerid_hashval(id) \
166 ((id) & OWNER_HASH_MASK)
167#define ownerstr_hashval(clientid, ownername) \
168 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
169
170static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
171static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
172
173/* hash table for nfs4_file */
174#define FILE_HASH_BITS 8
175#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
176#define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
177/* hash table for (open)nfs4_stateid */
178#define STATEID_HASH_BITS 10
179#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
180#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
181
182#define file_hashval(x) \
183 hash_ptr(x, FILE_HASH_BITS)
184#define stateid_hashval(owner_id, file_id) \
185 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
186
187static struct list_head file_hashtbl[FILE_HASH_SIZE];
188static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static struct nfs4_delegation *
191alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
192{
193 struct nfs4_delegation *dp;
194 struct nfs4_file *fp = stp->st_file;
195 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
196
197 dprintk("NFSD alloc_init_deleg\n");
Meelap Shah47f99402007-07-17 04:04:40 -0700198 if (fp->fi_had_conflict)
199 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700200 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700201 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700202 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
203 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700205 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700206 INIT_LIST_HEAD(&dp->dl_perfile);
207 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 INIT_LIST_HEAD(&dp->dl_recall_lru);
209 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700210 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 dp->dl_file = fp;
212 dp->dl_flock = NULL;
213 get_file(stp->st_vfs_file);
214 dp->dl_vfs_file = stp->st_vfs_file;
215 dp->dl_type = type;
216 dp->dl_recall.cbr_dp = NULL;
217 dp->dl_recall.cbr_ident = cb->cb_ident;
218 dp->dl_recall.cbr_trunc = 0;
219 dp->dl_stateid.si_boot = boot_time;
220 dp->dl_stateid.si_stateownerid = current_delegid++;
221 dp->dl_stateid.si_fileid = 0;
222 dp->dl_stateid.si_generation = 0;
223 dp->dl_fhlen = current_fh->fh_handle.fh_size;
224 memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
225 current_fh->fh_handle.fh_size);
226 dp->dl_time = 0;
227 atomic_set(&dp->dl_count, 1);
NeilBrownea1da632005-06-23 22:04:17 -0700228 list_add(&dp->dl_perfile, &fp->fi_delegations);
229 list_add(&dp->dl_perclnt, &clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return dp;
231}
232
233void
234nfs4_put_delegation(struct nfs4_delegation *dp)
235{
236 if (atomic_dec_and_test(&dp->dl_count)) {
237 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700238 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700239 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700240 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242}
243
244/* Remove the associated file_lock first, then remove the delegation.
245 * lease_modify() is called to remove the FS_LEASE file_lock from
246 * the i_flock list, eventually calling nfsd's lock_manager
247 * fl_release_callback.
248 */
249static void
250nfs4_close_delegation(struct nfs4_delegation *dp)
251{
252 struct file *filp = dp->dl_vfs_file;
253
254 dprintk("NFSD: close_delegation dp %p\n",dp);
255 dp->dl_vfs_file = NULL;
256 /* The following nfsd_close may not actually close the file,
257 * but we want to remove the lease in any case. */
NeilBrownc9071322005-04-16 15:26:38 -0700258 if (dp->dl_flock)
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -0400259 vfs_setlease(filp, F_UNLCK, &dp->dl_flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/* Called under the state lock. */
264static void
265unhash_delegation(struct nfs4_delegation *dp)
266{
NeilBrownea1da632005-06-23 22:04:17 -0700267 list_del_init(&dp->dl_perfile);
268 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spin_lock(&recall_lock);
270 list_del_init(&dp->dl_recall_lru);
271 spin_unlock(&recall_lock);
272 nfs4_close_delegation(dp);
273 nfs4_put_delegation(dp);
274}
275
276/*
277 * SETCLIENTID state
278 */
279
280/* Hash tables for nfs4_clientid state */
281#define CLIENT_HASH_BITS 4
282#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
283#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
284
285#define clientid_hashval(id) \
286 ((id) & CLIENT_HASH_MASK)
NeilBrowna55370a2005-06-23 22:03:52 -0700287#define clientstr_hashval(name) \
288 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*
290 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
291 * used in reboot/reset lease grace period processing
292 *
293 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
294 * setclientid_confirmed info.
295 *
296 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
297 * setclientid info.
298 *
299 * client_lru holds client queue ordered by nfs4_client.cl_time
300 * for lease renewal.
301 *
302 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
303 * for last close replay.
304 */
305static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
306static int reclaim_str_hashtbl_size = 0;
307static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
308static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
309static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
310static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
311static struct list_head client_lru;
312static struct list_head close_lru;
313
314static inline void
315renew_client(struct nfs4_client *clp)
316{
317 /*
318 * Move client to the end to the LRU list.
319 */
320 dprintk("renewing client (clientid %08x/%08x)\n",
321 clp->cl_clientid.cl_boot,
322 clp->cl_clientid.cl_id);
323 list_move_tail(&clp->cl_lru, &client_lru);
324 clp->cl_time = get_seconds();
325}
326
327/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
328static int
329STALE_CLIENTID(clientid_t *clid)
330{
331 if (clid->cl_boot == boot_time)
332 return 0;
333 dprintk("NFSD stale clientid (%08x/%08x)\n",
334 clid->cl_boot, clid->cl_id);
335 return 1;
336}
337
338/*
339 * XXX Should we use a slab cache ?
340 * This type of memory management is somewhat inefficient, but we use it
341 * anyway since SETCLIENTID is not a common operation.
342 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500343static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct nfs4_client *clp;
346
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500347 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
348 if (clp == NULL)
349 return NULL;
350 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
351 if (clp->cl_name.data == NULL) {
352 kfree(clp);
353 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500355 memcpy(clp->cl_name.data, name.data, name.len);
356 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return clp;
358}
359
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400360static void
361shutdown_callback_client(struct nfs4_client *clp)
362{
363 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
364
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400365 if (clnt) {
J. Bruce Fields404ec112007-11-23 22:26:18 -0500366 /*
367 * Callback threads take a reference on the client, so there
368 * should be no outstanding callbacks at this point.
369 */
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400370 clp->cl_callback.cb_client = NULL;
371 rpc_shutdown_client(clnt);
372 }
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static inline void
376free_client(struct nfs4_client *clp)
377{
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400378 shutdown_callback_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (clp->cl_cred.cr_group_info)
380 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500381 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kfree(clp->cl_name.data);
383 kfree(clp);
384}
385
386void
387put_nfs4_client(struct nfs4_client *clp)
388{
389 if (atomic_dec_and_test(&clp->cl_count))
390 free_client(clp);
391}
392
393static void
394expire_client(struct nfs4_client *clp)
395{
396 struct nfs4_stateowner *sop;
397 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct list_head reaplist;
399
400 dprintk("NFSD: expire_client cl_count %d\n",
401 atomic_read(&clp->cl_count));
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 INIT_LIST_HEAD(&reaplist);
404 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700405 while (!list_empty(&clp->cl_delegations)) {
406 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
408 dp->dl_flock);
NeilBrownea1da632005-06-23 22:04:17 -0700409 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 list_move(&dp->dl_recall_lru, &reaplist);
411 }
412 spin_unlock(&recall_lock);
413 while (!list_empty(&reaplist)) {
414 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
415 list_del_init(&dp->dl_recall_lru);
416 unhash_delegation(dp);
417 }
418 list_del(&clp->cl_idhash);
419 list_del(&clp->cl_strhash);
420 list_del(&clp->cl_lru);
NeilBrownea1da632005-06-23 22:04:17 -0700421 while (!list_empty(&clp->cl_openowners)) {
422 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 release_stateowner(sop);
424 }
425 put_nfs4_client(clp);
426}
427
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500428static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir)
429{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct nfs4_client *clp;
431
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500432 clp = alloc_client(name);
433 if (clp == NULL)
434 return NULL;
NeilBrowna55370a2005-06-23 22:03:52 -0700435 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 atomic_set(&clp->cl_count, 1);
437 atomic_set(&clp->cl_callback.cb_set, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 INIT_LIST_HEAD(&clp->cl_idhash);
439 INIT_LIST_HEAD(&clp->cl_strhash);
NeilBrownea1da632005-06-23 22:04:17 -0700440 INIT_LIST_HEAD(&clp->cl_openowners);
441 INIT_LIST_HEAD(&clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 INIT_LIST_HEAD(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return clp;
444}
445
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500446static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
447{
448 memcpy(target->cl_verifier.data, source->data,
449 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500452static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
453{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
455 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
456}
457
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500458static void copy_cred(struct svc_cred *target, struct svc_cred *source)
459{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 target->cr_uid = source->cr_uid;
461 target->cr_gid = source->cr_gid;
462 target->cr_group_info = source->cr_group_info;
463 get_group_info(target->cr_group_info);
464}
465
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500466static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400467{
NeilBrowna55370a2005-06-23 22:03:52 -0700468 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400472same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
473{
474 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400478same_clid(clientid_t *cl1, clientid_t *cl2)
479{
480 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483/* XXX what about NGROUP */
484static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400485same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
486{
487 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
J. Bruce Fields5ec7b462007-11-21 21:58:56 -0500490static void gen_clid(struct nfs4_client *clp)
491{
492 static u32 current_clientid = 1;
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 clp->cl_clientid.cl_boot = boot_time;
495 clp->cl_clientid.cl_id = current_clientid++;
496}
497
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -0500498static void gen_confirm(struct nfs4_client *clp)
499{
500 static u32 i;
501 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -0500504 *p++ = get_seconds();
505 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500508static int check_name(struct xdr_netobj name)
509{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (name.len == 0)
511 return 0;
512 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400513 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
515 }
516 return 1;
517}
518
NeilBrownfd39ca92005-06-23 22:04:03 -0700519static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
521{
522 unsigned int idhashval;
523
524 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
525 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
526 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
527 list_add_tail(&clp->cl_lru, &client_lru);
528 clp->cl_time = get_seconds();
529}
530
NeilBrownfd39ca92005-06-23 22:04:03 -0700531static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532move_to_confirmed(struct nfs4_client *clp)
533{
534 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
535 unsigned int strhashval;
536
537 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
538 list_del_init(&clp->cl_strhash);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700539 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -0700540 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
542 renew_client(clp);
543}
544
545static struct nfs4_client *
546find_confirmed_client(clientid_t *clid)
547{
548 struct nfs4_client *clp;
549 unsigned int idhashval = clientid_hashval(clid->cl_id);
550
551 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400552 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return clp;
554 }
555 return NULL;
556}
557
558static struct nfs4_client *
559find_unconfirmed_client(clientid_t *clid)
560{
561 struct nfs4_client *clp;
562 unsigned int idhashval = clientid_hashval(clid->cl_id);
563
564 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400565 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return clp;
567 }
568 return NULL;
569}
570
NeilBrown28ce6052005-06-23 22:03:56 -0700571static struct nfs4_client *
572find_confirmed_client_by_str(const char *dname, unsigned int hashval)
573{
574 struct nfs4_client *clp;
575
576 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
577 if (same_name(clp->cl_recdir, dname))
578 return clp;
579 }
580 return NULL;
581}
582
583static struct nfs4_client *
584find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
585{
586 struct nfs4_client *clp;
587
588 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
589 if (same_name(clp->cl_recdir, dname))
590 return clp;
591 }
592 return NULL;
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/* a helper function for parse_callback */
596static int
597parse_octet(unsigned int *lenp, char **addrp)
598{
599 unsigned int len = *lenp;
600 char *p = *addrp;
601 int n = -1;
602 char c;
603
604 for (;;) {
605 if (!len)
606 break;
607 len--;
608 c = *p++;
609 if (c == '.')
610 break;
611 if ((c < '0') || (c > '9')) {
612 n = -1;
613 break;
614 }
615 if (n < 0)
616 n = 0;
617 n = (n * 10) + (c - '0');
618 if (n > 255) {
619 n = -1;
620 break;
621 }
622 }
623 *lenp = len;
624 *addrp = p;
625 return n;
626}
627
628/* parse and set the setclientid ipv4 callback address */
NeilBrownfd39ca92005-06-23 22:04:03 -0700629static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
631{
632 int temp = 0;
633 u32 cbaddr = 0;
634 u16 cbport = 0;
635 u32 addrlen = addr_len;
636 char *addr = addr_val;
637 int i, shift;
638
639 /* ipaddress */
640 shift = 24;
641 for(i = 4; i > 0 ; i--) {
642 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
643 return 0;
644 }
645 cbaddr |= (temp << shift);
646 if (shift > 0)
647 shift -= 8;
648 }
649 *cbaddrp = cbaddr;
650
651 /* port */
652 shift = 8;
653 for(i = 2; i > 0 ; i--) {
654 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
655 return 0;
656 }
657 cbport |= (temp << shift);
658 if (shift > 0)
659 shift -= 8;
660 }
661 *cbportp = cbport;
662 return 1;
663}
664
NeilBrownfd39ca92005-06-23 22:04:03 -0700665static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
667{
668 struct nfs4_callback *cb = &clp->cl_callback;
669
670 /* Currently, we only support tcp for the callback channel */
671 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
672 goto out_err;
673
674 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
675 &cb->cb_addr, &cb->cb_port)))
676 goto out_err;
677 cb->cb_prog = se->se_callback_prog;
678 cb->cb_ident = se->se_callback_ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return;
680out_err:
Neil Brown849823c2005-09-13 01:25:36 -0700681 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 "will not receive delegations\n",
683 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return;
686}
687
Al Virob37ad282006-10-19 23:28:59 -0700688__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800689nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
690 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Chuck Lever27459f02007-02-12 00:53:34 -0800692 struct sockaddr_in *sin = svc_addr_in(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct xdr_netobj clname = {
694 .len = setclid->se_namelen,
695 .data = setclid->se_name,
696 };
697 nfs4_verifier clverifier = setclid->se_verf;
698 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -0700699 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -0700700 __be32 status;
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500701 char *princ;
NeilBrowna55370a2005-06-23 22:03:52 -0700702 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -0700705 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
NeilBrowna55370a2005-06-23 22:03:52 -0700707 status = nfs4_make_rec_clidname(dname, &clname);
708 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -0700709 return status;
NeilBrowna55370a2005-06-23 22:03:52 -0700710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /*
712 * XXX The Duplicate Request Cache (DRC) has been checked (??)
713 * We get here on a DRC miss.
714 */
715
NeilBrowna55370a2005-06-23 22:03:52 -0700716 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 nfs4_lock_state();
NeilBrown28ce6052005-06-23 22:03:56 -0700719 conf = find_confirmed_client_by_str(dname, strhashval);
720 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500721 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 status = nfserr_clid_inuse;
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400723 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)
Chuck Lever27459f02007-02-12 00:53:34 -0800724 || conf->cl_addr != sin->sin_addr.s_addr) {
Harvey Harrisonbe859402008-10-31 00:56:28 -0700725 dprintk("NFSD: setclientid: string in use by clientat %pI4\n",
726 &conf->cl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 goto out;
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500730 /*
731 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
732 * has a description of SETCLIENTID request processing consisting
733 * of 5 bullet points, labeled as CASE0 - CASE4 below.
734 */
NeilBrown28ce6052005-06-23 22:03:56 -0700735 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 status = nfserr_resource;
737 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500738 /*
739 * RFC 3530 14.2.33 CASE 4:
740 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
742 if (unconf)
743 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700744 new = create_client(clname, dname);
745 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400748 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500750 * RFC 3530 14.2.33 CASE 1:
751 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
NeilBrown31f4a6c2005-06-23 22:04:06 -0700753 if (unconf) {
754 /* Note this is removing unconfirmed {*x***},
755 * which is stronger than RFC recommended {vxc**}.
756 * This has the advantage that there is at most
757 * one {*x***} in either list at any time.
758 */
759 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
NeilBrowna55370a2005-06-23 22:03:52 -0700761 new = create_client(clname, dname);
762 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 } else if (!unconf) {
766 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500767 * RFC 3530 14.2.33 CASE 2:
768 * probable client reboot; state will be removed if
769 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
NeilBrowna55370a2005-06-23 22:03:52 -0700771 new = create_client(clname, dname);
772 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -0500775 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500776 /*
777 * RFC 3530 14.2.33 CASE 3:
778 * probable client reboot; state will be removed if
779 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
781 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700782 new = create_client(clname, dname);
783 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
J. Bruce Fieldsc175b832007-08-09 18:34:32 -0400787 copy_verf(new, &clverifier);
788 new->cl_addr = sin->sin_addr.s_addr;
Olga Kornievskaia61054b12008-12-23 16:19:00 -0500789 new->cl_flavor = rqstp->rq_flavor;
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500790 princ = svc_gss_principal(rqstp);
791 if (princ) {
792 new->cl_principal = kstrdup(princ, GFP_KERNEL);
793 if (new->cl_principal == NULL) {
794 free_client(new);
795 goto out;
796 }
797 }
J. Bruce Fieldsc175b832007-08-09 18:34:32 -0400798 copy_cred(&new->cl_cred, &rqstp->rq_cred);
799 gen_confirm(new);
800 gen_callback(new, setclid);
801 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
803 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
804 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
805 status = nfs_ok;
806out:
807 nfs4_unlock_state();
808 return status;
809}
810
811
812/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500813 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
814 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
815 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Al Virob37ad282006-10-19 23:28:59 -0700817__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800818nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
819 struct nfsd4_compound_state *cstate,
820 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Chuck Lever27459f02007-02-12 00:53:34 -0800822 struct sockaddr_in *sin = svc_addr_in(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -0700823 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
825 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -0700826 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 if (STALE_CLIENTID(clid))
829 return nfserr_stale_clientid;
830 /*
831 * XXX The Duplicate Request Cache (DRC) has been checked (??)
832 * We get here on a DRC miss.
833 */
834
835 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -0700836
837 conf = find_confirmed_client(clid);
838 unconf = find_unconfirmed_client(clid);
839
840 status = nfserr_clid_inuse;
Chuck Lever27459f02007-02-12 00:53:34 -0800841 if (conf && conf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -0700842 goto out;
Chuck Lever27459f02007-02-12 00:53:34 -0800843 if (unconf && unconf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -0700844 goto out;
845
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500846 /*
847 * section 14.2.34 of RFC 3530 has a description of
848 * SETCLIENTID_CONFIRM request processing consisting
849 * of 4 bullet points, labeled as CASE1 - CASE4 below.
850 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -0500851 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500852 /*
853 * RFC 3530 14.2.34 CASE 1:
854 * callback update
855 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400856 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 status = nfserr_clid_inuse;
858 else {
NeilBrown1a69c172005-06-23 22:04:08 -0700859 /* XXX: We just turn off callbacks until we can handle
860 * change request correctly. */
NeilBrowncb36d632005-06-23 22:04:23 -0700861 atomic_set(&conf->cl_callback.cb_set, 0);
NeilBrown21ab45a2005-06-23 22:04:14 -0700862 gen_confirm(conf);
NeilBrown46309022005-07-07 17:59:10 -0700863 nfsd4_remove_clid_dir(unconf);
NeilBrown1a69c172005-06-23 22:04:08 -0700864 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -0700866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -0500868 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500869 /*
870 * RFC 3530 14.2.34 CASE 2:
871 * probable retransmitted request; play it safe and
872 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -0700873 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400874 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -0700876 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -0700878 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400879 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500880 /*
881 * RFC 3530 14.2.34 CASE 3:
882 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -0700883 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400884 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 status = nfserr_clid_inuse;
886 } else {
NeilBrown1a69c172005-06-23 22:04:08 -0700887 unsigned int hash =
888 clientstr_hashval(unconf->cl_recdir);
889 conf = find_confirmed_client_by_str(unconf->cl_recdir,
890 hash);
891 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -0700892 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -0700893 expire_client(conf);
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -0700896 conf = unconf;
J. Bruce Fields46f8a642007-11-22 13:54:18 -0500897 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -0700898 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400900 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
901 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -0700902 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500903 /*
904 * RFC 3530 14.2.34 CASE 4:
905 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -0700906 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -0700908 } else {
NeilBrown08e89872005-06-23 22:04:11 -0700909 /* check that we have hit one of the cases...*/
910 status = nfserr_clid_inuse;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 nfs4_unlock_state();
914 return status;
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/* OPEN Share state helper functions */
918static inline struct nfs4_file *
919alloc_init_file(struct inode *ino)
920{
921 struct nfs4_file *fp;
922 unsigned int hashval = file_hashval(ino);
923
NeilBrowne60d4392005-06-23 22:03:01 -0700924 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
925 if (fp) {
NeilBrown13cd2182005-06-23 22:03:10 -0700926 kref_init(&fp->fi_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -0700928 INIT_LIST_HEAD(&fp->fi_stateids);
929 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
931 fp->fi_inode = igrab(ino);
932 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -0700933 fp->fi_had_conflict = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return fp;
935 }
936 return NULL;
937}
938
939static void
Christoph Lametere18b8902006-12-06 20:33:20 -0800940nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -0700941{
NeilBrowne60d4392005-06-23 22:03:01 -0700942 if (*slab == NULL)
943 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700944 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -0700945 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -0700946}
947
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -0400948void
NeilBrowne60d4392005-06-23 22:03:01 -0700949nfsd4_free_slabs(void)
950{
951 nfsd4_free_slab(&stateowner_slab);
952 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -0700953 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700954 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -0700955}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957static int
958nfsd4_init_slabs(void)
959{
960 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
Paul Mundt20c2df82007-07-20 10:11:58 +0900961 sizeof(struct nfs4_stateowner), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -0700962 if (stateowner_slab == NULL)
963 goto out_nomem;
964 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +0900965 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -0700966 if (file_slab == NULL)
967 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -0700968 stateid_slab = kmem_cache_create("nfsd4_stateids",
Paul Mundt20c2df82007-07-20 10:11:58 +0900969 sizeof(struct nfs4_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -0700970 if (stateid_slab == NULL)
971 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700972 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +0900973 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700974 if (deleg_slab == NULL)
975 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -0700977out_nomem:
978 nfsd4_free_slabs();
979 dprintk("nfsd4: out of memory while initializing nfsv4\n");
980 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983void
984nfs4_free_stateowner(struct kref *kref)
985{
986 struct nfs4_stateowner *sop =
987 container_of(kref, struct nfs4_stateowner, so_ref);
988 kfree(sop->so_owner.data);
989 kmem_cache_free(stateowner_slab, sop);
990}
991
992static inline struct nfs4_stateowner *
993alloc_stateowner(struct xdr_netobj *owner)
994{
995 struct nfs4_stateowner *sop;
996
997 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
998 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
999 memcpy(sop->so_owner.data, owner->data, owner->len);
1000 sop->so_owner.len = owner->len;
1001 kref_init(&sop->so_ref);
1002 return sop;
1003 }
1004 kmem_cache_free(stateowner_slab, sop);
1005 }
1006 return NULL;
1007}
1008
1009static struct nfs4_stateowner *
1010alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1011 struct nfs4_stateowner *sop;
1012 struct nfs4_replay *rp;
1013 unsigned int idhashval;
1014
1015 if (!(sop = alloc_stateowner(&open->op_owner)))
1016 return NULL;
1017 idhashval = ownerid_hashval(current_ownerid);
1018 INIT_LIST_HEAD(&sop->so_idhash);
1019 INIT_LIST_HEAD(&sop->so_strhash);
1020 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001021 INIT_LIST_HEAD(&sop->so_stateids);
1022 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 INIT_LIST_HEAD(&sop->so_close_lru);
1024 sop->so_time = 0;
1025 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1026 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001027 list_add(&sop->so_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 sop->so_is_open_owner = 1;
1029 sop->so_id = current_ownerid++;
1030 sop->so_client = clp;
1031 sop->so_seqid = open->op_seqid;
1032 sop->so_confirmed = 0;
1033 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08001034 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 rp->rp_buflen = 0;
1036 rp->rp_buf = rp->rp_ibuf;
1037 return sop;
1038}
1039
1040static void
1041release_stateid_lockowners(struct nfs4_stateid *open_stp)
1042{
1043 struct nfs4_stateowner *lock_sop;
1044
NeilBrownea1da632005-06-23 22:04:17 -07001045 while (!list_empty(&open_stp->st_lockowners)) {
1046 lock_sop = list_entry(open_stp->st_lockowners.next,
1047 struct nfs4_stateowner, so_perstateid);
1048 /* list_del(&open_stp->st_lockowners); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 BUG_ON(lock_sop->so_is_open_owner);
1050 release_stateowner(lock_sop);
1051 }
1052}
1053
1054static void
1055unhash_stateowner(struct nfs4_stateowner *sop)
1056{
1057 struct nfs4_stateid *stp;
1058
1059 list_del(&sop->so_idhash);
1060 list_del(&sop->so_strhash);
NeilBrown6fa305d2005-06-23 22:03:06 -07001061 if (sop->so_is_open_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 list_del(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001063 list_del(&sop->so_perstateid);
1064 while (!list_empty(&sop->so_stateids)) {
1065 stp = list_entry(sop->so_stateids.next,
1066 struct nfs4_stateid, st_perstateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (sop->so_is_open_owner)
1068 release_stateid(stp, OPEN_STATE);
1069 else
1070 release_stateid(stp, LOCK_STATE);
1071 }
1072}
1073
1074static void
1075release_stateowner(struct nfs4_stateowner *sop)
1076{
1077 unhash_stateowner(sop);
1078 list_del(&sop->so_close_lru);
1079 nfs4_put_stateowner(sop);
1080}
1081
1082static inline void
1083init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1084 struct nfs4_stateowner *sop = open->op_stateowner;
1085 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1086
1087 INIT_LIST_HEAD(&stp->st_hash);
NeilBrownea1da632005-06-23 22:04:17 -07001088 INIT_LIST_HEAD(&stp->st_perstateowner);
1089 INIT_LIST_HEAD(&stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 INIT_LIST_HEAD(&stp->st_perfile);
1091 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001092 list_add(&stp->st_perstateowner, &sop->so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07001093 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07001095 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 stp->st_file = fp;
1097 stp->st_stateid.si_boot = boot_time;
1098 stp->st_stateid.si_stateownerid = sop->so_id;
1099 stp->st_stateid.si_fileid = fp->fi_id;
1100 stp->st_stateid.si_generation = 0;
1101 stp->st_access_bmap = 0;
1102 stp->st_deny_bmap = 0;
1103 __set_bit(open->op_share_access, &stp->st_access_bmap);
1104 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07001105 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108static void
1109release_stateid(struct nfs4_stateid *stp, int flags)
1110{
1111 struct file *filp = stp->st_vfs_file;
1112
1113 list_del(&stp->st_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 list_del(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07001115 list_del(&stp->st_perstateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (flags & OPEN_STATE) {
1117 release_stateid_lockowners(stp);
1118 stp->st_vfs_file = NULL;
1119 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 } else if (flags & LOCK_STATE)
1121 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
NeilBrown13cd2182005-06-23 22:03:10 -07001122 put_nfs4_file(stp->st_file);
NeilBrown5ac049a2005-06-23 22:03:03 -07001123 kmem_cache_free(stateid_slab, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
NeilBrownfd39ca92005-06-23 22:04:03 -07001126static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127move_to_close_lru(struct nfs4_stateowner *sop)
1128{
1129 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1130
NeilBrown358dd552006-04-10 22:55:42 -07001131 list_move_tail(&sop->so_close_lru, &close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 sop->so_time = get_seconds();
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001136same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
1137 clientid_t *clid)
1138{
1139 return (sop->so_owner.len == owner->len) &&
1140 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
1141 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144static struct nfs4_stateowner *
1145find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1146{
1147 struct nfs4_stateowner *so = NULL;
1148
1149 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001150 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return so;
1152 }
1153 return NULL;
1154}
1155
1156/* search file_hashtbl[] for file */
1157static struct nfs4_file *
1158find_file(struct inode *ino)
1159{
1160 unsigned int hashval = file_hashval(ino);
1161 struct nfs4_file *fp;
1162
1163 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07001164 if (fp->fi_inode == ino) {
1165 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07001167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
1169 return NULL;
1170}
1171
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001172static inline int access_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001173{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001174 if (x < NFS4_SHARE_ACCESS_READ)
1175 return 0;
1176 if (x > NFS4_SHARE_ACCESS_BOTH)
1177 return 0;
1178 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001179}
1180
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001181static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001182{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001183 /* Note: unlike access bits, deny bits may be zero. */
1184 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04001187/*
1188 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1189 * st_{access,deny}_bmap field of the stateid, in order to track not
1190 * only what share bits are currently in force, but also what
1191 * combinations of share bits previous opens have used. This allows us
1192 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1193 * return an error if the client attempt to downgrade to a combination
1194 * of share bits not explicable by closing some of its previous opens.
1195 *
1196 * XXX: This enforcement is actually incomplete, since we don't keep
1197 * track of access/deny bit combinations; so, e.g., we allow:
1198 *
1199 * OPEN allow read, deny write
1200 * OPEN allow both, deny none
1201 * DOWNGRADE allow read, deny none
1202 *
1203 * which we should reject.
1204 */
NeilBrownfd39ca92005-06-23 22:04:03 -07001205static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206set_access(unsigned int *access, unsigned long bmap) {
1207 int i;
1208
1209 *access = 0;
1210 for (i = 1; i < 4; i++) {
1211 if (test_bit(i, &bmap))
1212 *access |= i;
1213 }
1214}
1215
NeilBrownfd39ca92005-06-23 22:04:03 -07001216static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217set_deny(unsigned int *deny, unsigned long bmap) {
1218 int i;
1219
1220 *deny = 0;
1221 for (i = 0; i < 4; i++) {
1222 if (test_bit(i, &bmap))
1223 *deny |= i ;
1224 }
1225}
1226
1227static int
1228test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1229 unsigned int access, deny;
1230
1231 set_access(&access, stp->st_access_bmap);
1232 set_deny(&deny, stp->st_deny_bmap);
1233 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1234 return 0;
1235 return 1;
1236}
1237
1238/*
1239 * Called to check deny when READ with all zero stateid or
1240 * WRITE with all zero or all one stateid
1241 */
Al Virob37ad282006-10-19 23:28:59 -07001242static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1244{
1245 struct inode *ino = current_fh->fh_dentry->d_inode;
1246 struct nfs4_file *fp;
1247 struct nfs4_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07001248 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 dprintk("NFSD: nfs4_share_conflict\n");
1251
1252 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07001253 if (!fp)
1254 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07001255 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07001257 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1258 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1259 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1260 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
NeilBrown13cd2182005-06-23 22:03:10 -07001262 ret = nfs_ok;
1263out:
1264 put_nfs4_file(fp);
1265 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268static inline void
1269nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1270{
1271 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
Dave Hansenaceaf782008-02-15 14:37:31 -08001272 drop_file_write_access(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1274 }
1275}
1276
1277/*
1278 * Recall a delegation
1279 */
1280static int
1281do_recall(void *__dp)
1282{
1283 struct nfs4_delegation *dp = __dp;
1284
Meelap Shah47f99402007-07-17 04:04:40 -07001285 dp->dl_file->fi_had_conflict = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 nfsd4_cb_recall(dp);
1287 return 0;
1288}
1289
1290/*
1291 * Spawn a thread to perform a recall on the delegation represented
1292 * by the lease (file_lock)
1293 *
1294 * Called from break_lease() with lock_kernel() held.
1295 * Note: we assume break_lease will only call this *once* for any given
1296 * lease.
1297 */
1298static
1299void nfsd_break_deleg_cb(struct file_lock *fl)
1300{
1301 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1302 struct task_struct *t;
1303
1304 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1305 if (!dp)
1306 return;
1307
1308 /* We're assuming the state code never drops its reference
1309 * without first removing the lease. Since we're in this lease
1310 * callback (and since the lease code is serialized by the kernel
1311 * lock) we know the server hasn't removed the lease yet, we know
1312 * it's safe to take a reference: */
1313 atomic_inc(&dp->dl_count);
J. Bruce Fieldscfdcad42007-09-12 20:35:15 -04001314 atomic_inc(&dp->dl_client->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 spin_lock(&recall_lock);
1317 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1318 spin_unlock(&recall_lock);
1319
1320 /* only place dl_time is set. protected by lock_kernel*/
1321 dp->dl_time = get_seconds();
1322
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04001323 /*
1324 * We don't want the locks code to timeout the lease for us;
1325 * we'll remove it ourself if the delegation isn't returned
1326 * in time.
1327 */
1328 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1331 if (IS_ERR(t)) {
1332 struct nfs4_client *clp = dp->dl_client;
1333
1334 printk(KERN_INFO "NFSD: Callback thread failed for "
1335 "for client (clientid %08x/%08x)\n",
1336 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
J. Bruce Fieldscfdcad42007-09-12 20:35:15 -04001337 put_nfs4_client(dp->dl_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 nfs4_put_delegation(dp);
1339 }
1340}
1341
1342/*
1343 * The file_lock is being reapd.
1344 *
1345 * Called by locks_free_lock() with lock_kernel() held.
1346 */
1347static
1348void nfsd_release_deleg_cb(struct file_lock *fl)
1349{
1350 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1351
1352 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1353
1354 if (!(fl->fl_flags & FL_LEASE) || !dp)
1355 return;
1356 dp->dl_flock = NULL;
1357}
1358
1359/*
1360 * Set the delegation file_lock back pointer.
1361 *
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001362 * Called from setlease() with lock_kernel() held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 */
1364static
1365void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1366{
1367 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1368
1369 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1370 if (!dp)
1371 return;
1372 dp->dl_flock = new;
1373}
1374
1375/*
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001376 * Called from setlease() with lock_kernel() held
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 */
1378static
1379int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1380{
1381 struct nfs4_delegation *onlistd =
1382 (struct nfs4_delegation *)onlist->fl_owner;
1383 struct nfs4_delegation *tryd =
1384 (struct nfs4_delegation *)try->fl_owner;
1385
1386 if (onlist->fl_lmops != try->fl_lmops)
1387 return 0;
1388
1389 return onlistd->dl_client == tryd->dl_client;
1390}
1391
1392
1393static
1394int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1395{
1396 if (arg & F_UNLCK)
1397 return lease_modify(onlist, arg);
1398 else
1399 return -EAGAIN;
1400}
1401
NeilBrownfd39ca92005-06-23 22:04:03 -07001402static struct lock_manager_operations nfsd_lease_mng_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 .fl_break = nfsd_break_deleg_cb,
1404 .fl_release_private = nfsd_release_deleg_cb,
1405 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1406 .fl_mylease = nfsd_same_client_deleg_cb,
1407 .fl_change = nfsd_change_deleg_cb,
1408};
1409
1410
Al Virob37ad282006-10-19 23:28:59 -07001411__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412nfsd4_process_open1(struct nfsd4_open *open)
1413{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 clientid_t *clientid = &open->op_clientid;
1415 struct nfs4_client *clp = NULL;
1416 unsigned int strhashval;
1417 struct nfs4_stateowner *sop = NULL;
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001420 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 if (STALE_CLIENTID(&open->op_clientid))
1423 return nfserr_stale_clientid;
1424
1425 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1426 sop = find_openstateowner_str(strhashval, open);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001427 open->op_stateowner = sop;
1428 if (!sop) {
1429 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 clp = find_confirmed_client(clientid);
1431 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001432 return nfserr_expired;
1433 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001435 if (!sop->so_confirmed) {
1436 /* Replace unconfirmed owners without checking for replay. */
1437 clp = sop->so_client;
1438 release_stateowner(sop);
1439 open->op_stateowner = NULL;
1440 goto renew;
1441 }
1442 if (open->op_seqid == sop->so_seqid - 1) {
1443 if (sop->so_replay.rp_buflen)
Al Viroa90b0612006-10-19 23:29:03 -07001444 return nfserr_replay_me;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001445 /* The original OPEN failed so spectacularly
1446 * that we don't even have replay data saved!
1447 * Therefore, we have no choice but to continue
1448 * processing this OPEN; presumably, we'll
1449 * fail again for the same reason.
1450 */
1451 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1452 goto renew;
1453 }
1454 if (open->op_seqid != sop->so_seqid)
1455 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456renew:
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001457 if (open->op_stateowner == NULL) {
1458 sop = alloc_init_open_stateowner(strhashval, clp, open);
1459 if (sop == NULL)
1460 return nfserr_resource;
1461 open->op_stateowner = sop;
1462 }
1463 list_del_init(&sop->so_close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 renew_client(sop->so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001465 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
Al Virob37ad282006-10-19 23:28:59 -07001468static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07001469nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1470{
1471 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1472 return nfserr_openmode;
1473 else
1474 return nfs_ok;
1475}
1476
NeilBrown52f4fb42005-06-23 22:02:49 -07001477static struct nfs4_delegation *
1478find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1479{
1480 struct nfs4_delegation *dp;
1481
NeilBrownea1da632005-06-23 22:04:17 -07001482 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
NeilBrown52f4fb42005-06-23 22:02:49 -07001483 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1484 return dp;
1485 }
1486 return NULL;
1487}
1488
Al Virob37ad282006-10-19 23:28:59 -07001489static __be32
NeilBrown567d9822005-06-23 22:02:53 -07001490nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1491 struct nfs4_delegation **dp)
1492{
1493 int flags;
Al Virob37ad282006-10-19 23:28:59 -07001494 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07001495
1496 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1497 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07001498 goto out;
NeilBrown567d9822005-06-23 22:02:53 -07001499 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1500 RD_STATE : WR_STATE;
1501 status = nfs4_check_delegmode(*dp, flags);
1502 if (status)
1503 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001504out:
1505 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1506 return nfs_ok;
1507 if (status)
1508 return status;
1509 open->op_stateowner->so_confirmed = 1;
1510 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07001511}
1512
Al Virob37ad282006-10-19 23:28:59 -07001513static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1515{
1516 struct nfs4_stateid *local;
Al Virob37ad282006-10-19 23:28:59 -07001517 __be32 status = nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 struct nfs4_stateowner *sop = open->op_stateowner;
1519
NeilBrown8beefa22005-06-23 22:03:08 -07001520 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /* ignore lock owners */
1522 if (local->st_stateowner->so_is_open_owner == 0)
1523 continue;
1524 /* remember if we have seen this open owner */
1525 if (local->st_stateowner == sop)
1526 *stpp = local;
1527 /* check for conflicting share reservations */
1528 if (!test_share(local, open))
1529 goto out;
1530 }
1531 status = 0;
1532out:
1533 return status;
1534}
1535
NeilBrown5ac049a2005-06-23 22:03:03 -07001536static inline struct nfs4_stateid *
1537nfs4_alloc_stateid(void)
1538{
1539 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1540}
1541
Al Virob37ad282006-10-19 23:28:59 -07001542static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
NeilBrown567d9822005-06-23 22:02:53 -07001544 struct nfs4_delegation *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct svc_fh *cur_fh, int flags)
1546{
1547 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
NeilBrown5ac049a2005-06-23 22:03:03 -07001549 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (stp == NULL)
1551 return nfserr_resource;
1552
NeilBrown567d9822005-06-23 22:02:53 -07001553 if (dp) {
1554 get_file(dp->dl_vfs_file);
1555 stp->st_vfs_file = dp->dl_vfs_file;
1556 } else {
Al Virob37ad282006-10-19 23:28:59 -07001557 __be32 status;
NeilBrown567d9822005-06-23 22:02:53 -07001558 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1559 &stp->st_vfs_file);
1560 if (status) {
1561 if (status == nfserr_dropit)
1562 status = nfserr_jukebox;
NeilBrown5ac049a2005-06-23 22:03:03 -07001563 kmem_cache_free(stateid_slab, stp);
NeilBrown567d9822005-06-23 22:02:53 -07001564 return status;
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 *stpp = stp;
1568 return 0;
1569}
1570
Al Virob37ad282006-10-19 23:28:59 -07001571static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1573 struct nfsd4_open *open)
1574{
1575 struct iattr iattr = {
1576 .ia_valid = ATTR_SIZE,
1577 .ia_size = 0,
1578 };
1579 if (!open->op_truncate)
1580 return 0;
1581 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08001582 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1584}
1585
Al Virob37ad282006-10-19 23:28:59 -07001586static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1588{
1589 struct file *filp = stp->st_vfs_file;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001590 struct inode *inode = filp->f_path.dentry->d_inode;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001591 unsigned int share_access, new_writer;
Al Virob37ad282006-10-19 23:28:59 -07001592 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 set_access(&share_access, stp->st_access_bmap);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001595 new_writer = (~share_access) & open->op_share_access
1596 & NFS4_SHARE_ACCESS_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001598 if (new_writer) {
Al Virob37ad282006-10-19 23:28:59 -07001599 int err = get_write_access(inode);
1600 if (err)
1601 return nfserrno(err);
Benny Halevye518f052008-07-04 15:38:41 +03001602 err = mnt_want_write(cur_fh->fh_export->ex_path.mnt);
1603 if (err)
1604 return nfserrno(err);
1605 file_take_write(filp);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 status = nfsd4_truncate(rqstp, cur_fh, open);
1608 if (status) {
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001609 if (new_writer)
1610 put_write_access(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return status;
1612 }
1613 /* remember the open */
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001614 filp->f_mode |= open->op_share_access;
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04001615 __set_bit(open->op_share_access, &stp->st_access_bmap);
1616 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 return nfs_ok;
1619}
1620
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622static void
NeilBrown37515172005-07-07 17:59:16 -07001623nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
NeilBrown37515172005-07-07 17:59:16 -07001625 open->op_stateowner->so_confirmed = 1;
1626 open->op_stateowner->so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
1629/*
1630 * Attempt to hand out a delegation.
1631 */
1632static void
1633nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1634{
1635 struct nfs4_delegation *dp;
1636 struct nfs4_stateowner *sop = stp->st_stateowner;
1637 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1638 struct file_lock fl, *flp = &fl;
1639 int status, flag = 0;
1640
1641 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07001642 open->op_recall = 0;
1643 switch (open->op_claim_type) {
1644 case NFS4_OPEN_CLAIM_PREVIOUS:
1645 if (!atomic_read(&cb->cb_set))
1646 open->op_recall = 1;
1647 flag = open->op_delegate_type;
1648 if (flag == NFS4_OPEN_DELEGATE_NONE)
1649 goto out;
1650 break;
1651 case NFS4_OPEN_CLAIM_NULL:
1652 /* Let's not give out any delegations till everyone's
1653 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001654 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07001655 goto out;
1656 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1657 goto out;
1658 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1659 flag = NFS4_OPEN_DELEGATE_WRITE;
1660 else
1661 flag = NFS4_OPEN_DELEGATE_READ;
1662 break;
1663 default:
1664 goto out;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1668 if (dp == NULL) {
1669 flag = NFS4_OPEN_DELEGATE_NONE;
1670 goto out;
1671 }
1672 locks_init_lock(&fl);
1673 fl.fl_lmops = &nfsd_lease_mng_ops;
1674 fl.fl_flags = FL_LEASE;
Felix Blyakher9167f502008-02-26 10:54:36 -08001675 fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 fl.fl_end = OFFSET_MAX;
1677 fl.fl_owner = (fl_owner_t)dp;
1678 fl.fl_file = stp->st_vfs_file;
1679 fl.fl_pid = current->tgid;
1680
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001681 /* vfs_setlease checks to see if delegation should be handed out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * the lock_manager callbacks fl_mylease and fl_change are used
1683 */
Felix Blyakher9167f502008-02-26 10:54:36 -08001684 if ((status = vfs_setlease(stp->st_vfs_file, fl.fl_type, &flp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
NeilBrownc9071322005-04-16 15:26:38 -07001686 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 flag = NFS4_OPEN_DELEGATE_NONE;
1688 goto out;
1689 }
1690
1691 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1692
1693 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1694 dp->dl_stateid.si_boot,
1695 dp->dl_stateid.si_stateownerid,
1696 dp->dl_stateid.si_fileid,
1697 dp->dl_stateid.si_generation);
1698out:
NeilBrown7b190fe2005-06-23 22:03:23 -07001699 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1700 && flag == NFS4_OPEN_DELEGATE_NONE
1701 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001702 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 open->op_delegate_type = flag;
1704}
1705
1706/*
1707 * called with nfs4_lock_state() held.
1708 */
Al Virob37ad282006-10-19 23:28:59 -07001709__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1711{
1712 struct nfs4_file *fp = NULL;
1713 struct inode *ino = current_fh->fh_dentry->d_inode;
1714 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07001715 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07001716 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 status = nfserr_inval;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001719 if (!access_valid(open->op_share_access)
1720 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 goto out;
1722 /*
1723 * Lookup file; if found, lookup stateid and check open request,
1724 * and check for delegations in the process of being recalled.
1725 * If not found, create the nfs4_file struct
1726 */
1727 fp = find_file(ino);
1728 if (fp) {
1729 if ((status = nfs4_check_open(fp, open, &stp)))
1730 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001731 status = nfs4_check_deleg(fp, open, &dp);
1732 if (status)
1733 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07001735 status = nfserr_bad_stateid;
1736 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1737 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 status = nfserr_resource;
1739 fp = alloc_init_file(ino);
1740 if (fp == NULL)
1741 goto out;
1742 }
1743
1744 /*
1745 * OPEN the file, or upgrade an existing OPEN.
1746 * If truncate fails, the OPEN fails.
1747 */
1748 if (stp) {
1749 /* Stateid was found, this is an OPEN upgrade */
1750 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1751 if (status)
1752 goto out;
NeilBrown444c2c02005-07-07 17:59:22 -07001753 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 } else {
1755 /* Stateid was not found, this is a new OPEN */
1756 int flags = 0;
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -07001757 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001758 flags |= NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001760 flags |= NFSD_MAY_WRITE;
NeilBrown567d9822005-06-23 22:02:53 -07001761 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1762 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto out;
1764 init_stateid(stp, fp, open);
1765 status = nfsd4_truncate(rqstp, current_fh, open);
1766 if (status) {
1767 release_stateid(stp, OPEN_STATE);
1768 goto out;
1769 }
1770 }
1771 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1772
1773 /*
1774 * Attempt to hand out a delegation. No error return, because the
1775 * OPEN succeeds even if we fail.
1776 */
1777 nfs4_open_delegation(current_fh, open, stp);
1778
1779 status = nfs_ok;
1780
1781 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1782 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1783 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1784out:
NeilBrown13cd2182005-06-23 22:03:10 -07001785 if (fp)
1786 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07001787 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1788 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 /*
1790 * To finish the open response, we just need to set the rflags.
1791 */
1792 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1793 if (!open->op_stateowner->so_confirmed)
1794 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1795
1796 return status;
1797}
1798
Al Virob37ad282006-10-19 23:28:59 -07001799__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001800nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1801 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07001804 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 nfs4_lock_state();
1807 dprintk("process_renew(%08x/%08x): starting\n",
1808 clid->cl_boot, clid->cl_id);
1809 status = nfserr_stale_clientid;
1810 if (STALE_CLIENTID(clid))
1811 goto out;
1812 clp = find_confirmed_client(clid);
1813 status = nfserr_expired;
1814 if (clp == NULL) {
1815 /* We assume the client took too long to RENEW. */
1816 dprintk("nfsd4_renew: clientid not found!\n");
1817 goto out;
1818 }
1819 renew_client(clp);
1820 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07001821 if (!list_empty(&clp->cl_delegations)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 && !atomic_read(&clp->cl_callback.cb_set))
1823 goto out;
1824 status = nfs_ok;
1825out:
1826 nfs4_unlock_state();
1827 return status;
1828}
1829
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001830struct lock_manager nfsd4_manager = {
1831};
1832
NeilBrowna76b4312005-06-23 22:04:01 -07001833static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001834nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07001835{
1836 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07001837 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001838 locks_end_grace(&nfsd4_manager);
NeilBrowna76b4312005-06-23 22:04:01 -07001839}
1840
NeilBrownfd39ca92005-06-23 22:04:03 -07001841static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842nfs4_laundromat(void)
1843{
1844 struct nfs4_client *clp;
1845 struct nfs4_stateowner *sop;
1846 struct nfs4_delegation *dp;
1847 struct list_head *pos, *next, reaplist;
1848 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1849 time_t t, clientid_val = NFSD_LEASE_TIME;
1850 time_t u, test_val = NFSD_LEASE_TIME;
1851
1852 nfs4_lock_state();
1853
1854 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001855 if (locks_in_grace())
1856 nfsd4_end_grace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 list_for_each_safe(pos, next, &client_lru) {
1858 clp = list_entry(pos, struct nfs4_client, cl_lru);
1859 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1860 t = clp->cl_time - cutoff;
1861 if (clientid_val > t)
1862 clientid_val = t;
1863 break;
1864 }
1865 dprintk("NFSD: purging unused client (clientid %08x)\n",
1866 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07001867 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 expire_client(clp);
1869 }
1870 INIT_LIST_HEAD(&reaplist);
1871 spin_lock(&recall_lock);
1872 list_for_each_safe(pos, next, &del_recall_lru) {
1873 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1874 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1875 u = dp->dl_time - cutoff;
1876 if (test_val > u)
1877 test_val = u;
1878 break;
1879 }
1880 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1881 dp, dp->dl_flock);
1882 list_move(&dp->dl_recall_lru, &reaplist);
1883 }
1884 spin_unlock(&recall_lock);
1885 list_for_each_safe(pos, next, &reaplist) {
1886 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1887 list_del_init(&dp->dl_recall_lru);
1888 unhash_delegation(dp);
1889 }
1890 test_val = NFSD_LEASE_TIME;
1891 list_for_each_safe(pos, next, &close_lru) {
1892 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1893 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1894 u = sop->so_time - cutoff;
1895 if (test_val > u)
1896 test_val = u;
1897 break;
1898 }
1899 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1900 sop->so_id);
NeilBrown358dd552006-04-10 22:55:42 -07001901 release_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1904 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1905 nfs4_unlock_state();
1906 return clientid_val;
1907}
1908
Harvey Harrisona254b242008-02-20 12:49:00 -08001909static struct workqueue_struct *laundry_wq;
1910static void laundromat_main(struct work_struct *);
1911static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
1912
1913static void
David Howellsc4028952006-11-22 14:57:56 +00001914laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 time_t t;
1917
1918 t = nfs4_laundromat();
1919 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07001920 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
NeilBrownfd39ca92005-06-23 22:04:03 -07001923static struct nfs4_stateowner *
NeilBrownf8816512005-07-07 17:59:25 -07001924search_close_lru(u32 st_id, int flags)
1925{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 struct nfs4_stateowner *local = NULL;
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (flags & CLOSE_STATE) {
1929 list_for_each_entry(local, &close_lru, so_close_lru) {
1930 if (local->so_id == st_id)
1931 return local;
1932 }
1933 }
1934 return NULL;
1935}
1936
1937static inline int
1938nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1939{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001940 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
1943static int
1944STALE_STATEID(stateid_t *stateid)
1945{
1946 if (stateid->si_boot == boot_time)
1947 return 0;
Neil Brown849823c2005-09-13 01:25:36 -07001948 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1950 stateid->si_generation);
1951 return 1;
1952}
1953
1954static inline int
1955access_permit_read(unsigned long access_bmap)
1956{
1957 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1958 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1959 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1960}
1961
1962static inline int
1963access_permit_write(unsigned long access_bmap)
1964{
1965 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1966 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
1967}
1968
1969static
Al Virob37ad282006-10-19 23:28:59 -07001970__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Al Virob37ad282006-10-19 23:28:59 -07001972 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
1975 goto out;
1976 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
1977 goto out;
1978 status = nfs_ok;
1979out:
1980 return status;
1981}
1982
Al Virob37ad282006-10-19 23:28:59 -07001983static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
1985{
1986 /* Trying to call delegreturn with a special stateid? Yuch: */
1987 if (!(flags & (RD_STATE | WR_STATE)))
1988 return nfserr_bad_stateid;
1989 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
1990 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001991 else if (locks_in_grace()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 /* Answer in remaining cases depends on existance of
1993 * conflicting state; so we must wait out the grace period. */
1994 return nfserr_grace;
1995 } else if (flags & WR_STATE)
1996 return nfs4_share_conflict(current_fh,
1997 NFS4_SHARE_DENY_WRITE);
1998 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
1999 return nfs4_share_conflict(current_fh,
2000 NFS4_SHARE_DENY_READ);
2001}
2002
2003/*
2004 * Allow READ/WRITE during grace period on recovered state only for files
2005 * that are not able to provide mandatory locking.
2006 */
2007static inline int
2008io_during_grace_disallowed(struct inode *inode, int flags)
2009{
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002010 return locks_in_grace() && (flags & (RD_STATE | WR_STATE))
Pavel Emelyanova16877c2007-10-01 14:41:11 -07002011 && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
J. Bruce Fields0836f582008-01-26 19:08:12 -05002014static int check_stateid_generation(stateid_t *in, stateid_t *ref)
2015{
2016 /* If the client sends us a stateid from the future, it's buggy: */
2017 if (in->si_generation > ref->si_generation)
2018 return nfserr_bad_stateid;
2019 /*
2020 * The following, however, can happen. For example, if the
2021 * client sends an open and some IO at the same time, the open
2022 * may bump si_generation while the IO is still in flight.
2023 * Thanks to hard links and renames, the client never knows what
2024 * file an open will affect. So it could avoid that situation
2025 * only by serializing all opens and IO from the same open
2026 * owner. To recover from the old_stateid error, the client
2027 * will just have to retry the IO:
2028 */
2029 if (in->si_generation < ref->si_generation)
2030 return nfserr_old_stateid;
2031 return nfs_ok;
2032}
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034/*
2035* Checks for stateid operations
2036*/
Al Virob37ad282006-10-19 23:28:59 -07002037__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2039{
2040 struct nfs4_stateid *stp = NULL;
2041 struct nfs4_delegation *dp = NULL;
2042 stateid_t *stidp;
2043 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07002044 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2047 stateid->si_boot, stateid->si_stateownerid,
2048 stateid->si_fileid, stateid->si_generation);
2049 if (filpp)
2050 *filpp = NULL;
2051
2052 if (io_during_grace_disallowed(ino, flags))
2053 return nfserr_grace;
2054
2055 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2056 return check_special_stateids(current_fh, stateid, flags);
2057
2058 /* STALE STATEID */
2059 status = nfserr_stale_stateid;
2060 if (STALE_STATEID(stateid))
2061 goto out;
2062
2063 /* BAD STATEID */
2064 status = nfserr_bad_stateid;
2065 if (!stateid->si_fileid) { /* delegation stateid */
2066 if(!(dp = find_delegation_stateid(ino, stateid))) {
2067 dprintk("NFSD: delegation stateid not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 goto out;
2069 }
2070 stidp = &dp->dl_stateid;
2071 } else { /* open or lock stateid */
2072 if (!(stp = find_stateid(stateid, flags))) {
2073 dprintk("NFSD: open or lock stateid not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 goto out;
2075 }
2076 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2077 goto out;
2078 if (!stp->st_stateowner->so_confirmed)
2079 goto out;
2080 stidp = &stp->st_stateid;
2081 }
J. Bruce Fields0836f582008-01-26 19:08:12 -05002082 status = check_stateid_generation(stateid, stidp);
2083 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 goto out;
2085 if (stp) {
2086 if ((status = nfs4_check_openmode(stp,flags)))
2087 goto out;
2088 renew_client(stp->st_stateowner->so_client);
2089 if (filpp)
2090 *filpp = stp->st_vfs_file;
J. Bruce Fields6a85fa32008-01-26 23:36:48 -05002091 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if ((status = nfs4_check_delegmode(dp, flags)))
2093 goto out;
2094 renew_client(dp->dl_client);
2095 if (flags & DELEG_RET)
2096 unhash_delegation(dp);
2097 if (filpp)
2098 *filpp = dp->dl_vfs_file;
2099 }
2100 status = nfs_ok;
2101out:
2102 return status;
2103}
2104
NeilBrown4c4cd222005-07-07 17:59:27 -07002105static inline int
2106setlkflg (int type)
2107{
2108 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2109 RD_STATE : WR_STATE;
2110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112/*
2113 * Checks for sequence id mutating operations.
2114 */
Al Virob37ad282006-10-19 23:28:59 -07002115static __be32
NeilBrown4c4cd222005-07-07 17:59:27 -07002116nfs4_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 -07002117{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 struct nfs4_stateid *stp;
2119 struct nfs4_stateowner *sop;
J. Bruce Fields0836f582008-01-26 19:08:12 -05002120 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2123 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2124 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2125 stateid->si_generation);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 *stpp = NULL;
2128 *sopp = NULL;
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002131 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002132 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (STALE_STATEID(stateid))
NeilBrown3a4f98b2005-07-07 17:59:26 -07002136 return nfserr_stale_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /*
2138 * We return BAD_STATEID if filehandle doesn't match stateid,
2139 * the confirmed flag is incorrecly set, or the generation
2140 * number is incorrect.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
NeilBrownf8816512005-07-07 17:59:25 -07002142 stp = find_stateid(stateid, flags);
2143 if (stp == NULL) {
2144 /*
2145 * Also, we should make sure this isn't just the result of
2146 * a replayed close:
2147 */
2148 sop = search_close_lru(stateid->si_stateownerid, flags);
2149 if (sop == NULL)
2150 return nfserr_bad_stateid;
2151 *sopp = sop;
2152 goto check_replay;
2153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
J. Bruce Fields39325bd2007-11-26 17:06:39 -05002155 *stpp = stp;
2156 *sopp = sop = stp->st_stateowner;
2157
NeilBrown4c4cd222005-07-07 17:59:27 -07002158 if (lock) {
NeilBrown4c4cd222005-07-07 17:59:27 -07002159 clientid_t *lockclid = &lock->v.new.clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 struct nfs4_client *clp = sop->so_client;
NeilBrown4c4cd222005-07-07 17:59:27 -07002161 int lkflg = 0;
Al Virob37ad282006-10-19 23:28:59 -07002162 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
NeilBrown4c4cd222005-07-07 17:59:27 -07002164 lkflg = setlkflg(lock->lk_type);
2165
2166 if (lock->lk_is_new) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002167 if (!sop->so_is_open_owner)
2168 return nfserr_bad_stateid;
2169 if (!same_clid(&clp->cl_clientid, lockclid))
NeilBrown4c4cd222005-07-07 17:59:27 -07002170 return nfserr_bad_stateid;
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002171 /* stp is the open stateid */
2172 status = nfs4_check_openmode(stp, lkflg);
2173 if (status)
2174 return status;
2175 } else {
2176 /* stp is the lock stateid */
2177 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2178 if (status)
2179 return status;
NeilBrown4c4cd222005-07-07 17:59:27 -07002180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 }
2182
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002183 if (nfs4_check_fh(current_fh, stp)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002184 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002185 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 /*
2189 * We now validate the seqid and stateid generation numbers.
2190 * For the moment, we ignore the possibility of
2191 * generation number wraparound.
2192 */
NeilBrownbd9aac52005-07-07 17:59:19 -07002193 if (seqid != sop->so_seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 goto check_replay;
2195
NeilBrown3a4f98b2005-07-07 17:59:26 -07002196 if (sop->so_confirmed && flags & CONFIRM) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002197 dprintk("NFSD: preprocess_seqid_op: expected"
NeilBrown3a4f98b2005-07-07 17:59:26 -07002198 " unconfirmed stateowner!\n");
2199 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07002201 if (!sop->so_confirmed && !(flags & CONFIRM)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002202 dprintk("NFSD: preprocess_seqid_op: stateowner not"
NeilBrown3a4f98b2005-07-07 17:59:26 -07002203 " confirmed yet!\n");
2204 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
J. Bruce Fields0836f582008-01-26 19:08:12 -05002206 status = check_stateid_generation(stateid, &stp->st_stateid);
2207 if (status)
2208 return status;
NeilBrown52fd0042005-07-07 17:59:24 -07002209 renew_client(sop->so_client);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002210 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212check_replay:
NeilBrownbd9aac52005-07-07 17:59:19 -07002213 if (seqid == sop->so_seqid - 1) {
Neil Brown849823c2005-09-13 01:25:36 -07002214 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /* indicate replay to calling function */
Al Viroa90b0612006-10-19 23:29:03 -07002216 return nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002218 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
NeilBrown3a4f98b2005-07-07 17:59:26 -07002219 sop->so_seqid, seqid);
2220 *sopp = NULL;
2221 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
Al Virob37ad282006-10-19 23:28:59 -07002224__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002225nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002226 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Al Virob37ad282006-10-19 23:28:59 -07002228 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 struct nfs4_stateowner *sop;
2230 struct nfs4_stateid *stp;
2231
2232 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002233 (int)cstate->current_fh.fh_dentry->d_name.len,
2234 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002236 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07002237 if (status)
2238 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 nfs4_lock_state();
2241
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002242 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2243 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002244 CONFIRM | OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 &oc->oc_stateowner, &stp, NULL)))
2246 goto out;
2247
2248 sop = oc->oc_stateowner;
2249 sop->so_confirmed = 1;
2250 update_stateid(&stp->st_stateid);
2251 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2252 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2253 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2254 stp->st_stateid.si_boot,
2255 stp->st_stateid.si_stateownerid,
2256 stp->st_stateid.si_fileid,
2257 stp->st_stateid.si_generation);
NeilBrownc7b9a452005-06-23 22:04:30 -07002258
2259 nfsd4_create_clid_dir(sop->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260out:
Neil Brownf2327d92005-09-13 01:25:37 -07002261 if (oc->oc_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 nfs4_get_stateowner(oc->oc_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002263 cstate->replay_owner = oc->oc_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 nfs4_unlock_state();
2266 return status;
2267}
2268
2269
2270/*
2271 * unset all bits in union bitmap (bmap) that
2272 * do not exist in share (from successful OPEN_DOWNGRADE)
2273 */
2274static void
2275reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2276{
2277 int i;
2278 for (i = 1; i < 4; i++) {
2279 if ((i & access) != i)
2280 __clear_bit(i, bmap);
2281 }
2282}
2283
2284static void
2285reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2286{
2287 int i;
2288 for (i = 0; i < 4; i++) {
2289 if ((i & deny) != i)
2290 __clear_bit(i, bmap);
2291 }
2292}
2293
Al Virob37ad282006-10-19 23:28:59 -07002294__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002295nfsd4_open_downgrade(struct svc_rqst *rqstp,
2296 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002297 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Al Virob37ad282006-10-19 23:28:59 -07002299 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 struct nfs4_stateid *stp;
2301 unsigned int share_access;
2302
2303 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002304 (int)cstate->current_fh.fh_dentry->d_name.len,
2305 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002307 if (!access_valid(od->od_share_access)
2308 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return nfserr_inval;
2310
2311 nfs4_lock_state();
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002312 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2313 od->od_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 &od->od_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002315 OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 &od->od_stateowner, &stp, NULL)))
2317 goto out;
2318
2319 status = nfserr_inval;
2320 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2321 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2322 stp->st_access_bmap, od->od_share_access);
2323 goto out;
2324 }
2325 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2326 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2327 stp->st_deny_bmap, od->od_share_deny);
2328 goto out;
2329 }
2330 set_access(&share_access, stp->st_access_bmap);
2331 nfs4_file_downgrade(stp->st_vfs_file,
2332 share_access & ~od->od_share_access);
2333
2334 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2335 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2336
2337 update_stateid(&stp->st_stateid);
2338 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2339 status = nfs_ok;
2340out:
Neil Brownf2327d92005-09-13 01:25:37 -07002341 if (od->od_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 nfs4_get_stateowner(od->od_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002343 cstate->replay_owner = od->od_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 nfs4_unlock_state();
2346 return status;
2347}
2348
2349/*
2350 * nfs4_unlock_state() called after encode
2351 */
Al Virob37ad282006-10-19 23:28:59 -07002352__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002353nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002354 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Al Virob37ad282006-10-19 23:28:59 -07002356 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct nfs4_stateid *stp;
2358
2359 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002360 (int)cstate->current_fh.fh_dentry->d_name.len,
2361 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 nfs4_lock_state();
2364 /* check close_lru for replay */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002365 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2366 close->cl_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 &close->cl_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002368 OPEN_STATE | CLOSE_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 &close->cl_stateowner, &stp, NULL)))
2370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 status = nfs_ok;
2372 update_stateid(&stp->st_stateid);
2373 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2374
J. Bruce Fields04ef5952006-01-18 17:43:21 -08002375 /* release_stateid() calls nfsd_close() if needed */
2376 release_stateid(stp, OPEN_STATE);
2377
2378 /* place unused nfs4_stateowners on so_close_lru list to be
2379 * released by the laundromat service after the lease period
2380 * to enable us to handle CLOSE replay
2381 */
2382 if (list_empty(&close->cl_stateowner->so_stateids))
2383 move_to_close_lru(close->cl_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384out:
Neil Brownf2327d92005-09-13 01:25:37 -07002385 if (close->cl_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 nfs4_get_stateowner(close->cl_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002387 cstate->replay_owner = close->cl_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 nfs4_unlock_state();
2390 return status;
2391}
2392
Al Virob37ad282006-10-19 23:28:59 -07002393__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002394nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2395 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Al Virob37ad282006-10-19 23:28:59 -07002397 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002399 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 goto out;
2401
2402 nfs4_lock_state();
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002403 status = nfs4_preprocess_stateid_op(&cstate->current_fh,
2404 &dr->dr_stateid, DELEG_RET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 nfs4_unlock_state();
2406out:
2407 return status;
2408}
2409
2410
2411/*
2412 * Lock owner state (byte-range locks)
2413 */
2414#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2415#define LOCK_HASH_BITS 8
2416#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2417#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2418
Benny Halevy87df4de2008-12-15 19:42:03 +02002419static inline u64
2420end_offset(u64 start, u64 len)
2421{
2422 u64 end;
2423
2424 end = start + len;
2425 return end >= start ? end: NFS4_MAX_UINT64;
2426}
2427
2428/* last octet in a range */
2429static inline u64
2430last_byte_offset(u64 start, u64 len)
2431{
2432 u64 end;
2433
2434 BUG_ON(!len);
2435 end = start + len;
2436 return end > start ? end - 1: NFS4_MAX_UINT64;
2437}
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439#define lockownerid_hashval(id) \
2440 ((id) & LOCK_HASH_MASK)
2441
2442static inline unsigned int
2443lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2444 struct xdr_netobj *ownername)
2445{
2446 return (file_hashval(inode) + cl_id
2447 + opaque_hashval(ownername->data, ownername->len))
2448 & LOCK_HASH_MASK;
2449}
2450
2451static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2452static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2453static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2454
NeilBrownfd39ca92005-06-23 22:04:03 -07002455static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456find_stateid(stateid_t *stid, int flags)
2457{
Krishna Kumar9346eff2008-10-20 11:44:28 +05302458 struct nfs4_stateid *local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 u32 st_id = stid->si_stateownerid;
2460 u32 f_id = stid->si_fileid;
2461 unsigned int hashval;
2462
2463 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
Krishna Kumar9346eff2008-10-20 11:44:28 +05302464 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 hashval = stateid_hashval(st_id, f_id);
2466 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2467 if ((local->st_stateid.si_stateownerid == st_id) &&
2468 (local->st_stateid.si_fileid == f_id))
2469 return local;
2470 }
2471 }
Krishna Kumar9346eff2008-10-20 11:44:28 +05302472
2473 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 hashval = stateid_hashval(st_id, f_id);
2475 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2476 if ((local->st_stateid.si_stateownerid == st_id) &&
2477 (local->st_stateid.si_fileid == f_id))
2478 return local;
2479 }
Neil Brown849823c2005-09-13 01:25:36 -07002480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return NULL;
2482}
2483
2484static struct nfs4_delegation *
2485find_delegation_stateid(struct inode *ino, stateid_t *stid)
2486{
NeilBrown13cd2182005-06-23 22:03:10 -07002487 struct nfs4_file *fp;
2488 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2491 stid->si_boot, stid->si_stateownerid,
2492 stid->si_fileid, stid->si_generation);
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002495 if (!fp)
2496 return NULL;
2497 dl = find_delegation_file(fp, stid);
2498 put_nfs4_file(fp);
2499 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
2502/*
2503 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2504 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2505 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2506 * locking, this prevents us from being completely protocol-compliant. The
2507 * real solution to this problem is to start using unsigned file offsets in
2508 * the VFS, but this is a very deep change!
2509 */
2510static inline void
2511nfs4_transform_lock_offset(struct file_lock *lock)
2512{
2513 if (lock->fl_start < 0)
2514 lock->fl_start = OFFSET_MAX;
2515 if (lock->fl_end < 0)
2516 lock->fl_end = OFFSET_MAX;
2517}
2518
NeilBrownd5b90262006-04-10 22:55:22 -07002519/* Hack!: For now, we're defining this just so we can use a pointer to it
2520 * as a unique cookie to identify our (NFSv4's) posix locks. */
Adrian Bunke465a772006-04-10 22:55:23 -07002521static struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07002522};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524static inline void
2525nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2526{
NeilBrownd5b90262006-04-10 22:55:22 -07002527 struct nfs4_stateowner *sop;
2528 unsigned int hval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
NeilBrownd5b90262006-04-10 22:55:22 -07002530 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2531 sop = (struct nfs4_stateowner *) fl->fl_owner;
2532 hval = lockownerid_hashval(sop->so_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 kref_get(&sop->so_ref);
2534 deny->ld_sop = sop;
2535 deny->ld_clientid = sop->so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07002536 } else {
2537 deny->ld_sop = NULL;
2538 deny->ld_clientid.cl_boot = 0;
2539 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
2541 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02002542 deny->ld_length = NFS4_MAX_UINT64;
2543 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2545 deny->ld_type = NFS4_READ_LT;
2546 if (fl->fl_type != F_RDLCK)
2547 deny->ld_type = NFS4_WRITE_LT;
2548}
2549
2550static struct nfs4_stateowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2552 struct xdr_netobj *owner)
2553{
2554 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2555 struct nfs4_stateowner *op;
2556
2557 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002558 if (same_owner_str(op, owner, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return op;
2560 }
2561 return NULL;
2562}
2563
2564/*
2565 * Alloc a lock owner structure.
2566 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2567 * occured.
2568 *
2569 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 */
2571
2572static struct nfs4_stateowner *
2573alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2574 struct nfs4_stateowner *sop;
2575 struct nfs4_replay *rp;
2576 unsigned int idhashval;
2577
2578 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2579 return NULL;
2580 idhashval = lockownerid_hashval(current_ownerid);
2581 INIT_LIST_HEAD(&sop->so_idhash);
2582 INIT_LIST_HEAD(&sop->so_strhash);
2583 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07002584 INIT_LIST_HEAD(&sop->so_stateids);
2585 INIT_LIST_HEAD(&sop->so_perstateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2587 sop->so_time = 0;
2588 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2589 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07002590 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 sop->so_is_open_owner = 0;
2592 sop->so_id = current_ownerid++;
2593 sop->so_client = clp;
Neil Brownb59e3c02005-09-13 01:25:38 -07002594 /* It is the openowner seqid that will be incremented in encode in the
2595 * case of new lockowners; so increment the lock seqid manually: */
2596 sop->so_seqid = lock->lk_new_lock_seqid + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 sop->so_confirmed = 1;
2598 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08002599 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 rp->rp_buflen = 0;
2601 rp->rp_buf = rp->rp_ibuf;
2602 return sop;
2603}
2604
NeilBrownfd39ca92005-06-23 22:04:03 -07002605static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2607{
2608 struct nfs4_stateid *stp;
2609 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2610
NeilBrown5ac049a2005-06-23 22:03:03 -07002611 stp = nfs4_alloc_stateid();
2612 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 goto out;
2614 INIT_LIST_HEAD(&stp->st_hash);
2615 INIT_LIST_HEAD(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07002616 INIT_LIST_HEAD(&stp->st_perstateowner);
2617 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07002619 list_add(&stp->st_perfile, &fp->fi_stateids);
NeilBrownea1da632005-06-23 22:04:17 -07002620 list_add(&stp->st_perstateowner, &sop->so_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07002622 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 stp->st_file = fp;
2624 stp->st_stateid.si_boot = boot_time;
2625 stp->st_stateid.si_stateownerid = sop->so_id;
2626 stp->st_stateid.si_fileid = fp->fi_id;
2627 stp->st_stateid.si_generation = 0;
2628 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2629 stp->st_access_bmap = open_stp->st_access_bmap;
2630 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07002631 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633out:
2634 return stp;
2635}
2636
NeilBrownfd39ca92005-06-23 22:04:03 -07002637static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638check_lock_length(u64 offset, u64 length)
2639{
Benny Halevy87df4de2008-12-15 19:42:03 +02002640 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 LOFF_OVERFLOW(offset, length)));
2642}
2643
2644/*
2645 * LOCK operation
2646 */
Al Virob37ad282006-10-19 23:28:59 -07002647__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002648nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002649 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
NeilBrown3e9e3db2005-06-23 22:04:20 -07002651 struct nfs4_stateowner *open_sop = NULL;
Neil Brownb59e3c02005-09-13 01:25:38 -07002652 struct nfs4_stateowner *lock_sop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 struct nfs4_stateid *lock_stp;
2654 struct file *filp;
2655 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05002656 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07002657 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 unsigned int strhashval;
Marc Eshel150b3932007-01-18 16:15:35 -05002659 unsigned int cmd;
Al Virob8dd7b92006-10-19 23:29:01 -07002660 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2663 (long long) lock->lk_offset,
2664 (long long) lock->lk_length);
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (check_lock_length(lock->lk_offset, lock->lk_length))
2667 return nfserr_inval;
2668
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002669 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002670 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08002671 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2672 return status;
2673 }
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 nfs4_lock_state();
2676
2677 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07002678 /*
2679 * Client indicates that this is a new lockowner.
2680 * Use open owner and open stateid to create lock owner and
2681 * lock stateid.
2682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 struct nfs4_stateid *open_stp = NULL;
2684 struct nfs4_file *fp;
2685
2686 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002687 if (STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 /* validate and update open stateid and open seqid */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002691 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 lock->lk_new_open_seqid,
2693 &lock->lk_new_open_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002694 OPEN_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002695 &lock->lk_replay_owner, &open_stp,
Neil Brownb59e3c02005-09-13 01:25:38 -07002696 lock);
NeilBrown37515172005-07-07 17:59:16 -07002697 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002699 open_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /* create lockowner and lock stateid */
2701 fp = open_stp->st_file;
2702 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2703 open_sop->so_client->cl_clientid.cl_id,
2704 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07002705 /* XXX: Do we need to check for duplicate stateowners on
2706 * the same file, or should they just be allowed (and
2707 * create new stateids)? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 status = nfserr_resource;
Neil Brownb59e3c02005-09-13 01:25:38 -07002709 lock_sop = alloc_init_lock_stateowner(strhashval,
2710 open_sop->so_client, open_stp, lock);
2711 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07002713 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08002714 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 } else {
2717 /* lock (lock owner + lock stateid) already exists */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002718 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 lock->lk_old_lock_seqid,
2720 &lock->lk_old_lock_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002721 LOCK_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002722 &lock->lk_replay_owner, &lock_stp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (status)
2724 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002725 lock_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 }
J. Bruce Fields3a655882006-01-18 17:43:19 -08002727 /* lock->lk_replay_owner and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 filp = lock_stp->st_vfs_file;
2729
NeilBrown0dd395d2005-07-07 17:59:15 -07002730 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002731 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07002732 goto out;
2733 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002734 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07002735 goto out;
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 locks_init_lock(&file_lock);
2738 switch (lock->lk_type) {
2739 case NFS4_READ_LT:
2740 case NFS4_READW_LT:
2741 file_lock.fl_type = F_RDLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05002742 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 break;
2744 case NFS4_WRITE_LT:
2745 case NFS4_WRITEW_LT:
2746 file_lock.fl_type = F_WRLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05002747 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 break;
2749 default:
2750 status = nfserr_inval;
2751 goto out;
2752 }
Neil Brownb59e3c02005-09-13 01:25:38 -07002753 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 file_lock.fl_pid = current->tgid;
2755 file_lock.fl_file = filp;
2756 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002757 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02002760 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 nfs4_transform_lock_offset(&file_lock);
2762
2763 /*
2764 * Try to lock the file in the VFS.
2765 * Note: locks.c uses the BKL to protect the inode's lock list.
2766 */
2767
Marc Eshelfd85b812006-11-28 16:26:41 -05002768 err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07002769 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 case 0: /* success! */
2771 update_stateid(&lock_stp->st_stateid);
2772 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2773 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07002774 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002775 break;
2776 case (EAGAIN): /* conflock holds conflicting lock */
2777 status = nfserr_denied;
2778 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2779 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2780 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 case (EDEADLK):
2782 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002783 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05002785 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002786 status = nfserr_resource;
2787 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08002790 if (status && lock->lk_is_new && lock_sop)
2791 release_stateowner(lock_sop);
J. Bruce Fields3a655882006-01-18 17:43:19 -08002792 if (lock->lk_replay_owner) {
2793 nfs4_get_stateowner(lock->lk_replay_owner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002794 cstate->replay_owner = lock->lk_replay_owner;
Neil Brownf2327d92005-09-13 01:25:37 -07002795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 nfs4_unlock_state();
2797 return status;
2798}
2799
2800/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08002801 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
2802 * so we do a temporary open here just to get an open file to pass to
2803 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
2804 * inode operation.)
2805 */
2806static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
2807{
2808 struct file *file;
2809 int err;
2810
2811 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
2812 if (err)
2813 return err;
2814 err = vfs_test_lock(file, lock);
2815 nfsd_close(file);
2816 return err;
2817}
2818
2819/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 * LOCKT operation
2821 */
Al Virob37ad282006-10-19 23:28:59 -07002822__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002823nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2824 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
2826 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 struct file_lock file_lock;
Marc Eshelfd85b812006-11-28 16:26:41 -05002828 int error;
Al Virob37ad282006-10-19 23:28:59 -07002829 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002831 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return nfserr_grace;
2833
2834 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2835 return nfserr_inval;
2836
2837 lockt->lt_stateowner = NULL;
2838 nfs4_lock_state();
2839
2840 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002841 if (STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002844 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
Neil Brown849823c2005-09-13 01:25:36 -07002845 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (status == nfserr_symlink)
2847 status = nfserr_inval;
2848 goto out;
2849 }
2850
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002851 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 locks_init_lock(&file_lock);
2853 switch (lockt->lt_type) {
2854 case NFS4_READ_LT:
2855 case NFS4_READW_LT:
2856 file_lock.fl_type = F_RDLCK;
2857 break;
2858 case NFS4_WRITE_LT:
2859 case NFS4_WRITEW_LT:
2860 file_lock.fl_type = F_WRLCK;
2861 break;
2862 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002863 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 status = nfserr_inval;
2865 goto out;
2866 }
2867
2868 lockt->lt_stateowner = find_lockstateowner_str(inode,
2869 &lockt->lt_clientid, &lockt->lt_owner);
2870 if (lockt->lt_stateowner)
2871 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2872 file_lock.fl_pid = current->tgid;
2873 file_lock.fl_flags = FL_POSIX;
2874
2875 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02002876 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 nfs4_transform_lock_offset(&file_lock);
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08002881 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05002882 if (error) {
2883 status = nfserrno(error);
2884 goto out;
2885 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002886 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002888 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890out:
2891 nfs4_unlock_state();
2892 return status;
2893}
2894
Al Virob37ad282006-10-19 23:28:59 -07002895__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002896nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002897 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
2899 struct nfs4_stateid *stp;
2900 struct file *filp = NULL;
2901 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07002902 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002903 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2906 (long long) locku->lu_offset,
2907 (long long) locku->lu_length);
2908
2909 if (check_lock_length(locku->lu_offset, locku->lu_length))
2910 return nfserr_inval;
2911
2912 nfs4_lock_state();
2913
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002914 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 locku->lu_seqid,
2916 &locku->lu_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002917 LOCK_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 &locku->lu_stateowner, &stp, NULL)))
2919 goto out;
2920
2921 filp = stp->st_vfs_file;
2922 BUG_ON(!filp);
2923 locks_init_lock(&file_lock);
2924 file_lock.fl_type = F_UNLCK;
2925 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2926 file_lock.fl_pid = current->tgid;
2927 file_lock.fl_file = filp;
2928 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002929 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 file_lock.fl_start = locku->lu_offset;
2931
Benny Halevy87df4de2008-12-15 19:42:03 +02002932 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 nfs4_transform_lock_offset(&file_lock);
2934
2935 /*
2936 * Try to unlock the file in the VFS.
2937 */
Marc Eshelfd85b812006-11-28 16:26:41 -05002938 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07002939 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05002940 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 goto out_nfserr;
2942 }
2943 /*
2944 * OK, unlock succeeded; the only thing left to do is update the stateid.
2945 */
2946 update_stateid(&stp->st_stateid);
2947 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2948
2949out:
Neil Brownf2327d92005-09-13 01:25:37 -07002950 if (locku->lu_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 nfs4_get_stateowner(locku->lu_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002952 cstate->replay_owner = locku->lu_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 nfs4_unlock_state();
2955 return status;
2956
2957out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002958 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 goto out;
2960}
2961
2962/*
2963 * returns
2964 * 1: locks held by lockowner
2965 * 0: no locks held by lockowner
2966 */
2967static int
2968check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2969{
2970 struct file_lock **flpp;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08002971 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 int status = 0;
2973
2974 lock_kernel();
2975 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08002976 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 status = 1;
2978 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08002979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
2981out:
2982 unlock_kernel();
2983 return status;
2984}
2985
Al Virob37ad282006-10-19 23:28:59 -07002986__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002987nfsd4_release_lockowner(struct svc_rqst *rqstp,
2988 struct nfsd4_compound_state *cstate,
2989 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
2991 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07002992 struct nfs4_stateowner *sop;
2993 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07002995 struct list_head matches;
2996 int i;
Al Virob37ad282006-10-19 23:28:59 -07002997 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3000 clid->cl_boot, clid->cl_id);
3001
3002 /* XXX check for lease expiration */
3003
3004 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07003005 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008 nfs4_lock_state();
3009
NeilBrown3e9e3db2005-06-23 22:04:20 -07003010 status = nfserr_locks_held;
3011 /* XXX: we're doing a linear search through all the lockowners.
3012 * Yipes! For now we'll just hope clients aren't really using
3013 * release_lockowner much, but eventually we have to fix these
3014 * data structures. */
3015 INIT_LIST_HEAD(&matches);
3016 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3017 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003018 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07003019 continue;
3020 list_for_each_entry(stp, &sop->so_stateids,
3021 st_perstateowner) {
3022 if (check_for_locks(stp->st_vfs_file, sop))
3023 goto out;
3024 /* Note: so_perclient unused for lockowners,
3025 * so it's OK to fool with here. */
3026 list_add(&sop->so_perclient, &matches);
3027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07003029 }
3030 /* Clients probably won't expect us to return with some (but not all)
3031 * of the lockowner state released; so don't release any until all
3032 * have been checked. */
3033 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07003034 while (!list_empty(&matches)) {
3035 sop = list_entry(matches.next, struct nfs4_stateowner,
3036 so_perclient);
3037 /* unhash_stateowner deletes so_perclient only
3038 * for openowners. */
3039 list_del(&sop->so_perclient);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003040 release_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042out:
3043 nfs4_unlock_state();
3044 return status;
3045}
3046
3047static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07003048alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
NeilBrowna55370a2005-06-23 22:03:52 -07003050 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
NeilBrownc7b9a452005-06-23 22:04:30 -07003053int
3054nfs4_has_reclaimed_state(const char *name)
3055{
3056 unsigned int strhashval = clientstr_hashval(name);
3057 struct nfs4_client *clp;
3058
3059 clp = find_confirmed_client_by_str(name, strhashval);
3060 return clp ? 1 : 0;
3061}
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063/*
3064 * failure => all reset bets are off, nfserr_no_grace...
3065 */
NeilBrown190e4fb2005-06-23 22:04:25 -07003066int
3067nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 unsigned int strhashval;
3070 struct nfs4_client_reclaim *crp = NULL;
3071
NeilBrowna55370a2005-06-23 22:03:52 -07003072 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3073 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 if (!crp)
3075 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07003076 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 INIT_LIST_HEAD(&crp->cr_strhash);
3078 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07003079 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 reclaim_str_hashtbl_size++;
3081 return 1;
3082}
3083
3084static void
3085nfs4_release_reclaim(void)
3086{
3087 struct nfs4_client_reclaim *crp = NULL;
3088 int i;
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3091 while (!list_empty(&reclaim_str_hashtbl[i])) {
3092 crp = list_entry(reclaim_str_hashtbl[i].next,
3093 struct nfs4_client_reclaim, cr_strhash);
3094 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 kfree(crp);
3096 reclaim_str_hashtbl_size--;
3097 }
3098 }
3099 BUG_ON(reclaim_str_hashtbl_size);
3100}
3101
3102/*
3103 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07003104static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105nfs4_find_reclaim_client(clientid_t *clid)
3106{
3107 unsigned int strhashval;
3108 struct nfs4_client *clp;
3109 struct nfs4_client_reclaim *crp = NULL;
3110
3111
3112 /* find clientid in conf_id_hashtbl */
3113 clp = find_confirmed_client(clid);
3114 if (clp == NULL)
3115 return NULL;
3116
NeilBrowna55370a2005-06-23 22:03:52 -07003117 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3118 clp->cl_name.len, clp->cl_name.data,
3119 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07003122 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07003124 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return crp;
3126 }
3127 }
3128 return NULL;
3129}
3130
3131/*
3132* Called from OPEN. Look for clientid in reclaim list.
3133*/
Al Virob37ad282006-10-19 23:28:59 -07003134__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135nfs4_check_open_reclaim(clientid_t *clid)
3136{
NeilBrowndfc83562005-06-23 22:03:16 -07003137 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
NeilBrownac4d8ff22005-06-23 22:03:30 -07003140/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003142int
NeilBrownac4d8ff22005-06-23 22:03:30 -07003143nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003145 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003147 status = nfsd4_init_slabs();
3148 if (status)
3149 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3151 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3152 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3153 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3154 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3155 }
3156 for (i = 0; i < FILE_HASH_SIZE; i++) {
3157 INIT_LIST_HEAD(&file_hashtbl[i]);
3158 }
3159 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3160 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3161 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3162 }
3163 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3164 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3165 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3166 }
3167 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3168 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3169 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 INIT_LIST_HEAD(&close_lru);
3173 INIT_LIST_HEAD(&client_lru);
3174 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07003175 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3176 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3177 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003178 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003179}
3180
NeilBrown190e4fb2005-06-23 22:04:25 -07003181static void
3182nfsd4_load_reboot_recovery_data(void)
3183{
3184 int status;
3185
NeilBrown0964a3d2005-06-23 22:04:32 -07003186 nfs4_lock_state();
3187 nfsd4_init_recdir(user_recovery_dirname);
NeilBrown190e4fb2005-06-23 22:04:25 -07003188 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07003189 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07003190 if (status)
3191 printk("NFSD: Failure reading reboot recovery data\n");
3192}
3193
Marc Eshel9a8db972007-07-17 04:04:35 -07003194unsigned long
3195get_nfs4_grace_period(void)
3196{
3197 return max(user_lease_time, lease_time) * HZ;
3198}
3199
Meelap Shahc2f1a552007-07-17 04:04:39 -07003200/*
3201 * Since the lifetime of a delegation isn't limited to that of an open, a
3202 * client may quite reasonably hang on to a delegation as long as it has
3203 * the inode cached. This becomes an obvious problem the first time a
3204 * client's inode cache approaches the size of the server's total memory.
3205 *
3206 * For now we avoid this problem by imposing a hard limit on the number
3207 * of delegations, which varies according to the server's memory size.
3208 */
3209static void
3210set_max_delegations(void)
3211{
3212 /*
3213 * Allow at most 4 delegations per megabyte of RAM. Quick
3214 * estimates suggest that in the worst case (where every delegation
3215 * is for a different inode), a delegation could take about 1.5K,
3216 * giving a worst case usage of about 6% of memory.
3217 */
3218 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
3219}
3220
NeilBrownac4d8ff22005-06-23 22:03:30 -07003221/* initialization to perform when the nfsd service is started: */
3222
3223static void
3224__nfs4_state_start(void)
3225{
Marc Eshel9a8db972007-07-17 04:04:35 -07003226 unsigned long grace_time;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003229 grace_time = get_nfs4_grace_period();
NeilBrownd99a05a2005-06-23 22:03:21 -07003230 lease_time = user_lease_time;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003231 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07003232 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
3233 grace_time/HZ);
NeilBrown58da2822005-06-23 22:03:19 -07003234 laundry_wq = create_singlethread_workqueue("nfsd4");
Marc Eshel9a8db972007-07-17 04:04:35 -07003235 queue_delayed_work(laundry_wq, &laundromat_work, grace_time);
Meelap Shahc2f1a552007-07-17 04:04:39 -07003236 set_max_delegations();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003239void
NeilBrown76a35502005-06-23 22:03:26 -07003240nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 if (nfs4_init)
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003243 return;
NeilBrown190e4fb2005-06-23 22:04:25 -07003244 nfsd4_load_reboot_recovery_data();
NeilBrown76a35502005-06-23 22:03:26 -07003245 __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 nfs4_init = 1;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003247 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250time_t
3251nfs4_lease_time(void)
3252{
3253 return lease_time;
3254}
3255
3256static void
3257__nfs4_state_shutdown(void)
3258{
3259 int i;
3260 struct nfs4_client *clp = NULL;
3261 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 struct list_head *pos, *next, reaplist;
3263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3265 while (!list_empty(&conf_id_hashtbl[i])) {
3266 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3267 expire_client(clp);
3268 }
3269 while (!list_empty(&unconf_str_hashtbl[i])) {
3270 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3271 expire_client(clp);
3272 }
3273 }
3274 INIT_LIST_HEAD(&reaplist);
3275 spin_lock(&recall_lock);
3276 list_for_each_safe(pos, next, &del_recall_lru) {
3277 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3278 list_move(&dp->dl_recall_lru, &reaplist);
3279 }
3280 spin_unlock(&recall_lock);
3281 list_for_each_safe(pos, next, &reaplist) {
3282 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3283 list_del_init(&dp->dl_recall_lru);
3284 unhash_delegation(dp);
3285 }
3286
NeilBrown190e4fb2005-06-23 22:04:25 -07003287 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 nfs4_init = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
3291void
3292nfs4_state_shutdown(void)
3293{
NeilBrown5e8d5c22006-04-10 22:55:37 -07003294 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3295 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06003296 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 nfs4_lock_state();
3298 nfs4_release_reclaim();
3299 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 nfs4_unlock_state();
3301}
3302
Jeff Layton3dd98a32008-06-10 08:40:36 -04003303/*
3304 * user_recovery_dirname is protected by the nfsd_mutex since it's only
3305 * accessed when nfsd is starting.
3306 */
NeilBrown0964a3d2005-06-23 22:04:32 -07003307static void
3308nfs4_set_recdir(char *recdir)
3309{
NeilBrown0964a3d2005-06-23 22:04:32 -07003310 strcpy(user_recovery_dirname, recdir);
NeilBrown0964a3d2005-06-23 22:04:32 -07003311}
3312
3313/*
3314 * Change the NFSv4 recovery directory to recdir.
3315 */
3316int
3317nfs4_reset_recoverydir(char *recdir)
3318{
3319 int status;
Al Viroa63bb992008-08-02 01:03:36 -04003320 struct path path;
NeilBrown0964a3d2005-06-23 22:04:32 -07003321
Al Viroa63bb992008-08-02 01:03:36 -04003322 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
NeilBrown0964a3d2005-06-23 22:04:32 -07003323 if (status)
3324 return status;
3325 status = -ENOTDIR;
Al Viroa63bb992008-08-02 01:03:36 -04003326 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
NeilBrown0964a3d2005-06-23 22:04:32 -07003327 nfs4_set_recdir(recdir);
3328 status = 0;
3329 }
Al Viroa63bb992008-08-02 01:03:36 -04003330 path_put(&path);
NeilBrown0964a3d2005-06-23 22:04:32 -07003331 return status;
3332}
3333
Jeff Layton3dd98a32008-06-10 08:40:36 -04003334char *
3335nfs4_recoverydir(void)
3336{
3337 return user_recovery_dirname;
3338}
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340/*
3341 * Called when leasetime is changed.
3342 *
NeilBrownd99a05a2005-06-23 22:03:21 -07003343 * The only way the protocol gives us to handle on-the-fly lease changes is to
3344 * simulate a reboot. Instead of doing that, we just wait till the next time
3345 * we start to register any changes in lease time. If the administrator
3346 * really wants to change the lease time *now*, they can go ahead and bring
3347 * nfsd down and then back up again after changing the lease time.
Jeff Layton3dd98a32008-06-10 08:40:36 -04003348 *
3349 * user_lease_time is protected by nfsd_mutex since it's only really accessed
3350 * when nfsd is starting
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 */
3352void
3353nfs4_reset_lease(time_t leasetime)
3354{
NeilBrownd99a05a2005-06-23 22:03:21 -07003355 user_lease_time = leasetime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}