blob: a174841b262e8f910ed96304158627a0ebe39d29 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static u32 current_ownerid = 1;
53static u32 current_fileid = 1;
54static u32 current_delegid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070055static stateid_t zerostateid; /* bits all 0 */
56static stateid_t onestateid; /* bits all 1 */
Andy Adamsonec6b5d72009-04-03 08:28:28 +030057static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070058
59#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
60#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040063static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
J. Bruce Fields8b671b82009-02-22 14:51:34 -080065/* Locking: */
66
67/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080068static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
J. Bruce Fields8b671b82009-02-22 14:51:34 -080070/*
71 * Currently used for the del_recall_lru and file hash table. In an
72 * effort to decrease the scope of the client_mutex, this spinlock may
73 * eventually cover more:
74 */
75static DEFINE_SPINLOCK(recall_lock);
76
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040077static struct kmem_cache *openowner_slab = NULL;
78static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080079static struct kmem_cache *file_slab = NULL;
80static struct kmem_cache *stateid_slab = NULL;
81static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083void
84nfs4_lock_state(void)
85{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080086 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89void
90nfs4_unlock_state(void)
91{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080092 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95static inline u32
96opaque_hashval(const void *ptr, int nbytes)
97{
98 unsigned char *cptr = (unsigned char *) ptr;
99
100 u32 x = 0;
101 while (nbytes--) {
102 x *= 37;
103 x += *cptr++;
104 }
105 return x;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static struct list_head del_recall_lru;
109
NeilBrown13cd2182005-06-23 22:03:10 -0700110static inline void
111put_nfs4_file(struct nfs4_file *fi)
112{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800113 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
114 list_del(&fi->fi_hash);
115 spin_unlock(&recall_lock);
116 iput(fi->fi_inode);
117 kmem_cache_free(file_slab, fi);
118 }
NeilBrown13cd2182005-06-23 22:03:10 -0700119}
120
121static inline void
122get_nfs4_file(struct nfs4_file *fi)
123{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800124 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700125}
126
NeilBrownef0f3392006-04-10 22:55:41 -0700127static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700128unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700129
130/*
131 * Open owner state (share locks)
132 */
133
J. Bruce Fields506f2752011-08-11 18:50:18 -0400134/* hash tables for open owners */
135#define OPEN_OWNER_HASH_BITS 8
136#define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS)
137#define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700138
J. Bruce Fields506f2752011-08-11 18:50:18 -0400139static unsigned int open_ownerid_hashval(const u32 id)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400140{
J. Bruce Fields506f2752011-08-11 18:50:18 -0400141 return id & OPEN_OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400142}
143
J. Bruce Fields506f2752011-08-11 18:50:18 -0400144static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400145{
146 unsigned int ret;
147
148 ret = opaque_hashval(ownername->data, ownername->len);
149 ret += clientid;
J. Bruce Fields506f2752011-08-11 18:50:18 -0400150 return ret & OPEN_OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400151}
NeilBrownef0f3392006-04-10 22:55:41 -0700152
J. Bruce Fields506f2752011-08-11 18:50:18 -0400153static struct list_head open_ownerid_hashtbl[OPEN_OWNER_HASH_SIZE];
154static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700155
156/* hash table for nfs4_file */
157#define FILE_HASH_BITS 8
158#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800159
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400160/* hash table for (open)nfs4_ol_stateid */
NeilBrownef0f3392006-04-10 22:55:41 -0700161#define STATEID_HASH_BITS 10
162#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
163#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
164
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400165static unsigned int file_hashval(struct inode *ino)
166{
167 /* XXX: why are we hashing on inode pointer, anyway? */
168 return hash_ptr(ino, FILE_HASH_BITS);
169}
170
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400171static unsigned int stateid_hashval(stateid_t *s)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400172{
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400173 return opaque_hashval(&s->si_opaque, sizeof(stateid_opaque_t)) & STATEID_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400174}
NeilBrownef0f3392006-04-10 22:55:41 -0700175
176static struct list_head file_hashtbl[FILE_HASH_SIZE];
177static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
178
J. Bruce Fields998db522010-08-07 09:21:41 -0400179static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400180{
181 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
182 atomic_inc(&fp->fi_access[oflag]);
183}
184
J. Bruce Fields998db522010-08-07 09:21:41 -0400185static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
186{
187 if (oflag == O_RDWR) {
188 __nfs4_file_get_access(fp, O_RDONLY);
189 __nfs4_file_get_access(fp, O_WRONLY);
190 } else
191 __nfs4_file_get_access(fp, oflag);
192}
193
194static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400195{
196 if (fp->fi_fds[oflag]) {
197 fput(fp->fi_fds[oflag]);
198 fp->fi_fds[oflag] = NULL;
199 }
200}
201
J. Bruce Fields998db522010-08-07 09:21:41 -0400202static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400203{
204 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
205 nfs4_file_put_fd(fp, O_RDWR);
206 nfs4_file_put_fd(fp, oflag);
207 }
208}
209
J. Bruce Fields998db522010-08-07 09:21:41 -0400210static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
211{
212 if (oflag == O_RDWR) {
213 __nfs4_file_put_access(fp, O_RDONLY);
214 __nfs4_file_put_access(fp, O_WRONLY);
215 } else
216 __nfs4_file_put_access(fp, oflag);
217}
218
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400219static inline void hash_stid(struct nfs4_stid *stid)
220{
221 stateid_t *s = &stid->sc_stateid;
222 unsigned int hashval;
223
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400224 hashval = stateid_hashval(s);
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400225 list_add(&stid->sc_hash, &stateid_hashtbl[hashval]);
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400229alloc_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 -0700230{
231 struct nfs4_delegation *dp;
232 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400235 /*
236 * Major work on the lease subsystem (for example, to support
237 * calbacks on stat) will be required before we can support
238 * write delegations properly.
239 */
240 if (type != NFS4_OPEN_DELEGATE_READ)
241 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700242 if (fp->fi_had_conflict)
243 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700244 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700245 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700246 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
247 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700249 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700250 INIT_LIST_HEAD(&dp->dl_perfile);
251 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 INIT_LIST_HEAD(&dp->dl_recall_lru);
253 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700254 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 dp->dl_type = type;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -0400257 dp->dl_stid.sc_type = NFS4_DELEG_STID;
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -0400258 dp->dl_stid.sc_stateid.si_boot = boot_time;
259 dp->dl_stid.sc_stateid.si_stateownerid = current_delegid++;
260 dp->dl_stid.sc_stateid.si_fileid = 0;
261 dp->dl_stid.sc_stateid.si_generation = 1;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -0400262 hash_stid(&dp->dl_stid);
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500263 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 dp->dl_time = 0;
265 atomic_set(&dp->dl_count, 1);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500266 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return dp;
268}
269
270void
271nfs4_put_delegation(struct nfs4_delegation *dp)
272{
273 if (atomic_dec_and_test(&dp->dl_count)) {
274 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700275 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700276 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700277 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279}
280
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500281static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500283 if (atomic_dec_and_test(&fp->fi_delegees)) {
284 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
285 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400286 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500287 fp->fi_deleg_file = NULL;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/* Called under the state lock. */
292static void
293unhash_delegation(struct nfs4_delegation *dp)
294{
J. Bruce Fieldsf459e452011-09-09 09:06:12 -0400295 list_del_init(&dp->dl_stid.sc_hash);
NeilBrownea1da632005-06-23 22:04:17 -0700296 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500298 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 list_del_init(&dp->dl_recall_lru);
300 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500301 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 nfs4_put_delegation(dp);
303}
304
305/*
306 * SETCLIENTID state
307 */
308
Benny Halevy36acb662010-05-12 00:13:04 +0300309/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300310static DEFINE_SPINLOCK(client_lock);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/* Hash tables for nfs4_clientid state */
313#define CLIENT_HASH_BITS 4
314#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
315#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
316
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400317static unsigned int clientid_hashval(u32 id)
318{
319 return id & CLIENT_HASH_MASK;
320}
321
322static unsigned int clientstr_hashval(const char *name)
323{
324 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
328 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
329 * used in reboot/reset lease grace period processing
330 *
331 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
332 * setclientid_confirmed info.
333 *
334 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
335 * setclientid info.
336 *
337 * client_lru holds client queue ordered by nfs4_client.cl_time
338 * for lease renewal.
339 *
340 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
341 * for last close replay.
342 */
343static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
344static int reclaim_str_hashtbl_size = 0;
345static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
346static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
347static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
348static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
349static struct list_head client_lru;
350static struct list_head close_lru;
351
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400352/*
353 * We store the NONE, READ, WRITE, and BOTH bits separately in the
354 * st_{access,deny}_bmap field of the stateid, in order to track not
355 * only what share bits are currently in force, but also what
356 * combinations of share bits previous opens have used. This allows us
357 * to enforce the recommendation of rfc 3530 14.2.19 that the server
358 * return an error if the client attempt to downgrade to a combination
359 * of share bits not explicable by closing some of its previous opens.
360 *
361 * XXX: This enforcement is actually incomplete, since we don't keep
362 * track of access/deny bit combinations; so, e.g., we allow:
363 *
364 * OPEN allow read, deny write
365 * OPEN allow both, deny none
366 * DOWNGRADE allow read, deny none
367 *
368 * which we should reject.
369 */
370static void
371set_access(unsigned int *access, unsigned long bmap) {
372 int i;
373
374 *access = 0;
375 for (i = 1; i < 4; i++) {
376 if (test_bit(i, &bmap))
377 *access |= i;
378 }
379}
380
381static void
382set_deny(unsigned int *deny, unsigned long bmap) {
383 int i;
384
385 *deny = 0;
386 for (i = 0; i < 4; i++) {
387 if (test_bit(i, &bmap))
388 *deny |= i ;
389 }
390}
391
392static int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400393test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400394 unsigned int access, deny;
395
396 set_access(&access, stp->st_access_bmap);
397 set_deny(&deny, stp->st_deny_bmap);
398 if ((access & open->op_share_deny) || (deny & open->op_share_access))
399 return 0;
400 return 1;
401}
402
403static int nfs4_access_to_omode(u32 access)
404{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400405 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400406 case NFS4_SHARE_ACCESS_READ:
407 return O_RDONLY;
408 case NFS4_SHARE_ACCESS_WRITE:
409 return O_WRONLY;
410 case NFS4_SHARE_ACCESS_BOTH:
411 return O_RDWR;
412 }
413 BUG();
414}
415
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400416static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500417{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400418 list_del(&stp->st_stid.sc_hash);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500419 list_del(&stp->st_perfile);
420 list_del(&stp->st_perstateowner);
421}
422
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400423static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500424{
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400425 int i;
J. Bruce Fields0997b172011-03-02 18:01:35 -0500426
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800427 if (stp->st_access_bmap) {
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400428 for (i = 1; i < 4; i++) {
429 if (test_bit(i, &stp->st_access_bmap))
430 nfs4_file_put_access(stp->st_file,
431 nfs4_access_to_omode(i));
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400432 __clear_bit(i, &stp->st_access_bmap);
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400433 }
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800434 }
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400435 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400436 stp->st_file = NULL;
437}
438
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400439static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400440{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500441 kmem_cache_free(stateid_slab, stp);
442}
443
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400444static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500445{
446 struct file *file;
447
448 unhash_generic_stateid(stp);
449 file = find_any_file(stp->st_file);
450 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400451 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400452 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500453 free_generic_stateid(stp);
454}
455
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400456static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500457{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400458 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500459
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400460 list_del(&lo->lo_owner.so_idhash);
461 list_del(&lo->lo_owner.so_strhash);
462 list_del(&lo->lo_perstateid);
463 while (!list_empty(&lo->lo_owner.so_stateids)) {
464 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400465 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500466 release_lock_stateid(stp);
467 }
468}
469
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400470static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500471{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400472 unhash_lockowner(lo);
473 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500474}
475
476static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400477release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500478{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400479 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500480
481 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400482 lo = list_entry(open_stp->st_lockowners.next,
483 struct nfs4_lockowner, lo_perstateid);
484 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500485 }
486}
487
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400488static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500489{
490 unhash_generic_stateid(stp);
491 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400492 close_generic_stateid(stp);
493}
494
495static void release_open_stateid(struct nfs4_ol_stateid *stp)
496{
497 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500498 free_generic_stateid(stp);
499}
500
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400501static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500502{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400503 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500504
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400505 list_del(&oo->oo_owner.so_idhash);
506 list_del(&oo->oo_owner.so_strhash);
507 list_del(&oo->oo_perclient);
508 while (!list_empty(&oo->oo_owner.so_stateids)) {
509 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400510 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500511 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500512 }
513}
514
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400515static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500516{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400517 unhash_openowner(oo);
518 list_del(&oo->oo_close_lru);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400519 if (oo->oo_last_closed_stid)
520 free_generic_stateid(oo->oo_last_closed_stid);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400521 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500522}
523
Marc Eshel5282fd72009-04-03 08:27:52 +0300524#define SESSION_HASH_SIZE 512
525static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
526
527static inline int
528hash_sessionid(struct nfs4_sessionid *sessionid)
529{
530 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
531
532 return sid->sequence % SESSION_HASH_SIZE;
533}
534
535static inline void
536dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
537{
538 u32 *ptr = (u32 *)(&sessionid->data[0]);
539 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
540}
541
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300542static void
543gen_sessionid(struct nfsd4_session *ses)
544{
545 struct nfs4_client *clp = ses->se_client;
546 struct nfsd4_sessionid *sid;
547
548 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
549 sid->clientid = clp->cl_clientid;
550 sid->sequence = current_sessionid++;
551 sid->reserved = 0;
552}
553
554/*
Andy Adamsona649637c72009-08-28 08:45:01 -0400555 * The protocol defines ca_maxresponssize_cached to include the size of
556 * the rpc header, but all we need to cache is the data starting after
557 * the end of the initial SEQUENCE operation--the rest we regenerate
558 * each time. Therefore we can advertise a ca_maxresponssize_cached
559 * value that is the number of bytes in our cache plus a few additional
560 * bytes. In order to stay on the safe side, and not promise more than
561 * we can cache, those additional bytes must be the minimum possible: 24
562 * bytes of rpc header (xid through accept state, with AUTH_NULL
563 * verifier), 12 for the compound header (with zero-length tag), and 44
564 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300565 */
Andy Adamsona649637c72009-08-28 08:45:01 -0400566#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
567
Andy Adamson557ce262009-08-28 08:45:04 -0400568static void
569free_session_slots(struct nfsd4_session *ses)
570{
571 int i;
572
573 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
574 kfree(ses->se_slots[i]);
575}
576
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400577/*
578 * We don't actually need to cache the rpc and session headers, so we
579 * can allocate a little less for each slot:
580 */
581static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
582{
583 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
584}
585
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400586static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300587{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400588 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
589 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300590
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400591 return size;
592}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300593
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400594/*
595 * XXX: If we run out of reserved DRC memory we could (up to a point)
596 * re-negotiate active sessions and reduce their slot usage to make
597 * rooom for new connections. For now we just fail the create session.
598 */
599static int nfsd4_get_drc_mem(int slotsize, u32 num)
600{
601 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300602
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400603 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400604
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400605 spin_lock(&nfsd_drc_lock);
606 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
607 nfsd_drc_max_mem - nfsd_drc_mem_used);
608 num = min_t(int, num, avail / slotsize);
609 nfsd_drc_mem_used += num * slotsize;
610 spin_unlock(&nfsd_drc_lock);
611
612 return num;
613}
614
615static void nfsd4_put_drc_mem(int slotsize, int num)
616{
617 spin_lock(&nfsd_drc_lock);
618 nfsd_drc_mem_used -= slotsize * num;
619 spin_unlock(&nfsd_drc_lock);
620}
621
622static struct nfsd4_session *alloc_session(int slotsize, int numslots)
623{
624 struct nfsd4_session *new;
625 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300626
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400627 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400628 + sizeof(struct nfsd4_session) > PAGE_SIZE);
629 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400630
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400631 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300632 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400633 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400634 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400635 for (i = 0; i < numslots; i++) {
636 mem = sizeof(struct nfsd4_slot) + slotsize;
637 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
638 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400639 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400640 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400641 return new;
642out_free:
643 while (i--)
644 kfree(new->se_slots[i]);
645 kfree(new);
646 return NULL;
647}
648
649static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
650{
651 u32 maxrpc = nfsd_serv->sv_max_mesg;
652
653 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800654 new->maxresp_cached = min_t(u32, req->maxresp_cached,
655 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400656 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
657 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
658 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
659}
660
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400661static void free_conn(struct nfsd4_conn *c)
662{
663 svc_xprt_put(c->cn_xprt);
664 kfree(c);
665}
666
667static void nfsd4_conn_lost(struct svc_xpt_user *u)
668{
669 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
670 struct nfs4_client *clp = c->cn_session->se_client;
671
672 spin_lock(&clp->cl_lock);
673 if (!list_empty(&c->cn_persession)) {
674 list_del(&c->cn_persession);
675 free_conn(c);
676 }
677 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500678 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400679}
680
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400681static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400682{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400683 struct nfsd4_conn *conn;
684
685 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
686 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400687 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400688 svc_xprt_get(rqstp->rq_xprt);
689 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400690 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400691 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
692 return conn;
693}
694
J. Bruce Fields328ead22010-09-29 16:11:06 -0400695static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
696{
697 conn->cn_session = ses;
698 list_add(&conn->cn_persession, &ses->se_conns);
699}
700
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400701static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
702{
703 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400704
705 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400706 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400707 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400708}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400709
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400710static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400711{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400712 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400713 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400714}
715
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400716static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400717{
718 struct nfsd4_conn *conn;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400719 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400720
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400721 conn = alloc_conn(rqstp, dir);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400722 if (!conn)
723 return nfserr_jukebox;
724 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400725 ret = nfsd4_register_conn(conn);
726 if (ret)
727 /* oops; xprt is already down: */
728 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400729 return nfs_ok;
730}
731
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400732static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
733{
734 u32 dir = NFS4_CDFC4_FORE;
735
736 if (ses->se_flags & SESSION4_BACK_CHAN)
737 dir |= NFS4_CDFC4_BACK;
738
739 return nfsd4_new_conn(rqstp, ses, dir);
740}
741
742/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400743static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400744{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400745 struct nfs4_client *clp = s->se_client;
746 struct nfsd4_conn *c;
747
748 spin_lock(&clp->cl_lock);
749 while (!list_empty(&s->se_conns)) {
750 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
751 list_del_init(&c->cn_persession);
752 spin_unlock(&clp->cl_lock);
753
754 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
755 free_conn(c);
756
757 spin_lock(&clp->cl_lock);
758 }
759 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400760}
761
762void free_session(struct kref *kref)
763{
764 struct nfsd4_session *ses;
765 int mem;
766
767 ses = container_of(kref, struct nfsd4_session, se_ref);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400768 nfsd4_del_conns(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400769 spin_lock(&nfsd_drc_lock);
770 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
771 nfsd_drc_mem_used -= mem;
772 spin_unlock(&nfsd_drc_lock);
773 free_session_slots(ses);
774 kfree(ses);
775}
776
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400777static 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 -0400778{
779 struct nfsd4_session *new;
780 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
781 int numslots, slotsize;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400782 int status;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400783 int idx;
784
785 /*
786 * Note decreasing slot size below client's request may
787 * make it difficult for client to function correctly, whereas
788 * decreasing the number of slots will (just?) affect
789 * performance. When short on memory we therefore prefer to
790 * decrease number of slots instead of their size.
791 */
792 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
793 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800794 if (numslots < 1)
795 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400796
797 new = alloc_session(slotsize, numslots);
798 if (!new) {
799 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400800 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400801 }
802 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
Andy Adamson557ce262009-08-28 08:45:04 -0400803
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300804 new->se_client = clp;
805 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300806
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400807 INIT_LIST_HEAD(&new->se_conns);
808
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400809 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300810 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400811 new->se_cb_prog = cses->callback_prog;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300812 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400813 idx = hash_sessionid(&new->se_sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300814 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300815 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400816 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300817 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400818 spin_unlock(&clp->cl_lock);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300819 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300820
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400821 status = nfsd4_new_conn_from_crses(rqstp, new);
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400822 /* whoops: benny points out, status is ignored! (err, or bogus) */
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400823 if (status) {
824 free_session(&new->se_ref);
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400825 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400826 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400827 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400828 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400829 /*
830 * This is a little silly; with sessions there's no real
831 * use for the callback address. Use the peer address
832 * as a reasonable default for now, but consider fixing
833 * the rpc client not to require an address in the
834 * future:
835 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400836 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
837 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400838 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400839 nfsd4_probe_callback(clp);
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -0400840 return new;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300841}
842
Benny Halevy9089f1b2010-05-12 00:12:26 +0300843/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300844static struct nfsd4_session *
845find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
846{
847 struct nfsd4_session *elem;
848 int idx;
849
850 dump_sessionid(__func__, sessionid);
851 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300852 /* Search in the appropriate list */
853 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300854 if (!memcmp(elem->se_sessionid.data, sessionid->data,
855 NFS4_MAX_SESSIONID_LEN)) {
856 return elem;
857 }
858 }
859
860 dprintk("%s: session not found\n", __func__);
861 return NULL;
862}
863
Benny Halevy9089f1b2010-05-12 00:12:26 +0300864/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300865static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300866unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300867{
868 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400869 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300870 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400871 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +0300872}
873
Benny Halevy36acb662010-05-12 00:13:04 +0300874/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300876renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Benny Halevy07cd4902010-05-12 00:13:41 +0300878 if (is_client_expired(clp)) {
879 dprintk("%s: client (clientid %08x/%08x) already expired\n",
880 __func__,
881 clp->cl_clientid.cl_boot,
882 clp->cl_clientid.cl_id);
883 return;
884 }
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * Move client to the end to the LRU list.
888 */
889 dprintk("renewing client (clientid %08x/%08x)\n",
890 clp->cl_clientid.cl_boot,
891 clp->cl_clientid.cl_id);
892 list_move_tail(&clp->cl_lru, &client_lru);
893 clp->cl_time = get_seconds();
894}
895
Benny Halevy36acb662010-05-12 00:13:04 +0300896static inline void
897renew_client(struct nfs4_client *clp)
898{
899 spin_lock(&client_lock);
900 renew_client_locked(clp);
901 spin_unlock(&client_lock);
902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
905static int
906STALE_CLIENTID(clientid_t *clid)
907{
908 if (clid->cl_boot == boot_time)
909 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +0300910 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
911 clid->cl_boot, clid->cl_id, boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 1;
913}
914
915/*
916 * XXX Should we use a slab cache ?
917 * This type of memory management is somewhat inefficient, but we use it
918 * anyway since SETCLIENTID is not a common operation.
919 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500920static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct nfs4_client *clp;
923
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500924 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
925 if (clp == NULL)
926 return NULL;
927 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
928 if (clp->cl_name.data == NULL) {
929 kfree(clp);
930 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500932 memcpy(clp->cl_name.data, name.data, name.len);
933 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return clp;
935}
936
937static inline void
938free_client(struct nfs4_client *clp)
939{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400940 while (!list_empty(&clp->cl_sessions)) {
941 struct nfsd4_session *ses;
942 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
943 se_perclnt);
944 list_del(&ses->se_perclnt);
945 nfsd4_put_session(ses);
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (clp->cl_cred.cr_group_info)
948 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500949 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 kfree(clp->cl_name.data);
951 kfree(clp);
952}
953
Benny Halevyd7682982010-05-12 00:13:54 +0300954void
955release_session_client(struct nfsd4_session *session)
956{
957 struct nfs4_client *clp = session->se_client;
958
959 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
960 return;
961 if (is_client_expired(clp)) {
962 free_client(clp);
963 session->se_client = NULL;
964 } else
965 renew_client_locked(clp);
966 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +0300967}
968
Benny Halevy84d38ac2010-05-12 00:13:16 +0300969/* must be called under the client_lock */
970static inline void
971unhash_client_locked(struct nfs4_client *clp)
972{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400973 struct nfsd4_session *ses;
974
Benny Halevy07cd4902010-05-12 00:13:41 +0300975 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300976 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400977 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400978 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
979 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400980 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983static void
984expire_client(struct nfs4_client *clp)
985{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400986 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 struct list_head reaplist;
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 INIT_LIST_HEAD(&reaplist);
991 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700992 while (!list_empty(&clp->cl_delegations)) {
993 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -0700994 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 list_move(&dp->dl_recall_lru, &reaplist);
996 }
997 spin_unlock(&recall_lock);
998 while (!list_empty(&reaplist)) {
999 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1000 list_del_init(&dp->dl_recall_lru);
1001 unhash_delegation(dp);
1002 }
NeilBrownea1da632005-06-23 22:04:17 -07001003 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001004 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1005 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001007 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001008 if (clp->cl_cb_conn.cb_xprt)
1009 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001010 list_del(&clp->cl_idhash);
1011 list_del(&clp->cl_strhash);
1012 spin_lock(&client_lock);
1013 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +03001014 if (atomic_read(&clp->cl_refcount) == 0)
1015 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001016 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001019static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1020{
1021 memcpy(target->cl_verifier.data, source->data,
1022 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001025static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1028 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1029}
1030
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001031static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 target->cr_uid = source->cr_uid;
1034 target->cr_gid = source->cr_gid;
1035 target->cr_group_info = source->cr_group_info;
1036 get_group_info(target->cr_group_info);
1037}
1038
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001039static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001040{
NeilBrowna55370a2005-06-23 22:03:52 -07001041 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001045same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1046{
1047 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001051same_clid(clientid_t *cl1, clientid_t *cl2)
1052{
1053 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056/* XXX what about NGROUP */
1057static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001058same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1059{
1060 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001063static void gen_clid(struct nfs4_client *clp)
1064{
1065 static u32 current_clientid = 1;
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 clp->cl_clientid.cl_boot = boot_time;
1068 clp->cl_clientid.cl_id = current_clientid++;
1069}
1070
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001071static void gen_confirm(struct nfs4_client *clp)
1072{
1073 static u32 i;
1074 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001077 *p++ = get_seconds();
1078 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
J. Bruce Fields4581d142011-09-06 14:56:09 -04001081static int
1082same_stateid(stateid_t *id_one, stateid_t *id_two)
1083{
1084 if (id_one->si_stateownerid != id_two->si_stateownerid)
1085 return 0;
1086 return id_one->si_fileid == id_two->si_fileid;
1087}
1088
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001089static struct nfs4_stid *find_stateid(stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001090{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001091 struct nfs4_stid *s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001092 unsigned int hashval;
1093
J. Bruce Fieldsee626a72011-09-11 13:48:41 -04001094 hashval = stateid_hashval(t);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001095 list_for_each_entry(s, &stateid_hashtbl[hashval], sc_hash)
1096 if (same_stateid(&s->sc_stateid, t))
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001097 return s;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001098 return NULL;
1099}
1100
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001101static struct nfs4_ol_stateid *find_ol_stateid(stateid_t *t)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001102{
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001103 struct nfs4_stid *s;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001104
1105 s = find_stateid(t);
1106 if (!s)
1107 return NULL;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001108 return openlockstateid(s);
1109}
1110
1111static struct nfs4_stid *find_stateid_by_type(stateid_t *t, char typemask)
1112{
1113 struct nfs4_stid *s;
1114
1115 s = find_stateid(t);
1116 if (!s)
1117 return NULL;
1118 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001119 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001120 return NULL;
1121}
1122
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001123static struct nfs4_ol_stateid *find_ol_stateid_by_type(stateid_t *t, char typemask)
1124{
1125 struct nfs4_stid *s;
1126
1127 s = find_stateid_by_type(t, typemask);
1128 if (!s)
1129 return NULL;
1130 return openlockstateid(s);
1131}
1132
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001133static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1134 struct svc_rqst *rqstp, nfs4_verifier *verf)
1135{
1136 struct nfs4_client *clp;
1137 struct sockaddr *sa = svc_addr(rqstp);
1138 char *princ;
1139
1140 clp = alloc_client(name);
1141 if (clp == NULL)
1142 return NULL;
1143
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001144 INIT_LIST_HEAD(&clp->cl_sessions);
1145
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001146 princ = svc_gss_principal(rqstp);
1147 if (princ) {
1148 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1149 if (clp->cl_principal == NULL) {
1150 free_client(clp);
1151 return NULL;
1152 }
1153 }
1154
1155 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Benny Halevy46583e22010-05-12 00:13:29 +03001156 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001157 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001158 INIT_LIST_HEAD(&clp->cl_idhash);
1159 INIT_LIST_HEAD(&clp->cl_strhash);
1160 INIT_LIST_HEAD(&clp->cl_openowners);
1161 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001162 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001163 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001164 spin_lock_init(&clp->cl_lock);
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001165 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
Benny Halevy07cd4902010-05-12 00:13:41 +03001166 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001167 clear_bit(0, &clp->cl_cb_slot_busy);
1168 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1169 copy_verf(clp, verf);
1170 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1171 clp->cl_flavor = rqstp->rq_flavor;
1172 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1173 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001174 clp->cl_cb_session = NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001175 return clp;
1176}
1177
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001178static int check_name(struct xdr_netobj name)
1179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (name.len == 0)
1181 return 0;
1182 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001183 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return 0;
1185 }
1186 return 1;
1187}
1188
NeilBrownfd39ca92005-06-23 22:04:03 -07001189static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1191{
1192 unsigned int idhashval;
1193
1194 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1195 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1196 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001197 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
NeilBrownfd39ca92005-06-23 22:04:03 -07001200static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201move_to_confirmed(struct nfs4_client *clp)
1202{
1203 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1204 unsigned int strhashval;
1205
1206 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001207 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07001208 strhashval = clientstr_hashval(clp->cl_recdir);
Benny Halevy328efba2010-05-12 00:12:51 +03001209 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 renew_client(clp);
1211}
1212
1213static struct nfs4_client *
1214find_confirmed_client(clientid_t *clid)
1215{
1216 struct nfs4_client *clp;
1217 unsigned int idhashval = clientid_hashval(clid->cl_id);
1218
1219 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001220 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return clp;
1222 }
1223 return NULL;
1224}
1225
1226static struct nfs4_client *
1227find_unconfirmed_client(clientid_t *clid)
1228{
1229 struct nfs4_client *clp;
1230 unsigned int idhashval = clientid_hashval(clid->cl_id);
1231
1232 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001233 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return clp;
1235 }
1236 return NULL;
1237}
1238
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001239static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001240{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001241 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001242}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001243
NeilBrown28ce6052005-06-23 22:03:56 -07001244static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001245find_confirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001246{
1247 struct nfs4_client *clp;
1248
1249 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001250 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001251 return clp;
1252 }
1253 return NULL;
1254}
1255
1256static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001257find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001258{
1259 struct nfs4_client *clp;
1260
1261 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001262 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001263 return clp;
1264 }
1265 return NULL;
1266}
1267
NeilBrownfd39ca92005-06-23 22:04:03 -07001268static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001269gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001271 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001272 struct sockaddr *sa = svc_addr(rqstp);
1273 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001274 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Jeff Layton7077ecb2009-08-14 12:57:58 -04001276 /* Currently, we only support tcp and tcp6 for the callback channel */
1277 if (se->se_callback_netid_len == 3 &&
1278 !memcmp(se->se_callback_netid_val, "tcp", 3))
1279 expected_family = AF_INET;
1280 else if (se->se_callback_netid_len == 4 &&
1281 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1282 expected_family = AF_INET6;
1283 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto out_err;
1285
J. Bruce Fields07263f12010-05-31 19:09:40 -04001286 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001287 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001288 (struct sockaddr *)&conn->cb_addr,
1289 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001290
J. Bruce Fields07263f12010-05-31 19:09:40 -04001291 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001293
J. Bruce Fields07263f12010-05-31 19:09:40 -04001294 if (conn->cb_addr.ss_family == AF_INET6)
1295 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001296
J. Bruce Fields07263f12010-05-31 19:09:40 -04001297 conn->cb_prog = se->se_callback_prog;
1298 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001299 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return;
1301out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001302 conn->cb_addr.ss_family = AF_UNSPEC;
1303 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001304 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 "will not receive delegations\n",
1306 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return;
1309}
1310
Andy Adamson074fe892009-04-03 08:28:15 +03001311/*
Andy Adamson557ce262009-08-28 08:45:04 -04001312 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001313 */
1314void
1315nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1316{
Andy Adamson557ce262009-08-28 08:45:04 -04001317 struct nfsd4_slot *slot = resp->cstate.slot;
1318 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001319
Andy Adamson557ce262009-08-28 08:45:04 -04001320 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001321
Andy Adamson557ce262009-08-28 08:45:04 -04001322 slot->sl_opcnt = resp->opcnt;
1323 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001324
1325 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001326 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001327 return;
1328 }
Andy Adamson557ce262009-08-28 08:45:04 -04001329 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1330 base = (char *)resp->cstate.datap -
1331 (char *)resp->xbuf->head[0].iov_base;
1332 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1333 slot->sl_datalen))
1334 WARN("%s: sessions DRC could not cache compound\n", __func__);
1335 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001336}
1337
1338/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001339 * Encode the replay sequence operation from the slot values.
1340 * If cachethis is FALSE encode the uncached rep error on the next
1341 * operation which sets resp->p and increments resp->opcnt for
1342 * nfs4svc_encode_compoundres.
1343 *
Andy Adamson074fe892009-04-03 08:28:15 +03001344 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001345static __be32
1346nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1347 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001348{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001349 struct nfsd4_op *op;
1350 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001351
Andy Adamsonabfabf82009-07-23 19:02:18 -04001352 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
Andy Adamson557ce262009-08-28 08:45:04 -04001353 resp->opcnt, resp->cstate.slot->sl_cachethis);
Andy Adamsonabfabf82009-07-23 19:02:18 -04001354
1355 /* Encode the replayed sequence operation */
1356 op = &args->ops[resp->opcnt - 1];
1357 nfsd4_encode_operation(resp, op);
1358
1359 /* Return nfserr_retry_uncached_rep in next operation. */
Andy Adamson557ce262009-08-28 08:45:04 -04001360 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001361 op = &args->ops[resp->opcnt++];
1362 op->status = nfserr_retry_uncached_rep;
1363 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001364 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001365 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001366}
1367
1368/*
Andy Adamson557ce262009-08-28 08:45:04 -04001369 * The sequence operation is not cached because we can use the slot and
1370 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001371 */
1372__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001373nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1374 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001375{
Andy Adamson557ce262009-08-28 08:45:04 -04001376 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001377 __be32 status;
1378
Andy Adamson557ce262009-08-28 08:45:04 -04001379 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001380
Andy Adamsonabfabf82009-07-23 19:02:18 -04001381 /* Either returns 0 or nfserr_retry_uncached */
1382 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1383 if (status == nfserr_retry_uncached_rep)
1384 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001385
Andy Adamson557ce262009-08-28 08:45:04 -04001386 /* The sequence operation has been encoded, cstate->datap set. */
1387 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001388
Andy Adamson557ce262009-08-28 08:45:04 -04001389 resp->opcnt = slot->sl_opcnt;
1390 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1391 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001392
1393 return status;
1394}
1395
Andy Adamson0733d212009-04-03 08:28:01 +03001396/*
1397 * Set the exchange_id flags returned by the server.
1398 */
1399static void
1400nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1401{
1402 /* pNFS is not supported */
1403 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1404
1405 /* Referrals are supported, Migration is not. */
1406 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1407
1408 /* set the wire flags to return to client. */
1409 clid->flags = new->cl_exchange_flags;
1410}
1411
Al Virob37ad282006-10-19 23:28:59 -07001412__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001413nfsd4_exchange_id(struct svc_rqst *rqstp,
1414 struct nfsd4_compound_state *cstate,
1415 struct nfsd4_exchange_id *exid)
1416{
Andy Adamson0733d212009-04-03 08:28:01 +03001417 struct nfs4_client *unconf, *conf, *new;
1418 int status;
1419 unsigned int strhashval;
1420 char dname[HEXDIR_LEN];
Jeff Layton363168b2009-08-14 12:57:56 -04001421 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001422 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001423 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamson0733d212009-04-03 08:28:01 +03001424
Jeff Layton363168b2009-08-14 12:57:56 -04001425 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001426 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001427 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001428 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001429 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001430
1431 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1432 return nfserr_inval;
1433
1434 /* Currently only support SP4_NONE */
1435 switch (exid->spa_how) {
1436 case SP4_NONE:
1437 break;
1438 case SP4_SSV:
J. Bruce Fields044bc1d2010-11-12 14:36:06 -05001439 return nfserr_serverfault;
Andy Adamson0733d212009-04-03 08:28:01 +03001440 default:
1441 BUG(); /* checked by xdr code */
1442 case SP4_MACH_CRED:
1443 return nfserr_serverfault; /* no excuse :-/ */
1444 }
1445
1446 status = nfs4_make_rec_clidname(dname, &exid->clname);
1447
1448 if (status)
1449 goto error;
1450
1451 strhashval = clientstr_hashval(dname);
1452
1453 nfs4_lock_state();
1454 status = nfs_ok;
1455
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001456 conf = find_confirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001457 if (conf) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001458 if (!clp_used_exchangeid(conf)) {
1459 status = nfserr_clid_inuse; /* XXX: ? */
1460 goto out;
1461 }
Andy Adamson0733d212009-04-03 08:28:01 +03001462 if (!same_verf(&verf, &conf->cl_verifier)) {
1463 /* 18.35.4 case 8 */
1464 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1465 status = nfserr_not_same;
1466 goto out;
1467 }
1468 /* Client reboot: destroy old state */
1469 expire_client(conf);
1470 goto out_new;
1471 }
1472 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1473 /* 18.35.4 case 9 */
1474 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1475 status = nfserr_perm;
1476 goto out;
1477 }
1478 expire_client(conf);
1479 goto out_new;
1480 }
Andy Adamson0733d212009-04-03 08:28:01 +03001481 /*
1482 * Set bit when the owner id and verifier map to an already
1483 * confirmed client id (18.35.3).
1484 */
1485 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1486
1487 /*
1488 * Falling into 18.35.4 case 2, possible router replay.
1489 * Leave confirmed record intact and return same result.
1490 */
1491 copy_verf(conf, &verf);
1492 new = conf;
1493 goto out_copy;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001494 }
1495
1496 /* 18.35.4 case 7 */
1497 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1498 status = nfserr_noent;
1499 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001500 }
1501
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001502 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001503 if (unconf) {
1504 /*
1505 * Possible retry or client restart. Per 18.35.4 case 4,
1506 * a new unconfirmed record should be generated regardless
1507 * of whether any properties have changed.
1508 */
1509 expire_client(unconf);
1510 }
1511
1512out_new:
1513 /* Normal case */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001514 new = create_client(exid->clname, dname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001515 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001516 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001517 goto out;
1518 }
1519
Andy Adamson0733d212009-04-03 08:28:01 +03001520 gen_clid(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001521 add_to_unconfirmed(new, strhashval);
1522out_copy:
1523 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1524 exid->clientid.cl_id = new->cl_clientid.cl_id;
1525
Andy Adamson38eb76a2009-04-03 08:28:32 +03001526 exid->seqid = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001527 nfsd4_set_ex_flags(new, exid);
1528
1529 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001530 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001531 status = nfs_ok;
1532
1533out:
1534 nfs4_unlock_state();
1535error:
1536 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1537 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001538}
1539
Benny Halevyb85d4c02009-04-03 08:28:08 +03001540static int
Andy Adamson88e588d2009-07-23 19:02:15 -04001541check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001542{
Andy Adamson88e588d2009-07-23 19:02:15 -04001543 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1544 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001545
1546 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001547 if (slot_inuse) {
1548 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001549 return nfserr_jukebox;
1550 else
1551 return nfserr_seq_misordered;
1552 }
1553 /* Normal */
Andy Adamson88e588d2009-07-23 19:02:15 -04001554 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001555 return nfs_ok;
1556 /* Replay */
Andy Adamson88e588d2009-07-23 19:02:15 -04001557 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001558 return nfserr_replay_cache;
1559 /* Wraparound */
Andy Adamson88e588d2009-07-23 19:02:15 -04001560 if (seqid == 1 && (slot_seqid + 1) == 0)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001561 return nfs_ok;
1562 /* Misordered replay or misordered new request */
1563 return nfserr_seq_misordered;
1564}
1565
Andy Adamson49557cc2009-07-23 19:02:16 -04001566/*
1567 * Cache the create session result into the create session single DRC
1568 * slot cache by saving the xdr structure. sl_seqid has been set.
1569 * Do this for solo or embedded create session operations.
1570 */
1571static void
1572nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1573 struct nfsd4_clid_slot *slot, int nfserr)
1574{
1575 slot->sl_status = nfserr;
1576 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1577}
1578
1579static __be32
1580nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1581 struct nfsd4_clid_slot *slot)
1582{
1583 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1584 return slot->sl_status;
1585}
1586
Mi Jinlong1b74c252011-07-14 14:50:17 +08001587#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1588 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1589 1 + /* MIN tag is length with zero, only length */ \
1590 3 + /* version, opcount, opcode */ \
1591 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1592 /* seqid, slotID, slotID, cache */ \
1593 4 ) * sizeof(__be32))
1594
1595#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1596 2 + /* verifier: AUTH_NULL, length 0 */\
1597 1 + /* status */ \
1598 1 + /* MIN tag is length with zero, only length */ \
1599 3 + /* opcount, opcode, opstatus*/ \
1600 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1601 /* seqid, slotID, slotID, slotID, status */ \
1602 5 ) * sizeof(__be32))
1603
1604static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1605{
1606 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1607 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1608}
1609
Andy Adamson069b6ad2009-04-03 08:27:58 +03001610__be32
1611nfsd4_create_session(struct svc_rqst *rqstp,
1612 struct nfsd4_compound_state *cstate,
1613 struct nfsd4_create_session *cr_ses)
1614{
Jeff Layton363168b2009-08-14 12:57:56 -04001615 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001616 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001617 struct nfsd4_session *new;
Andy Adamson49557cc2009-07-23 19:02:16 -04001618 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001619 bool confirm_me = false;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001620 int status = 0;
1621
Mi Jinlonga62573d2011-03-23 17:57:07 +08001622 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1623 return nfserr_inval;
1624
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001625 nfs4_lock_state();
1626 unconf = find_unconfirmed_client(&cr_ses->clientid);
1627 conf = find_confirmed_client(&cr_ses->clientid);
1628
1629 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001630 cs_slot = &conf->cl_cs_slot;
1631 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001632 if (status == nfserr_replay_cache) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001633 dprintk("Got a create_session replay! seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001634 cs_slot->sl_seqid);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001635 /* Return the cached reply status */
Andy Adamson49557cc2009-07-23 19:02:16 -04001636 status = nfsd4_replay_create_session(cr_ses, cs_slot);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001637 goto out;
Andy Adamson49557cc2009-07-23 19:02:16 -04001638 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001639 status = nfserr_seq_misordered;
1640 dprintk("Sequence misordered!\n");
1641 dprintk("Expected seqid= %d but got seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001642 cs_slot->sl_seqid, cr_ses->seqid);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001643 goto out;
1644 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001645 } else if (unconf) {
1646 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001647 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001648 status = nfserr_clid_inuse;
1649 goto out;
1650 }
1651
Andy Adamson49557cc2009-07-23 19:02:16 -04001652 cs_slot = &unconf->cl_cs_slot;
1653 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001654 if (status) {
1655 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001656 status = nfserr_seq_misordered;
J. Bruce Fieldscd5b8142010-10-02 17:03:35 -04001657 goto out;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001658 }
1659
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001660 confirm_me = true;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001661 conf = unconf;
1662 } else {
1663 status = nfserr_stale_clientid;
1664 goto out;
1665 }
1666
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001667 /*
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001668 * XXX: we should probably set this at creation time, and check
1669 * for consistent minorversion use throughout:
1670 */
1671 conf->cl_minorversion = 1;
1672 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001673 * We do not support RDMA or persistent sessions
1674 */
1675 cr_ses->flags &= ~SESSION4_PERSIST;
1676 cr_ses->flags &= ~SESSION4_RDMA;
1677
Mi Jinlong1b74c252011-07-14 14:50:17 +08001678 status = nfserr_toosmall;
1679 if (check_forechannel_attrs(cr_ses->fore_channel))
1680 goto out;
1681
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001682 status = nfserr_jukebox;
1683 new = alloc_init_session(rqstp, conf, cr_ses);
1684 if (!new)
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001685 goto out;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001686 status = nfs_ok;
1687 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001688 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001689 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1690 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001691 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001692 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001693
Andy Adamson49557cc2009-07-23 19:02:16 -04001694 /* cache solo and embedded create sessions under the state lock */
1695 nfsd4_cache_create_session(cr_ses, cs_slot, status);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001696 if (confirm_me)
1697 move_to_confirmed(conf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001698out:
1699 nfs4_unlock_state();
1700 dprintk("%s returns %d\n", __func__, ntohl(status));
1701 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001702}
1703
J. Bruce Fields57716352010-04-21 12:27:19 -04001704static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1705{
1706 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1707 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1708
1709 return argp->opcnt == resp->opcnt;
1710}
1711
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001712static __be32 nfsd4_map_bcts_dir(u32 *dir)
1713{
1714 switch (*dir) {
1715 case NFS4_CDFC4_FORE:
1716 case NFS4_CDFC4_BACK:
1717 return nfs_ok;
1718 case NFS4_CDFC4_FORE_OR_BOTH:
1719 case NFS4_CDFC4_BACK_OR_BOTH:
1720 *dir = NFS4_CDFC4_BOTH;
1721 return nfs_ok;
1722 };
1723 return nfserr_inval;
1724}
1725
1726__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1727 struct nfsd4_compound_state *cstate,
1728 struct nfsd4_bind_conn_to_session *bcts)
1729{
1730 __be32 status;
1731
1732 if (!nfsd4_last_compound_op(rqstp))
1733 return nfserr_not_only_op;
1734 spin_lock(&client_lock);
1735 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1736 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1737 * client_lock iself: */
1738 if (cstate->session) {
1739 nfsd4_get_session(cstate->session);
1740 atomic_inc(&cstate->session->se_client->cl_refcount);
1741 }
1742 spin_unlock(&client_lock);
1743 if (!cstate->session)
1744 return nfserr_badsession;
1745
1746 status = nfsd4_map_bcts_dir(&bcts->dir);
Bryan Schumaker1db2b9d2011-04-27 15:47:15 -04001747 if (!status)
1748 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1749 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001750}
1751
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001752static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1753{
1754 if (!session)
1755 return 0;
1756 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1757}
1758
Andy Adamson069b6ad2009-04-03 08:27:58 +03001759__be32
1760nfsd4_destroy_session(struct svc_rqst *r,
1761 struct nfsd4_compound_state *cstate,
1762 struct nfsd4_destroy_session *sessionid)
1763{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001764 struct nfsd4_session *ses;
1765 u32 status = nfserr_badsession;
1766
1767 /* Notes:
1768 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1769 * - Should we return nfserr_back_chan_busy if waiting for
1770 * callbacks on to-be-destroyed session?
1771 * - Do we need to clear any callback info from previous session?
1772 */
1773
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001774 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001775 if (!nfsd4_last_compound_op(r))
1776 return nfserr_not_only_op;
1777 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001778 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001779 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001780 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1781 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001782 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001783 goto out;
1784 }
1785
1786 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001787 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001788
Benny Halevyab707e152010-05-12 00:14:06 +03001789 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001790 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001791 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001792
1793 nfsd4_del_conns(ses);
1794
Benny Halevye10e0cf2009-04-03 08:28:38 +03001795 nfsd4_put_session(ses);
1796 status = nfs_ok;
1797out:
1798 dprintk("%s returns %d\n", __func__, ntohl(status));
1799 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001800}
1801
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001802static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001803{
1804 struct nfsd4_conn *c;
1805
1806 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001807 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001808 return c;
1809 }
1810 }
1811 return NULL;
1812}
1813
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001814static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001815{
1816 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001817 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001818 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001819
1820 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001821 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001822 if (c) {
1823 spin_unlock(&clp->cl_lock);
1824 free_conn(new);
1825 return;
1826 }
1827 __nfsd4_hash_conn(new, ses);
1828 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001829 ret = nfsd4_register_conn(new);
1830 if (ret)
1831 /* oops; xprt is already down: */
1832 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001833 return;
1834}
1835
Mi Jinlong868b89c2011-04-27 09:09:58 +08001836static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1837{
1838 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1839
1840 return args->opcnt > session->se_fchannel.maxops;
1841}
1842
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001843static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1844 struct nfsd4_session *session)
1845{
1846 struct xdr_buf *xb = &rqstp->rq_arg;
1847
1848 return xb->len > session->se_fchannel.maxreq_sz;
1849}
1850
Andy Adamson069b6ad2009-04-03 08:27:58 +03001851__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001852nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001853 struct nfsd4_compound_state *cstate,
1854 struct nfsd4_sequence *seq)
1855{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001856 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001857 struct nfsd4_session *session;
1858 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001859 struct nfsd4_conn *conn;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001860 int status;
1861
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001862 if (resp->opcnt != 1)
1863 return nfserr_sequence_pos;
1864
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001865 /*
1866 * Will be either used or freed by nfsd4_sequence_check_conn
1867 * below.
1868 */
1869 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1870 if (!conn)
1871 return nfserr_jukebox;
1872
Benny Halevy9089f1b2010-05-12 00:12:26 +03001873 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001874 status = nfserr_badsession;
1875 session = find_in_sessionid_hashtbl(&seq->sessionid);
1876 if (!session)
1877 goto out;
1878
Mi Jinlong868b89c2011-04-27 09:09:58 +08001879 status = nfserr_too_many_ops;
1880 if (nfsd4_session_too_many_ops(rqstp, session))
1881 goto out;
1882
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001883 status = nfserr_req_too_big;
1884 if (nfsd4_request_too_big(rqstp, session))
1885 goto out;
1886
Benny Halevyb85d4c02009-04-03 08:28:08 +03001887 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03001888 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001889 goto out;
1890
Andy Adamson557ce262009-08-28 08:45:04 -04001891 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03001892 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1893
Andy Adamsona8dfdae2009-08-28 08:45:02 -04001894 /* We do not negotiate the number of slots yet, so set the
1895 * maxslots to the session maxreqs which is used to encode
1896 * sr_highest_slotid and the sr_target_slot id to maxslots */
1897 seq->maxslots = session->se_fchannel.maxreqs;
1898
Andy Adamson88e588d2009-07-23 19:02:15 -04001899 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001900 if (status == nfserr_replay_cache) {
1901 cstate->slot = slot;
1902 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001903 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04001904 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03001905 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03001906 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001907 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001908 }
1909 if (status)
1910 goto out;
1911
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001912 nfsd4_sequence_check_conn(conn, session);
1913 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001914
Benny Halevyb85d4c02009-04-03 08:28:08 +03001915 /* Success! bump slot seqid */
1916 slot->sl_inuse = true;
1917 slot->sl_seqid = seq->seqid;
Andy Adamson557ce262009-08-28 08:45:04 -04001918 slot->sl_cachethis = seq->cachethis;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001919
1920 cstate->slot = slot;
1921 cstate->session = session;
1922
Benny Halevyb85d4c02009-04-03 08:28:08 +03001923out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04001924 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001925 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001926 struct nfs4_client *clp = session->se_client;
1927
Benny Halevy36acb662010-05-12 00:13:04 +03001928 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001929 atomic_inc(&clp->cl_refcount);
1930 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1931 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001932 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001933 kfree(conn);
Benny Halevy36acb662010-05-12 00:13:04 +03001934 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001935 dprintk("%s: return %d\n", __func__, ntohl(status));
1936 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001937}
1938
1939__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001940nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1941{
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001942 int status = 0;
1943
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001944 if (rc->rca_one_fs) {
1945 if (!cstate->current_fh.fh_dentry)
1946 return nfserr_nofilehandle;
1947 /*
1948 * We don't take advantage of the rca_one_fs case.
1949 * That's OK, it's optional, we can safely ignore it.
1950 */
1951 return nfs_ok;
1952 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001953
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001954 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001955 status = nfserr_complete_already;
1956 if (cstate->session->se_client->cl_firststate)
1957 goto out;
1958
1959 status = nfserr_stale_clientid;
1960 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001961 /*
1962 * The following error isn't really legal.
1963 * But we only get here if the client just explicitly
1964 * destroyed the client. Surely it no longer cares what
1965 * error it gets back on an operation for the dead
1966 * client.
1967 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001968 goto out;
1969
1970 status = nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001971 nfsd4_create_clid_dir(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001972out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001973 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001974 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001975}
1976
1977__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001978nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1979 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 struct xdr_netobj clname = {
1982 .len = setclid->se_namelen,
1983 .data = setclid->se_name,
1984 };
1985 nfs4_verifier clverifier = setclid->se_verf;
1986 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07001987 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07001988 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -07001989 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -07001992 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
NeilBrowna55370a2005-06-23 22:03:52 -07001994 status = nfs4_make_rec_clidname(dname, &clname);
1995 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07001996 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 /*
1999 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2000 * We get here on a DRC miss.
2001 */
2002
NeilBrowna55370a2005-06-23 22:03:52 -07002003 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 nfs4_lock_state();
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002006 conf = find_confirmed_client_by_str(dname, strhashval);
NeilBrown28ce6052005-06-23 22:03:56 -07002007 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002008 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002010 if (clp_used_exchangeid(conf))
2011 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002012 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002013 char addr_str[INET6_ADDRSTRLEN];
2014 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2015 sizeof(addr_str));
2016 dprintk("NFSD: setclientid: string in use by client "
2017 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 goto out;
2019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002021 /*
2022 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
2023 * has a description of SETCLIENTID request processing consisting
2024 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2025 */
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002026 unconf = find_unconfirmed_client_by_str(dname, strhashval);
J. Bruce Fields3e772462011-08-10 19:07:33 -04002027 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002029 /*
2030 * RFC 3530 14.2.33 CASE 4:
2031 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 */
2033 if (unconf)
2034 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002035 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002036 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002039 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002041 * RFC 3530 14.2.33 CASE 1:
2042 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07002044 if (unconf) {
2045 /* Note this is removing unconfirmed {*x***},
2046 * which is stronger than RFC recommended {vxc**}.
2047 * This has the advantage that there is at most
2048 * one {*x***} in either list at any time.
2049 */
2050 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002052 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002053 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 } else if (!unconf) {
2057 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002058 * RFC 3530 14.2.33 CASE 2:
2059 * probable client reboot; state will be removed if
2060 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002062 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002063 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05002066 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002067 /*
2068 * RFC 3530 14.2.33 CASE 3:
2069 * probable client reboot; state will be removed if
2070 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 */
2072 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002073 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002074 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002078 /*
2079 * XXX: we should probably set this at creation time, and check
2080 * for consistent minorversion use throughout:
2081 */
2082 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002083 gen_callback(new, setclid, rqstp);
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04002084 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2086 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2087 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2088 status = nfs_ok;
2089out:
2090 nfs4_unlock_state();
2091 return status;
2092}
2093
2094
2095/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002096 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2097 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2098 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 */
Al Virob37ad282006-10-19 23:28:59 -07002100__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002101nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2102 struct nfsd4_compound_state *cstate,
2103 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Jeff Layton363168b2009-08-14 12:57:56 -04002105 struct sockaddr *sa = svc_addr(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -07002106 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2108 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002109 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 if (STALE_CLIENTID(clid))
2112 return nfserr_stale_clientid;
2113 /*
2114 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2115 * We get here on a DRC miss.
2116 */
2117
2118 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002119
2120 conf = find_confirmed_client(clid);
2121 unconf = find_unconfirmed_client(clid);
2122
2123 status = nfserr_clid_inuse;
Jeff Layton363168b2009-08-14 12:57:56 -04002124 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002125 goto out;
Jeff Layton363168b2009-08-14 12:57:56 -04002126 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002127 goto out;
2128
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002129 /*
2130 * section 14.2.34 of RFC 3530 has a description of
2131 * SETCLIENTID_CONFIRM request processing consisting
2132 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2133 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -05002134 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002135 /*
2136 * RFC 3530 14.2.34 CASE 1:
2137 * callback update
2138 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002139 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 status = nfserr_clid_inuse;
2141 else {
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002142 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2143 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002144 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05002148 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002149 /*
2150 * RFC 3530 14.2.34 CASE 2:
2151 * probable retransmitted request; play it safe and
2152 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07002153 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002154 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07002156 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07002158 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002159 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002160 /*
2161 * RFC 3530 14.2.34 CASE 3:
2162 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07002163 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002164 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 status = nfserr_clid_inuse;
2166 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07002167 unsigned int hash =
2168 clientstr_hashval(unconf->cl_recdir);
2169 conf = find_confirmed_client_by_str(unconf->cl_recdir,
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002170 hash);
NeilBrown1a69c172005-06-23 22:04:08 -07002171 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07002172 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002173 expire_client(conf);
2174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07002176 conf = unconf;
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002177 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002178 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002180 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2181 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07002182 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002183 /*
2184 * RFC 3530 14.2.34 CASE 4:
2185 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07002186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07002188 } else {
NeilBrown08e89872005-06-23 22:04:11 -07002189 /* check that we have hit one of the cases...*/
2190 status = nfserr_clid_inuse;
2191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 nfs4_unlock_state();
2194 return status;
2195}
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197/* OPEN Share state helper functions */
2198static inline struct nfs4_file *
2199alloc_init_file(struct inode *ino)
2200{
2201 struct nfs4_file *fp;
2202 unsigned int hashval = file_hashval(ino);
2203
NeilBrowne60d4392005-06-23 22:03:01 -07002204 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2205 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002206 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07002208 INIT_LIST_HEAD(&fp->fi_stateids);
2209 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 fp->fi_inode = igrab(ino);
2211 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -07002212 fp->fi_had_conflict = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002213 fp->fi_lease = NULL;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002214 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2215 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Pavel Emelyanov47cee542010-05-17 20:00:37 +04002216 spin_lock(&recall_lock);
2217 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2218 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return fp;
2220 }
2221 return NULL;
2222}
2223
2224static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002225nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002226{
NeilBrowne60d4392005-06-23 22:03:01 -07002227 if (*slab == NULL)
2228 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002229 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002230 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002231}
2232
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002233void
NeilBrowne60d4392005-06-23 22:03:01 -07002234nfsd4_free_slabs(void)
2235{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002236 nfsd4_free_slab(&openowner_slab);
2237 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002238 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002239 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002240 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002241}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243static int
2244nfsd4_init_slabs(void)
2245{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002246 openowner_slab = kmem_cache_create("nfsd4_openowners",
2247 sizeof(struct nfs4_openowner), 0, 0, NULL);
2248 if (openowner_slab == NULL)
2249 goto out_nomem;
2250 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2251 sizeof(struct nfs4_openowner), 0, 0, NULL);
2252 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002253 goto out_nomem;
2254 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002255 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002256 if (file_slab == NULL)
2257 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002258 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002259 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002260 if (stateid_slab == NULL)
2261 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002262 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002263 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002264 if (deleg_slab == NULL)
2265 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002267out_nomem:
2268 nfsd4_free_slabs();
2269 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2270 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002273void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002275 kfree(oo->oo_owner.so_owner.data);
2276 kmem_cache_free(openowner_slab, oo);
2277}
2278
2279void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2280{
2281 kfree(lo->lo_owner.so_owner.data);
2282 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002285static void init_nfs4_replay(struct nfs4_replay *rp)
2286{
2287 rp->rp_status = nfserr_serverfault;
2288 rp->rp_buflen = 0;
2289 rp->rp_buf = rp->rp_ibuf;
2290}
2291
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002292static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 struct nfs4_stateowner *sop;
2295
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002296 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002297 if (!sop)
2298 return NULL;
2299
2300 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2301 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002302 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002303 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002305 sop->so_owner.len = owner->len;
2306
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002307 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002308 sop->so_id = current_ownerid++;
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002309 sop->so_client = clp;
2310 init_nfs4_replay(&sop->so_replay);
2311 return sop;
2312}
2313
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002314static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002315{
2316 unsigned int idhashval;
2317
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002318 idhashval = open_ownerid_hashval(oo->oo_owner.so_id);
2319 list_add(&oo->oo_owner.so_idhash, &open_ownerid_hashtbl[idhashval]);
2320 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2321 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002324static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002326 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002328 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2329 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002331 oo->oo_owner.so_is_open_owner = 1;
2332 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002333 oo->oo_flags = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002334 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002335 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002336 INIT_LIST_HEAD(&oo->oo_close_lru);
2337 hash_openowner(oo, clp, strhashval);
2338 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341static inline void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002342init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002343 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
NeilBrownea1da632005-06-23 22:04:17 -07002345 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002346 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002347 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002348 stp->st_stid.sc_type = NFS4_OPEN_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002349 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002350 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 stp->st_file = fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002352 stp->st_stid.sc_stateid.si_boot = boot_time;
2353 stp->st_stid.sc_stateid.si_stateownerid = oo->oo_owner.so_id;
2354 stp->st_stid.sc_stateid.si_fileid = fp->fi_id;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04002355 /* note will be incremented before first return to client: */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002356 stp->st_stid.sc_stateid.si_generation = 0;
J. Bruce Fields36d44c62011-09-08 12:16:03 -04002357 hash_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 stp->st_access_bmap = 0;
2359 stp->st_deny_bmap = 0;
Andy Adamson84459a12009-04-03 08:28:56 +03002360 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2361 &stp->st_access_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07002363 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
2366static void
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002367move_to_close_lru(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002369 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002371 list_move_tail(&oo->oo_close_lru, &close_lru);
2372 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002376same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2377 clientid_t *clid)
2378{
2379 return (sop->so_owner.len == owner->len) &&
2380 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2381 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002384static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2386{
2387 struct nfs4_stateowner *so = NULL;
2388
J. Bruce Fields506f2752011-08-11 18:50:18 -04002389 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002390 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002391 return container_of(so, struct nfs4_openowner, oo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 return NULL;
2394}
2395
2396/* search file_hashtbl[] for file */
2397static struct nfs4_file *
2398find_file(struct inode *ino)
2399{
2400 unsigned int hashval = file_hashval(ino);
2401 struct nfs4_file *fp;
2402
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002403 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002405 if (fp->fi_inode == ino) {
2406 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002407 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002411 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return NULL;
2413}
2414
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002415static inline int access_valid(u32 x, u32 minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002416{
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002417 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002418 return 0;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002419 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2420 return 0;
2421 x &= ~NFS4_SHARE_ACCESS_MASK;
2422 if (minorversion && x) {
2423 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2424 return 0;
2425 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2426 return 0;
2427 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2428 }
2429 if (x)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002430 return 0;
2431 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002432}
2433
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002434static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002435{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002436 /* Note: unlike access bits, deny bits may be zero. */
2437 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002438}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002440/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 * Called to check deny when READ with all zero stateid or
2442 * WRITE with all zero or all one stateid
2443 */
Al Virob37ad282006-10-19 23:28:59 -07002444static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2446{
2447 struct inode *ino = current_fh->fh_dentry->d_inode;
2448 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002449 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002450 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 dprintk("NFSD: nfs4_share_conflict\n");
2453
2454 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002455 if (!fp)
2456 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002457 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002459 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2460 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2461 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2462 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
NeilBrown13cd2182005-06-23 22:03:10 -07002464 ret = nfs_ok;
2465out:
2466 put_nfs4_file(fp);
2467 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002470static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /* We're assuming the state code never drops its reference
2473 * without first removing the lease. Since we're in this lease
2474 * callback (and since the lease code is serialized by the kernel
2475 * lock) we know the server hasn't removed the lease yet, we know
2476 * it's safe to take a reference: */
2477 atomic_inc(&dp->dl_count);
2478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Arnd Bergmann460781b2010-11-17 16:26:56 +01002481 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 dp->dl_time = get_seconds();
2483
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002484 nfsd4_cb_recall(dp);
2485}
2486
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002487/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002488static void nfsd_break_deleg_cb(struct file_lock *fl)
2489{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002490 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2491 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002492
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002493 BUG_ON(!fp);
2494 /* We assume break_lease is only called once per lease: */
2495 BUG_ON(fp->fi_had_conflict);
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002496 /*
2497 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002498 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002499 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002500 */
2501 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002503 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002504 fp->fi_had_conflict = true;
2505 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2506 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002507 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510static
2511int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2512{
2513 if (arg & F_UNLCK)
2514 return lease_modify(onlist, arg);
2515 else
2516 return -EAGAIN;
2517}
2518
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002519static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002520 .lm_break = nfsd_break_deleg_cb,
2521 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522};
2523
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002524static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2525{
2526 if (nfsd4_has_session(cstate))
2527 return nfs_ok;
2528 if (seqid == so->so_seqid - 1)
2529 return nfserr_replay_me;
2530 if (seqid == so->so_seqid)
2531 return nfs_ok;
2532 return nfserr_bad_seqid;
2533}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Al Virob37ad282006-10-19 23:28:59 -07002535__be32
Andy Adamson66689582009-04-03 08:28:45 +03002536nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2537 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 clientid_t *clientid = &open->op_clientid;
2540 struct nfs4_client *clp = NULL;
2541 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002542 struct nfs4_openowner *oo = NULL;
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002543 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002546 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 if (STALE_CLIENTID(&open->op_clientid))
2549 return nfserr_stale_clientid;
2550
J. Bruce Fields506f2752011-08-11 18:50:18 -04002551 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002552 oo = find_openstateowner_str(strhashval, open);
2553 open->op_openowner = oo;
2554 if (!oo) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002555 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 clp = find_confirmed_client(clientid);
2557 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002558 return nfserr_expired;
2559 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002561 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002562 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002563 clp = oo->oo_owner.so_client;
2564 release_openowner(oo);
2565 open->op_openowner = NULL;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002566 goto renew;
2567 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002568 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002569 if (status)
2570 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571renew:
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002572 if (open->op_openowner == NULL) {
2573 oo = alloc_init_open_stateowner(strhashval, clp, open);
2574 if (oo == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002575 return nfserr_jukebox;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002576 open->op_openowner = oo;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002577 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002578 list_del_init(&oo->oo_close_lru);
2579 renew_client(oo->oo_owner.so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002580 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Al Virob37ad282006-10-19 23:28:59 -07002583static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002584nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2585{
2586 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2587 return nfserr_openmode;
2588 else
2589 return nfs_ok;
2590}
2591
Daniel Mackc47d8322011-05-16 16:38:14 +02002592static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002593{
2594 share_access &= ~NFS4_SHARE_WANT_MASK;
2595
2596 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2597}
2598
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002599static struct nfs4_delegation *find_deleg_stateid(stateid_t *s)
2600{
2601 struct nfs4_stid *ret;
2602
2603 ret = find_stateid_by_type(s, NFS4_DELEG_STID);
2604 if (!ret)
2605 return NULL;
2606 return delegstateid(ret);
2607}
2608
Al Virob37ad282006-10-19 23:28:59 -07002609static __be32
NeilBrown567d9822005-06-23 22:02:53 -07002610nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2611 struct nfs4_delegation **dp)
2612{
2613 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002614 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002615
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002616 *dp = find_deleg_stateid(&open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002617 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002618 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002619 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002620 status = nfs4_check_delegmode(*dp, flags);
2621 if (status)
2622 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002623out:
2624 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2625 return nfs_ok;
2626 if (status)
2627 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002628 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002629 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002630}
2631
Al Virob37ad282006-10-19 23:28:59 -07002632static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002633nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002635 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002636 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
NeilBrown8beefa22005-06-23 22:03:08 -07002638 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 /* ignore lock owners */
2640 if (local->st_stateowner->so_is_open_owner == 0)
2641 continue;
2642 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002643 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 *stpp = local;
2645 /* check for conflicting share reservations */
2646 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002647 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002649 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002652static inline struct nfs4_ol_stateid *
NeilBrown5ac049a2005-06-23 22:03:03 -07002653nfs4_alloc_stateid(void)
2654{
2655 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2656}
2657
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002658static inline int nfs4_access_to_access(u32 nfs4_access)
2659{
2660 int flags = 0;
2661
2662 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2663 flags |= NFSD_MAY_READ;
2664 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2665 flags |= NFSD_MAY_WRITE;
2666 return flags;
2667}
2668
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002669static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2670 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002671{
2672 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002673 int oflag = nfs4_access_to_omode(open->op_share_access);
2674 int access = nfs4_access_to_access(open->op_share_access);
2675
2676 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2677 * allowing it to break the lease and return EAGAIN leaves the
2678 * client unable to make progress in returning the delegation */
2679 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2680 access |= NFSD_MAY_NOT_BREAK_LEASE;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002681
2682 if (!fp->fi_fds[oflag]) {
2683 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2684 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002685 if (status)
2686 return status;
2687 }
2688 nfs4_file_get_access(fp, oflag);
2689
2690 return nfs_ok;
2691}
2692
Al Virob37ad282006-10-19 23:28:59 -07002693static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002694nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_ol_stateid **stpp,
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002695 struct nfs4_file *fp, struct svc_fh *cur_fh,
2696 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002698 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002699 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
NeilBrown5ac049a2005-06-23 22:03:03 -07002701 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (stp == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002703 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002705 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002706 if (status) {
2707 kmem_cache_free(stateid_slab, stp);
2708 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 *stpp = stp;
2711 return 0;
2712}
2713
Al Virob37ad282006-10-19 23:28:59 -07002714static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2716 struct nfsd4_open *open)
2717{
2718 struct iattr iattr = {
2719 .ia_valid = ATTR_SIZE,
2720 .ia_size = 0,
2721 };
2722 if (!open->op_truncate)
2723 return 0;
2724 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002725 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2727}
2728
Al Virob37ad282006-10-19 23:28:59 -07002729static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002730nfs4_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 -07002731{
J. Bruce Fields7d947842010-08-20 18:09:31 -04002732 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2733 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002734 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
J. Bruce Fields7d947842010-08-20 18:09:31 -04002736 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002737 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002738 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002739 if (status)
2740 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 status = nfsd4_truncate(rqstp, cur_fh, open);
2743 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002744 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002745 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002746 nfs4_file_put_access(fp, oflag);
2747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return status;
2749 }
2750 /* remember the open */
J. Bruce Fields24a01112010-05-18 20:01:35 -04002751 __set_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002752 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 return nfs_ok;
2755}
2756
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758static void
NeilBrown37515172005-07-07 17:59:16 -07002759nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002761 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002762 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002765/* Should we give out recallable state?: */
2766static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2767{
2768 if (clp->cl_cb_state == NFSD4_CB_UP)
2769 return true;
2770 /*
2771 * In the sessions case, since we don't have to establish a
2772 * separate connection for callbacks, we assume it's OK
2773 * until we hear otherwise:
2774 */
2775 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2776}
2777
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002778static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2779{
2780 struct file_lock *fl;
2781
2782 fl = locks_alloc_lock();
2783 if (!fl)
2784 return NULL;
2785 locks_init_lock(fl);
2786 fl->fl_lmops = &nfsd_lease_mng_ops;
2787 fl->fl_flags = FL_LEASE;
2788 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2789 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002790 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002791 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002792 return fl;
2793}
2794
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002795static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2796{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002797 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002798 struct file_lock *fl;
2799 int status;
2800
2801 fl = nfs4_alloc_init_lease(dp, flag);
2802 if (!fl)
2803 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002804 fl->fl_file = find_readable_file(fp);
2805 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2806 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002807 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002808 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002809 locks_free_lock(fl);
2810 return -ENOMEM;
2811 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002812 fp->fi_lease = fl;
2813 fp->fi_deleg_file = fl->fl_file;
2814 get_file(fp->fi_deleg_file);
2815 atomic_set(&fp->fi_delegees, 1);
2816 list_add(&dp->dl_perfile, &fp->fi_delegations);
2817 return 0;
2818}
2819
2820static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2821{
2822 struct nfs4_file *fp = dp->dl_file;
2823
2824 if (!fp->fi_lease)
2825 return nfs4_setlease(dp, flag);
2826 spin_lock(&recall_lock);
2827 if (fp->fi_had_conflict) {
2828 spin_unlock(&recall_lock);
2829 return -EAGAIN;
2830 }
2831 atomic_inc(&fp->fi_delegees);
2832 list_add(&dp->dl_perfile, &fp->fi_delegations);
2833 spin_unlock(&recall_lock);
2834 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002835 return 0;
2836}
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838/*
2839 * Attempt to hand out a delegation.
2840 */
2841static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002842nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002845 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002846 int cb_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 int status, flag = 0;
2848
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002849 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002851 open->op_recall = 0;
2852 switch (open->op_claim_type) {
2853 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002854 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002855 open->op_recall = 1;
2856 flag = open->op_delegate_type;
2857 if (flag == NFS4_OPEN_DELEGATE_NONE)
2858 goto out;
2859 break;
2860 case NFS4_OPEN_CLAIM_NULL:
2861 /* Let's not give out any delegations till everyone's
2862 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002863 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002864 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002865 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07002866 goto out;
2867 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2868 flag = NFS4_OPEN_DELEGATE_WRITE;
2869 else
2870 flag = NFS4_OPEN_DELEGATE_READ;
2871 break;
2872 default:
2873 goto out;
2874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002876 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002877 if (dp == NULL)
2878 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002879 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002880 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002881 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002883 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002885 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002886 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887out:
NeilBrown7b190fe2005-06-23 22:03:23 -07002888 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2889 && flag == NFS4_OPEN_DELEGATE_NONE
2890 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002891 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 open->op_delegate_type = flag;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002893 return;
2894out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002895 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002896out_no_deleg:
2897 flag = NFS4_OPEN_DELEGATE_NONE;
2898 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
2901/*
2902 * called with nfs4_lock_state() held.
2903 */
Al Virob37ad282006-10-19 23:28:59 -07002904__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2906{
Andy Adamson66689582009-04-03 08:28:45 +03002907 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 struct nfs4_file *fp = NULL;
2909 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002910 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07002911 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002912 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914 status = nfserr_inval;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002915 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002916 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 goto out;
2918 /*
2919 * Lookup file; if found, lookup stateid and check open request,
2920 * and check for delegations in the process of being recalled.
2921 * If not found, create the nfs4_file struct
2922 */
2923 fp = find_file(ino);
2924 if (fp) {
2925 if ((status = nfs4_check_open(fp, open, &stp)))
2926 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002927 status = nfs4_check_deleg(fp, open, &dp);
2928 if (status)
2929 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07002931 status = nfserr_bad_stateid;
2932 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2933 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04002934 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 fp = alloc_init_file(ino);
2936 if (fp == NULL)
2937 goto out;
2938 }
2939
2940 /*
2941 * OPEN the file, or upgrade an existing OPEN.
2942 * If truncate fails, the OPEN fails.
2943 */
2944 if (stp) {
2945 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002946 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (status)
2948 goto out;
2949 } else {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002950 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07002951 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 goto out;
J. Bruce Fields881ea2b2011-09-06 17:20:34 -04002953 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 status = nfsd4_truncate(rqstp, current_fh, open);
2955 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05002956 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 goto out;
2958 }
2959 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002960 update_stateid(&stp->st_stid.sc_stateid);
2961 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002963 if (nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002964 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03002965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 /*
2967 * Attempt to hand out a delegation. No error return, because the
2968 * OPEN succeeds even if we fail.
2969 */
2970 nfs4_open_delegation(current_fh, open, stp);
2971
2972 status = nfs_ok;
2973
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002974 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002975 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976out:
NeilBrown13cd2182005-06-23 22:03:10 -07002977 if (fp)
2978 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07002979 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2980 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 /*
2982 * To finish the open response, we just need to set the rflags.
2983 */
2984 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002985 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03002986 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2988
2989 return status;
2990}
2991
Al Virob37ad282006-10-19 23:28:59 -07002992__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002993nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2994 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
2996 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07002997 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 nfs4_lock_state();
3000 dprintk("process_renew(%08x/%08x): starting\n",
3001 clid->cl_boot, clid->cl_id);
3002 status = nfserr_stale_clientid;
3003 if (STALE_CLIENTID(clid))
3004 goto out;
3005 clp = find_confirmed_client(clid);
3006 status = nfserr_expired;
3007 if (clp == NULL) {
3008 /* We assume the client took too long to RENEW. */
3009 dprintk("nfsd4_renew: clientid not found!\n");
3010 goto out;
3011 }
3012 renew_client(clp);
3013 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003014 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003015 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 goto out;
3017 status = nfs_ok;
3018out:
3019 nfs4_unlock_state();
3020 return status;
3021}
3022
Daniel Mackc47d8322011-05-16 16:38:14 +02003023static struct lock_manager nfsd4_manager = {
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003024};
3025
NeilBrowna76b4312005-06-23 22:04:01 -07003026static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003027nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07003028{
3029 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07003030 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003031 locks_end_grace(&nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003032 /*
3033 * Now that every NFSv4 client has had the chance to recover and
3034 * to see the (possibly new, possibly shorter) lease time, we
3035 * can safely set the next grace time to the current lease time:
3036 */
3037 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003038}
3039
NeilBrownfd39ca92005-06-23 22:04:03 -07003040static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041nfs4_laundromat(void)
3042{
3043 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003044 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 struct nfs4_delegation *dp;
3046 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003047 time_t cutoff = get_seconds() - nfsd4_lease;
3048 time_t t, clientid_val = nfsd4_lease;
3049 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 nfs4_lock_state();
3052
3053 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003054 if (locks_in_grace())
3055 nfsd4_end_grace();
Benny Halevy36acb662010-05-12 00:13:04 +03003056 INIT_LIST_HEAD(&reaplist);
3057 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 list_for_each_safe(pos, next, &client_lru) {
3059 clp = list_entry(pos, struct nfs4_client, cl_lru);
3060 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3061 t = clp->cl_time - cutoff;
3062 if (clientid_val > t)
3063 clientid_val = t;
3064 break;
3065 }
Benny Halevyd7682982010-05-12 00:13:54 +03003066 if (atomic_read(&clp->cl_refcount)) {
3067 dprintk("NFSD: client in use (clientid %08x)\n",
3068 clp->cl_clientid.cl_id);
3069 continue;
3070 }
3071 unhash_client_locked(clp);
3072 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003073 }
3074 spin_unlock(&client_lock);
3075 list_for_each_safe(pos, next, &reaplist) {
3076 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 dprintk("NFSD: purging unused client (clientid %08x)\n",
3078 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07003079 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 expire_client(clp);
3081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 spin_lock(&recall_lock);
3083 list_for_each_safe(pos, next, &del_recall_lru) {
3084 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3085 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3086 u = dp->dl_time - cutoff;
3087 if (test_val > u)
3088 test_val = u;
3089 break;
3090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 list_move(&dp->dl_recall_lru, &reaplist);
3092 }
3093 spin_unlock(&recall_lock);
3094 list_for_each_safe(pos, next, &reaplist) {
3095 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3096 list_del_init(&dp->dl_recall_lru);
3097 unhash_delegation(dp);
3098 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003099 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 list_for_each_safe(pos, next, &close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003101 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3102 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3103 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (test_val > u)
3105 test_val = u;
3106 break;
3107 }
3108 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003109 oo->oo_owner.so_id);
3110 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 }
3112 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3113 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3114 nfs4_unlock_state();
3115 return clientid_val;
3116}
3117
Harvey Harrisona254b242008-02-20 12:49:00 -08003118static struct workqueue_struct *laundry_wq;
3119static void laundromat_main(struct work_struct *);
3120static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3121
3122static void
David Howellsc4028952006-11-22 14:57:56 +00003123laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
3125 time_t t;
3126
3127 t = nfs4_laundromat();
3128 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07003129 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003132static struct nfs4_openowner * search_close_lru(stateid_t *s)
NeilBrownf8816512005-07-07 17:59:25 -07003133{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003134 struct nfs4_openowner *local;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003135 struct nfs4_ol_stateid *os;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003137 list_for_each_entry(local, &close_lru, oo_close_lru) {
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003138 os = local->oo_last_closed_stid;
3139 if (same_stateid(&os->st_stid.sc_stateid, s))
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003140 return local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
3142 return NULL;
3143}
3144
3145static inline int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003146nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003148 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
3151static int
3152STALE_STATEID(stateid_t *stateid)
3153{
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003154 if (stateid->si_boot == boot_time)
3155 return 0;
3156 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003157 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003158 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
3161static inline int
3162access_permit_read(unsigned long access_bmap)
3163{
3164 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3165 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3166 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3167}
3168
3169static inline int
3170access_permit_write(unsigned long access_bmap)
3171{
3172 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3173 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3174}
3175
3176static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003177__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
Al Virob37ad282006-10-19 23:28:59 -07003179 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
J. Bruce Fields02921912010-07-29 15:16:59 -04003181 /* For lock stateid's, we test the parent open, not the lock: */
3182 if (stp->st_openstp)
3183 stp = stp->st_openstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3185 goto out;
3186 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3187 goto out;
3188 status = nfs_ok;
3189out:
3190 return status;
3191}
3192
Al Virob37ad282006-10-19 23:28:59 -07003193static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3195{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003196 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003198 else if (locks_in_grace()) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003199 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 * conflicting state; so we must wait out the grace period. */
3201 return nfserr_grace;
3202 } else if (flags & WR_STATE)
3203 return nfs4_share_conflict(current_fh,
3204 NFS4_SHARE_DENY_WRITE);
3205 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3206 return nfs4_share_conflict(current_fh,
3207 NFS4_SHARE_DENY_READ);
3208}
3209
3210/*
3211 * Allow READ/WRITE during grace period on recovered state only for files
3212 * that are not able to provide mandatory locking.
3213 */
3214static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08003215grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003217 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
J. Bruce Fields81b82962011-08-23 11:03:29 -04003220/* Returns true iff a is later than b: */
3221static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3222{
3223 return (s32)a->si_generation - (s32)b->si_generation > 0;
3224}
3225
J. Bruce Fields28dde242011-08-22 10:07:12 -04003226static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003227{
Andy Adamson66689582009-04-03 08:28:45 +03003228 /*
3229 * When sessions are used the stateid generation number is ignored
3230 * when it is zero.
3231 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003232 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003233 return nfs_ok;
3234
3235 if (in->si_generation == ref->si_generation)
3236 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003237
J. Bruce Fields0836f582008-01-26 19:08:12 -05003238 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003239 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003240 return nfserr_bad_stateid;
3241 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003242 * However, we could see a stateid from the past, even from a
3243 * non-buggy client. For example, if the client sends a lock
3244 * while some IO is outstanding, the lock may bump si_generation
3245 * while the IO is still in flight. The client could avoid that
3246 * situation by waiting for responses on all the IO requests,
3247 * but better performance may result in retrying IO that
3248 * receives an old_stateid error if requests are rarely
3249 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003250 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003251 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003252}
3253
J. Bruce Fields3e633072009-02-21 13:17:19 -08003254static int is_delegation_stateid(stateid_t *stateid)
3255{
3256 return stateid->si_fileid == 0;
3257}
3258
J. Bruce Fields28dde242011-08-22 10:07:12 -04003259__be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
Bryan Schumaker17456802011-07-13 10:50:48 -04003260{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003261 struct nfs4_stid *s;
3262 struct nfs4_ol_stateid *ols;
3263 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003264
3265 if (STALE_STATEID(stateid))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003266 return nfserr_stale_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04003267
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003268 s = find_stateid(stateid);
3269 if (!s)
3270 return nfserr_stale_stateid;
3271 status = check_stateid_generation(stateid, &s->sc_stateid, has_session);
Bryan Schumaker17456802011-07-13 10:50:48 -04003272 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003273 return status;
3274 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3275 return nfs_ok;
3276 ols = openlockstateid(s);
3277 if (ols->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003278 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003279 return nfserr_bad_stateid;
3280 return nfs_ok;
Bryan Schumaker17456802011-07-13 10:50:48 -04003281}
3282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283/*
3284* Checks for stateid operations
3285*/
Al Virob37ad282006-10-19 23:28:59 -07003286__be32
Benny Halevydd453df2009-04-03 08:28:41 +03003287nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3288 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003290 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003291 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003293 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07003295 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 if (filpp)
3298 *filpp = NULL;
3299
J. Bruce Fields18f82732009-02-21 15:23:01 -08003300 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 return nfserr_grace;
3302
3303 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3304 return check_special_stateids(current_fh, stateid, flags);
3305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 status = nfserr_stale_stateid;
3307 if (STALE_STATEID(stateid))
3308 goto out;
3309
J. Bruce Fields33515142010-10-02 18:42:39 -04003310 /*
3311 * We assume that any stateid that has the current boot time,
3312 * but that we can't find, is expired:
3313 */
3314 status = nfserr_expired;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003315 s = find_stateid(stateid);
3316 if (!s)
3317 goto out;
3318 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3319 if (status)
3320 goto out;
3321 if (s->sc_type == NFS4_DELEG_STID) {
3322 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003323 status = nfs4_check_delegmode(dp, flags);
3324 if (status)
3325 goto out;
3326 renew_client(dp->dl_client);
Dan Carpenter43b01782010-10-27 23:19:04 +02003327 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003328 *filpp = dp->dl_file->fi_deleg_file;
Dan Carpenter43b01782010-10-27 23:19:04 +02003329 BUG_ON(!*filpp);
3330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 } else { /* open or lock stateid */
J. Bruce Fields69064a22011-09-09 11:54:57 -04003332 stp = openlockstateid(s);
J. Bruce Fields33515142010-10-02 18:42:39 -04003333 status = nfserr_bad_stateid;
J. Bruce Fields6150ef02009-02-21 13:36:16 -08003334 if (nfs4_check_fh(current_fh, stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003336 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003337 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003339 status = nfs4_check_openmode(stp, flags);
3340 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 goto out;
3342 renew_client(stp->st_stateowner->so_client);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003343 if (filpp) {
3344 if (flags & RD_STATE)
3345 *filpp = find_readable_file(stp->st_file);
3346 else
3347 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
3350 status = nfs_ok;
3351out:
3352 return status;
3353}
3354
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003355static __be32
3356nfsd4_free_delegation_stateid(stateid_t *stateid)
3357{
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003358 struct nfs4_delegation *dp = find_deleg_stateid(stateid);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003359 if (dp)
3360 return nfserr_locks_held;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003361
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003362 return nfserr_bad_stateid;
3363}
3364
3365static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003366nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003367{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003368 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003369 return nfserr_locks_held;
3370 release_lock_stateid(stp);
3371 return nfs_ok;
3372}
3373
3374/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003375 * Test if the stateid is valid
3376 */
3377__be32
3378nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3379 struct nfsd4_test_stateid *test_stateid)
3380{
3381 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3382 return nfs_ok;
3383}
3384
3385/*
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003386 * Free a state id
3387 */
3388__be32
3389nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3390 struct nfsd4_free_stateid *free_stateid)
3391{
3392 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003393 struct nfs4_ol_stateid *stp;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003394 __be32 ret;
3395
3396 nfs4_lock_state();
3397 if (is_delegation_stateid(stateid)) {
3398 ret = nfsd4_free_delegation_stateid(stateid);
3399 goto out;
3400 }
3401
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003402 stp = find_ol_stateid(stateid);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003403 if (!stp) {
3404 ret = nfserr_bad_stateid;
3405 goto out;
3406 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003407 ret = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, 1);
J. Bruce Fields81b82962011-08-23 11:03:29 -04003408 if (ret)
3409 goto out;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003410
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003411 if (stp->st_stid.sc_type == NFS4_OPEN_STID) {
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003412 ret = nfserr_locks_held;
3413 goto out;
3414 } else {
3415 ret = nfsd4_free_lock_stateid(stp);
3416 goto out;
3417 }
3418
3419out:
3420 nfs4_unlock_state();
3421 return ret;
3422}
3423
NeilBrown4c4cd222005-07-07 17:59:27 -07003424static inline int
3425setlkflg (int type)
3426{
3427 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3428 RD_STATE : WR_STATE;
3429}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003431static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid)
3432{
3433 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3434 return nfserr_bad_stateid;
3435 if (STALE_STATEID(stateid))
3436 return nfserr_stale_stateid;
3437 return nfs_ok;
3438}
3439
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003440static __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 -04003441{
3442 struct svc_fh *current_fh = &cstate->current_fh;
3443 struct nfs4_stateowner *sop = stp->st_stateowner;
3444 __be32 status;
3445
3446 if (nfs4_check_fh(current_fh, stp))
3447 return nfserr_bad_stateid;
3448 status = nfsd4_check_seqid(cstate, sop, seqid);
3449 if (status)
3450 return status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003451 return check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003452}
3453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454/*
3455 * Checks for sequence id mutating operations.
3456 */
Al Virob37ad282006-10-19 23:28:59 -07003457static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003458nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003459 stateid_t *stateid, char typemask,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003460 struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003462 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003464 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3465 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 *stpp = NULL;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003468 status = nfs4_nospecial_stateid_checks(stateid);
3469 if (status)
3470 return status;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003471 *stpp = find_ol_stateid_by_type(stateid, typemask);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003472 if (*stpp == NULL)
3473 return nfserr_expired;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003474 cstate->replay_owner = (*stpp)->st_stateowner;
3475 renew_client((*stpp)->st_stateowner->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003477 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3478}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003479
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003480static __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 -04003481{
3482 __be32 status;
3483 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003485 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003486 NFS4_OPEN_STID, stpp);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003487 if (status)
3488 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003489 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003490 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003491 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003492 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493}
3494
Al Virob37ad282006-10-19 23:28:59 -07003495__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003496nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003497 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Al Virob37ad282006-10-19 23:28:59 -07003499 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003500 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003501 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003504 (int)cstate->current_fh.fh_dentry->d_name.len,
3505 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003507 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003508 if (status)
3509 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
3511 nfs4_lock_state();
3512
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003513 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003514 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003515 NFS4_OPEN_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003516 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003517 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003518 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003519 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003520 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003521 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003522 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003523 update_stateid(&stp->st_stid.sc_stateid);
3524 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003525 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003526 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003527
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003528 nfsd4_create_clid_dir(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003529 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003531 if (!cstate->replay_owner)
3532 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return status;
3534}
3535
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003536static inline void nfs4_file_downgrade(struct nfs4_ol_stateid *stp, unsigned int to_access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
3538 int i;
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 for (i = 1; i < 4; i++) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003541 if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
3542 nfs4_file_put_access(stp->st_file, i);
3543 __clear_bit(i, &stp->st_access_bmap);
3544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 }
3546}
3547
3548static void
3549reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3550{
3551 int i;
3552 for (i = 0; i < 4; i++) {
3553 if ((i & deny) != i)
3554 __clear_bit(i, bmap);
3555 }
3556}
3557
Al Virob37ad282006-10-19 23:28:59 -07003558__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003559nfsd4_open_downgrade(struct svc_rqst *rqstp,
3560 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003561 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
Al Virob37ad282006-10-19 23:28:59 -07003563 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003564 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003567 (int)cstate->current_fh.fh_dentry->d_name.len,
3568 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Andy Adamsond87a8ad2009-04-03 08:28:53 +03003570 if (!access_valid(od->od_share_access, cstate->minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07003571 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 return nfserr_inval;
3573
3574 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003575 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3576 &od->od_stateid, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003577 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 status = nfserr_inval;
3580 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3581 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3582 stp->st_access_bmap, od->od_share_access);
3583 goto out;
3584 }
3585 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3586 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3587 stp->st_deny_bmap, od->od_share_deny);
3588 goto out;
3589 }
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003590 nfs4_file_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3593
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003594 update_stateid(&stp->st_stid.sc_stateid);
3595 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 status = nfs_ok;
3597out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003598 if (!cstate->replay_owner)
3599 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 return status;
3601}
3602
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003603void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3604{
3605 struct nfs4_openowner *oo;
3606 struct nfs4_ol_stateid *s;
3607
3608 if (!so->so_is_open_owner)
3609 return;
3610 oo = openowner(so);
3611 s = oo->oo_last_closed_stid;
3612 if (!s)
3613 return;
3614 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3615 /* Release the last_closed_stid on the next seqid bump: */
3616 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3617 return;
3618 }
3619 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
3620 free_generic_stateid(oo->oo_last_closed_stid);
3621 oo->oo_last_closed_stid = NULL;
3622}
3623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624/*
3625 * nfs4_unlock_state() called after encode
3626 */
Al Virob37ad282006-10-19 23:28:59 -07003627__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003628nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003629 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
Al Virob37ad282006-10-19 23:28:59 -07003631 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003632 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003633 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
3635 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003636 (int)cstate->current_fh.fh_dentry->d_name.len,
3637 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
3639 nfs4_lock_state();
3640 /* check close_lru for replay */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003641 status = nfs4_preprocess_confirmed_seqid_op(cstate, close->cl_seqid,
3642 &close->cl_stateid, &stp);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003643 if (stp == NULL && status == nfserr_expired) {
3644 /*
3645 * Also, we should make sure this isn't just the result of
3646 * a replayed close:
3647 */
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003648 oo = search_close_lru(&close->cl_stateid);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003649 /* It's not stale; let's assume it's expired: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003650 if (oo == NULL)
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003651 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003652 cstate->replay_owner = &oo->oo_owner;
3653 status = nfsd4_check_seqid(cstate, &oo->oo_owner, close->cl_seqid);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003654 if (status)
3655 goto out;
3656 status = nfserr_bad_seqid;
3657 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003658 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003660 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 status = nfs_ok;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003662 update_stateid(&stp->st_stid.sc_stateid);
3663 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003665 /* unhash_open_stateid() calls nfsd_close() if needed */
3666 oo->oo_last_closed_stid = stp;
3667 unhash_open_stateid(stp);
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003668
3669 /* place unused nfs4_stateowners on so_close_lru list to be
3670 * released by the laundromat service after the lease period
3671 * to enable us to handle CLOSE replay
3672 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003673 if (list_empty(&oo->oo_owner.so_stateids))
3674 move_to_close_lru(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003676 if (!cstate->replay_owner)
3677 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return status;
3679}
3680
Al Virob37ad282006-10-19 23:28:59 -07003681__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003682nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3683 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003685 struct nfs4_delegation *dp;
3686 stateid_t *stateid = &dr->dr_stateid;
3687 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07003688 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003690 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003691 return status;
3692 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
3694 nfs4_lock_state();
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003695 status = nfserr_bad_stateid;
3696 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3697 goto out;
3698 status = nfserr_stale_stateid;
3699 if (STALE_STATEID(stateid))
3700 goto out;
J. Bruce Fields7e0f7cf2009-02-21 13:32:28 -08003701 status = nfserr_bad_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003702 if (!is_delegation_stateid(stateid))
3703 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003704 status = nfserr_expired;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003705 dp = find_deleg_stateid(stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003706 if (!dp)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003707 goto out;
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003708 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003709 if (status)
3710 goto out;
3711 renew_client(dp->dl_client);
3712
3713 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003715 nfs4_unlock_state();
3716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 return status;
3718}
3719
3720
3721/*
3722 * Lock owner state (byte-range locks)
3723 */
3724#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3725#define LOCK_HASH_BITS 8
3726#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3727#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3728
Benny Halevy87df4de2008-12-15 19:42:03 +02003729static inline u64
3730end_offset(u64 start, u64 len)
3731{
3732 u64 end;
3733
3734 end = start + len;
3735 return end >= start ? end: NFS4_MAX_UINT64;
3736}
3737
3738/* last octet in a range */
3739static inline u64
3740last_byte_offset(u64 start, u64 len)
3741{
3742 u64 end;
3743
3744 BUG_ON(!len);
3745 end = start + len;
3746 return end > start ? end - 1: NFS4_MAX_UINT64;
3747}
3748
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04003749static unsigned int lockownerid_hashval(u32 id)
3750{
3751 return id & LOCK_HASH_MASK;
3752}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
3754static inline unsigned int
3755lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3756 struct xdr_netobj *ownername)
3757{
3758 return (file_hashval(inode) + cl_id
3759 + opaque_hashval(ownername->data, ownername->len))
3760 & LOCK_HASH_MASK;
3761}
3762
3763static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3764static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766/*
3767 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3768 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3769 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3770 * locking, this prevents us from being completely protocol-compliant. The
3771 * real solution to this problem is to start using unsigned file offsets in
3772 * the VFS, but this is a very deep change!
3773 */
3774static inline void
3775nfs4_transform_lock_offset(struct file_lock *lock)
3776{
3777 if (lock->fl_start < 0)
3778 lock->fl_start = OFFSET_MAX;
3779 if (lock->fl_end < 0)
3780 lock->fl_end = OFFSET_MAX;
3781}
3782
NeilBrownd5b90262006-04-10 22:55:22 -07003783/* Hack!: For now, we're defining this just so we can use a pointer to it
3784 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003785static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003786};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
3788static inline void
3789nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3790{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003791 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
NeilBrownd5b90262006-04-10 22:55:22 -07003793 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003794 lo = (struct nfs4_lockowner *) fl->fl_owner;
3795 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3796 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003797 if (!deny->ld_owner.data)
3798 /* We just don't care that much */
3799 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003800 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3801 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003802 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003803nevermind:
3804 deny->ld_owner.len = 0;
3805 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003806 deny->ld_clientid.cl_boot = 0;
3807 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
3809 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003810 deny->ld_length = NFS4_MAX_UINT64;
3811 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3813 deny->ld_type = NFS4_READ_LT;
3814 if (fl->fl_type != F_RDLCK)
3815 deny->ld_type = NFS4_WRITE_LT;
3816}
3817
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003818static struct nfs4_lockowner *
3819find_lockowner_str(struct inode *inode, clientid_t *clid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 struct xdr_netobj *owner)
3821{
3822 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3823 struct nfs4_stateowner *op;
3824
3825 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003826 if (same_owner_str(op, owner, clid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003827 return lockowner(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 }
3829 return NULL;
3830}
3831
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003832static 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 -04003833{
3834 unsigned int idhashval;
3835
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003836 idhashval = lockownerid_hashval(lo->lo_owner.so_id);
3837 list_add(&lo->lo_owner.so_idhash, &lock_ownerid_hashtbl[idhashval]);
3838 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3839 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003840}
3841
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842/*
3843 * Alloc a lock owner structure.
3844 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003845 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 *
3847 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 */
3849
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003850static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003851alloc_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 -04003852 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003854 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3855 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003857 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3858 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07003859 /* It is the openowner seqid that will be incremented in encode in the
3860 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003861 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3862 hash_lockowner(lo, strhashval, clp, open_stp);
3863 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864}
3865
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003866static struct nfs4_ol_stateid *
3867alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003869 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
NeilBrown5ac049a2005-06-23 22:03:03 -07003871 stp = nfs4_alloc_stateid();
3872 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 goto out;
NeilBrown8beefa22005-06-23 22:03:08 -07003874 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003875 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3876 stp->st_stateowner = &lo->lo_owner;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003877 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown13cd2182005-06-23 22:03:10 -07003878 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 stp->st_file = fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003880 stp->st_stid.sc_stateid.si_boot = boot_time;
3881 stp->st_stid.sc_stateid.si_stateownerid = lo->lo_owner.so_id;
3882 stp->st_stid.sc_stateid.si_fileid = fp->fi_id;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04003883 /* note will be incremented before first return to client: */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003884 stp->st_stid.sc_stateid.si_generation = 0;
J. Bruce Fields36d44c62011-09-08 12:16:03 -04003885 hash_stid(&stp->st_stid);
J. Bruce Fields0997b172011-03-02 18:01:35 -05003886 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003888 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890out:
3891 return stp;
3892}
3893
NeilBrownfd39ca92005-06-23 22:04:03 -07003894static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895check_lock_length(u64 offset, u64 length)
3896{
Benny Halevy87df4de2008-12-15 19:42:03 +02003897 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 LOFF_OVERFLOW(offset, length)));
3899}
3900
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003901static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05003902{
3903 struct nfs4_file *fp = lock_stp->st_file;
3904 int oflag = nfs4_access_to_omode(access);
3905
3906 if (test_bit(access, &lock_stp->st_access_bmap))
3907 return;
3908 nfs4_file_get_access(fp, oflag);
3909 __set_bit(access, &lock_stp->st_access_bmap);
3910}
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912/*
3913 * LOCK operation
3914 */
Al Virob37ad282006-10-19 23:28:59 -07003915__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003916nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003917 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003919 struct nfs4_openowner *open_sop = NULL;
3920 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003921 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04003922 struct nfs4_file *fp;
3923 struct file *filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05003925 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07003926 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 unsigned int strhashval;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003928 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07003929 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3932 (long long) lock->lk_offset,
3933 (long long) lock->lk_length);
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 if (check_lock_length(lock->lk_offset, lock->lk_length))
3936 return nfserr_inval;
3937
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003938 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02003939 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08003940 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3941 return status;
3942 }
3943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 nfs4_lock_state();
3945
3946 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07003947 /*
3948 * Client indicates that this is a new lockowner.
3949 * Use open owner and open stateid to create lock owner and
3950 * lock stateid.
3951 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003952 struct nfs4_ol_stateid *open_stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
3954 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03003955 if (!nfsd4_has_session(cstate) &&
3956 STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003960 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 lock->lk_new_open_seqid,
3962 &lock->lk_new_open_stateid,
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003963 &open_stp);
NeilBrown37515172005-07-07 17:59:16 -07003964 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003966 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003967 status = nfserr_bad_stateid;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003968 if (!nfsd4_has_session(cstate) &&
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003969 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003970 &lock->v.new.clientid))
3971 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 /* create lockowner and lock stateid */
3973 fp = open_stp->st_file;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003974 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3975 open_sop->oo_owner.so_client->cl_clientid.cl_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003977 /* XXX: Do we need to check for duplicate stateowners on
3978 * the same file, or should they just be allowed (and
3979 * create new stateids)? */
J. Bruce Fields3e772462011-08-10 19:07:33 -04003980 status = nfserr_jukebox;
Neil Brownb59e3c02005-09-13 01:25:38 -07003981 lock_sop = alloc_init_lock_stateowner(strhashval,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003982 open_sop->oo_owner.so_client, open_stp, lock);
Neil Brownb59e3c02005-09-13 01:25:38 -07003983 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07003985 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08003986 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 } else {
3989 /* lock (lock owner + lock stateid) already exists */
Benny Halevydd453df2009-04-03 08:28:41 +03003990 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003991 lock->lk_old_lock_seqid,
3992 &lock->lk_old_lock_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003993 NFS4_LOCK_STID, &lock_stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 if (status)
3995 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003996 lock_sop = lockowner(lock_stp->st_stateowner);
J. Bruce Fields7d947842010-08-20 18:09:31 -04003997 fp = lock_stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003999 /* lock_sop and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004001 lkflg = setlkflg(lock->lk_type);
4002 status = nfs4_check_openmode(lock_stp, lkflg);
4003 if (status)
4004 goto out;
4005
NeilBrown0dd395d2005-07-07 17:59:15 -07004006 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004007 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004008 goto out;
4009 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004010 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004011 goto out;
4012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 locks_init_lock(&file_lock);
4014 switch (lock->lk_type) {
4015 case NFS4_READ_LT:
4016 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004017 filp = find_readable_file(lock_stp->st_file);
4018 if (filp)
4019 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 file_lock.fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 case NFS4_WRITE_LT:
4023 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004024 filp = find_writeable_file(lock_stp->st_file);
4025 if (filp)
4026 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 file_lock.fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 default:
4030 status = nfserr_inval;
4031 goto out;
4032 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004033 if (!filp) {
4034 status = nfserr_openmode;
4035 goto out;
4036 }
Neil Brownb59e3c02005-09-13 01:25:38 -07004037 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 file_lock.fl_pid = current->tgid;
4039 file_lock.fl_file = filp;
4040 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004041 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
4043 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004044 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 nfs4_transform_lock_offset(&file_lock);
4046
4047 /*
4048 * Try to lock the file in the VFS.
4049 * Note: locks.c uses the BKL to protect the inode's lock list.
4050 */
4051
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004052 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004053 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004055 update_stateid(&lock_stp->st_stid.sc_stateid);
4056 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004058 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004059 break;
4060 case (EAGAIN): /* conflock holds conflicting lock */
4061 status = nfserr_denied;
4062 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4063 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 case (EDEADLK):
4066 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004067 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004068 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004069 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004070 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08004074 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004075 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004076 if (!cstate->replay_owner)
4077 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return status;
4079}
4080
4081/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004082 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4083 * so we do a temporary open here just to get an open file to pass to
4084 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4085 * inode operation.)
4086 */
4087static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4088{
4089 struct file *file;
4090 int err;
4091
4092 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4093 if (err)
4094 return err;
4095 err = vfs_test_lock(file, lock);
4096 nfsd_close(file);
4097 return err;
4098}
4099
4100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 * LOCKT operation
4102 */
Al Virob37ad282006-10-19 23:28:59 -07004103__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004104nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4105 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
4107 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 struct file_lock file_lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004109 struct nfs4_lockowner *lo;
Marc Eshelfd85b812006-11-28 16:26:41 -05004110 int error;
Al Virob37ad282006-10-19 23:28:59 -07004111 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004113 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 return nfserr_grace;
4115
4116 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4117 return nfserr_inval;
4118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 nfs4_lock_state();
4120
4121 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03004122 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004125 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004128 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 locks_init_lock(&file_lock);
4130 switch (lockt->lt_type) {
4131 case NFS4_READ_LT:
4132 case NFS4_READW_LT:
4133 file_lock.fl_type = F_RDLCK;
4134 break;
4135 case NFS4_WRITE_LT:
4136 case NFS4_WRITEW_LT:
4137 file_lock.fl_type = F_WRLCK;
4138 break;
4139 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004140 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 status = nfserr_inval;
4142 goto out;
4143 }
4144
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004145 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4146 if (lo)
4147 file_lock.fl_owner = (fl_owner_t)lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 file_lock.fl_pid = current->tgid;
4149 file_lock.fl_flags = FL_POSIX;
4150
4151 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004152 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
4154 nfs4_transform_lock_offset(&file_lock);
4155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004157 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05004158 if (error) {
4159 status = nfserrno(error);
4160 goto out;
4161 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004162 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004164 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
4166out:
4167 nfs4_unlock_state();
4168 return status;
4169}
4170
Al Virob37ad282006-10-19 23:28:59 -07004171__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004172nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004173 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004175 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 struct file *filp = NULL;
4177 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07004178 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004179 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
4181 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4182 (long long) locku->lu_offset,
4183 (long long) locku->lu_length);
4184
4185 if (check_lock_length(locku->lu_offset, locku->lu_length))
4186 return nfserr_inval;
4187
4188 nfs4_lock_state();
4189
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004190 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004191 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004192 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004194 filp = find_any_file(stp->st_file);
4195 if (!filp) {
4196 status = nfserr_lock_range;
4197 goto out;
4198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 BUG_ON(!filp);
4200 locks_init_lock(&file_lock);
4201 file_lock.fl_type = F_UNLCK;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004202 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 file_lock.fl_pid = current->tgid;
4204 file_lock.fl_file = filp;
4205 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004206 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 file_lock.fl_start = locku->lu_offset;
4208
Benny Halevy87df4de2008-12-15 19:42:03 +02004209 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 nfs4_transform_lock_offset(&file_lock);
4211
4212 /*
4213 * Try to unlock the file in the VFS.
4214 */
Marc Eshelfd85b812006-11-28 16:26:41 -05004215 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004216 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004217 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 goto out_nfserr;
4219 }
4220 /*
4221 * OK, unlock succeeded; the only thing left to do is update the stateid.
4222 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004223 update_stateid(&stp->st_stid.sc_stateid);
4224 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
4226out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 nfs4_unlock_state();
4228 return status;
4229
4230out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004231 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 goto out;
4233}
4234
4235/*
4236 * returns
4237 * 1: locks held by lockowner
4238 * 0: no locks held by lockowner
4239 */
4240static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004241check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
4243 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004244 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 int status = 0;
4246
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004247 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004249 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 status = 1;
4251 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 }
4254out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004255 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 return status;
4257}
4258
Al Virob37ad282006-10-19 23:28:59 -07004259__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004260nfsd4_release_lockowner(struct svc_rqst *rqstp,
4261 struct nfsd4_compound_state *cstate,
4262 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
4264 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004265 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004266 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004267 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004269 struct list_head matches;
4270 int i;
Al Virob37ad282006-10-19 23:28:59 -07004271 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4274 clid->cl_boot, clid->cl_id);
4275
4276 /* XXX check for lease expiration */
4277
4278 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07004279 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
4282 nfs4_lock_state();
4283
NeilBrown3e9e3db2005-06-23 22:04:20 -07004284 status = nfserr_locks_held;
4285 /* XXX: we're doing a linear search through all the lockowners.
4286 * Yipes! For now we'll just hope clients aren't really using
4287 * release_lockowner much, but eventually we have to fix these
4288 * data structures. */
4289 INIT_LIST_HEAD(&matches);
4290 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4291 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04004292 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004293 continue;
4294 list_for_each_entry(stp, &sop->so_stateids,
4295 st_perstateowner) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004296 lo = lockowner(sop);
4297 if (check_for_locks(stp->st_file, lo))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004298 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004299 list_add(&lo->lo_list, &matches);
NeilBrown3e9e3db2005-06-23 22:04:20 -07004300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004302 }
4303 /* Clients probably won't expect us to return with some (but not all)
4304 * of the lockowner state released; so don't release any until all
4305 * have been checked. */
4306 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004307 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004308 lo = list_entry(matches.next, struct nfs4_lockowner,
4309 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004310 /* unhash_stateowner deletes so_perclient only
4311 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004312 list_del(&lo->lo_list);
4313 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 }
4315out:
4316 nfs4_unlock_state();
4317 return status;
4318}
4319
4320static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004321alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
NeilBrowna55370a2005-06-23 22:03:52 -07004323 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324}
4325
NeilBrownc7b9a452005-06-23 22:04:30 -07004326int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03004327nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07004328{
4329 unsigned int strhashval = clientstr_hashval(name);
4330 struct nfs4_client *clp;
4331
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004332 clp = find_confirmed_client_by_str(name, strhashval);
NeilBrownc7b9a452005-06-23 22:04:30 -07004333 return clp ? 1 : 0;
4334}
4335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336/*
4337 * failure => all reset bets are off, nfserr_no_grace...
4338 */
NeilBrown190e4fb2005-06-23 22:04:25 -07004339int
4340nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341{
4342 unsigned int strhashval;
4343 struct nfs4_client_reclaim *crp = NULL;
4344
NeilBrowna55370a2005-06-23 22:03:52 -07004345 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4346 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 if (!crp)
4348 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07004349 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 INIT_LIST_HEAD(&crp->cr_strhash);
4351 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07004352 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 reclaim_str_hashtbl_size++;
4354 return 1;
4355}
4356
4357static void
4358nfs4_release_reclaim(void)
4359{
4360 struct nfs4_client_reclaim *crp = NULL;
4361 int i;
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4364 while (!list_empty(&reclaim_str_hashtbl[i])) {
4365 crp = list_entry(reclaim_str_hashtbl[i].next,
4366 struct nfs4_client_reclaim, cr_strhash);
4367 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 kfree(crp);
4369 reclaim_str_hashtbl_size--;
4370 }
4371 }
4372 BUG_ON(reclaim_str_hashtbl_size);
4373}
4374
4375/*
4376 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07004377static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378nfs4_find_reclaim_client(clientid_t *clid)
4379{
4380 unsigned int strhashval;
4381 struct nfs4_client *clp;
4382 struct nfs4_client_reclaim *crp = NULL;
4383
4384
4385 /* find clientid in conf_id_hashtbl */
4386 clp = find_confirmed_client(clid);
4387 if (clp == NULL)
4388 return NULL;
4389
NeilBrowna55370a2005-06-23 22:03:52 -07004390 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4391 clp->cl_name.len, clp->cl_name.data,
4392 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07004395 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07004397 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return crp;
4399 }
4400 }
4401 return NULL;
4402}
4403
4404/*
4405* Called from OPEN. Look for clientid in reclaim list.
4406*/
Al Virob37ad282006-10-19 23:28:59 -07004407__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408nfs4_check_open_reclaim(clientid_t *clid)
4409{
NeilBrowndfc83562005-06-23 22:03:16 -07004410 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411}
4412
NeilBrownac4d8ff22005-06-23 22:03:30 -07004413/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004415int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004416nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004418 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004420 status = nfsd4_init_slabs();
4421 if (status)
4422 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4424 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4425 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4426 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4427 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
Wang Chen02cb2852009-04-24 15:41:57 +08004428 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 }
Marc Eshel5282fd72009-04-03 08:27:52 +03004430 for (i = 0; i < SESSION_HASH_SIZE; i++)
4431 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 for (i = 0; i < FILE_HASH_SIZE; i++) {
4433 INIT_LIST_HEAD(&file_hashtbl[i]);
4434 }
J. Bruce Fields506f2752011-08-11 18:50:18 -04004435 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4436 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
4437 INIT_LIST_HEAD(&open_ownerid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 }
J. Bruce Fieldsb79abad2011-08-22 18:01:43 -04004439 for (i = 0; i < STATEID_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 INIT_LIST_HEAD(&stateid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4442 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4443 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 INIT_LIST_HEAD(&close_lru);
4447 INIT_LIST_HEAD(&client_lru);
4448 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004449 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004450 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07004451}
4452
NeilBrown190e4fb2005-06-23 22:04:25 -07004453static void
4454nfsd4_load_reboot_recovery_data(void)
4455{
4456 int status;
4457
NeilBrown0964a3d2005-06-23 22:04:32 -07004458 nfs4_lock_state();
J. Bruce Fields48483bf2011-08-26 20:40:28 -04004459 nfsd4_init_recdir();
NeilBrown190e4fb2005-06-23 22:04:25 -07004460 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07004461 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07004462 if (status)
4463 printk("NFSD: Failure reading reboot recovery data\n");
4464}
4465
Meelap Shahc2f1a552007-07-17 04:04:39 -07004466/*
4467 * Since the lifetime of a delegation isn't limited to that of an open, a
4468 * client may quite reasonably hang on to a delegation as long as it has
4469 * the inode cached. This becomes an obvious problem the first time a
4470 * client's inode cache approaches the size of the server's total memory.
4471 *
4472 * For now we avoid this problem by imposing a hard limit on the number
4473 * of delegations, which varies according to the server's memory size.
4474 */
4475static void
4476set_max_delegations(void)
4477{
4478 /*
4479 * Allow at most 4 delegations per megabyte of RAM. Quick
4480 * estimates suggest that in the worst case (where every delegation
4481 * is for a different inode), a delegation could take about 1.5K,
4482 * giving a worst case usage of about 6% of memory.
4483 */
4484 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4485}
4486
NeilBrownac4d8ff22005-06-23 22:03:30 -07004487/* initialization to perform when the nfsd service is started: */
4488
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004489static int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004490__nfs4_state_start(void)
4491{
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004492 int ret;
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004495 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07004496 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004497 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004498 ret = set_callback_cred();
4499 if (ret)
4500 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004501 laundry_wq = create_singlethread_workqueue("nfsd4");
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004502 if (laundry_wq == NULL)
4503 return -ENOMEM;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004504 ret = nfsd4_create_callback_queue();
4505 if (ret)
4506 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004507 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004508 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004509 return 0;
4510out_free_laundry:
4511 destroy_workqueue(laundry_wq);
4512 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513}
4514
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004515int
NeilBrown76a35502005-06-23 22:03:26 -07004516nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517{
NeilBrown190e4fb2005-06-23 22:04:25 -07004518 nfsd4_load_reboot_recovery_data();
Jeff Layton4ad9a342010-07-19 16:50:04 -04004519 return __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522static void
4523__nfs4_state_shutdown(void)
4524{
4525 int i;
4526 struct nfs4_client *clp = NULL;
4527 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 struct list_head *pos, *next, reaplist;
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4531 while (!list_empty(&conf_id_hashtbl[i])) {
4532 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4533 expire_client(clp);
4534 }
4535 while (!list_empty(&unconf_str_hashtbl[i])) {
4536 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4537 expire_client(clp);
4538 }
4539 }
4540 INIT_LIST_HEAD(&reaplist);
4541 spin_lock(&recall_lock);
4542 list_for_each_safe(pos, next, &del_recall_lru) {
4543 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4544 list_move(&dp->dl_recall_lru, &reaplist);
4545 }
4546 spin_unlock(&recall_lock);
4547 list_for_each_safe(pos, next, &reaplist) {
4548 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4549 list_del_init(&dp->dl_recall_lru);
4550 unhash_delegation(dp);
4551 }
4552
NeilBrown190e4fb2005-06-23 22:04:25 -07004553 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554}
4555
4556void
4557nfs4_state_shutdown(void)
4558{
Tejun Heoafe2c512010-12-14 16:21:17 +01004559 cancel_delayed_work_sync(&laundromat_work);
NeilBrown5e8d5c22006-04-10 22:55:37 -07004560 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06004561 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 nfs4_lock_state();
4563 nfs4_release_reclaim();
4564 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 nfs4_unlock_state();
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004566 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}