blob: fc20e1f38d7544bf0f84a4b5c0c078b26974ec2a [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);
NeilBrown0964a3d2005-06-23 22:04:32 -070078static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
79static void nfs4_set_recdir(char *recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/* Locking: */
82
83/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080084static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
J. Bruce Fields8b671b82009-02-22 14:51:34 -080086/*
87 * Currently used for the del_recall_lru and file hash table. In an
88 * effort to decrease the scope of the client_mutex, this spinlock may
89 * eventually cover more:
90 */
91static DEFINE_SPINLOCK(recall_lock);
92
Christoph Lametere18b8902006-12-06 20:33:20 -080093static struct kmem_cache *stateowner_slab = NULL;
94static struct kmem_cache *file_slab = NULL;
95static struct kmem_cache *stateid_slab = NULL;
96static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098void
99nfs4_lock_state(void)
100{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800101 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104void
105nfs4_unlock_state(void)
106{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800107 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110static inline u32
111opaque_hashval(const void *ptr, int nbytes)
112{
113 unsigned char *cptr = (unsigned char *) ptr;
114
115 u32 x = 0;
116 while (nbytes--) {
117 x *= 37;
118 x += *cptr++;
119 }
120 return x;
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static struct list_head del_recall_lru;
124
NeilBrown13cd2182005-06-23 22:03:10 -0700125static inline void
126put_nfs4_file(struct nfs4_file *fi)
127{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800128 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
129 list_del(&fi->fi_hash);
130 spin_unlock(&recall_lock);
131 iput(fi->fi_inode);
132 kmem_cache_free(file_slab, fi);
133 }
NeilBrown13cd2182005-06-23 22:03:10 -0700134}
135
136static inline void
137get_nfs4_file(struct nfs4_file *fi)
138{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800139 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700140}
141
NeilBrownef0f3392006-04-10 22:55:41 -0700142static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700143unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700144
145/*
146 * Open owner state (share locks)
147 */
148
149/* hash tables for nfs4_stateowner */
150#define OWNER_HASH_BITS 8
151#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
152#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
153
154#define ownerid_hashval(id) \
155 ((id) & OWNER_HASH_MASK)
156#define ownerstr_hashval(clientid, ownername) \
157 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
158
159static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
160static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
161
162/* hash table for nfs4_file */
163#define FILE_HASH_BITS 8
164#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
165#define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
166/* hash table for (open)nfs4_stateid */
167#define STATEID_HASH_BITS 10
168#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
169#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
170
171#define file_hashval(x) \
172 hash_ptr(x, FILE_HASH_BITS)
173#define stateid_hashval(owner_id, file_id) \
174 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
175
176static struct list_head file_hashtbl[FILE_HASH_SIZE];
177static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static struct nfs4_delegation *
180alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
181{
182 struct nfs4_delegation *dp;
183 struct nfs4_file *fp = stp->st_file;
184 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
185
186 dprintk("NFSD alloc_init_deleg\n");
Meelap Shah47f99402007-07-17 04:04:40 -0700187 if (fp->fi_had_conflict)
188 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700189 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700190 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700191 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
192 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700194 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700195 INIT_LIST_HEAD(&dp->dl_perfile);
196 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 INIT_LIST_HEAD(&dp->dl_recall_lru);
198 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700199 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 dp->dl_file = fp;
201 dp->dl_flock = NULL;
202 get_file(stp->st_vfs_file);
203 dp->dl_vfs_file = stp->st_vfs_file;
204 dp->dl_type = type;
205 dp->dl_recall.cbr_dp = NULL;
206 dp->dl_recall.cbr_ident = cb->cb_ident;
207 dp->dl_recall.cbr_trunc = 0;
208 dp->dl_stateid.si_boot = boot_time;
209 dp->dl_stateid.si_stateownerid = current_delegid++;
210 dp->dl_stateid.si_fileid = 0;
211 dp->dl_stateid.si_generation = 0;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500212 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 dp->dl_time = 0;
214 atomic_set(&dp->dl_count, 1);
NeilBrownea1da632005-06-23 22:04:17 -0700215 list_add(&dp->dl_perfile, &fp->fi_delegations);
216 list_add(&dp->dl_perclnt, &clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return dp;
218}
219
220void
221nfs4_put_delegation(struct nfs4_delegation *dp)
222{
223 if (atomic_dec_and_test(&dp->dl_count)) {
224 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700225 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700226 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700227 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229}
230
231/* Remove the associated file_lock first, then remove the delegation.
232 * lease_modify() is called to remove the FS_LEASE file_lock from
233 * the i_flock list, eventually calling nfsd's lock_manager
234 * fl_release_callback.
235 */
236static void
237nfs4_close_delegation(struct nfs4_delegation *dp)
238{
239 struct file *filp = dp->dl_vfs_file;
240
241 dprintk("NFSD: close_delegation dp %p\n",dp);
242 dp->dl_vfs_file = NULL;
243 /* The following nfsd_close may not actually close the file,
244 * but we want to remove the lease in any case. */
NeilBrownc9071322005-04-16 15:26:38 -0700245 if (dp->dl_flock)
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -0400246 vfs_setlease(filp, F_UNLCK, &dp->dl_flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250/* Called under the state lock. */
251static void
252unhash_delegation(struct nfs4_delegation *dp)
253{
NeilBrownea1da632005-06-23 22:04:17 -0700254 list_del_init(&dp->dl_perfile);
255 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 spin_lock(&recall_lock);
257 list_del_init(&dp->dl_recall_lru);
258 spin_unlock(&recall_lock);
259 nfs4_close_delegation(dp);
260 nfs4_put_delegation(dp);
261}
262
263/*
264 * SETCLIENTID state
265 */
266
267/* Hash tables for nfs4_clientid state */
268#define CLIENT_HASH_BITS 4
269#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
270#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
271
272#define clientid_hashval(id) \
273 ((id) & CLIENT_HASH_MASK)
NeilBrowna55370a2005-06-23 22:03:52 -0700274#define clientstr_hashval(name) \
275 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
277 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
278 * used in reboot/reset lease grace period processing
279 *
280 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
281 * setclientid_confirmed info.
282 *
283 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
284 * setclientid info.
285 *
286 * client_lru holds client queue ordered by nfs4_client.cl_time
287 * for lease renewal.
288 *
289 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
290 * for last close replay.
291 */
292static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
293static int reclaim_str_hashtbl_size = 0;
294static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
295static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
296static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
297static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
298static struct list_head client_lru;
299static struct list_head close_lru;
300
J. Bruce Fields22839632009-01-11 14:27:17 -0500301static void unhash_generic_stateid(struct nfs4_stateid *stp)
302{
303 list_del(&stp->st_hash);
304 list_del(&stp->st_perfile);
305 list_del(&stp->st_perstateowner);
306}
307
308static void free_generic_stateid(struct nfs4_stateid *stp)
309{
310 put_nfs4_file(stp->st_file);
311 kmem_cache_free(stateid_slab, stp);
312}
313
314static void release_lock_stateid(struct nfs4_stateid *stp)
315{
316 unhash_generic_stateid(stp);
317 locks_remove_posix(stp->st_vfs_file, (fl_owner_t)stp->st_stateowner);
318 free_generic_stateid(stp);
319}
320
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500321static void unhash_lockowner(struct nfs4_stateowner *sop)
322{
323 struct nfs4_stateid *stp;
324
325 list_del(&sop->so_idhash);
326 list_del(&sop->so_strhash);
327 list_del(&sop->so_perstateid);
328 while (!list_empty(&sop->so_stateids)) {
329 stp = list_first_entry(&sop->so_stateids,
330 struct nfs4_stateid, st_perstateowner);
331 release_lock_stateid(stp);
332 }
333}
334
335static void release_lockowner(struct nfs4_stateowner *sop)
336{
337 unhash_lockowner(sop);
338 nfs4_put_stateowner(sop);
339}
340
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500341static void
342release_stateid_lockowners(struct nfs4_stateid *open_stp)
343{
344 struct nfs4_stateowner *lock_sop;
345
346 while (!list_empty(&open_stp->st_lockowners)) {
347 lock_sop = list_entry(open_stp->st_lockowners.next,
348 struct nfs4_stateowner, so_perstateid);
349 /* list_del(&open_stp->st_lockowners); */
350 BUG_ON(lock_sop->so_is_open_owner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500351 release_lockowner(lock_sop);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500352 }
353}
354
J. Bruce Fields22839632009-01-11 14:27:17 -0500355static void release_open_stateid(struct nfs4_stateid *stp)
356{
357 unhash_generic_stateid(stp);
358 release_stateid_lockowners(stp);
359 nfsd_close(stp->st_vfs_file);
360 free_generic_stateid(stp);
361}
362
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500363static void unhash_openowner(struct nfs4_stateowner *sop)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500364{
365 struct nfs4_stateid *stp;
366
367 list_del(&sop->so_idhash);
368 list_del(&sop->so_strhash);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500369 list_del(&sop->so_perclient);
370 list_del(&sop->so_perstateid); /* XXX: necessary? */
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500371 while (!list_empty(&sop->so_stateids)) {
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500372 stp = list_first_entry(&sop->so_stateids,
373 struct nfs4_stateid, st_perstateowner);
374 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500375 }
376}
377
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500378static void release_openowner(struct nfs4_stateowner *sop)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500379{
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500380 unhash_openowner(sop);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500381 list_del(&sop->so_close_lru);
382 nfs4_put_stateowner(sop);
383}
384
Marc Eshel5282fd72009-04-03 08:27:52 +0300385static DEFINE_SPINLOCK(sessionid_lock);
386#define SESSION_HASH_SIZE 512
387static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
388
389static inline int
390hash_sessionid(struct nfs4_sessionid *sessionid)
391{
392 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
393
394 return sid->sequence % SESSION_HASH_SIZE;
395}
396
397static inline void
398dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
399{
400 u32 *ptr = (u32 *)(&sessionid->data[0]);
401 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
402}
403
404/* caller must hold sessionid_lock */
405static struct nfsd4_session *
406find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
407{
408 struct nfsd4_session *elem;
409 int idx;
410
411 dump_sessionid(__func__, sessionid);
412 idx = hash_sessionid(sessionid);
413 dprintk("%s: idx is %d\n", __func__, idx);
414 /* Search in the appropriate list */
415 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
416 dump_sessionid("list traversal", &elem->se_sessionid);
417 if (!memcmp(elem->se_sessionid.data, sessionid->data,
418 NFS4_MAX_SESSIONID_LEN)) {
419 return elem;
420 }
421 }
422
423 dprintk("%s: session not found\n", __func__);
424 return NULL;
425}
426
427/* caller must hold sessionid_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300428static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300429unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300430{
431 list_del(&ses->se_hash);
432 list_del(&ses->se_perclnt);
Marc Eshel5282fd72009-04-03 08:27:52 +0300433}
434
435static void
436release_session(struct nfsd4_session *ses)
437{
438 spin_lock(&sessionid_lock);
439 unhash_session(ses);
440 spin_unlock(&sessionid_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300441 nfsd4_put_session(ses);
442}
443
444void
445free_session(struct kref *kref)
446{
447 struct nfsd4_session *ses;
448
449 ses = container_of(kref, struct nfsd4_session, se_ref);
450 kfree(ses->se_slots);
451 kfree(ses);
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static inline void
455renew_client(struct nfs4_client *clp)
456{
457 /*
458 * Move client to the end to the LRU list.
459 */
460 dprintk("renewing client (clientid %08x/%08x)\n",
461 clp->cl_clientid.cl_boot,
462 clp->cl_clientid.cl_id);
463 list_move_tail(&clp->cl_lru, &client_lru);
464 clp->cl_time = get_seconds();
465}
466
467/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
468static int
469STALE_CLIENTID(clientid_t *clid)
470{
471 if (clid->cl_boot == boot_time)
472 return 0;
473 dprintk("NFSD stale clientid (%08x/%08x)\n",
474 clid->cl_boot, clid->cl_id);
475 return 1;
476}
477
478/*
479 * XXX Should we use a slab cache ?
480 * This type of memory management is somewhat inefficient, but we use it
481 * anyway since SETCLIENTID is not a common operation.
482 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500483static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct nfs4_client *clp;
486
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500487 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
488 if (clp == NULL)
489 return NULL;
490 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
491 if (clp->cl_name.data == NULL) {
492 kfree(clp);
493 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500495 memcpy(clp->cl_name.data, name.data, name.len);
496 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return clp;
498}
499
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400500static void
501shutdown_callback_client(struct nfs4_client *clp)
502{
503 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
504
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400505 if (clnt) {
J. Bruce Fields404ec112007-11-23 22:26:18 -0500506 /*
507 * Callback threads take a reference on the client, so there
508 * should be no outstanding callbacks at this point.
509 */
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400510 clp->cl_callback.cb_client = NULL;
511 rpc_shutdown_client(clnt);
512 }
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static inline void
516free_client(struct nfs4_client *clp)
517{
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400518 shutdown_callback_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (clp->cl_cred.cr_group_info)
520 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500521 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 kfree(clp->cl_name.data);
523 kfree(clp);
524}
525
526void
527put_nfs4_client(struct nfs4_client *clp)
528{
529 if (atomic_dec_and_test(&clp->cl_count))
530 free_client(clp);
531}
532
533static void
534expire_client(struct nfs4_client *clp)
535{
536 struct nfs4_stateowner *sop;
537 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct list_head reaplist;
539
540 dprintk("NFSD: expire_client cl_count %d\n",
541 atomic_read(&clp->cl_count));
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 INIT_LIST_HEAD(&reaplist);
544 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700545 while (!list_empty(&clp->cl_delegations)) {
546 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
548 dp->dl_flock);
NeilBrownea1da632005-06-23 22:04:17 -0700549 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 list_move(&dp->dl_recall_lru, &reaplist);
551 }
552 spin_unlock(&recall_lock);
553 while (!list_empty(&reaplist)) {
554 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
555 list_del_init(&dp->dl_recall_lru);
556 unhash_delegation(dp);
557 }
558 list_del(&clp->cl_idhash);
559 list_del(&clp->cl_strhash);
560 list_del(&clp->cl_lru);
NeilBrownea1da632005-06-23 22:04:17 -0700561 while (!list_empty(&clp->cl_openowners)) {
562 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500563 release_openowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Marc Eshelc4bf7862009-04-03 08:27:49 +0300565 while (!list_empty(&clp->cl_sessions)) {
566 struct nfsd4_session *ses;
567 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
568 se_perclnt);
569 release_session(ses);
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 put_nfs4_client(clp);
572}
573
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500574static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir)
575{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct nfs4_client *clp;
577
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500578 clp = alloc_client(name);
579 if (clp == NULL)
580 return NULL;
NeilBrowna55370a2005-06-23 22:03:52 -0700581 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 atomic_set(&clp->cl_count, 1);
583 atomic_set(&clp->cl_callback.cb_set, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 INIT_LIST_HEAD(&clp->cl_idhash);
585 INIT_LIST_HEAD(&clp->cl_strhash);
NeilBrownea1da632005-06-23 22:04:17 -0700586 INIT_LIST_HEAD(&clp->cl_openowners);
587 INIT_LIST_HEAD(&clp->cl_delegations);
Marc Eshel9fb87072009-04-03 08:27:46 +0300588 INIT_LIST_HEAD(&clp->cl_sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 INIT_LIST_HEAD(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return clp;
591}
592
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500593static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
594{
595 memcpy(target->cl_verifier.data, source->data,
596 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500599static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
600{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
602 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
603}
604
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500605static void copy_cred(struct svc_cred *target, struct svc_cred *source)
606{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 target->cr_uid = source->cr_uid;
608 target->cr_gid = source->cr_gid;
609 target->cr_group_info = source->cr_group_info;
610 get_group_info(target->cr_group_info);
611}
612
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500613static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400614{
NeilBrowna55370a2005-06-23 22:03:52 -0700615 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400619same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
620{
621 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400625same_clid(clientid_t *cl1, clientid_t *cl2)
626{
627 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630/* XXX what about NGROUP */
631static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400632same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
633{
634 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
J. Bruce Fields5ec7b462007-11-21 21:58:56 -0500637static void gen_clid(struct nfs4_client *clp)
638{
639 static u32 current_clientid = 1;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 clp->cl_clientid.cl_boot = boot_time;
642 clp->cl_clientid.cl_id = current_clientid++;
643}
644
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -0500645static void gen_confirm(struct nfs4_client *clp)
646{
647 static u32 i;
648 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -0500651 *p++ = get_seconds();
652 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500655static int check_name(struct xdr_netobj name)
656{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (name.len == 0)
658 return 0;
659 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400660 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return 0;
662 }
663 return 1;
664}
665
NeilBrownfd39ca92005-06-23 22:04:03 -0700666static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
668{
669 unsigned int idhashval;
670
671 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
672 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
673 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
674 list_add_tail(&clp->cl_lru, &client_lru);
675 clp->cl_time = get_seconds();
676}
677
NeilBrownfd39ca92005-06-23 22:04:03 -0700678static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679move_to_confirmed(struct nfs4_client *clp)
680{
681 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
682 unsigned int strhashval;
683
684 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
685 list_del_init(&clp->cl_strhash);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700686 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -0700687 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
689 renew_client(clp);
690}
691
692static struct nfs4_client *
693find_confirmed_client(clientid_t *clid)
694{
695 struct nfs4_client *clp;
696 unsigned int idhashval = clientid_hashval(clid->cl_id);
697
698 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400699 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return clp;
701 }
702 return NULL;
703}
704
705static struct nfs4_client *
706find_unconfirmed_client(clientid_t *clid)
707{
708 struct nfs4_client *clp;
709 unsigned int idhashval = clientid_hashval(clid->cl_id);
710
711 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400712 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return clp;
714 }
715 return NULL;
716}
717
NeilBrown28ce6052005-06-23 22:03:56 -0700718static struct nfs4_client *
719find_confirmed_client_by_str(const char *dname, unsigned int hashval)
720{
721 struct nfs4_client *clp;
722
723 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
724 if (same_name(clp->cl_recdir, dname))
725 return clp;
726 }
727 return NULL;
728}
729
730static struct nfs4_client *
731find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
732{
733 struct nfs4_client *clp;
734
735 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
736 if (same_name(clp->cl_recdir, dname))
737 return clp;
738 }
739 return NULL;
740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/* a helper function for parse_callback */
743static int
744parse_octet(unsigned int *lenp, char **addrp)
745{
746 unsigned int len = *lenp;
747 char *p = *addrp;
748 int n = -1;
749 char c;
750
751 for (;;) {
752 if (!len)
753 break;
754 len--;
755 c = *p++;
756 if (c == '.')
757 break;
758 if ((c < '0') || (c > '9')) {
759 n = -1;
760 break;
761 }
762 if (n < 0)
763 n = 0;
764 n = (n * 10) + (c - '0');
765 if (n > 255) {
766 n = -1;
767 break;
768 }
769 }
770 *lenp = len;
771 *addrp = p;
772 return n;
773}
774
775/* parse and set the setclientid ipv4 callback address */
NeilBrownfd39ca92005-06-23 22:04:03 -0700776static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
778{
779 int temp = 0;
780 u32 cbaddr = 0;
781 u16 cbport = 0;
782 u32 addrlen = addr_len;
783 char *addr = addr_val;
784 int i, shift;
785
786 /* ipaddress */
787 shift = 24;
788 for(i = 4; i > 0 ; i--) {
789 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
790 return 0;
791 }
792 cbaddr |= (temp << shift);
793 if (shift > 0)
794 shift -= 8;
795 }
796 *cbaddrp = cbaddr;
797
798 /* port */
799 shift = 8;
800 for(i = 2; i > 0 ; i--) {
801 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
802 return 0;
803 }
804 cbport |= (temp << shift);
805 if (shift > 0)
806 shift -= 8;
807 }
808 *cbportp = cbport;
809 return 1;
810}
811
NeilBrownfd39ca92005-06-23 22:04:03 -0700812static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
814{
815 struct nfs4_callback *cb = &clp->cl_callback;
816
817 /* Currently, we only support tcp for the callback channel */
818 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
819 goto out_err;
820
821 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
822 &cb->cb_addr, &cb->cb_port)))
823 goto out_err;
824 cb->cb_prog = se->se_callback_prog;
825 cb->cb_ident = se->se_callback_ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return;
827out_err:
Neil Brown849823c2005-09-13 01:25:36 -0700828 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 "will not receive delegations\n",
830 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return;
833}
834
Al Virob37ad282006-10-19 23:28:59 -0700835__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800836nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
837 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Chuck Lever27459f02007-02-12 00:53:34 -0800839 struct sockaddr_in *sin = svc_addr_in(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct xdr_netobj clname = {
841 .len = setclid->se_namelen,
842 .data = setclid->se_name,
843 };
844 nfs4_verifier clverifier = setclid->se_verf;
845 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -0700846 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -0700847 __be32 status;
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500848 char *princ;
NeilBrowna55370a2005-06-23 22:03:52 -0700849 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -0700852 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
NeilBrowna55370a2005-06-23 22:03:52 -0700854 status = nfs4_make_rec_clidname(dname, &clname);
855 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -0700856 return status;
NeilBrowna55370a2005-06-23 22:03:52 -0700857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /*
859 * XXX The Duplicate Request Cache (DRC) has been checked (??)
860 * We get here on a DRC miss.
861 */
862
NeilBrowna55370a2005-06-23 22:03:52 -0700863 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 nfs4_lock_state();
NeilBrown28ce6052005-06-23 22:03:56 -0700866 conf = find_confirmed_client_by_str(dname, strhashval);
867 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500868 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 status = nfserr_clid_inuse;
J. Bruce Fields026722c2009-03-18 15:06:26 -0400870 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
871 dprintk("NFSD: setclientid: string in use by client"
872 " at %pI4\n", &conf->cl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto out;
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500876 /*
877 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
878 * has a description of SETCLIENTID request processing consisting
879 * of 5 bullet points, labeled as CASE0 - CASE4 below.
880 */
NeilBrown28ce6052005-06-23 22:03:56 -0700881 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 status = nfserr_resource;
883 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500884 /*
885 * RFC 3530 14.2.33 CASE 4:
886 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
888 if (unconf)
889 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700890 new = create_client(clname, dname);
891 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400894 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500896 * RFC 3530 14.2.33 CASE 1:
897 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
NeilBrown31f4a6c2005-06-23 22:04:06 -0700899 if (unconf) {
900 /* Note this is removing unconfirmed {*x***},
901 * which is stronger than RFC recommended {vxc**}.
902 * This has the advantage that there is at most
903 * one {*x***} in either list at any time.
904 */
905 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
NeilBrowna55370a2005-06-23 22:03:52 -0700907 new = create_client(clname, dname);
908 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 } else if (!unconf) {
912 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500913 * RFC 3530 14.2.33 CASE 2:
914 * probable client reboot; state will be removed if
915 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 */
NeilBrowna55370a2005-06-23 22:03:52 -0700917 new = create_client(clname, dname);
918 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -0500921 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500922 /*
923 * RFC 3530 14.2.33 CASE 3:
924 * probable client reboot; state will be removed if
925 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 */
927 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700928 new = create_client(clname, dname);
929 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
J. Bruce Fieldsc175b832007-08-09 18:34:32 -0400933 copy_verf(new, &clverifier);
934 new->cl_addr = sin->sin_addr.s_addr;
Olga Kornievskaia61054b12008-12-23 16:19:00 -0500935 new->cl_flavor = rqstp->rq_flavor;
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500936 princ = svc_gss_principal(rqstp);
937 if (princ) {
938 new->cl_principal = kstrdup(princ, GFP_KERNEL);
939 if (new->cl_principal == NULL) {
940 free_client(new);
941 goto out;
942 }
943 }
J. Bruce Fieldsc175b832007-08-09 18:34:32 -0400944 copy_cred(&new->cl_cred, &rqstp->rq_cred);
945 gen_confirm(new);
946 gen_callback(new, setclid);
947 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
949 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
950 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
951 status = nfs_ok;
952out:
953 nfs4_unlock_state();
954 return status;
955}
956
957
958/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500959 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
960 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
961 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 */
Al Virob37ad282006-10-19 23:28:59 -0700963__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800964nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
965 struct nfsd4_compound_state *cstate,
966 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Chuck Lever27459f02007-02-12 00:53:34 -0800968 struct sockaddr_in *sin = svc_addr_in(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -0700969 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
971 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -0700972 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 if (STALE_CLIENTID(clid))
975 return nfserr_stale_clientid;
976 /*
977 * XXX The Duplicate Request Cache (DRC) has been checked (??)
978 * We get here on a DRC miss.
979 */
980
981 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -0700982
983 conf = find_confirmed_client(clid);
984 unconf = find_unconfirmed_client(clid);
985
986 status = nfserr_clid_inuse;
Chuck Lever27459f02007-02-12 00:53:34 -0800987 if (conf && conf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -0700988 goto out;
Chuck Lever27459f02007-02-12 00:53:34 -0800989 if (unconf && unconf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -0700990 goto out;
991
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500992 /*
993 * section 14.2.34 of RFC 3530 has a description of
994 * SETCLIENTID_CONFIRM request processing consisting
995 * of 4 bullet points, labeled as CASE1 - CASE4 below.
996 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -0500997 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500998 /*
999 * RFC 3530 14.2.34 CASE 1:
1000 * callback update
1001 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001002 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 status = nfserr_clid_inuse;
1004 else {
NeilBrown1a69c172005-06-23 22:04:08 -07001005 /* XXX: We just turn off callbacks until we can handle
1006 * change request correctly. */
NeilBrowncb36d632005-06-23 22:04:23 -07001007 atomic_set(&conf->cl_callback.cb_set, 0);
NeilBrown21ab45a2005-06-23 22:04:14 -07001008 gen_confirm(conf);
NeilBrown46309022005-07-07 17:59:10 -07001009 nfsd4_remove_clid_dir(unconf);
NeilBrown1a69c172005-06-23 22:04:08 -07001010 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05001014 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001015 /*
1016 * RFC 3530 14.2.34 CASE 2:
1017 * probable retransmitted request; play it safe and
1018 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07001019 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001020 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07001022 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07001024 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001025 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001026 /*
1027 * RFC 3530 14.2.34 CASE 3:
1028 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07001029 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001030 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 status = nfserr_clid_inuse;
1032 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07001033 unsigned int hash =
1034 clientstr_hashval(unconf->cl_recdir);
1035 conf = find_confirmed_client_by_str(unconf->cl_recdir,
1036 hash);
1037 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07001038 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07001039 expire_client(conf);
1040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07001042 conf = unconf;
J. Bruce Fields46f8a642007-11-22 13:54:18 -05001043 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07001044 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001046 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
1047 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07001048 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001049 /*
1050 * RFC 3530 14.2.34 CASE 4:
1051 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07001052 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07001054 } else {
NeilBrown08e89872005-06-23 22:04:11 -07001055 /* check that we have hit one of the cases...*/
1056 status = nfserr_clid_inuse;
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 nfs4_unlock_state();
1060 return status;
1061}
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063/* OPEN Share state helper functions */
1064static inline struct nfs4_file *
1065alloc_init_file(struct inode *ino)
1066{
1067 struct nfs4_file *fp;
1068 unsigned int hashval = file_hashval(ino);
1069
NeilBrowne60d4392005-06-23 22:03:01 -07001070 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
1071 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001072 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07001074 INIT_LIST_HEAD(&fp->fi_stateids);
1075 INIT_LIST_HEAD(&fp->fi_delegations);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001076 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001078 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 fp->fi_inode = igrab(ino);
1080 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -07001081 fp->fi_had_conflict = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return fp;
1083 }
1084 return NULL;
1085}
1086
1087static void
Christoph Lametere18b8902006-12-06 20:33:20 -08001088nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07001089{
NeilBrowne60d4392005-06-23 22:03:01 -07001090 if (*slab == NULL)
1091 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001092 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001093 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07001094}
1095
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001096void
NeilBrowne60d4392005-06-23 22:03:01 -07001097nfsd4_free_slabs(void)
1098{
1099 nfsd4_free_slab(&stateowner_slab);
1100 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07001101 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001102 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001103}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105static int
1106nfsd4_init_slabs(void)
1107{
1108 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
Paul Mundt20c2df82007-07-20 10:11:58 +09001109 sizeof(struct nfs4_stateowner), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001110 if (stateowner_slab == NULL)
1111 goto out_nomem;
1112 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09001113 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001114 if (file_slab == NULL)
1115 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07001116 stateid_slab = kmem_cache_create("nfsd4_stateids",
Paul Mundt20c2df82007-07-20 10:11:58 +09001117 sizeof(struct nfs4_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07001118 if (stateid_slab == NULL)
1119 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07001120 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09001121 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001122 if (deleg_slab == NULL)
1123 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07001125out_nomem:
1126 nfsd4_free_slabs();
1127 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1128 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131void
1132nfs4_free_stateowner(struct kref *kref)
1133{
1134 struct nfs4_stateowner *sop =
1135 container_of(kref, struct nfs4_stateowner, so_ref);
1136 kfree(sop->so_owner.data);
1137 kmem_cache_free(stateowner_slab, sop);
1138}
1139
1140static inline struct nfs4_stateowner *
1141alloc_stateowner(struct xdr_netobj *owner)
1142{
1143 struct nfs4_stateowner *sop;
1144
1145 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1146 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1147 memcpy(sop->so_owner.data, owner->data, owner->len);
1148 sop->so_owner.len = owner->len;
1149 kref_init(&sop->so_ref);
1150 return sop;
1151 }
1152 kmem_cache_free(stateowner_slab, sop);
1153 }
1154 return NULL;
1155}
1156
1157static struct nfs4_stateowner *
1158alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1159 struct nfs4_stateowner *sop;
1160 struct nfs4_replay *rp;
1161 unsigned int idhashval;
1162
1163 if (!(sop = alloc_stateowner(&open->op_owner)))
1164 return NULL;
1165 idhashval = ownerid_hashval(current_ownerid);
1166 INIT_LIST_HEAD(&sop->so_idhash);
1167 INIT_LIST_HEAD(&sop->so_strhash);
1168 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001169 INIT_LIST_HEAD(&sop->so_stateids);
1170 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 INIT_LIST_HEAD(&sop->so_close_lru);
1172 sop->so_time = 0;
1173 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1174 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001175 list_add(&sop->so_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 sop->so_is_open_owner = 1;
1177 sop->so_id = current_ownerid++;
1178 sop->so_client = clp;
1179 sop->so_seqid = open->op_seqid;
1180 sop->so_confirmed = 0;
1181 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08001182 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 rp->rp_buflen = 0;
1184 rp->rp_buf = rp->rp_ibuf;
1185 return sop;
1186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188static inline void
1189init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1190 struct nfs4_stateowner *sop = open->op_stateowner;
1191 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1192
1193 INIT_LIST_HEAD(&stp->st_hash);
NeilBrownea1da632005-06-23 22:04:17 -07001194 INIT_LIST_HEAD(&stp->st_perstateowner);
1195 INIT_LIST_HEAD(&stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 INIT_LIST_HEAD(&stp->st_perfile);
1197 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001198 list_add(&stp->st_perstateowner, &sop->so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07001199 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07001201 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 stp->st_file = fp;
1203 stp->st_stateid.si_boot = boot_time;
1204 stp->st_stateid.si_stateownerid = sop->so_id;
1205 stp->st_stateid.si_fileid = fp->fi_id;
1206 stp->st_stateid.si_generation = 0;
1207 stp->st_access_bmap = 0;
1208 stp->st_deny_bmap = 0;
1209 __set_bit(open->op_share_access, &stp->st_access_bmap);
1210 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07001211 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215move_to_close_lru(struct nfs4_stateowner *sop)
1216{
1217 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1218
NeilBrown358dd552006-04-10 22:55:42 -07001219 list_move_tail(&sop->so_close_lru, &close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 sop->so_time = get_seconds();
1221}
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001224same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
1225 clientid_t *clid)
1226{
1227 return (sop->so_owner.len == owner->len) &&
1228 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
1229 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232static struct nfs4_stateowner *
1233find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1234{
1235 struct nfs4_stateowner *so = NULL;
1236
1237 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001238 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return so;
1240 }
1241 return NULL;
1242}
1243
1244/* search file_hashtbl[] for file */
1245static struct nfs4_file *
1246find_file(struct inode *ino)
1247{
1248 unsigned int hashval = file_hashval(ino);
1249 struct nfs4_file *fp;
1250
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001251 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07001253 if (fp->fi_inode == ino) {
1254 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001255 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001259 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return NULL;
1261}
1262
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001263static inline int access_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001264{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001265 if (x < NFS4_SHARE_ACCESS_READ)
1266 return 0;
1267 if (x > NFS4_SHARE_ACCESS_BOTH)
1268 return 0;
1269 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001270}
1271
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001272static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001273{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001274 /* Note: unlike access bits, deny bits may be zero. */
1275 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001276}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04001278/*
1279 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1280 * st_{access,deny}_bmap field of the stateid, in order to track not
1281 * only what share bits are currently in force, but also what
1282 * combinations of share bits previous opens have used. This allows us
1283 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1284 * return an error if the client attempt to downgrade to a combination
1285 * of share bits not explicable by closing some of its previous opens.
1286 *
1287 * XXX: This enforcement is actually incomplete, since we don't keep
1288 * track of access/deny bit combinations; so, e.g., we allow:
1289 *
1290 * OPEN allow read, deny write
1291 * OPEN allow both, deny none
1292 * DOWNGRADE allow read, deny none
1293 *
1294 * which we should reject.
1295 */
NeilBrownfd39ca92005-06-23 22:04:03 -07001296static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297set_access(unsigned int *access, unsigned long bmap) {
1298 int i;
1299
1300 *access = 0;
1301 for (i = 1; i < 4; i++) {
1302 if (test_bit(i, &bmap))
1303 *access |= i;
1304 }
1305}
1306
NeilBrownfd39ca92005-06-23 22:04:03 -07001307static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308set_deny(unsigned int *deny, unsigned long bmap) {
1309 int i;
1310
1311 *deny = 0;
1312 for (i = 0; i < 4; i++) {
1313 if (test_bit(i, &bmap))
1314 *deny |= i ;
1315 }
1316}
1317
1318static int
1319test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1320 unsigned int access, deny;
1321
1322 set_access(&access, stp->st_access_bmap);
1323 set_deny(&deny, stp->st_deny_bmap);
1324 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1325 return 0;
1326 return 1;
1327}
1328
1329/*
1330 * Called to check deny when READ with all zero stateid or
1331 * WRITE with all zero or all one stateid
1332 */
Al Virob37ad282006-10-19 23:28:59 -07001333static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1335{
1336 struct inode *ino = current_fh->fh_dentry->d_inode;
1337 struct nfs4_file *fp;
1338 struct nfs4_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07001339 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 dprintk("NFSD: nfs4_share_conflict\n");
1342
1343 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07001344 if (!fp)
1345 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07001346 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07001348 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1349 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1350 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
NeilBrown13cd2182005-06-23 22:03:10 -07001353 ret = nfs_ok;
1354out:
1355 put_nfs4_file(fp);
1356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359static inline void
1360nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1361{
1362 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
Dave Hansenaceaf782008-02-15 14:37:31 -08001363 drop_file_write_access(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1365 }
1366}
1367
1368/*
1369 * Recall a delegation
1370 */
1371static int
1372do_recall(void *__dp)
1373{
1374 struct nfs4_delegation *dp = __dp;
1375
Meelap Shah47f99402007-07-17 04:04:40 -07001376 dp->dl_file->fi_had_conflict = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 nfsd4_cb_recall(dp);
1378 return 0;
1379}
1380
1381/*
1382 * Spawn a thread to perform a recall on the delegation represented
1383 * by the lease (file_lock)
1384 *
1385 * Called from break_lease() with lock_kernel() held.
1386 * Note: we assume break_lease will only call this *once* for any given
1387 * lease.
1388 */
1389static
1390void nfsd_break_deleg_cb(struct file_lock *fl)
1391{
1392 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1393 struct task_struct *t;
1394
1395 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1396 if (!dp)
1397 return;
1398
1399 /* We're assuming the state code never drops its reference
1400 * without first removing the lease. Since we're in this lease
1401 * callback (and since the lease code is serialized by the kernel
1402 * lock) we know the server hasn't removed the lease yet, we know
1403 * it's safe to take a reference: */
1404 atomic_inc(&dp->dl_count);
J. Bruce Fieldscfdcad42007-09-12 20:35:15 -04001405 atomic_inc(&dp->dl_client->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 spin_lock(&recall_lock);
1408 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1409 spin_unlock(&recall_lock);
1410
1411 /* only place dl_time is set. protected by lock_kernel*/
1412 dp->dl_time = get_seconds();
1413
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04001414 /*
1415 * We don't want the locks code to timeout the lease for us;
1416 * we'll remove it ourself if the delegation isn't returned
1417 * in time.
1418 */
1419 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1422 if (IS_ERR(t)) {
1423 struct nfs4_client *clp = dp->dl_client;
1424
1425 printk(KERN_INFO "NFSD: Callback thread failed for "
1426 "for client (clientid %08x/%08x)\n",
1427 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
J. Bruce Fieldscfdcad42007-09-12 20:35:15 -04001428 put_nfs4_client(dp->dl_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 nfs4_put_delegation(dp);
1430 }
1431}
1432
1433/*
1434 * The file_lock is being reapd.
1435 *
1436 * Called by locks_free_lock() with lock_kernel() held.
1437 */
1438static
1439void nfsd_release_deleg_cb(struct file_lock *fl)
1440{
1441 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1442
1443 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1444
1445 if (!(fl->fl_flags & FL_LEASE) || !dp)
1446 return;
1447 dp->dl_flock = NULL;
1448}
1449
1450/*
1451 * Set the delegation file_lock back pointer.
1452 *
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001453 * Called from setlease() with lock_kernel() held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 */
1455static
1456void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1457{
1458 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1459
1460 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1461 if (!dp)
1462 return;
1463 dp->dl_flock = new;
1464}
1465
1466/*
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001467 * Called from setlease() with lock_kernel() held
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 */
1469static
1470int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1471{
1472 struct nfs4_delegation *onlistd =
1473 (struct nfs4_delegation *)onlist->fl_owner;
1474 struct nfs4_delegation *tryd =
1475 (struct nfs4_delegation *)try->fl_owner;
1476
1477 if (onlist->fl_lmops != try->fl_lmops)
1478 return 0;
1479
1480 return onlistd->dl_client == tryd->dl_client;
1481}
1482
1483
1484static
1485int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1486{
1487 if (arg & F_UNLCK)
1488 return lease_modify(onlist, arg);
1489 else
1490 return -EAGAIN;
1491}
1492
NeilBrownfd39ca92005-06-23 22:04:03 -07001493static struct lock_manager_operations nfsd_lease_mng_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 .fl_break = nfsd_break_deleg_cb,
1495 .fl_release_private = nfsd_release_deleg_cb,
1496 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1497 .fl_mylease = nfsd_same_client_deleg_cb,
1498 .fl_change = nfsd_change_deleg_cb,
1499};
1500
1501
Al Virob37ad282006-10-19 23:28:59 -07001502__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503nfsd4_process_open1(struct nfsd4_open *open)
1504{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 clientid_t *clientid = &open->op_clientid;
1506 struct nfs4_client *clp = NULL;
1507 unsigned int strhashval;
1508 struct nfs4_stateowner *sop = NULL;
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001511 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (STALE_CLIENTID(&open->op_clientid))
1514 return nfserr_stale_clientid;
1515
1516 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1517 sop = find_openstateowner_str(strhashval, open);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001518 open->op_stateowner = sop;
1519 if (!sop) {
1520 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 clp = find_confirmed_client(clientid);
1522 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001523 return nfserr_expired;
1524 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001526 if (!sop->so_confirmed) {
1527 /* Replace unconfirmed owners without checking for replay. */
1528 clp = sop->so_client;
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05001529 release_openowner(sop);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001530 open->op_stateowner = NULL;
1531 goto renew;
1532 }
1533 if (open->op_seqid == sop->so_seqid - 1) {
1534 if (sop->so_replay.rp_buflen)
Al Viroa90b0612006-10-19 23:29:03 -07001535 return nfserr_replay_me;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001536 /* The original OPEN failed so spectacularly
1537 * that we don't even have replay data saved!
1538 * Therefore, we have no choice but to continue
1539 * processing this OPEN; presumably, we'll
1540 * fail again for the same reason.
1541 */
1542 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1543 goto renew;
1544 }
1545 if (open->op_seqid != sop->so_seqid)
1546 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547renew:
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001548 if (open->op_stateowner == NULL) {
1549 sop = alloc_init_open_stateowner(strhashval, clp, open);
1550 if (sop == NULL)
1551 return nfserr_resource;
1552 open->op_stateowner = sop;
1553 }
1554 list_del_init(&sop->so_close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 renew_client(sop->so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001556 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Al Virob37ad282006-10-19 23:28:59 -07001559static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07001560nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1561{
1562 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1563 return nfserr_openmode;
1564 else
1565 return nfs_ok;
1566}
1567
NeilBrown52f4fb42005-06-23 22:02:49 -07001568static struct nfs4_delegation *
1569find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1570{
1571 struct nfs4_delegation *dp;
1572
NeilBrownea1da632005-06-23 22:04:17 -07001573 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
NeilBrown52f4fb42005-06-23 22:02:49 -07001574 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1575 return dp;
1576 }
1577 return NULL;
1578}
1579
Al Virob37ad282006-10-19 23:28:59 -07001580static __be32
NeilBrown567d9822005-06-23 22:02:53 -07001581nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1582 struct nfs4_delegation **dp)
1583{
1584 int flags;
Al Virob37ad282006-10-19 23:28:59 -07001585 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07001586
1587 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1588 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07001589 goto out;
NeilBrown567d9822005-06-23 22:02:53 -07001590 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1591 RD_STATE : WR_STATE;
1592 status = nfs4_check_delegmode(*dp, flags);
1593 if (status)
1594 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001595out:
1596 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1597 return nfs_ok;
1598 if (status)
1599 return status;
1600 open->op_stateowner->so_confirmed = 1;
1601 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07001602}
1603
Al Virob37ad282006-10-19 23:28:59 -07001604static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1606{
1607 struct nfs4_stateid *local;
Al Virob37ad282006-10-19 23:28:59 -07001608 __be32 status = nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 struct nfs4_stateowner *sop = open->op_stateowner;
1610
NeilBrown8beefa22005-06-23 22:03:08 -07001611 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* ignore lock owners */
1613 if (local->st_stateowner->so_is_open_owner == 0)
1614 continue;
1615 /* remember if we have seen this open owner */
1616 if (local->st_stateowner == sop)
1617 *stpp = local;
1618 /* check for conflicting share reservations */
1619 if (!test_share(local, open))
1620 goto out;
1621 }
1622 status = 0;
1623out:
1624 return status;
1625}
1626
NeilBrown5ac049a2005-06-23 22:03:03 -07001627static inline struct nfs4_stateid *
1628nfs4_alloc_stateid(void)
1629{
1630 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1631}
1632
Al Virob37ad282006-10-19 23:28:59 -07001633static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
NeilBrown567d9822005-06-23 22:02:53 -07001635 struct nfs4_delegation *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct svc_fh *cur_fh, int flags)
1637{
1638 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
NeilBrown5ac049a2005-06-23 22:03:03 -07001640 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (stp == NULL)
1642 return nfserr_resource;
1643
NeilBrown567d9822005-06-23 22:02:53 -07001644 if (dp) {
1645 get_file(dp->dl_vfs_file);
1646 stp->st_vfs_file = dp->dl_vfs_file;
1647 } else {
Al Virob37ad282006-10-19 23:28:59 -07001648 __be32 status;
NeilBrown567d9822005-06-23 22:02:53 -07001649 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1650 &stp->st_vfs_file);
1651 if (status) {
1652 if (status == nfserr_dropit)
1653 status = nfserr_jukebox;
NeilBrown5ac049a2005-06-23 22:03:03 -07001654 kmem_cache_free(stateid_slab, stp);
NeilBrown567d9822005-06-23 22:02:53 -07001655 return status;
1656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 *stpp = stp;
1659 return 0;
1660}
1661
Al Virob37ad282006-10-19 23:28:59 -07001662static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1664 struct nfsd4_open *open)
1665{
1666 struct iattr iattr = {
1667 .ia_valid = ATTR_SIZE,
1668 .ia_size = 0,
1669 };
1670 if (!open->op_truncate)
1671 return 0;
1672 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08001673 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1675}
1676
Al Virob37ad282006-10-19 23:28:59 -07001677static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1679{
1680 struct file *filp = stp->st_vfs_file;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001681 struct inode *inode = filp->f_path.dentry->d_inode;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001682 unsigned int share_access, new_writer;
Al Virob37ad282006-10-19 23:28:59 -07001683 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 set_access(&share_access, stp->st_access_bmap);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001686 new_writer = (~share_access) & open->op_share_access
1687 & NFS4_SHARE_ACCESS_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001689 if (new_writer) {
Al Virob37ad282006-10-19 23:28:59 -07001690 int err = get_write_access(inode);
1691 if (err)
1692 return nfserrno(err);
Benny Halevye518f052008-07-04 15:38:41 +03001693 err = mnt_want_write(cur_fh->fh_export->ex_path.mnt);
1694 if (err)
1695 return nfserrno(err);
1696 file_take_write(filp);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 status = nfsd4_truncate(rqstp, cur_fh, open);
1699 if (status) {
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001700 if (new_writer)
1701 put_write_access(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return status;
1703 }
1704 /* remember the open */
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001705 filp->f_mode |= open->op_share_access;
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04001706 __set_bit(open->op_share_access, &stp->st_access_bmap);
1707 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 return nfs_ok;
1710}
1711
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713static void
NeilBrown37515172005-07-07 17:59:16 -07001714nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
NeilBrown37515172005-07-07 17:59:16 -07001716 open->op_stateowner->so_confirmed = 1;
1717 open->op_stateowner->so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720/*
1721 * Attempt to hand out a delegation.
1722 */
1723static void
1724nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1725{
1726 struct nfs4_delegation *dp;
1727 struct nfs4_stateowner *sop = stp->st_stateowner;
1728 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1729 struct file_lock fl, *flp = &fl;
1730 int status, flag = 0;
1731
1732 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07001733 open->op_recall = 0;
1734 switch (open->op_claim_type) {
1735 case NFS4_OPEN_CLAIM_PREVIOUS:
1736 if (!atomic_read(&cb->cb_set))
1737 open->op_recall = 1;
1738 flag = open->op_delegate_type;
1739 if (flag == NFS4_OPEN_DELEGATE_NONE)
1740 goto out;
1741 break;
1742 case NFS4_OPEN_CLAIM_NULL:
1743 /* Let's not give out any delegations till everyone's
1744 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001745 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07001746 goto out;
1747 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1748 goto out;
1749 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1750 flag = NFS4_OPEN_DELEGATE_WRITE;
1751 else
1752 flag = NFS4_OPEN_DELEGATE_READ;
1753 break;
1754 default:
1755 goto out;
1756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1759 if (dp == NULL) {
1760 flag = NFS4_OPEN_DELEGATE_NONE;
1761 goto out;
1762 }
1763 locks_init_lock(&fl);
1764 fl.fl_lmops = &nfsd_lease_mng_ops;
1765 fl.fl_flags = FL_LEASE;
Felix Blyakher9167f502008-02-26 10:54:36 -08001766 fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 fl.fl_end = OFFSET_MAX;
1768 fl.fl_owner = (fl_owner_t)dp;
1769 fl.fl_file = stp->st_vfs_file;
1770 fl.fl_pid = current->tgid;
1771
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001772 /* vfs_setlease checks to see if delegation should be handed out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 * the lock_manager callbacks fl_mylease and fl_change are used
1774 */
Felix Blyakher9167f502008-02-26 10:54:36 -08001775 if ((status = vfs_setlease(stp->st_vfs_file, fl.fl_type, &flp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
NeilBrownc9071322005-04-16 15:26:38 -07001777 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 flag = NFS4_OPEN_DELEGATE_NONE;
1779 goto out;
1780 }
1781
1782 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1783
1784 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1785 dp->dl_stateid.si_boot,
1786 dp->dl_stateid.si_stateownerid,
1787 dp->dl_stateid.si_fileid,
1788 dp->dl_stateid.si_generation);
1789out:
NeilBrown7b190fe2005-06-23 22:03:23 -07001790 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1791 && flag == NFS4_OPEN_DELEGATE_NONE
1792 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001793 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 open->op_delegate_type = flag;
1795}
1796
1797/*
1798 * called with nfs4_lock_state() held.
1799 */
Al Virob37ad282006-10-19 23:28:59 -07001800__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1802{
1803 struct nfs4_file *fp = NULL;
1804 struct inode *ino = current_fh->fh_dentry->d_inode;
1805 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07001806 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07001807 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 status = nfserr_inval;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001810 if (!access_valid(open->op_share_access)
1811 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto out;
1813 /*
1814 * Lookup file; if found, lookup stateid and check open request,
1815 * and check for delegations in the process of being recalled.
1816 * If not found, create the nfs4_file struct
1817 */
1818 fp = find_file(ino);
1819 if (fp) {
1820 if ((status = nfs4_check_open(fp, open, &stp)))
1821 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001822 status = nfs4_check_deleg(fp, open, &dp);
1823 if (status)
1824 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07001826 status = nfserr_bad_stateid;
1827 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1828 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 status = nfserr_resource;
1830 fp = alloc_init_file(ino);
1831 if (fp == NULL)
1832 goto out;
1833 }
1834
1835 /*
1836 * OPEN the file, or upgrade an existing OPEN.
1837 * If truncate fails, the OPEN fails.
1838 */
1839 if (stp) {
1840 /* Stateid was found, this is an OPEN upgrade */
1841 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1842 if (status)
1843 goto out;
NeilBrown444c2c02005-07-07 17:59:22 -07001844 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 } else {
1846 /* Stateid was not found, this is a new OPEN */
1847 int flags = 0;
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -07001848 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001849 flags |= NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001851 flags |= NFSD_MAY_WRITE;
NeilBrown567d9822005-06-23 22:02:53 -07001852 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1853 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto out;
1855 init_stateid(stp, fp, open);
1856 status = nfsd4_truncate(rqstp, current_fh, open);
1857 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05001858 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 goto out;
1860 }
1861 }
1862 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1863
1864 /*
1865 * Attempt to hand out a delegation. No error return, because the
1866 * OPEN succeeds even if we fail.
1867 */
1868 nfs4_open_delegation(current_fh, open, stp);
1869
1870 status = nfs_ok;
1871
1872 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1873 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1874 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1875out:
NeilBrown13cd2182005-06-23 22:03:10 -07001876 if (fp)
1877 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07001878 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1879 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /*
1881 * To finish the open response, we just need to set the rflags.
1882 */
1883 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1884 if (!open->op_stateowner->so_confirmed)
1885 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1886
1887 return status;
1888}
1889
Al Virob37ad282006-10-19 23:28:59 -07001890__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001891nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1892 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07001895 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 nfs4_lock_state();
1898 dprintk("process_renew(%08x/%08x): starting\n",
1899 clid->cl_boot, clid->cl_id);
1900 status = nfserr_stale_clientid;
1901 if (STALE_CLIENTID(clid))
1902 goto out;
1903 clp = find_confirmed_client(clid);
1904 status = nfserr_expired;
1905 if (clp == NULL) {
1906 /* We assume the client took too long to RENEW. */
1907 dprintk("nfsd4_renew: clientid not found!\n");
1908 goto out;
1909 }
1910 renew_client(clp);
1911 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07001912 if (!list_empty(&clp->cl_delegations)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 && !atomic_read(&clp->cl_callback.cb_set))
1914 goto out;
1915 status = nfs_ok;
1916out:
1917 nfs4_unlock_state();
1918 return status;
1919}
1920
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001921struct lock_manager nfsd4_manager = {
1922};
1923
NeilBrowna76b4312005-06-23 22:04:01 -07001924static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001925nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07001926{
1927 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07001928 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001929 locks_end_grace(&nfsd4_manager);
NeilBrowna76b4312005-06-23 22:04:01 -07001930}
1931
NeilBrownfd39ca92005-06-23 22:04:03 -07001932static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933nfs4_laundromat(void)
1934{
1935 struct nfs4_client *clp;
1936 struct nfs4_stateowner *sop;
1937 struct nfs4_delegation *dp;
1938 struct list_head *pos, *next, reaplist;
1939 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1940 time_t t, clientid_val = NFSD_LEASE_TIME;
1941 time_t u, test_val = NFSD_LEASE_TIME;
1942
1943 nfs4_lock_state();
1944
1945 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001946 if (locks_in_grace())
1947 nfsd4_end_grace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 list_for_each_safe(pos, next, &client_lru) {
1949 clp = list_entry(pos, struct nfs4_client, cl_lru);
1950 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1951 t = clp->cl_time - cutoff;
1952 if (clientid_val > t)
1953 clientid_val = t;
1954 break;
1955 }
1956 dprintk("NFSD: purging unused client (clientid %08x)\n",
1957 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07001958 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 expire_client(clp);
1960 }
1961 INIT_LIST_HEAD(&reaplist);
1962 spin_lock(&recall_lock);
1963 list_for_each_safe(pos, next, &del_recall_lru) {
1964 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1965 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1966 u = dp->dl_time - cutoff;
1967 if (test_val > u)
1968 test_val = u;
1969 break;
1970 }
1971 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1972 dp, dp->dl_flock);
1973 list_move(&dp->dl_recall_lru, &reaplist);
1974 }
1975 spin_unlock(&recall_lock);
1976 list_for_each_safe(pos, next, &reaplist) {
1977 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1978 list_del_init(&dp->dl_recall_lru);
1979 unhash_delegation(dp);
1980 }
1981 test_val = NFSD_LEASE_TIME;
1982 list_for_each_safe(pos, next, &close_lru) {
1983 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1984 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1985 u = sop->so_time - cutoff;
1986 if (test_val > u)
1987 test_val = u;
1988 break;
1989 }
1990 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1991 sop->so_id);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05001992 release_openowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1995 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1996 nfs4_unlock_state();
1997 return clientid_val;
1998}
1999
Harvey Harrisona254b242008-02-20 12:49:00 -08002000static struct workqueue_struct *laundry_wq;
2001static void laundromat_main(struct work_struct *);
2002static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
2003
2004static void
David Howellsc4028952006-11-22 14:57:56 +00002005laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 time_t t;
2008
2009 t = nfs4_laundromat();
2010 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07002011 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
NeilBrownfd39ca92005-06-23 22:04:03 -07002014static struct nfs4_stateowner *
NeilBrownf8816512005-07-07 17:59:25 -07002015search_close_lru(u32 st_id, int flags)
2016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct nfs4_stateowner *local = NULL;
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (flags & CLOSE_STATE) {
2020 list_for_each_entry(local, &close_lru, so_close_lru) {
2021 if (local->so_id == st_id)
2022 return local;
2023 }
2024 }
2025 return NULL;
2026}
2027
2028static inline int
2029nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2030{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08002031 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034static int
2035STALE_STATEID(stateid_t *stateid)
2036{
2037 if (stateid->si_boot == boot_time)
2038 return 0;
Neil Brown849823c2005-09-13 01:25:36 -07002039 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2041 stateid->si_generation);
2042 return 1;
2043}
2044
2045static inline int
2046access_permit_read(unsigned long access_bmap)
2047{
2048 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2049 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2050 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2051}
2052
2053static inline int
2054access_permit_write(unsigned long access_bmap)
2055{
2056 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2057 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2058}
2059
2060static
Al Virob37ad282006-10-19 23:28:59 -07002061__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Al Virob37ad282006-10-19 23:28:59 -07002063 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2066 goto out;
2067 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2068 goto out;
2069 status = nfs_ok;
2070out:
2071 return status;
2072}
2073
Al Virob37ad282006-10-19 23:28:59 -07002074static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2076{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002077 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002079 else if (locks_in_grace()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* Answer in remaining cases depends on existance of
2081 * conflicting state; so we must wait out the grace period. */
2082 return nfserr_grace;
2083 } else if (flags & WR_STATE)
2084 return nfs4_share_conflict(current_fh,
2085 NFS4_SHARE_DENY_WRITE);
2086 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2087 return nfs4_share_conflict(current_fh,
2088 NFS4_SHARE_DENY_READ);
2089}
2090
2091/*
2092 * Allow READ/WRITE during grace period on recovered state only for files
2093 * that are not able to provide mandatory locking.
2094 */
2095static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08002096grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002098 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
J. Bruce Fields0836f582008-01-26 19:08:12 -05002101static int check_stateid_generation(stateid_t *in, stateid_t *ref)
2102{
2103 /* If the client sends us a stateid from the future, it's buggy: */
2104 if (in->si_generation > ref->si_generation)
2105 return nfserr_bad_stateid;
2106 /*
2107 * The following, however, can happen. For example, if the
2108 * client sends an open and some IO at the same time, the open
2109 * may bump si_generation while the IO is still in flight.
2110 * Thanks to hard links and renames, the client never knows what
2111 * file an open will affect. So it could avoid that situation
2112 * only by serializing all opens and IO from the same open
2113 * owner. To recover from the old_stateid error, the client
2114 * will just have to retry the IO:
2115 */
2116 if (in->si_generation < ref->si_generation)
2117 return nfserr_old_stateid;
2118 return nfs_ok;
2119}
2120
J. Bruce Fields3e633072009-02-21 13:17:19 -08002121static int is_delegation_stateid(stateid_t *stateid)
2122{
2123 return stateid->si_fileid == 0;
2124}
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126/*
2127* Checks for stateid operations
2128*/
Al Virob37ad282006-10-19 23:28:59 -07002129__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2131{
2132 struct nfs4_stateid *stp = NULL;
2133 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07002135 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (filpp)
2138 *filpp = NULL;
2139
J. Bruce Fields18f82732009-02-21 15:23:01 -08002140 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return nfserr_grace;
2142
2143 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2144 return check_special_stateids(current_fh, stateid, flags);
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 status = nfserr_stale_stateid;
2147 if (STALE_STATEID(stateid))
2148 goto out;
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 status = nfserr_bad_stateid;
J. Bruce Fields3e633072009-02-21 13:17:19 -08002151 if (is_delegation_stateid(stateid)) {
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08002152 dp = find_delegation_stateid(ino, stateid);
J. Bruce Fields819a8f52009-02-21 12:13:24 -08002153 if (!dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 goto out;
J. Bruce Fieldsfd03b092009-02-21 11:27:30 -08002155 status = check_stateid_generation(stateid, &dp->dl_stateid);
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08002156 if (status)
2157 goto out;
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08002158 status = nfs4_check_delegmode(dp, flags);
2159 if (status)
2160 goto out;
2161 renew_client(dp->dl_client);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08002162 if (filpp)
2163 *filpp = dp->dl_vfs_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 } else { /* open or lock stateid */
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08002165 stp = find_stateid(stateid, flags);
J. Bruce Fields819a8f52009-02-21 12:13:24 -08002166 if (!stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 goto out;
J. Bruce Fields6150ef02009-02-21 13:36:16 -08002168 if (nfs4_check_fh(current_fh, stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto out;
2170 if (!stp->st_stateowner->so_confirmed)
2171 goto out;
J. Bruce Fieldsfd03b092009-02-21 11:27:30 -08002172 status = check_stateid_generation(stateid, &stp->st_stateid);
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08002173 if (status)
2174 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08002175 status = nfs4_check_openmode(stp, flags);
2176 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 goto out;
2178 renew_client(stp->st_stateowner->so_client);
2179 if (filpp)
2180 *filpp = stp->st_vfs_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 }
2182 status = nfs_ok;
2183out:
2184 return status;
2185}
2186
NeilBrown4c4cd222005-07-07 17:59:27 -07002187static inline int
2188setlkflg (int type)
2189{
2190 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2191 RD_STATE : WR_STATE;
2192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194/*
2195 * Checks for sequence id mutating operations.
2196 */
Al Virob37ad282006-10-19 23:28:59 -07002197static __be32
NeilBrown4c4cd222005-07-07 17:59:27 -07002198nfs4_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 -07002199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 struct nfs4_stateid *stp;
2201 struct nfs4_stateowner *sop;
J. Bruce Fields0836f582008-01-26 19:08:12 -05002202 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2205 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2206 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2207 stateid->si_generation);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 *stpp = NULL;
2210 *sopp = NULL;
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002213 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002214 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (STALE_STATEID(stateid))
NeilBrown3a4f98b2005-07-07 17:59:26 -07002218 return nfserr_stale_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 /*
2220 * We return BAD_STATEID if filehandle doesn't match stateid,
2221 * the confirmed flag is incorrecly set, or the generation
2222 * number is incorrect.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 */
NeilBrownf8816512005-07-07 17:59:25 -07002224 stp = find_stateid(stateid, flags);
2225 if (stp == NULL) {
2226 /*
2227 * Also, we should make sure this isn't just the result of
2228 * a replayed close:
2229 */
2230 sop = search_close_lru(stateid->si_stateownerid, flags);
2231 if (sop == NULL)
2232 return nfserr_bad_stateid;
2233 *sopp = sop;
2234 goto check_replay;
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
J. Bruce Fields39325bd2007-11-26 17:06:39 -05002237 *stpp = stp;
2238 *sopp = sop = stp->st_stateowner;
2239
NeilBrown4c4cd222005-07-07 17:59:27 -07002240 if (lock) {
NeilBrown4c4cd222005-07-07 17:59:27 -07002241 clientid_t *lockclid = &lock->v.new.clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 struct nfs4_client *clp = sop->so_client;
NeilBrown4c4cd222005-07-07 17:59:27 -07002243 int lkflg = 0;
Al Virob37ad282006-10-19 23:28:59 -07002244 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
NeilBrown4c4cd222005-07-07 17:59:27 -07002246 lkflg = setlkflg(lock->lk_type);
2247
2248 if (lock->lk_is_new) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002249 if (!sop->so_is_open_owner)
2250 return nfserr_bad_stateid;
2251 if (!same_clid(&clp->cl_clientid, lockclid))
NeilBrown4c4cd222005-07-07 17:59:27 -07002252 return nfserr_bad_stateid;
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002253 /* stp is the open stateid */
2254 status = nfs4_check_openmode(stp, lkflg);
2255 if (status)
2256 return status;
2257 } else {
2258 /* stp is the lock stateid */
2259 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2260 if (status)
2261 return status;
NeilBrown4c4cd222005-07-07 17:59:27 -07002262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002265 if (nfs4_check_fh(current_fh, stp)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002266 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002267 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 /*
2271 * We now validate the seqid and stateid generation numbers.
2272 * For the moment, we ignore the possibility of
2273 * generation number wraparound.
2274 */
NeilBrownbd9aac52005-07-07 17:59:19 -07002275 if (seqid != sop->so_seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto check_replay;
2277
NeilBrown3a4f98b2005-07-07 17:59:26 -07002278 if (sop->so_confirmed && flags & CONFIRM) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002279 dprintk("NFSD: preprocess_seqid_op: expected"
NeilBrown3a4f98b2005-07-07 17:59:26 -07002280 " unconfirmed stateowner!\n");
2281 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07002283 if (!sop->so_confirmed && !(flags & CONFIRM)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002284 dprintk("NFSD: preprocess_seqid_op: stateowner not"
NeilBrown3a4f98b2005-07-07 17:59:26 -07002285 " confirmed yet!\n");
2286 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
J. Bruce Fields0836f582008-01-26 19:08:12 -05002288 status = check_stateid_generation(stateid, &stp->st_stateid);
2289 if (status)
2290 return status;
NeilBrown52fd0042005-07-07 17:59:24 -07002291 renew_client(sop->so_client);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002292 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294check_replay:
NeilBrownbd9aac52005-07-07 17:59:19 -07002295 if (seqid == sop->so_seqid - 1) {
Neil Brown849823c2005-09-13 01:25:36 -07002296 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 /* indicate replay to calling function */
Al Viroa90b0612006-10-19 23:29:03 -07002298 return nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002300 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
NeilBrown3a4f98b2005-07-07 17:59:26 -07002301 sop->so_seqid, seqid);
2302 *sopp = NULL;
2303 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Al Virob37ad282006-10-19 23:28:59 -07002306__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002307nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002308 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Al Virob37ad282006-10-19 23:28:59 -07002310 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 struct nfs4_stateowner *sop;
2312 struct nfs4_stateid *stp;
2313
2314 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002315 (int)cstate->current_fh.fh_dentry->d_name.len,
2316 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002318 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07002319 if (status)
2320 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 nfs4_lock_state();
2323
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002324 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2325 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002326 CONFIRM | OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 &oc->oc_stateowner, &stp, NULL)))
2328 goto out;
2329
2330 sop = oc->oc_stateowner;
2331 sop->so_confirmed = 1;
2332 update_stateid(&stp->st_stateid);
2333 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2334 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2335 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2336 stp->st_stateid.si_boot,
2337 stp->st_stateid.si_stateownerid,
2338 stp->st_stateid.si_fileid,
2339 stp->st_stateid.si_generation);
NeilBrownc7b9a452005-06-23 22:04:30 -07002340
2341 nfsd4_create_clid_dir(sop->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342out:
Neil Brownf2327d92005-09-13 01:25:37 -07002343 if (oc->oc_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 nfs4_get_stateowner(oc->oc_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002345 cstate->replay_owner = oc->oc_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 nfs4_unlock_state();
2348 return status;
2349}
2350
2351
2352/*
2353 * unset all bits in union bitmap (bmap) that
2354 * do not exist in share (from successful OPEN_DOWNGRADE)
2355 */
2356static void
2357reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2358{
2359 int i;
2360 for (i = 1; i < 4; i++) {
2361 if ((i & access) != i)
2362 __clear_bit(i, bmap);
2363 }
2364}
2365
2366static void
2367reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2368{
2369 int i;
2370 for (i = 0; i < 4; i++) {
2371 if ((i & deny) != i)
2372 __clear_bit(i, bmap);
2373 }
2374}
2375
Al Virob37ad282006-10-19 23:28:59 -07002376__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002377nfsd4_open_downgrade(struct svc_rqst *rqstp,
2378 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002379 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Al Virob37ad282006-10-19 23:28:59 -07002381 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 struct nfs4_stateid *stp;
2383 unsigned int share_access;
2384
2385 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002386 (int)cstate->current_fh.fh_dentry->d_name.len,
2387 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002389 if (!access_valid(od->od_share_access)
2390 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return nfserr_inval;
2392
2393 nfs4_lock_state();
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002394 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2395 od->od_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 &od->od_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002397 OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 &od->od_stateowner, &stp, NULL)))
2399 goto out;
2400
2401 status = nfserr_inval;
2402 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2403 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2404 stp->st_access_bmap, od->od_share_access);
2405 goto out;
2406 }
2407 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2408 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2409 stp->st_deny_bmap, od->od_share_deny);
2410 goto out;
2411 }
2412 set_access(&share_access, stp->st_access_bmap);
2413 nfs4_file_downgrade(stp->st_vfs_file,
2414 share_access & ~od->od_share_access);
2415
2416 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2417 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2418
2419 update_stateid(&stp->st_stateid);
2420 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2421 status = nfs_ok;
2422out:
Neil Brownf2327d92005-09-13 01:25:37 -07002423 if (od->od_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 nfs4_get_stateowner(od->od_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002425 cstate->replay_owner = od->od_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 nfs4_unlock_state();
2428 return status;
2429}
2430
2431/*
2432 * nfs4_unlock_state() called after encode
2433 */
Al Virob37ad282006-10-19 23:28:59 -07002434__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002435nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002436 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Al Virob37ad282006-10-19 23:28:59 -07002438 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct nfs4_stateid *stp;
2440
2441 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002442 (int)cstate->current_fh.fh_dentry->d_name.len,
2443 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 nfs4_lock_state();
2446 /* check close_lru for replay */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002447 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2448 close->cl_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 &close->cl_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002450 OPEN_STATE | CLOSE_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 &close->cl_stateowner, &stp, NULL)))
2452 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 status = nfs_ok;
2454 update_stateid(&stp->st_stateid);
2455 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2456
J. Bruce Fields04ef5952006-01-18 17:43:21 -08002457 /* release_stateid() calls nfsd_close() if needed */
J. Bruce Fields22839632009-01-11 14:27:17 -05002458 release_open_stateid(stp);
J. Bruce Fields04ef5952006-01-18 17:43:21 -08002459
2460 /* place unused nfs4_stateowners on so_close_lru list to be
2461 * released by the laundromat service after the lease period
2462 * to enable us to handle CLOSE replay
2463 */
2464 if (list_empty(&close->cl_stateowner->so_stateids))
2465 move_to_close_lru(close->cl_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466out:
Neil Brownf2327d92005-09-13 01:25:37 -07002467 if (close->cl_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 nfs4_get_stateowner(close->cl_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002469 cstate->replay_owner = close->cl_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 nfs4_unlock_state();
2472 return status;
2473}
2474
Al Virob37ad282006-10-19 23:28:59 -07002475__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002476nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2477 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002479 struct nfs4_delegation *dp;
2480 stateid_t *stateid = &dr->dr_stateid;
2481 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07002482 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002484 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002485 return status;
2486 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 nfs4_lock_state();
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002489 status = nfserr_bad_stateid;
2490 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2491 goto out;
2492 status = nfserr_stale_stateid;
2493 if (STALE_STATEID(stateid))
2494 goto out;
J. Bruce Fields7e0f7cf2009-02-21 13:32:28 -08002495 status = nfserr_bad_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002496 if (!is_delegation_stateid(stateid))
2497 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002498 dp = find_delegation_stateid(inode, stateid);
2499 if (!dp)
2500 goto out;
2501 status = check_stateid_generation(stateid, &dp->dl_stateid);
2502 if (status)
2503 goto out;
2504 renew_client(dp->dl_client);
2505
2506 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002508 nfs4_unlock_state();
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return status;
2511}
2512
2513
2514/*
2515 * Lock owner state (byte-range locks)
2516 */
2517#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2518#define LOCK_HASH_BITS 8
2519#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2520#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2521
Benny Halevy87df4de2008-12-15 19:42:03 +02002522static inline u64
2523end_offset(u64 start, u64 len)
2524{
2525 u64 end;
2526
2527 end = start + len;
2528 return end >= start ? end: NFS4_MAX_UINT64;
2529}
2530
2531/* last octet in a range */
2532static inline u64
2533last_byte_offset(u64 start, u64 len)
2534{
2535 u64 end;
2536
2537 BUG_ON(!len);
2538 end = start + len;
2539 return end > start ? end - 1: NFS4_MAX_UINT64;
2540}
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542#define lockownerid_hashval(id) \
2543 ((id) & LOCK_HASH_MASK)
2544
2545static inline unsigned int
2546lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2547 struct xdr_netobj *ownername)
2548{
2549 return (file_hashval(inode) + cl_id
2550 + opaque_hashval(ownername->data, ownername->len))
2551 & LOCK_HASH_MASK;
2552}
2553
2554static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2555static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2556static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2557
NeilBrownfd39ca92005-06-23 22:04:03 -07002558static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559find_stateid(stateid_t *stid, int flags)
2560{
Krishna Kumar9346eff2008-10-20 11:44:28 +05302561 struct nfs4_stateid *local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 u32 st_id = stid->si_stateownerid;
2563 u32 f_id = stid->si_fileid;
2564 unsigned int hashval;
2565
2566 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
Krishna Kumar9346eff2008-10-20 11:44:28 +05302567 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 hashval = stateid_hashval(st_id, f_id);
2569 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2570 if ((local->st_stateid.si_stateownerid == st_id) &&
2571 (local->st_stateid.si_fileid == f_id))
2572 return local;
2573 }
2574 }
Krishna Kumar9346eff2008-10-20 11:44:28 +05302575
2576 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 hashval = stateid_hashval(st_id, f_id);
2578 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2579 if ((local->st_stateid.si_stateownerid == st_id) &&
2580 (local->st_stateid.si_fileid == f_id))
2581 return local;
2582 }
Neil Brown849823c2005-09-13 01:25:36 -07002583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return NULL;
2585}
2586
2587static struct nfs4_delegation *
2588find_delegation_stateid(struct inode *ino, stateid_t *stid)
2589{
NeilBrown13cd2182005-06-23 22:03:10 -07002590 struct nfs4_file *fp;
2591 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2594 stid->si_boot, stid->si_stateownerid,
2595 stid->si_fileid, stid->si_generation);
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002598 if (!fp)
2599 return NULL;
2600 dl = find_delegation_file(fp, stid);
2601 put_nfs4_file(fp);
2602 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
2605/*
2606 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2607 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2608 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2609 * locking, this prevents us from being completely protocol-compliant. The
2610 * real solution to this problem is to start using unsigned file offsets in
2611 * the VFS, but this is a very deep change!
2612 */
2613static inline void
2614nfs4_transform_lock_offset(struct file_lock *lock)
2615{
2616 if (lock->fl_start < 0)
2617 lock->fl_start = OFFSET_MAX;
2618 if (lock->fl_end < 0)
2619 lock->fl_end = OFFSET_MAX;
2620}
2621
NeilBrownd5b90262006-04-10 22:55:22 -07002622/* Hack!: For now, we're defining this just so we can use a pointer to it
2623 * as a unique cookie to identify our (NFSv4's) posix locks. */
Adrian Bunke465a772006-04-10 22:55:23 -07002624static struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07002625};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627static inline void
2628nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2629{
NeilBrownd5b90262006-04-10 22:55:22 -07002630 struct nfs4_stateowner *sop;
2631 unsigned int hval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
NeilBrownd5b90262006-04-10 22:55:22 -07002633 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2634 sop = (struct nfs4_stateowner *) fl->fl_owner;
2635 hval = lockownerid_hashval(sop->so_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 kref_get(&sop->so_ref);
2637 deny->ld_sop = sop;
2638 deny->ld_clientid = sop->so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07002639 } else {
2640 deny->ld_sop = NULL;
2641 deny->ld_clientid.cl_boot = 0;
2642 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
2644 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02002645 deny->ld_length = NFS4_MAX_UINT64;
2646 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2648 deny->ld_type = NFS4_READ_LT;
2649 if (fl->fl_type != F_RDLCK)
2650 deny->ld_type = NFS4_WRITE_LT;
2651}
2652
2653static struct nfs4_stateowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2655 struct xdr_netobj *owner)
2656{
2657 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2658 struct nfs4_stateowner *op;
2659
2660 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002661 if (same_owner_str(op, owner, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return op;
2663 }
2664 return NULL;
2665}
2666
2667/*
2668 * Alloc a lock owner structure.
2669 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2670 * occured.
2671 *
2672 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 */
2674
2675static struct nfs4_stateowner *
2676alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2677 struct nfs4_stateowner *sop;
2678 struct nfs4_replay *rp;
2679 unsigned int idhashval;
2680
2681 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2682 return NULL;
2683 idhashval = lockownerid_hashval(current_ownerid);
2684 INIT_LIST_HEAD(&sop->so_idhash);
2685 INIT_LIST_HEAD(&sop->so_strhash);
2686 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07002687 INIT_LIST_HEAD(&sop->so_stateids);
2688 INIT_LIST_HEAD(&sop->so_perstateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2690 sop->so_time = 0;
2691 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2692 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07002693 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 sop->so_is_open_owner = 0;
2695 sop->so_id = current_ownerid++;
2696 sop->so_client = clp;
Neil Brownb59e3c02005-09-13 01:25:38 -07002697 /* It is the openowner seqid that will be incremented in encode in the
2698 * case of new lockowners; so increment the lock seqid manually: */
2699 sop->so_seqid = lock->lk_new_lock_seqid + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 sop->so_confirmed = 1;
2701 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08002702 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 rp->rp_buflen = 0;
2704 rp->rp_buf = rp->rp_ibuf;
2705 return sop;
2706}
2707
NeilBrownfd39ca92005-06-23 22:04:03 -07002708static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2710{
2711 struct nfs4_stateid *stp;
2712 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2713
NeilBrown5ac049a2005-06-23 22:03:03 -07002714 stp = nfs4_alloc_stateid();
2715 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 goto out;
2717 INIT_LIST_HEAD(&stp->st_hash);
2718 INIT_LIST_HEAD(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07002719 INIT_LIST_HEAD(&stp->st_perstateowner);
2720 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07002722 list_add(&stp->st_perfile, &fp->fi_stateids);
NeilBrownea1da632005-06-23 22:04:17 -07002723 list_add(&stp->st_perstateowner, &sop->so_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07002725 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 stp->st_file = fp;
2727 stp->st_stateid.si_boot = boot_time;
2728 stp->st_stateid.si_stateownerid = sop->so_id;
2729 stp->st_stateid.si_fileid = fp->fi_id;
2730 stp->st_stateid.si_generation = 0;
2731 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2732 stp->st_access_bmap = open_stp->st_access_bmap;
2733 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07002734 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736out:
2737 return stp;
2738}
2739
NeilBrownfd39ca92005-06-23 22:04:03 -07002740static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741check_lock_length(u64 offset, u64 length)
2742{
Benny Halevy87df4de2008-12-15 19:42:03 +02002743 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 LOFF_OVERFLOW(offset, length)));
2745}
2746
2747/*
2748 * LOCK operation
2749 */
Al Virob37ad282006-10-19 23:28:59 -07002750__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002751nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002752 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
NeilBrown3e9e3db2005-06-23 22:04:20 -07002754 struct nfs4_stateowner *open_sop = NULL;
Neil Brownb59e3c02005-09-13 01:25:38 -07002755 struct nfs4_stateowner *lock_sop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 struct nfs4_stateid *lock_stp;
2757 struct file *filp;
2758 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05002759 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07002760 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 unsigned int strhashval;
Marc Eshel150b3932007-01-18 16:15:35 -05002762 unsigned int cmd;
Al Virob8dd7b92006-10-19 23:29:01 -07002763 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2766 (long long) lock->lk_offset,
2767 (long long) lock->lk_length);
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (check_lock_length(lock->lk_offset, lock->lk_length))
2770 return nfserr_inval;
2771
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002772 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002773 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08002774 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2775 return status;
2776 }
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 nfs4_lock_state();
2779
2780 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07002781 /*
2782 * Client indicates that this is a new lockowner.
2783 * Use open owner and open stateid to create lock owner and
2784 * lock stateid.
2785 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 struct nfs4_stateid *open_stp = NULL;
2787 struct nfs4_file *fp;
2788
2789 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002790 if (STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 /* validate and update open stateid and open seqid */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002794 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 lock->lk_new_open_seqid,
2796 &lock->lk_new_open_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002797 OPEN_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002798 &lock->lk_replay_owner, &open_stp,
Neil Brownb59e3c02005-09-13 01:25:38 -07002799 lock);
NeilBrown37515172005-07-07 17:59:16 -07002800 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002802 open_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 /* create lockowner and lock stateid */
2804 fp = open_stp->st_file;
2805 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2806 open_sop->so_client->cl_clientid.cl_id,
2807 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07002808 /* XXX: Do we need to check for duplicate stateowners on
2809 * the same file, or should they just be allowed (and
2810 * create new stateids)? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 status = nfserr_resource;
Neil Brownb59e3c02005-09-13 01:25:38 -07002812 lock_sop = alloc_init_lock_stateowner(strhashval,
2813 open_sop->so_client, open_stp, lock);
2814 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07002816 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08002817 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 } else {
2820 /* lock (lock owner + lock stateid) already exists */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002821 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 lock->lk_old_lock_seqid,
2823 &lock->lk_old_lock_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002824 LOCK_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002825 &lock->lk_replay_owner, &lock_stp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (status)
2827 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002828 lock_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
J. Bruce Fields3a655882006-01-18 17:43:19 -08002830 /* lock->lk_replay_owner and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 filp = lock_stp->st_vfs_file;
2832
NeilBrown0dd395d2005-07-07 17:59:15 -07002833 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002834 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07002835 goto out;
2836 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002837 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07002838 goto out;
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 locks_init_lock(&file_lock);
2841 switch (lock->lk_type) {
2842 case NFS4_READ_LT:
2843 case NFS4_READW_LT:
2844 file_lock.fl_type = F_RDLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05002845 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 break;
2847 case NFS4_WRITE_LT:
2848 case NFS4_WRITEW_LT:
2849 file_lock.fl_type = F_WRLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05002850 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 break;
2852 default:
2853 status = nfserr_inval;
2854 goto out;
2855 }
Neil Brownb59e3c02005-09-13 01:25:38 -07002856 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 file_lock.fl_pid = current->tgid;
2858 file_lock.fl_file = filp;
2859 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002860 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02002863 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 nfs4_transform_lock_offset(&file_lock);
2865
2866 /*
2867 * Try to lock the file in the VFS.
2868 * Note: locks.c uses the BKL to protect the inode's lock list.
2869 */
2870
Marc Eshelfd85b812006-11-28 16:26:41 -05002871 err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07002872 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 case 0: /* success! */
2874 update_stateid(&lock_stp->st_stateid);
2875 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2876 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07002877 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002878 break;
2879 case (EAGAIN): /* conflock holds conflicting lock */
2880 status = nfserr_denied;
2881 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2882 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2883 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 case (EDEADLK):
2885 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05002888 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002889 status = nfserr_resource;
2890 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08002893 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05002894 release_lockowner(lock_sop);
J. Bruce Fields3a655882006-01-18 17:43:19 -08002895 if (lock->lk_replay_owner) {
2896 nfs4_get_stateowner(lock->lk_replay_owner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002897 cstate->replay_owner = lock->lk_replay_owner;
Neil Brownf2327d92005-09-13 01:25:37 -07002898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 nfs4_unlock_state();
2900 return status;
2901}
2902
2903/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08002904 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
2905 * so we do a temporary open here just to get an open file to pass to
2906 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
2907 * inode operation.)
2908 */
2909static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
2910{
2911 struct file *file;
2912 int err;
2913
2914 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
2915 if (err)
2916 return err;
2917 err = vfs_test_lock(file, lock);
2918 nfsd_close(file);
2919 return err;
2920}
2921
2922/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 * LOCKT operation
2924 */
Al Virob37ad282006-10-19 23:28:59 -07002925__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002926nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2927 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
2929 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 struct file_lock file_lock;
Marc Eshelfd85b812006-11-28 16:26:41 -05002931 int error;
Al Virob37ad282006-10-19 23:28:59 -07002932 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002934 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return nfserr_grace;
2936
2937 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2938 return nfserr_inval;
2939
2940 lockt->lt_stateowner = NULL;
2941 nfs4_lock_state();
2942
2943 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002944 if (STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002947 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
Neil Brown849823c2005-09-13 01:25:36 -07002948 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if (status == nfserr_symlink)
2950 status = nfserr_inval;
2951 goto out;
2952 }
2953
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002954 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 locks_init_lock(&file_lock);
2956 switch (lockt->lt_type) {
2957 case NFS4_READ_LT:
2958 case NFS4_READW_LT:
2959 file_lock.fl_type = F_RDLCK;
2960 break;
2961 case NFS4_WRITE_LT:
2962 case NFS4_WRITEW_LT:
2963 file_lock.fl_type = F_WRLCK;
2964 break;
2965 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002966 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 status = nfserr_inval;
2968 goto out;
2969 }
2970
2971 lockt->lt_stateowner = find_lockstateowner_str(inode,
2972 &lockt->lt_clientid, &lockt->lt_owner);
2973 if (lockt->lt_stateowner)
2974 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2975 file_lock.fl_pid = current->tgid;
2976 file_lock.fl_flags = FL_POSIX;
2977
2978 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02002979 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 nfs4_transform_lock_offset(&file_lock);
2982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08002984 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05002985 if (error) {
2986 status = nfserrno(error);
2987 goto out;
2988 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002989 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002991 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
2993out:
2994 nfs4_unlock_state();
2995 return status;
2996}
2997
Al Virob37ad282006-10-19 23:28:59 -07002998__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002999nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003000 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
3002 struct nfs4_stateid *stp;
3003 struct file *filp = NULL;
3004 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07003005 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07003006 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
3009 (long long) locku->lu_offset,
3010 (long long) locku->lu_length);
3011
3012 if (check_lock_length(locku->lu_offset, locku->lu_length))
3013 return nfserr_inval;
3014
3015 nfs4_lock_state();
3016
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003017 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 locku->lu_seqid,
3019 &locku->lu_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003020 LOCK_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 &locku->lu_stateowner, &stp, NULL)))
3022 goto out;
3023
3024 filp = stp->st_vfs_file;
3025 BUG_ON(!filp);
3026 locks_init_lock(&file_lock);
3027 file_lock.fl_type = F_UNLCK;
3028 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
3029 file_lock.fl_pid = current->tgid;
3030 file_lock.fl_file = filp;
3031 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07003032 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 file_lock.fl_start = locku->lu_offset;
3034
Benny Halevy87df4de2008-12-15 19:42:03 +02003035 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 nfs4_transform_lock_offset(&file_lock);
3037
3038 /*
3039 * Try to unlock the file in the VFS.
3040 */
Marc Eshelfd85b812006-11-28 16:26:41 -05003041 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07003042 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05003043 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 goto out_nfserr;
3045 }
3046 /*
3047 * OK, unlock succeeded; the only thing left to do is update the stateid.
3048 */
3049 update_stateid(&stp->st_stateid);
3050 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3051
3052out:
Neil Brownf2327d92005-09-13 01:25:37 -07003053 if (locku->lu_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 nfs4_get_stateowner(locku->lu_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003055 cstate->replay_owner = locku->lu_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07003056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 nfs4_unlock_state();
3058 return status;
3059
3060out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07003061 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 goto out;
3063}
3064
3065/*
3066 * returns
3067 * 1: locks held by lockowner
3068 * 0: no locks held by lockowner
3069 */
3070static int
3071check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3072{
3073 struct file_lock **flpp;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08003074 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 int status = 0;
3076
3077 lock_kernel();
3078 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08003079 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 status = 1;
3081 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08003082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
3084out:
3085 unlock_kernel();
3086 return status;
3087}
3088
Al Virob37ad282006-10-19 23:28:59 -07003089__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003090nfsd4_release_lockowner(struct svc_rqst *rqstp,
3091 struct nfsd4_compound_state *cstate,
3092 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
3094 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07003095 struct nfs4_stateowner *sop;
3096 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07003098 struct list_head matches;
3099 int i;
Al Virob37ad282006-10-19 23:28:59 -07003100 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3103 clid->cl_boot, clid->cl_id);
3104
3105 /* XXX check for lease expiration */
3106
3107 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07003108 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
3111 nfs4_lock_state();
3112
NeilBrown3e9e3db2005-06-23 22:04:20 -07003113 status = nfserr_locks_held;
3114 /* XXX: we're doing a linear search through all the lockowners.
3115 * Yipes! For now we'll just hope clients aren't really using
3116 * release_lockowner much, but eventually we have to fix these
3117 * data structures. */
3118 INIT_LIST_HEAD(&matches);
3119 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3120 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003121 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07003122 continue;
3123 list_for_each_entry(stp, &sop->so_stateids,
3124 st_perstateowner) {
3125 if (check_for_locks(stp->st_vfs_file, sop))
3126 goto out;
3127 /* Note: so_perclient unused for lockowners,
3128 * so it's OK to fool with here. */
3129 list_add(&sop->so_perclient, &matches);
3130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07003132 }
3133 /* Clients probably won't expect us to return with some (but not all)
3134 * of the lockowner state released; so don't release any until all
3135 * have been checked. */
3136 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07003137 while (!list_empty(&matches)) {
3138 sop = list_entry(matches.next, struct nfs4_stateowner,
3139 so_perclient);
3140 /* unhash_stateowner deletes so_perclient only
3141 * for openowners. */
3142 list_del(&sop->so_perclient);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05003143 release_lockowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
3145out:
3146 nfs4_unlock_state();
3147 return status;
3148}
3149
3150static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07003151alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
NeilBrowna55370a2005-06-23 22:03:52 -07003153 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
NeilBrownc7b9a452005-06-23 22:04:30 -07003156int
3157nfs4_has_reclaimed_state(const char *name)
3158{
3159 unsigned int strhashval = clientstr_hashval(name);
3160 struct nfs4_client *clp;
3161
3162 clp = find_confirmed_client_by_str(name, strhashval);
3163 return clp ? 1 : 0;
3164}
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166/*
3167 * failure => all reset bets are off, nfserr_no_grace...
3168 */
NeilBrown190e4fb2005-06-23 22:04:25 -07003169int
3170nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
3172 unsigned int strhashval;
3173 struct nfs4_client_reclaim *crp = NULL;
3174
NeilBrowna55370a2005-06-23 22:03:52 -07003175 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3176 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 if (!crp)
3178 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07003179 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 INIT_LIST_HEAD(&crp->cr_strhash);
3181 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07003182 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 reclaim_str_hashtbl_size++;
3184 return 1;
3185}
3186
3187static void
3188nfs4_release_reclaim(void)
3189{
3190 struct nfs4_client_reclaim *crp = NULL;
3191 int i;
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3194 while (!list_empty(&reclaim_str_hashtbl[i])) {
3195 crp = list_entry(reclaim_str_hashtbl[i].next,
3196 struct nfs4_client_reclaim, cr_strhash);
3197 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 kfree(crp);
3199 reclaim_str_hashtbl_size--;
3200 }
3201 }
3202 BUG_ON(reclaim_str_hashtbl_size);
3203}
3204
3205/*
3206 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07003207static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208nfs4_find_reclaim_client(clientid_t *clid)
3209{
3210 unsigned int strhashval;
3211 struct nfs4_client *clp;
3212 struct nfs4_client_reclaim *crp = NULL;
3213
3214
3215 /* find clientid in conf_id_hashtbl */
3216 clp = find_confirmed_client(clid);
3217 if (clp == NULL)
3218 return NULL;
3219
NeilBrowna55370a2005-06-23 22:03:52 -07003220 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3221 clp->cl_name.len, clp->cl_name.data,
3222 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07003225 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07003227 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return crp;
3229 }
3230 }
3231 return NULL;
3232}
3233
3234/*
3235* Called from OPEN. Look for clientid in reclaim list.
3236*/
Al Virob37ad282006-10-19 23:28:59 -07003237__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238nfs4_check_open_reclaim(clientid_t *clid)
3239{
NeilBrowndfc83562005-06-23 22:03:16 -07003240 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
NeilBrownac4d8ff22005-06-23 22:03:30 -07003243/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003245int
NeilBrownac4d8ff22005-06-23 22:03:30 -07003246nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003248 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003250 status = nfsd4_init_slabs();
3251 if (status)
3252 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3254 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3255 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3256 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3257 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3258 }
Marc Eshel5282fd72009-04-03 08:27:52 +03003259 for (i = 0; i < SESSION_HASH_SIZE; i++)
3260 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 for (i = 0; i < FILE_HASH_SIZE; i++) {
3262 INIT_LIST_HEAD(&file_hashtbl[i]);
3263 }
3264 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3265 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3266 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3267 }
3268 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3269 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3270 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3271 }
3272 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3273 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3274 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 INIT_LIST_HEAD(&close_lru);
3278 INIT_LIST_HEAD(&client_lru);
3279 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07003280 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3281 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3282 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003283 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003284}
3285
NeilBrown190e4fb2005-06-23 22:04:25 -07003286static void
3287nfsd4_load_reboot_recovery_data(void)
3288{
3289 int status;
3290
NeilBrown0964a3d2005-06-23 22:04:32 -07003291 nfs4_lock_state();
3292 nfsd4_init_recdir(user_recovery_dirname);
NeilBrown190e4fb2005-06-23 22:04:25 -07003293 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07003294 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07003295 if (status)
3296 printk("NFSD: Failure reading reboot recovery data\n");
3297}
3298
Marc Eshel9a8db972007-07-17 04:04:35 -07003299unsigned long
3300get_nfs4_grace_period(void)
3301{
3302 return max(user_lease_time, lease_time) * HZ;
3303}
3304
Meelap Shahc2f1a552007-07-17 04:04:39 -07003305/*
3306 * Since the lifetime of a delegation isn't limited to that of an open, a
3307 * client may quite reasonably hang on to a delegation as long as it has
3308 * the inode cached. This becomes an obvious problem the first time a
3309 * client's inode cache approaches the size of the server's total memory.
3310 *
3311 * For now we avoid this problem by imposing a hard limit on the number
3312 * of delegations, which varies according to the server's memory size.
3313 */
3314static void
3315set_max_delegations(void)
3316{
3317 /*
3318 * Allow at most 4 delegations per megabyte of RAM. Quick
3319 * estimates suggest that in the worst case (where every delegation
3320 * is for a different inode), a delegation could take about 1.5K,
3321 * giving a worst case usage of about 6% of memory.
3322 */
3323 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
3324}
3325
NeilBrownac4d8ff22005-06-23 22:03:30 -07003326/* initialization to perform when the nfsd service is started: */
3327
3328static void
3329__nfs4_state_start(void)
3330{
Marc Eshel9a8db972007-07-17 04:04:35 -07003331 unsigned long grace_time;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003334 grace_time = get_nfs4_grace_period();
NeilBrownd99a05a2005-06-23 22:03:21 -07003335 lease_time = user_lease_time;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003336 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07003337 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
3338 grace_time/HZ);
NeilBrown58da2822005-06-23 22:03:19 -07003339 laundry_wq = create_singlethread_workqueue("nfsd4");
Marc Eshel9a8db972007-07-17 04:04:35 -07003340 queue_delayed_work(laundry_wq, &laundromat_work, grace_time);
Meelap Shahc2f1a552007-07-17 04:04:39 -07003341 set_max_delegations();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003344void
NeilBrown76a35502005-06-23 22:03:26 -07003345nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 if (nfs4_init)
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003348 return;
NeilBrown190e4fb2005-06-23 22:04:25 -07003349 nfsd4_load_reboot_recovery_data();
NeilBrown76a35502005-06-23 22:03:26 -07003350 __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 nfs4_init = 1;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003352 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355time_t
3356nfs4_lease_time(void)
3357{
3358 return lease_time;
3359}
3360
3361static void
3362__nfs4_state_shutdown(void)
3363{
3364 int i;
3365 struct nfs4_client *clp = NULL;
3366 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 struct list_head *pos, *next, reaplist;
3368
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3370 while (!list_empty(&conf_id_hashtbl[i])) {
3371 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3372 expire_client(clp);
3373 }
3374 while (!list_empty(&unconf_str_hashtbl[i])) {
3375 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3376 expire_client(clp);
3377 }
3378 }
3379 INIT_LIST_HEAD(&reaplist);
3380 spin_lock(&recall_lock);
3381 list_for_each_safe(pos, next, &del_recall_lru) {
3382 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3383 list_move(&dp->dl_recall_lru, &reaplist);
3384 }
3385 spin_unlock(&recall_lock);
3386 list_for_each_safe(pos, next, &reaplist) {
3387 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3388 list_del_init(&dp->dl_recall_lru);
3389 unhash_delegation(dp);
3390 }
3391
NeilBrown190e4fb2005-06-23 22:04:25 -07003392 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 nfs4_init = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394}
3395
3396void
3397nfs4_state_shutdown(void)
3398{
NeilBrown5e8d5c22006-04-10 22:55:37 -07003399 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3400 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06003401 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 nfs4_lock_state();
3403 nfs4_release_reclaim();
3404 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 nfs4_unlock_state();
3406}
3407
Jeff Layton3dd98a32008-06-10 08:40:36 -04003408/*
3409 * user_recovery_dirname is protected by the nfsd_mutex since it's only
3410 * accessed when nfsd is starting.
3411 */
NeilBrown0964a3d2005-06-23 22:04:32 -07003412static void
3413nfs4_set_recdir(char *recdir)
3414{
NeilBrown0964a3d2005-06-23 22:04:32 -07003415 strcpy(user_recovery_dirname, recdir);
NeilBrown0964a3d2005-06-23 22:04:32 -07003416}
3417
3418/*
3419 * Change the NFSv4 recovery directory to recdir.
3420 */
3421int
3422nfs4_reset_recoverydir(char *recdir)
3423{
3424 int status;
Al Viroa63bb992008-08-02 01:03:36 -04003425 struct path path;
NeilBrown0964a3d2005-06-23 22:04:32 -07003426
Al Viroa63bb992008-08-02 01:03:36 -04003427 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
NeilBrown0964a3d2005-06-23 22:04:32 -07003428 if (status)
3429 return status;
3430 status = -ENOTDIR;
Al Viroa63bb992008-08-02 01:03:36 -04003431 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
NeilBrown0964a3d2005-06-23 22:04:32 -07003432 nfs4_set_recdir(recdir);
3433 status = 0;
3434 }
Al Viroa63bb992008-08-02 01:03:36 -04003435 path_put(&path);
NeilBrown0964a3d2005-06-23 22:04:32 -07003436 return status;
3437}
3438
Jeff Layton3dd98a32008-06-10 08:40:36 -04003439char *
3440nfs4_recoverydir(void)
3441{
3442 return user_recovery_dirname;
3443}
3444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445/*
3446 * Called when leasetime is changed.
3447 *
NeilBrownd99a05a2005-06-23 22:03:21 -07003448 * The only way the protocol gives us to handle on-the-fly lease changes is to
3449 * simulate a reboot. Instead of doing that, we just wait till the next time
3450 * we start to register any changes in lease time. If the administrator
3451 * really wants to change the lease time *now*, they can go ahead and bring
3452 * nfsd down and then back up again after changing the lease time.
Jeff Layton3dd98a32008-06-10 08:40:36 -04003453 *
3454 * user_lease_time is protected by nfsd_mutex since it's only really accessed
3455 * when nfsd is starting
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 */
3457void
3458nfs4_reset_lease(time_t leasetime)
3459{
NeilBrownd99a05a2005-06-23 22:03:21 -07003460 user_lease_time = leasetime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}