blob: 6ab30772496efc964a20a4ecf57547fbe83829f2 [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
81/* Locking:
82 *
Ingo Molnar353ab6e2006-03-26 01:37:12 -080083 * client_mutex:
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * protects clientid_hashtbl[], clientstr_hashtbl[],
85 * unconfstr_hashtbl[], uncofid_hashtbl[].
86 */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080087static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Christoph Lametere18b8902006-12-06 20:33:20 -080089static struct kmem_cache *stateowner_slab = NULL;
90static struct kmem_cache *file_slab = NULL;
91static struct kmem_cache *stateid_slab = NULL;
92static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094void
95nfs4_lock_state(void)
96{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080097 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100void
101nfs4_unlock_state(void)
102{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800103 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static inline u32
107opaque_hashval(const void *ptr, int nbytes)
108{
109 unsigned char *cptr = (unsigned char *) ptr;
110
111 u32 x = 0;
112 while (nbytes--) {
113 x *= 37;
114 x += *cptr++;
115 }
116 return x;
117}
118
119/* forward declarations */
120static void release_stateowner(struct nfs4_stateowner *sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/*
123 * Delegation state
124 */
125
126/* recall_lock protects the del_recall_lru */
Ingo Molnar34af9462006-06-27 02:53:55 -0700127static DEFINE_SPINLOCK(recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static struct list_head del_recall_lru;
129
NeilBrown13cd2182005-06-23 22:03:10 -0700130static void
131free_nfs4_file(struct kref *kref)
132{
133 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
134 list_del(&fp->fi_hash);
135 iput(fp->fi_inode);
136 kmem_cache_free(file_slab, fp);
137}
138
139static inline void
140put_nfs4_file(struct nfs4_file *fi)
141{
142 kref_put(&fi->fi_ref, free_nfs4_file);
143}
144
145static inline void
146get_nfs4_file(struct nfs4_file *fi)
147{
148 kref_get(&fi->fi_ref);
149}
150
NeilBrownef0f3392006-04-10 22:55:41 -0700151static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700152unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700153
154/*
155 * Open owner state (share locks)
156 */
157
158/* hash tables for nfs4_stateowner */
159#define OWNER_HASH_BITS 8
160#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
161#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
162
163#define ownerid_hashval(id) \
164 ((id) & OWNER_HASH_MASK)
165#define ownerstr_hashval(clientid, ownername) \
166 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
167
168static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
169static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
170
171/* hash table for nfs4_file */
172#define FILE_HASH_BITS 8
173#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
174#define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
175/* hash table for (open)nfs4_stateid */
176#define STATEID_HASH_BITS 10
177#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
178#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
179
180#define file_hashval(x) \
181 hash_ptr(x, FILE_HASH_BITS)
182#define stateid_hashval(owner_id, file_id) \
183 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
184
185static struct list_head file_hashtbl[FILE_HASH_SIZE];
186static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static struct nfs4_delegation *
189alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
190{
191 struct nfs4_delegation *dp;
192 struct nfs4_file *fp = stp->st_file;
193 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
194
195 dprintk("NFSD alloc_init_deleg\n");
Meelap Shah47f99402007-07-17 04:04:40 -0700196 if (fp->fi_had_conflict)
197 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700198 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700199 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700200 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
201 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700203 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700204 INIT_LIST_HEAD(&dp->dl_perfile);
205 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 INIT_LIST_HEAD(&dp->dl_recall_lru);
207 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700208 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 dp->dl_file = fp;
210 dp->dl_flock = NULL;
211 get_file(stp->st_vfs_file);
212 dp->dl_vfs_file = stp->st_vfs_file;
213 dp->dl_type = type;
214 dp->dl_recall.cbr_dp = NULL;
215 dp->dl_recall.cbr_ident = cb->cb_ident;
216 dp->dl_recall.cbr_trunc = 0;
217 dp->dl_stateid.si_boot = boot_time;
218 dp->dl_stateid.si_stateownerid = current_delegid++;
219 dp->dl_stateid.si_fileid = 0;
220 dp->dl_stateid.si_generation = 0;
221 dp->dl_fhlen = current_fh->fh_handle.fh_size;
222 memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
223 current_fh->fh_handle.fh_size);
224 dp->dl_time = 0;
225 atomic_set(&dp->dl_count, 1);
NeilBrownea1da632005-06-23 22:04:17 -0700226 list_add(&dp->dl_perfile, &fp->fi_delegations);
227 list_add(&dp->dl_perclnt, &clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return dp;
229}
230
231void
232nfs4_put_delegation(struct nfs4_delegation *dp)
233{
234 if (atomic_dec_and_test(&dp->dl_count)) {
235 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700236 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700237 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700238 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240}
241
242/* Remove the associated file_lock first, then remove the delegation.
243 * lease_modify() is called to remove the FS_LEASE file_lock from
244 * the i_flock list, eventually calling nfsd's lock_manager
245 * fl_release_callback.
246 */
247static void
248nfs4_close_delegation(struct nfs4_delegation *dp)
249{
250 struct file *filp = dp->dl_vfs_file;
251
252 dprintk("NFSD: close_delegation dp %p\n",dp);
253 dp->dl_vfs_file = NULL;
254 /* The following nfsd_close may not actually close the file,
255 * but we want to remove the lease in any case. */
NeilBrownc9071322005-04-16 15:26:38 -0700256 if (dp->dl_flock)
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -0400257 vfs_setlease(filp, F_UNLCK, &dp->dl_flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/* Called under the state lock. */
262static void
263unhash_delegation(struct nfs4_delegation *dp)
264{
NeilBrownea1da632005-06-23 22:04:17 -0700265 list_del_init(&dp->dl_perfile);
266 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spin_lock(&recall_lock);
268 list_del_init(&dp->dl_recall_lru);
269 spin_unlock(&recall_lock);
270 nfs4_close_delegation(dp);
271 nfs4_put_delegation(dp);
272}
273
274/*
275 * SETCLIENTID state
276 */
277
278/* Hash tables for nfs4_clientid state */
279#define CLIENT_HASH_BITS 4
280#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
281#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
282
283#define clientid_hashval(id) \
284 ((id) & CLIENT_HASH_MASK)
NeilBrowna55370a2005-06-23 22:03:52 -0700285#define clientstr_hashval(name) \
286 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*
288 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
289 * used in reboot/reset lease grace period processing
290 *
291 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
292 * setclientid_confirmed info.
293 *
294 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
295 * setclientid info.
296 *
297 * client_lru holds client queue ordered by nfs4_client.cl_time
298 * for lease renewal.
299 *
300 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
301 * for last close replay.
302 */
303static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
304static int reclaim_str_hashtbl_size = 0;
305static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
306static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
307static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
308static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
309static struct list_head client_lru;
310static struct list_head close_lru;
311
J. Bruce Fields22839632009-01-11 14:27:17 -0500312static void unhash_generic_stateid(struct nfs4_stateid *stp)
313{
314 list_del(&stp->st_hash);
315 list_del(&stp->st_perfile);
316 list_del(&stp->st_perstateowner);
317}
318
319static void free_generic_stateid(struct nfs4_stateid *stp)
320{
321 put_nfs4_file(stp->st_file);
322 kmem_cache_free(stateid_slab, stp);
323}
324
325static void release_lock_stateid(struct nfs4_stateid *stp)
326{
327 unhash_generic_stateid(stp);
328 locks_remove_posix(stp->st_vfs_file, (fl_owner_t)stp->st_stateowner);
329 free_generic_stateid(stp);
330}
331
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500332static void
333release_stateid_lockowners(struct nfs4_stateid *open_stp)
334{
335 struct nfs4_stateowner *lock_sop;
336
337 while (!list_empty(&open_stp->st_lockowners)) {
338 lock_sop = list_entry(open_stp->st_lockowners.next,
339 struct nfs4_stateowner, so_perstateid);
340 /* list_del(&open_stp->st_lockowners); */
341 BUG_ON(lock_sop->so_is_open_owner);
342 release_stateowner(lock_sop);
343 }
344}
345
J. Bruce Fields22839632009-01-11 14:27:17 -0500346static void release_open_stateid(struct nfs4_stateid *stp)
347{
348 unhash_generic_stateid(stp);
349 release_stateid_lockowners(stp);
350 nfsd_close(stp->st_vfs_file);
351 free_generic_stateid(stp);
352}
353
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500354static void
355unhash_stateowner(struct nfs4_stateowner *sop)
356{
357 struct nfs4_stateid *stp;
358
359 list_del(&sop->so_idhash);
360 list_del(&sop->so_strhash);
361 if (sop->so_is_open_owner)
362 list_del(&sop->so_perclient);
363 list_del(&sop->so_perstateid);
364 while (!list_empty(&sop->so_stateids)) {
365 stp = list_entry(sop->so_stateids.next,
366 struct nfs4_stateid, st_perstateowner);
367 if (sop->so_is_open_owner)
368 release_open_stateid(stp);
369 else
370 release_lock_stateid(stp);
371 }
372}
373
374static void
375release_stateowner(struct nfs4_stateowner *sop)
376{
377 unhash_stateowner(sop);
378 list_del(&sop->so_close_lru);
379 nfs4_put_stateowner(sop);
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static inline void
383renew_client(struct nfs4_client *clp)
384{
385 /*
386 * Move client to the end to the LRU list.
387 */
388 dprintk("renewing client (clientid %08x/%08x)\n",
389 clp->cl_clientid.cl_boot,
390 clp->cl_clientid.cl_id);
391 list_move_tail(&clp->cl_lru, &client_lru);
392 clp->cl_time = get_seconds();
393}
394
395/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
396static int
397STALE_CLIENTID(clientid_t *clid)
398{
399 if (clid->cl_boot == boot_time)
400 return 0;
401 dprintk("NFSD stale clientid (%08x/%08x)\n",
402 clid->cl_boot, clid->cl_id);
403 return 1;
404}
405
406/*
407 * XXX Should we use a slab cache ?
408 * This type of memory management is somewhat inefficient, but we use it
409 * anyway since SETCLIENTID is not a common operation.
410 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500411static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct nfs4_client *clp;
414
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500415 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
416 if (clp == NULL)
417 return NULL;
418 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
419 if (clp->cl_name.data == NULL) {
420 kfree(clp);
421 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500423 memcpy(clp->cl_name.data, name.data, name.len);
424 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return clp;
426}
427
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400428static void
429shutdown_callback_client(struct nfs4_client *clp)
430{
431 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
432
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400433 if (clnt) {
J. Bruce Fields404ec112007-11-23 22:26:18 -0500434 /*
435 * Callback threads take a reference on the client, so there
436 * should be no outstanding callbacks at this point.
437 */
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400438 clp->cl_callback.cb_client = NULL;
439 rpc_shutdown_client(clnt);
440 }
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static inline void
444free_client(struct nfs4_client *clp)
445{
J. Bruce Fields1b1a9b32007-09-12 08:43:59 -0400446 shutdown_callback_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (clp->cl_cred.cr_group_info)
448 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500449 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 kfree(clp->cl_name.data);
451 kfree(clp);
452}
453
454void
455put_nfs4_client(struct nfs4_client *clp)
456{
457 if (atomic_dec_and_test(&clp->cl_count))
458 free_client(clp);
459}
460
461static void
462expire_client(struct nfs4_client *clp)
463{
464 struct nfs4_stateowner *sop;
465 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct list_head reaplist;
467
468 dprintk("NFSD: expire_client cl_count %d\n",
469 atomic_read(&clp->cl_count));
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 INIT_LIST_HEAD(&reaplist);
472 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700473 while (!list_empty(&clp->cl_delegations)) {
474 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
476 dp->dl_flock);
NeilBrownea1da632005-06-23 22:04:17 -0700477 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 list_move(&dp->dl_recall_lru, &reaplist);
479 }
480 spin_unlock(&recall_lock);
481 while (!list_empty(&reaplist)) {
482 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
483 list_del_init(&dp->dl_recall_lru);
484 unhash_delegation(dp);
485 }
486 list_del(&clp->cl_idhash);
487 list_del(&clp->cl_strhash);
488 list_del(&clp->cl_lru);
NeilBrownea1da632005-06-23 22:04:17 -0700489 while (!list_empty(&clp->cl_openowners)) {
490 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 release_stateowner(sop);
492 }
493 put_nfs4_client(clp);
494}
495
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500496static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir)
497{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct nfs4_client *clp;
499
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500500 clp = alloc_client(name);
501 if (clp == NULL)
502 return NULL;
NeilBrowna55370a2005-06-23 22:03:52 -0700503 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 atomic_set(&clp->cl_count, 1);
505 atomic_set(&clp->cl_callback.cb_set, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 INIT_LIST_HEAD(&clp->cl_idhash);
507 INIT_LIST_HEAD(&clp->cl_strhash);
NeilBrownea1da632005-06-23 22:04:17 -0700508 INIT_LIST_HEAD(&clp->cl_openowners);
509 INIT_LIST_HEAD(&clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 INIT_LIST_HEAD(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return clp;
512}
513
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500514static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
515{
516 memcpy(target->cl_verifier.data, source->data,
517 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500520static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
521{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
523 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
524}
525
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500526static void copy_cred(struct svc_cred *target, struct svc_cred *source)
527{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 target->cr_uid = source->cr_uid;
529 target->cr_gid = source->cr_gid;
530 target->cr_group_info = source->cr_group_info;
531 get_group_info(target->cr_group_info);
532}
533
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500534static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400535{
NeilBrowna55370a2005-06-23 22:03:52 -0700536 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400540same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
541{
542 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400546same_clid(clientid_t *cl1, clientid_t *cl2)
547{
548 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551/* XXX what about NGROUP */
552static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400553same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
554{
555 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
J. Bruce Fields5ec7b462007-11-21 21:58:56 -0500558static void gen_clid(struct nfs4_client *clp)
559{
560 static u32 current_clientid = 1;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 clp->cl_clientid.cl_boot = boot_time;
563 clp->cl_clientid.cl_id = current_clientid++;
564}
565
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -0500566static void gen_confirm(struct nfs4_client *clp)
567{
568 static u32 i;
569 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -0500572 *p++ = get_seconds();
573 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500576static int check_name(struct xdr_netobj name)
577{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (name.len == 0)
579 return 0;
580 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400581 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 0;
583 }
584 return 1;
585}
586
NeilBrownfd39ca92005-06-23 22:04:03 -0700587static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
589{
590 unsigned int idhashval;
591
592 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
593 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
594 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
595 list_add_tail(&clp->cl_lru, &client_lru);
596 clp->cl_time = get_seconds();
597}
598
NeilBrownfd39ca92005-06-23 22:04:03 -0700599static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600move_to_confirmed(struct nfs4_client *clp)
601{
602 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
603 unsigned int strhashval;
604
605 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
606 list_del_init(&clp->cl_strhash);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700607 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -0700608 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
610 renew_client(clp);
611}
612
613static struct nfs4_client *
614find_confirmed_client(clientid_t *clid)
615{
616 struct nfs4_client *clp;
617 unsigned int idhashval = clientid_hashval(clid->cl_id);
618
619 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400620 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return clp;
622 }
623 return NULL;
624}
625
626static struct nfs4_client *
627find_unconfirmed_client(clientid_t *clid)
628{
629 struct nfs4_client *clp;
630 unsigned int idhashval = clientid_hashval(clid->cl_id);
631
632 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400633 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return clp;
635 }
636 return NULL;
637}
638
NeilBrown28ce6052005-06-23 22:03:56 -0700639static struct nfs4_client *
640find_confirmed_client_by_str(const char *dname, unsigned int hashval)
641{
642 struct nfs4_client *clp;
643
644 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
645 if (same_name(clp->cl_recdir, dname))
646 return clp;
647 }
648 return NULL;
649}
650
651static struct nfs4_client *
652find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
653{
654 struct nfs4_client *clp;
655
656 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
657 if (same_name(clp->cl_recdir, dname))
658 return clp;
659 }
660 return NULL;
661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/* a helper function for parse_callback */
664static int
665parse_octet(unsigned int *lenp, char **addrp)
666{
667 unsigned int len = *lenp;
668 char *p = *addrp;
669 int n = -1;
670 char c;
671
672 for (;;) {
673 if (!len)
674 break;
675 len--;
676 c = *p++;
677 if (c == '.')
678 break;
679 if ((c < '0') || (c > '9')) {
680 n = -1;
681 break;
682 }
683 if (n < 0)
684 n = 0;
685 n = (n * 10) + (c - '0');
686 if (n > 255) {
687 n = -1;
688 break;
689 }
690 }
691 *lenp = len;
692 *addrp = p;
693 return n;
694}
695
696/* parse and set the setclientid ipv4 callback address */
NeilBrownfd39ca92005-06-23 22:04:03 -0700697static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
699{
700 int temp = 0;
701 u32 cbaddr = 0;
702 u16 cbport = 0;
703 u32 addrlen = addr_len;
704 char *addr = addr_val;
705 int i, shift;
706
707 /* ipaddress */
708 shift = 24;
709 for(i = 4; i > 0 ; i--) {
710 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
711 return 0;
712 }
713 cbaddr |= (temp << shift);
714 if (shift > 0)
715 shift -= 8;
716 }
717 *cbaddrp = cbaddr;
718
719 /* port */
720 shift = 8;
721 for(i = 2; i > 0 ; i--) {
722 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
723 return 0;
724 }
725 cbport |= (temp << shift);
726 if (shift > 0)
727 shift -= 8;
728 }
729 *cbportp = cbport;
730 return 1;
731}
732
NeilBrownfd39ca92005-06-23 22:04:03 -0700733static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
735{
736 struct nfs4_callback *cb = &clp->cl_callback;
737
738 /* Currently, we only support tcp for the callback channel */
739 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
740 goto out_err;
741
742 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
743 &cb->cb_addr, &cb->cb_port)))
744 goto out_err;
745 cb->cb_prog = se->se_callback_prog;
746 cb->cb_ident = se->se_callback_ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return;
748out_err:
Neil Brown849823c2005-09-13 01:25:36 -0700749 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 "will not receive delegations\n",
751 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return;
754}
755
Al Virob37ad282006-10-19 23:28:59 -0700756__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800757nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
758 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Chuck Lever27459f02007-02-12 00:53:34 -0800760 struct sockaddr_in *sin = svc_addr_in(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct xdr_netobj clname = {
762 .len = setclid->se_namelen,
763 .data = setclid->se_name,
764 };
765 nfs4_verifier clverifier = setclid->se_verf;
766 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -0700767 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -0700768 __be32 status;
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500769 char *princ;
NeilBrowna55370a2005-06-23 22:03:52 -0700770 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -0700773 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
NeilBrowna55370a2005-06-23 22:03:52 -0700775 status = nfs4_make_rec_clidname(dname, &clname);
776 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -0700777 return status;
NeilBrowna55370a2005-06-23 22:03:52 -0700778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /*
780 * XXX The Duplicate Request Cache (DRC) has been checked (??)
781 * We get here on a DRC miss.
782 */
783
NeilBrowna55370a2005-06-23 22:03:52 -0700784 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 nfs4_lock_state();
NeilBrown28ce6052005-06-23 22:03:56 -0700787 conf = find_confirmed_client_by_str(dname, strhashval);
788 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500789 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 status = nfserr_clid_inuse;
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400791 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)
Chuck Lever27459f02007-02-12 00:53:34 -0800792 || conf->cl_addr != sin->sin_addr.s_addr) {
Harvey Harrisonbe859402008-10-31 00:56:28 -0700793 dprintk("NFSD: setclientid: string in use by clientat %pI4\n",
794 &conf->cl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 goto out;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500798 /*
799 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
800 * has a description of SETCLIENTID request processing consisting
801 * of 5 bullet points, labeled as CASE0 - CASE4 below.
802 */
NeilBrown28ce6052005-06-23 22:03:56 -0700803 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 status = nfserr_resource;
805 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500806 /*
807 * RFC 3530 14.2.33 CASE 4:
808 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
810 if (unconf)
811 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700812 new = create_client(clname, dname);
813 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400816 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500818 * RFC 3530 14.2.33 CASE 1:
819 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
NeilBrown31f4a6c2005-06-23 22:04:06 -0700821 if (unconf) {
822 /* Note this is removing unconfirmed {*x***},
823 * which is stronger than RFC recommended {vxc**}.
824 * This has the advantage that there is at most
825 * one {*x***} in either list at any time.
826 */
827 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
NeilBrowna55370a2005-06-23 22:03:52 -0700829 new = create_client(clname, dname);
830 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 } else if (!unconf) {
834 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500835 * RFC 3530 14.2.33 CASE 2:
836 * probable client reboot; state will be removed if
837 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
NeilBrowna55370a2005-06-23 22:03:52 -0700839 new = create_client(clname, dname);
840 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -0500843 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500844 /*
845 * RFC 3530 14.2.33 CASE 3:
846 * probable client reboot; state will be removed if
847 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
849 expire_client(unconf);
NeilBrowna55370a2005-06-23 22:03:52 -0700850 new = create_client(clname, dname);
851 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
J. Bruce Fieldsc175b832007-08-09 18:34:32 -0400855 copy_verf(new, &clverifier);
856 new->cl_addr = sin->sin_addr.s_addr;
Olga Kornievskaia61054b12008-12-23 16:19:00 -0500857 new->cl_flavor = rqstp->rq_flavor;
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500858 princ = svc_gss_principal(rqstp);
859 if (princ) {
860 new->cl_principal = kstrdup(princ, GFP_KERNEL);
861 if (new->cl_principal == NULL) {
862 free_client(new);
863 goto out;
864 }
865 }
J. Bruce Fieldsc175b832007-08-09 18:34:32 -0400866 copy_cred(&new->cl_cred, &rqstp->rq_cred);
867 gen_confirm(new);
868 gen_callback(new, setclid);
869 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
871 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
872 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
873 status = nfs_ok;
874out:
875 nfs4_unlock_state();
876 return status;
877}
878
879
880/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500881 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
882 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
883 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
Al Virob37ad282006-10-19 23:28:59 -0700885__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800886nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
887 struct nfsd4_compound_state *cstate,
888 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Chuck Lever27459f02007-02-12 00:53:34 -0800890 struct sockaddr_in *sin = svc_addr_in(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -0700891 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
893 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -0700894 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (STALE_CLIENTID(clid))
897 return nfserr_stale_clientid;
898 /*
899 * XXX The Duplicate Request Cache (DRC) has been checked (??)
900 * We get here on a DRC miss.
901 */
902
903 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -0700904
905 conf = find_confirmed_client(clid);
906 unconf = find_unconfirmed_client(clid);
907
908 status = nfserr_clid_inuse;
Chuck Lever27459f02007-02-12 00:53:34 -0800909 if (conf && conf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -0700910 goto out;
Chuck Lever27459f02007-02-12 00:53:34 -0800911 if (unconf && unconf->cl_addr != sin->sin_addr.s_addr)
NeilBrown21ab45a2005-06-23 22:04:14 -0700912 goto out;
913
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500914 /*
915 * section 14.2.34 of RFC 3530 has a description of
916 * SETCLIENTID_CONFIRM request processing consisting
917 * of 4 bullet points, labeled as CASE1 - CASE4 below.
918 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -0500919 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500920 /*
921 * RFC 3530 14.2.34 CASE 1:
922 * callback update
923 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400924 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 status = nfserr_clid_inuse;
926 else {
NeilBrown1a69c172005-06-23 22:04:08 -0700927 /* XXX: We just turn off callbacks until we can handle
928 * change request correctly. */
NeilBrowncb36d632005-06-23 22:04:23 -0700929 atomic_set(&conf->cl_callback.cb_set, 0);
NeilBrown21ab45a2005-06-23 22:04:14 -0700930 gen_confirm(conf);
NeilBrown46309022005-07-07 17:59:10 -0700931 nfsd4_remove_clid_dir(unconf);
NeilBrown1a69c172005-06-23 22:04:08 -0700932 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -0700934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -0500936 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500937 /*
938 * RFC 3530 14.2.34 CASE 2:
939 * probable retransmitted request; play it safe and
940 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -0700941 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400942 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -0700944 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -0700946 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400947 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500948 /*
949 * RFC 3530 14.2.34 CASE 3:
950 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -0700951 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400952 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 status = nfserr_clid_inuse;
954 } else {
NeilBrown1a69c172005-06-23 22:04:08 -0700955 unsigned int hash =
956 clientstr_hashval(unconf->cl_recdir);
957 conf = find_confirmed_client_by_str(unconf->cl_recdir,
958 hash);
959 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -0700960 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -0700961 expire_client(conf);
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -0700964 conf = unconf;
J. Bruce Fields46f8a642007-11-22 13:54:18 -0500965 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -0700966 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400968 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
969 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -0700970 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -0500971 /*
972 * RFC 3530 14.2.34 CASE 4:
973 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -0700974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -0700976 } else {
NeilBrown08e89872005-06-23 22:04:11 -0700977 /* check that we have hit one of the cases...*/
978 status = nfserr_clid_inuse;
979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 nfs4_unlock_state();
982 return status;
983}
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/* OPEN Share state helper functions */
986static inline struct nfs4_file *
987alloc_init_file(struct inode *ino)
988{
989 struct nfs4_file *fp;
990 unsigned int hashval = file_hashval(ino);
991
NeilBrowne60d4392005-06-23 22:03:01 -0700992 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
993 if (fp) {
NeilBrown13cd2182005-06-23 22:03:10 -0700994 kref_init(&fp->fi_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -0700996 INIT_LIST_HEAD(&fp->fi_stateids);
997 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
999 fp->fi_inode = igrab(ino);
1000 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -07001001 fp->fi_had_conflict = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return fp;
1003 }
1004 return NULL;
1005}
1006
1007static void
Christoph Lametere18b8902006-12-06 20:33:20 -08001008nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07001009{
NeilBrowne60d4392005-06-23 22:03:01 -07001010 if (*slab == NULL)
1011 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001012 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001013 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07001014}
1015
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001016void
NeilBrowne60d4392005-06-23 22:03:01 -07001017nfsd4_free_slabs(void)
1018{
1019 nfsd4_free_slab(&stateowner_slab);
1020 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07001021 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001022 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025static int
1026nfsd4_init_slabs(void)
1027{
1028 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
Paul Mundt20c2df82007-07-20 10:11:58 +09001029 sizeof(struct nfs4_stateowner), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001030 if (stateowner_slab == NULL)
1031 goto out_nomem;
1032 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09001033 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001034 if (file_slab == NULL)
1035 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07001036 stateid_slab = kmem_cache_create("nfsd4_stateids",
Paul Mundt20c2df82007-07-20 10:11:58 +09001037 sizeof(struct nfs4_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07001038 if (stateid_slab == NULL)
1039 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07001040 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09001041 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001042 if (deleg_slab == NULL)
1043 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07001045out_nomem:
1046 nfsd4_free_slabs();
1047 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1048 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051void
1052nfs4_free_stateowner(struct kref *kref)
1053{
1054 struct nfs4_stateowner *sop =
1055 container_of(kref, struct nfs4_stateowner, so_ref);
1056 kfree(sop->so_owner.data);
1057 kmem_cache_free(stateowner_slab, sop);
1058}
1059
1060static inline struct nfs4_stateowner *
1061alloc_stateowner(struct xdr_netobj *owner)
1062{
1063 struct nfs4_stateowner *sop;
1064
1065 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1066 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1067 memcpy(sop->so_owner.data, owner->data, owner->len);
1068 sop->so_owner.len = owner->len;
1069 kref_init(&sop->so_ref);
1070 return sop;
1071 }
1072 kmem_cache_free(stateowner_slab, sop);
1073 }
1074 return NULL;
1075}
1076
1077static struct nfs4_stateowner *
1078alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1079 struct nfs4_stateowner *sop;
1080 struct nfs4_replay *rp;
1081 unsigned int idhashval;
1082
1083 if (!(sop = alloc_stateowner(&open->op_owner)))
1084 return NULL;
1085 idhashval = ownerid_hashval(current_ownerid);
1086 INIT_LIST_HEAD(&sop->so_idhash);
1087 INIT_LIST_HEAD(&sop->so_strhash);
1088 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001089 INIT_LIST_HEAD(&sop->so_stateids);
1090 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 INIT_LIST_HEAD(&sop->so_close_lru);
1092 sop->so_time = 0;
1093 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1094 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001095 list_add(&sop->so_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 sop->so_is_open_owner = 1;
1097 sop->so_id = current_ownerid++;
1098 sop->so_client = clp;
1099 sop->so_seqid = open->op_seqid;
1100 sop->so_confirmed = 0;
1101 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08001102 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 rp->rp_buflen = 0;
1104 rp->rp_buf = rp->rp_ibuf;
1105 return sop;
1106}
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108static inline void
1109init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1110 struct nfs4_stateowner *sop = open->op_stateowner;
1111 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1112
1113 INIT_LIST_HEAD(&stp->st_hash);
NeilBrownea1da632005-06-23 22:04:17 -07001114 INIT_LIST_HEAD(&stp->st_perstateowner);
1115 INIT_LIST_HEAD(&stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 INIT_LIST_HEAD(&stp->st_perfile);
1117 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001118 list_add(&stp->st_perstateowner, &sop->so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07001119 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07001121 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 stp->st_file = fp;
1123 stp->st_stateid.si_boot = boot_time;
1124 stp->st_stateid.si_stateownerid = sop->so_id;
1125 stp->st_stateid.si_fileid = fp->fi_id;
1126 stp->st_stateid.si_generation = 0;
1127 stp->st_access_bmap = 0;
1128 stp->st_deny_bmap = 0;
1129 __set_bit(open->op_share_access, &stp->st_access_bmap);
1130 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07001131 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135move_to_close_lru(struct nfs4_stateowner *sop)
1136{
1137 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1138
NeilBrown358dd552006-04-10 22:55:42 -07001139 list_move_tail(&sop->so_close_lru, &close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 sop->so_time = get_seconds();
1141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001144same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
1145 clientid_t *clid)
1146{
1147 return (sop->so_owner.len == owner->len) &&
1148 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
1149 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152static struct nfs4_stateowner *
1153find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1154{
1155 struct nfs4_stateowner *so = NULL;
1156
1157 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001158 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return so;
1160 }
1161 return NULL;
1162}
1163
1164/* search file_hashtbl[] for file */
1165static struct nfs4_file *
1166find_file(struct inode *ino)
1167{
1168 unsigned int hashval = file_hashval(ino);
1169 struct nfs4_file *fp;
1170
1171 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07001172 if (fp->fi_inode == ino) {
1173 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 return NULL;
1178}
1179
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001180static inline int access_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001181{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001182 if (x < NFS4_SHARE_ACCESS_READ)
1183 return 0;
1184 if (x > NFS4_SHARE_ACCESS_BOTH)
1185 return 0;
1186 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001187}
1188
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001189static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001190{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001191 /* Note: unlike access bits, deny bits may be zero. */
1192 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001193}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04001195/*
1196 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1197 * st_{access,deny}_bmap field of the stateid, in order to track not
1198 * only what share bits are currently in force, but also what
1199 * combinations of share bits previous opens have used. This allows us
1200 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1201 * return an error if the client attempt to downgrade to a combination
1202 * of share bits not explicable by closing some of its previous opens.
1203 *
1204 * XXX: This enforcement is actually incomplete, since we don't keep
1205 * track of access/deny bit combinations; so, e.g., we allow:
1206 *
1207 * OPEN allow read, deny write
1208 * OPEN allow both, deny none
1209 * DOWNGRADE allow read, deny none
1210 *
1211 * which we should reject.
1212 */
NeilBrownfd39ca92005-06-23 22:04:03 -07001213static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214set_access(unsigned int *access, unsigned long bmap) {
1215 int i;
1216
1217 *access = 0;
1218 for (i = 1; i < 4; i++) {
1219 if (test_bit(i, &bmap))
1220 *access |= i;
1221 }
1222}
1223
NeilBrownfd39ca92005-06-23 22:04:03 -07001224static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225set_deny(unsigned int *deny, unsigned long bmap) {
1226 int i;
1227
1228 *deny = 0;
1229 for (i = 0; i < 4; i++) {
1230 if (test_bit(i, &bmap))
1231 *deny |= i ;
1232 }
1233}
1234
1235static int
1236test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1237 unsigned int access, deny;
1238
1239 set_access(&access, stp->st_access_bmap);
1240 set_deny(&deny, stp->st_deny_bmap);
1241 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1242 return 0;
1243 return 1;
1244}
1245
1246/*
1247 * Called to check deny when READ with all zero stateid or
1248 * WRITE with all zero or all one stateid
1249 */
Al Virob37ad282006-10-19 23:28:59 -07001250static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1252{
1253 struct inode *ino = current_fh->fh_dentry->d_inode;
1254 struct nfs4_file *fp;
1255 struct nfs4_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07001256 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 dprintk("NFSD: nfs4_share_conflict\n");
1259
1260 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07001261 if (!fp)
1262 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07001263 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07001265 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1266 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1267 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
NeilBrown13cd2182005-06-23 22:03:10 -07001270 ret = nfs_ok;
1271out:
1272 put_nfs4_file(fp);
1273 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
1276static inline void
1277nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1278{
1279 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
Dave Hansenaceaf782008-02-15 14:37:31 -08001280 drop_file_write_access(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1282 }
1283}
1284
1285/*
1286 * Recall a delegation
1287 */
1288static int
1289do_recall(void *__dp)
1290{
1291 struct nfs4_delegation *dp = __dp;
1292
Meelap Shah47f99402007-07-17 04:04:40 -07001293 dp->dl_file->fi_had_conflict = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 nfsd4_cb_recall(dp);
1295 return 0;
1296}
1297
1298/*
1299 * Spawn a thread to perform a recall on the delegation represented
1300 * by the lease (file_lock)
1301 *
1302 * Called from break_lease() with lock_kernel() held.
1303 * Note: we assume break_lease will only call this *once* for any given
1304 * lease.
1305 */
1306static
1307void nfsd_break_deleg_cb(struct file_lock *fl)
1308{
1309 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1310 struct task_struct *t;
1311
1312 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1313 if (!dp)
1314 return;
1315
1316 /* We're assuming the state code never drops its reference
1317 * without first removing the lease. Since we're in this lease
1318 * callback (and since the lease code is serialized by the kernel
1319 * lock) we know the server hasn't removed the lease yet, we know
1320 * it's safe to take a reference: */
1321 atomic_inc(&dp->dl_count);
J. Bruce Fieldscfdcad42007-09-12 20:35:15 -04001322 atomic_inc(&dp->dl_client->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 spin_lock(&recall_lock);
1325 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1326 spin_unlock(&recall_lock);
1327
1328 /* only place dl_time is set. protected by lock_kernel*/
1329 dp->dl_time = get_seconds();
1330
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04001331 /*
1332 * We don't want the locks code to timeout the lease for us;
1333 * we'll remove it ourself if the delegation isn't returned
1334 * in time.
1335 */
1336 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1339 if (IS_ERR(t)) {
1340 struct nfs4_client *clp = dp->dl_client;
1341
1342 printk(KERN_INFO "NFSD: Callback thread failed for "
1343 "for client (clientid %08x/%08x)\n",
1344 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
J. Bruce Fieldscfdcad42007-09-12 20:35:15 -04001345 put_nfs4_client(dp->dl_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 nfs4_put_delegation(dp);
1347 }
1348}
1349
1350/*
1351 * The file_lock is being reapd.
1352 *
1353 * Called by locks_free_lock() with lock_kernel() held.
1354 */
1355static
1356void nfsd_release_deleg_cb(struct file_lock *fl)
1357{
1358 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1359
1360 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1361
1362 if (!(fl->fl_flags & FL_LEASE) || !dp)
1363 return;
1364 dp->dl_flock = NULL;
1365}
1366
1367/*
1368 * Set the delegation file_lock back pointer.
1369 *
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001370 * Called from setlease() with lock_kernel() held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
1372static
1373void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1374{
1375 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1376
1377 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1378 if (!dp)
1379 return;
1380 dp->dl_flock = new;
1381}
1382
1383/*
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001384 * Called from setlease() with lock_kernel() held
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
1386static
1387int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1388{
1389 struct nfs4_delegation *onlistd =
1390 (struct nfs4_delegation *)onlist->fl_owner;
1391 struct nfs4_delegation *tryd =
1392 (struct nfs4_delegation *)try->fl_owner;
1393
1394 if (onlist->fl_lmops != try->fl_lmops)
1395 return 0;
1396
1397 return onlistd->dl_client == tryd->dl_client;
1398}
1399
1400
1401static
1402int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1403{
1404 if (arg & F_UNLCK)
1405 return lease_modify(onlist, arg);
1406 else
1407 return -EAGAIN;
1408}
1409
NeilBrownfd39ca92005-06-23 22:04:03 -07001410static struct lock_manager_operations nfsd_lease_mng_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 .fl_break = nfsd_break_deleg_cb,
1412 .fl_release_private = nfsd_release_deleg_cb,
1413 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1414 .fl_mylease = nfsd_same_client_deleg_cb,
1415 .fl_change = nfsd_change_deleg_cb,
1416};
1417
1418
Al Virob37ad282006-10-19 23:28:59 -07001419__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420nfsd4_process_open1(struct nfsd4_open *open)
1421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 clientid_t *clientid = &open->op_clientid;
1423 struct nfs4_client *clp = NULL;
1424 unsigned int strhashval;
1425 struct nfs4_stateowner *sop = NULL;
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001428 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 if (STALE_CLIENTID(&open->op_clientid))
1431 return nfserr_stale_clientid;
1432
1433 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1434 sop = find_openstateowner_str(strhashval, open);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001435 open->op_stateowner = sop;
1436 if (!sop) {
1437 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 clp = find_confirmed_client(clientid);
1439 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001440 return nfserr_expired;
1441 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001443 if (!sop->so_confirmed) {
1444 /* Replace unconfirmed owners without checking for replay. */
1445 clp = sop->so_client;
1446 release_stateowner(sop);
1447 open->op_stateowner = NULL;
1448 goto renew;
1449 }
1450 if (open->op_seqid == sop->so_seqid - 1) {
1451 if (sop->so_replay.rp_buflen)
Al Viroa90b0612006-10-19 23:29:03 -07001452 return nfserr_replay_me;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001453 /* The original OPEN failed so spectacularly
1454 * that we don't even have replay data saved!
1455 * Therefore, we have no choice but to continue
1456 * processing this OPEN; presumably, we'll
1457 * fail again for the same reason.
1458 */
1459 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1460 goto renew;
1461 }
1462 if (open->op_seqid != sop->so_seqid)
1463 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464renew:
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001465 if (open->op_stateowner == NULL) {
1466 sop = alloc_init_open_stateowner(strhashval, clp, open);
1467 if (sop == NULL)
1468 return nfserr_resource;
1469 open->op_stateowner = sop;
1470 }
1471 list_del_init(&sop->so_close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 renew_client(sop->so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08001473 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Al Virob37ad282006-10-19 23:28:59 -07001476static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07001477nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1478{
1479 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1480 return nfserr_openmode;
1481 else
1482 return nfs_ok;
1483}
1484
NeilBrown52f4fb42005-06-23 22:02:49 -07001485static struct nfs4_delegation *
1486find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1487{
1488 struct nfs4_delegation *dp;
1489
NeilBrownea1da632005-06-23 22:04:17 -07001490 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
NeilBrown52f4fb42005-06-23 22:02:49 -07001491 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1492 return dp;
1493 }
1494 return NULL;
1495}
1496
Al Virob37ad282006-10-19 23:28:59 -07001497static __be32
NeilBrown567d9822005-06-23 22:02:53 -07001498nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1499 struct nfs4_delegation **dp)
1500{
1501 int flags;
Al Virob37ad282006-10-19 23:28:59 -07001502 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07001503
1504 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1505 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07001506 goto out;
NeilBrown567d9822005-06-23 22:02:53 -07001507 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1508 RD_STATE : WR_STATE;
1509 status = nfs4_check_delegmode(*dp, flags);
1510 if (status)
1511 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001512out:
1513 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1514 return nfs_ok;
1515 if (status)
1516 return status;
1517 open->op_stateowner->so_confirmed = 1;
1518 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07001519}
1520
Al Virob37ad282006-10-19 23:28:59 -07001521static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1523{
1524 struct nfs4_stateid *local;
Al Virob37ad282006-10-19 23:28:59 -07001525 __be32 status = nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 struct nfs4_stateowner *sop = open->op_stateowner;
1527
NeilBrown8beefa22005-06-23 22:03:08 -07001528 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /* ignore lock owners */
1530 if (local->st_stateowner->so_is_open_owner == 0)
1531 continue;
1532 /* remember if we have seen this open owner */
1533 if (local->st_stateowner == sop)
1534 *stpp = local;
1535 /* check for conflicting share reservations */
1536 if (!test_share(local, open))
1537 goto out;
1538 }
1539 status = 0;
1540out:
1541 return status;
1542}
1543
NeilBrown5ac049a2005-06-23 22:03:03 -07001544static inline struct nfs4_stateid *
1545nfs4_alloc_stateid(void)
1546{
1547 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1548}
1549
Al Virob37ad282006-10-19 23:28:59 -07001550static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
NeilBrown567d9822005-06-23 22:02:53 -07001552 struct nfs4_delegation *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 struct svc_fh *cur_fh, int flags)
1554{
1555 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
NeilBrown5ac049a2005-06-23 22:03:03 -07001557 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (stp == NULL)
1559 return nfserr_resource;
1560
NeilBrown567d9822005-06-23 22:02:53 -07001561 if (dp) {
1562 get_file(dp->dl_vfs_file);
1563 stp->st_vfs_file = dp->dl_vfs_file;
1564 } else {
Al Virob37ad282006-10-19 23:28:59 -07001565 __be32 status;
NeilBrown567d9822005-06-23 22:02:53 -07001566 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1567 &stp->st_vfs_file);
1568 if (status) {
1569 if (status == nfserr_dropit)
1570 status = nfserr_jukebox;
NeilBrown5ac049a2005-06-23 22:03:03 -07001571 kmem_cache_free(stateid_slab, stp);
NeilBrown567d9822005-06-23 22:02:53 -07001572 return status;
1573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 *stpp = stp;
1576 return 0;
1577}
1578
Al Virob37ad282006-10-19 23:28:59 -07001579static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1581 struct nfsd4_open *open)
1582{
1583 struct iattr iattr = {
1584 .ia_valid = ATTR_SIZE,
1585 .ia_size = 0,
1586 };
1587 if (!open->op_truncate)
1588 return 0;
1589 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08001590 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1592}
1593
Al Virob37ad282006-10-19 23:28:59 -07001594static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1596{
1597 struct file *filp = stp->st_vfs_file;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001598 struct inode *inode = filp->f_path.dentry->d_inode;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001599 unsigned int share_access, new_writer;
Al Virob37ad282006-10-19 23:28:59 -07001600 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 set_access(&share_access, stp->st_access_bmap);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001603 new_writer = (~share_access) & open->op_share_access
1604 & NFS4_SHARE_ACCESS_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001606 if (new_writer) {
Al Virob37ad282006-10-19 23:28:59 -07001607 int err = get_write_access(inode);
1608 if (err)
1609 return nfserrno(err);
Benny Halevye518f052008-07-04 15:38:41 +03001610 err = mnt_want_write(cur_fh->fh_export->ex_path.mnt);
1611 if (err)
1612 return nfserrno(err);
1613 file_take_write(filp);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 status = nfsd4_truncate(rqstp, cur_fh, open);
1616 if (status) {
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001617 if (new_writer)
1618 put_write_access(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return status;
1620 }
1621 /* remember the open */
J. Bruce Fields6c26d082006-01-18 17:43:38 -08001622 filp->f_mode |= open->op_share_access;
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04001623 __set_bit(open->op_share_access, &stp->st_access_bmap);
1624 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 return nfs_ok;
1627}
1628
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630static void
NeilBrown37515172005-07-07 17:59:16 -07001631nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
NeilBrown37515172005-07-07 17:59:16 -07001633 open->op_stateowner->so_confirmed = 1;
1634 open->op_stateowner->so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637/*
1638 * Attempt to hand out a delegation.
1639 */
1640static void
1641nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1642{
1643 struct nfs4_delegation *dp;
1644 struct nfs4_stateowner *sop = stp->st_stateowner;
1645 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1646 struct file_lock fl, *flp = &fl;
1647 int status, flag = 0;
1648
1649 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07001650 open->op_recall = 0;
1651 switch (open->op_claim_type) {
1652 case NFS4_OPEN_CLAIM_PREVIOUS:
1653 if (!atomic_read(&cb->cb_set))
1654 open->op_recall = 1;
1655 flag = open->op_delegate_type;
1656 if (flag == NFS4_OPEN_DELEGATE_NONE)
1657 goto out;
1658 break;
1659 case NFS4_OPEN_CLAIM_NULL:
1660 /* Let's not give out any delegations till everyone's
1661 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001662 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07001663 goto out;
1664 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1665 goto out;
1666 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1667 flag = NFS4_OPEN_DELEGATE_WRITE;
1668 else
1669 flag = NFS4_OPEN_DELEGATE_READ;
1670 break;
1671 default:
1672 goto out;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1676 if (dp == NULL) {
1677 flag = NFS4_OPEN_DELEGATE_NONE;
1678 goto out;
1679 }
1680 locks_init_lock(&fl);
1681 fl.fl_lmops = &nfsd_lease_mng_ops;
1682 fl.fl_flags = FL_LEASE;
Felix Blyakher9167f502008-02-26 10:54:36 -08001683 fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 fl.fl_end = OFFSET_MAX;
1685 fl.fl_owner = (fl_owner_t)dp;
1686 fl.fl_file = stp->st_vfs_file;
1687 fl.fl_pid = current->tgid;
1688
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001689 /* vfs_setlease checks to see if delegation should be handed out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 * the lock_manager callbacks fl_mylease and fl_change are used
1691 */
Felix Blyakher9167f502008-02-26 10:54:36 -08001692 if ((status = vfs_setlease(stp->st_vfs_file, fl.fl_type, &flp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
NeilBrownc9071322005-04-16 15:26:38 -07001694 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 flag = NFS4_OPEN_DELEGATE_NONE;
1696 goto out;
1697 }
1698
1699 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1700
1701 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1702 dp->dl_stateid.si_boot,
1703 dp->dl_stateid.si_stateownerid,
1704 dp->dl_stateid.si_fileid,
1705 dp->dl_stateid.si_generation);
1706out:
NeilBrown7b190fe2005-06-23 22:03:23 -07001707 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1708 && flag == NFS4_OPEN_DELEGATE_NONE
1709 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001710 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 open->op_delegate_type = flag;
1712}
1713
1714/*
1715 * called with nfs4_lock_state() held.
1716 */
Al Virob37ad282006-10-19 23:28:59 -07001717__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1719{
1720 struct nfs4_file *fp = NULL;
1721 struct inode *ino = current_fh->fh_dentry->d_inode;
1722 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07001723 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07001724 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 status = nfserr_inval;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001727 if (!access_valid(open->op_share_access)
1728 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto out;
1730 /*
1731 * Lookup file; if found, lookup stateid and check open request,
1732 * and check for delegations in the process of being recalled.
1733 * If not found, create the nfs4_file struct
1734 */
1735 fp = find_file(ino);
1736 if (fp) {
1737 if ((status = nfs4_check_open(fp, open, &stp)))
1738 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001739 status = nfs4_check_deleg(fp, open, &dp);
1740 if (status)
1741 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07001743 status = nfserr_bad_stateid;
1744 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1745 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 status = nfserr_resource;
1747 fp = alloc_init_file(ino);
1748 if (fp == NULL)
1749 goto out;
1750 }
1751
1752 /*
1753 * OPEN the file, or upgrade an existing OPEN.
1754 * If truncate fails, the OPEN fails.
1755 */
1756 if (stp) {
1757 /* Stateid was found, this is an OPEN upgrade */
1758 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1759 if (status)
1760 goto out;
NeilBrown444c2c02005-07-07 17:59:22 -07001761 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 } else {
1763 /* Stateid was not found, this is a new OPEN */
1764 int flags = 0;
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -07001765 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001766 flags |= NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001768 flags |= NFSD_MAY_WRITE;
NeilBrown567d9822005-06-23 22:02:53 -07001769 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1770 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 goto out;
1772 init_stateid(stp, fp, open);
1773 status = nfsd4_truncate(rqstp, current_fh, open);
1774 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05001775 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto out;
1777 }
1778 }
1779 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1780
1781 /*
1782 * Attempt to hand out a delegation. No error return, because the
1783 * OPEN succeeds even if we fail.
1784 */
1785 nfs4_open_delegation(current_fh, open, stp);
1786
1787 status = nfs_ok;
1788
1789 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1790 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1791 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1792out:
NeilBrown13cd2182005-06-23 22:03:10 -07001793 if (fp)
1794 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07001795 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1796 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /*
1798 * To finish the open response, we just need to set the rflags.
1799 */
1800 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1801 if (!open->op_stateowner->so_confirmed)
1802 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1803
1804 return status;
1805}
1806
Al Virob37ad282006-10-19 23:28:59 -07001807__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001808nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1809 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
1811 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07001812 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 nfs4_lock_state();
1815 dprintk("process_renew(%08x/%08x): starting\n",
1816 clid->cl_boot, clid->cl_id);
1817 status = nfserr_stale_clientid;
1818 if (STALE_CLIENTID(clid))
1819 goto out;
1820 clp = find_confirmed_client(clid);
1821 status = nfserr_expired;
1822 if (clp == NULL) {
1823 /* We assume the client took too long to RENEW. */
1824 dprintk("nfsd4_renew: clientid not found!\n");
1825 goto out;
1826 }
1827 renew_client(clp);
1828 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07001829 if (!list_empty(&clp->cl_delegations)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 && !atomic_read(&clp->cl_callback.cb_set))
1831 goto out;
1832 status = nfs_ok;
1833out:
1834 nfs4_unlock_state();
1835 return status;
1836}
1837
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001838struct lock_manager nfsd4_manager = {
1839};
1840
NeilBrowna76b4312005-06-23 22:04:01 -07001841static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001842nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07001843{
1844 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07001845 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001846 locks_end_grace(&nfsd4_manager);
NeilBrowna76b4312005-06-23 22:04:01 -07001847}
1848
NeilBrownfd39ca92005-06-23 22:04:03 -07001849static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850nfs4_laundromat(void)
1851{
1852 struct nfs4_client *clp;
1853 struct nfs4_stateowner *sop;
1854 struct nfs4_delegation *dp;
1855 struct list_head *pos, *next, reaplist;
1856 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1857 time_t t, clientid_val = NFSD_LEASE_TIME;
1858 time_t u, test_val = NFSD_LEASE_TIME;
1859
1860 nfs4_lock_state();
1861
1862 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001863 if (locks_in_grace())
1864 nfsd4_end_grace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 list_for_each_safe(pos, next, &client_lru) {
1866 clp = list_entry(pos, struct nfs4_client, cl_lru);
1867 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1868 t = clp->cl_time - cutoff;
1869 if (clientid_val > t)
1870 clientid_val = t;
1871 break;
1872 }
1873 dprintk("NFSD: purging unused client (clientid %08x)\n",
1874 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07001875 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 expire_client(clp);
1877 }
1878 INIT_LIST_HEAD(&reaplist);
1879 spin_lock(&recall_lock);
1880 list_for_each_safe(pos, next, &del_recall_lru) {
1881 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1882 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1883 u = dp->dl_time - cutoff;
1884 if (test_val > u)
1885 test_val = u;
1886 break;
1887 }
1888 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1889 dp, dp->dl_flock);
1890 list_move(&dp->dl_recall_lru, &reaplist);
1891 }
1892 spin_unlock(&recall_lock);
1893 list_for_each_safe(pos, next, &reaplist) {
1894 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1895 list_del_init(&dp->dl_recall_lru);
1896 unhash_delegation(dp);
1897 }
1898 test_val = NFSD_LEASE_TIME;
1899 list_for_each_safe(pos, next, &close_lru) {
1900 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1901 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1902 u = sop->so_time - cutoff;
1903 if (test_val > u)
1904 test_val = u;
1905 break;
1906 }
1907 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1908 sop->so_id);
NeilBrown358dd552006-04-10 22:55:42 -07001909 release_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1912 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1913 nfs4_unlock_state();
1914 return clientid_val;
1915}
1916
Harvey Harrisona254b242008-02-20 12:49:00 -08001917static struct workqueue_struct *laundry_wq;
1918static void laundromat_main(struct work_struct *);
1919static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
1920
1921static void
David Howellsc4028952006-11-22 14:57:56 +00001922laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 time_t t;
1925
1926 t = nfs4_laundromat();
1927 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07001928 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
NeilBrownfd39ca92005-06-23 22:04:03 -07001931static struct nfs4_stateowner *
NeilBrownf8816512005-07-07 17:59:25 -07001932search_close_lru(u32 st_id, int flags)
1933{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct nfs4_stateowner *local = NULL;
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (flags & CLOSE_STATE) {
1937 list_for_each_entry(local, &close_lru, so_close_lru) {
1938 if (local->so_id == st_id)
1939 return local;
1940 }
1941 }
1942 return NULL;
1943}
1944
1945static inline int
1946nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1947{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001948 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
1951static int
1952STALE_STATEID(stateid_t *stateid)
1953{
1954 if (stateid->si_boot == boot_time)
1955 return 0;
Neil Brown849823c2005-09-13 01:25:36 -07001956 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1958 stateid->si_generation);
1959 return 1;
1960}
1961
1962static inline int
1963access_permit_read(unsigned long access_bmap)
1964{
1965 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1966 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1967 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1968}
1969
1970static inline int
1971access_permit_write(unsigned long access_bmap)
1972{
1973 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1974 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
1975}
1976
1977static
Al Virob37ad282006-10-19 23:28:59 -07001978__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Al Virob37ad282006-10-19 23:28:59 -07001980 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
1983 goto out;
1984 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
1985 goto out;
1986 status = nfs_ok;
1987out:
1988 return status;
1989}
1990
Al Virob37ad282006-10-19 23:28:59 -07001991static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
1993{
1994 /* Trying to call delegreturn with a special stateid? Yuch: */
1995 if (!(flags & (RD_STATE | WR_STATE)))
1996 return nfserr_bad_stateid;
1997 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
1998 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04001999 else if (locks_in_grace()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /* Answer in remaining cases depends on existance of
2001 * conflicting state; so we must wait out the grace period. */
2002 return nfserr_grace;
2003 } else if (flags & WR_STATE)
2004 return nfs4_share_conflict(current_fh,
2005 NFS4_SHARE_DENY_WRITE);
2006 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2007 return nfs4_share_conflict(current_fh,
2008 NFS4_SHARE_DENY_READ);
2009}
2010
2011/*
2012 * Allow READ/WRITE during grace period on recovered state only for files
2013 * that are not able to provide mandatory locking.
2014 */
2015static inline int
2016io_during_grace_disallowed(struct inode *inode, int flags)
2017{
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002018 return locks_in_grace() && (flags & (RD_STATE | WR_STATE))
Pavel Emelyanova16877c2007-10-01 14:41:11 -07002019 && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
J. Bruce Fields0836f582008-01-26 19:08:12 -05002022static int check_stateid_generation(stateid_t *in, stateid_t *ref)
2023{
2024 /* If the client sends us a stateid from the future, it's buggy: */
2025 if (in->si_generation > ref->si_generation)
2026 return nfserr_bad_stateid;
2027 /*
2028 * The following, however, can happen. For example, if the
2029 * client sends an open and some IO at the same time, the open
2030 * may bump si_generation while the IO is still in flight.
2031 * Thanks to hard links and renames, the client never knows what
2032 * file an open will affect. So it could avoid that situation
2033 * only by serializing all opens and IO from the same open
2034 * owner. To recover from the old_stateid error, the client
2035 * will just have to retry the IO:
2036 */
2037 if (in->si_generation < ref->si_generation)
2038 return nfserr_old_stateid;
2039 return nfs_ok;
2040}
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042/*
2043* Checks for stateid operations
2044*/
Al Virob37ad282006-10-19 23:28:59 -07002045__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2047{
2048 struct nfs4_stateid *stp = NULL;
2049 struct nfs4_delegation *dp = NULL;
2050 stateid_t *stidp;
2051 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07002052 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2055 stateid->si_boot, stateid->si_stateownerid,
2056 stateid->si_fileid, stateid->si_generation);
2057 if (filpp)
2058 *filpp = NULL;
2059
2060 if (io_during_grace_disallowed(ino, flags))
2061 return nfserr_grace;
2062
2063 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2064 return check_special_stateids(current_fh, stateid, flags);
2065
2066 /* STALE STATEID */
2067 status = nfserr_stale_stateid;
2068 if (STALE_STATEID(stateid))
2069 goto out;
2070
2071 /* BAD STATEID */
2072 status = nfserr_bad_stateid;
2073 if (!stateid->si_fileid) { /* delegation stateid */
2074 if(!(dp = find_delegation_stateid(ino, stateid))) {
2075 dprintk("NFSD: delegation stateid not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 goto out;
2077 }
2078 stidp = &dp->dl_stateid;
2079 } else { /* open or lock stateid */
2080 if (!(stp = find_stateid(stateid, flags))) {
2081 dprintk("NFSD: open or lock stateid not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 goto out;
2083 }
2084 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2085 goto out;
2086 if (!stp->st_stateowner->so_confirmed)
2087 goto out;
2088 stidp = &stp->st_stateid;
2089 }
J. Bruce Fields0836f582008-01-26 19:08:12 -05002090 status = check_stateid_generation(stateid, stidp);
2091 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 goto out;
2093 if (stp) {
2094 if ((status = nfs4_check_openmode(stp,flags)))
2095 goto out;
2096 renew_client(stp->st_stateowner->so_client);
2097 if (filpp)
2098 *filpp = stp->st_vfs_file;
J. Bruce Fields6a85fa32008-01-26 23:36:48 -05002099 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if ((status = nfs4_check_delegmode(dp, flags)))
2101 goto out;
2102 renew_client(dp->dl_client);
2103 if (flags & DELEG_RET)
2104 unhash_delegation(dp);
2105 if (filpp)
2106 *filpp = dp->dl_vfs_file;
2107 }
2108 status = nfs_ok;
2109out:
2110 return status;
2111}
2112
NeilBrown4c4cd222005-07-07 17:59:27 -07002113static inline int
2114setlkflg (int type)
2115{
2116 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2117 RD_STATE : WR_STATE;
2118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120/*
2121 * Checks for sequence id mutating operations.
2122 */
Al Virob37ad282006-10-19 23:28:59 -07002123static __be32
NeilBrown4c4cd222005-07-07 17:59:27 -07002124nfs4_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 -07002125{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 struct nfs4_stateid *stp;
2127 struct nfs4_stateowner *sop;
J. Bruce Fields0836f582008-01-26 19:08:12 -05002128 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2131 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2132 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2133 stateid->si_generation);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 *stpp = NULL;
2136 *sopp = NULL;
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002139 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002140 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (STALE_STATEID(stateid))
NeilBrown3a4f98b2005-07-07 17:59:26 -07002144 return nfserr_stale_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 /*
2146 * We return BAD_STATEID if filehandle doesn't match stateid,
2147 * the confirmed flag is incorrecly set, or the generation
2148 * number is incorrect.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 */
NeilBrownf8816512005-07-07 17:59:25 -07002150 stp = find_stateid(stateid, flags);
2151 if (stp == NULL) {
2152 /*
2153 * Also, we should make sure this isn't just the result of
2154 * a replayed close:
2155 */
2156 sop = search_close_lru(stateid->si_stateownerid, flags);
2157 if (sop == NULL)
2158 return nfserr_bad_stateid;
2159 *sopp = sop;
2160 goto check_replay;
2161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
J. Bruce Fields39325bd2007-11-26 17:06:39 -05002163 *stpp = stp;
2164 *sopp = sop = stp->st_stateowner;
2165
NeilBrown4c4cd222005-07-07 17:59:27 -07002166 if (lock) {
NeilBrown4c4cd222005-07-07 17:59:27 -07002167 clientid_t *lockclid = &lock->v.new.clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 struct nfs4_client *clp = sop->so_client;
NeilBrown4c4cd222005-07-07 17:59:27 -07002169 int lkflg = 0;
Al Virob37ad282006-10-19 23:28:59 -07002170 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
NeilBrown4c4cd222005-07-07 17:59:27 -07002172 lkflg = setlkflg(lock->lk_type);
2173
2174 if (lock->lk_is_new) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002175 if (!sop->so_is_open_owner)
2176 return nfserr_bad_stateid;
2177 if (!same_clid(&clp->cl_clientid, lockclid))
NeilBrown4c4cd222005-07-07 17:59:27 -07002178 return nfserr_bad_stateid;
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002179 /* stp is the open stateid */
2180 status = nfs4_check_openmode(stp, lkflg);
2181 if (status)
2182 return status;
2183 } else {
2184 /* stp is the lock stateid */
2185 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2186 if (status)
2187 return status;
NeilBrown4c4cd222005-07-07 17:59:27 -07002188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002191 if (nfs4_check_fh(current_fh, stp)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002192 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002193 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /*
2197 * We now validate the seqid and stateid generation numbers.
2198 * For the moment, we ignore the possibility of
2199 * generation number wraparound.
2200 */
NeilBrownbd9aac52005-07-07 17:59:19 -07002201 if (seqid != sop->so_seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 goto check_replay;
2203
NeilBrown3a4f98b2005-07-07 17:59:26 -07002204 if (sop->so_confirmed && flags & CONFIRM) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002205 dprintk("NFSD: preprocess_seqid_op: expected"
NeilBrown3a4f98b2005-07-07 17:59:26 -07002206 " unconfirmed stateowner!\n");
2207 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07002209 if (!sop->so_confirmed && !(flags & CONFIRM)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002210 dprintk("NFSD: preprocess_seqid_op: stateowner not"
NeilBrown3a4f98b2005-07-07 17:59:26 -07002211 " confirmed yet!\n");
2212 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
J. Bruce Fields0836f582008-01-26 19:08:12 -05002214 status = check_stateid_generation(stateid, &stp->st_stateid);
2215 if (status)
2216 return status;
NeilBrown52fd0042005-07-07 17:59:24 -07002217 renew_client(sop->so_client);
NeilBrown3a4f98b2005-07-07 17:59:26 -07002218 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220check_replay:
NeilBrownbd9aac52005-07-07 17:59:19 -07002221 if (seqid == sop->so_seqid - 1) {
Neil Brown849823c2005-09-13 01:25:36 -07002222 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* indicate replay to calling function */
Al Viroa90b0612006-10-19 23:29:03 -07002224 return nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002226 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
NeilBrown3a4f98b2005-07-07 17:59:26 -07002227 sop->so_seqid, seqid);
2228 *sopp = NULL;
2229 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
Al Virob37ad282006-10-19 23:28:59 -07002232__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002233nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002234 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Al Virob37ad282006-10-19 23:28:59 -07002236 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct nfs4_stateowner *sop;
2238 struct nfs4_stateid *stp;
2239
2240 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002241 (int)cstate->current_fh.fh_dentry->d_name.len,
2242 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002244 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07002245 if (status)
2246 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 nfs4_lock_state();
2249
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002250 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2251 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002252 CONFIRM | OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 &oc->oc_stateowner, &stp, NULL)))
2254 goto out;
2255
2256 sop = oc->oc_stateowner;
2257 sop->so_confirmed = 1;
2258 update_stateid(&stp->st_stateid);
2259 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2260 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2261 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2262 stp->st_stateid.si_boot,
2263 stp->st_stateid.si_stateownerid,
2264 stp->st_stateid.si_fileid,
2265 stp->st_stateid.si_generation);
NeilBrownc7b9a452005-06-23 22:04:30 -07002266
2267 nfsd4_create_clid_dir(sop->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268out:
Neil Brownf2327d92005-09-13 01:25:37 -07002269 if (oc->oc_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 nfs4_get_stateowner(oc->oc_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002271 cstate->replay_owner = oc->oc_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 nfs4_unlock_state();
2274 return status;
2275}
2276
2277
2278/*
2279 * unset all bits in union bitmap (bmap) that
2280 * do not exist in share (from successful OPEN_DOWNGRADE)
2281 */
2282static void
2283reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2284{
2285 int i;
2286 for (i = 1; i < 4; i++) {
2287 if ((i & access) != i)
2288 __clear_bit(i, bmap);
2289 }
2290}
2291
2292static void
2293reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2294{
2295 int i;
2296 for (i = 0; i < 4; i++) {
2297 if ((i & deny) != i)
2298 __clear_bit(i, bmap);
2299 }
2300}
2301
Al Virob37ad282006-10-19 23:28:59 -07002302__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002303nfsd4_open_downgrade(struct svc_rqst *rqstp,
2304 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002305 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Al Virob37ad282006-10-19 23:28:59 -07002307 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 struct nfs4_stateid *stp;
2309 unsigned int share_access;
2310
2311 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002312 (int)cstate->current_fh.fh_dentry->d_name.len,
2313 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002315 if (!access_valid(od->od_share_access)
2316 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return nfserr_inval;
2318
2319 nfs4_lock_state();
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002320 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2321 od->od_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 &od->od_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002323 OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 &od->od_stateowner, &stp, NULL)))
2325 goto out;
2326
2327 status = nfserr_inval;
2328 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2329 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2330 stp->st_access_bmap, od->od_share_access);
2331 goto out;
2332 }
2333 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2334 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2335 stp->st_deny_bmap, od->od_share_deny);
2336 goto out;
2337 }
2338 set_access(&share_access, stp->st_access_bmap);
2339 nfs4_file_downgrade(stp->st_vfs_file,
2340 share_access & ~od->od_share_access);
2341
2342 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2343 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2344
2345 update_stateid(&stp->st_stateid);
2346 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2347 status = nfs_ok;
2348out:
Neil Brownf2327d92005-09-13 01:25:37 -07002349 if (od->od_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 nfs4_get_stateowner(od->od_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002351 cstate->replay_owner = od->od_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 nfs4_unlock_state();
2354 return status;
2355}
2356
2357/*
2358 * nfs4_unlock_state() called after encode
2359 */
Al Virob37ad282006-10-19 23:28:59 -07002360__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002361nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002362 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Al Virob37ad282006-10-19 23:28:59 -07002364 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct nfs4_stateid *stp;
2366
2367 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002368 (int)cstate->current_fh.fh_dentry->d_name.len,
2369 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 nfs4_lock_state();
2372 /* check close_lru for replay */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002373 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2374 close->cl_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 &close->cl_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002376 OPEN_STATE | CLOSE_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 &close->cl_stateowner, &stp, NULL)))
2378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 status = nfs_ok;
2380 update_stateid(&stp->st_stateid);
2381 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2382
J. Bruce Fields04ef5952006-01-18 17:43:21 -08002383 /* release_stateid() calls nfsd_close() if needed */
J. Bruce Fields22839632009-01-11 14:27:17 -05002384 release_open_stateid(stp);
J. Bruce Fields04ef5952006-01-18 17:43:21 -08002385
2386 /* place unused nfs4_stateowners on so_close_lru list to be
2387 * released by the laundromat service after the lease period
2388 * to enable us to handle CLOSE replay
2389 */
2390 if (list_empty(&close->cl_stateowner->so_stateids))
2391 move_to_close_lru(close->cl_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392out:
Neil Brownf2327d92005-09-13 01:25:37 -07002393 if (close->cl_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 nfs4_get_stateowner(close->cl_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002395 cstate->replay_owner = close->cl_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 nfs4_unlock_state();
2398 return status;
2399}
2400
Al Virob37ad282006-10-19 23:28:59 -07002401__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002402nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2403 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Al Virob37ad282006-10-19 23:28:59 -07002405 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002407 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 goto out;
2409
2410 nfs4_lock_state();
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002411 status = nfs4_preprocess_stateid_op(&cstate->current_fh,
2412 &dr->dr_stateid, DELEG_RET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 nfs4_unlock_state();
2414out:
2415 return status;
2416}
2417
2418
2419/*
2420 * Lock owner state (byte-range locks)
2421 */
2422#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2423#define LOCK_HASH_BITS 8
2424#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2425#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2426
Benny Halevy87df4de2008-12-15 19:42:03 +02002427static inline u64
2428end_offset(u64 start, u64 len)
2429{
2430 u64 end;
2431
2432 end = start + len;
2433 return end >= start ? end: NFS4_MAX_UINT64;
2434}
2435
2436/* last octet in a range */
2437static inline u64
2438last_byte_offset(u64 start, u64 len)
2439{
2440 u64 end;
2441
2442 BUG_ON(!len);
2443 end = start + len;
2444 return end > start ? end - 1: NFS4_MAX_UINT64;
2445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447#define lockownerid_hashval(id) \
2448 ((id) & LOCK_HASH_MASK)
2449
2450static inline unsigned int
2451lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2452 struct xdr_netobj *ownername)
2453{
2454 return (file_hashval(inode) + cl_id
2455 + opaque_hashval(ownername->data, ownername->len))
2456 & LOCK_HASH_MASK;
2457}
2458
2459static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2460static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2461static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2462
NeilBrownfd39ca92005-06-23 22:04:03 -07002463static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464find_stateid(stateid_t *stid, int flags)
2465{
Krishna Kumar9346eff2008-10-20 11:44:28 +05302466 struct nfs4_stateid *local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 u32 st_id = stid->si_stateownerid;
2468 u32 f_id = stid->si_fileid;
2469 unsigned int hashval;
2470
2471 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
Krishna Kumar9346eff2008-10-20 11:44:28 +05302472 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 hashval = stateid_hashval(st_id, f_id);
2474 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2475 if ((local->st_stateid.si_stateownerid == st_id) &&
2476 (local->st_stateid.si_fileid == f_id))
2477 return local;
2478 }
2479 }
Krishna Kumar9346eff2008-10-20 11:44:28 +05302480
2481 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 hashval = stateid_hashval(st_id, f_id);
2483 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2484 if ((local->st_stateid.si_stateownerid == st_id) &&
2485 (local->st_stateid.si_fileid == f_id))
2486 return local;
2487 }
Neil Brown849823c2005-09-13 01:25:36 -07002488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return NULL;
2490}
2491
2492static struct nfs4_delegation *
2493find_delegation_stateid(struct inode *ino, stateid_t *stid)
2494{
NeilBrown13cd2182005-06-23 22:03:10 -07002495 struct nfs4_file *fp;
2496 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2499 stid->si_boot, stid->si_stateownerid,
2500 stid->si_fileid, stid->si_generation);
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002503 if (!fp)
2504 return NULL;
2505 dl = find_delegation_file(fp, stid);
2506 put_nfs4_file(fp);
2507 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510/*
2511 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2512 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2513 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2514 * locking, this prevents us from being completely protocol-compliant. The
2515 * real solution to this problem is to start using unsigned file offsets in
2516 * the VFS, but this is a very deep change!
2517 */
2518static inline void
2519nfs4_transform_lock_offset(struct file_lock *lock)
2520{
2521 if (lock->fl_start < 0)
2522 lock->fl_start = OFFSET_MAX;
2523 if (lock->fl_end < 0)
2524 lock->fl_end = OFFSET_MAX;
2525}
2526
NeilBrownd5b90262006-04-10 22:55:22 -07002527/* Hack!: For now, we're defining this just so we can use a pointer to it
2528 * as a unique cookie to identify our (NFSv4's) posix locks. */
Adrian Bunke465a772006-04-10 22:55:23 -07002529static struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07002530};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532static inline void
2533nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2534{
NeilBrownd5b90262006-04-10 22:55:22 -07002535 struct nfs4_stateowner *sop;
2536 unsigned int hval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
NeilBrownd5b90262006-04-10 22:55:22 -07002538 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2539 sop = (struct nfs4_stateowner *) fl->fl_owner;
2540 hval = lockownerid_hashval(sop->so_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 kref_get(&sop->so_ref);
2542 deny->ld_sop = sop;
2543 deny->ld_clientid = sop->so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07002544 } else {
2545 deny->ld_sop = NULL;
2546 deny->ld_clientid.cl_boot = 0;
2547 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
2549 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02002550 deny->ld_length = NFS4_MAX_UINT64;
2551 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2553 deny->ld_type = NFS4_READ_LT;
2554 if (fl->fl_type != F_RDLCK)
2555 deny->ld_type = NFS4_WRITE_LT;
2556}
2557
2558static struct nfs4_stateowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2560 struct xdr_netobj *owner)
2561{
2562 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2563 struct nfs4_stateowner *op;
2564
2565 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002566 if (same_owner_str(op, owner, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return op;
2568 }
2569 return NULL;
2570}
2571
2572/*
2573 * Alloc a lock owner structure.
2574 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2575 * occured.
2576 *
2577 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 */
2579
2580static struct nfs4_stateowner *
2581alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2582 struct nfs4_stateowner *sop;
2583 struct nfs4_replay *rp;
2584 unsigned int idhashval;
2585
2586 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2587 return NULL;
2588 idhashval = lockownerid_hashval(current_ownerid);
2589 INIT_LIST_HEAD(&sop->so_idhash);
2590 INIT_LIST_HEAD(&sop->so_strhash);
2591 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07002592 INIT_LIST_HEAD(&sop->so_stateids);
2593 INIT_LIST_HEAD(&sop->so_perstateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2595 sop->so_time = 0;
2596 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2597 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07002598 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 sop->so_is_open_owner = 0;
2600 sop->so_id = current_ownerid++;
2601 sop->so_client = clp;
Neil Brownb59e3c02005-09-13 01:25:38 -07002602 /* It is the openowner seqid that will be incremented in encode in the
2603 * case of new lockowners; so increment the lock seqid manually: */
2604 sop->so_seqid = lock->lk_new_lock_seqid + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 sop->so_confirmed = 1;
2606 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08002607 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 rp->rp_buflen = 0;
2609 rp->rp_buf = rp->rp_ibuf;
2610 return sop;
2611}
2612
NeilBrownfd39ca92005-06-23 22:04:03 -07002613static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2615{
2616 struct nfs4_stateid *stp;
2617 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2618
NeilBrown5ac049a2005-06-23 22:03:03 -07002619 stp = nfs4_alloc_stateid();
2620 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 goto out;
2622 INIT_LIST_HEAD(&stp->st_hash);
2623 INIT_LIST_HEAD(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07002624 INIT_LIST_HEAD(&stp->st_perstateowner);
2625 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07002627 list_add(&stp->st_perfile, &fp->fi_stateids);
NeilBrownea1da632005-06-23 22:04:17 -07002628 list_add(&stp->st_perstateowner, &sop->so_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07002630 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 stp->st_file = fp;
2632 stp->st_stateid.si_boot = boot_time;
2633 stp->st_stateid.si_stateownerid = sop->so_id;
2634 stp->st_stateid.si_fileid = fp->fi_id;
2635 stp->st_stateid.si_generation = 0;
2636 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2637 stp->st_access_bmap = open_stp->st_access_bmap;
2638 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07002639 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641out:
2642 return stp;
2643}
2644
NeilBrownfd39ca92005-06-23 22:04:03 -07002645static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646check_lock_length(u64 offset, u64 length)
2647{
Benny Halevy87df4de2008-12-15 19:42:03 +02002648 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 LOFF_OVERFLOW(offset, length)));
2650}
2651
2652/*
2653 * LOCK operation
2654 */
Al Virob37ad282006-10-19 23:28:59 -07002655__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002656nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002657 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
NeilBrown3e9e3db2005-06-23 22:04:20 -07002659 struct nfs4_stateowner *open_sop = NULL;
Neil Brownb59e3c02005-09-13 01:25:38 -07002660 struct nfs4_stateowner *lock_sop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct nfs4_stateid *lock_stp;
2662 struct file *filp;
2663 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05002664 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07002665 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 unsigned int strhashval;
Marc Eshel150b3932007-01-18 16:15:35 -05002667 unsigned int cmd;
Al Virob8dd7b92006-10-19 23:29:01 -07002668 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2671 (long long) lock->lk_offset,
2672 (long long) lock->lk_length);
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (check_lock_length(lock->lk_offset, lock->lk_length))
2675 return nfserr_inval;
2676
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002677 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002678 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08002679 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2680 return status;
2681 }
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 nfs4_lock_state();
2684
2685 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07002686 /*
2687 * Client indicates that this is a new lockowner.
2688 * Use open owner and open stateid to create lock owner and
2689 * lock stateid.
2690 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 struct nfs4_stateid *open_stp = NULL;
2692 struct nfs4_file *fp;
2693
2694 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002695 if (STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 /* validate and update open stateid and open seqid */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002699 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 lock->lk_new_open_seqid,
2701 &lock->lk_new_open_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002702 OPEN_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002703 &lock->lk_replay_owner, &open_stp,
Neil Brownb59e3c02005-09-13 01:25:38 -07002704 lock);
NeilBrown37515172005-07-07 17:59:16 -07002705 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002707 open_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 /* create lockowner and lock stateid */
2709 fp = open_stp->st_file;
2710 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2711 open_sop->so_client->cl_clientid.cl_id,
2712 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07002713 /* XXX: Do we need to check for duplicate stateowners on
2714 * the same file, or should they just be allowed (and
2715 * create new stateids)? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 status = nfserr_resource;
Neil Brownb59e3c02005-09-13 01:25:38 -07002717 lock_sop = alloc_init_lock_stateowner(strhashval,
2718 open_sop->so_client, open_stp, lock);
2719 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07002721 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08002722 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 } else {
2725 /* lock (lock owner + lock stateid) already exists */
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002726 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 lock->lk_old_lock_seqid,
2728 &lock->lk_old_lock_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002729 LOCK_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08002730 &lock->lk_replay_owner, &lock_stp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 if (status)
2732 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08002733 lock_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 }
J. Bruce Fields3a655882006-01-18 17:43:19 -08002735 /* lock->lk_replay_owner and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 filp = lock_stp->st_vfs_file;
2737
NeilBrown0dd395d2005-07-07 17:59:15 -07002738 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002739 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07002740 goto out;
2741 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002742 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07002743 goto out;
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 locks_init_lock(&file_lock);
2746 switch (lock->lk_type) {
2747 case NFS4_READ_LT:
2748 case NFS4_READW_LT:
2749 file_lock.fl_type = F_RDLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05002750 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 break;
2752 case NFS4_WRITE_LT:
2753 case NFS4_WRITEW_LT:
2754 file_lock.fl_type = F_WRLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05002755 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 break;
2757 default:
2758 status = nfserr_inval;
2759 goto out;
2760 }
Neil Brownb59e3c02005-09-13 01:25:38 -07002761 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 file_lock.fl_pid = current->tgid;
2763 file_lock.fl_file = filp;
2764 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002765 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02002768 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 nfs4_transform_lock_offset(&file_lock);
2770
2771 /*
2772 * Try to lock the file in the VFS.
2773 * Note: locks.c uses the BKL to protect the inode's lock list.
2774 */
2775
Marc Eshelfd85b812006-11-28 16:26:41 -05002776 err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07002777 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 case 0: /* success! */
2779 update_stateid(&lock_stp->st_stateid);
2780 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2781 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07002782 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002783 break;
2784 case (EAGAIN): /* conflock holds conflicting lock */
2785 status = nfserr_denied;
2786 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2787 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2788 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 case (EDEADLK):
2790 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05002793 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08002794 status = nfserr_resource;
2795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08002798 if (status && lock->lk_is_new && lock_sop)
2799 release_stateowner(lock_sop);
J. Bruce Fields3a655882006-01-18 17:43:19 -08002800 if (lock->lk_replay_owner) {
2801 nfs4_get_stateowner(lock->lk_replay_owner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002802 cstate->replay_owner = lock->lk_replay_owner;
Neil Brownf2327d92005-09-13 01:25:37 -07002803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 nfs4_unlock_state();
2805 return status;
2806}
2807
2808/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08002809 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
2810 * so we do a temporary open here just to get an open file to pass to
2811 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
2812 * inode operation.)
2813 */
2814static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
2815{
2816 struct file *file;
2817 int err;
2818
2819 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
2820 if (err)
2821 return err;
2822 err = vfs_test_lock(file, lock);
2823 nfsd_close(file);
2824 return err;
2825}
2826
2827/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 * LOCKT operation
2829 */
Al Virob37ad282006-10-19 23:28:59 -07002830__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002831nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2832 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
2834 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 struct file_lock file_lock;
Marc Eshelfd85b812006-11-28 16:26:41 -05002836 int error;
Al Virob37ad282006-10-19 23:28:59 -07002837 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002839 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return nfserr_grace;
2841
2842 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2843 return nfserr_inval;
2844
2845 lockt->lt_stateowner = NULL;
2846 nfs4_lock_state();
2847
2848 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07002849 if (STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002852 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
Neil Brown849823c2005-09-13 01:25:36 -07002853 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 if (status == nfserr_symlink)
2855 status = nfserr_inval;
2856 goto out;
2857 }
2858
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002859 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 locks_init_lock(&file_lock);
2861 switch (lockt->lt_type) {
2862 case NFS4_READ_LT:
2863 case NFS4_READW_LT:
2864 file_lock.fl_type = F_RDLCK;
2865 break;
2866 case NFS4_WRITE_LT:
2867 case NFS4_WRITEW_LT:
2868 file_lock.fl_type = F_WRLCK;
2869 break;
2870 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002871 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 status = nfserr_inval;
2873 goto out;
2874 }
2875
2876 lockt->lt_stateowner = find_lockstateowner_str(inode,
2877 &lockt->lt_clientid, &lockt->lt_owner);
2878 if (lockt->lt_stateowner)
2879 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2880 file_lock.fl_pid = current->tgid;
2881 file_lock.fl_flags = FL_POSIX;
2882
2883 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02002884 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
2886 nfs4_transform_lock_offset(&file_lock);
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08002889 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05002890 if (error) {
2891 status = nfserrno(error);
2892 goto out;
2893 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002894 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002896 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898out:
2899 nfs4_unlock_state();
2900 return status;
2901}
2902
Al Virob37ad282006-10-19 23:28:59 -07002903__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002904nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002905 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 struct nfs4_stateid *stp;
2908 struct file *filp = NULL;
2909 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07002910 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002911 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2914 (long long) locku->lu_offset,
2915 (long long) locku->lu_length);
2916
2917 if (check_lock_length(locku->lu_offset, locku->lu_length))
2918 return nfserr_inval;
2919
2920 nfs4_lock_state();
2921
J.Bruce Fieldsca364312006-12-13 00:35:27 -08002922 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 locku->lu_seqid,
2924 &locku->lu_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002925 LOCK_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 &locku->lu_stateowner, &stp, NULL)))
2927 goto out;
2928
2929 filp = stp->st_vfs_file;
2930 BUG_ON(!filp);
2931 locks_init_lock(&file_lock);
2932 file_lock.fl_type = F_UNLCK;
2933 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2934 file_lock.fl_pid = current->tgid;
2935 file_lock.fl_file = filp;
2936 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07002937 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 file_lock.fl_start = locku->lu_offset;
2939
Benny Halevy87df4de2008-12-15 19:42:03 +02002940 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 nfs4_transform_lock_offset(&file_lock);
2942
2943 /*
2944 * Try to unlock the file in the VFS.
2945 */
Marc Eshelfd85b812006-11-28 16:26:41 -05002946 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07002947 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05002948 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 goto out_nfserr;
2950 }
2951 /*
2952 * OK, unlock succeeded; the only thing left to do is update the stateid.
2953 */
2954 update_stateid(&stp->st_stateid);
2955 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2956
2957out:
Neil Brownf2327d92005-09-13 01:25:37 -07002958 if (locku->lu_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 nfs4_get_stateowner(locku->lu_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002960 cstate->replay_owner = locku->lu_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07002961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 nfs4_unlock_state();
2963 return status;
2964
2965out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002966 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 goto out;
2968}
2969
2970/*
2971 * returns
2972 * 1: locks held by lockowner
2973 * 0: no locks held by lockowner
2974 */
2975static int
2976check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2977{
2978 struct file_lock **flpp;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08002979 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 int status = 0;
2981
2982 lock_kernel();
2983 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08002984 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 status = 1;
2986 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08002987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
2989out:
2990 unlock_kernel();
2991 return status;
2992}
2993
Al Virob37ad282006-10-19 23:28:59 -07002994__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002995nfsd4_release_lockowner(struct svc_rqst *rqstp,
2996 struct nfsd4_compound_state *cstate,
2997 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
2999 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07003000 struct nfs4_stateowner *sop;
3001 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07003003 struct list_head matches;
3004 int i;
Al Virob37ad282006-10-19 23:28:59 -07003005 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3008 clid->cl_boot, clid->cl_id);
3009
3010 /* XXX check for lease expiration */
3011
3012 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07003013 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016 nfs4_lock_state();
3017
NeilBrown3e9e3db2005-06-23 22:04:20 -07003018 status = nfserr_locks_held;
3019 /* XXX: we're doing a linear search through all the lockowners.
3020 * Yipes! For now we'll just hope clients aren't really using
3021 * release_lockowner much, but eventually we have to fix these
3022 * data structures. */
3023 INIT_LIST_HEAD(&matches);
3024 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3025 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003026 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07003027 continue;
3028 list_for_each_entry(stp, &sop->so_stateids,
3029 st_perstateowner) {
3030 if (check_for_locks(stp->st_vfs_file, sop))
3031 goto out;
3032 /* Note: so_perclient unused for lockowners,
3033 * so it's OK to fool with here. */
3034 list_add(&sop->so_perclient, &matches);
3035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07003037 }
3038 /* Clients probably won't expect us to return with some (but not all)
3039 * of the lockowner state released; so don't release any until all
3040 * have been checked. */
3041 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07003042 while (!list_empty(&matches)) {
3043 sop = list_entry(matches.next, struct nfs4_stateowner,
3044 so_perclient);
3045 /* unhash_stateowner deletes so_perclient only
3046 * for openowners. */
3047 list_del(&sop->so_perclient);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003048 release_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
3050out:
3051 nfs4_unlock_state();
3052 return status;
3053}
3054
3055static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07003056alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
NeilBrowna55370a2005-06-23 22:03:52 -07003058 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
NeilBrownc7b9a452005-06-23 22:04:30 -07003061int
3062nfs4_has_reclaimed_state(const char *name)
3063{
3064 unsigned int strhashval = clientstr_hashval(name);
3065 struct nfs4_client *clp;
3066
3067 clp = find_confirmed_client_by_str(name, strhashval);
3068 return clp ? 1 : 0;
3069}
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071/*
3072 * failure => all reset bets are off, nfserr_no_grace...
3073 */
NeilBrown190e4fb2005-06-23 22:04:25 -07003074int
3075nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
3077 unsigned int strhashval;
3078 struct nfs4_client_reclaim *crp = NULL;
3079
NeilBrowna55370a2005-06-23 22:03:52 -07003080 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3081 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 if (!crp)
3083 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07003084 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 INIT_LIST_HEAD(&crp->cr_strhash);
3086 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07003087 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 reclaim_str_hashtbl_size++;
3089 return 1;
3090}
3091
3092static void
3093nfs4_release_reclaim(void)
3094{
3095 struct nfs4_client_reclaim *crp = NULL;
3096 int i;
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3099 while (!list_empty(&reclaim_str_hashtbl[i])) {
3100 crp = list_entry(reclaim_str_hashtbl[i].next,
3101 struct nfs4_client_reclaim, cr_strhash);
3102 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 kfree(crp);
3104 reclaim_str_hashtbl_size--;
3105 }
3106 }
3107 BUG_ON(reclaim_str_hashtbl_size);
3108}
3109
3110/*
3111 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07003112static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113nfs4_find_reclaim_client(clientid_t *clid)
3114{
3115 unsigned int strhashval;
3116 struct nfs4_client *clp;
3117 struct nfs4_client_reclaim *crp = NULL;
3118
3119
3120 /* find clientid in conf_id_hashtbl */
3121 clp = find_confirmed_client(clid);
3122 if (clp == NULL)
3123 return NULL;
3124
NeilBrowna55370a2005-06-23 22:03:52 -07003125 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3126 clp->cl_name.len, clp->cl_name.data,
3127 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07003130 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07003132 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return crp;
3134 }
3135 }
3136 return NULL;
3137}
3138
3139/*
3140* Called from OPEN. Look for clientid in reclaim list.
3141*/
Al Virob37ad282006-10-19 23:28:59 -07003142__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143nfs4_check_open_reclaim(clientid_t *clid)
3144{
NeilBrowndfc83562005-06-23 22:03:16 -07003145 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}
3147
NeilBrownac4d8ff22005-06-23 22:03:30 -07003148/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003150int
NeilBrownac4d8ff22005-06-23 22:03:30 -07003151nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003153 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003155 status = nfsd4_init_slabs();
3156 if (status)
3157 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3159 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3160 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3161 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3162 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3163 }
3164 for (i = 0; i < FILE_HASH_SIZE; i++) {
3165 INIT_LIST_HEAD(&file_hashtbl[i]);
3166 }
3167 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3168 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3169 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3170 }
3171 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3172 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3173 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3174 }
3175 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3176 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3177 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 INIT_LIST_HEAD(&close_lru);
3181 INIT_LIST_HEAD(&client_lru);
3182 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07003183 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3184 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3185 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003186 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003187}
3188
NeilBrown190e4fb2005-06-23 22:04:25 -07003189static void
3190nfsd4_load_reboot_recovery_data(void)
3191{
3192 int status;
3193
NeilBrown0964a3d2005-06-23 22:04:32 -07003194 nfs4_lock_state();
3195 nfsd4_init_recdir(user_recovery_dirname);
NeilBrown190e4fb2005-06-23 22:04:25 -07003196 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07003197 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07003198 if (status)
3199 printk("NFSD: Failure reading reboot recovery data\n");
3200}
3201
Marc Eshel9a8db972007-07-17 04:04:35 -07003202unsigned long
3203get_nfs4_grace_period(void)
3204{
3205 return max(user_lease_time, lease_time) * HZ;
3206}
3207
Meelap Shahc2f1a552007-07-17 04:04:39 -07003208/*
3209 * Since the lifetime of a delegation isn't limited to that of an open, a
3210 * client may quite reasonably hang on to a delegation as long as it has
3211 * the inode cached. This becomes an obvious problem the first time a
3212 * client's inode cache approaches the size of the server's total memory.
3213 *
3214 * For now we avoid this problem by imposing a hard limit on the number
3215 * of delegations, which varies according to the server's memory size.
3216 */
3217static void
3218set_max_delegations(void)
3219{
3220 /*
3221 * Allow at most 4 delegations per megabyte of RAM. Quick
3222 * estimates suggest that in the worst case (where every delegation
3223 * is for a different inode), a delegation could take about 1.5K,
3224 * giving a worst case usage of about 6% of memory.
3225 */
3226 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
3227}
3228
NeilBrownac4d8ff22005-06-23 22:03:30 -07003229/* initialization to perform when the nfsd service is started: */
3230
3231static void
3232__nfs4_state_start(void)
3233{
Marc Eshel9a8db972007-07-17 04:04:35 -07003234 unsigned long grace_time;
NeilBrownac4d8ff22005-06-23 22:03:30 -07003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003237 grace_time = get_nfs4_grace_period();
NeilBrownd99a05a2005-06-23 22:03:21 -07003238 lease_time = user_lease_time;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003239 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07003240 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
3241 grace_time/HZ);
NeilBrown58da2822005-06-23 22:03:19 -07003242 laundry_wq = create_singlethread_workqueue("nfsd4");
Marc Eshel9a8db972007-07-17 04:04:35 -07003243 queue_delayed_work(laundry_wq, &laundromat_work, grace_time);
Meelap Shahc2f1a552007-07-17 04:04:39 -07003244 set_max_delegations();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003247void
NeilBrown76a35502005-06-23 22:03:26 -07003248nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 if (nfs4_init)
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003251 return;
NeilBrown190e4fb2005-06-23 22:04:25 -07003252 nfsd4_load_reboot_recovery_data();
NeilBrown76a35502005-06-23 22:03:26 -07003253 __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 nfs4_init = 1;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003255 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258time_t
3259nfs4_lease_time(void)
3260{
3261 return lease_time;
3262}
3263
3264static void
3265__nfs4_state_shutdown(void)
3266{
3267 int i;
3268 struct nfs4_client *clp = NULL;
3269 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 struct list_head *pos, *next, reaplist;
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3273 while (!list_empty(&conf_id_hashtbl[i])) {
3274 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3275 expire_client(clp);
3276 }
3277 while (!list_empty(&unconf_str_hashtbl[i])) {
3278 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3279 expire_client(clp);
3280 }
3281 }
3282 INIT_LIST_HEAD(&reaplist);
3283 spin_lock(&recall_lock);
3284 list_for_each_safe(pos, next, &del_recall_lru) {
3285 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3286 list_move(&dp->dl_recall_lru, &reaplist);
3287 }
3288 spin_unlock(&recall_lock);
3289 list_for_each_safe(pos, next, &reaplist) {
3290 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3291 list_del_init(&dp->dl_recall_lru);
3292 unhash_delegation(dp);
3293 }
3294
NeilBrown190e4fb2005-06-23 22:04:25 -07003295 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 nfs4_init = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
3299void
3300nfs4_state_shutdown(void)
3301{
NeilBrown5e8d5c22006-04-10 22:55:37 -07003302 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3303 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06003304 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 nfs4_lock_state();
3306 nfs4_release_reclaim();
3307 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 nfs4_unlock_state();
3309}
3310
Jeff Layton3dd98a32008-06-10 08:40:36 -04003311/*
3312 * user_recovery_dirname is protected by the nfsd_mutex since it's only
3313 * accessed when nfsd is starting.
3314 */
NeilBrown0964a3d2005-06-23 22:04:32 -07003315static void
3316nfs4_set_recdir(char *recdir)
3317{
NeilBrown0964a3d2005-06-23 22:04:32 -07003318 strcpy(user_recovery_dirname, recdir);
NeilBrown0964a3d2005-06-23 22:04:32 -07003319}
3320
3321/*
3322 * Change the NFSv4 recovery directory to recdir.
3323 */
3324int
3325nfs4_reset_recoverydir(char *recdir)
3326{
3327 int status;
Al Viroa63bb992008-08-02 01:03:36 -04003328 struct path path;
NeilBrown0964a3d2005-06-23 22:04:32 -07003329
Al Viroa63bb992008-08-02 01:03:36 -04003330 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
NeilBrown0964a3d2005-06-23 22:04:32 -07003331 if (status)
3332 return status;
3333 status = -ENOTDIR;
Al Viroa63bb992008-08-02 01:03:36 -04003334 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
NeilBrown0964a3d2005-06-23 22:04:32 -07003335 nfs4_set_recdir(recdir);
3336 status = 0;
3337 }
Al Viroa63bb992008-08-02 01:03:36 -04003338 path_put(&path);
NeilBrown0964a3d2005-06-23 22:04:32 -07003339 return status;
3340}
3341
Jeff Layton3dd98a32008-06-10 08:40:36 -04003342char *
3343nfs4_recoverydir(void)
3344{
3345 return user_recovery_dirname;
3346}
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348/*
3349 * Called when leasetime is changed.
3350 *
NeilBrownd99a05a2005-06-23 22:03:21 -07003351 * The only way the protocol gives us to handle on-the-fly lease changes is to
3352 * simulate a reboot. Instead of doing that, we just wait till the next time
3353 * we start to register any changes in lease time. If the administrator
3354 * really wants to change the lease time *now*, they can go ahead and bring
3355 * nfsd down and then back up again after changing the lease time.
Jeff Layton3dd98a32008-06-10 08:40:36 -04003356 *
3357 * user_lease_time is protected by nfsd_mutex since it's only really accessed
3358 * when nfsd is starting
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 */
3360void
3361nfs4_reset_lease(time_t leasetime)
3362{
NeilBrownd99a05a2005-06-23 22:03:21 -07003363 user_lease_time = leasetime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}