blob: f25a7d2a9fac19a4658ab6059740597f8d0fe345 [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
Andy Adamson14778a12009-04-03 08:28:25 +0300444static void nfsd4_release_respages(struct page **respages, short resused);
445
Andy Adamson7116ed62009-04-03 08:27:43 +0300446void
447free_session(struct kref *kref)
448{
449 struct nfsd4_session *ses;
Andy Adamson14778a12009-04-03 08:28:25 +0300450 int i;
Andy Adamson7116ed62009-04-03 08:27:43 +0300451
452 ses = container_of(kref, struct nfsd4_session, se_ref);
Andy Adamson14778a12009-04-03 08:28:25 +0300453 for (i = 0; i < ses->se_fnumslots; i++) {
454 struct nfsd4_cache_entry *e = &ses->se_slots[i].sl_cache_entry;
455 nfsd4_release_respages(e->ce_respages, e->ce_resused);
456 }
Andy Adamson7116ed62009-04-03 08:27:43 +0300457 kfree(ses->se_slots);
458 kfree(ses);
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static inline void
462renew_client(struct nfs4_client *clp)
463{
464 /*
465 * Move client to the end to the LRU list.
466 */
467 dprintk("renewing client (clientid %08x/%08x)\n",
468 clp->cl_clientid.cl_boot,
469 clp->cl_clientid.cl_id);
470 list_move_tail(&clp->cl_lru, &client_lru);
471 clp->cl_time = get_seconds();
472}
473
474/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
475static int
476STALE_CLIENTID(clientid_t *clid)
477{
478 if (clid->cl_boot == boot_time)
479 return 0;
480 dprintk("NFSD stale clientid (%08x/%08x)\n",
481 clid->cl_boot, clid->cl_id);
482 return 1;
483}
484
485/*
486 * XXX Should we use a slab cache ?
487 * This type of memory management is somewhat inefficient, but we use it
488 * anyway since SETCLIENTID is not a common operation.
489 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500490static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct nfs4_client *clp;
493
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500494 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
495 if (clp == NULL)
496 return NULL;
497 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
498 if (clp->cl_name.data == NULL) {
499 kfree(clp);
500 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500502 memcpy(clp->cl_name.data, name.data, name.len);
503 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return clp;
505}
506
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400507static void
508shutdown_callback_client(struct nfs4_client *clp)
509{
510 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
511
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400512 if (clnt) {
J. Bruce Fields404ec112007-11-23 22:26:18 -0500513 /*
514 * Callback threads take a reference on the client, so there
515 * should be no outstanding callbacks at this point.
516 */
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400517 clp->cl_callback.cb_client = NULL;
518 rpc_shutdown_client(clnt);
519 }
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static inline void
523free_client(struct nfs4_client *clp)
524{
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400525 shutdown_callback_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (clp->cl_cred.cr_group_info)
527 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500528 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 kfree(clp->cl_name.data);
530 kfree(clp);
531}
532
533void
534put_nfs4_client(struct nfs4_client *clp)
535{
536 if (atomic_dec_and_test(&clp->cl_count))
537 free_client(clp);
538}
539
540static void
541expire_client(struct nfs4_client *clp)
542{
543 struct nfs4_stateowner *sop;
544 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct list_head reaplist;
546
547 dprintk("NFSD: expire_client cl_count %d\n",
548 atomic_read(&clp->cl_count));
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 INIT_LIST_HEAD(&reaplist);
551 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700552 while (!list_empty(&clp->cl_delegations)) {
553 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
555 dp->dl_flock);
NeilBrownea1da632005-06-23 22:04:17 -0700556 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 list_move(&dp->dl_recall_lru, &reaplist);
558 }
559 spin_unlock(&recall_lock);
560 while (!list_empty(&reaplist)) {
561 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
562 list_del_init(&dp->dl_recall_lru);
563 unhash_delegation(dp);
564 }
565 list_del(&clp->cl_idhash);
566 list_del(&clp->cl_strhash);
567 list_del(&clp->cl_lru);
NeilBrownea1da632005-06-23 22:04:17 -0700568 while (!list_empty(&clp->cl_openowners)) {
569 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500570 release_openowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Marc Eshelc4bf7862009-04-03 08:27:49 +0300572 while (!list_empty(&clp->cl_sessions)) {
573 struct nfsd4_session *ses;
574 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
575 se_perclnt);
576 release_session(ses);
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 put_nfs4_client(clp);
579}
580
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500581static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir)
582{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct nfs4_client *clp;
584
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500585 clp = alloc_client(name);
586 if (clp == NULL)
587 return NULL;
NeilBrowna55370a2005-06-23 22:03:52 -0700588 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 atomic_set(&clp->cl_count, 1);
590 atomic_set(&clp->cl_callback.cb_set, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 INIT_LIST_HEAD(&clp->cl_idhash);
592 INIT_LIST_HEAD(&clp->cl_strhash);
NeilBrownea1da632005-06-23 22:04:17 -0700593 INIT_LIST_HEAD(&clp->cl_openowners);
594 INIT_LIST_HEAD(&clp->cl_delegations);
Marc Eshel9fb87072009-04-03 08:27:46 +0300595 INIT_LIST_HEAD(&clp->cl_sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 INIT_LIST_HEAD(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return clp;
598}
599
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500600static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
601{
602 memcpy(target->cl_verifier.data, source->data,
603 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500606static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
607{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
609 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
610}
611
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500612static void copy_cred(struct svc_cred *target, struct svc_cred *source)
613{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 target->cr_uid = source->cr_uid;
615 target->cr_gid = source->cr_gid;
616 target->cr_group_info = source->cr_group_info;
617 get_group_info(target->cr_group_info);
618}
619
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500620static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400621{
NeilBrowna55370a2005-06-23 22:03:52 -0700622 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400626same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
627{
628 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400632same_clid(clientid_t *cl1, clientid_t *cl2)
633{
634 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637/* XXX what about NGROUP */
638static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400639same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
640{
641 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
J. Bruce Fields5ec7b462007-11-21 21:58:56 -0500644static void gen_clid(struct nfs4_client *clp)
645{
646 static u32 current_clientid = 1;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 clp->cl_clientid.cl_boot = boot_time;
649 clp->cl_clientid.cl_id = current_clientid++;
650}
651
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -0500652static void gen_confirm(struct nfs4_client *clp)
653{
654 static u32 i;
655 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -0500658 *p++ = get_seconds();
659 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500662static int check_name(struct xdr_netobj name)
663{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (name.len == 0)
665 return 0;
666 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400667 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 0;
669 }
670 return 1;
671}
672
NeilBrownfd39ca92005-06-23 22:04:03 -0700673static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
675{
676 unsigned int idhashval;
677
678 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
679 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
680 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
681 list_add_tail(&clp->cl_lru, &client_lru);
682 clp->cl_time = get_seconds();
683}
684
NeilBrownfd39ca92005-06-23 22:04:03 -0700685static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686move_to_confirmed(struct nfs4_client *clp)
687{
688 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
689 unsigned int strhashval;
690
691 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
692 list_del_init(&clp->cl_strhash);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700693 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -0700694 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
696 renew_client(clp);
697}
698
699static struct nfs4_client *
700find_confirmed_client(clientid_t *clid)
701{
702 struct nfs4_client *clp;
703 unsigned int idhashval = clientid_hashval(clid->cl_id);
704
705 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400706 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return clp;
708 }
709 return NULL;
710}
711
712static struct nfs4_client *
713find_unconfirmed_client(clientid_t *clid)
714{
715 struct nfs4_client *clp;
716 unsigned int idhashval = clientid_hashval(clid->cl_id);
717
718 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400719 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return clp;
721 }
722 return NULL;
723}
724
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300725/*
726 * Return 1 iff clp's clientid establishment method matches the use_exchange_id
727 * parameter. Matching is based on the fact the at least one of the
728 * EXCHGID4_FLAG_USE_{NON_PNFS,PNFS_MDS,PNFS_DS} flags must be set for v4.1
729 *
730 * FIXME: we need to unify the clientid namespaces for nfsv4.x
731 * and correctly deal with client upgrade/downgrade in EXCHANGE_ID
732 * and SET_CLIENTID{,_CONFIRM}
733 */
734static inline int
735match_clientid_establishment(struct nfs4_client *clp, bool use_exchange_id)
736{
737 bool has_exchange_flags = (clp->cl_exchange_flags != 0);
738 return use_exchange_id == has_exchange_flags;
739}
740
NeilBrown28ce6052005-06-23 22:03:56 -0700741static struct nfs4_client *
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300742find_confirmed_client_by_str(const char *dname, unsigned int hashval,
743 bool use_exchange_id)
NeilBrown28ce6052005-06-23 22:03:56 -0700744{
745 struct nfs4_client *clp;
746
747 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300748 if (same_name(clp->cl_recdir, dname) &&
749 match_clientid_establishment(clp, use_exchange_id))
NeilBrown28ce6052005-06-23 22:03:56 -0700750 return clp;
751 }
752 return NULL;
753}
754
755static struct nfs4_client *
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300756find_unconfirmed_client_by_str(const char *dname, unsigned int hashval,
757 bool use_exchange_id)
NeilBrown28ce6052005-06-23 22:03:56 -0700758{
759 struct nfs4_client *clp;
760
761 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300762 if (same_name(clp->cl_recdir, dname) &&
763 match_clientid_establishment(clp, use_exchange_id))
NeilBrown28ce6052005-06-23 22:03:56 -0700764 return clp;
765 }
766 return NULL;
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/* a helper function for parse_callback */
770static int
771parse_octet(unsigned int *lenp, char **addrp)
772{
773 unsigned int len = *lenp;
774 char *p = *addrp;
775 int n = -1;
776 char c;
777
778 for (;;) {
779 if (!len)
780 break;
781 len--;
782 c = *p++;
783 if (c == '.')
784 break;
785 if ((c < '0') || (c > '9')) {
786 n = -1;
787 break;
788 }
789 if (n < 0)
790 n = 0;
791 n = (n * 10) + (c - '0');
792 if (n > 255) {
793 n = -1;
794 break;
795 }
796 }
797 *lenp = len;
798 *addrp = p;
799 return n;
800}
801
802/* parse and set the setclientid ipv4 callback address */
NeilBrownfd39ca92005-06-23 22:04:03 -0700803static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
805{
806 int temp = 0;
807 u32 cbaddr = 0;
808 u16 cbport = 0;
809 u32 addrlen = addr_len;
810 char *addr = addr_val;
811 int i, shift;
812
813 /* ipaddress */
814 shift = 24;
815 for(i = 4; i > 0 ; i--) {
816 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
817 return 0;
818 }
819 cbaddr |= (temp << shift);
820 if (shift > 0)
821 shift -= 8;
822 }
823 *cbaddrp = cbaddr;
824
825 /* port */
826 shift = 8;
827 for(i = 2; i > 0 ; i--) {
828 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
829 return 0;
830 }
831 cbport |= (temp << shift);
832 if (shift > 0)
833 shift -= 8;
834 }
835 *cbportp = cbport;
836 return 1;
837}
838
NeilBrownfd39ca92005-06-23 22:04:03 -0700839static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
841{
842 struct nfs4_callback *cb = &clp->cl_callback;
843
844 /* Currently, we only support tcp for the callback channel */
845 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
846 goto out_err;
847
848 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
849 &cb->cb_addr, &cb->cb_port)))
850 goto out_err;
851 cb->cb_prog = se->se_callback_prog;
852 cb->cb_ident = se->se_callback_ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return;
854out_err:
Neil Brown849823c2005-09-13 01:25:36 -0700855 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 "will not receive delegations\n",
857 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return;
860}
861
Andy Adamson074fe892009-04-03 08:28:15 +0300862void
863nfsd4_set_statp(struct svc_rqst *rqstp, __be32 *statp)
864{
865 struct nfsd4_compoundres *resp = rqstp->rq_resp;
866
867 resp->cstate.statp = statp;
868}
869
870/*
871 * Dereference the result pages.
872 */
873static void
874nfsd4_release_respages(struct page **respages, short resused)
875{
876 int i;
877
878 dprintk("--> %s\n", __func__);
879 for (i = 0; i < resused; i++) {
880 if (!respages[i])
881 continue;
882 put_page(respages[i]);
883 respages[i] = NULL;
884 }
885}
886
887static void
888nfsd4_copy_pages(struct page **topages, struct page **frompages, short count)
889{
890 int i;
891
892 for (i = 0; i < count; i++) {
893 topages[i] = frompages[i];
894 if (!topages[i])
895 continue;
896 get_page(topages[i]);
897 }
898}
899
900/*
901 * Cache the reply pages up to NFSD_PAGES_PER_SLOT + 1, clearing the previous
902 * pages. We add a page to NFSD_PAGES_PER_SLOT for the case where the total
903 * length of the XDR response is less than se_fmaxresp_cached
904 * (NFSD_PAGES_PER_SLOT * PAGE_SIZE) but the xdr_buf pages is used for a
905 * of the reply (e.g. readdir).
906 *
907 * Store the base and length of the rq_req.head[0] page
908 * of the NFSv4.1 data, just past the rpc header.
909 */
910void
911nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
912{
913 struct nfsd4_cache_entry *entry = &resp->cstate.slot->sl_cache_entry;
914 struct svc_rqst *rqstp = resp->rqstp;
915 struct nfsd4_compoundargs *args = rqstp->rq_argp;
916 struct nfsd4_op *op = &args->ops[resp->opcnt];
917 struct kvec *resv = &rqstp->rq_res.head[0];
918
919 dprintk("--> %s entry %p\n", __func__, entry);
920
921 /* Don't cache a failed OP_SEQUENCE. */
922 if (resp->opcnt == 1 && op->opnum == OP_SEQUENCE && resp->cstate.status)
923 return;
924 nfsd4_release_respages(entry->ce_respages, entry->ce_resused);
925 entry->ce_resused = rqstp->rq_resused;
926 if (entry->ce_resused > NFSD_PAGES_PER_SLOT + 1)
927 entry->ce_resused = NFSD_PAGES_PER_SLOT + 1;
928 nfsd4_copy_pages(entry->ce_respages, rqstp->rq_respages,
929 entry->ce_resused);
930 entry->ce_status = resp->cstate.status;
931 entry->ce_datav.iov_base = resp->cstate.statp;
932 entry->ce_datav.iov_len = resv->iov_len - ((char *)resp->cstate.statp -
933 (char *)page_address(rqstp->rq_respages[0]));
934 entry->ce_opcnt = resp->opcnt;
935 /* Current request rpc header length*/
936 entry->ce_rpchdrlen = (char *)resp->cstate.statp -
937 (char *)page_address(rqstp->rq_respages[0]);
938}
939
940/*
941 * We keep the rpc header, but take the nfs reply from the replycache.
942 */
943static int
944nfsd41_copy_replay_data(struct nfsd4_compoundres *resp,
945 struct nfsd4_cache_entry *entry)
946{
947 struct svc_rqst *rqstp = resp->rqstp;
948 struct kvec *resv = &resp->rqstp->rq_res.head[0];
949 int len;
950
951 /* Current request rpc header length*/
952 len = (char *)resp->cstate.statp -
953 (char *)page_address(rqstp->rq_respages[0]);
954 if (entry->ce_datav.iov_len + len > PAGE_SIZE) {
955 dprintk("%s v41 cached reply too large (%Zd).\n", __func__,
956 entry->ce_datav.iov_len);
957 return 0;
958 }
959 /* copy the cached reply nfsd data past the current rpc header */
960 memcpy((char *)resv->iov_base + len, entry->ce_datav.iov_base,
961 entry->ce_datav.iov_len);
962 resv->iov_len = len + entry->ce_datav.iov_len;
963 return 1;
964}
965
966/*
967 * Keep the first page of the replay. Copy the NFSv4.1 data from the first
968 * cached page. Replace any futher replay pages from the cache.
969 */
970__be32
971nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp)
972{
973 struct nfsd4_cache_entry *entry = &resp->cstate.slot->sl_cache_entry;
974 __be32 status;
975
976 dprintk("--> %s entry %p\n", __func__, entry);
977
978
979 if (!nfsd41_copy_replay_data(resp, entry)) {
980 /*
981 * Not enough room to use the replay rpc header, send the
982 * cached header. Release all the allocated result pages.
983 */
984 svc_free_res_pages(resp->rqstp);
985 nfsd4_copy_pages(resp->rqstp->rq_respages, entry->ce_respages,
986 entry->ce_resused);
987 } else {
988 /* Release all but the first allocated result page */
989
990 resp->rqstp->rq_resused--;
991 svc_free_res_pages(resp->rqstp);
992
993 nfsd4_copy_pages(&resp->rqstp->rq_respages[1],
994 &entry->ce_respages[1],
995 entry->ce_resused - 1);
996 }
997
998 resp->rqstp->rq_resused = entry->ce_resused;
Andy Adamsonda3846a2009-04-03 08:28:22 +0300999 resp->opcnt = entry->ce_opcnt;
1000 resp->cstate.iovlen = entry->ce_datav.iov_len + entry->ce_rpchdrlen;
Andy Adamson074fe892009-04-03 08:28:15 +03001001 status = entry->ce_status;
1002
1003 return status;
1004}
1005
Andy Adamson0733d212009-04-03 08:28:01 +03001006/*
1007 * Set the exchange_id flags returned by the server.
1008 */
1009static void
1010nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1011{
1012 /* pNFS is not supported */
1013 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1014
1015 /* Referrals are supported, Migration is not. */
1016 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1017
1018 /* set the wire flags to return to client. */
1019 clid->flags = new->cl_exchange_flags;
1020}
1021
Al Virob37ad282006-10-19 23:28:59 -07001022__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001023nfsd4_exchange_id(struct svc_rqst *rqstp,
1024 struct nfsd4_compound_state *cstate,
1025 struct nfsd4_exchange_id *exid)
1026{
Andy Adamson0733d212009-04-03 08:28:01 +03001027 struct nfs4_client *unconf, *conf, *new;
1028 int status;
1029 unsigned int strhashval;
1030 char dname[HEXDIR_LEN];
1031 nfs4_verifier verf = exid->verifier;
1032 u32 ip_addr = svc_addr_in(rqstp)->sin_addr.s_addr;
1033
1034 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1035 " ip_addr=%u flags %x, spa_how %d\n",
1036 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1037 ip_addr, exid->flags, exid->spa_how);
1038
1039 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1040 return nfserr_inval;
1041
1042 /* Currently only support SP4_NONE */
1043 switch (exid->spa_how) {
1044 case SP4_NONE:
1045 break;
1046 case SP4_SSV:
1047 return nfserr_encr_alg_unsupp;
1048 default:
1049 BUG(); /* checked by xdr code */
1050 case SP4_MACH_CRED:
1051 return nfserr_serverfault; /* no excuse :-/ */
1052 }
1053
1054 status = nfs4_make_rec_clidname(dname, &exid->clname);
1055
1056 if (status)
1057 goto error;
1058
1059 strhashval = clientstr_hashval(dname);
1060
1061 nfs4_lock_state();
1062 status = nfs_ok;
1063
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001064 conf = find_confirmed_client_by_str(dname, strhashval, true);
Andy Adamson0733d212009-04-03 08:28:01 +03001065 if (conf) {
1066 if (!same_verf(&verf, &conf->cl_verifier)) {
1067 /* 18.35.4 case 8 */
1068 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1069 status = nfserr_not_same;
1070 goto out;
1071 }
1072 /* Client reboot: destroy old state */
1073 expire_client(conf);
1074 goto out_new;
1075 }
1076 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1077 /* 18.35.4 case 9 */
1078 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1079 status = nfserr_perm;
1080 goto out;
1081 }
1082 expire_client(conf);
1083 goto out_new;
1084 }
1085 if (ip_addr != conf->cl_addr &&
1086 !(exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A)) {
1087 /* Client collision. 18.35.4 case 3 */
1088 status = nfserr_clid_inuse;
1089 goto out;
1090 }
1091 /*
1092 * Set bit when the owner id and verifier map to an already
1093 * confirmed client id (18.35.3).
1094 */
1095 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1096
1097 /*
1098 * Falling into 18.35.4 case 2, possible router replay.
1099 * Leave confirmed record intact and return same result.
1100 */
1101 copy_verf(conf, &verf);
1102 new = conf;
1103 goto out_copy;
1104 } else {
1105 /* 18.35.4 case 7 */
1106 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1107 status = nfserr_noent;
1108 goto out;
1109 }
1110 }
1111
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001112 unconf = find_unconfirmed_client_by_str(dname, strhashval, true);
Andy Adamson0733d212009-04-03 08:28:01 +03001113 if (unconf) {
1114 /*
1115 * Possible retry or client restart. Per 18.35.4 case 4,
1116 * a new unconfirmed record should be generated regardless
1117 * of whether any properties have changed.
1118 */
1119 expire_client(unconf);
1120 }
1121
1122out_new:
1123 /* Normal case */
1124 new = create_client(exid->clname, dname);
1125 if (new == NULL) {
1126 status = nfserr_resource;
1127 goto out;
1128 }
1129
1130 copy_verf(new, &verf);
1131 copy_cred(&new->cl_cred, &rqstp->rq_cred);
1132 new->cl_addr = ip_addr;
1133 gen_clid(new);
1134 gen_confirm(new);
1135 add_to_unconfirmed(new, strhashval);
1136out_copy:
1137 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1138 exid->clientid.cl_id = new->cl_clientid.cl_id;
1139
1140 new->cl_seqid = exid->seqid = 1;
1141 nfsd4_set_ex_flags(new, exid);
1142
1143 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1144 new->cl_seqid, new->cl_exchange_flags);
1145 status = nfs_ok;
1146
1147out:
1148 nfs4_unlock_state();
1149error:
1150 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1151 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001152}
1153
Benny Halevyb85d4c02009-04-03 08:28:08 +03001154static int
1155check_slot_seqid(u32 seqid, struct nfsd4_slot *slot)
1156{
1157 dprintk("%s enter. seqid %d slot->sl_seqid %d\n", __func__, seqid,
1158 slot->sl_seqid);
1159
1160 /* The slot is in use, and no response has been sent. */
1161 if (slot->sl_inuse) {
1162 if (seqid == slot->sl_seqid)
1163 return nfserr_jukebox;
1164 else
1165 return nfserr_seq_misordered;
1166 }
1167 /* Normal */
1168 if (likely(seqid == slot->sl_seqid + 1))
1169 return nfs_ok;
1170 /* Replay */
1171 if (seqid == slot->sl_seqid)
1172 return nfserr_replay_cache;
1173 /* Wraparound */
1174 if (seqid == 1 && (slot->sl_seqid + 1) == 0)
1175 return nfs_ok;
1176 /* Misordered replay or misordered new request */
1177 return nfserr_seq_misordered;
1178}
1179
Andy Adamson069b6ad2009-04-03 08:27:58 +03001180__be32
1181nfsd4_create_session(struct svc_rqst *rqstp,
1182 struct nfsd4_compound_state *cstate,
1183 struct nfsd4_create_session *cr_ses)
1184{
1185 return -1; /* stub */
1186}
1187
1188__be32
1189nfsd4_destroy_session(struct svc_rqst *r,
1190 struct nfsd4_compound_state *cstate,
1191 struct nfsd4_destroy_session *sessionid)
1192{
1193 return -1; /* stub */
1194}
1195
1196__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001197nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001198 struct nfsd4_compound_state *cstate,
1199 struct nfsd4_sequence *seq)
1200{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001201 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001202 struct nfsd4_session *session;
1203 struct nfsd4_slot *slot;
1204 int status;
1205
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001206 if (resp->opcnt != 1)
1207 return nfserr_sequence_pos;
1208
Benny Halevyb85d4c02009-04-03 08:28:08 +03001209 spin_lock(&sessionid_lock);
1210 status = nfserr_badsession;
1211 session = find_in_sessionid_hashtbl(&seq->sessionid);
1212 if (!session)
1213 goto out;
1214
1215 status = nfserr_badslot;
1216 if (seq->slotid >= session->se_fnumslots)
1217 goto out;
1218
1219 slot = &session->se_slots[seq->slotid];
1220 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1221
1222 status = check_slot_seqid(seq->seqid, slot);
1223 if (status == nfserr_replay_cache) {
1224 cstate->slot = slot;
1225 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001226 /* Return the cached reply status and set cstate->status
1227 * for nfsd4_svc_encode_compoundres processing*/
1228 status = nfsd4_replay_cache_entry(resp);
1229 cstate->status = nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001230 goto replay_cache;
1231 }
1232 if (status)
1233 goto out;
1234
1235 /* Success! bump slot seqid */
1236 slot->sl_inuse = true;
1237 slot->sl_seqid = seq->seqid;
1238
1239 cstate->slot = slot;
1240 cstate->session = session;
1241
1242replay_cache:
1243 /* Renew the clientid on success and on replay.
1244 * Hold a session reference until done processing the compound:
1245 * nfsd4_put_session called only if the cstate slot is set.
1246 */
1247 renew_client(session->se_client);
1248 nfsd4_get_session(session);
1249out:
1250 spin_unlock(&sessionid_lock);
1251 dprintk("%s: return %d\n", __func__, ntohl(status));
1252 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001253}
1254
1255__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001256nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1257 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Chuck Lever27459f02007-02-12 00:53:34 -08001259 struct sockaddr_in *sin = svc_addr_in(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 struct xdr_netobj clname = {
1261 .len = setclid->se_namelen,
1262 .data = setclid->se_name,
1263 };
1264 nfs4_verifier clverifier = setclid->se_verf;
1265 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07001266 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07001267 __be32 status;
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -05001268 char *princ;
NeilBrowna55370a2005-06-23 22:03:52 -07001269 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -07001272 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
NeilBrowna55370a2005-06-23 22:03:52 -07001274 status = nfs4_make_rec_clidname(dname, &clname);
1275 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07001276 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /*
1279 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1280 * We get here on a DRC miss.
1281 */
1282
NeilBrowna55370a2005-06-23 22:03:52 -07001283 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 nfs4_lock_state();
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001286 conf = find_confirmed_client_by_str(dname, strhashval, false);
NeilBrown28ce6052005-06-23 22:03:56 -07001287 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001288 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 status = nfserr_clid_inuse;
J. Bruce Fields026722c2009-03-18 15:06:26 -04001290 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1291 dprintk("NFSD: setclientid: string in use by client"
1292 " at %pI4\n", &conf->cl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 goto out;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001296 /*
1297 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1298 * has a description of SETCLIENTID request processing consisting
1299 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1300 */
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001301 unconf = find_unconfirmed_client_by_str(dname, strhashval, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 status = nfserr_resource;
1303 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001304 /*
1305 * RFC 3530 14.2.33 CASE 4:
1306 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
1308 if (unconf)
1309 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -07001310 new = create_client(clname, dname);
1311 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001314 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001316 * RFC 3530 14.2.33 CASE 1:
1317 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07001319 if (unconf) {
1320 /* Note this is removing unconfirmed {*x***},
1321 * which is stronger than RFC recommended {vxc**}.
1322 * This has the advantage that there is at most
1323 * one {*x***} in either list at any time.
1324 */
1325 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
NeilBrowna55370a2005-06-23 22:03:52 -07001327 new = create_client(clname, dname);
1328 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 } else if (!unconf) {
1332 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001333 * RFC 3530 14.2.33 CASE 2:
1334 * probable client reboot; state will be removed if
1335 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
NeilBrowna55370a2005-06-23 22:03:52 -07001337 new = create_client(clname, dname);
1338 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05001341 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001342 /*
1343 * RFC 3530 14.2.33 CASE 3:
1344 * probable client reboot; state will be removed if
1345 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
1347 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -07001348 new = create_client(clname, dname);
1349 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04001353 copy_verf(new, &clverifier);
1354 new->cl_addr = sin->sin_addr.s_addr;
Olga Kornievskaia61054b12008-12-23 16:19:00 -05001355 new->cl_flavor = rqstp->rq_flavor;
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -05001356 princ = svc_gss_principal(rqstp);
1357 if (princ) {
1358 new->cl_principal = kstrdup(princ, GFP_KERNEL);
1359 if (new->cl_principal == NULL) {
1360 free_client(new);
1361 goto out;
1362 }
1363 }
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04001364 copy_cred(&new->cl_cred, &rqstp->rq_cred);
1365 gen_confirm(new);
1366 gen_callback(new, setclid);
1367 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
1369 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
1370 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
1371 status = nfs_ok;
1372out:
1373 nfs4_unlock_state();
1374 return status;
1375}
1376
1377
1378/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001379 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
1380 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
1381 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
Al Virob37ad282006-10-19 23:28:59 -07001383__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001384nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
1385 struct nfsd4_compound_state *cstate,
1386 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Chuck Lever27459f02007-02-12 00:53:34 -08001388 struct sockaddr_in *sin = svc_addr_in(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -07001389 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
1391 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07001392 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 if (STALE_CLIENTID(clid))
1395 return nfserr_stale_clientid;
1396 /*
1397 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1398 * We get here on a DRC miss.
1399 */
1400
1401 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07001402
1403 conf = find_confirmed_client(clid);
1404 unconf = find_unconfirmed_client(clid);
1405
1406 status = nfserr_clid_inuse;
Chuck Lever27459f02007-02-12 00:53:34 -08001407 if (conf && conf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -07001408 goto out;
Chuck Lever27459f02007-02-12 00:53:34 -08001409 if (unconf && unconf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -07001410 goto out;
1411
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001412 /*
1413 * section 14.2.34 of RFC 3530 has a description of
1414 * SETCLIENTID_CONFIRM request processing consisting
1415 * of 4 bullet points, labeled as CASE1 - CASE4 below.
1416 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -05001417 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001418 /*
1419 * RFC 3530 14.2.34 CASE 1:
1420 * callback update
1421 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001422 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 status = nfserr_clid_inuse;
1424 else {
NeilBrown1a69c172005-06-23 22:04:08 -07001425 /* XXX: We just turn off callbacks until we can handle
1426 * change request correctly. */
NeilBrowncb36d632005-06-23 22:04:23 -07001427 atomic_set(&conf->cl_callback.cb_set, 0);
NeilBrown21ab45a2005-06-23 22:04:14 -07001428 gen_confirm(conf);
NeilBrown46309022005-07-07 17:59:10 -07001429 nfsd4_remove_clid_dir(unconf);
NeilBrown1a69c172005-06-23 22:04:08 -07001430 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05001434 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001435 /*
1436 * RFC 3530 14.2.34 CASE 2:
1437 * probable retransmitted request; play it safe and
1438 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07001439 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001440 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07001442 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07001444 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001445 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001446 /*
1447 * RFC 3530 14.2.34 CASE 3:
1448 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07001449 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001450 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 status = nfserr_clid_inuse;
1452 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07001453 unsigned int hash =
1454 clientstr_hashval(unconf->cl_recdir);
1455 conf = find_confirmed_client_by_str(unconf->cl_recdir,
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001456 hash, false);
NeilBrown1a69c172005-06-23 22:04:08 -07001457 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07001458 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07001459 expire_client(conf);
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07001462 conf = unconf;
J. Bruce Fields46f8a642007-11-22 13:54:18 -05001463 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07001464 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001466 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
1467 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07001468 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001469 /*
1470 * RFC 3530 14.2.34 CASE 4:
1471 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07001472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07001474 } else {
NeilBrown08e89872005-06-23 22:04:11 -07001475 /* check that we have hit one of the cases...*/
1476 status = nfserr_clid_inuse;
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 nfs4_unlock_state();
1480 return status;
1481}
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483/* OPEN Share state helper functions */
1484static inline struct nfs4_file *
1485alloc_init_file(struct inode *ino)
1486{
1487 struct nfs4_file *fp;
1488 unsigned int hashval = file_hashval(ino);
1489
NeilBrowne60d4392005-06-23 22:03:01 -07001490 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
1491 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001492 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07001494 INIT_LIST_HEAD(&fp->fi_stateids);
1495 INIT_LIST_HEAD(&fp->fi_delegations);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001496 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001498 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 fp->fi_inode = igrab(ino);
1500 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -07001501 fp->fi_had_conflict = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return fp;
1503 }
1504 return NULL;
1505}
1506
1507static void
Christoph Lametere18b8902006-12-06 20:33:20 -08001508nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07001509{
NeilBrowne60d4392005-06-23 22:03:01 -07001510 if (*slab == NULL)
1511 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001512 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001513 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07001514}
1515
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001516void
NeilBrowne60d4392005-06-23 22:03:01 -07001517nfsd4_free_slabs(void)
1518{
1519 nfsd4_free_slab(&stateowner_slab);
1520 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07001521 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001522 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525static int
1526nfsd4_init_slabs(void)
1527{
1528 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
Paul Mundt20c2df82007-07-20 10:11:58 +09001529 sizeof(struct nfs4_stateowner), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001530 if (stateowner_slab == NULL)
1531 goto out_nomem;
1532 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09001533 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001534 if (file_slab == NULL)
1535 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07001536 stateid_slab = kmem_cache_create("nfsd4_stateids",
Paul Mundt20c2df82007-07-20 10:11:58 +09001537 sizeof(struct nfs4_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07001538 if (stateid_slab == NULL)
1539 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07001540 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09001541 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001542 if (deleg_slab == NULL)
1543 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07001545out_nomem:
1546 nfsd4_free_slabs();
1547 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1548 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551void
1552nfs4_free_stateowner(struct kref *kref)
1553{
1554 struct nfs4_stateowner *sop =
1555 container_of(kref, struct nfs4_stateowner, so_ref);
1556 kfree(sop->so_owner.data);
1557 kmem_cache_free(stateowner_slab, sop);
1558}
1559
1560static inline struct nfs4_stateowner *
1561alloc_stateowner(struct xdr_netobj *owner)
1562{
1563 struct nfs4_stateowner *sop;
1564
1565 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1566 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1567 memcpy(sop->so_owner.data, owner->data, owner->len);
1568 sop->so_owner.len = owner->len;
1569 kref_init(&sop->so_ref);
1570 return sop;
1571 }
1572 kmem_cache_free(stateowner_slab, sop);
1573 }
1574 return NULL;
1575}
1576
1577static struct nfs4_stateowner *
1578alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1579 struct nfs4_stateowner *sop;
1580 struct nfs4_replay *rp;
1581 unsigned int idhashval;
1582
1583 if (!(sop = alloc_stateowner(&open->op_owner)))
1584 return NULL;
1585 idhashval = ownerid_hashval(current_ownerid);
1586 INIT_LIST_HEAD(&sop->so_idhash);
1587 INIT_LIST_HEAD(&sop->so_strhash);
1588 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001589 INIT_LIST_HEAD(&sop->so_stateids);
1590 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 INIT_LIST_HEAD(&sop->so_close_lru);
1592 sop->so_time = 0;
1593 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1594 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001595 list_add(&sop->so_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 sop->so_is_open_owner = 1;
1597 sop->so_id = current_ownerid++;
1598 sop->so_client = clp;
1599 sop->so_seqid = open->op_seqid;
1600 sop->so_confirmed = 0;
1601 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08001602 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 rp->rp_buflen = 0;
1604 rp->rp_buf = rp->rp_ibuf;
1605 return sop;
1606}
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608static inline void
1609init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1610 struct nfs4_stateowner *sop = open->op_stateowner;
1611 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1612
1613 INIT_LIST_HEAD(&stp->st_hash);
NeilBrownea1da632005-06-23 22:04:17 -07001614 INIT_LIST_HEAD(&stp->st_perstateowner);
1615 INIT_LIST_HEAD(&stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 INIT_LIST_HEAD(&stp->st_perfile);
1617 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001618 list_add(&stp->st_perstateowner, &sop->so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07001619 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07001621 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 stp->st_file = fp;
1623 stp->st_stateid.si_boot = boot_time;
1624 stp->st_stateid.si_stateownerid = sop->so_id;
1625 stp->st_stateid.si_fileid = fp->fi_id;
1626 stp->st_stateid.si_generation = 0;
1627 stp->st_access_bmap = 0;
1628 stp->st_deny_bmap = 0;
1629 __set_bit(open->op_share_access, &stp->st_access_bmap);
1630 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07001631 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635move_to_close_lru(struct nfs4_stateowner *sop)
1636{
1637 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1638
NeilBrown358dd552006-04-10 22:55:42 -07001639 list_move_tail(&sop->so_close_lru, &close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 sop->so_time = get_seconds();
1641}
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001644same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
1645 clientid_t *clid)
1646{
1647 return (sop->so_owner.len == owner->len) &&
1648 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
1649 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
1652static struct nfs4_stateowner *
1653find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1654{
1655 struct nfs4_stateowner *so = NULL;
1656
1657 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001658 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return so;
1660 }
1661 return NULL;
1662}
1663
1664/* search file_hashtbl[] for file */
1665static struct nfs4_file *
1666find_file(struct inode *ino)
1667{
1668 unsigned int hashval = file_hashval(ino);
1669 struct nfs4_file *fp;
1670
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001671 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07001673 if (fp->fi_inode == ino) {
1674 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001675 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07001677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001679 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return NULL;
1681}
1682
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001683static inline int access_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001684{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001685 if (x < NFS4_SHARE_ACCESS_READ)
1686 return 0;
1687 if (x > NFS4_SHARE_ACCESS_BOTH)
1688 return 0;
1689 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001690}
1691
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001692static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001693{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001694 /* Note: unlike access bits, deny bits may be zero. */
1695 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001696}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04001698/*
1699 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1700 * st_{access,deny}_bmap field of the stateid, in order to track not
1701 * only what share bits are currently in force, but also what
1702 * combinations of share bits previous opens have used. This allows us
1703 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1704 * return an error if the client attempt to downgrade to a combination
1705 * of share bits not explicable by closing some of its previous opens.
1706 *
1707 * XXX: This enforcement is actually incomplete, since we don't keep
1708 * track of access/deny bit combinations; so, e.g., we allow:
1709 *
1710 * OPEN allow read, deny write
1711 * OPEN allow both, deny none
1712 * DOWNGRADE allow read, deny none
1713 *
1714 * which we should reject.
1715 */
NeilBrownfd39ca92005-06-23 22:04:03 -07001716static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717set_access(unsigned int *access, unsigned long bmap) {
1718 int i;
1719
1720 *access = 0;
1721 for (i = 1; i < 4; i++) {
1722 if (test_bit(i, &bmap))
1723 *access |= i;
1724 }
1725}
1726
NeilBrownfd39ca92005-06-23 22:04:03 -07001727static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728set_deny(unsigned int *deny, unsigned long bmap) {
1729 int i;
1730
1731 *deny = 0;
1732 for (i = 0; i < 4; i++) {
1733 if (test_bit(i, &bmap))
1734 *deny |= i ;
1735 }
1736}
1737
1738static int
1739test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1740 unsigned int access, deny;
1741
1742 set_access(&access, stp->st_access_bmap);
1743 set_deny(&deny, stp->st_deny_bmap);
1744 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1745 return 0;
1746 return 1;
1747}
1748
1749/*
1750 * Called to check deny when READ with all zero stateid or
1751 * WRITE with all zero or all one stateid
1752 */
Al Virob37ad282006-10-19 23:28:59 -07001753static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1755{
1756 struct inode *ino = current_fh->fh_dentry->d_inode;
1757 struct nfs4_file *fp;
1758 struct nfs4_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07001759 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 dprintk("NFSD: nfs4_share_conflict\n");
1762
1763 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07001764 if (!fp)
1765 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07001766 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07001768 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1769 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1770 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
NeilBrown13cd2182005-06-23 22:03:10 -07001773 ret = nfs_ok;
1774out:
1775 put_nfs4_file(fp);
1776 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779static inline void
1780nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1781{
1782 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
Dave Hansenaceaf782008-02-15 14:37:31 -08001783 drop_file_write_access(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1785 }
1786}
1787
1788/*
1789 * Recall a delegation
1790 */
1791static int
1792do_recall(void *__dp)
1793{
1794 struct nfs4_delegation *dp = __dp;
1795
Meelap Shah47f99402007-07-17 04:04:40 -07001796 dp->dl_file->fi_had_conflict = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 nfsd4_cb_recall(dp);
1798 return 0;
1799}
1800
1801/*
1802 * Spawn a thread to perform a recall on the delegation represented
1803 * by the lease (file_lock)
1804 *
1805 * Called from break_lease() with lock_kernel() held.
1806 * Note: we assume break_lease will only call this *once* for any given
1807 * lease.
1808 */
1809static
1810void nfsd_break_deleg_cb(struct file_lock *fl)
1811{
1812 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1813 struct task_struct *t;
1814
1815 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1816 if (!dp)
1817 return;
1818
1819 /* We're assuming the state code never drops its reference
1820 * without first removing the lease. Since we're in this lease
1821 * callback (and since the lease code is serialized by the kernel
1822 * lock) we know the server hasn't removed the lease yet, we know
1823 * it's safe to take a reference: */
1824 atomic_inc(&dp->dl_count);
J. Bruce Fieldscfdcad42007-09-12 20:35:15 -04001825 atomic_inc(&dp->dl_client->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 spin_lock(&recall_lock);
1828 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1829 spin_unlock(&recall_lock);
1830
1831 /* only place dl_time is set. protected by lock_kernel*/
1832 dp->dl_time = get_seconds();
1833
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04001834 /*
1835 * We don't want the locks code to timeout the lease for us;
1836 * we'll remove it ourself if the delegation isn't returned
1837 * in time.
1838 */
1839 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1842 if (IS_ERR(t)) {
1843 struct nfs4_client *clp = dp->dl_client;
1844
1845 printk(KERN_INFO "NFSD: Callback thread failed for "
1846 "for client (clientid %08x/%08x)\n",
1847 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
J. Bruce Fieldscfdcad42007-09-12 20:35:15 -04001848 put_nfs4_client(dp->dl_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 nfs4_put_delegation(dp);
1850 }
1851}
1852
1853/*
1854 * The file_lock is being reapd.
1855 *
1856 * Called by locks_free_lock() with lock_kernel() held.
1857 */
1858static
1859void nfsd_release_deleg_cb(struct file_lock *fl)
1860{
1861 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1862
1863 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1864
1865 if (!(fl->fl_flags & FL_LEASE) || !dp)
1866 return;
1867 dp->dl_flock = NULL;
1868}
1869
1870/*
1871 * Set the delegation file_lock back pointer.
1872 *
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001873 * Called from setlease() with lock_kernel() held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
1875static
1876void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1877{
1878 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1879
1880 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1881 if (!dp)
1882 return;
1883 dp->dl_flock = new;
1884}
1885
1886/*
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001887 * Called from setlease() with lock_kernel() held
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
1889static
1890int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1891{
1892 struct nfs4_delegation *onlistd =
1893 (struct nfs4_delegation *)onlist->fl_owner;
1894 struct nfs4_delegation *tryd =
1895 (struct nfs4_delegation *)try->fl_owner;
1896
1897 if (onlist->fl_lmops != try->fl_lmops)
1898 return 0;
1899
1900 return onlistd->dl_client == tryd->dl_client;
1901}
1902
1903
1904static
1905int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1906{
1907 if (arg & F_UNLCK)
1908 return lease_modify(onlist, arg);
1909 else
1910 return -EAGAIN;
1911}
1912
NeilBrownfd39ca92005-06-23 22:04:03 -07001913static struct lock_manager_operations nfsd_lease_mng_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 .fl_break = nfsd_break_deleg_cb,
1915 .fl_release_private = nfsd_release_deleg_cb,
1916 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1917 .fl_mylease = nfsd_same_client_deleg_cb,
1918 .fl_change = nfsd_change_deleg_cb,
1919};
1920
1921
Al Virob37ad282006-10-19 23:28:59 -07001922__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923nfsd4_process_open1(struct nfsd4_open *open)
1924{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 clientid_t *clientid = &open->op_clientid;
1926 struct nfs4_client *clp = NULL;
1927 unsigned int strhashval;
1928 struct nfs4_stateowner *sop = NULL;
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001931 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 if (STALE_CLIENTID(&open->op_clientid))
1934 return nfserr_stale_clientid;
1935
1936 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1937 sop = find_openstateowner_str(strhashval, open);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001938 open->op_stateowner = sop;
1939 if (!sop) {
1940 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 clp = find_confirmed_client(clientid);
1942 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001943 return nfserr_expired;
1944 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001946 if (!sop->so_confirmed) {
1947 /* Replace unconfirmed owners without checking for replay. */
1948 clp = sop->so_client;
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05001949 release_openowner(sop);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001950 open->op_stateowner = NULL;
1951 goto renew;
1952 }
1953 if (open->op_seqid == sop->so_seqid - 1) {
1954 if (sop->so_replay.rp_buflen)
Al Viroa90b0612006-10-19 23:29:03 -07001955 return nfserr_replay_me;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001956 /* The original OPEN failed so spectacularly
1957 * that we don't even have replay data saved!
1958 * Therefore, we have no choice but to continue
1959 * processing this OPEN; presumably, we'll
1960 * fail again for the same reason.
1961 */
1962 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1963 goto renew;
1964 }
1965 if (open->op_seqid != sop->so_seqid)
1966 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967renew:
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001968 if (open->op_stateowner == NULL) {
1969 sop = alloc_init_open_stateowner(strhashval, clp, open);
1970 if (sop == NULL)
1971 return nfserr_resource;
1972 open->op_stateowner = sop;
1973 }
1974 list_del_init(&sop->so_close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 renew_client(sop->so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001976 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Al Virob37ad282006-10-19 23:28:59 -07001979static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07001980nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1981{
1982 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1983 return nfserr_openmode;
1984 else
1985 return nfs_ok;
1986}
1987
NeilBrown52f4fb42005-06-23 22:02:49 -07001988static struct nfs4_delegation *
1989find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1990{
1991 struct nfs4_delegation *dp;
1992
NeilBrownea1da632005-06-23 22:04:17 -07001993 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
NeilBrown52f4fb42005-06-23 22:02:49 -07001994 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1995 return dp;
1996 }
1997 return NULL;
1998}
1999
Al Virob37ad282006-10-19 23:28:59 -07002000static __be32
NeilBrown567d9822005-06-23 22:02:53 -07002001nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2002 struct nfs4_delegation **dp)
2003{
2004 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002005 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002006
2007 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2008 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002009 goto out;
NeilBrown567d9822005-06-23 22:02:53 -07002010 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
2011 RD_STATE : WR_STATE;
2012 status = nfs4_check_delegmode(*dp, flags);
2013 if (status)
2014 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002015out:
2016 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2017 return nfs_ok;
2018 if (status)
2019 return status;
2020 open->op_stateowner->so_confirmed = 1;
2021 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002022}
2023
Al Virob37ad282006-10-19 23:28:59 -07002024static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2026{
2027 struct nfs4_stateid *local;
Al Virob37ad282006-10-19 23:28:59 -07002028 __be32 status = nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 struct nfs4_stateowner *sop = open->op_stateowner;
2030
NeilBrown8beefa22005-06-23 22:03:08 -07002031 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /* ignore lock owners */
2033 if (local->st_stateowner->so_is_open_owner == 0)
2034 continue;
2035 /* remember if we have seen this open owner */
2036 if (local->st_stateowner == sop)
2037 *stpp = local;
2038 /* check for conflicting share reservations */
2039 if (!test_share(local, open))
2040 goto out;
2041 }
2042 status = 0;
2043out:
2044 return status;
2045}
2046
NeilBrown5ac049a2005-06-23 22:03:03 -07002047static inline struct nfs4_stateid *
2048nfs4_alloc_stateid(void)
2049{
2050 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2051}
2052
Al Virob37ad282006-10-19 23:28:59 -07002053static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
NeilBrown567d9822005-06-23 22:02:53 -07002055 struct nfs4_delegation *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct svc_fh *cur_fh, int flags)
2057{
2058 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
NeilBrown5ac049a2005-06-23 22:03:03 -07002060 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (stp == NULL)
2062 return nfserr_resource;
2063
NeilBrown567d9822005-06-23 22:02:53 -07002064 if (dp) {
2065 get_file(dp->dl_vfs_file);
2066 stp->st_vfs_file = dp->dl_vfs_file;
2067 } else {
Al Virob37ad282006-10-19 23:28:59 -07002068 __be32 status;
NeilBrown567d9822005-06-23 22:02:53 -07002069 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
2070 &stp->st_vfs_file);
2071 if (status) {
2072 if (status == nfserr_dropit)
2073 status = nfserr_jukebox;
NeilBrown5ac049a2005-06-23 22:03:03 -07002074 kmem_cache_free(stateid_slab, stp);
NeilBrown567d9822005-06-23 22:02:53 -07002075 return status;
2076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 *stpp = stp;
2079 return 0;
2080}
2081
Al Virob37ad282006-10-19 23:28:59 -07002082static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2084 struct nfsd4_open *open)
2085{
2086 struct iattr iattr = {
2087 .ia_valid = ATTR_SIZE,
2088 .ia_size = 0,
2089 };
2090 if (!open->op_truncate)
2091 return 0;
2092 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002093 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2095}
2096
Al Virob37ad282006-10-19 23:28:59 -07002097static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
2099{
2100 struct file *filp = stp->st_vfs_file;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08002101 struct inode *inode = filp->f_path.dentry->d_inode;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002102 unsigned int share_access, new_writer;
Al Virob37ad282006-10-19 23:28:59 -07002103 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 set_access(&share_access, stp->st_access_bmap);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002106 new_writer = (~share_access) & open->op_share_access
2107 & NFS4_SHARE_ACCESS_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002109 if (new_writer) {
Al Virob37ad282006-10-19 23:28:59 -07002110 int err = get_write_access(inode);
2111 if (err)
2112 return nfserrno(err);
Benny Halevye518f052008-07-04 15:38:41 +03002113 err = mnt_want_write(cur_fh->fh_export->ex_path.mnt);
2114 if (err)
2115 return nfserrno(err);
2116 file_take_write(filp);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 status = nfsd4_truncate(rqstp, cur_fh, open);
2119 if (status) {
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002120 if (new_writer)
2121 put_write_access(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return status;
2123 }
2124 /* remember the open */
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002125 filp->f_mode |= open->op_share_access;
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002126 __set_bit(open->op_share_access, &stp->st_access_bmap);
2127 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 return nfs_ok;
2130}
2131
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133static void
NeilBrown37515172005-07-07 17:59:16 -07002134nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
NeilBrown37515172005-07-07 17:59:16 -07002136 open->op_stateowner->so_confirmed = 1;
2137 open->op_stateowner->so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
2140/*
2141 * Attempt to hand out a delegation.
2142 */
2143static void
2144nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2145{
2146 struct nfs4_delegation *dp;
2147 struct nfs4_stateowner *sop = stp->st_stateowner;
2148 struct nfs4_callback *cb = &sop->so_client->cl_callback;
2149 struct file_lock fl, *flp = &fl;
2150 int status, flag = 0;
2151
2152 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002153 open->op_recall = 0;
2154 switch (open->op_claim_type) {
2155 case NFS4_OPEN_CLAIM_PREVIOUS:
2156 if (!atomic_read(&cb->cb_set))
2157 open->op_recall = 1;
2158 flag = open->op_delegate_type;
2159 if (flag == NFS4_OPEN_DELEGATE_NONE)
2160 goto out;
2161 break;
2162 case NFS4_OPEN_CLAIM_NULL:
2163 /* Let's not give out any delegations till everyone's
2164 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002165 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002166 goto out;
2167 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
2168 goto out;
2169 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2170 flag = NFS4_OPEN_DELEGATE_WRITE;
2171 else
2172 flag = NFS4_OPEN_DELEGATE_READ;
2173 break;
2174 default:
2175 goto out;
2176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
2179 if (dp == NULL) {
2180 flag = NFS4_OPEN_DELEGATE_NONE;
2181 goto out;
2182 }
2183 locks_init_lock(&fl);
2184 fl.fl_lmops = &nfsd_lease_mng_ops;
2185 fl.fl_flags = FL_LEASE;
Felix Blyakher9167f502008-02-26 10:54:36 -08002186 fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 fl.fl_end = OFFSET_MAX;
2188 fl.fl_owner = (fl_owner_t)dp;
2189 fl.fl_file = stp->st_vfs_file;
2190 fl.fl_pid = current->tgid;
2191
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04002192 /* vfs_setlease checks to see if delegation should be handed out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 * the lock_manager callbacks fl_mylease and fl_change are used
2194 */
Felix Blyakher9167f502008-02-26 10:54:36 -08002195 if ((status = vfs_setlease(stp->st_vfs_file, fl.fl_type, &flp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
NeilBrownc9071322005-04-16 15:26:38 -07002197 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 flag = NFS4_OPEN_DELEGATE_NONE;
2199 goto out;
2200 }
2201
2202 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2203
2204 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
2205 dp->dl_stateid.si_boot,
2206 dp->dl_stateid.si_stateownerid,
2207 dp->dl_stateid.si_fileid,
2208 dp->dl_stateid.si_generation);
2209out:
NeilBrown7b190fe2005-06-23 22:03:23 -07002210 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2211 && flag == NFS4_OPEN_DELEGATE_NONE
2212 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002213 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 open->op_delegate_type = flag;
2215}
2216
2217/*
2218 * called with nfs4_lock_state() held.
2219 */
Al Virob37ad282006-10-19 23:28:59 -07002220__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2222{
2223 struct nfs4_file *fp = NULL;
2224 struct inode *ino = current_fh->fh_dentry->d_inode;
2225 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07002226 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002227 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 status = nfserr_inval;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002230 if (!access_valid(open->op_share_access)
2231 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 goto out;
2233 /*
2234 * Lookup file; if found, lookup stateid and check open request,
2235 * and check for delegations in the process of being recalled.
2236 * If not found, create the nfs4_file struct
2237 */
2238 fp = find_file(ino);
2239 if (fp) {
2240 if ((status = nfs4_check_open(fp, open, &stp)))
2241 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002242 status = nfs4_check_deleg(fp, open, &dp);
2243 if (status)
2244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07002246 status = nfserr_bad_stateid;
2247 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 status = nfserr_resource;
2250 fp = alloc_init_file(ino);
2251 if (fp == NULL)
2252 goto out;
2253 }
2254
2255 /*
2256 * OPEN the file, or upgrade an existing OPEN.
2257 * If truncate fails, the OPEN fails.
2258 */
2259 if (stp) {
2260 /* Stateid was found, this is an OPEN upgrade */
2261 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
2262 if (status)
2263 goto out;
NeilBrown444c2c02005-07-07 17:59:22 -07002264 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 } else {
2266 /* Stateid was not found, this is a new OPEN */
2267 int flags = 0;
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -07002268 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002269 flags |= NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002271 flags |= NFSD_MAY_WRITE;
NeilBrown567d9822005-06-23 22:02:53 -07002272 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
2273 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 goto out;
2275 init_stateid(stp, fp, open);
2276 status = nfsd4_truncate(rqstp, current_fh, open);
2277 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05002278 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 goto out;
2280 }
2281 }
2282 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2283
2284 /*
2285 * Attempt to hand out a delegation. No error return, because the
2286 * OPEN succeeds even if we fail.
2287 */
2288 nfs4_open_delegation(current_fh, open, stp);
2289
2290 status = nfs_ok;
2291
2292 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
2293 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
2294 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
2295out:
NeilBrown13cd2182005-06-23 22:03:10 -07002296 if (fp)
2297 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07002298 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2299 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /*
2301 * To finish the open response, we just need to set the rflags.
2302 */
2303 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
2304 if (!open->op_stateowner->so_confirmed)
2305 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2306
2307 return status;
2308}
2309
Al Virob37ad282006-10-19 23:28:59 -07002310__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002311nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2312 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07002315 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 nfs4_lock_state();
2318 dprintk("process_renew(%08x/%08x): starting\n",
2319 clid->cl_boot, clid->cl_id);
2320 status = nfserr_stale_clientid;
2321 if (STALE_CLIENTID(clid))
2322 goto out;
2323 clp = find_confirmed_client(clid);
2324 status = nfserr_expired;
2325 if (clp == NULL) {
2326 /* We assume the client took too long to RENEW. */
2327 dprintk("nfsd4_renew: clientid not found!\n");
2328 goto out;
2329 }
2330 renew_client(clp);
2331 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07002332 if (!list_empty(&clp->cl_delegations)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 && !atomic_read(&clp->cl_callback.cb_set))
2334 goto out;
2335 status = nfs_ok;
2336out:
2337 nfs4_unlock_state();
2338 return status;
2339}
2340
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002341struct lock_manager nfsd4_manager = {
2342};
2343
NeilBrowna76b4312005-06-23 22:04:01 -07002344static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002345nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07002346{
2347 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07002348 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002349 locks_end_grace(&nfsd4_manager);
NeilBrowna76b4312005-06-23 22:04:01 -07002350}
2351
NeilBrownfd39ca92005-06-23 22:04:03 -07002352static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353nfs4_laundromat(void)
2354{
2355 struct nfs4_client *clp;
2356 struct nfs4_stateowner *sop;
2357 struct nfs4_delegation *dp;
2358 struct list_head *pos, *next, reaplist;
2359 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
2360 time_t t, clientid_val = NFSD_LEASE_TIME;
2361 time_t u, test_val = NFSD_LEASE_TIME;
2362
2363 nfs4_lock_state();
2364
2365 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002366 if (locks_in_grace())
2367 nfsd4_end_grace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 list_for_each_safe(pos, next, &client_lru) {
2369 clp = list_entry(pos, struct nfs4_client, cl_lru);
2370 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
2371 t = clp->cl_time - cutoff;
2372 if (clientid_val > t)
2373 clientid_val = t;
2374 break;
2375 }
2376 dprintk("NFSD: purging unused client (clientid %08x)\n",
2377 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07002378 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 expire_client(clp);
2380 }
2381 INIT_LIST_HEAD(&reaplist);
2382 spin_lock(&recall_lock);
2383 list_for_each_safe(pos, next, &del_recall_lru) {
2384 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2385 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
2386 u = dp->dl_time - cutoff;
2387 if (test_val > u)
2388 test_val = u;
2389 break;
2390 }
2391 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
2392 dp, dp->dl_flock);
2393 list_move(&dp->dl_recall_lru, &reaplist);
2394 }
2395 spin_unlock(&recall_lock);
2396 list_for_each_safe(pos, next, &reaplist) {
2397 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2398 list_del_init(&dp->dl_recall_lru);
2399 unhash_delegation(dp);
2400 }
2401 test_val = NFSD_LEASE_TIME;
2402 list_for_each_safe(pos, next, &close_lru) {
2403 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
2404 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
2405 u = sop->so_time - cutoff;
2406 if (test_val > u)
2407 test_val = u;
2408 break;
2409 }
2410 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
2411 sop->so_id);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05002412 release_openowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
2414 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
2415 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
2416 nfs4_unlock_state();
2417 return clientid_val;
2418}
2419
Harvey Harrisona254b242008-02-20 12:49:00 -08002420static struct workqueue_struct *laundry_wq;
2421static void laundromat_main(struct work_struct *);
2422static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
2423
2424static void
David Howellsc4028952006-11-22 14:57:56 +00002425laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
2427 time_t t;
2428
2429 t = nfs4_laundromat();
2430 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07002431 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
NeilBrownfd39ca92005-06-23 22:04:03 -07002434static struct nfs4_stateowner *
NeilBrownf8816512005-07-07 17:59:25 -07002435search_close_lru(u32 st_id, int flags)
2436{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 struct nfs4_stateowner *local = NULL;
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (flags & CLOSE_STATE) {
2440 list_for_each_entry(local, &close_lru, so_close_lru) {
2441 if (local->so_id == st_id)
2442 return local;
2443 }
2444 }
2445 return NULL;
2446}
2447
2448static inline int
2449nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2450{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08002451 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
2454static int
2455STALE_STATEID(stateid_t *stateid)
2456{
2457 if (stateid->si_boot == boot_time)
2458 return 0;
Neil Brown849823c2005-09-13 01:25:36 -07002459 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2461 stateid->si_generation);
2462 return 1;
2463}
2464
2465static inline int
2466access_permit_read(unsigned long access_bmap)
2467{
2468 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2469 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2470 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2471}
2472
2473static inline int
2474access_permit_write(unsigned long access_bmap)
2475{
2476 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2477 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2478}
2479
2480static
Al Virob37ad282006-10-19 23:28:59 -07002481__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
Al Virob37ad282006-10-19 23:28:59 -07002483 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2486 goto out;
2487 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2488 goto out;
2489 status = nfs_ok;
2490out:
2491 return status;
2492}
2493
Al Virob37ad282006-10-19 23:28:59 -07002494static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2496{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002497 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002499 else if (locks_in_grace()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 /* Answer in remaining cases depends on existance of
2501 * conflicting state; so we must wait out the grace period. */
2502 return nfserr_grace;
2503 } else if (flags & WR_STATE)
2504 return nfs4_share_conflict(current_fh,
2505 NFS4_SHARE_DENY_WRITE);
2506 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2507 return nfs4_share_conflict(current_fh,
2508 NFS4_SHARE_DENY_READ);
2509}
2510
2511/*
2512 * Allow READ/WRITE during grace period on recovered state only for files
2513 * that are not able to provide mandatory locking.
2514 */
2515static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08002516grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002518 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
J. Bruce Fields0836f582008-01-26 19:08:12 -05002521static int check_stateid_generation(stateid_t *in, stateid_t *ref)
2522{
2523 /* If the client sends us a stateid from the future, it's buggy: */
2524 if (in->si_generation > ref->si_generation)
2525 return nfserr_bad_stateid;
2526 /*
2527 * The following, however, can happen. For example, if the
2528 * client sends an open and some IO at the same time, the open
2529 * may bump si_generation while the IO is still in flight.
2530 * Thanks to hard links and renames, the client never knows what
2531 * file an open will affect. So it could avoid that situation
2532 * only by serializing all opens and IO from the same open
2533 * owner. To recover from the old_stateid error, the client
2534 * will just have to retry the IO:
2535 */
2536 if (in->si_generation < ref->si_generation)
2537 return nfserr_old_stateid;
2538 return nfs_ok;
2539}
2540
J. Bruce Fields3e633072009-02-21 13:17:19 -08002541static int is_delegation_stateid(stateid_t *stateid)
2542{
2543 return stateid->si_fileid == 0;
2544}
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546/*
2547* Checks for stateid operations
2548*/
Al Virob37ad282006-10-19 23:28:59 -07002549__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2551{
2552 struct nfs4_stateid *stp = NULL;
2553 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07002555 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (filpp)
2558 *filpp = NULL;
2559
J. Bruce Fields18f82732009-02-21 15:23:01 -08002560 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return nfserr_grace;
2562
2563 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2564 return check_special_stateids(current_fh, stateid, flags);
2565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 status = nfserr_stale_stateid;
2567 if (STALE_STATEID(stateid))
2568 goto out;
2569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 status = nfserr_bad_stateid;
J. Bruce Fields3e633072009-02-21 13:17:19 -08002571 if (is_delegation_stateid(stateid)) {
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08002572 dp = find_delegation_stateid(ino, stateid);
J. Bruce Fields819a8f52009-02-21 12:13:24 -08002573 if (!dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 goto out;
J. Bruce Fieldsfd03b092009-02-21 11:27:30 -08002575 status = check_stateid_generation(stateid, &dp->dl_stateid);
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08002576 if (status)
2577 goto out;
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08002578 status = nfs4_check_delegmode(dp, flags);
2579 if (status)
2580 goto out;
2581 renew_client(dp->dl_client);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08002582 if (filpp)
2583 *filpp = dp->dl_vfs_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 } else { /* open or lock stateid */
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08002585 stp = find_stateid(stateid, flags);
J. Bruce Fields819a8f52009-02-21 12:13:24 -08002586 if (!stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 goto out;
J. Bruce Fields6150ef02009-02-21 13:36:16 -08002588 if (nfs4_check_fh(current_fh, stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 goto out;
2590 if (!stp->st_stateowner->so_confirmed)
2591 goto out;
J. Bruce Fieldsfd03b092009-02-21 11:27:30 -08002592 status = check_stateid_generation(stateid, &stp->st_stateid);
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08002593 if (status)
2594 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08002595 status = nfs4_check_openmode(stp, flags);
2596 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 goto out;
2598 renew_client(stp->st_stateowner->so_client);
2599 if (filpp)
2600 *filpp = stp->st_vfs_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
2602 status = nfs_ok;
2603out:
2604 return status;
2605}
2606
NeilBrown4c4cd222005-07-07 17:59:27 -07002607static inline int
2608setlkflg (int type)
2609{
2610 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2611 RD_STATE : WR_STATE;
2612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614/*
2615 * Checks for sequence id mutating operations.
2616 */
Al Virob37ad282006-10-19 23:28:59 -07002617static __be32
NeilBrown4c4cd222005-07-07 17:59:27 -07002618nfs4_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 -07002619{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 struct nfs4_stateid *stp;
2621 struct nfs4_stateowner *sop;
J. Bruce Fields0836f582008-01-26 19:08:12 -05002622 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2625 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2626 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2627 stateid->si_generation);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 *stpp = NULL;
2630 *sopp = NULL;
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002633 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002634 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 }
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (STALE_STATEID(stateid))
NeilBrown3a4f98b2005-07-07 17:59:26 -07002638 return nfserr_stale_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 /*
2640 * We return BAD_STATEID if filehandle doesn't match stateid,
2641 * the confirmed flag is incorrecly set, or the generation
2642 * number is incorrect.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 */
NeilBrownf8816512005-07-07 17:59:25 -07002644 stp = find_stateid(stateid, flags);
2645 if (stp == NULL) {
2646 /*
2647 * Also, we should make sure this isn't just the result of
2648 * a replayed close:
2649 */
2650 sop = search_close_lru(stateid->si_stateownerid, flags);
2651 if (sop == NULL)
2652 return nfserr_bad_stateid;
2653 *sopp = sop;
2654 goto check_replay;
2655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
J. Bruce Fields39325bd2007-11-26 17:06:39 -05002657 *stpp = stp;
2658 *sopp = sop = stp->st_stateowner;
2659
NeilBrown4c4cd222005-07-07 17:59:27 -07002660 if (lock) {
NeilBrown4c4cd222005-07-07 17:59:27 -07002661 clientid_t *lockclid = &lock->v.new.clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 struct nfs4_client *clp = sop->so_client;
NeilBrown4c4cd222005-07-07 17:59:27 -07002663 int lkflg = 0;
Al Virob37ad282006-10-19 23:28:59 -07002664 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
NeilBrown4c4cd222005-07-07 17:59:27 -07002666 lkflg = setlkflg(lock->lk_type);
2667
2668 if (lock->lk_is_new) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002669 if (!sop->so_is_open_owner)
2670 return nfserr_bad_stateid;
2671 if (!same_clid(&clp->cl_clientid, lockclid))
NeilBrown4c4cd222005-07-07 17:59:27 -07002672 return nfserr_bad_stateid;
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002673 /* stp is the open stateid */
2674 status = nfs4_check_openmode(stp, lkflg);
2675 if (status)
2676 return status;
2677 } else {
2678 /* stp is the lock stateid */
2679 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2680 if (status)
2681 return status;
NeilBrown4c4cd222005-07-07 17:59:27 -07002682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
2684
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002685 if (nfs4_check_fh(current_fh, stp)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002686 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002687 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 /*
2691 * We now validate the seqid and stateid generation numbers.
2692 * For the moment, we ignore the possibility of
2693 * generation number wraparound.
2694 */
NeilBrownbd9aac52005-07-07 17:59:19 -07002695 if (seqid != sop->so_seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 goto check_replay;
2697
NeilBrown3a4f98b2005-07-07 17:59:26 -07002698 if (sop->so_confirmed && flags & CONFIRM) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002699 dprintk("NFSD: preprocess_seqid_op: expected"
NeilBrown3a4f98b2005-07-07 17:59:26 -07002700 " unconfirmed stateowner!\n");
2701 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07002703 if (!sop->so_confirmed && !(flags & CONFIRM)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002704 dprintk("NFSD: preprocess_seqid_op: stateowner not"
NeilBrown3a4f98b2005-07-07 17:59:26 -07002705 " confirmed yet!\n");
2706 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
J. Bruce Fields0836f582008-01-26 19:08:12 -05002708 status = check_stateid_generation(stateid, &stp->st_stateid);
2709 if (status)
2710 return status;
NeilBrown52fd0042005-07-07 17:59:24 -07002711 renew_client(sop->so_client);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002712 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714check_replay:
NeilBrownbd9aac52005-07-07 17:59:19 -07002715 if (seqid == sop->so_seqid - 1) {
Neil Brown849823c2005-09-13 01:25:36 -07002716 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 /* indicate replay to calling function */
Al Viroa90b0612006-10-19 23:29:03 -07002718 return nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002720 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
NeilBrown3a4f98b2005-07-07 17:59:26 -07002721 sop->so_seqid, seqid);
2722 *sopp = NULL;
2723 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
Al Virob37ad282006-10-19 23:28:59 -07002726__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002727nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002728 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
Al Virob37ad282006-10-19 23:28:59 -07002730 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 struct nfs4_stateowner *sop;
2732 struct nfs4_stateid *stp;
2733
2734 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002735 (int)cstate->current_fh.fh_dentry->d_name.len,
2736 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002738 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07002739 if (status)
2740 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 nfs4_lock_state();
2743
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002744 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2745 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002746 CONFIRM | OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 &oc->oc_stateowner, &stp, NULL)))
2748 goto out;
2749
2750 sop = oc->oc_stateowner;
2751 sop->so_confirmed = 1;
2752 update_stateid(&stp->st_stateid);
2753 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2754 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2755 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2756 stp->st_stateid.si_boot,
2757 stp->st_stateid.si_stateownerid,
2758 stp->st_stateid.si_fileid,
2759 stp->st_stateid.si_generation);
NeilBrownc7b9a452005-06-23 22:04:30 -07002760
2761 nfsd4_create_clid_dir(sop->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762out:
Neil Brownf2327d92005-09-13 01:25:37 -07002763 if (oc->oc_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 nfs4_get_stateowner(oc->oc_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002765 cstate->replay_owner = oc->oc_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 nfs4_unlock_state();
2768 return status;
2769}
2770
2771
2772/*
2773 * unset all bits in union bitmap (bmap) that
2774 * do not exist in share (from successful OPEN_DOWNGRADE)
2775 */
2776static void
2777reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2778{
2779 int i;
2780 for (i = 1; i < 4; i++) {
2781 if ((i & access) != i)
2782 __clear_bit(i, bmap);
2783 }
2784}
2785
2786static void
2787reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2788{
2789 int i;
2790 for (i = 0; i < 4; i++) {
2791 if ((i & deny) != i)
2792 __clear_bit(i, bmap);
2793 }
2794}
2795
Al Virob37ad282006-10-19 23:28:59 -07002796__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002797nfsd4_open_downgrade(struct svc_rqst *rqstp,
2798 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002799 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
Al Virob37ad282006-10-19 23:28:59 -07002801 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 struct nfs4_stateid *stp;
2803 unsigned int share_access;
2804
2805 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002806 (int)cstate->current_fh.fh_dentry->d_name.len,
2807 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002809 if (!access_valid(od->od_share_access)
2810 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return nfserr_inval;
2812
2813 nfs4_lock_state();
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002814 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2815 od->od_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 &od->od_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002817 OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 &od->od_stateowner, &stp, NULL)))
2819 goto out;
2820
2821 status = nfserr_inval;
2822 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2823 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2824 stp->st_access_bmap, od->od_share_access);
2825 goto out;
2826 }
2827 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2828 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2829 stp->st_deny_bmap, od->od_share_deny);
2830 goto out;
2831 }
2832 set_access(&share_access, stp->st_access_bmap);
2833 nfs4_file_downgrade(stp->st_vfs_file,
2834 share_access & ~od->od_share_access);
2835
2836 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2837 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2838
2839 update_stateid(&stp->st_stateid);
2840 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2841 status = nfs_ok;
2842out:
Neil Brownf2327d92005-09-13 01:25:37 -07002843 if (od->od_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 nfs4_get_stateowner(od->od_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002845 cstate->replay_owner = od->od_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 nfs4_unlock_state();
2848 return status;
2849}
2850
2851/*
2852 * nfs4_unlock_state() called after encode
2853 */
Al Virob37ad282006-10-19 23:28:59 -07002854__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002855nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002856 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Al Virob37ad282006-10-19 23:28:59 -07002858 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct nfs4_stateid *stp;
2860
2861 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002862 (int)cstate->current_fh.fh_dentry->d_name.len,
2863 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
2865 nfs4_lock_state();
2866 /* check close_lru for replay */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002867 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2868 close->cl_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 &close->cl_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002870 OPEN_STATE | CLOSE_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 &close->cl_stateowner, &stp, NULL)))
2872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 status = nfs_ok;
2874 update_stateid(&stp->st_stateid);
2875 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2876
J. Bruce Fields04ef5952006-01-18 17:43:21 -08002877 /* release_stateid() calls nfsd_close() if needed */
J. Bruce Fields22839632009-01-11 14:27:17 -05002878 release_open_stateid(stp);
J. Bruce Fields04ef5952006-01-18 17:43:21 -08002879
2880 /* place unused nfs4_stateowners on so_close_lru list to be
2881 * released by the laundromat service after the lease period
2882 * to enable us to handle CLOSE replay
2883 */
2884 if (list_empty(&close->cl_stateowner->so_stateids))
2885 move_to_close_lru(close->cl_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886out:
Neil Brownf2327d92005-09-13 01:25:37 -07002887 if (close->cl_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 nfs4_get_stateowner(close->cl_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002889 cstate->replay_owner = close->cl_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 nfs4_unlock_state();
2892 return status;
2893}
2894
Al Virob37ad282006-10-19 23:28:59 -07002895__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002896nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2897 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002899 struct nfs4_delegation *dp;
2900 stateid_t *stateid = &dr->dr_stateid;
2901 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07002902 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002904 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002905 return status;
2906 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 nfs4_lock_state();
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002909 status = nfserr_bad_stateid;
2910 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2911 goto out;
2912 status = nfserr_stale_stateid;
2913 if (STALE_STATEID(stateid))
2914 goto out;
J. Bruce Fields7e0f7cf2009-02-21 13:32:28 -08002915 status = nfserr_bad_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002916 if (!is_delegation_stateid(stateid))
2917 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002918 dp = find_delegation_stateid(inode, stateid);
2919 if (!dp)
2920 goto out;
2921 status = check_stateid_generation(stateid, &dp->dl_stateid);
2922 if (status)
2923 goto out;
2924 renew_client(dp->dl_client);
2925
2926 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002928 nfs4_unlock_state();
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return status;
2931}
2932
2933
2934/*
2935 * Lock owner state (byte-range locks)
2936 */
2937#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2938#define LOCK_HASH_BITS 8
2939#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2940#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2941
Benny Halevy87df4de2008-12-15 19:42:03 +02002942static inline u64
2943end_offset(u64 start, u64 len)
2944{
2945 u64 end;
2946
2947 end = start + len;
2948 return end >= start ? end: NFS4_MAX_UINT64;
2949}
2950
2951/* last octet in a range */
2952static inline u64
2953last_byte_offset(u64 start, u64 len)
2954{
2955 u64 end;
2956
2957 BUG_ON(!len);
2958 end = start + len;
2959 return end > start ? end - 1: NFS4_MAX_UINT64;
2960}
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962#define lockownerid_hashval(id) \
2963 ((id) & LOCK_HASH_MASK)
2964
2965static inline unsigned int
2966lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2967 struct xdr_netobj *ownername)
2968{
2969 return (file_hashval(inode) + cl_id
2970 + opaque_hashval(ownername->data, ownername->len))
2971 & LOCK_HASH_MASK;
2972}
2973
2974static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2975static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2976static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2977
NeilBrownfd39ca92005-06-23 22:04:03 -07002978static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979find_stateid(stateid_t *stid, int flags)
2980{
Krishna Kumar9346eff2008-10-20 11:44:28 +05302981 struct nfs4_stateid *local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 u32 st_id = stid->si_stateownerid;
2983 u32 f_id = stid->si_fileid;
2984 unsigned int hashval;
2985
2986 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
Krishna Kumar9346eff2008-10-20 11:44:28 +05302987 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 hashval = stateid_hashval(st_id, f_id);
2989 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2990 if ((local->st_stateid.si_stateownerid == st_id) &&
2991 (local->st_stateid.si_fileid == f_id))
2992 return local;
2993 }
2994 }
Krishna Kumar9346eff2008-10-20 11:44:28 +05302995
2996 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 hashval = stateid_hashval(st_id, f_id);
2998 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2999 if ((local->st_stateid.si_stateownerid == st_id) &&
3000 (local->st_stateid.si_fileid == f_id))
3001 return local;
3002 }
Neil Brown849823c2005-09-13 01:25:36 -07003003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return NULL;
3005}
3006
3007static struct nfs4_delegation *
3008find_delegation_stateid(struct inode *ino, stateid_t *stid)
3009{
NeilBrown13cd2182005-06-23 22:03:10 -07003010 struct nfs4_file *fp;
3011 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
3014 stid->si_boot, stid->si_stateownerid,
3015 stid->si_fileid, stid->si_generation);
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07003018 if (!fp)
3019 return NULL;
3020 dl = find_delegation_file(fp, stid);
3021 put_nfs4_file(fp);
3022 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024
3025/*
3026 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3027 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3028 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3029 * locking, this prevents us from being completely protocol-compliant. The
3030 * real solution to this problem is to start using unsigned file offsets in
3031 * the VFS, but this is a very deep change!
3032 */
3033static inline void
3034nfs4_transform_lock_offset(struct file_lock *lock)
3035{
3036 if (lock->fl_start < 0)
3037 lock->fl_start = OFFSET_MAX;
3038 if (lock->fl_end < 0)
3039 lock->fl_end = OFFSET_MAX;
3040}
3041
NeilBrownd5b90262006-04-10 22:55:22 -07003042/* Hack!: For now, we're defining this just so we can use a pointer to it
3043 * as a unique cookie to identify our (NFSv4's) posix locks. */
Adrian Bunke465a772006-04-10 22:55:23 -07003044static struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003045};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
3047static inline void
3048nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3049{
NeilBrownd5b90262006-04-10 22:55:22 -07003050 struct nfs4_stateowner *sop;
3051 unsigned int hval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
NeilBrownd5b90262006-04-10 22:55:22 -07003053 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3054 sop = (struct nfs4_stateowner *) fl->fl_owner;
3055 hval = lockownerid_hashval(sop->so_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 kref_get(&sop->so_ref);
3057 deny->ld_sop = sop;
3058 deny->ld_clientid = sop->so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003059 } else {
3060 deny->ld_sop = NULL;
3061 deny->ld_clientid.cl_boot = 0;
3062 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
3064 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003065 deny->ld_length = NFS4_MAX_UINT64;
3066 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3068 deny->ld_type = NFS4_READ_LT;
3069 if (fl->fl_type != F_RDLCK)
3070 deny->ld_type = NFS4_WRITE_LT;
3071}
3072
3073static struct nfs4_stateowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074find_lockstateowner_str(struct inode *inode, clientid_t *clid,
3075 struct xdr_netobj *owner)
3076{
3077 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3078 struct nfs4_stateowner *op;
3079
3080 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003081 if (same_owner_str(op, owner, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 return op;
3083 }
3084 return NULL;
3085}
3086
3087/*
3088 * Alloc a lock owner structure.
3089 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3090 * occured.
3091 *
3092 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 */
3094
3095static struct nfs4_stateowner *
3096alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3097 struct nfs4_stateowner *sop;
3098 struct nfs4_replay *rp;
3099 unsigned int idhashval;
3100
3101 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
3102 return NULL;
3103 idhashval = lockownerid_hashval(current_ownerid);
3104 INIT_LIST_HEAD(&sop->so_idhash);
3105 INIT_LIST_HEAD(&sop->so_strhash);
3106 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07003107 INIT_LIST_HEAD(&sop->so_stateids);
3108 INIT_LIST_HEAD(&sop->so_perstateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
3110 sop->so_time = 0;
3111 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
3112 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07003113 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 sop->so_is_open_owner = 0;
3115 sop->so_id = current_ownerid++;
3116 sop->so_client = clp;
Neil Brownb59e3c02005-09-13 01:25:38 -07003117 /* It is the openowner seqid that will be incremented in encode in the
3118 * case of new lockowners; so increment the lock seqid manually: */
3119 sop->so_seqid = lock->lk_new_lock_seqid + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 sop->so_confirmed = 1;
3121 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08003122 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 rp->rp_buflen = 0;
3124 rp->rp_buf = rp->rp_ibuf;
3125 return sop;
3126}
3127
NeilBrownfd39ca92005-06-23 22:04:03 -07003128static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3130{
3131 struct nfs4_stateid *stp;
3132 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
3133
NeilBrown5ac049a2005-06-23 22:03:03 -07003134 stp = nfs4_alloc_stateid();
3135 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 goto out;
3137 INIT_LIST_HEAD(&stp->st_hash);
3138 INIT_LIST_HEAD(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07003139 INIT_LIST_HEAD(&stp->st_perstateowner);
3140 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07003142 list_add(&stp->st_perfile, &fp->fi_stateids);
NeilBrownea1da632005-06-23 22:04:17 -07003143 list_add(&stp->st_perstateowner, &sop->so_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07003145 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 stp->st_file = fp;
3147 stp->st_stateid.si_boot = boot_time;
3148 stp->st_stateid.si_stateownerid = sop->so_id;
3149 stp->st_stateid.si_fileid = fp->fi_id;
3150 stp->st_stateid.si_generation = 0;
3151 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
3152 stp->st_access_bmap = open_stp->st_access_bmap;
3153 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003154 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
3156out:
3157 return stp;
3158}
3159
NeilBrownfd39ca92005-06-23 22:04:03 -07003160static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161check_lock_length(u64 offset, u64 length)
3162{
Benny Halevy87df4de2008-12-15 19:42:03 +02003163 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 LOFF_OVERFLOW(offset, length)));
3165}
3166
3167/*
3168 * LOCK operation
3169 */
Al Virob37ad282006-10-19 23:28:59 -07003170__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003171nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003172 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
NeilBrown3e9e3db2005-06-23 22:04:20 -07003174 struct nfs4_stateowner *open_sop = NULL;
Neil Brownb59e3c02005-09-13 01:25:38 -07003175 struct nfs4_stateowner *lock_sop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 struct nfs4_stateid *lock_stp;
3177 struct file *filp;
3178 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05003179 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07003180 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 unsigned int strhashval;
Marc Eshel150b3932007-01-18 16:15:35 -05003182 unsigned int cmd;
Al Virob8dd7b92006-10-19 23:29:01 -07003183 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3186 (long long) lock->lk_offset,
3187 (long long) lock->lk_length);
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (check_lock_length(lock->lk_offset, lock->lk_length))
3190 return nfserr_inval;
3191
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003192 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02003193 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08003194 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3195 return status;
3196 }
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 nfs4_lock_state();
3199
3200 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07003201 /*
3202 * Client indicates that this is a new lockowner.
3203 * Use open owner and open stateid to create lock owner and
3204 * lock stateid.
3205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 struct nfs4_stateid *open_stp = NULL;
3207 struct nfs4_file *fp;
3208
3209 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07003210 if (STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 /* validate and update open stateid and open seqid */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003214 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 lock->lk_new_open_seqid,
3216 &lock->lk_new_open_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003217 OPEN_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08003218 &lock->lk_replay_owner, &open_stp,
Neil Brownb59e3c02005-09-13 01:25:38 -07003219 lock);
NeilBrown37515172005-07-07 17:59:16 -07003220 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08003222 open_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 /* create lockowner and lock stateid */
3224 fp = open_stp->st_file;
3225 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3226 open_sop->so_client->cl_clientid.cl_id,
3227 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003228 /* XXX: Do we need to check for duplicate stateowners on
3229 * the same file, or should they just be allowed (and
3230 * create new stateids)? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 status = nfserr_resource;
Neil Brownb59e3c02005-09-13 01:25:38 -07003232 lock_sop = alloc_init_lock_stateowner(strhashval,
3233 open_sop->so_client, open_stp, lock);
3234 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07003236 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08003237 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 } else {
3240 /* lock (lock owner + lock stateid) already exists */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003241 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 lock->lk_old_lock_seqid,
3243 &lock->lk_old_lock_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003244 LOCK_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08003245 &lock->lk_replay_owner, &lock_stp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 if (status)
3247 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08003248 lock_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 }
J. Bruce Fields3a655882006-01-18 17:43:19 -08003250 /* lock->lk_replay_owner and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 filp = lock_stp->st_vfs_file;
3252
NeilBrown0dd395d2005-07-07 17:59:15 -07003253 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003254 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07003255 goto out;
3256 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003257 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07003258 goto out;
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 locks_init_lock(&file_lock);
3261 switch (lock->lk_type) {
3262 case NFS4_READ_LT:
3263 case NFS4_READW_LT:
3264 file_lock.fl_type = F_RDLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05003265 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 break;
3267 case NFS4_WRITE_LT:
3268 case NFS4_WRITEW_LT:
3269 file_lock.fl_type = F_WRLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05003270 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 break;
3272 default:
3273 status = nfserr_inval;
3274 goto out;
3275 }
Neil Brownb59e3c02005-09-13 01:25:38 -07003276 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 file_lock.fl_pid = current->tgid;
3278 file_lock.fl_file = filp;
3279 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07003280 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
3282 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02003283 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 nfs4_transform_lock_offset(&file_lock);
3285
3286 /*
3287 * Try to lock the file in the VFS.
3288 * Note: locks.c uses the BKL to protect the inode's lock list.
3289 */
3290
Marc Eshelfd85b812006-11-28 16:26:41 -05003291 err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07003292 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 case 0: /* success! */
3294 update_stateid(&lock_stp->st_stateid);
3295 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
3296 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07003297 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08003298 break;
3299 case (EAGAIN): /* conflock holds conflicting lock */
3300 status = nfserr_denied;
3301 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
3302 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
3303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 case (EDEADLK):
3305 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08003306 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05003308 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08003309 status = nfserr_resource;
3310 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08003313 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05003314 release_lockowner(lock_sop);
J. Bruce Fields3a655882006-01-18 17:43:19 -08003315 if (lock->lk_replay_owner) {
3316 nfs4_get_stateowner(lock->lk_replay_owner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003317 cstate->replay_owner = lock->lk_replay_owner;
Neil Brownf2327d92005-09-13 01:25:37 -07003318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 nfs4_unlock_state();
3320 return status;
3321}
3322
3323/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08003324 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
3325 * so we do a temporary open here just to get an open file to pass to
3326 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
3327 * inode operation.)
3328 */
3329static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
3330{
3331 struct file *file;
3332 int err;
3333
3334 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
3335 if (err)
3336 return err;
3337 err = vfs_test_lock(file, lock);
3338 nfsd_close(file);
3339 return err;
3340}
3341
3342/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 * LOCKT operation
3344 */
Al Virob37ad282006-10-19 23:28:59 -07003345__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003346nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3347 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
3349 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 struct file_lock file_lock;
Marc Eshelfd85b812006-11-28 16:26:41 -05003351 int error;
Al Virob37ad282006-10-19 23:28:59 -07003352 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003354 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return nfserr_grace;
3356
3357 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
3358 return nfserr_inval;
3359
3360 lockt->lt_stateowner = NULL;
3361 nfs4_lock_state();
3362
3363 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07003364 if (STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003367 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
Neil Brown849823c2005-09-13 01:25:36 -07003368 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 if (status == nfserr_symlink)
3370 status = nfserr_inval;
3371 goto out;
3372 }
3373
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003374 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 locks_init_lock(&file_lock);
3376 switch (lockt->lt_type) {
3377 case NFS4_READ_LT:
3378 case NFS4_READW_LT:
3379 file_lock.fl_type = F_RDLCK;
3380 break;
3381 case NFS4_WRITE_LT:
3382 case NFS4_WRITEW_LT:
3383 file_lock.fl_type = F_WRLCK;
3384 break;
3385 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04003386 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 status = nfserr_inval;
3388 goto out;
3389 }
3390
3391 lockt->lt_stateowner = find_lockstateowner_str(inode,
3392 &lockt->lt_clientid, &lockt->lt_owner);
3393 if (lockt->lt_stateowner)
3394 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
3395 file_lock.fl_pid = current->tgid;
3396 file_lock.fl_flags = FL_POSIX;
3397
3398 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02003399 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
3401 nfs4_transform_lock_offset(&file_lock);
3402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08003404 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05003405 if (error) {
3406 status = nfserrno(error);
3407 goto out;
3408 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05003409 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05003411 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
3413out:
3414 nfs4_unlock_state();
3415 return status;
3416}
3417
Al Virob37ad282006-10-19 23:28:59 -07003418__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003419nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003420 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
3422 struct nfs4_stateid *stp;
3423 struct file *filp = NULL;
3424 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07003425 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07003426 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
3428 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
3429 (long long) locku->lu_offset,
3430 (long long) locku->lu_length);
3431
3432 if (check_lock_length(locku->lu_offset, locku->lu_length))
3433 return nfserr_inval;
3434
3435 nfs4_lock_state();
3436
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003437 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 locku->lu_seqid,
3439 &locku->lu_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003440 LOCK_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 &locku->lu_stateowner, &stp, NULL)))
3442 goto out;
3443
3444 filp = stp->st_vfs_file;
3445 BUG_ON(!filp);
3446 locks_init_lock(&file_lock);
3447 file_lock.fl_type = F_UNLCK;
3448 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
3449 file_lock.fl_pid = current->tgid;
3450 file_lock.fl_file = filp;
3451 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07003452 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 file_lock.fl_start = locku->lu_offset;
3454
Benny Halevy87df4de2008-12-15 19:42:03 +02003455 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 nfs4_transform_lock_offset(&file_lock);
3457
3458 /*
3459 * Try to unlock the file in the VFS.
3460 */
Marc Eshelfd85b812006-11-28 16:26:41 -05003461 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07003462 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05003463 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 goto out_nfserr;
3465 }
3466 /*
3467 * OK, unlock succeeded; the only thing left to do is update the stateid.
3468 */
3469 update_stateid(&stp->st_stateid);
3470 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3471
3472out:
Neil Brownf2327d92005-09-13 01:25:37 -07003473 if (locku->lu_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 nfs4_get_stateowner(locku->lu_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003475 cstate->replay_owner = locku->lu_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07003476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 nfs4_unlock_state();
3478 return status;
3479
3480out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07003481 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 goto out;
3483}
3484
3485/*
3486 * returns
3487 * 1: locks held by lockowner
3488 * 0: no locks held by lockowner
3489 */
3490static int
3491check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3492{
3493 struct file_lock **flpp;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08003494 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 int status = 0;
3496
3497 lock_kernel();
3498 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08003499 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 status = 1;
3501 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08003502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 }
3504out:
3505 unlock_kernel();
3506 return status;
3507}
3508
Al Virob37ad282006-10-19 23:28:59 -07003509__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003510nfsd4_release_lockowner(struct svc_rqst *rqstp,
3511 struct nfsd4_compound_state *cstate,
3512 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
3514 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07003515 struct nfs4_stateowner *sop;
3516 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07003518 struct list_head matches;
3519 int i;
Al Virob37ad282006-10-19 23:28:59 -07003520 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
3522 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3523 clid->cl_boot, clid->cl_id);
3524
3525 /* XXX check for lease expiration */
3526
3527 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07003528 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
3531 nfs4_lock_state();
3532
NeilBrown3e9e3db2005-06-23 22:04:20 -07003533 status = nfserr_locks_held;
3534 /* XXX: we're doing a linear search through all the lockowners.
3535 * Yipes! For now we'll just hope clients aren't really using
3536 * release_lockowner much, but eventually we have to fix these
3537 * data structures. */
3538 INIT_LIST_HEAD(&matches);
3539 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3540 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003541 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07003542 continue;
3543 list_for_each_entry(stp, &sop->so_stateids,
3544 st_perstateowner) {
3545 if (check_for_locks(stp->st_vfs_file, sop))
3546 goto out;
3547 /* Note: so_perclient unused for lockowners,
3548 * so it's OK to fool with here. */
3549 list_add(&sop->so_perclient, &matches);
3550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07003552 }
3553 /* Clients probably won't expect us to return with some (but not all)
3554 * of the lockowner state released; so don't release any until all
3555 * have been checked. */
3556 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07003557 while (!list_empty(&matches)) {
3558 sop = list_entry(matches.next, struct nfs4_stateowner,
3559 so_perclient);
3560 /* unhash_stateowner deletes so_perclient only
3561 * for openowners. */
3562 list_del(&sop->so_perclient);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05003563 release_lockowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
3565out:
3566 nfs4_unlock_state();
3567 return status;
3568}
3569
3570static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07003571alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
NeilBrowna55370a2005-06-23 22:03:52 -07003573 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
NeilBrownc7b9a452005-06-23 22:04:30 -07003576int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03003577nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07003578{
3579 unsigned int strhashval = clientstr_hashval(name);
3580 struct nfs4_client *clp;
3581
Andy Adamsona1bcecd2009-04-03 08:28:05 +03003582 clp = find_confirmed_client_by_str(name, strhashval, use_exchange_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07003583 return clp ? 1 : 0;
3584}
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586/*
3587 * failure => all reset bets are off, nfserr_no_grace...
3588 */
NeilBrown190e4fb2005-06-23 22:04:25 -07003589int
3590nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
3592 unsigned int strhashval;
3593 struct nfs4_client_reclaim *crp = NULL;
3594
NeilBrowna55370a2005-06-23 22:03:52 -07003595 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3596 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 if (!crp)
3598 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07003599 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 INIT_LIST_HEAD(&crp->cr_strhash);
3601 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07003602 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 reclaim_str_hashtbl_size++;
3604 return 1;
3605}
3606
3607static void
3608nfs4_release_reclaim(void)
3609{
3610 struct nfs4_client_reclaim *crp = NULL;
3611 int i;
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3614 while (!list_empty(&reclaim_str_hashtbl[i])) {
3615 crp = list_entry(reclaim_str_hashtbl[i].next,
3616 struct nfs4_client_reclaim, cr_strhash);
3617 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 kfree(crp);
3619 reclaim_str_hashtbl_size--;
3620 }
3621 }
3622 BUG_ON(reclaim_str_hashtbl_size);
3623}
3624
3625/*
3626 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07003627static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628nfs4_find_reclaim_client(clientid_t *clid)
3629{
3630 unsigned int strhashval;
3631 struct nfs4_client *clp;
3632 struct nfs4_client_reclaim *crp = NULL;
3633
3634
3635 /* find clientid in conf_id_hashtbl */
3636 clp = find_confirmed_client(clid);
3637 if (clp == NULL)
3638 return NULL;
3639
NeilBrowna55370a2005-06-23 22:03:52 -07003640 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3641 clp->cl_name.len, clp->cl_name.data,
3642 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
3644 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07003645 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07003647 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return crp;
3649 }
3650 }
3651 return NULL;
3652}
3653
3654/*
3655* Called from OPEN. Look for clientid in reclaim list.
3656*/
Al Virob37ad282006-10-19 23:28:59 -07003657__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658nfs4_check_open_reclaim(clientid_t *clid)
3659{
NeilBrowndfc83562005-06-23 22:03:16 -07003660 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662
NeilBrownac4d8ff22005-06-23 22:03:30 -07003663/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003665int
NeilBrownac4d8ff22005-06-23 22:03:30 -07003666nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003668 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003670 status = nfsd4_init_slabs();
3671 if (status)
3672 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3674 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3675 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3676 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3677 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3678 }
Marc Eshel5282fd72009-04-03 08:27:52 +03003679 for (i = 0; i < SESSION_HASH_SIZE; i++)
3680 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 for (i = 0; i < FILE_HASH_SIZE; i++) {
3682 INIT_LIST_HEAD(&file_hashtbl[i]);
3683 }
3684 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3685 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3686 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3687 }
3688 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3689 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3690 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3691 }
3692 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3693 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3694 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 INIT_LIST_HEAD(&close_lru);
3698 INIT_LIST_HEAD(&client_lru);
3699 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07003700 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3701 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3702 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003703 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003704}
3705
NeilBrown190e4fb2005-06-23 22:04:25 -07003706static void
3707nfsd4_load_reboot_recovery_data(void)
3708{
3709 int status;
3710
NeilBrown0964a3d2005-06-23 22:04:32 -07003711 nfs4_lock_state();
3712 nfsd4_init_recdir(user_recovery_dirname);
NeilBrown190e4fb2005-06-23 22:04:25 -07003713 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07003714 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07003715 if (status)
3716 printk("NFSD: Failure reading reboot recovery data\n");
3717}
3718
Marc Eshel9a8db972007-07-17 04:04:35 -07003719unsigned long
3720get_nfs4_grace_period(void)
3721{
3722 return max(user_lease_time, lease_time) * HZ;
3723}
3724
Meelap Shahc2f1a552007-07-17 04:04:39 -07003725/*
3726 * Since the lifetime of a delegation isn't limited to that of an open, a
3727 * client may quite reasonably hang on to a delegation as long as it has
3728 * the inode cached. This becomes an obvious problem the first time a
3729 * client's inode cache approaches the size of the server's total memory.
3730 *
3731 * For now we avoid this problem by imposing a hard limit on the number
3732 * of delegations, which varies according to the server's memory size.
3733 */
3734static void
3735set_max_delegations(void)
3736{
3737 /*
3738 * Allow at most 4 delegations per megabyte of RAM. Quick
3739 * estimates suggest that in the worst case (where every delegation
3740 * is for a different inode), a delegation could take about 1.5K,
3741 * giving a worst case usage of about 6% of memory.
3742 */
3743 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
3744}
3745
NeilBrownac4d8ff22005-06-23 22:03:30 -07003746/* initialization to perform when the nfsd service is started: */
3747
3748static void
3749__nfs4_state_start(void)
3750{
Marc Eshel9a8db972007-07-17 04:04:35 -07003751 unsigned long grace_time;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003754 grace_time = get_nfs4_grace_period();
NeilBrownd99a05a2005-06-23 22:03:21 -07003755 lease_time = user_lease_time;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003756 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07003757 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
3758 grace_time/HZ);
NeilBrown58da2822005-06-23 22:03:19 -07003759 laundry_wq = create_singlethread_workqueue("nfsd4");
Marc Eshel9a8db972007-07-17 04:04:35 -07003760 queue_delayed_work(laundry_wq, &laundromat_work, grace_time);
Meelap Shahc2f1a552007-07-17 04:04:39 -07003761 set_max_delegations();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762}
3763
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003764void
NeilBrown76a35502005-06-23 22:03:26 -07003765nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 if (nfs4_init)
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003768 return;
NeilBrown190e4fb2005-06-23 22:04:25 -07003769 nfsd4_load_reboot_recovery_data();
NeilBrown76a35502005-06-23 22:03:26 -07003770 __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 nfs4_init = 1;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003772 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773}
3774
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775time_t
3776nfs4_lease_time(void)
3777{
3778 return lease_time;
3779}
3780
3781static void
3782__nfs4_state_shutdown(void)
3783{
3784 int i;
3785 struct nfs4_client *clp = NULL;
3786 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 struct list_head *pos, *next, reaplist;
3788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3790 while (!list_empty(&conf_id_hashtbl[i])) {
3791 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3792 expire_client(clp);
3793 }
3794 while (!list_empty(&unconf_str_hashtbl[i])) {
3795 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3796 expire_client(clp);
3797 }
3798 }
3799 INIT_LIST_HEAD(&reaplist);
3800 spin_lock(&recall_lock);
3801 list_for_each_safe(pos, next, &del_recall_lru) {
3802 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3803 list_move(&dp->dl_recall_lru, &reaplist);
3804 }
3805 spin_unlock(&recall_lock);
3806 list_for_each_safe(pos, next, &reaplist) {
3807 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3808 list_del_init(&dp->dl_recall_lru);
3809 unhash_delegation(dp);
3810 }
3811
NeilBrown190e4fb2005-06-23 22:04:25 -07003812 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 nfs4_init = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814}
3815
3816void
3817nfs4_state_shutdown(void)
3818{
NeilBrown5e8d5c22006-04-10 22:55:37 -07003819 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3820 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06003821 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 nfs4_lock_state();
3823 nfs4_release_reclaim();
3824 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 nfs4_unlock_state();
3826}
3827
Jeff Layton3dd98a32008-06-10 08:40:36 -04003828/*
3829 * user_recovery_dirname is protected by the nfsd_mutex since it's only
3830 * accessed when nfsd is starting.
3831 */
NeilBrown0964a3d2005-06-23 22:04:32 -07003832static void
3833nfs4_set_recdir(char *recdir)
3834{
NeilBrown0964a3d2005-06-23 22:04:32 -07003835 strcpy(user_recovery_dirname, recdir);
NeilBrown0964a3d2005-06-23 22:04:32 -07003836}
3837
3838/*
3839 * Change the NFSv4 recovery directory to recdir.
3840 */
3841int
3842nfs4_reset_recoverydir(char *recdir)
3843{
3844 int status;
Al Viroa63bb992008-08-02 01:03:36 -04003845 struct path path;
NeilBrown0964a3d2005-06-23 22:04:32 -07003846
Al Viroa63bb992008-08-02 01:03:36 -04003847 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
NeilBrown0964a3d2005-06-23 22:04:32 -07003848 if (status)
3849 return status;
3850 status = -ENOTDIR;
Al Viroa63bb992008-08-02 01:03:36 -04003851 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
NeilBrown0964a3d2005-06-23 22:04:32 -07003852 nfs4_set_recdir(recdir);
3853 status = 0;
3854 }
Al Viroa63bb992008-08-02 01:03:36 -04003855 path_put(&path);
NeilBrown0964a3d2005-06-23 22:04:32 -07003856 return status;
3857}
3858
Jeff Layton3dd98a32008-06-10 08:40:36 -04003859char *
3860nfs4_recoverydir(void)
3861{
3862 return user_recovery_dirname;
3863}
3864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865/*
3866 * Called when leasetime is changed.
3867 *
NeilBrownd99a05a2005-06-23 22:03:21 -07003868 * The only way the protocol gives us to handle on-the-fly lease changes is to
3869 * simulate a reboot. Instead of doing that, we just wait till the next time
3870 * we start to register any changes in lease time. If the administrator
3871 * really wants to change the lease time *now*, they can go ahead and bring
3872 * nfsd down and then back up again after changing the lease time.
Jeff Layton3dd98a32008-06-10 08:40:36 -04003873 *
3874 * user_lease_time is protected by nfsd_mutex since it's only really accessed
3875 * when nfsd is starting
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 */
3877void
3878nfs4_reset_lease(time_t leasetime)
3879{
NeilBrownd99a05a2005-06-23 22:03:21 -07003880 user_lease_time = leasetime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881}