blob: edcced18caa835e59a435004ff40e712f5ccd8d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050041#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton363168b2009-08-14 12:57:56 -040042#include <linux/sunrpc/clnt.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020043#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050044#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define NFSDDBG_FACILITY NFSDDBG_PROC
47
48/* Globals */
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -050049time_t nfsd4_lease = 90; /* default lease time */
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -050050time_t nfsd4_grace = 90;
NeilBrownfd39ca92005-06-23 22:04:03 -070051static time_t boot_time;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -040052static u32 current_stateid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070053static stateid_t zerostateid; /* bits all 0 */
54static stateid_t onestateid; /* bits all 1 */
Andy Adamsonec6b5d72009-04-03 08:28:28 +030055static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070056
57#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
58#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040061static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
J. Bruce Fields8b671b82009-02-22 14:51:34 -080063/* Locking: */
64
65/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080066static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
J. Bruce Fields8b671b82009-02-22 14:51:34 -080068/*
69 * Currently used for the del_recall_lru and file hash table. In an
70 * effort to decrease the scope of the client_mutex, this spinlock may
71 * eventually cover more:
72 */
73static DEFINE_SPINLOCK(recall_lock);
74
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040075static struct kmem_cache *openowner_slab = NULL;
76static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080077static struct kmem_cache *file_slab = NULL;
78static struct kmem_cache *stateid_slab = NULL;
79static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081void
82nfs4_lock_state(void)
83{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080084 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87void
88nfs4_unlock_state(void)
89{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080090 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93static inline u32
94opaque_hashval(const void *ptr, int nbytes)
95{
96 unsigned char *cptr = (unsigned char *) ptr;
97
98 u32 x = 0;
99 while (nbytes--) {
100 x *= 37;
101 x += *cptr++;
102 }
103 return x;
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static struct list_head del_recall_lru;
107
NeilBrown13cd2182005-06-23 22:03:10 -0700108static inline void
109put_nfs4_file(struct nfs4_file *fi)
110{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800111 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
112 list_del(&fi->fi_hash);
113 spin_unlock(&recall_lock);
114 iput(fi->fi_inode);
115 kmem_cache_free(file_slab, fi);
116 }
NeilBrown13cd2182005-06-23 22:03:10 -0700117}
118
119static inline void
120get_nfs4_file(struct nfs4_file *fi)
121{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800122 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700123}
124
NeilBrownef0f3392006-04-10 22:55:41 -0700125static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700126unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700127
128/*
129 * Open owner state (share locks)
130 */
131
J. Bruce Fields506f2752011-08-11 18:50:18 -0400132/* hash tables for open owners */
133#define OPEN_OWNER_HASH_BITS 8
134#define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS)
135#define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700136
J. Bruce Fields506f2752011-08-11 18:50:18 -0400137static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400138{
139 unsigned int ret;
140
141 ret = opaque_hashval(ownername->data, ownername->len);
142 ret += clientid;
J. Bruce Fields506f2752011-08-11 18:50:18 -0400143 return ret & OPEN_OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400144}
NeilBrownef0f3392006-04-10 22:55:41 -0700145
J. Bruce Fields506f2752011-08-11 18:50:18 -0400146static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700147
148/* hash table for nfs4_file */
149#define FILE_HASH_BITS 8
150#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800151
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400152/* hash table for (open)nfs4_ol_stateid */
NeilBrownef0f3392006-04-10 22:55:41 -0700153#define STATEID_HASH_BITS 10
154#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
155#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
156
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400157static unsigned int file_hashval(struct inode *ino)
158{
159 /* XXX: why are we hashing on inode pointer, anyway? */
160 return hash_ptr(ino, FILE_HASH_BITS);
161}
162
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400163static unsigned int stateid_hashval(stateid_t *s)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400164{
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400165 return opaque_hashval(&s->si_opaque, sizeof(stateid_opaque_t)) & STATEID_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400166}
NeilBrownef0f3392006-04-10 22:55:41 -0700167
168static struct list_head file_hashtbl[FILE_HASH_SIZE];
169static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
170
J. Bruce Fields998db522010-08-07 09:21:41 -0400171static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400172{
173 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
174 atomic_inc(&fp->fi_access[oflag]);
175}
176
J. Bruce Fields998db522010-08-07 09:21:41 -0400177static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
178{
179 if (oflag == O_RDWR) {
180 __nfs4_file_get_access(fp, O_RDONLY);
181 __nfs4_file_get_access(fp, O_WRONLY);
182 } else
183 __nfs4_file_get_access(fp, oflag);
184}
185
186static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400187{
188 if (fp->fi_fds[oflag]) {
189 fput(fp->fi_fds[oflag]);
190 fp->fi_fds[oflag] = NULL;
191 }
192}
193
J. Bruce Fields998db522010-08-07 09:21:41 -0400194static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400195{
196 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400197 nfs4_file_put_fd(fp, oflag);
J. Bruce Fields3d02fa22011-09-19 15:07:41 -0400198 /*
199 * It's also safe to get rid of the RDWR open *if*
200 * we no longer have need of the other kind of access
201 * or if we already have the other kind of open:
202 */
203 if (fp->fi_fds[1-oflag]
204 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
205 nfs4_file_put_fd(fp, O_RDWR);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400206 }
207}
208
J. Bruce Fields998db522010-08-07 09:21:41 -0400209static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
210{
211 if (oflag == O_RDWR) {
212 __nfs4_file_put_access(fp, O_RDONLY);
213 __nfs4_file_put_access(fp, O_WRONLY);
214 } else
215 __nfs4_file_put_access(fp, oflag);
216}
217
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400218static inline void hash_stid(struct nfs4_stid *stid)
219{
220 stateid_t *s = &stid->sc_stateid;
221 unsigned int hashval;
222
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400223 hashval = stateid_hashval(s);
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400224 list_add(&stid->sc_hash, &stateid_hashtbl[hashval]);
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400228alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 struct nfs4_delegation *dp;
231 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400234 /*
235 * Major work on the lease subsystem (for example, to support
236 * calbacks on stat) will be required before we can support
237 * write delegations properly.
238 */
239 if (type != NFS4_OPEN_DELEGATE_READ)
240 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700241 if (fp->fi_had_conflict)
242 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700243 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700244 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700245 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
246 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700248 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700249 INIT_LIST_HEAD(&dp->dl_perfile);
250 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 INIT_LIST_HEAD(&dp->dl_recall_lru);
252 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700253 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dp->dl_type = type;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -0400256 dp->dl_stid.sc_type = NFS4_DELEG_STID;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -0400257 dp->dl_stid.sc_stateid.si_opaque.so_clid = clp->cl_clientid;
258 dp->dl_stid.sc_stateid.si_opaque.so_id = current_stateid++;
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -0400259 dp->dl_stid.sc_stateid.si_generation = 1;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -0400260 hash_stid(&dp->dl_stid);
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500261 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 dp->dl_time = 0;
263 atomic_set(&dp->dl_count, 1);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500264 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return dp;
266}
267
268void
269nfs4_put_delegation(struct nfs4_delegation *dp)
270{
271 if (atomic_dec_and_test(&dp->dl_count)) {
272 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700273 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700274 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700275 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277}
278
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500279static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500281 if (atomic_dec_and_test(&fp->fi_delegees)) {
282 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
283 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400284 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500285 fp->fi_deleg_file = NULL;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/* Called under the state lock. */
290static void
291unhash_delegation(struct nfs4_delegation *dp)
292{
J. Bruce Fieldsf459e452011-09-09 09:06:12 -0400293 list_del_init(&dp->dl_stid.sc_hash);
NeilBrownea1da632005-06-23 22:04:17 -0700294 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500296 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 list_del_init(&dp->dl_recall_lru);
298 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500299 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 nfs4_put_delegation(dp);
301}
302
303/*
304 * SETCLIENTID state
305 */
306
Benny Halevy36acb662010-05-12 00:13:04 +0300307/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300308static DEFINE_SPINLOCK(client_lock);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/* Hash tables for nfs4_clientid state */
311#define CLIENT_HASH_BITS 4
312#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
313#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
314
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400315static unsigned int clientid_hashval(u32 id)
316{
317 return id & CLIENT_HASH_MASK;
318}
319
320static unsigned int clientstr_hashval(const char *name)
321{
322 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*
326 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
327 * used in reboot/reset lease grace period processing
328 *
329 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
330 * setclientid_confirmed info.
331 *
332 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
333 * setclientid info.
334 *
335 * client_lru holds client queue ordered by nfs4_client.cl_time
336 * for lease renewal.
337 *
338 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
339 * for last close replay.
340 */
341static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
342static int reclaim_str_hashtbl_size = 0;
343static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
344static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
345static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
346static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
347static struct list_head client_lru;
348static struct list_head close_lru;
349
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400350/*
351 * We store the NONE, READ, WRITE, and BOTH bits separately in the
352 * st_{access,deny}_bmap field of the stateid, in order to track not
353 * only what share bits are currently in force, but also what
354 * combinations of share bits previous opens have used. This allows us
355 * to enforce the recommendation of rfc 3530 14.2.19 that the server
356 * return an error if the client attempt to downgrade to a combination
357 * of share bits not explicable by closing some of its previous opens.
358 *
359 * XXX: This enforcement is actually incomplete, since we don't keep
360 * track of access/deny bit combinations; so, e.g., we allow:
361 *
362 * OPEN allow read, deny write
363 * OPEN allow both, deny none
364 * DOWNGRADE allow read, deny none
365 *
366 * which we should reject.
367 */
368static void
369set_access(unsigned int *access, unsigned long bmap) {
370 int i;
371
372 *access = 0;
373 for (i = 1; i < 4; i++) {
374 if (test_bit(i, &bmap))
375 *access |= i;
376 }
377}
378
379static void
380set_deny(unsigned int *deny, unsigned long bmap) {
381 int i;
382
383 *deny = 0;
384 for (i = 0; i < 4; i++) {
385 if (test_bit(i, &bmap))
386 *deny |= i ;
387 }
388}
389
390static int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400391test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400392 unsigned int access, deny;
393
394 set_access(&access, stp->st_access_bmap);
395 set_deny(&deny, stp->st_deny_bmap);
396 if ((access & open->op_share_deny) || (deny & open->op_share_access))
397 return 0;
398 return 1;
399}
400
401static int nfs4_access_to_omode(u32 access)
402{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400403 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400404 case NFS4_SHARE_ACCESS_READ:
405 return O_RDONLY;
406 case NFS4_SHARE_ACCESS_WRITE:
407 return O_WRONLY;
408 case NFS4_SHARE_ACCESS_BOTH:
409 return O_RDWR;
410 }
411 BUG();
412}
413
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400414static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500415{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500416 list_del(&stp->st_perfile);
417 list_del(&stp->st_perstateowner);
418}
419
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400420static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500421{
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400422 int i;
J. Bruce Fields0997b172011-03-02 18:01:35 -0500423
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800424 if (stp->st_access_bmap) {
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400425 for (i = 1; i < 4; i++) {
426 if (test_bit(i, &stp->st_access_bmap))
427 nfs4_file_put_access(stp->st_file,
428 nfs4_access_to_omode(i));
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400429 __clear_bit(i, &stp->st_access_bmap);
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400430 }
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800431 }
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400432 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400433 stp->st_file = NULL;
434}
435
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400436static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400437{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500438 kmem_cache_free(stateid_slab, stp);
439}
440
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400441static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500442{
443 struct file *file;
444
445 unhash_generic_stateid(stp);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400446 list_del(&stp->st_stid.sc_hash);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500447 file = find_any_file(stp->st_file);
448 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400449 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400450 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500451 free_generic_stateid(stp);
452}
453
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400454static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500455{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400456 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500457
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400458 list_del(&lo->lo_owner.so_strhash);
459 list_del(&lo->lo_perstateid);
460 while (!list_empty(&lo->lo_owner.so_stateids)) {
461 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400462 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500463 release_lock_stateid(stp);
464 }
465}
466
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400467static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500468{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400469 unhash_lockowner(lo);
470 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500471}
472
473static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400474release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500475{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400476 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500477
478 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400479 lo = list_entry(open_stp->st_lockowners.next,
480 struct nfs4_lockowner, lo_perstateid);
481 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500482 }
483}
484
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400485static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500486{
487 unhash_generic_stateid(stp);
488 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400489 close_generic_stateid(stp);
490}
491
492static void release_open_stateid(struct nfs4_ol_stateid *stp)
493{
494 unhash_open_stateid(stp);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400495 list_del(&stp->st_stid.sc_hash);
J. Bruce Fields22839632009-01-11 14:27:17 -0500496 free_generic_stateid(stp);
497}
498
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400499static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500500{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400501 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500502
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400503 list_del(&oo->oo_owner.so_strhash);
504 list_del(&oo->oo_perclient);
505 while (!list_empty(&oo->oo_owner.so_stateids)) {
506 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400507 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500508 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500509 }
510}
511
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400512static void release_last_closed_stateid(struct nfs4_openowner *oo)
513{
514 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
515
516 if (s) {
517 list_del_init(&s->st_stid.sc_hash);
518 free_generic_stateid(s);
519 oo->oo_last_closed_stid = NULL;
520 }
521}
522
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400523static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500524{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400525 unhash_openowner(oo);
526 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400527 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400528 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500529}
530
Marc Eshel5282fd72009-04-03 08:27:52 +0300531#define SESSION_HASH_SIZE 512
532static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
533
534static inline int
535hash_sessionid(struct nfs4_sessionid *sessionid)
536{
537 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
538
539 return sid->sequence % SESSION_HASH_SIZE;
540}
541
542static inline void
543dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
544{
545 u32 *ptr = (u32 *)(&sessionid->data[0]);
546 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
547}
548
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300549static void
550gen_sessionid(struct nfsd4_session *ses)
551{
552 struct nfs4_client *clp = ses->se_client;
553 struct nfsd4_sessionid *sid;
554
555 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
556 sid->clientid = clp->cl_clientid;
557 sid->sequence = current_sessionid++;
558 sid->reserved = 0;
559}
560
561/*
Andy Adamsona649637c72009-08-28 08:45:01 -0400562 * The protocol defines ca_maxresponssize_cached to include the size of
563 * the rpc header, but all we need to cache is the data starting after
564 * the end of the initial SEQUENCE operation--the rest we regenerate
565 * each time. Therefore we can advertise a ca_maxresponssize_cached
566 * value that is the number of bytes in our cache plus a few additional
567 * bytes. In order to stay on the safe side, and not promise more than
568 * we can cache, those additional bytes must be the minimum possible: 24
569 * bytes of rpc header (xid through accept state, with AUTH_NULL
570 * verifier), 12 for the compound header (with zero-length tag), and 44
571 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300572 */
Andy Adamsona649637c72009-08-28 08:45:01 -0400573#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
574
Andy Adamson557ce262009-08-28 08:45:04 -0400575static void
576free_session_slots(struct nfsd4_session *ses)
577{
578 int i;
579
580 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
581 kfree(ses->se_slots[i]);
582}
583
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400584/*
585 * We don't actually need to cache the rpc and session headers, so we
586 * can allocate a little less for each slot:
587 */
588static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
589{
590 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
591}
592
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400593static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300594{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400595 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
596 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300597
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400598 return size;
599}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300600
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400601/*
602 * XXX: If we run out of reserved DRC memory we could (up to a point)
603 * re-negotiate active sessions and reduce their slot usage to make
604 * rooom for new connections. For now we just fail the create session.
605 */
606static int nfsd4_get_drc_mem(int slotsize, u32 num)
607{
608 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300609
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400610 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400611
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400612 spin_lock(&nfsd_drc_lock);
613 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
614 nfsd_drc_max_mem - nfsd_drc_mem_used);
615 num = min_t(int, num, avail / slotsize);
616 nfsd_drc_mem_used += num * slotsize;
617 spin_unlock(&nfsd_drc_lock);
618
619 return num;
620}
621
622static void nfsd4_put_drc_mem(int slotsize, int num)
623{
624 spin_lock(&nfsd_drc_lock);
625 nfsd_drc_mem_used -= slotsize * num;
626 spin_unlock(&nfsd_drc_lock);
627}
628
629static struct nfsd4_session *alloc_session(int slotsize, int numslots)
630{
631 struct nfsd4_session *new;
632 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300633
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400634 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400635 + sizeof(struct nfsd4_session) > PAGE_SIZE);
636 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400637
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400638 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300639 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400640 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400641 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400642 for (i = 0; i < numslots; i++) {
643 mem = sizeof(struct nfsd4_slot) + slotsize;
644 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
645 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400646 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400647 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400648 return new;
649out_free:
650 while (i--)
651 kfree(new->se_slots[i]);
652 kfree(new);
653 return NULL;
654}
655
656static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
657{
658 u32 maxrpc = nfsd_serv->sv_max_mesg;
659
660 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800661 new->maxresp_cached = min_t(u32, req->maxresp_cached,
662 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400663 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
664 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
665 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
666}
667
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400668static void free_conn(struct nfsd4_conn *c)
669{
670 svc_xprt_put(c->cn_xprt);
671 kfree(c);
672}
673
674static void nfsd4_conn_lost(struct svc_xpt_user *u)
675{
676 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
677 struct nfs4_client *clp = c->cn_session->se_client;
678
679 spin_lock(&clp->cl_lock);
680 if (!list_empty(&c->cn_persession)) {
681 list_del(&c->cn_persession);
682 free_conn(c);
683 }
684 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500685 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400686}
687
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400688static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400689{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400690 struct nfsd4_conn *conn;
691
692 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
693 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400694 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400695 svc_xprt_get(rqstp->rq_xprt);
696 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400697 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400698 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
699 return conn;
700}
701
J. Bruce Fields328ead22010-09-29 16:11:06 -0400702static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
703{
704 conn->cn_session = ses;
705 list_add(&conn->cn_persession, &ses->se_conns);
706}
707
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400708static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
709{
710 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400711
712 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400713 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400714 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400715}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400716
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400717static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400718{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400719 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400720 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400721}
722
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400723static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400724{
725 struct nfsd4_conn *conn;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400726 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400727
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400728 conn = alloc_conn(rqstp, dir);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400729 if (!conn)
730 return nfserr_jukebox;
731 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400732 ret = nfsd4_register_conn(conn);
733 if (ret)
734 /* oops; xprt is already down: */
735 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400736 return nfs_ok;
737}
738
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400739static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
740{
741 u32 dir = NFS4_CDFC4_FORE;
742
743 if (ses->se_flags & SESSION4_BACK_CHAN)
744 dir |= NFS4_CDFC4_BACK;
745
746 return nfsd4_new_conn(rqstp, ses, dir);
747}
748
749/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400750static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400751{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400752 struct nfs4_client *clp = s->se_client;
753 struct nfsd4_conn *c;
754
755 spin_lock(&clp->cl_lock);
756 while (!list_empty(&s->se_conns)) {
757 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
758 list_del_init(&c->cn_persession);
759 spin_unlock(&clp->cl_lock);
760
761 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
762 free_conn(c);
763
764 spin_lock(&clp->cl_lock);
765 }
766 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400767}
768
769void free_session(struct kref *kref)
770{
771 struct nfsd4_session *ses;
772 int mem;
773
774 ses = container_of(kref, struct nfsd4_session, se_ref);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400775 nfsd4_del_conns(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400776 spin_lock(&nfsd_drc_lock);
777 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
778 nfsd_drc_mem_used -= mem;
779 spin_unlock(&nfsd_drc_lock);
780 free_session_slots(ses);
781 kfree(ses);
782}
783
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400784static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400785{
786 struct nfsd4_session *new;
787 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
788 int numslots, slotsize;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400789 int status;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400790 int idx;
791
792 /*
793 * Note decreasing slot size below client's request may
794 * make it difficult for client to function correctly, whereas
795 * decreasing the number of slots will (just?) affect
796 * performance. When short on memory we therefore prefer to
797 * decrease number of slots instead of their size.
798 */
799 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
800 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800801 if (numslots < 1)
802 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400803
804 new = alloc_session(slotsize, numslots);
805 if (!new) {
806 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400807 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400808 }
809 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
Andy Adamson557ce262009-08-28 08:45:04 -0400810
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300811 new->se_client = clp;
812 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300813
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400814 INIT_LIST_HEAD(&new->se_conns);
815
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400816 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300817 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400818 new->se_cb_prog = cses->callback_prog;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300819 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400820 idx = hash_sessionid(&new->se_sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300821 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300822 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400823 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300824 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400825 spin_unlock(&clp->cl_lock);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300826 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300827
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400828 status = nfsd4_new_conn_from_crses(rqstp, new);
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400829 /* whoops: benny points out, status is ignored! (err, or bogus) */
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400830 if (status) {
831 free_session(&new->se_ref);
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400832 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400833 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400834 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400835 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400836 /*
837 * This is a little silly; with sessions there's no real
838 * use for the callback address. Use the peer address
839 * as a reasonable default for now, but consider fixing
840 * the rpc client not to require an address in the
841 * future:
842 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400843 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
844 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400845 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400846 nfsd4_probe_callback(clp);
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400847 return new;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300848}
849
Benny Halevy9089f1b2010-05-12 00:12:26 +0300850/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300851static struct nfsd4_session *
852find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
853{
854 struct nfsd4_session *elem;
855 int idx;
856
857 dump_sessionid(__func__, sessionid);
858 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300859 /* Search in the appropriate list */
860 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300861 if (!memcmp(elem->se_sessionid.data, sessionid->data,
862 NFS4_MAX_SESSIONID_LEN)) {
863 return elem;
864 }
865 }
866
867 dprintk("%s: session not found\n", __func__);
868 return NULL;
869}
870
Benny Halevy9089f1b2010-05-12 00:12:26 +0300871/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300872static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300873unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300874{
875 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400876 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300877 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400878 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +0300879}
880
Benny Halevy36acb662010-05-12 00:13:04 +0300881/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300883renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Benny Halevy07cd4902010-05-12 00:13:41 +0300885 if (is_client_expired(clp)) {
886 dprintk("%s: client (clientid %08x/%08x) already expired\n",
887 __func__,
888 clp->cl_clientid.cl_boot,
889 clp->cl_clientid.cl_id);
890 return;
891 }
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /*
894 * Move client to the end to the LRU list.
895 */
896 dprintk("renewing client (clientid %08x/%08x)\n",
897 clp->cl_clientid.cl_boot,
898 clp->cl_clientid.cl_id);
899 list_move_tail(&clp->cl_lru, &client_lru);
900 clp->cl_time = get_seconds();
901}
902
Benny Halevy36acb662010-05-12 00:13:04 +0300903static inline void
904renew_client(struct nfs4_client *clp)
905{
906 spin_lock(&client_lock);
907 renew_client_locked(clp);
908 spin_unlock(&client_lock);
909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
912static int
913STALE_CLIENTID(clientid_t *clid)
914{
915 if (clid->cl_boot == boot_time)
916 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +0300917 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
918 clid->cl_boot, clid->cl_id, boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 1;
920}
921
922/*
923 * XXX Should we use a slab cache ?
924 * This type of memory management is somewhat inefficient, but we use it
925 * anyway since SETCLIENTID is not a common operation.
926 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500927static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 struct nfs4_client *clp;
930
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500931 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
932 if (clp == NULL)
933 return NULL;
934 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
935 if (clp->cl_name.data == NULL) {
936 kfree(clp);
937 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500939 memcpy(clp->cl_name.data, name.data, name.len);
940 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return clp;
942}
943
944static inline void
945free_client(struct nfs4_client *clp)
946{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400947 while (!list_empty(&clp->cl_sessions)) {
948 struct nfsd4_session *ses;
949 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
950 se_perclnt);
951 list_del(&ses->se_perclnt);
952 nfsd4_put_session(ses);
953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (clp->cl_cred.cr_group_info)
955 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500956 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 kfree(clp->cl_name.data);
958 kfree(clp);
959}
960
Benny Halevyd7682982010-05-12 00:13:54 +0300961void
962release_session_client(struct nfsd4_session *session)
963{
964 struct nfs4_client *clp = session->se_client;
965
966 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
967 return;
968 if (is_client_expired(clp)) {
969 free_client(clp);
970 session->se_client = NULL;
971 } else
972 renew_client_locked(clp);
973 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +0300974}
975
Benny Halevy84d38ac2010-05-12 00:13:16 +0300976/* must be called under the client_lock */
977static inline void
978unhash_client_locked(struct nfs4_client *clp)
979{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400980 struct nfsd4_session *ses;
981
Benny Halevy07cd4902010-05-12 00:13:41 +0300982 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300983 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400984 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400985 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
986 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400987 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990static void
991expire_client(struct nfs4_client *clp)
992{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400993 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 struct list_head reaplist;
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 INIT_LIST_HEAD(&reaplist);
998 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700999 while (!list_empty(&clp->cl_delegations)) {
1000 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001001 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 list_move(&dp->dl_recall_lru, &reaplist);
1003 }
1004 spin_unlock(&recall_lock);
1005 while (!list_empty(&reaplist)) {
1006 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1007 list_del_init(&dp->dl_recall_lru);
1008 unhash_delegation(dp);
1009 }
NeilBrownea1da632005-06-23 22:04:17 -07001010 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001011 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1012 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001014 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001015 if (clp->cl_cb_conn.cb_xprt)
1016 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001017 list_del(&clp->cl_idhash);
1018 list_del(&clp->cl_strhash);
1019 spin_lock(&client_lock);
1020 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +03001021 if (atomic_read(&clp->cl_refcount) == 0)
1022 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001023 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001026static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1027{
1028 memcpy(target->cl_verifier.data, source->data,
1029 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001032static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1035 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1036}
1037
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001038static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 target->cr_uid = source->cr_uid;
1041 target->cr_gid = source->cr_gid;
1042 target->cr_group_info = source->cr_group_info;
1043 get_group_info(target->cr_group_info);
1044}
1045
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001046static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001047{
NeilBrowna55370a2005-06-23 22:03:52 -07001048 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001052same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1053{
1054 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001058same_clid(clientid_t *cl1, clientid_t *cl2)
1059{
1060 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
1063/* XXX what about NGROUP */
1064static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001065same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1066{
1067 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001070static void gen_clid(struct nfs4_client *clp)
1071{
1072 static u32 current_clientid = 1;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 clp->cl_clientid.cl_boot = boot_time;
1075 clp->cl_clientid.cl_id = current_clientid++;
1076}
1077
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001078static void gen_confirm(struct nfs4_client *clp)
1079{
1080 static u32 i;
1081 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001084 *p++ = get_seconds();
1085 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
J. Bruce Fields4581d142011-09-06 14:56:09 -04001088static int
1089same_stateid(stateid_t *id_one, stateid_t *id_two)
1090{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04001091 return 0 == memcmp(&id_one->si_opaque, &id_two->si_opaque,
1092 sizeof(stateid_opaque_t));
J. Bruce Fields4581d142011-09-06 14:56:09 -04001093}
1094
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001095static struct nfs4_stid *find_stateid(stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001096{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001097 struct nfs4_stid *s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001098 unsigned int hashval;
1099
J. Bruce Fieldsee626a72011-09-11 13:48:41 -04001100 hashval = stateid_hashval(t);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001101 list_for_each_entry(s, &stateid_hashtbl[hashval], sc_hash)
1102 if (same_stateid(&s->sc_stateid, t))
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001103 return s;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001104 return NULL;
1105}
1106
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001107static struct nfs4_stid *find_stateid_by_type(stateid_t *t, char typemask)
1108{
1109 struct nfs4_stid *s;
1110
1111 s = find_stateid(t);
1112 if (!s)
1113 return NULL;
1114 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001115 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001116 return NULL;
1117}
1118
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001119static struct nfs4_ol_stateid *find_ol_stateid_by_type(stateid_t *t, char typemask)
1120{
1121 struct nfs4_stid *s;
1122
1123 s = find_stateid_by_type(t, typemask);
1124 if (!s)
1125 return NULL;
1126 return openlockstateid(s);
1127}
1128
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001129static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1130 struct svc_rqst *rqstp, nfs4_verifier *verf)
1131{
1132 struct nfs4_client *clp;
1133 struct sockaddr *sa = svc_addr(rqstp);
1134 char *princ;
1135
1136 clp = alloc_client(name);
1137 if (clp == NULL)
1138 return NULL;
1139
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001140 INIT_LIST_HEAD(&clp->cl_sessions);
1141
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001142 princ = svc_gss_principal(rqstp);
1143 if (princ) {
1144 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1145 if (clp->cl_principal == NULL) {
1146 free_client(clp);
1147 return NULL;
1148 }
1149 }
1150
1151 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Benny Halevy46583e22010-05-12 00:13:29 +03001152 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001153 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001154 INIT_LIST_HEAD(&clp->cl_idhash);
1155 INIT_LIST_HEAD(&clp->cl_strhash);
1156 INIT_LIST_HEAD(&clp->cl_openowners);
1157 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001158 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001159 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001160 spin_lock_init(&clp->cl_lock);
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001161 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
Benny Halevy07cd4902010-05-12 00:13:41 +03001162 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001163 clear_bit(0, &clp->cl_cb_slot_busy);
1164 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1165 copy_verf(clp, verf);
1166 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1167 clp->cl_flavor = rqstp->rq_flavor;
1168 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1169 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001170 clp->cl_cb_session = NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001171 return clp;
1172}
1173
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001174static int check_name(struct xdr_netobj name)
1175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (name.len == 0)
1177 return 0;
1178 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001179 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return 0;
1181 }
1182 return 1;
1183}
1184
NeilBrownfd39ca92005-06-23 22:04:03 -07001185static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1187{
1188 unsigned int idhashval;
1189
1190 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1191 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1192 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001193 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
NeilBrownfd39ca92005-06-23 22:04:03 -07001196static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197move_to_confirmed(struct nfs4_client *clp)
1198{
1199 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1200 unsigned int strhashval;
1201
1202 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001203 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07001204 strhashval = clientstr_hashval(clp->cl_recdir);
Benny Halevy328efba2010-05-12 00:12:51 +03001205 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 renew_client(clp);
1207}
1208
1209static struct nfs4_client *
1210find_confirmed_client(clientid_t *clid)
1211{
1212 struct nfs4_client *clp;
1213 unsigned int idhashval = clientid_hashval(clid->cl_id);
1214
1215 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001216 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return clp;
1218 }
1219 return NULL;
1220}
1221
1222static struct nfs4_client *
1223find_unconfirmed_client(clientid_t *clid)
1224{
1225 struct nfs4_client *clp;
1226 unsigned int idhashval = clientid_hashval(clid->cl_id);
1227
1228 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001229 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return clp;
1231 }
1232 return NULL;
1233}
1234
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001235static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001236{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001237 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001238}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001239
NeilBrown28ce6052005-06-23 22:03:56 -07001240static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001241find_confirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001242{
1243 struct nfs4_client *clp;
1244
1245 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001246 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001247 return clp;
1248 }
1249 return NULL;
1250}
1251
1252static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001253find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001254{
1255 struct nfs4_client *clp;
1256
1257 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001258 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001259 return clp;
1260 }
1261 return NULL;
1262}
1263
NeilBrownfd39ca92005-06-23 22:04:03 -07001264static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001265gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001267 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001268 struct sockaddr *sa = svc_addr(rqstp);
1269 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001270 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Jeff Layton7077ecb2009-08-14 12:57:58 -04001272 /* Currently, we only support tcp and tcp6 for the callback channel */
1273 if (se->se_callback_netid_len == 3 &&
1274 !memcmp(se->se_callback_netid_val, "tcp", 3))
1275 expected_family = AF_INET;
1276 else if (se->se_callback_netid_len == 4 &&
1277 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1278 expected_family = AF_INET6;
1279 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 goto out_err;
1281
J. Bruce Fields07263f12010-05-31 19:09:40 -04001282 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001283 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001284 (struct sockaddr *)&conn->cb_addr,
1285 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001286
J. Bruce Fields07263f12010-05-31 19:09:40 -04001287 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001289
J. Bruce Fields07263f12010-05-31 19:09:40 -04001290 if (conn->cb_addr.ss_family == AF_INET6)
1291 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001292
J. Bruce Fields07263f12010-05-31 19:09:40 -04001293 conn->cb_prog = se->se_callback_prog;
1294 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001295 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return;
1297out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001298 conn->cb_addr.ss_family = AF_UNSPEC;
1299 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001300 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 "will not receive delegations\n",
1302 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return;
1305}
1306
Andy Adamson074fe892009-04-03 08:28:15 +03001307/*
Andy Adamson557ce262009-08-28 08:45:04 -04001308 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001309 */
1310void
1311nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1312{
Andy Adamson557ce262009-08-28 08:45:04 -04001313 struct nfsd4_slot *slot = resp->cstate.slot;
1314 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001315
Andy Adamson557ce262009-08-28 08:45:04 -04001316 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001317
Andy Adamson557ce262009-08-28 08:45:04 -04001318 slot->sl_opcnt = resp->opcnt;
1319 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001320
1321 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001322 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001323 return;
1324 }
Andy Adamson557ce262009-08-28 08:45:04 -04001325 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1326 base = (char *)resp->cstate.datap -
1327 (char *)resp->xbuf->head[0].iov_base;
1328 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1329 slot->sl_datalen))
1330 WARN("%s: sessions DRC could not cache compound\n", __func__);
1331 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001332}
1333
1334/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001335 * Encode the replay sequence operation from the slot values.
1336 * If cachethis is FALSE encode the uncached rep error on the next
1337 * operation which sets resp->p and increments resp->opcnt for
1338 * nfs4svc_encode_compoundres.
1339 *
Andy Adamson074fe892009-04-03 08:28:15 +03001340 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001341static __be32
1342nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1343 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001344{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001345 struct nfsd4_op *op;
1346 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001347
Andy Adamsonabfabf82009-07-23 19:02:18 -04001348 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
Andy Adamson557ce262009-08-28 08:45:04 -04001349 resp->opcnt, resp->cstate.slot->sl_cachethis);
Andy Adamsonabfabf82009-07-23 19:02:18 -04001350
1351 /* Encode the replayed sequence operation */
1352 op = &args->ops[resp->opcnt - 1];
1353 nfsd4_encode_operation(resp, op);
1354
1355 /* Return nfserr_retry_uncached_rep in next operation. */
Andy Adamson557ce262009-08-28 08:45:04 -04001356 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001357 op = &args->ops[resp->opcnt++];
1358 op->status = nfserr_retry_uncached_rep;
1359 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001360 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001361 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001362}
1363
1364/*
Andy Adamson557ce262009-08-28 08:45:04 -04001365 * The sequence operation is not cached because we can use the slot and
1366 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001367 */
1368__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001369nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1370 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001371{
Andy Adamson557ce262009-08-28 08:45:04 -04001372 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001373 __be32 status;
1374
Andy Adamson557ce262009-08-28 08:45:04 -04001375 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001376
Andy Adamsonabfabf82009-07-23 19:02:18 -04001377 /* Either returns 0 or nfserr_retry_uncached */
1378 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1379 if (status == nfserr_retry_uncached_rep)
1380 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001381
Andy Adamson557ce262009-08-28 08:45:04 -04001382 /* The sequence operation has been encoded, cstate->datap set. */
1383 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001384
Andy Adamson557ce262009-08-28 08:45:04 -04001385 resp->opcnt = slot->sl_opcnt;
1386 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1387 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001388
1389 return status;
1390}
1391
Andy Adamson0733d212009-04-03 08:28:01 +03001392/*
1393 * Set the exchange_id flags returned by the server.
1394 */
1395static void
1396nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1397{
1398 /* pNFS is not supported */
1399 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1400
1401 /* Referrals are supported, Migration is not. */
1402 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1403
1404 /* set the wire flags to return to client. */
1405 clid->flags = new->cl_exchange_flags;
1406}
1407
Al Virob37ad282006-10-19 23:28:59 -07001408__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001409nfsd4_exchange_id(struct svc_rqst *rqstp,
1410 struct nfsd4_compound_state *cstate,
1411 struct nfsd4_exchange_id *exid)
1412{
Andy Adamson0733d212009-04-03 08:28:01 +03001413 struct nfs4_client *unconf, *conf, *new;
1414 int status;
1415 unsigned int strhashval;
1416 char dname[HEXDIR_LEN];
Jeff Layton363168b2009-08-14 12:57:56 -04001417 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001418 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001419 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamson0733d212009-04-03 08:28:01 +03001420
Jeff Layton363168b2009-08-14 12:57:56 -04001421 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001422 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001423 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001424 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001425 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001426
1427 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1428 return nfserr_inval;
1429
1430 /* Currently only support SP4_NONE */
1431 switch (exid->spa_how) {
1432 case SP4_NONE:
1433 break;
1434 case SP4_SSV:
J. Bruce Fields044bc1d2010-11-12 14:36:06 -05001435 return nfserr_serverfault;
Andy Adamson0733d212009-04-03 08:28:01 +03001436 default:
1437 BUG(); /* checked by xdr code */
1438 case SP4_MACH_CRED:
1439 return nfserr_serverfault; /* no excuse :-/ */
1440 }
1441
1442 status = nfs4_make_rec_clidname(dname, &exid->clname);
1443
1444 if (status)
1445 goto error;
1446
1447 strhashval = clientstr_hashval(dname);
1448
1449 nfs4_lock_state();
1450 status = nfs_ok;
1451
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001452 conf = find_confirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001453 if (conf) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001454 if (!clp_used_exchangeid(conf)) {
1455 status = nfserr_clid_inuse; /* XXX: ? */
1456 goto out;
1457 }
Andy Adamson0733d212009-04-03 08:28:01 +03001458 if (!same_verf(&verf, &conf->cl_verifier)) {
1459 /* 18.35.4 case 8 */
1460 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1461 status = nfserr_not_same;
1462 goto out;
1463 }
1464 /* Client reboot: destroy old state */
1465 expire_client(conf);
1466 goto out_new;
1467 }
1468 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1469 /* 18.35.4 case 9 */
1470 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1471 status = nfserr_perm;
1472 goto out;
1473 }
1474 expire_client(conf);
1475 goto out_new;
1476 }
Andy Adamson0733d212009-04-03 08:28:01 +03001477 /*
1478 * Set bit when the owner id and verifier map to an already
1479 * confirmed client id (18.35.3).
1480 */
1481 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1482
1483 /*
1484 * Falling into 18.35.4 case 2, possible router replay.
1485 * Leave confirmed record intact and return same result.
1486 */
1487 copy_verf(conf, &verf);
1488 new = conf;
1489 goto out_copy;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001490 }
1491
1492 /* 18.35.4 case 7 */
1493 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1494 status = nfserr_noent;
1495 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001496 }
1497
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001498 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001499 if (unconf) {
1500 /*
1501 * Possible retry or client restart. Per 18.35.4 case 4,
1502 * a new unconfirmed record should be generated regardless
1503 * of whether any properties have changed.
1504 */
1505 expire_client(unconf);
1506 }
1507
1508out_new:
1509 /* Normal case */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001510 new = create_client(exid->clname, dname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001511 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001512 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001513 goto out;
1514 }
1515
Andy Adamson0733d212009-04-03 08:28:01 +03001516 gen_clid(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001517 add_to_unconfirmed(new, strhashval);
1518out_copy:
1519 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1520 exid->clientid.cl_id = new->cl_clientid.cl_id;
1521
Andy Adamson38eb76a2009-04-03 08:28:32 +03001522 exid->seqid = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001523 nfsd4_set_ex_flags(new, exid);
1524
1525 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001526 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001527 status = nfs_ok;
1528
1529out:
1530 nfs4_unlock_state();
1531error:
1532 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1533 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001534}
1535
Benny Halevyb85d4c02009-04-03 08:28:08 +03001536static int
Andy Adamson88e588d2009-07-23 19:02:15 -04001537check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001538{
Andy Adamson88e588d2009-07-23 19:02:15 -04001539 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1540 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001541
1542 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001543 if (slot_inuse) {
1544 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001545 return nfserr_jukebox;
1546 else
1547 return nfserr_seq_misordered;
1548 }
1549 /* Normal */
Andy Adamson88e588d2009-07-23 19:02:15 -04001550 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001551 return nfs_ok;
1552 /* Replay */
Andy Adamson88e588d2009-07-23 19:02:15 -04001553 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001554 return nfserr_replay_cache;
1555 /* Wraparound */
Andy Adamson88e588d2009-07-23 19:02:15 -04001556 if (seqid == 1 && (slot_seqid + 1) == 0)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001557 return nfs_ok;
1558 /* Misordered replay or misordered new request */
1559 return nfserr_seq_misordered;
1560}
1561
Andy Adamson49557cc2009-07-23 19:02:16 -04001562/*
1563 * Cache the create session result into the create session single DRC
1564 * slot cache by saving the xdr structure. sl_seqid has been set.
1565 * Do this for solo or embedded create session operations.
1566 */
1567static void
1568nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1569 struct nfsd4_clid_slot *slot, int nfserr)
1570{
1571 slot->sl_status = nfserr;
1572 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1573}
1574
1575static __be32
1576nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1577 struct nfsd4_clid_slot *slot)
1578{
1579 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1580 return slot->sl_status;
1581}
1582
Mi Jinlong1b74c252011-07-14 14:50:17 +08001583#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1584 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1585 1 + /* MIN tag is length with zero, only length */ \
1586 3 + /* version, opcount, opcode */ \
1587 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1588 /* seqid, slotID, slotID, cache */ \
1589 4 ) * sizeof(__be32))
1590
1591#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1592 2 + /* verifier: AUTH_NULL, length 0 */\
1593 1 + /* status */ \
1594 1 + /* MIN tag is length with zero, only length */ \
1595 3 + /* opcount, opcode, opstatus*/ \
1596 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1597 /* seqid, slotID, slotID, slotID, status */ \
1598 5 ) * sizeof(__be32))
1599
1600static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1601{
1602 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1603 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1604}
1605
Andy Adamson069b6ad2009-04-03 08:27:58 +03001606__be32
1607nfsd4_create_session(struct svc_rqst *rqstp,
1608 struct nfsd4_compound_state *cstate,
1609 struct nfsd4_create_session *cr_ses)
1610{
Jeff Layton363168b2009-08-14 12:57:56 -04001611 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001612 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001613 struct nfsd4_session *new;
Andy Adamson49557cc2009-07-23 19:02:16 -04001614 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001615 bool confirm_me = false;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001616 int status = 0;
1617
Mi Jinlonga62573d2011-03-23 17:57:07 +08001618 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1619 return nfserr_inval;
1620
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001621 nfs4_lock_state();
1622 unconf = find_unconfirmed_client(&cr_ses->clientid);
1623 conf = find_confirmed_client(&cr_ses->clientid);
1624
1625 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001626 cs_slot = &conf->cl_cs_slot;
1627 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001628 if (status == nfserr_replay_cache) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001629 dprintk("Got a create_session replay! seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001630 cs_slot->sl_seqid);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001631 /* Return the cached reply status */
Andy Adamson49557cc2009-07-23 19:02:16 -04001632 status = nfsd4_replay_create_session(cr_ses, cs_slot);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001633 goto out;
Andy Adamson49557cc2009-07-23 19:02:16 -04001634 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001635 status = nfserr_seq_misordered;
1636 dprintk("Sequence misordered!\n");
1637 dprintk("Expected seqid= %d but got seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001638 cs_slot->sl_seqid, cr_ses->seqid);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001639 goto out;
1640 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001641 } else if (unconf) {
1642 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001643 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001644 status = nfserr_clid_inuse;
1645 goto out;
1646 }
1647
Andy Adamson49557cc2009-07-23 19:02:16 -04001648 cs_slot = &unconf->cl_cs_slot;
1649 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001650 if (status) {
1651 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001652 status = nfserr_seq_misordered;
J. Bruce Fieldscd5b8142010-10-02 17:03:35 -04001653 goto out;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001654 }
1655
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001656 confirm_me = true;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001657 conf = unconf;
1658 } else {
1659 status = nfserr_stale_clientid;
1660 goto out;
1661 }
1662
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001663 /*
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001664 * XXX: we should probably set this at creation time, and check
1665 * for consistent minorversion use throughout:
1666 */
1667 conf->cl_minorversion = 1;
1668 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001669 * We do not support RDMA or persistent sessions
1670 */
1671 cr_ses->flags &= ~SESSION4_PERSIST;
1672 cr_ses->flags &= ~SESSION4_RDMA;
1673
Mi Jinlong1b74c252011-07-14 14:50:17 +08001674 status = nfserr_toosmall;
1675 if (check_forechannel_attrs(cr_ses->fore_channel))
1676 goto out;
1677
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001678 status = nfserr_jukebox;
1679 new = alloc_init_session(rqstp, conf, cr_ses);
1680 if (!new)
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001681 goto out;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001682 status = nfs_ok;
1683 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001684 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001685 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1686 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001687 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001688 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001689
Andy Adamson49557cc2009-07-23 19:02:16 -04001690 /* cache solo and embedded create sessions under the state lock */
1691 nfsd4_cache_create_session(cr_ses, cs_slot, status);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001692 if (confirm_me)
1693 move_to_confirmed(conf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001694out:
1695 nfs4_unlock_state();
1696 dprintk("%s returns %d\n", __func__, ntohl(status));
1697 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001698}
1699
J. Bruce Fields57716352010-04-21 12:27:19 -04001700static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1701{
1702 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1703 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1704
1705 return argp->opcnt == resp->opcnt;
1706}
1707
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001708static __be32 nfsd4_map_bcts_dir(u32 *dir)
1709{
1710 switch (*dir) {
1711 case NFS4_CDFC4_FORE:
1712 case NFS4_CDFC4_BACK:
1713 return nfs_ok;
1714 case NFS4_CDFC4_FORE_OR_BOTH:
1715 case NFS4_CDFC4_BACK_OR_BOTH:
1716 *dir = NFS4_CDFC4_BOTH;
1717 return nfs_ok;
1718 };
1719 return nfserr_inval;
1720}
1721
1722__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1723 struct nfsd4_compound_state *cstate,
1724 struct nfsd4_bind_conn_to_session *bcts)
1725{
1726 __be32 status;
1727
1728 if (!nfsd4_last_compound_op(rqstp))
1729 return nfserr_not_only_op;
1730 spin_lock(&client_lock);
1731 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1732 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1733 * client_lock iself: */
1734 if (cstate->session) {
1735 nfsd4_get_session(cstate->session);
1736 atomic_inc(&cstate->session->se_client->cl_refcount);
1737 }
1738 spin_unlock(&client_lock);
1739 if (!cstate->session)
1740 return nfserr_badsession;
1741
1742 status = nfsd4_map_bcts_dir(&bcts->dir);
Bryan Schumaker1db2b9d2011-04-27 15:47:15 -04001743 if (!status)
1744 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1745 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001746}
1747
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001748static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1749{
1750 if (!session)
1751 return 0;
1752 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1753}
1754
Andy Adamson069b6ad2009-04-03 08:27:58 +03001755__be32
1756nfsd4_destroy_session(struct svc_rqst *r,
1757 struct nfsd4_compound_state *cstate,
1758 struct nfsd4_destroy_session *sessionid)
1759{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001760 struct nfsd4_session *ses;
1761 u32 status = nfserr_badsession;
1762
1763 /* Notes:
1764 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1765 * - Should we return nfserr_back_chan_busy if waiting for
1766 * callbacks on to-be-destroyed session?
1767 * - Do we need to clear any callback info from previous session?
1768 */
1769
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001770 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001771 if (!nfsd4_last_compound_op(r))
1772 return nfserr_not_only_op;
1773 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001774 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001775 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001776 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1777 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001778 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001779 goto out;
1780 }
1781
1782 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001783 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001784
Benny Halevyab707e152010-05-12 00:14:06 +03001785 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001786 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001787 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001788
1789 nfsd4_del_conns(ses);
1790
Benny Halevye10e0cf2009-04-03 08:28:38 +03001791 nfsd4_put_session(ses);
1792 status = nfs_ok;
1793out:
1794 dprintk("%s returns %d\n", __func__, ntohl(status));
1795 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001796}
1797
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001798static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001799{
1800 struct nfsd4_conn *c;
1801
1802 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001803 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001804 return c;
1805 }
1806 }
1807 return NULL;
1808}
1809
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001810static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001811{
1812 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001813 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001814 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001815
1816 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001817 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001818 if (c) {
1819 spin_unlock(&clp->cl_lock);
1820 free_conn(new);
1821 return;
1822 }
1823 __nfsd4_hash_conn(new, ses);
1824 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001825 ret = nfsd4_register_conn(new);
1826 if (ret)
1827 /* oops; xprt is already down: */
1828 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001829 return;
1830}
1831
Mi Jinlong868b89c2011-04-27 09:09:58 +08001832static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1833{
1834 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1835
1836 return args->opcnt > session->se_fchannel.maxops;
1837}
1838
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001839static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1840 struct nfsd4_session *session)
1841{
1842 struct xdr_buf *xb = &rqstp->rq_arg;
1843
1844 return xb->len > session->se_fchannel.maxreq_sz;
1845}
1846
Andy Adamson069b6ad2009-04-03 08:27:58 +03001847__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001848nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001849 struct nfsd4_compound_state *cstate,
1850 struct nfsd4_sequence *seq)
1851{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001852 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001853 struct nfsd4_session *session;
1854 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001855 struct nfsd4_conn *conn;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001856 int status;
1857
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001858 if (resp->opcnt != 1)
1859 return nfserr_sequence_pos;
1860
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001861 /*
1862 * Will be either used or freed by nfsd4_sequence_check_conn
1863 * below.
1864 */
1865 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1866 if (!conn)
1867 return nfserr_jukebox;
1868
Benny Halevy9089f1b2010-05-12 00:12:26 +03001869 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001870 status = nfserr_badsession;
1871 session = find_in_sessionid_hashtbl(&seq->sessionid);
1872 if (!session)
1873 goto out;
1874
Mi Jinlong868b89c2011-04-27 09:09:58 +08001875 status = nfserr_too_many_ops;
1876 if (nfsd4_session_too_many_ops(rqstp, session))
1877 goto out;
1878
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001879 status = nfserr_req_too_big;
1880 if (nfsd4_request_too_big(rqstp, session))
1881 goto out;
1882
Benny Halevyb85d4c02009-04-03 08:28:08 +03001883 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03001884 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001885 goto out;
1886
Andy Adamson557ce262009-08-28 08:45:04 -04001887 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03001888 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1889
Andy Adamsona8dfdae2009-08-28 08:45:02 -04001890 /* We do not negotiate the number of slots yet, so set the
1891 * maxslots to the session maxreqs which is used to encode
1892 * sr_highest_slotid and the sr_target_slot id to maxslots */
1893 seq->maxslots = session->se_fchannel.maxreqs;
1894
Andy Adamson88e588d2009-07-23 19:02:15 -04001895 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001896 if (status == nfserr_replay_cache) {
1897 cstate->slot = slot;
1898 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001899 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04001900 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03001901 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03001902 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001903 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001904 }
1905 if (status)
1906 goto out;
1907
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001908 nfsd4_sequence_check_conn(conn, session);
1909 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001910
Benny Halevyb85d4c02009-04-03 08:28:08 +03001911 /* Success! bump slot seqid */
1912 slot->sl_inuse = true;
1913 slot->sl_seqid = seq->seqid;
Andy Adamson557ce262009-08-28 08:45:04 -04001914 slot->sl_cachethis = seq->cachethis;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001915
1916 cstate->slot = slot;
1917 cstate->session = session;
1918
Benny Halevyb85d4c02009-04-03 08:28:08 +03001919out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04001920 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001921 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001922 struct nfs4_client *clp = session->se_client;
1923
Benny Halevy36acb662010-05-12 00:13:04 +03001924 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001925 atomic_inc(&clp->cl_refcount);
1926 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1927 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001928 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001929 kfree(conn);
Benny Halevy36acb662010-05-12 00:13:04 +03001930 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001931 dprintk("%s: return %d\n", __func__, ntohl(status));
1932 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001933}
1934
1935__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001936nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1937{
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001938 int status = 0;
1939
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001940 if (rc->rca_one_fs) {
1941 if (!cstate->current_fh.fh_dentry)
1942 return nfserr_nofilehandle;
1943 /*
1944 * We don't take advantage of the rca_one_fs case.
1945 * That's OK, it's optional, we can safely ignore it.
1946 */
1947 return nfs_ok;
1948 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001949
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001950 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001951 status = nfserr_complete_already;
1952 if (cstate->session->se_client->cl_firststate)
1953 goto out;
1954
1955 status = nfserr_stale_clientid;
1956 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001957 /*
1958 * The following error isn't really legal.
1959 * But we only get here if the client just explicitly
1960 * destroyed the client. Surely it no longer cares what
1961 * error it gets back on an operation for the dead
1962 * client.
1963 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001964 goto out;
1965
1966 status = nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001967 nfsd4_create_clid_dir(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001968out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001969 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001970 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001971}
1972
1973__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001974nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1975 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 struct xdr_netobj clname = {
1978 .len = setclid->se_namelen,
1979 .data = setclid->se_name,
1980 };
1981 nfs4_verifier clverifier = setclid->se_verf;
1982 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07001983 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07001984 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -07001985 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -07001988 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
NeilBrowna55370a2005-06-23 22:03:52 -07001990 status = nfs4_make_rec_clidname(dname, &clname);
1991 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07001992 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /*
1995 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1996 * We get here on a DRC miss.
1997 */
1998
NeilBrowna55370a2005-06-23 22:03:52 -07001999 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 nfs4_lock_state();
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002002 conf = find_confirmed_client_by_str(dname, strhashval);
NeilBrown28ce6052005-06-23 22:03:56 -07002003 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002004 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002006 if (clp_used_exchangeid(conf))
2007 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002008 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002009 char addr_str[INET6_ADDRSTRLEN];
2010 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2011 sizeof(addr_str));
2012 dprintk("NFSD: setclientid: string in use by client "
2013 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 goto out;
2015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002017 /*
2018 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
2019 * has a description of SETCLIENTID request processing consisting
2020 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2021 */
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002022 unconf = find_unconfirmed_client_by_str(dname, strhashval);
J. Bruce Fields3e772462011-08-10 19:07:33 -04002023 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002025 /*
2026 * RFC 3530 14.2.33 CASE 4:
2027 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 */
2029 if (unconf)
2030 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002031 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002032 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002035 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002037 * RFC 3530 14.2.33 CASE 1:
2038 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07002040 if (unconf) {
2041 /* Note this is removing unconfirmed {*x***},
2042 * which is stronger than RFC recommended {vxc**}.
2043 * This has the advantage that there is at most
2044 * one {*x***} in either list at any time.
2045 */
2046 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002048 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002049 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 } else if (!unconf) {
2053 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002054 * RFC 3530 14.2.33 CASE 2:
2055 * probable client reboot; state will be removed if
2056 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002058 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002059 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05002062 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002063 /*
2064 * RFC 3530 14.2.33 CASE 3:
2065 * probable client reboot; state will be removed if
2066 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 */
2068 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002069 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002070 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002074 /*
2075 * XXX: we should probably set this at creation time, and check
2076 * for consistent minorversion use throughout:
2077 */
2078 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002079 gen_callback(new, setclid, rqstp);
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04002080 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2082 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2083 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2084 status = nfs_ok;
2085out:
2086 nfs4_unlock_state();
2087 return status;
2088}
2089
2090
2091/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002092 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2093 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2094 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 */
Al Virob37ad282006-10-19 23:28:59 -07002096__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002097nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2098 struct nfsd4_compound_state *cstate,
2099 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Jeff Layton363168b2009-08-14 12:57:56 -04002101 struct sockaddr *sa = svc_addr(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -07002102 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2104 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002105 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 if (STALE_CLIENTID(clid))
2108 return nfserr_stale_clientid;
2109 /*
2110 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2111 * We get here on a DRC miss.
2112 */
2113
2114 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002115
2116 conf = find_confirmed_client(clid);
2117 unconf = find_unconfirmed_client(clid);
2118
2119 status = nfserr_clid_inuse;
Jeff Layton363168b2009-08-14 12:57:56 -04002120 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002121 goto out;
Jeff Layton363168b2009-08-14 12:57:56 -04002122 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002123 goto out;
2124
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002125 /*
2126 * section 14.2.34 of RFC 3530 has a description of
2127 * SETCLIENTID_CONFIRM request processing consisting
2128 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2129 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -05002130 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002131 /*
2132 * RFC 3530 14.2.34 CASE 1:
2133 * callback update
2134 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002135 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 status = nfserr_clid_inuse;
2137 else {
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002138 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2139 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002140 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07002142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05002144 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002145 /*
2146 * RFC 3530 14.2.34 CASE 2:
2147 * probable retransmitted request; play it safe and
2148 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07002149 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002150 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07002152 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07002154 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002155 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002156 /*
2157 * RFC 3530 14.2.34 CASE 3:
2158 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07002159 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002160 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 status = nfserr_clid_inuse;
2162 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07002163 unsigned int hash =
2164 clientstr_hashval(unconf->cl_recdir);
2165 conf = find_confirmed_client_by_str(unconf->cl_recdir,
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002166 hash);
NeilBrown1a69c172005-06-23 22:04:08 -07002167 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07002168 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002169 expire_client(conf);
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07002172 conf = unconf;
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002173 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002174 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002176 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2177 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07002178 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002179 /*
2180 * RFC 3530 14.2.34 CASE 4:
2181 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07002182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07002184 } else {
NeilBrown08e89872005-06-23 22:04:11 -07002185 /* check that we have hit one of the cases...*/
2186 status = nfserr_clid_inuse;
2187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 nfs4_unlock_state();
2190 return status;
2191}
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193/* OPEN Share state helper functions */
2194static inline struct nfs4_file *
2195alloc_init_file(struct inode *ino)
2196{
2197 struct nfs4_file *fp;
2198 unsigned int hashval = file_hashval(ino);
2199
NeilBrowne60d4392005-06-23 22:03:01 -07002200 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2201 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002202 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07002204 INIT_LIST_HEAD(&fp->fi_stateids);
2205 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 fp->fi_inode = igrab(ino);
Meelap Shah47f99402007-07-17 04:04:40 -07002207 fp->fi_had_conflict = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002208 fp->fi_lease = NULL;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002209 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2210 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Pavel Emelyanov47cee542010-05-17 20:00:37 +04002211 spin_lock(&recall_lock);
2212 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2213 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 return fp;
2215 }
2216 return NULL;
2217}
2218
2219static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002220nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002221{
NeilBrowne60d4392005-06-23 22:03:01 -07002222 if (*slab == NULL)
2223 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002224 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002225 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002226}
2227
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002228void
NeilBrowne60d4392005-06-23 22:03:01 -07002229nfsd4_free_slabs(void)
2230{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002231 nfsd4_free_slab(&openowner_slab);
2232 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002233 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002234 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002235 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002236}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238static int
2239nfsd4_init_slabs(void)
2240{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002241 openowner_slab = kmem_cache_create("nfsd4_openowners",
2242 sizeof(struct nfs4_openowner), 0, 0, NULL);
2243 if (openowner_slab == NULL)
2244 goto out_nomem;
2245 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2246 sizeof(struct nfs4_openowner), 0, 0, NULL);
2247 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002248 goto out_nomem;
2249 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002250 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002251 if (file_slab == NULL)
2252 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002253 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002254 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002255 if (stateid_slab == NULL)
2256 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002257 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002258 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002259 if (deleg_slab == NULL)
2260 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002262out_nomem:
2263 nfsd4_free_slabs();
2264 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2265 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002268void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002270 kfree(oo->oo_owner.so_owner.data);
2271 kmem_cache_free(openowner_slab, oo);
2272}
2273
2274void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2275{
2276 kfree(lo->lo_owner.so_owner.data);
2277 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002280static void init_nfs4_replay(struct nfs4_replay *rp)
2281{
2282 rp->rp_status = nfserr_serverfault;
2283 rp->rp_buflen = 0;
2284 rp->rp_buf = rp->rp_ibuf;
2285}
2286
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002287static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
2289 struct nfs4_stateowner *sop;
2290
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002291 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002292 if (!sop)
2293 return NULL;
2294
2295 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2296 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002297 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002298 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002300 sop->so_owner.len = owner->len;
2301
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002302 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002303 sop->so_client = clp;
2304 init_nfs4_replay(&sop->so_replay);
2305 return sop;
2306}
2307
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002308static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002309{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002310 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2311 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002314static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002316 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002318 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2319 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002321 oo->oo_owner.so_is_open_owner = 1;
2322 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002323 oo->oo_flags = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002324 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002325 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002326 INIT_LIST_HEAD(&oo->oo_close_lru);
2327 hash_openowner(oo, clp, strhashval);
2328 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331static inline void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002332init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002333 struct nfs4_openowner *oo = open->op_openowner;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04002334 struct nfs4_client *clp = oo->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
NeilBrownea1da632005-06-23 22:04:17 -07002336 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002337 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002338 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002339 stp->st_stid.sc_type = NFS4_OPEN_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002340 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002341 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 stp->st_file = fp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04002343 stp->st_stid.sc_stateid.si_opaque.so_clid = clp->cl_clientid;
2344 stp->st_stid.sc_stateid.si_opaque.so_id = current_stateid++;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04002345 /* note will be incremented before first return to client: */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002346 stp->st_stid.sc_stateid.si_generation = 0;
J. Bruce Fields36d44c62011-09-08 12:16:03 -04002347 hash_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 stp->st_access_bmap = 0;
2349 stp->st_deny_bmap = 0;
Andy Adamson84459a12009-04-03 08:28:56 +03002350 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2351 &stp->st_access_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07002353 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
2356static void
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002357move_to_close_lru(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002359 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002361 list_move_tail(&oo->oo_close_lru, &close_lru);
2362 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002366same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2367 clientid_t *clid)
2368{
2369 return (sop->so_owner.len == owner->len) &&
2370 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2371 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002374static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2376{
2377 struct nfs4_stateowner *so = NULL;
2378
J. Bruce Fields506f2752011-08-11 18:50:18 -04002379 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002380 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002381 return container_of(so, struct nfs4_openowner, oo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383 return NULL;
2384}
2385
2386/* search file_hashtbl[] for file */
2387static struct nfs4_file *
2388find_file(struct inode *ino)
2389{
2390 unsigned int hashval = file_hashval(ino);
2391 struct nfs4_file *fp;
2392
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002393 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002395 if (fp->fi_inode == ino) {
2396 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002397 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002401 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return NULL;
2403}
2404
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002405static inline int access_valid(u32 x, u32 minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002406{
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002407 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002408 return 0;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002409 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2410 return 0;
2411 x &= ~NFS4_SHARE_ACCESS_MASK;
2412 if (minorversion && x) {
2413 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2414 return 0;
2415 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2416 return 0;
2417 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2418 }
2419 if (x)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002420 return 0;
2421 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002422}
2423
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002424static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002425{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002426 /* Note: unlike access bits, deny bits may be zero. */
2427 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002430/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 * Called to check deny when READ with all zero stateid or
2432 * WRITE with all zero or all one stateid
2433 */
Al Virob37ad282006-10-19 23:28:59 -07002434static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2436{
2437 struct inode *ino = current_fh->fh_dentry->d_inode;
2438 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002439 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002440 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 dprintk("NFSD: nfs4_share_conflict\n");
2443
2444 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002445 if (!fp)
2446 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002447 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002449 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2450 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2451 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2452 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
NeilBrown13cd2182005-06-23 22:03:10 -07002454 ret = nfs_ok;
2455out:
2456 put_nfs4_file(fp);
2457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002460static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /* We're assuming the state code never drops its reference
2463 * without first removing the lease. Since we're in this lease
2464 * callback (and since the lease code is serialized by the kernel
2465 * lock) we know the server hasn't removed the lease yet, we know
2466 * it's safe to take a reference: */
2467 atomic_inc(&dp->dl_count);
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Arnd Bergmann460781b2010-11-17 16:26:56 +01002471 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 dp->dl_time = get_seconds();
2473
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002474 nfsd4_cb_recall(dp);
2475}
2476
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002477/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002478static void nfsd_break_deleg_cb(struct file_lock *fl)
2479{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002480 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2481 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002482
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002483 BUG_ON(!fp);
2484 /* We assume break_lease is only called once per lease: */
2485 BUG_ON(fp->fi_had_conflict);
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002486 /*
2487 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002488 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002489 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002490 */
2491 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002493 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002494 fp->fi_had_conflict = true;
2495 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2496 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002497 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500static
2501int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2502{
2503 if (arg & F_UNLCK)
2504 return lease_modify(onlist, arg);
2505 else
2506 return -EAGAIN;
2507}
2508
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002509static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002510 .lm_break = nfsd_break_deleg_cb,
2511 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512};
2513
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002514static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2515{
2516 if (nfsd4_has_session(cstate))
2517 return nfs_ok;
2518 if (seqid == so->so_seqid - 1)
2519 return nfserr_replay_me;
2520 if (seqid == so->so_seqid)
2521 return nfs_ok;
2522 return nfserr_bad_seqid;
2523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Al Virob37ad282006-10-19 23:28:59 -07002525__be32
Andy Adamson66689582009-04-03 08:28:45 +03002526nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2527 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 clientid_t *clientid = &open->op_clientid;
2530 struct nfs4_client *clp = NULL;
2531 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002532 struct nfs4_openowner *oo = NULL;
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002533 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002536 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 if (STALE_CLIENTID(&open->op_clientid))
2539 return nfserr_stale_clientid;
2540
J. Bruce Fields506f2752011-08-11 18:50:18 -04002541 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002542 oo = find_openstateowner_str(strhashval, open);
2543 open->op_openowner = oo;
2544 if (!oo) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002545 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 clp = find_confirmed_client(clientid);
2547 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002548 return nfserr_expired;
2549 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002551 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002552 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002553 clp = oo->oo_owner.so_client;
2554 release_openowner(oo);
2555 open->op_openowner = NULL;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002556 goto renew;
2557 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002558 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002559 if (status)
2560 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561renew:
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002562 if (open->op_openowner == NULL) {
2563 oo = alloc_init_open_stateowner(strhashval, clp, open);
2564 if (oo == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002565 return nfserr_jukebox;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002566 open->op_openowner = oo;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002567 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002568 list_del_init(&oo->oo_close_lru);
2569 renew_client(oo->oo_owner.so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002570 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Al Virob37ad282006-10-19 23:28:59 -07002573static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002574nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2575{
2576 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2577 return nfserr_openmode;
2578 else
2579 return nfs_ok;
2580}
2581
Daniel Mackc47d8322011-05-16 16:38:14 +02002582static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002583{
2584 share_access &= ~NFS4_SHARE_WANT_MASK;
2585
2586 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2587}
2588
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002589static struct nfs4_delegation *find_deleg_stateid(stateid_t *s)
2590{
2591 struct nfs4_stid *ret;
2592
2593 ret = find_stateid_by_type(s, NFS4_DELEG_STID);
2594 if (!ret)
2595 return NULL;
2596 return delegstateid(ret);
2597}
2598
Al Virob37ad282006-10-19 23:28:59 -07002599static __be32
NeilBrown567d9822005-06-23 22:02:53 -07002600nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2601 struct nfs4_delegation **dp)
2602{
2603 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002604 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002605
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002606 *dp = find_deleg_stateid(&open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002607 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002608 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002609 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002610 status = nfs4_check_delegmode(*dp, flags);
2611 if (status)
2612 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002613out:
2614 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2615 return nfs_ok;
2616 if (status)
2617 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002618 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002619 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002620}
2621
Al Virob37ad282006-10-19 23:28:59 -07002622static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002623nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002625 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002626 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
NeilBrown8beefa22005-06-23 22:03:08 -07002628 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 /* ignore lock owners */
2630 if (local->st_stateowner->so_is_open_owner == 0)
2631 continue;
2632 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002633 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 *stpp = local;
2635 /* check for conflicting share reservations */
2636 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002637 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002639 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002642static inline struct nfs4_ol_stateid *
NeilBrown5ac049a2005-06-23 22:03:03 -07002643nfs4_alloc_stateid(void)
2644{
2645 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2646}
2647
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002648static inline int nfs4_access_to_access(u32 nfs4_access)
2649{
2650 int flags = 0;
2651
2652 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2653 flags |= NFSD_MAY_READ;
2654 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2655 flags |= NFSD_MAY_WRITE;
2656 return flags;
2657}
2658
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002659static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2660 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002661{
2662 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002663 int oflag = nfs4_access_to_omode(open->op_share_access);
2664 int access = nfs4_access_to_access(open->op_share_access);
2665
2666 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2667 * allowing it to break the lease and return EAGAIN leaves the
2668 * client unable to make progress in returning the delegation */
2669 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2670 access |= NFSD_MAY_NOT_BREAK_LEASE;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002671
2672 if (!fp->fi_fds[oflag]) {
2673 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2674 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002675 if (status)
2676 return status;
2677 }
2678 nfs4_file_get_access(fp, oflag);
2679
2680 return nfs_ok;
2681}
2682
Al Virob37ad282006-10-19 23:28:59 -07002683static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002684nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_ol_stateid **stpp,
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002685 struct nfs4_file *fp, struct svc_fh *cur_fh,
2686 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002688 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002689 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
NeilBrown5ac049a2005-06-23 22:03:03 -07002691 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (stp == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002693 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002695 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002696 if (status) {
2697 kmem_cache_free(stateid_slab, stp);
2698 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 *stpp = stp;
2701 return 0;
2702}
2703
Al Virob37ad282006-10-19 23:28:59 -07002704static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2706 struct nfsd4_open *open)
2707{
2708 struct iattr iattr = {
2709 .ia_valid = ATTR_SIZE,
2710 .ia_size = 0,
2711 };
2712 if (!open->op_truncate)
2713 return 0;
2714 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002715 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2717}
2718
Al Virob37ad282006-10-19 23:28:59 -07002719static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002720nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
J. Bruce Fields7d947842010-08-20 18:09:31 -04002722 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2723 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002724 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
J. Bruce Fields7d947842010-08-20 18:09:31 -04002726 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002727 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002728 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002729 if (status)
2730 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 status = nfsd4_truncate(rqstp, cur_fh, open);
2733 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002734 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002735 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002736 nfs4_file_put_access(fp, oflag);
2737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return status;
2739 }
2740 /* remember the open */
J. Bruce Fields24a01112010-05-18 20:01:35 -04002741 __set_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002742 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 return nfs_ok;
2745}
2746
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748static void
NeilBrown37515172005-07-07 17:59:16 -07002749nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002751 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002752 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002755/* Should we give out recallable state?: */
2756static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2757{
2758 if (clp->cl_cb_state == NFSD4_CB_UP)
2759 return true;
2760 /*
2761 * In the sessions case, since we don't have to establish a
2762 * separate connection for callbacks, we assume it's OK
2763 * until we hear otherwise:
2764 */
2765 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2766}
2767
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002768static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2769{
2770 struct file_lock *fl;
2771
2772 fl = locks_alloc_lock();
2773 if (!fl)
2774 return NULL;
2775 locks_init_lock(fl);
2776 fl->fl_lmops = &nfsd_lease_mng_ops;
2777 fl->fl_flags = FL_LEASE;
2778 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2779 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002780 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002781 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002782 return fl;
2783}
2784
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002785static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2786{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002787 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002788 struct file_lock *fl;
2789 int status;
2790
2791 fl = nfs4_alloc_init_lease(dp, flag);
2792 if (!fl)
2793 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002794 fl->fl_file = find_readable_file(fp);
2795 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2796 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002797 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002798 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002799 locks_free_lock(fl);
2800 return -ENOMEM;
2801 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002802 fp->fi_lease = fl;
2803 fp->fi_deleg_file = fl->fl_file;
2804 get_file(fp->fi_deleg_file);
2805 atomic_set(&fp->fi_delegees, 1);
2806 list_add(&dp->dl_perfile, &fp->fi_delegations);
2807 return 0;
2808}
2809
2810static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2811{
2812 struct nfs4_file *fp = dp->dl_file;
2813
2814 if (!fp->fi_lease)
2815 return nfs4_setlease(dp, flag);
2816 spin_lock(&recall_lock);
2817 if (fp->fi_had_conflict) {
2818 spin_unlock(&recall_lock);
2819 return -EAGAIN;
2820 }
2821 atomic_inc(&fp->fi_delegees);
2822 list_add(&dp->dl_perfile, &fp->fi_delegations);
2823 spin_unlock(&recall_lock);
2824 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002825 return 0;
2826}
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828/*
2829 * Attempt to hand out a delegation.
2830 */
2831static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002832nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
2834 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002835 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002836 int cb_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 int status, flag = 0;
2838
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002839 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002841 open->op_recall = 0;
2842 switch (open->op_claim_type) {
2843 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002844 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002845 open->op_recall = 1;
2846 flag = open->op_delegate_type;
2847 if (flag == NFS4_OPEN_DELEGATE_NONE)
2848 goto out;
2849 break;
2850 case NFS4_OPEN_CLAIM_NULL:
2851 /* Let's not give out any delegations till everyone's
2852 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002853 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002854 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002855 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07002856 goto out;
2857 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2858 flag = NFS4_OPEN_DELEGATE_WRITE;
2859 else
2860 flag = NFS4_OPEN_DELEGATE_READ;
2861 break;
2862 default:
2863 goto out;
2864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002866 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002867 if (dp == NULL)
2868 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002869 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002870 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002871 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002873 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002875 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002876 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877out:
NeilBrown7b190fe2005-06-23 22:03:23 -07002878 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2879 && flag == NFS4_OPEN_DELEGATE_NONE
2880 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002881 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 open->op_delegate_type = flag;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002883 return;
2884out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002885 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002886out_no_deleg:
2887 flag = NFS4_OPEN_DELEGATE_NONE;
2888 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
2890
2891/*
2892 * called with nfs4_lock_state() held.
2893 */
Al Virob37ad282006-10-19 23:28:59 -07002894__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2896{
Andy Adamson66689582009-04-03 08:28:45 +03002897 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 struct nfs4_file *fp = NULL;
2899 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002900 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07002901 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002902 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 status = nfserr_inval;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002905 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002906 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 goto out;
2908 /*
2909 * Lookup file; if found, lookup stateid and check open request,
2910 * and check for delegations in the process of being recalled.
2911 * If not found, create the nfs4_file struct
2912 */
2913 fp = find_file(ino);
2914 if (fp) {
2915 if ((status = nfs4_check_open(fp, open, &stp)))
2916 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002917 status = nfs4_check_deleg(fp, open, &dp);
2918 if (status)
2919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07002921 status = nfserr_bad_stateid;
2922 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2923 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04002924 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 fp = alloc_init_file(ino);
2926 if (fp == NULL)
2927 goto out;
2928 }
2929
2930 /*
2931 * OPEN the file, or upgrade an existing OPEN.
2932 * If truncate fails, the OPEN fails.
2933 */
2934 if (stp) {
2935 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002936 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (status)
2938 goto out;
2939 } else {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002940 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07002941 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 goto out;
J. Bruce Fields881ea2b2011-09-06 17:20:34 -04002943 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 status = nfsd4_truncate(rqstp, current_fh, open);
2945 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05002946 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 goto out;
2948 }
2949 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002950 update_stateid(&stp->st_stid.sc_stateid);
2951 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002953 if (nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002954 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03002955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 /*
2957 * Attempt to hand out a delegation. No error return, because the
2958 * OPEN succeeds even if we fail.
2959 */
2960 nfs4_open_delegation(current_fh, open, stp);
2961
2962 status = nfs_ok;
2963
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002964 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002965 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966out:
NeilBrown13cd2182005-06-23 22:03:10 -07002967 if (fp)
2968 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07002969 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2970 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 /*
2972 * To finish the open response, we just need to set the rflags.
2973 */
2974 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002975 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03002976 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2978
2979 return status;
2980}
2981
Al Virob37ad282006-10-19 23:28:59 -07002982__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002983nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2984 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07002987 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 nfs4_lock_state();
2990 dprintk("process_renew(%08x/%08x): starting\n",
2991 clid->cl_boot, clid->cl_id);
2992 status = nfserr_stale_clientid;
2993 if (STALE_CLIENTID(clid))
2994 goto out;
2995 clp = find_confirmed_client(clid);
2996 status = nfserr_expired;
2997 if (clp == NULL) {
2998 /* We assume the client took too long to RENEW. */
2999 dprintk("nfsd4_renew: clientid not found!\n");
3000 goto out;
3001 }
3002 renew_client(clp);
3003 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003004 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003005 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 goto out;
3007 status = nfs_ok;
3008out:
3009 nfs4_unlock_state();
3010 return status;
3011}
3012
Daniel Mackc47d8322011-05-16 16:38:14 +02003013static struct lock_manager nfsd4_manager = {
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003014};
3015
NeilBrowna76b4312005-06-23 22:04:01 -07003016static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003017nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07003018{
3019 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07003020 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003021 locks_end_grace(&nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003022 /*
3023 * Now that every NFSv4 client has had the chance to recover and
3024 * to see the (possibly new, possibly shorter) lease time, we
3025 * can safely set the next grace time to the current lease time:
3026 */
3027 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003028}
3029
NeilBrownfd39ca92005-06-23 22:04:03 -07003030static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031nfs4_laundromat(void)
3032{
3033 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003034 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 struct nfs4_delegation *dp;
3036 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003037 time_t cutoff = get_seconds() - nfsd4_lease;
3038 time_t t, clientid_val = nfsd4_lease;
3039 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041 nfs4_lock_state();
3042
3043 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003044 if (locks_in_grace())
3045 nfsd4_end_grace();
Benny Halevy36acb662010-05-12 00:13:04 +03003046 INIT_LIST_HEAD(&reaplist);
3047 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 list_for_each_safe(pos, next, &client_lru) {
3049 clp = list_entry(pos, struct nfs4_client, cl_lru);
3050 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3051 t = clp->cl_time - cutoff;
3052 if (clientid_val > t)
3053 clientid_val = t;
3054 break;
3055 }
Benny Halevyd7682982010-05-12 00:13:54 +03003056 if (atomic_read(&clp->cl_refcount)) {
3057 dprintk("NFSD: client in use (clientid %08x)\n",
3058 clp->cl_clientid.cl_id);
3059 continue;
3060 }
3061 unhash_client_locked(clp);
3062 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003063 }
3064 spin_unlock(&client_lock);
3065 list_for_each_safe(pos, next, &reaplist) {
3066 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 dprintk("NFSD: purging unused client (clientid %08x)\n",
3068 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07003069 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 expire_client(clp);
3071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 spin_lock(&recall_lock);
3073 list_for_each_safe(pos, next, &del_recall_lru) {
3074 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3075 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3076 u = dp->dl_time - cutoff;
3077 if (test_val > u)
3078 test_val = u;
3079 break;
3080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 list_move(&dp->dl_recall_lru, &reaplist);
3082 }
3083 spin_unlock(&recall_lock);
3084 list_for_each_safe(pos, next, &reaplist) {
3085 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3086 list_del_init(&dp->dl_recall_lru);
3087 unhash_delegation(dp);
3088 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003089 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 list_for_each_safe(pos, next, &close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003091 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3092 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3093 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (test_val > u)
3095 test_val = u;
3096 break;
3097 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003098 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
3100 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3101 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3102 nfs4_unlock_state();
3103 return clientid_val;
3104}
3105
Harvey Harrisona254b242008-02-20 12:49:00 -08003106static struct workqueue_struct *laundry_wq;
3107static void laundromat_main(struct work_struct *);
3108static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3109
3110static void
David Howellsc4028952006-11-22 14:57:56 +00003111laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
3113 time_t t;
3114
3115 t = nfs4_laundromat();
3116 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07003117 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003120static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003121{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003122 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3123 return nfserr_bad_stateid;
3124 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
3127static int
3128STALE_STATEID(stateid_t *stateid)
3129{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04003130 if (stateid->si_opaque.so_clid.cl_boot == boot_time)
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003131 return 0;
3132 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003133 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003134 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
3137static inline int
3138access_permit_read(unsigned long access_bmap)
3139{
3140 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3141 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3142 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3143}
3144
3145static inline int
3146access_permit_write(unsigned long access_bmap)
3147{
3148 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3149 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3150}
3151
3152static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003153__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154{
Al Virob37ad282006-10-19 23:28:59 -07003155 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
J. Bruce Fields02921912010-07-29 15:16:59 -04003157 /* For lock stateid's, we test the parent open, not the lock: */
3158 if (stp->st_openstp)
3159 stp = stp->st_openstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3161 goto out;
3162 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3163 goto out;
3164 status = nfs_ok;
3165out:
3166 return status;
3167}
3168
Al Virob37ad282006-10-19 23:28:59 -07003169static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3171{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003172 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003174 else if (locks_in_grace()) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003175 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 * conflicting state; so we must wait out the grace period. */
3177 return nfserr_grace;
3178 } else if (flags & WR_STATE)
3179 return nfs4_share_conflict(current_fh,
3180 NFS4_SHARE_DENY_WRITE);
3181 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3182 return nfs4_share_conflict(current_fh,
3183 NFS4_SHARE_DENY_READ);
3184}
3185
3186/*
3187 * Allow READ/WRITE during grace period on recovered state only for files
3188 * that are not able to provide mandatory locking.
3189 */
3190static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08003191grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003193 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194}
3195
J. Bruce Fields81b82962011-08-23 11:03:29 -04003196/* Returns true iff a is later than b: */
3197static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3198{
3199 return (s32)a->si_generation - (s32)b->si_generation > 0;
3200}
3201
J. Bruce Fields28dde242011-08-22 10:07:12 -04003202static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003203{
Andy Adamson66689582009-04-03 08:28:45 +03003204 /*
3205 * When sessions are used the stateid generation number is ignored
3206 * when it is zero.
3207 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003208 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003209 return nfs_ok;
3210
3211 if (in->si_generation == ref->si_generation)
3212 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003213
J. Bruce Fields0836f582008-01-26 19:08:12 -05003214 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003215 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003216 return nfserr_bad_stateid;
3217 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003218 * However, we could see a stateid from the past, even from a
3219 * non-buggy client. For example, if the client sends a lock
3220 * while some IO is outstanding, the lock may bump si_generation
3221 * while the IO is still in flight. The client could avoid that
3222 * situation by waiting for responses on all the IO requests,
3223 * but better performance may result in retrying IO that
3224 * receives an old_stateid error if requests are rarely
3225 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003226 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003227 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003228}
3229
J. Bruce Fields28dde242011-08-22 10:07:12 -04003230__be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
Bryan Schumaker17456802011-07-13 10:50:48 -04003231{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003232 struct nfs4_stid *s;
3233 struct nfs4_ol_stateid *ols;
3234 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003235
3236 if (STALE_STATEID(stateid))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003237 return nfserr_stale_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04003238
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003239 s = find_stateid(stateid);
3240 if (!s)
3241 return nfserr_stale_stateid;
3242 status = check_stateid_generation(stateid, &s->sc_stateid, has_session);
Bryan Schumaker17456802011-07-13 10:50:48 -04003243 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003244 return status;
3245 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3246 return nfs_ok;
3247 ols = openlockstateid(s);
3248 if (ols->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003249 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003250 return nfserr_bad_stateid;
3251 return nfs_ok;
Bryan Schumaker17456802011-07-13 10:50:48 -04003252}
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254/*
3255* Checks for stateid operations
3256*/
Al Virob37ad282006-10-19 23:28:59 -07003257__be32
Benny Halevydd453df2009-04-03 08:28:41 +03003258nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3259 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003261 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003262 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003264 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07003266 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 if (filpp)
3269 *filpp = NULL;
3270
J. Bruce Fields18f82732009-02-21 15:23:01 -08003271 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return nfserr_grace;
3273
3274 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3275 return check_special_stateids(current_fh, stateid, flags);
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 status = nfserr_stale_stateid;
3278 if (STALE_STATEID(stateid))
3279 goto out;
3280
J. Bruce Fields33515142010-10-02 18:42:39 -04003281 /*
3282 * We assume that any stateid that has the current boot time,
3283 * but that we can't find, is expired:
3284 */
3285 status = nfserr_expired;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003286 s = find_stateid(stateid);
3287 if (!s)
3288 goto out;
3289 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3290 if (status)
3291 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003292 switch (s->sc_type) {
3293 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003294 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003295 status = nfs4_check_delegmode(dp, flags);
3296 if (status)
3297 goto out;
3298 renew_client(dp->dl_client);
Dan Carpenter43b01782010-10-27 23:19:04 +02003299 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003300 *filpp = dp->dl_file->fi_deleg_file;
Dan Carpenter43b01782010-10-27 23:19:04 +02003301 BUG_ON(!*filpp);
3302 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003303 break;
3304 case NFS4_OPEN_STID:
3305 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003306 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003307 status = nfs4_check_fh(current_fh, stp);
3308 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003310 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003311 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003313 status = nfs4_check_openmode(stp, flags);
3314 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 goto out;
3316 renew_client(stp->st_stateowner->so_client);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003317 if (filpp) {
3318 if (flags & RD_STATE)
3319 *filpp = find_readable_file(stp->st_file);
3320 else
3321 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003322 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003323 break;
3324 default:
3325 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 }
3327 status = nfs_ok;
3328out:
3329 return status;
3330}
3331
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003332static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003333nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003334{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003335 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003336 return nfserr_locks_held;
3337 release_lock_stateid(stp);
3338 return nfs_ok;
3339}
3340
3341/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003342 * Test if the stateid is valid
3343 */
3344__be32
3345nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3346 struct nfsd4_test_stateid *test_stateid)
3347{
3348 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3349 return nfs_ok;
3350}
3351
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003352__be32
3353nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3354 struct nfsd4_free_stateid *free_stateid)
3355{
3356 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003357 struct nfs4_stid *s;
3358 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003359
3360 nfs4_lock_state();
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003361 s = find_stateid(stateid);
3362 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003363 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003364 switch (s->sc_type) {
3365 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003366 ret = nfserr_locks_held;
3367 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003368 case NFS4_OPEN_STID:
3369 case NFS4_LOCK_STID:
3370 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3371 if (ret)
3372 goto out;
3373 if (s->sc_type == NFS4_LOCK_STID)
3374 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3375 else
3376 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003377 break;
3378 default:
3379 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003380 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003381out:
3382 nfs4_unlock_state();
3383 return ret;
3384}
3385
NeilBrown4c4cd222005-07-07 17:59:27 -07003386static inline int
3387setlkflg (int type)
3388{
3389 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3390 RD_STATE : WR_STATE;
3391}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003393static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid)
3394{
3395 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3396 return nfserr_bad_stateid;
3397 if (STALE_STATEID(stateid))
3398 return nfserr_stale_stateid;
3399 return nfs_ok;
3400}
3401
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003402static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003403{
3404 struct svc_fh *current_fh = &cstate->current_fh;
3405 struct nfs4_stateowner *sop = stp->st_stateowner;
3406 __be32 status;
3407
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003408 status = nfsd4_check_seqid(cstate, sop, seqid);
3409 if (status)
3410 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003411 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3412 /*
3413 * "Closed" stateid's exist *only* to return
3414 * nfserr_replay_me from the previous step.
3415 */
3416 return nfserr_bad_stateid;
3417 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3418 if (status)
3419 return status;
3420 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003421}
3422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423/*
3424 * Checks for sequence id mutating operations.
3425 */
Al Virob37ad282006-10-19 23:28:59 -07003426static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003427nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003428 stateid_t *stateid, char typemask,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003429 struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003431 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003433 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3434 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 *stpp = NULL;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003437 status = nfs4_nospecial_stateid_checks(stateid);
3438 if (status)
3439 return status;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003440 *stpp = find_ol_stateid_by_type(stateid, typemask);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003441 if (*stpp == NULL)
3442 return nfserr_expired;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003443 cstate->replay_owner = (*stpp)->st_stateowner;
3444 renew_client((*stpp)->st_stateowner->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003446 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3447}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003448
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003449static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003450{
3451 __be32 status;
3452 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003454 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003455 NFS4_OPEN_STID, stpp);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003456 if (status)
3457 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003458 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003459 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003460 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003461 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Al Virob37ad282006-10-19 23:28:59 -07003464__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003465nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003466 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
Al Virob37ad282006-10-19 23:28:59 -07003468 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003469 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003470 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003473 (int)cstate->current_fh.fh_dentry->d_name.len,
3474 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003476 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003477 if (status)
3478 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
3480 nfs4_lock_state();
3481
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003482 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003483 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003484 NFS4_OPEN_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003485 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003486 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003487 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003488 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003489 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003490 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003491 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003492 update_stateid(&stp->st_stid.sc_stateid);
3493 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003494 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003495 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003496
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003497 nfsd4_create_clid_dir(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003498 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003500 if (!cstate->replay_owner)
3501 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return status;
3503}
3504
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003505static inline void nfs4_file_downgrade(struct nfs4_ol_stateid *stp, unsigned int to_access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506{
3507 int i;
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 for (i = 1; i < 4; i++) {
J. Bruce Fields3d02fa22011-09-19 15:07:41 -04003510 if (test_bit(i, &stp->st_access_bmap)
3511 && ((i & to_access) != i)) {
3512 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(i));
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003513 __clear_bit(i, &stp->st_access_bmap);
3514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
3516}
3517
3518static void
3519reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3520{
3521 int i;
3522 for (i = 0; i < 4; i++) {
3523 if ((i & deny) != i)
3524 __clear_bit(i, bmap);
3525 }
3526}
3527
Al Virob37ad282006-10-19 23:28:59 -07003528__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003529nfsd4_open_downgrade(struct svc_rqst *rqstp,
3530 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003531 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
Al Virob37ad282006-10-19 23:28:59 -07003533 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003534 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
3536 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003537 (int)cstate->current_fh.fh_dentry->d_name.len,
3538 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Andy Adamsond87a8ad2009-04-03 08:28:53 +03003540 if (!access_valid(od->od_share_access, cstate->minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07003541 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 return nfserr_inval;
3543
3544 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003545 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3546 &od->od_stateid, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003547 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 status = nfserr_inval;
3550 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3551 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3552 stp->st_access_bmap, od->od_share_access);
3553 goto out;
3554 }
3555 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3556 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3557 stp->st_deny_bmap, od->od_share_deny);
3558 goto out;
3559 }
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003560 nfs4_file_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3563
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003564 update_stateid(&stp->st_stid.sc_stateid);
3565 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 status = nfs_ok;
3567out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003568 if (!cstate->replay_owner)
3569 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return status;
3571}
3572
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003573void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3574{
3575 struct nfs4_openowner *oo;
3576 struct nfs4_ol_stateid *s;
3577
3578 if (!so->so_is_open_owner)
3579 return;
3580 oo = openowner(so);
3581 s = oo->oo_last_closed_stid;
3582 if (!s)
3583 return;
3584 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3585 /* Release the last_closed_stid on the next seqid bump: */
3586 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3587 return;
3588 }
3589 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003590 release_last_closed_stateid(oo);
3591}
3592
3593static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3594{
3595 unhash_open_stateid(s);
3596 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003597}
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599/*
3600 * nfs4_unlock_state() called after encode
3601 */
Al Virob37ad282006-10-19 23:28:59 -07003602__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003603nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003604 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Al Virob37ad282006-10-19 23:28:59 -07003606 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003607 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003608 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003611 (int)cstate->current_fh.fh_dentry->d_name.len,
3612 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003615 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3616 &close->cl_stateid,
3617 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3618 &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003619 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003621 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 status = nfs_ok;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003623 update_stateid(&stp->st_stid.sc_stateid);
3624 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003626 nfsd4_close_open_stateid(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003627 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003628
3629 /* place unused nfs4_stateowners on so_close_lru list to be
3630 * released by the laundromat service after the lease period
3631 * to enable us to handle CLOSE replay
3632 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003633 if (list_empty(&oo->oo_owner.so_stateids))
3634 move_to_close_lru(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003636 if (!cstate->replay_owner)
3637 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return status;
3639}
3640
Al Virob37ad282006-10-19 23:28:59 -07003641__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003642nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3643 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003645 struct nfs4_delegation *dp;
3646 stateid_t *stateid = &dr->dr_stateid;
3647 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07003648 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003650 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003651 return status;
3652 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
3654 nfs4_lock_state();
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003655 status = nfserr_bad_stateid;
3656 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3657 goto out;
3658 status = nfserr_stale_stateid;
3659 if (STALE_STATEID(stateid))
3660 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003661 status = nfserr_expired;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003662 dp = find_deleg_stateid(stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003663 if (!dp)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003664 goto out;
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003665 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003666 if (status)
3667 goto out;
3668 renew_client(dp->dl_client);
3669
3670 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003672 nfs4_unlock_state();
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return status;
3675}
3676
3677
3678/*
3679 * Lock owner state (byte-range locks)
3680 */
3681#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3682#define LOCK_HASH_BITS 8
3683#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3684#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3685
Benny Halevy87df4de2008-12-15 19:42:03 +02003686static inline u64
3687end_offset(u64 start, u64 len)
3688{
3689 u64 end;
3690
3691 end = start + len;
3692 return end >= start ? end: NFS4_MAX_UINT64;
3693}
3694
3695/* last octet in a range */
3696static inline u64
3697last_byte_offset(u64 start, u64 len)
3698{
3699 u64 end;
3700
3701 BUG_ON(!len);
3702 end = start + len;
3703 return end > start ? end - 1: NFS4_MAX_UINT64;
3704}
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706static inline unsigned int
3707lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3708 struct xdr_netobj *ownername)
3709{
3710 return (file_hashval(inode) + cl_id
3711 + opaque_hashval(ownername->data, ownername->len))
3712 & LOCK_HASH_MASK;
3713}
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717/*
3718 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3719 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3720 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3721 * locking, this prevents us from being completely protocol-compliant. The
3722 * real solution to this problem is to start using unsigned file offsets in
3723 * the VFS, but this is a very deep change!
3724 */
3725static inline void
3726nfs4_transform_lock_offset(struct file_lock *lock)
3727{
3728 if (lock->fl_start < 0)
3729 lock->fl_start = OFFSET_MAX;
3730 if (lock->fl_end < 0)
3731 lock->fl_end = OFFSET_MAX;
3732}
3733
NeilBrownd5b90262006-04-10 22:55:22 -07003734/* Hack!: For now, we're defining this just so we can use a pointer to it
3735 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003736static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003737};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
3739static inline void
3740nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3741{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003742 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
NeilBrownd5b90262006-04-10 22:55:22 -07003744 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003745 lo = (struct nfs4_lockowner *) fl->fl_owner;
3746 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3747 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003748 if (!deny->ld_owner.data)
3749 /* We just don't care that much */
3750 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003751 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3752 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003753 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003754nevermind:
3755 deny->ld_owner.len = 0;
3756 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003757 deny->ld_clientid.cl_boot = 0;
3758 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 }
3760 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003761 deny->ld_length = NFS4_MAX_UINT64;
3762 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3764 deny->ld_type = NFS4_READ_LT;
3765 if (fl->fl_type != F_RDLCK)
3766 deny->ld_type = NFS4_WRITE_LT;
3767}
3768
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003769static struct nfs4_lockowner *
3770find_lockowner_str(struct inode *inode, clientid_t *clid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 struct xdr_netobj *owner)
3772{
3773 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3774 struct nfs4_stateowner *op;
3775
3776 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003777 if (same_owner_str(op, owner, clid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003778 return lockowner(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 }
3780 return NULL;
3781}
3782
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003783static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003784{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003785 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3786 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003787}
3788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789/*
3790 * Alloc a lock owner structure.
3791 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003792 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 *
3794 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 */
3796
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003797static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003798alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003799 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003801 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3802 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003804 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3805 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07003806 /* It is the openowner seqid that will be incremented in encode in the
3807 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003808 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3809 hash_lockowner(lo, strhashval, clp, open_stp);
3810 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811}
3812
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003813static struct nfs4_ol_stateid *
3814alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003816 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04003817 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
NeilBrown5ac049a2005-06-23 22:03:03 -07003819 stp = nfs4_alloc_stateid();
3820 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 goto out;
NeilBrown8beefa22005-06-23 22:03:08 -07003822 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003823 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3824 stp->st_stateowner = &lo->lo_owner;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003825 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown13cd2182005-06-23 22:03:10 -07003826 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 stp->st_file = fp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04003828 stp->st_stid.sc_stateid.si_opaque.so_clid = clp->cl_clientid;
3829 stp->st_stid.sc_stateid.si_opaque.so_id = current_stateid++;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04003830 /* note will be incremented before first return to client: */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003831 stp->st_stid.sc_stateid.si_generation = 0;
J. Bruce Fields36d44c62011-09-08 12:16:03 -04003832 hash_stid(&stp->st_stid);
J. Bruce Fields0997b172011-03-02 18:01:35 -05003833 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003835 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
3837out:
3838 return stp;
3839}
3840
NeilBrownfd39ca92005-06-23 22:04:03 -07003841static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842check_lock_length(u64 offset, u64 length)
3843{
Benny Halevy87df4de2008-12-15 19:42:03 +02003844 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 LOFF_OVERFLOW(offset, length)));
3846}
3847
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003848static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05003849{
3850 struct nfs4_file *fp = lock_stp->st_file;
3851 int oflag = nfs4_access_to_omode(access);
3852
3853 if (test_bit(access, &lock_stp->st_access_bmap))
3854 return;
3855 nfs4_file_get_access(fp, oflag);
3856 __set_bit(access, &lock_stp->st_access_bmap);
3857}
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859/*
3860 * LOCK operation
3861 */
Al Virob37ad282006-10-19 23:28:59 -07003862__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003863nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003864 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003866 struct nfs4_openowner *open_sop = NULL;
3867 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003868 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04003869 struct nfs4_file *fp;
3870 struct file *filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05003872 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07003873 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 unsigned int strhashval;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003875 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07003876 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3879 (long long) lock->lk_offset,
3880 (long long) lock->lk_length);
3881
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 if (check_lock_length(lock->lk_offset, lock->lk_length))
3883 return nfserr_inval;
3884
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003885 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02003886 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08003887 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3888 return status;
3889 }
3890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 nfs4_lock_state();
3892
3893 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07003894 /*
3895 * Client indicates that this is a new lockowner.
3896 * Use open owner and open stateid to create lock owner and
3897 * lock stateid.
3898 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003899 struct nfs4_ol_stateid *open_stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
3901 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03003902 if (!nfsd4_has_session(cstate) &&
3903 STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003907 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 lock->lk_new_open_seqid,
3909 &lock->lk_new_open_stateid,
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003910 &open_stp);
NeilBrown37515172005-07-07 17:59:16 -07003911 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003913 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003914 status = nfserr_bad_stateid;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003915 if (!nfsd4_has_session(cstate) &&
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003916 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003917 &lock->v.new.clientid))
3918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 /* create lockowner and lock stateid */
3920 fp = open_stp->st_file;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003921 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3922 open_sop->oo_owner.so_client->cl_clientid.cl_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003924 /* XXX: Do we need to check for duplicate stateowners on
3925 * the same file, or should they just be allowed (and
3926 * create new stateids)? */
J. Bruce Fields3e772462011-08-10 19:07:33 -04003927 status = nfserr_jukebox;
Neil Brownb59e3c02005-09-13 01:25:38 -07003928 lock_sop = alloc_init_lock_stateowner(strhashval,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003929 open_sop->oo_owner.so_client, open_stp, lock);
Neil Brownb59e3c02005-09-13 01:25:38 -07003930 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07003932 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08003933 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 } else {
3936 /* lock (lock owner + lock stateid) already exists */
Benny Halevydd453df2009-04-03 08:28:41 +03003937 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003938 lock->lk_old_lock_seqid,
3939 &lock->lk_old_lock_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003940 NFS4_LOCK_STID, &lock_stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 if (status)
3942 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003943 lock_sop = lockowner(lock_stp->st_stateowner);
J. Bruce Fields7d947842010-08-20 18:09:31 -04003944 fp = lock_stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003946 /* lock_sop and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003948 lkflg = setlkflg(lock->lk_type);
3949 status = nfs4_check_openmode(lock_stp, lkflg);
3950 if (status)
3951 goto out;
3952
NeilBrown0dd395d2005-07-07 17:59:15 -07003953 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003954 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07003955 goto out;
3956 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003957 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07003958 goto out;
3959
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 locks_init_lock(&file_lock);
3961 switch (lock->lk_type) {
3962 case NFS4_READ_LT:
3963 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05003964 filp = find_readable_file(lock_stp->st_file);
3965 if (filp)
3966 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 file_lock.fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05003968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 case NFS4_WRITE_LT:
3970 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05003971 filp = find_writeable_file(lock_stp->st_file);
3972 if (filp)
3973 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 file_lock.fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05003975 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 default:
3977 status = nfserr_inval;
3978 goto out;
3979 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003980 if (!filp) {
3981 status = nfserr_openmode;
3982 goto out;
3983 }
Neil Brownb59e3c02005-09-13 01:25:38 -07003984 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 file_lock.fl_pid = current->tgid;
3986 file_lock.fl_file = filp;
3987 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07003988 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
3990 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02003991 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 nfs4_transform_lock_offset(&file_lock);
3993
3994 /*
3995 * Try to lock the file in the VFS.
3996 * Note: locks.c uses the BKL to protect the inode's lock list.
3997 */
3998
J. Bruce Fields529d7b22011-03-02 23:48:33 -05003999 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004000 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004002 update_stateid(&lock_stp->st_stid.sc_stateid);
4003 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004005 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004006 break;
4007 case (EAGAIN): /* conflock holds conflicting lock */
4008 status = nfserr_denied;
4009 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4010 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4011 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 case (EDEADLK):
4013 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004014 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004015 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004016 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004017 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08004021 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004022 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004023 if (!cstate->replay_owner)
4024 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 return status;
4026}
4027
4028/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004029 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4030 * so we do a temporary open here just to get an open file to pass to
4031 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4032 * inode operation.)
4033 */
4034static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4035{
4036 struct file *file;
4037 int err;
4038
4039 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4040 if (err)
4041 return err;
4042 err = vfs_test_lock(file, lock);
4043 nfsd_close(file);
4044 return err;
4045}
4046
4047/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 * LOCKT operation
4049 */
Al Virob37ad282006-10-19 23:28:59 -07004050__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004051nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4052 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053{
4054 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 struct file_lock file_lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004056 struct nfs4_lockowner *lo;
Marc Eshelfd85b812006-11-28 16:26:41 -05004057 int error;
Al Virob37ad282006-10-19 23:28:59 -07004058 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004060 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return nfserr_grace;
4062
4063 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4064 return nfserr_inval;
4065
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 nfs4_lock_state();
4067
4068 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03004069 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004072 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004075 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 locks_init_lock(&file_lock);
4077 switch (lockt->lt_type) {
4078 case NFS4_READ_LT:
4079 case NFS4_READW_LT:
4080 file_lock.fl_type = F_RDLCK;
4081 break;
4082 case NFS4_WRITE_LT:
4083 case NFS4_WRITEW_LT:
4084 file_lock.fl_type = F_WRLCK;
4085 break;
4086 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004087 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 status = nfserr_inval;
4089 goto out;
4090 }
4091
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004092 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4093 if (lo)
4094 file_lock.fl_owner = (fl_owner_t)lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 file_lock.fl_pid = current->tgid;
4096 file_lock.fl_flags = FL_POSIX;
4097
4098 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004099 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
4101 nfs4_transform_lock_offset(&file_lock);
4102
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004104 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05004105 if (error) {
4106 status = nfserrno(error);
4107 goto out;
4108 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004109 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004111 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 }
4113out:
4114 nfs4_unlock_state();
4115 return status;
4116}
4117
Al Virob37ad282006-10-19 23:28:59 -07004118__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004119nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004120 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004122 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 struct file *filp = NULL;
4124 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07004125 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004126 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
4128 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4129 (long long) locku->lu_offset,
4130 (long long) locku->lu_length);
4131
4132 if (check_lock_length(locku->lu_offset, locku->lu_length))
4133 return nfserr_inval;
4134
4135 nfs4_lock_state();
4136
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004137 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004138 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004139 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004141 filp = find_any_file(stp->st_file);
4142 if (!filp) {
4143 status = nfserr_lock_range;
4144 goto out;
4145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 BUG_ON(!filp);
4147 locks_init_lock(&file_lock);
4148 file_lock.fl_type = F_UNLCK;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004149 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 file_lock.fl_pid = current->tgid;
4151 file_lock.fl_file = filp;
4152 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004153 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 file_lock.fl_start = locku->lu_offset;
4155
Benny Halevy87df4de2008-12-15 19:42:03 +02004156 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 nfs4_transform_lock_offset(&file_lock);
4158
4159 /*
4160 * Try to unlock the file in the VFS.
4161 */
Marc Eshelfd85b812006-11-28 16:26:41 -05004162 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004163 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004164 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 goto out_nfserr;
4166 }
4167 /*
4168 * OK, unlock succeeded; the only thing left to do is update the stateid.
4169 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004170 update_stateid(&stp->st_stid.sc_stateid);
4171 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
4173out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 nfs4_unlock_state();
4175 return status;
4176
4177out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004178 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 goto out;
4180}
4181
4182/*
4183 * returns
4184 * 1: locks held by lockowner
4185 * 0: no locks held by lockowner
4186 */
4187static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004188check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189{
4190 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004191 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 int status = 0;
4193
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004194 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004196 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 status = 1;
4198 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 }
4201out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004202 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 return status;
4204}
4205
Al Virob37ad282006-10-19 23:28:59 -07004206__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004207nfsd4_release_lockowner(struct svc_rqst *rqstp,
4208 struct nfsd4_compound_state *cstate,
4209 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210{
4211 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004212 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004213 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004214 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004216 struct list_head matches;
4217 int i;
Al Virob37ad282006-10-19 23:28:59 -07004218 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
4220 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4221 clid->cl_boot, clid->cl_id);
4222
4223 /* XXX check for lease expiration */
4224
4225 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07004226 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
4229 nfs4_lock_state();
4230
NeilBrown3e9e3db2005-06-23 22:04:20 -07004231 status = nfserr_locks_held;
4232 /* XXX: we're doing a linear search through all the lockowners.
4233 * Yipes! For now we'll just hope clients aren't really using
4234 * release_lockowner much, but eventually we have to fix these
4235 * data structures. */
4236 INIT_LIST_HEAD(&matches);
4237 for (i = 0; i < LOCK_HASH_SIZE; i++) {
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004238 list_for_each_entry(sop, &lock_ownerstr_hashtbl[i], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04004239 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004240 continue;
4241 list_for_each_entry(stp, &sop->so_stateids,
4242 st_perstateowner) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004243 lo = lockowner(sop);
4244 if (check_for_locks(stp->st_file, lo))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004245 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004246 list_add(&lo->lo_list, &matches);
NeilBrown3e9e3db2005-06-23 22:04:20 -07004247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004249 }
4250 /* Clients probably won't expect us to return with some (but not all)
4251 * of the lockowner state released; so don't release any until all
4252 * have been checked. */
4253 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004254 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004255 lo = list_entry(matches.next, struct nfs4_lockowner,
4256 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004257 /* unhash_stateowner deletes so_perclient only
4258 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004259 list_del(&lo->lo_list);
4260 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 }
4262out:
4263 nfs4_unlock_state();
4264 return status;
4265}
4266
4267static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004268alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
NeilBrowna55370a2005-06-23 22:03:52 -07004270 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271}
4272
NeilBrownc7b9a452005-06-23 22:04:30 -07004273int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03004274nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07004275{
4276 unsigned int strhashval = clientstr_hashval(name);
4277 struct nfs4_client *clp;
4278
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004279 clp = find_confirmed_client_by_str(name, strhashval);
NeilBrownc7b9a452005-06-23 22:04:30 -07004280 return clp ? 1 : 0;
4281}
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283/*
4284 * failure => all reset bets are off, nfserr_no_grace...
4285 */
NeilBrown190e4fb2005-06-23 22:04:25 -07004286int
4287nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
4289 unsigned int strhashval;
4290 struct nfs4_client_reclaim *crp = NULL;
4291
NeilBrowna55370a2005-06-23 22:03:52 -07004292 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4293 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 if (!crp)
4295 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07004296 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 INIT_LIST_HEAD(&crp->cr_strhash);
4298 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07004299 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 reclaim_str_hashtbl_size++;
4301 return 1;
4302}
4303
4304static void
4305nfs4_release_reclaim(void)
4306{
4307 struct nfs4_client_reclaim *crp = NULL;
4308 int i;
4309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4311 while (!list_empty(&reclaim_str_hashtbl[i])) {
4312 crp = list_entry(reclaim_str_hashtbl[i].next,
4313 struct nfs4_client_reclaim, cr_strhash);
4314 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 kfree(crp);
4316 reclaim_str_hashtbl_size--;
4317 }
4318 }
4319 BUG_ON(reclaim_str_hashtbl_size);
4320}
4321
4322/*
4323 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07004324static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325nfs4_find_reclaim_client(clientid_t *clid)
4326{
4327 unsigned int strhashval;
4328 struct nfs4_client *clp;
4329 struct nfs4_client_reclaim *crp = NULL;
4330
4331
4332 /* find clientid in conf_id_hashtbl */
4333 clp = find_confirmed_client(clid);
4334 if (clp == NULL)
4335 return NULL;
4336
NeilBrowna55370a2005-06-23 22:03:52 -07004337 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4338 clp->cl_name.len, clp->cl_name.data,
4339 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
4341 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07004342 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07004344 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 return crp;
4346 }
4347 }
4348 return NULL;
4349}
4350
4351/*
4352* Called from OPEN. Look for clientid in reclaim list.
4353*/
Al Virob37ad282006-10-19 23:28:59 -07004354__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355nfs4_check_open_reclaim(clientid_t *clid)
4356{
NeilBrowndfc83562005-06-23 22:03:16 -07004357 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
NeilBrownac4d8ff22005-06-23 22:03:30 -07004360/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004362int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004363nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004365 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004367 status = nfsd4_init_slabs();
4368 if (status)
4369 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4371 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4372 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4373 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4374 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
Wang Chen02cb2852009-04-24 15:41:57 +08004375 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 }
Marc Eshel5282fd72009-04-03 08:27:52 +03004377 for (i = 0; i < SESSION_HASH_SIZE; i++)
4378 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 for (i = 0; i < FILE_HASH_SIZE; i++) {
4380 INIT_LIST_HEAD(&file_hashtbl[i]);
4381 }
J. Bruce Fields506f2752011-08-11 18:50:18 -04004382 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4383 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 }
J. Bruce Fieldsb79abad2011-08-22 18:01:43 -04004385 for (i = 0; i < STATEID_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 INIT_LIST_HEAD(&stateid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 for (i = 0; i < LOCK_HASH_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 INIT_LIST_HEAD(&close_lru);
4392 INIT_LIST_HEAD(&client_lru);
4393 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004394 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004395 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07004396}
4397
NeilBrown190e4fb2005-06-23 22:04:25 -07004398static void
4399nfsd4_load_reboot_recovery_data(void)
4400{
4401 int status;
4402
NeilBrown0964a3d2005-06-23 22:04:32 -07004403 nfs4_lock_state();
J. Bruce Fields48483bf2011-08-26 20:40:28 -04004404 nfsd4_init_recdir();
NeilBrown190e4fb2005-06-23 22:04:25 -07004405 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07004406 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07004407 if (status)
4408 printk("NFSD: Failure reading reboot recovery data\n");
4409}
4410
Meelap Shahc2f1a552007-07-17 04:04:39 -07004411/*
4412 * Since the lifetime of a delegation isn't limited to that of an open, a
4413 * client may quite reasonably hang on to a delegation as long as it has
4414 * the inode cached. This becomes an obvious problem the first time a
4415 * client's inode cache approaches the size of the server's total memory.
4416 *
4417 * For now we avoid this problem by imposing a hard limit on the number
4418 * of delegations, which varies according to the server's memory size.
4419 */
4420static void
4421set_max_delegations(void)
4422{
4423 /*
4424 * Allow at most 4 delegations per megabyte of RAM. Quick
4425 * estimates suggest that in the worst case (where every delegation
4426 * is for a different inode), a delegation could take about 1.5K,
4427 * giving a worst case usage of about 6% of memory.
4428 */
4429 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4430}
4431
NeilBrownac4d8ff22005-06-23 22:03:30 -07004432/* initialization to perform when the nfsd service is started: */
4433
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004434static int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004435__nfs4_state_start(void)
4436{
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004437 int ret;
4438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004440 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07004441 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004442 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004443 ret = set_callback_cred();
4444 if (ret)
4445 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004446 laundry_wq = create_singlethread_workqueue("nfsd4");
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004447 if (laundry_wq == NULL)
4448 return -ENOMEM;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004449 ret = nfsd4_create_callback_queue();
4450 if (ret)
4451 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004452 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004453 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004454 return 0;
4455out_free_laundry:
4456 destroy_workqueue(laundry_wq);
4457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458}
4459
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004460int
NeilBrown76a35502005-06-23 22:03:26 -07004461nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462{
NeilBrown190e4fb2005-06-23 22:04:25 -07004463 nfsd4_load_reboot_recovery_data();
Jeff Layton4ad9a342010-07-19 16:50:04 -04004464 return __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465}
4466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467static void
4468__nfs4_state_shutdown(void)
4469{
4470 int i;
4471 struct nfs4_client *clp = NULL;
4472 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 struct list_head *pos, *next, reaplist;
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4476 while (!list_empty(&conf_id_hashtbl[i])) {
4477 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4478 expire_client(clp);
4479 }
4480 while (!list_empty(&unconf_str_hashtbl[i])) {
4481 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4482 expire_client(clp);
4483 }
4484 }
4485 INIT_LIST_HEAD(&reaplist);
4486 spin_lock(&recall_lock);
4487 list_for_each_safe(pos, next, &del_recall_lru) {
4488 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4489 list_move(&dp->dl_recall_lru, &reaplist);
4490 }
4491 spin_unlock(&recall_lock);
4492 list_for_each_safe(pos, next, &reaplist) {
4493 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4494 list_del_init(&dp->dl_recall_lru);
4495 unhash_delegation(dp);
4496 }
4497
NeilBrown190e4fb2005-06-23 22:04:25 -07004498 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499}
4500
4501void
4502nfs4_state_shutdown(void)
4503{
Tejun Heoafe2c512010-12-14 16:21:17 +01004504 cancel_delayed_work_sync(&laundromat_work);
NeilBrown5e8d5c22006-04-10 22:55:37 -07004505 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06004506 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 nfs4_lock_state();
4508 nfs4_release_reclaim();
4509 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 nfs4_unlock_state();
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004511 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512}