blob: cb36c9a2e8ca3987d67fe8181da0b8d7f0f9975d [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 Fieldsefc4bb42010-03-02 11:04:06 -050050time_t nfsd4_grace = 90;
NeilBrownfd39ca92005-06-23 22:04:03 -070051static time_t boot_time;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -040052static u32 current_stateid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070053static stateid_t zerostateid; /* bits all 0 */
54static stateid_t onestateid; /* bits all 1 */
Andy Adamsonec6b5d72009-04-03 08:28:28 +030055static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070056
57#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
58#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040061static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
J. Bruce Fields8b671b82009-02-22 14:51:34 -080063/* Locking: */
64
65/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080066static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
J. Bruce Fields8b671b82009-02-22 14:51:34 -080068/*
69 * Currently used for the del_recall_lru and file hash table. In an
70 * effort to decrease the scope of the client_mutex, this spinlock may
71 * eventually cover more:
72 */
73static DEFINE_SPINLOCK(recall_lock);
74
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040075static struct kmem_cache *openowner_slab = NULL;
76static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080077static struct kmem_cache *file_slab = NULL;
78static struct kmem_cache *stateid_slab = NULL;
79static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081void
82nfs4_lock_state(void)
83{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080084 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87void
88nfs4_unlock_state(void)
89{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080090 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93static inline u32
94opaque_hashval(const void *ptr, int nbytes)
95{
96 unsigned char *cptr = (unsigned char *) ptr;
97
98 u32 x = 0;
99 while (nbytes--) {
100 x *= 37;
101 x += *cptr++;
102 }
103 return x;
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static struct list_head del_recall_lru;
107
NeilBrown13cd2182005-06-23 22:03:10 -0700108static inline void
109put_nfs4_file(struct nfs4_file *fi)
110{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800111 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
112 list_del(&fi->fi_hash);
113 spin_unlock(&recall_lock);
114 iput(fi->fi_inode);
115 kmem_cache_free(file_slab, fi);
116 }
NeilBrown13cd2182005-06-23 22:03:10 -0700117}
118
119static inline void
120get_nfs4_file(struct nfs4_file *fi)
121{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800122 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700123}
124
NeilBrownef0f3392006-04-10 22:55:41 -0700125static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700126unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700127
128/*
129 * Open owner state (share locks)
130 */
131
J. Bruce Fields506f2752011-08-11 18:50:18 -0400132/* hash tables for open owners */
133#define OPEN_OWNER_HASH_BITS 8
134#define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS)
135#define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700136
J. Bruce Fields506f2752011-08-11 18:50:18 -0400137static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400138{
139 unsigned int ret;
140
141 ret = opaque_hashval(ownername->data, ownername->len);
142 ret += clientid;
J. Bruce Fields506f2752011-08-11 18:50:18 -0400143 return ret & OPEN_OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400144}
NeilBrownef0f3392006-04-10 22:55:41 -0700145
J. Bruce Fields506f2752011-08-11 18:50:18 -0400146static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700147
148/* hash table for nfs4_file */
149#define FILE_HASH_BITS 8
150#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800151
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400152/* hash table for (open)nfs4_ol_stateid */
NeilBrownef0f3392006-04-10 22:55:41 -0700153#define STATEID_HASH_BITS 10
154#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
155#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
156
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400157static unsigned int file_hashval(struct inode *ino)
158{
159 /* XXX: why are we hashing on inode pointer, anyway? */
160 return hash_ptr(ino, FILE_HASH_BITS);
161}
162
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400163static unsigned int stateid_hashval(stateid_t *s)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400164{
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400165 return opaque_hashval(&s->si_opaque, sizeof(stateid_opaque_t)) & STATEID_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400166}
NeilBrownef0f3392006-04-10 22:55:41 -0700167
168static struct list_head file_hashtbl[FILE_HASH_SIZE];
169static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
170
J. Bruce Fields998db522010-08-07 09:21:41 -0400171static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400172{
173 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
174 atomic_inc(&fp->fi_access[oflag]);
175}
176
J. Bruce Fields998db522010-08-07 09:21:41 -0400177static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
178{
179 if (oflag == O_RDWR) {
180 __nfs4_file_get_access(fp, O_RDONLY);
181 __nfs4_file_get_access(fp, O_WRONLY);
182 } else
183 __nfs4_file_get_access(fp, oflag);
184}
185
186static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400187{
188 if (fp->fi_fds[oflag]) {
189 fput(fp->fi_fds[oflag]);
190 fp->fi_fds[oflag] = NULL;
191 }
192}
193
J. Bruce Fields998db522010-08-07 09:21:41 -0400194static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400195{
196 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400197 nfs4_file_put_fd(fp, oflag);
J. Bruce Fields3d02fa22011-09-19 15:07:41 -0400198 /*
199 * It's also safe to get rid of the RDWR open *if*
200 * we no longer have need of the other kind of access
201 * or if we already have the other kind of open:
202 */
203 if (fp->fi_fds[1-oflag]
204 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
205 nfs4_file_put_fd(fp, O_RDWR);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400206 }
207}
208
J. Bruce Fields998db522010-08-07 09:21:41 -0400209static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
210{
211 if (oflag == O_RDWR) {
212 __nfs4_file_put_access(fp, O_RDONLY);
213 __nfs4_file_put_access(fp, O_WRONLY);
214 } else
215 __nfs4_file_put_access(fp, oflag);
216}
217
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400218static inline void hash_stid(struct nfs4_stid *stid)
219{
220 stateid_t *s = &stid->sc_stateid;
221 unsigned int hashval;
222
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400223 hashval = stateid_hashval(s);
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400224 list_add(&stid->sc_hash, &stateid_hashtbl[hashval]);
225}
226
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400227static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
228{
229 stateid_t *s = &stid->sc_stateid;
230
231 stid->sc_type = type;
232 stid->sc_client = cl;
233 s->si_opaque.so_clid = cl->cl_clientid;
234 s->si_opaque.so_id = current_stateid++;
235 /* Will be incremented before return to client: */
236 s->si_generation = 0;
237 hash_stid(stid);
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400241alloc_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 -0700242{
243 struct nfs4_delegation *dp;
244 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400247 /*
248 * Major work on the lease subsystem (for example, to support
249 * calbacks on stat) will be required before we can support
250 * write delegations properly.
251 */
252 if (type != NFS4_OPEN_DELEGATE_READ)
253 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700254 if (fp->fi_had_conflict)
255 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700256 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700257 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700258 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
259 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return dp;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400261 init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
262 /*
263 * delegation seqid's are never incremented. The 4.1 special
264 * meaning of seqid 0 isn't really meaningful, really, but let's
265 * avoid 0 anyway just for consistency and use 1:
266 */
267 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700268 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700269 INIT_LIST_HEAD(&dp->dl_perfile);
270 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 INIT_LIST_HEAD(&dp->dl_recall_lru);
NeilBrown13cd2182005-06-23 22:03:10 -0700272 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 dp->dl_type = type;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500275 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 dp->dl_time = 0;
277 atomic_set(&dp->dl_count, 1);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500278 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return dp;
280}
281
282void
283nfs4_put_delegation(struct nfs4_delegation *dp)
284{
285 if (atomic_dec_and_test(&dp->dl_count)) {
286 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700287 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700288 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700289 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291}
292
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500293static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500295 if (atomic_dec_and_test(&fp->fi_delegees)) {
296 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
297 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400298 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500299 fp->fi_deleg_file = NULL;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303/* Called under the state lock. */
304static void
305unhash_delegation(struct nfs4_delegation *dp)
306{
J. Bruce Fieldsf459e452011-09-09 09:06:12 -0400307 list_del_init(&dp->dl_stid.sc_hash);
NeilBrownea1da632005-06-23 22:04:17 -0700308 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500310 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 list_del_init(&dp->dl_recall_lru);
312 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500313 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 nfs4_put_delegation(dp);
315}
316
317/*
318 * SETCLIENTID state
319 */
320
Benny Halevy36acb662010-05-12 00:13:04 +0300321/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300322static DEFINE_SPINLOCK(client_lock);
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/* Hash tables for nfs4_clientid state */
325#define CLIENT_HASH_BITS 4
326#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
327#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
328
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400329static unsigned int clientid_hashval(u32 id)
330{
331 return id & CLIENT_HASH_MASK;
332}
333
334static unsigned int clientstr_hashval(const char *name)
335{
336 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/*
340 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
341 * used in reboot/reset lease grace period processing
342 *
343 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
344 * setclientid_confirmed info.
345 *
346 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
347 * setclientid info.
348 *
349 * client_lru holds client queue ordered by nfs4_client.cl_time
350 * for lease renewal.
351 *
352 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
353 * for last close replay.
354 */
355static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
356static int reclaim_str_hashtbl_size = 0;
357static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
358static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
359static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
360static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
361static struct list_head client_lru;
362static struct list_head close_lru;
363
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400364/*
365 * We store the NONE, READ, WRITE, and BOTH bits separately in the
366 * st_{access,deny}_bmap field of the stateid, in order to track not
367 * only what share bits are currently in force, but also what
368 * combinations of share bits previous opens have used. This allows us
369 * to enforce the recommendation of rfc 3530 14.2.19 that the server
370 * return an error if the client attempt to downgrade to a combination
371 * of share bits not explicable by closing some of its previous opens.
372 *
373 * XXX: This enforcement is actually incomplete, since we don't keep
374 * track of access/deny bit combinations; so, e.g., we allow:
375 *
376 * OPEN allow read, deny write
377 * OPEN allow both, deny none
378 * DOWNGRADE allow read, deny none
379 *
380 * which we should reject.
381 */
382static void
383set_access(unsigned int *access, unsigned long bmap) {
384 int i;
385
386 *access = 0;
387 for (i = 1; i < 4; i++) {
388 if (test_bit(i, &bmap))
389 *access |= i;
390 }
391}
392
393static void
394set_deny(unsigned int *deny, unsigned long bmap) {
395 int i;
396
397 *deny = 0;
398 for (i = 0; i < 4; i++) {
399 if (test_bit(i, &bmap))
400 *deny |= i ;
401 }
402}
403
404static int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400405test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400406 unsigned int access, deny;
407
408 set_access(&access, stp->st_access_bmap);
409 set_deny(&deny, stp->st_deny_bmap);
410 if ((access & open->op_share_deny) || (deny & open->op_share_access))
411 return 0;
412 return 1;
413}
414
415static int nfs4_access_to_omode(u32 access)
416{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400417 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400418 case NFS4_SHARE_ACCESS_READ:
419 return O_RDONLY;
420 case NFS4_SHARE_ACCESS_WRITE:
421 return O_WRONLY;
422 case NFS4_SHARE_ACCESS_BOTH:
423 return O_RDWR;
424 }
425 BUG();
426}
427
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400428static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500429{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500430 list_del(&stp->st_perfile);
431 list_del(&stp->st_perstateowner);
432}
433
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400434static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500435{
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400436 int i;
J. Bruce Fields0997b172011-03-02 18:01:35 -0500437
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800438 if (stp->st_access_bmap) {
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400439 for (i = 1; i < 4; i++) {
440 if (test_bit(i, &stp->st_access_bmap))
441 nfs4_file_put_access(stp->st_file,
442 nfs4_access_to_omode(i));
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400443 __clear_bit(i, &stp->st_access_bmap);
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400444 }
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800445 }
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400446 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400447 stp->st_file = NULL;
448}
449
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400450static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400451{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500452 kmem_cache_free(stateid_slab, stp);
453}
454
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400455static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500456{
457 struct file *file;
458
459 unhash_generic_stateid(stp);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400460 list_del(&stp->st_stid.sc_hash);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500461 file = find_any_file(stp->st_file);
462 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400463 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400464 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500465 free_generic_stateid(stp);
466}
467
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400468static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500469{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400470 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500471
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400472 list_del(&lo->lo_owner.so_strhash);
473 list_del(&lo->lo_perstateid);
474 while (!list_empty(&lo->lo_owner.so_stateids)) {
475 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400476 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500477 release_lock_stateid(stp);
478 }
479}
480
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400481static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500482{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400483 unhash_lockowner(lo);
484 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500485}
486
487static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400488release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500489{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400490 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500491
492 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400493 lo = list_entry(open_stp->st_lockowners.next,
494 struct nfs4_lockowner, lo_perstateid);
495 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500496 }
497}
498
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400499static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500500{
501 unhash_generic_stateid(stp);
502 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400503 close_generic_stateid(stp);
504}
505
506static void release_open_stateid(struct nfs4_ol_stateid *stp)
507{
508 unhash_open_stateid(stp);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400509 list_del(&stp->st_stid.sc_hash);
J. Bruce Fields22839632009-01-11 14:27:17 -0500510 free_generic_stateid(stp);
511}
512
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400513static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500514{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400515 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500516
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400517 list_del(&oo->oo_owner.so_strhash);
518 list_del(&oo->oo_perclient);
519 while (!list_empty(&oo->oo_owner.so_stateids)) {
520 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400521 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500522 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500523 }
524}
525
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400526static void release_last_closed_stateid(struct nfs4_openowner *oo)
527{
528 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
529
530 if (s) {
531 list_del_init(&s->st_stid.sc_hash);
532 free_generic_stateid(s);
533 oo->oo_last_closed_stid = NULL;
534 }
535}
536
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400537static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500538{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400539 unhash_openowner(oo);
540 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400541 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400542 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500543}
544
Marc Eshel5282fd72009-04-03 08:27:52 +0300545#define SESSION_HASH_SIZE 512
546static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
547
548static inline int
549hash_sessionid(struct nfs4_sessionid *sessionid)
550{
551 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
552
553 return sid->sequence % SESSION_HASH_SIZE;
554}
555
556static inline void
557dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
558{
559 u32 *ptr = (u32 *)(&sessionid->data[0]);
560 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
561}
562
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300563static void
564gen_sessionid(struct nfsd4_session *ses)
565{
566 struct nfs4_client *clp = ses->se_client;
567 struct nfsd4_sessionid *sid;
568
569 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
570 sid->clientid = clp->cl_clientid;
571 sid->sequence = current_sessionid++;
572 sid->reserved = 0;
573}
574
575/*
Andy Adamsona6496372009-08-28 08:45:01 -0400576 * The protocol defines ca_maxresponssize_cached to include the size of
577 * the rpc header, but all we need to cache is the data starting after
578 * the end of the initial SEQUENCE operation--the rest we regenerate
579 * each time. Therefore we can advertise a ca_maxresponssize_cached
580 * value that is the number of bytes in our cache plus a few additional
581 * bytes. In order to stay on the safe side, and not promise more than
582 * we can cache, those additional bytes must be the minimum possible: 24
583 * bytes of rpc header (xid through accept state, with AUTH_NULL
584 * verifier), 12 for the compound header (with zero-length tag), and 44
585 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300586 */
Andy Adamsona6496372009-08-28 08:45:01 -0400587#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
588
Andy Adamson557ce262009-08-28 08:45:04 -0400589static void
590free_session_slots(struct nfsd4_session *ses)
591{
592 int i;
593
594 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
595 kfree(ses->se_slots[i]);
596}
597
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400598/*
599 * We don't actually need to cache the rpc and session headers, so we
600 * can allocate a little less for each slot:
601 */
602static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
603{
604 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
605}
606
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400607static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300608{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400609 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
610 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300611
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400612 return size;
613}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300614
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400615/*
616 * XXX: If we run out of reserved DRC memory we could (up to a point)
617 * re-negotiate active sessions and reduce their slot usage to make
618 * rooom for new connections. For now we just fail the create session.
619 */
620static int nfsd4_get_drc_mem(int slotsize, u32 num)
621{
622 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300623
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400624 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400625
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400626 spin_lock(&nfsd_drc_lock);
627 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
628 nfsd_drc_max_mem - nfsd_drc_mem_used);
629 num = min_t(int, num, avail / slotsize);
630 nfsd_drc_mem_used += num * slotsize;
631 spin_unlock(&nfsd_drc_lock);
632
633 return num;
634}
635
636static void nfsd4_put_drc_mem(int slotsize, int num)
637{
638 spin_lock(&nfsd_drc_lock);
639 nfsd_drc_mem_used -= slotsize * num;
640 spin_unlock(&nfsd_drc_lock);
641}
642
643static struct nfsd4_session *alloc_session(int slotsize, int numslots)
644{
645 struct nfsd4_session *new;
646 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300647
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400648 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400649 + sizeof(struct nfsd4_session) > PAGE_SIZE);
650 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400651
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400652 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300653 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400654 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400655 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400656 for (i = 0; i < numslots; i++) {
657 mem = sizeof(struct nfsd4_slot) + slotsize;
658 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
659 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400660 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400661 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400662 return new;
663out_free:
664 while (i--)
665 kfree(new->se_slots[i]);
666 kfree(new);
667 return NULL;
668}
669
670static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
671{
672 u32 maxrpc = nfsd_serv->sv_max_mesg;
673
674 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800675 new->maxresp_cached = min_t(u32, req->maxresp_cached,
676 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400677 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
678 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
679 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
680}
681
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400682static void free_conn(struct nfsd4_conn *c)
683{
684 svc_xprt_put(c->cn_xprt);
685 kfree(c);
686}
687
688static void nfsd4_conn_lost(struct svc_xpt_user *u)
689{
690 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
691 struct nfs4_client *clp = c->cn_session->se_client;
692
693 spin_lock(&clp->cl_lock);
694 if (!list_empty(&c->cn_persession)) {
695 list_del(&c->cn_persession);
696 free_conn(c);
697 }
698 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500699 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400700}
701
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400702static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400703{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400704 struct nfsd4_conn *conn;
705
706 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
707 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400708 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400709 svc_xprt_get(rqstp->rq_xprt);
710 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400711 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400712 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
713 return conn;
714}
715
J. Bruce Fields328ead22010-09-29 16:11:06 -0400716static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
717{
718 conn->cn_session = ses;
719 list_add(&conn->cn_persession, &ses->se_conns);
720}
721
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400722static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
723{
724 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400725
726 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400727 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400728 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400729}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400730
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400731static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400732{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400733 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400734 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400735}
736
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400737static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400738{
739 struct nfsd4_conn *conn;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400740 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400741
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400742 conn = alloc_conn(rqstp, dir);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400743 if (!conn)
744 return nfserr_jukebox;
745 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400746 ret = nfsd4_register_conn(conn);
747 if (ret)
748 /* oops; xprt is already down: */
749 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400750 return nfs_ok;
751}
752
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400753static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
754{
755 u32 dir = NFS4_CDFC4_FORE;
756
757 if (ses->se_flags & SESSION4_BACK_CHAN)
758 dir |= NFS4_CDFC4_BACK;
759
760 return nfsd4_new_conn(rqstp, ses, dir);
761}
762
763/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400764static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400765{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400766 struct nfs4_client *clp = s->se_client;
767 struct nfsd4_conn *c;
768
769 spin_lock(&clp->cl_lock);
770 while (!list_empty(&s->se_conns)) {
771 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
772 list_del_init(&c->cn_persession);
773 spin_unlock(&clp->cl_lock);
774
775 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
776 free_conn(c);
777
778 spin_lock(&clp->cl_lock);
779 }
780 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400781}
782
783void free_session(struct kref *kref)
784{
785 struct nfsd4_session *ses;
786 int mem;
787
788 ses = container_of(kref, struct nfsd4_session, se_ref);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400789 nfsd4_del_conns(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400790 spin_lock(&nfsd_drc_lock);
791 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
792 nfsd_drc_mem_used -= mem;
793 spin_unlock(&nfsd_drc_lock);
794 free_session_slots(ses);
795 kfree(ses);
796}
797
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400798static 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 -0400799{
800 struct nfsd4_session *new;
801 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
802 int numslots, slotsize;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400803 int status;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400804 int idx;
805
806 /*
807 * Note decreasing slot size below client's request may
808 * make it difficult for client to function correctly, whereas
809 * decreasing the number of slots will (just?) affect
810 * performance. When short on memory we therefore prefer to
811 * decrease number of slots instead of their size.
812 */
813 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
814 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800815 if (numslots < 1)
816 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400817
818 new = alloc_session(slotsize, numslots);
819 if (!new) {
820 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400821 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400822 }
823 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
Andy Adamson557ce262009-08-28 08:45:04 -0400824
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300825 new->se_client = clp;
826 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300827
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400828 INIT_LIST_HEAD(&new->se_conns);
829
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400830 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300831 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400832 new->se_cb_prog = cses->callback_prog;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300833 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400834 idx = hash_sessionid(&new->se_sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300835 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300836 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400837 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300838 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400839 spin_unlock(&clp->cl_lock);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300840 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300841
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400842 status = nfsd4_new_conn_from_crses(rqstp, new);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400843 /* whoops: benny points out, status is ignored! (err, or bogus) */
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400844 if (status) {
845 free_session(&new->se_ref);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400846 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400847 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400848 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400849 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400850 /*
851 * This is a little silly; with sessions there's no real
852 * use for the callback address. Use the peer address
853 * as a reasonable default for now, but consider fixing
854 * the rpc client not to require an address in the
855 * future:
856 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400857 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
858 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400859 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400860 nfsd4_probe_callback(clp);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400861 return new;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300862}
863
Benny Halevy9089f1b2010-05-12 00:12:26 +0300864/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300865static struct nfsd4_session *
866find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
867{
868 struct nfsd4_session *elem;
869 int idx;
870
871 dump_sessionid(__func__, sessionid);
872 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300873 /* Search in the appropriate list */
874 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300875 if (!memcmp(elem->se_sessionid.data, sessionid->data,
876 NFS4_MAX_SESSIONID_LEN)) {
877 return elem;
878 }
879 }
880
881 dprintk("%s: session not found\n", __func__);
882 return NULL;
883}
884
Benny Halevy9089f1b2010-05-12 00:12:26 +0300885/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300886static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300887unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300888{
889 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400890 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300891 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400892 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +0300893}
894
Benny Halevy36acb662010-05-12 00:13:04 +0300895/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300897renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Benny Halevy07cd4902010-05-12 00:13:41 +0300899 if (is_client_expired(clp)) {
900 dprintk("%s: client (clientid %08x/%08x) already expired\n",
901 __func__,
902 clp->cl_clientid.cl_boot,
903 clp->cl_clientid.cl_id);
904 return;
905 }
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /*
908 * Move client to the end to the LRU list.
909 */
910 dprintk("renewing client (clientid %08x/%08x)\n",
911 clp->cl_clientid.cl_boot,
912 clp->cl_clientid.cl_id);
913 list_move_tail(&clp->cl_lru, &client_lru);
914 clp->cl_time = get_seconds();
915}
916
Benny Halevy36acb662010-05-12 00:13:04 +0300917static inline void
918renew_client(struct nfs4_client *clp)
919{
920 spin_lock(&client_lock);
921 renew_client_locked(clp);
922 spin_unlock(&client_lock);
923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
926static int
927STALE_CLIENTID(clientid_t *clid)
928{
929 if (clid->cl_boot == boot_time)
930 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +0300931 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
932 clid->cl_boot, clid->cl_id, boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 1;
934}
935
936/*
937 * XXX Should we use a slab cache ?
938 * This type of memory management is somewhat inefficient, but we use it
939 * anyway since SETCLIENTID is not a common operation.
940 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500941static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct nfs4_client *clp;
944
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500945 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
946 if (clp == NULL)
947 return NULL;
948 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
949 if (clp->cl_name.data == NULL) {
950 kfree(clp);
951 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500953 memcpy(clp->cl_name.data, name.data, name.len);
954 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return clp;
956}
957
958static inline void
959free_client(struct nfs4_client *clp)
960{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400961 while (!list_empty(&clp->cl_sessions)) {
962 struct nfsd4_session *ses;
963 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
964 se_perclnt);
965 list_del(&ses->se_perclnt);
966 nfsd4_put_session(ses);
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (clp->cl_cred.cr_group_info)
969 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500970 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 kfree(clp->cl_name.data);
972 kfree(clp);
973}
974
Benny Halevyd7682982010-05-12 00:13:54 +0300975void
976release_session_client(struct nfsd4_session *session)
977{
978 struct nfs4_client *clp = session->se_client;
979
980 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
981 return;
982 if (is_client_expired(clp)) {
983 free_client(clp);
984 session->se_client = NULL;
985 } else
986 renew_client_locked(clp);
987 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +0300988}
989
Benny Halevy84d38ac2010-05-12 00:13:16 +0300990/* must be called under the client_lock */
991static inline void
992unhash_client_locked(struct nfs4_client *clp)
993{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400994 struct nfsd4_session *ses;
995
Benny Halevy07cd4902010-05-12 00:13:41 +0300996 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300997 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400998 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400999 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1000 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001001 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004static void
1005expire_client(struct nfs4_client *clp)
1006{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001007 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct list_head reaplist;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 INIT_LIST_HEAD(&reaplist);
1012 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001013 while (!list_empty(&clp->cl_delegations)) {
1014 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001015 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 list_move(&dp->dl_recall_lru, &reaplist);
1017 }
1018 spin_unlock(&recall_lock);
1019 while (!list_empty(&reaplist)) {
1020 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1021 list_del_init(&dp->dl_recall_lru);
1022 unhash_delegation(dp);
1023 }
NeilBrownea1da632005-06-23 22:04:17 -07001024 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001025 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1026 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001028 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001029 if (clp->cl_cb_conn.cb_xprt)
1030 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001031 list_del(&clp->cl_idhash);
1032 list_del(&clp->cl_strhash);
1033 spin_lock(&client_lock);
1034 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +03001035 if (atomic_read(&clp->cl_refcount) == 0)
1036 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001037 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001040static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1041{
1042 memcpy(target->cl_verifier.data, source->data,
1043 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001046static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1049 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1050}
1051
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001052static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 target->cr_uid = source->cr_uid;
1055 target->cr_gid = source->cr_gid;
1056 target->cr_group_info = source->cr_group_info;
1057 get_group_info(target->cr_group_info);
1058}
1059
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001060static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001061{
NeilBrowna55370a2005-06-23 22:03:52 -07001062 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001066same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1067{
1068 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001072same_clid(clientid_t *cl1, clientid_t *cl2)
1073{
1074 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077/* XXX what about NGROUP */
1078static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001079same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1080{
1081 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001084static void gen_clid(struct nfs4_client *clp)
1085{
1086 static u32 current_clientid = 1;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 clp->cl_clientid.cl_boot = boot_time;
1089 clp->cl_clientid.cl_id = current_clientid++;
1090}
1091
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001092static void gen_confirm(struct nfs4_client *clp)
1093{
1094 static u32 i;
1095 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001098 *p++ = get_seconds();
1099 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
J. Bruce Fields4581d142011-09-06 14:56:09 -04001102static int
1103same_stateid(stateid_t *id_one, stateid_t *id_two)
1104{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04001105 return 0 == memcmp(&id_one->si_opaque, &id_two->si_opaque,
1106 sizeof(stateid_opaque_t));
J. Bruce Fields4581d142011-09-06 14:56:09 -04001107}
1108
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001109static struct nfs4_stid *find_stateid(stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001110{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001111 struct nfs4_stid *s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001112 unsigned int hashval;
1113
J. Bruce Fieldsee626a72011-09-11 13:48:41 -04001114 hashval = stateid_hashval(t);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001115 list_for_each_entry(s, &stateid_hashtbl[hashval], sc_hash)
1116 if (same_stateid(&s->sc_stateid, t))
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001117 return s;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001118 return NULL;
1119}
1120
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001121static struct nfs4_stid *find_stateid_by_type(stateid_t *t, char typemask)
1122{
1123 struct nfs4_stid *s;
1124
1125 s = find_stateid(t);
1126 if (!s)
1127 return NULL;
1128 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001129 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001130 return NULL;
1131}
1132
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001133static struct nfs4_ol_stateid *find_ol_stateid_by_type(stateid_t *t, char typemask)
1134{
1135 struct nfs4_stid *s;
1136
1137 s = find_stateid_by_type(t, typemask);
1138 if (!s)
1139 return NULL;
1140 return openlockstateid(s);
1141}
1142
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001143static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1144 struct svc_rqst *rqstp, nfs4_verifier *verf)
1145{
1146 struct nfs4_client *clp;
1147 struct sockaddr *sa = svc_addr(rqstp);
1148 char *princ;
1149
1150 clp = alloc_client(name);
1151 if (clp == NULL)
1152 return NULL;
1153
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001154 INIT_LIST_HEAD(&clp->cl_sessions);
1155
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001156 princ = svc_gss_principal(rqstp);
1157 if (princ) {
1158 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1159 if (clp->cl_principal == NULL) {
1160 free_client(clp);
1161 return NULL;
1162 }
1163 }
1164
1165 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Benny Halevy46583e22010-05-12 00:13:29 +03001166 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001167 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001168 INIT_LIST_HEAD(&clp->cl_idhash);
1169 INIT_LIST_HEAD(&clp->cl_strhash);
1170 INIT_LIST_HEAD(&clp->cl_openowners);
1171 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001172 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba252011-01-10 16:44:41 -05001173 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001174 spin_lock_init(&clp->cl_lock);
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001175 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
Benny Halevy07cd4902010-05-12 00:13:41 +03001176 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001177 clear_bit(0, &clp->cl_cb_slot_busy);
1178 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1179 copy_verf(clp, verf);
1180 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1181 clp->cl_flavor = rqstp->rq_flavor;
1182 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1183 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001184 clp->cl_cb_session = NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001185 return clp;
1186}
1187
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001188static int check_name(struct xdr_netobj name)
1189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (name.len == 0)
1191 return 0;
1192 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001193 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return 0;
1195 }
1196 return 1;
1197}
1198
NeilBrownfd39ca92005-06-23 22:04:03 -07001199static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1201{
1202 unsigned int idhashval;
1203
1204 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1205 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1206 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001207 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
NeilBrownfd39ca92005-06-23 22:04:03 -07001210static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211move_to_confirmed(struct nfs4_client *clp)
1212{
1213 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1214 unsigned int strhashval;
1215
1216 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001217 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07001218 strhashval = clientstr_hashval(clp->cl_recdir);
Benny Halevy328efba2010-05-12 00:12:51 +03001219 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 renew_client(clp);
1221}
1222
1223static struct nfs4_client *
1224find_confirmed_client(clientid_t *clid)
1225{
1226 struct nfs4_client *clp;
1227 unsigned int idhashval = clientid_hashval(clid->cl_id);
1228
1229 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001230 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return clp;
1232 }
1233 return NULL;
1234}
1235
1236static struct nfs4_client *
1237find_unconfirmed_client(clientid_t *clid)
1238{
1239 struct nfs4_client *clp;
1240 unsigned int idhashval = clientid_hashval(clid->cl_id);
1241
1242 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001243 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return clp;
1245 }
1246 return NULL;
1247}
1248
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001249static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001250{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001251 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001252}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001253
NeilBrown28ce6052005-06-23 22:03:56 -07001254static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001255find_confirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001256{
1257 struct nfs4_client *clp;
1258
1259 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001260 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001261 return clp;
1262 }
1263 return NULL;
1264}
1265
1266static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001267find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001268{
1269 struct nfs4_client *clp;
1270
1271 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001272 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001273 return clp;
1274 }
1275 return NULL;
1276}
1277
NeilBrownfd39ca92005-06-23 22:04:03 -07001278static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001279gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001281 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001282 struct sockaddr *sa = svc_addr(rqstp);
1283 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001284 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Jeff Layton7077ecb2009-08-14 12:57:58 -04001286 /* Currently, we only support tcp and tcp6 for the callback channel */
1287 if (se->se_callback_netid_len == 3 &&
1288 !memcmp(se->se_callback_netid_val, "tcp", 3))
1289 expected_family = AF_INET;
1290 else if (se->se_callback_netid_len == 4 &&
1291 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1292 expected_family = AF_INET6;
1293 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto out_err;
1295
J. Bruce Fields07263f12010-05-31 19:09:40 -04001296 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001297 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001298 (struct sockaddr *)&conn->cb_addr,
1299 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001300
J. Bruce Fields07263f12010-05-31 19:09:40 -04001301 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001303
J. Bruce Fields07263f12010-05-31 19:09:40 -04001304 if (conn->cb_addr.ss_family == AF_INET6)
1305 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001306
J. Bruce Fields07263f12010-05-31 19:09:40 -04001307 conn->cb_prog = se->se_callback_prog;
1308 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001309 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return;
1311out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001312 conn->cb_addr.ss_family = AF_UNSPEC;
1313 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001314 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 "will not receive delegations\n",
1316 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return;
1319}
1320
Andy Adamson074fe892009-04-03 08:28:15 +03001321/*
Andy Adamson557ce262009-08-28 08:45:04 -04001322 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001323 */
1324void
1325nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1326{
Andy Adamson557ce262009-08-28 08:45:04 -04001327 struct nfsd4_slot *slot = resp->cstate.slot;
1328 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001329
Andy Adamson557ce262009-08-28 08:45:04 -04001330 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001331
Andy Adamson557ce262009-08-28 08:45:04 -04001332 slot->sl_opcnt = resp->opcnt;
1333 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001334
1335 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001336 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001337 return;
1338 }
Andy Adamson557ce262009-08-28 08:45:04 -04001339 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1340 base = (char *)resp->cstate.datap -
1341 (char *)resp->xbuf->head[0].iov_base;
1342 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1343 slot->sl_datalen))
1344 WARN("%s: sessions DRC could not cache compound\n", __func__);
1345 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001346}
1347
1348/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001349 * Encode the replay sequence operation from the slot values.
1350 * If cachethis is FALSE encode the uncached rep error on the next
1351 * operation which sets resp->p and increments resp->opcnt for
1352 * nfs4svc_encode_compoundres.
1353 *
Andy Adamson074fe892009-04-03 08:28:15 +03001354 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001355static __be32
1356nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1357 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001358{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001359 struct nfsd4_op *op;
1360 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001361
Andy Adamsonabfabf82009-07-23 19:02:18 -04001362 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
Andy Adamson557ce262009-08-28 08:45:04 -04001363 resp->opcnt, resp->cstate.slot->sl_cachethis);
Andy Adamsonabfabf82009-07-23 19:02:18 -04001364
1365 /* Encode the replayed sequence operation */
1366 op = &args->ops[resp->opcnt - 1];
1367 nfsd4_encode_operation(resp, op);
1368
1369 /* Return nfserr_retry_uncached_rep in next operation. */
Andy Adamson557ce262009-08-28 08:45:04 -04001370 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001371 op = &args->ops[resp->opcnt++];
1372 op->status = nfserr_retry_uncached_rep;
1373 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001374 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001375 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001376}
1377
1378/*
Andy Adamson557ce262009-08-28 08:45:04 -04001379 * The sequence operation is not cached because we can use the slot and
1380 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001381 */
1382__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001383nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1384 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001385{
Andy Adamson557ce262009-08-28 08:45:04 -04001386 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001387 __be32 status;
1388
Andy Adamson557ce262009-08-28 08:45:04 -04001389 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001390
Andy Adamsonabfabf82009-07-23 19:02:18 -04001391 /* Either returns 0 or nfserr_retry_uncached */
1392 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1393 if (status == nfserr_retry_uncached_rep)
1394 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001395
Andy Adamson557ce262009-08-28 08:45:04 -04001396 /* The sequence operation has been encoded, cstate->datap set. */
1397 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001398
Andy Adamson557ce262009-08-28 08:45:04 -04001399 resp->opcnt = slot->sl_opcnt;
1400 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1401 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001402
1403 return status;
1404}
1405
Andy Adamson0733d212009-04-03 08:28:01 +03001406/*
1407 * Set the exchange_id flags returned by the server.
1408 */
1409static void
1410nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1411{
1412 /* pNFS is not supported */
1413 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1414
1415 /* Referrals are supported, Migration is not. */
1416 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1417
1418 /* set the wire flags to return to client. */
1419 clid->flags = new->cl_exchange_flags;
1420}
1421
Al Virob37ad282006-10-19 23:28:59 -07001422__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001423nfsd4_exchange_id(struct svc_rqst *rqstp,
1424 struct nfsd4_compound_state *cstate,
1425 struct nfsd4_exchange_id *exid)
1426{
Andy Adamson0733d212009-04-03 08:28:01 +03001427 struct nfs4_client *unconf, *conf, *new;
1428 int status;
1429 unsigned int strhashval;
1430 char dname[HEXDIR_LEN];
Jeff Layton363168b2009-08-14 12:57:56 -04001431 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001432 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001433 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamson0733d212009-04-03 08:28:01 +03001434
Jeff Layton363168b2009-08-14 12:57:56 -04001435 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001436 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001437 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001438 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001439 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001440
1441 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1442 return nfserr_inval;
1443
1444 /* Currently only support SP4_NONE */
1445 switch (exid->spa_how) {
1446 case SP4_NONE:
1447 break;
1448 case SP4_SSV:
J. Bruce Fields044bc1d2010-11-12 14:36:06 -05001449 return nfserr_serverfault;
Andy Adamson0733d212009-04-03 08:28:01 +03001450 default:
1451 BUG(); /* checked by xdr code */
1452 case SP4_MACH_CRED:
1453 return nfserr_serverfault; /* no excuse :-/ */
1454 }
1455
1456 status = nfs4_make_rec_clidname(dname, &exid->clname);
1457
1458 if (status)
1459 goto error;
1460
1461 strhashval = clientstr_hashval(dname);
1462
1463 nfs4_lock_state();
1464 status = nfs_ok;
1465
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001466 conf = find_confirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001467 if (conf) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001468 if (!clp_used_exchangeid(conf)) {
1469 status = nfserr_clid_inuse; /* XXX: ? */
1470 goto out;
1471 }
Andy Adamson0733d212009-04-03 08:28:01 +03001472 if (!same_verf(&verf, &conf->cl_verifier)) {
1473 /* 18.35.4 case 8 */
1474 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1475 status = nfserr_not_same;
1476 goto out;
1477 }
1478 /* Client reboot: destroy old state */
1479 expire_client(conf);
1480 goto out_new;
1481 }
1482 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1483 /* 18.35.4 case 9 */
1484 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1485 status = nfserr_perm;
1486 goto out;
1487 }
1488 expire_client(conf);
1489 goto out_new;
1490 }
Andy Adamson0733d212009-04-03 08:28:01 +03001491 /*
1492 * Set bit when the owner id and verifier map to an already
1493 * confirmed client id (18.35.3).
1494 */
1495 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1496
1497 /*
1498 * Falling into 18.35.4 case 2, possible router replay.
1499 * Leave confirmed record intact and return same result.
1500 */
1501 copy_verf(conf, &verf);
1502 new = conf;
1503 goto out_copy;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001504 }
1505
1506 /* 18.35.4 case 7 */
1507 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1508 status = nfserr_noent;
1509 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001510 }
1511
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001512 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001513 if (unconf) {
1514 /*
1515 * Possible retry or client restart. Per 18.35.4 case 4,
1516 * a new unconfirmed record should be generated regardless
1517 * of whether any properties have changed.
1518 */
1519 expire_client(unconf);
1520 }
1521
1522out_new:
1523 /* Normal case */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001524 new = create_client(exid->clname, dname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001525 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001526 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001527 goto out;
1528 }
1529
Andy Adamson0733d212009-04-03 08:28:01 +03001530 gen_clid(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001531 add_to_unconfirmed(new, strhashval);
1532out_copy:
1533 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1534 exid->clientid.cl_id = new->cl_clientid.cl_id;
1535
Andy Adamson38eb76a2009-04-03 08:28:32 +03001536 exid->seqid = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001537 nfsd4_set_ex_flags(new, exid);
1538
1539 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001540 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001541 status = nfs_ok;
1542
1543out:
1544 nfs4_unlock_state();
1545error:
1546 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1547 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001548}
1549
Benny Halevyb85d4c02009-04-03 08:28:08 +03001550static int
Andy Adamson88e588d2009-07-23 19:02:15 -04001551check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001552{
Andy Adamson88e588d2009-07-23 19:02:15 -04001553 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1554 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001555
1556 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001557 if (slot_inuse) {
1558 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001559 return nfserr_jukebox;
1560 else
1561 return nfserr_seq_misordered;
1562 }
1563 /* Normal */
Andy Adamson88e588d2009-07-23 19:02:15 -04001564 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001565 return nfs_ok;
1566 /* Replay */
Andy Adamson88e588d2009-07-23 19:02:15 -04001567 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001568 return nfserr_replay_cache;
1569 /* Wraparound */
Andy Adamson88e588d2009-07-23 19:02:15 -04001570 if (seqid == 1 && (slot_seqid + 1) == 0)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001571 return nfs_ok;
1572 /* Misordered replay or misordered new request */
1573 return nfserr_seq_misordered;
1574}
1575
Andy Adamson49557cc2009-07-23 19:02:16 -04001576/*
1577 * Cache the create session result into the create session single DRC
1578 * slot cache by saving the xdr structure. sl_seqid has been set.
1579 * Do this for solo or embedded create session operations.
1580 */
1581static void
1582nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1583 struct nfsd4_clid_slot *slot, int nfserr)
1584{
1585 slot->sl_status = nfserr;
1586 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1587}
1588
1589static __be32
1590nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1591 struct nfsd4_clid_slot *slot)
1592{
1593 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1594 return slot->sl_status;
1595}
1596
Mi Jinlong1b74c252011-07-14 14:50:17 +08001597#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1598 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1599 1 + /* MIN tag is length with zero, only length */ \
1600 3 + /* version, opcount, opcode */ \
1601 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1602 /* seqid, slotID, slotID, cache */ \
1603 4 ) * sizeof(__be32))
1604
1605#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1606 2 + /* verifier: AUTH_NULL, length 0 */\
1607 1 + /* status */ \
1608 1 + /* MIN tag is length with zero, only length */ \
1609 3 + /* opcount, opcode, opstatus*/ \
1610 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1611 /* seqid, slotID, slotID, slotID, status */ \
1612 5 ) * sizeof(__be32))
1613
1614static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1615{
1616 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1617 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1618}
1619
Andy Adamson069b6ad2009-04-03 08:27:58 +03001620__be32
1621nfsd4_create_session(struct svc_rqst *rqstp,
1622 struct nfsd4_compound_state *cstate,
1623 struct nfsd4_create_session *cr_ses)
1624{
Jeff Layton363168b2009-08-14 12:57:56 -04001625 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001626 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001627 struct nfsd4_session *new;
Andy Adamson49557cc2009-07-23 19:02:16 -04001628 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001629 bool confirm_me = false;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001630 int status = 0;
1631
Mi Jinlonga62573d2011-03-23 17:57:07 +08001632 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1633 return nfserr_inval;
1634
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001635 nfs4_lock_state();
1636 unconf = find_unconfirmed_client(&cr_ses->clientid);
1637 conf = find_confirmed_client(&cr_ses->clientid);
1638
1639 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001640 cs_slot = &conf->cl_cs_slot;
1641 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001642 if (status == nfserr_replay_cache) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001643 dprintk("Got a create_session replay! seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001644 cs_slot->sl_seqid);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001645 /* Return the cached reply status */
Andy Adamson49557cc2009-07-23 19:02:16 -04001646 status = nfsd4_replay_create_session(cr_ses, cs_slot);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001647 goto out;
Andy Adamson49557cc2009-07-23 19:02:16 -04001648 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001649 status = nfserr_seq_misordered;
1650 dprintk("Sequence misordered!\n");
1651 dprintk("Expected seqid= %d but got seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001652 cs_slot->sl_seqid, cr_ses->seqid);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001653 goto out;
1654 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001655 } else if (unconf) {
1656 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001657 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001658 status = nfserr_clid_inuse;
1659 goto out;
1660 }
1661
Andy Adamson49557cc2009-07-23 19:02:16 -04001662 cs_slot = &unconf->cl_cs_slot;
1663 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001664 if (status) {
1665 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001666 status = nfserr_seq_misordered;
J. Bruce Fieldscd5b8142010-10-02 17:03:35 -04001667 goto out;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001668 }
1669
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001670 confirm_me = true;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001671 conf = unconf;
1672 } else {
1673 status = nfserr_stale_clientid;
1674 goto out;
1675 }
1676
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001677 /*
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001678 * XXX: we should probably set this at creation time, and check
1679 * for consistent minorversion use throughout:
1680 */
1681 conf->cl_minorversion = 1;
1682 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001683 * We do not support RDMA or persistent sessions
1684 */
1685 cr_ses->flags &= ~SESSION4_PERSIST;
1686 cr_ses->flags &= ~SESSION4_RDMA;
1687
Mi Jinlong1b74c252011-07-14 14:50:17 +08001688 status = nfserr_toosmall;
1689 if (check_forechannel_attrs(cr_ses->fore_channel))
1690 goto out;
1691
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001692 status = nfserr_jukebox;
1693 new = alloc_init_session(rqstp, conf, cr_ses);
1694 if (!new)
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001695 goto out;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001696 status = nfs_ok;
1697 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001698 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001699 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1700 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001701 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001702 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001703
Andy Adamson49557cc2009-07-23 19:02:16 -04001704 /* cache solo and embedded create sessions under the state lock */
1705 nfsd4_cache_create_session(cr_ses, cs_slot, status);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001706 if (confirm_me)
1707 move_to_confirmed(conf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001708out:
1709 nfs4_unlock_state();
1710 dprintk("%s returns %d\n", __func__, ntohl(status));
1711 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001712}
1713
J. Bruce Fields57716352010-04-21 12:27:19 -04001714static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1715{
1716 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1717 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1718
1719 return argp->opcnt == resp->opcnt;
1720}
1721
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001722static __be32 nfsd4_map_bcts_dir(u32 *dir)
1723{
1724 switch (*dir) {
1725 case NFS4_CDFC4_FORE:
1726 case NFS4_CDFC4_BACK:
1727 return nfs_ok;
1728 case NFS4_CDFC4_FORE_OR_BOTH:
1729 case NFS4_CDFC4_BACK_OR_BOTH:
1730 *dir = NFS4_CDFC4_BOTH;
1731 return nfs_ok;
1732 };
1733 return nfserr_inval;
1734}
1735
1736__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1737 struct nfsd4_compound_state *cstate,
1738 struct nfsd4_bind_conn_to_session *bcts)
1739{
1740 __be32 status;
1741
1742 if (!nfsd4_last_compound_op(rqstp))
1743 return nfserr_not_only_op;
1744 spin_lock(&client_lock);
1745 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1746 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1747 * client_lock iself: */
1748 if (cstate->session) {
1749 nfsd4_get_session(cstate->session);
1750 atomic_inc(&cstate->session->se_client->cl_refcount);
1751 }
1752 spin_unlock(&client_lock);
1753 if (!cstate->session)
1754 return nfserr_badsession;
1755
1756 status = nfsd4_map_bcts_dir(&bcts->dir);
Bryan Schumaker1db2b9d2011-04-27 15:47:15 -04001757 if (!status)
1758 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1759 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001760}
1761
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001762static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1763{
1764 if (!session)
1765 return 0;
1766 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1767}
1768
Andy Adamson069b6ad2009-04-03 08:27:58 +03001769__be32
1770nfsd4_destroy_session(struct svc_rqst *r,
1771 struct nfsd4_compound_state *cstate,
1772 struct nfsd4_destroy_session *sessionid)
1773{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001774 struct nfsd4_session *ses;
1775 u32 status = nfserr_badsession;
1776
1777 /* Notes:
1778 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1779 * - Should we return nfserr_back_chan_busy if waiting for
1780 * callbacks on to-be-destroyed session?
1781 * - Do we need to clear any callback info from previous session?
1782 */
1783
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001784 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001785 if (!nfsd4_last_compound_op(r))
1786 return nfserr_not_only_op;
1787 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001788 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001789 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001790 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1791 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001792 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001793 goto out;
1794 }
1795
1796 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001797 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001798
Benny Halevyab707e152010-05-12 00:14:06 +03001799 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001800 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001801 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001802
1803 nfsd4_del_conns(ses);
1804
Benny Halevye10e0cf2009-04-03 08:28:38 +03001805 nfsd4_put_session(ses);
1806 status = nfs_ok;
1807out:
1808 dprintk("%s returns %d\n", __func__, ntohl(status));
1809 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001810}
1811
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001812static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001813{
1814 struct nfsd4_conn *c;
1815
1816 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001817 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001818 return c;
1819 }
1820 }
1821 return NULL;
1822}
1823
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001824static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001825{
1826 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001827 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001828 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001829
1830 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001831 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001832 if (c) {
1833 spin_unlock(&clp->cl_lock);
1834 free_conn(new);
1835 return;
1836 }
1837 __nfsd4_hash_conn(new, ses);
1838 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001839 ret = nfsd4_register_conn(new);
1840 if (ret)
1841 /* oops; xprt is already down: */
1842 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001843 return;
1844}
1845
Mi Jinlong868b89c2011-04-27 09:09:58 +08001846static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1847{
1848 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1849
1850 return args->opcnt > session->se_fchannel.maxops;
1851}
1852
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001853static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1854 struct nfsd4_session *session)
1855{
1856 struct xdr_buf *xb = &rqstp->rq_arg;
1857
1858 return xb->len > session->se_fchannel.maxreq_sz;
1859}
1860
Andy Adamson069b6ad2009-04-03 08:27:58 +03001861__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001862nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001863 struct nfsd4_compound_state *cstate,
1864 struct nfsd4_sequence *seq)
1865{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001866 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001867 struct nfsd4_session *session;
1868 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001869 struct nfsd4_conn *conn;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001870 int status;
1871
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001872 if (resp->opcnt != 1)
1873 return nfserr_sequence_pos;
1874
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001875 /*
1876 * Will be either used or freed by nfsd4_sequence_check_conn
1877 * below.
1878 */
1879 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1880 if (!conn)
1881 return nfserr_jukebox;
1882
Benny Halevy9089f1b2010-05-12 00:12:26 +03001883 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001884 status = nfserr_badsession;
1885 session = find_in_sessionid_hashtbl(&seq->sessionid);
1886 if (!session)
1887 goto out;
1888
Mi Jinlong868b89c2011-04-27 09:09:58 +08001889 status = nfserr_too_many_ops;
1890 if (nfsd4_session_too_many_ops(rqstp, session))
1891 goto out;
1892
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001893 status = nfserr_req_too_big;
1894 if (nfsd4_request_too_big(rqstp, session))
1895 goto out;
1896
Benny Halevyb85d4c02009-04-03 08:28:08 +03001897 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03001898 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001899 goto out;
1900
Andy Adamson557ce262009-08-28 08:45:04 -04001901 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03001902 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1903
Andy Adamsona8dfdae2009-08-28 08:45:02 -04001904 /* We do not negotiate the number of slots yet, so set the
1905 * maxslots to the session maxreqs which is used to encode
1906 * sr_highest_slotid and the sr_target_slot id to maxslots */
1907 seq->maxslots = session->se_fchannel.maxreqs;
1908
Andy Adamson88e588d2009-07-23 19:02:15 -04001909 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001910 if (status == nfserr_replay_cache) {
1911 cstate->slot = slot;
1912 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001913 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04001914 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03001915 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03001916 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001917 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001918 }
1919 if (status)
1920 goto out;
1921
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001922 nfsd4_sequence_check_conn(conn, session);
1923 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001924
Benny Halevyb85d4c02009-04-03 08:28:08 +03001925 /* Success! bump slot seqid */
1926 slot->sl_inuse = true;
1927 slot->sl_seqid = seq->seqid;
Andy Adamson557ce262009-08-28 08:45:04 -04001928 slot->sl_cachethis = seq->cachethis;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001929
1930 cstate->slot = slot;
1931 cstate->session = session;
1932
Benny Halevyb85d4c02009-04-03 08:28:08 +03001933out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04001934 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001935 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001936 struct nfs4_client *clp = session->se_client;
1937
Benny Halevy36acb662010-05-12 00:13:04 +03001938 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001939 atomic_inc(&clp->cl_refcount);
1940 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1941 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001942 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001943 kfree(conn);
Benny Halevy36acb662010-05-12 00:13:04 +03001944 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001945 dprintk("%s: return %d\n", __func__, ntohl(status));
1946 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001947}
1948
1949__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001950nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1951{
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001952 int status = 0;
1953
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001954 if (rc->rca_one_fs) {
1955 if (!cstate->current_fh.fh_dentry)
1956 return nfserr_nofilehandle;
1957 /*
1958 * We don't take advantage of the rca_one_fs case.
1959 * That's OK, it's optional, we can safely ignore it.
1960 */
1961 return nfs_ok;
1962 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001963
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001964 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001965 status = nfserr_complete_already;
1966 if (cstate->session->se_client->cl_firststate)
1967 goto out;
1968
1969 status = nfserr_stale_clientid;
1970 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001971 /*
1972 * The following error isn't really legal.
1973 * But we only get here if the client just explicitly
1974 * destroyed the client. Surely it no longer cares what
1975 * error it gets back on an operation for the dead
1976 * client.
1977 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001978 goto out;
1979
1980 status = nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001981 nfsd4_create_clid_dir(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001982out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001983 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001984 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001985}
1986
1987__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001988nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1989 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 struct xdr_netobj clname = {
1992 .len = setclid->se_namelen,
1993 .data = setclid->se_name,
1994 };
1995 nfs4_verifier clverifier = setclid->se_verf;
1996 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07001997 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07001998 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -07001999 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -07002002 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
NeilBrowna55370a2005-06-23 22:03:52 -07002004 status = nfs4_make_rec_clidname(dname, &clname);
2005 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07002006 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07002007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /*
2009 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2010 * We get here on a DRC miss.
2011 */
2012
NeilBrowna55370a2005-06-23 22:03:52 -07002013 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 nfs4_lock_state();
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002016 conf = find_confirmed_client_by_str(dname, strhashval);
NeilBrown28ce6052005-06-23 22:03:56 -07002017 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002018 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002020 if (clp_used_exchangeid(conf))
2021 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002022 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002023 char addr_str[INET6_ADDRSTRLEN];
2024 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2025 sizeof(addr_str));
2026 dprintk("NFSD: setclientid: string in use by client "
2027 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 goto out;
2029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002031 /*
2032 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
2033 * has a description of SETCLIENTID request processing consisting
2034 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2035 */
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002036 unconf = find_unconfirmed_client_by_str(dname, strhashval);
J. Bruce Fields3e772462011-08-10 19:07:33 -04002037 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002039 /*
2040 * RFC 3530 14.2.33 CASE 4:
2041 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 */
2043 if (unconf)
2044 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002045 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002046 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002049 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002051 * RFC 3530 14.2.33 CASE 1:
2052 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07002054 if (unconf) {
2055 /* Note this is removing unconfirmed {*x***},
2056 * which is stronger than RFC recommended {vxc**}.
2057 * This has the advantage that there is at most
2058 * one {*x***} in either list at any time.
2059 */
2060 expire_client(unconf);
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 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 } else if (!unconf) {
2067 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002068 * RFC 3530 14.2.33 CASE 2:
2069 * probable client reboot; state will be removed if
2070 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002072 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002073 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05002076 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002077 /*
2078 * RFC 3530 14.2.33 CASE 3:
2079 * probable client reboot; state will be removed if
2080 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 */
2082 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002083 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002084 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002088 /*
2089 * XXX: we should probably set this at creation time, and check
2090 * for consistent minorversion use throughout:
2091 */
2092 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002093 gen_callback(new, setclid, rqstp);
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04002094 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2096 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2097 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2098 status = nfs_ok;
2099out:
2100 nfs4_unlock_state();
2101 return status;
2102}
2103
2104
2105/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002106 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2107 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2108 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 */
Al Virob37ad282006-10-19 23:28:59 -07002110__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002111nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2112 struct nfsd4_compound_state *cstate,
2113 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Jeff Layton363168b2009-08-14 12:57:56 -04002115 struct sockaddr *sa = svc_addr(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -07002116 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2118 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002119 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 if (STALE_CLIENTID(clid))
2122 return nfserr_stale_clientid;
2123 /*
2124 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2125 * We get here on a DRC miss.
2126 */
2127
2128 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002129
2130 conf = find_confirmed_client(clid);
2131 unconf = find_unconfirmed_client(clid);
2132
2133 status = nfserr_clid_inuse;
Jeff Layton363168b2009-08-14 12:57:56 -04002134 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002135 goto out;
Jeff Layton363168b2009-08-14 12:57:56 -04002136 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002137 goto out;
2138
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002139 /*
2140 * section 14.2.34 of RFC 3530 has a description of
2141 * SETCLIENTID_CONFIRM request processing consisting
2142 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2143 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -05002144 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002145 /*
2146 * RFC 3530 14.2.34 CASE 1:
2147 * callback update
2148 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002149 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 status = nfserr_clid_inuse;
2151 else {
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002152 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2153 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002154 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05002158 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002159 /*
2160 * RFC 3530 14.2.34 CASE 2:
2161 * probable retransmitted request; play it safe and
2162 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07002163 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002164 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07002166 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07002168 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002169 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002170 /*
2171 * RFC 3530 14.2.34 CASE 3:
2172 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07002173 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002174 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 status = nfserr_clid_inuse;
2176 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07002177 unsigned int hash =
2178 clientstr_hashval(unconf->cl_recdir);
2179 conf = find_confirmed_client_by_str(unconf->cl_recdir,
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002180 hash);
NeilBrown1a69c172005-06-23 22:04:08 -07002181 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07002182 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002183 expire_client(conf);
2184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07002186 conf = unconf;
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002187 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002188 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002190 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2191 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07002192 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002193 /*
2194 * RFC 3530 14.2.34 CASE 4:
2195 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07002196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07002198 } else {
NeilBrown08e89872005-06-23 22:04:11 -07002199 /* check that we have hit one of the cases...*/
2200 status = nfserr_clid_inuse;
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 nfs4_unlock_state();
2204 return status;
2205}
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/* OPEN Share state helper functions */
2208static inline struct nfs4_file *
2209alloc_init_file(struct inode *ino)
2210{
2211 struct nfs4_file *fp;
2212 unsigned int hashval = file_hashval(ino);
2213
NeilBrowne60d4392005-06-23 22:03:01 -07002214 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2215 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002216 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07002218 INIT_LIST_HEAD(&fp->fi_stateids);
2219 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 fp->fi_inode = igrab(ino);
Meelap Shah47f99402007-07-17 04:04:40 -07002221 fp->fi_had_conflict = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002222 fp->fi_lease = NULL;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002223 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2224 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Pavel Emelyanov47cee542010-05-17 20:00:37 +04002225 spin_lock(&recall_lock);
2226 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2227 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return fp;
2229 }
2230 return NULL;
2231}
2232
2233static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002234nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002235{
NeilBrowne60d4392005-06-23 22:03:01 -07002236 if (*slab == NULL)
2237 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002238 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002239 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002240}
2241
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002242void
NeilBrowne60d4392005-06-23 22:03:01 -07002243nfsd4_free_slabs(void)
2244{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002245 nfsd4_free_slab(&openowner_slab);
2246 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002247 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002248 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002249 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252static int
2253nfsd4_init_slabs(void)
2254{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002255 openowner_slab = kmem_cache_create("nfsd4_openowners",
2256 sizeof(struct nfs4_openowner), 0, 0, NULL);
2257 if (openowner_slab == NULL)
2258 goto out_nomem;
2259 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2260 sizeof(struct nfs4_openowner), 0, 0, NULL);
2261 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002262 goto out_nomem;
2263 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002264 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002265 if (file_slab == NULL)
2266 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002267 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002268 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002269 if (stateid_slab == NULL)
2270 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002271 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002272 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002273 if (deleg_slab == NULL)
2274 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002276out_nomem:
2277 nfsd4_free_slabs();
2278 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2279 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002282void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002284 kfree(oo->oo_owner.so_owner.data);
2285 kmem_cache_free(openowner_slab, oo);
2286}
2287
2288void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2289{
2290 kfree(lo->lo_owner.so_owner.data);
2291 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002294static void init_nfs4_replay(struct nfs4_replay *rp)
2295{
2296 rp->rp_status = nfserr_serverfault;
2297 rp->rp_buflen = 0;
2298 rp->rp_buf = rp->rp_ibuf;
2299}
2300
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002301static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 struct nfs4_stateowner *sop;
2304
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002305 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002306 if (!sop)
2307 return NULL;
2308
2309 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2310 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002311 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002312 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002314 sop->so_owner.len = owner->len;
2315
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002316 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002317 sop->so_client = clp;
2318 init_nfs4_replay(&sop->so_replay);
2319 return sop;
2320}
2321
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002322static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002323{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002324 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2325 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002328static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002330 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002332 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2333 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002335 oo->oo_owner.so_is_open_owner = 1;
2336 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002337 oo->oo_flags = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002338 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002339 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002340 INIT_LIST_HEAD(&oo->oo_close_lru);
2341 hash_openowner(oo, clp, strhashval);
2342 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345static inline void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002346init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002347 struct nfs4_openowner *oo = open->op_openowner;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04002348 struct nfs4_client *clp = oo->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002350 init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
NeilBrownea1da632005-06-23 22:04:17 -07002351 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002352 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002353 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002354 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002355 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 stp->st_access_bmap = 0;
2358 stp->st_deny_bmap = 0;
Andy Adamson84459a12009-04-03 08:28:56 +03002359 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2360 &stp->st_access_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07002362 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
2365static void
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002366move_to_close_lru(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002368 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002370 list_move_tail(&oo->oo_close_lru, &close_lru);
2371 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002375same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2376 clientid_t *clid)
2377{
2378 return (sop->so_owner.len == owner->len) &&
2379 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2380 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002383static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2385{
2386 struct nfs4_stateowner *so = NULL;
2387
J. Bruce Fields506f2752011-08-11 18:50:18 -04002388 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002389 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002390 return container_of(so, struct nfs4_openowner, oo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392 return NULL;
2393}
2394
2395/* search file_hashtbl[] for file */
2396static struct nfs4_file *
2397find_file(struct inode *ino)
2398{
2399 unsigned int hashval = file_hashval(ino);
2400 struct nfs4_file *fp;
2401
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002402 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002404 if (fp->fi_inode == ino) {
2405 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002406 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002410 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return NULL;
2412}
2413
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002414static inline int access_valid(u32 x, u32 minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002415{
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002416 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002417 return 0;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002418 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2419 return 0;
2420 x &= ~NFS4_SHARE_ACCESS_MASK;
2421 if (minorversion && x) {
2422 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2423 return 0;
2424 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2425 return 0;
2426 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2427 }
2428 if (x)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002429 return 0;
2430 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002431}
2432
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002433static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002434{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002435 /* Note: unlike access bits, deny bits may be zero. */
2436 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002437}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002439/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 * Called to check deny when READ with all zero stateid or
2441 * WRITE with all zero or all one stateid
2442 */
Al Virob37ad282006-10-19 23:28:59 -07002443static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2445{
2446 struct inode *ino = current_fh->fh_dentry->d_inode;
2447 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002448 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002449 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 dprintk("NFSD: nfs4_share_conflict\n");
2452
2453 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002454 if (!fp)
2455 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002456 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002458 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2459 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2460 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2461 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
NeilBrown13cd2182005-06-23 22:03:10 -07002463 ret = nfs_ok;
2464out:
2465 put_nfs4_file(fp);
2466 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002469static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /* We're assuming the state code never drops its reference
2472 * without first removing the lease. Since we're in this lease
2473 * callback (and since the lease code is serialized by the kernel
2474 * lock) we know the server hasn't removed the lease yet, we know
2475 * it's safe to take a reference: */
2476 atomic_inc(&dp->dl_count);
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Arnd Bergmann460781b2010-11-17 16:26:56 +01002480 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 dp->dl_time = get_seconds();
2482
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002483 nfsd4_cb_recall(dp);
2484}
2485
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002486/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002487static void nfsd_break_deleg_cb(struct file_lock *fl)
2488{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002489 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2490 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002491
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002492 BUG_ON(!fp);
2493 /* We assume break_lease is only called once per lease: */
2494 BUG_ON(fp->fi_had_conflict);
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002495 /*
2496 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002497 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002498 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002499 */
2500 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002502 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002503 fp->fi_had_conflict = true;
2504 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2505 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002506 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509static
2510int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2511{
2512 if (arg & F_UNLCK)
2513 return lease_modify(onlist, arg);
2514 else
2515 return -EAGAIN;
2516}
2517
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002518static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002519 .lm_break = nfsd_break_deleg_cb,
2520 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521};
2522
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002523static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2524{
2525 if (nfsd4_has_session(cstate))
2526 return nfs_ok;
2527 if (seqid == so->so_seqid - 1)
2528 return nfserr_replay_me;
2529 if (seqid == so->so_seqid)
2530 return nfs_ok;
2531 return nfserr_bad_seqid;
2532}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Al Virob37ad282006-10-19 23:28:59 -07002534__be32
Andy Adamson66689582009-04-03 08:28:45 +03002535nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2536 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 clientid_t *clientid = &open->op_clientid;
2539 struct nfs4_client *clp = NULL;
2540 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002541 struct nfs4_openowner *oo = NULL;
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002542 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002545 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 if (STALE_CLIENTID(&open->op_clientid))
2548 return nfserr_stale_clientid;
2549
J. Bruce Fields506f2752011-08-11 18:50:18 -04002550 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002551 oo = find_openstateowner_str(strhashval, open);
2552 open->op_openowner = oo;
2553 if (!oo) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002554 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 clp = find_confirmed_client(clientid);
2556 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002557 return nfserr_expired;
2558 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002560 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002561 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002562 clp = oo->oo_owner.so_client;
2563 release_openowner(oo);
2564 open->op_openowner = NULL;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002565 goto renew;
2566 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002567 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002568 if (status)
2569 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570renew:
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002571 if (open->op_openowner == NULL) {
2572 oo = alloc_init_open_stateowner(strhashval, clp, open);
2573 if (oo == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002574 return nfserr_jukebox;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002575 open->op_openowner = oo;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002576 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002577 list_del_init(&oo->oo_close_lru);
2578 renew_client(oo->oo_owner.so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002579 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Al Virob37ad282006-10-19 23:28:59 -07002582static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002583nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2584{
2585 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2586 return nfserr_openmode;
2587 else
2588 return nfs_ok;
2589}
2590
Daniel Mackc47d8322011-05-16 16:38:14 +02002591static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002592{
2593 share_access &= ~NFS4_SHARE_WANT_MASK;
2594
2595 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2596}
2597
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002598static struct nfs4_delegation *find_deleg_stateid(stateid_t *s)
2599{
2600 struct nfs4_stid *ret;
2601
2602 ret = find_stateid_by_type(s, NFS4_DELEG_STID);
2603 if (!ret)
2604 return NULL;
2605 return delegstateid(ret);
2606}
2607
Al Virob37ad282006-10-19 23:28:59 -07002608static __be32
NeilBrown567d9822005-06-23 22:02:53 -07002609nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2610 struct nfs4_delegation **dp)
2611{
2612 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002613 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002614
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002615 *dp = find_deleg_stateid(&open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002616 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002617 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002618 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002619 status = nfs4_check_delegmode(*dp, flags);
2620 if (status)
2621 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002622out:
2623 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2624 return nfs_ok;
2625 if (status)
2626 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002627 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002628 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002629}
2630
Al Virob37ad282006-10-19 23:28:59 -07002631static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002632nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002634 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002635 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
NeilBrown8beefa22005-06-23 22:03:08 -07002637 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /* ignore lock owners */
2639 if (local->st_stateowner->so_is_open_owner == 0)
2640 continue;
2641 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002642 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 *stpp = local;
2644 /* check for conflicting share reservations */
2645 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002646 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002648 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649}
2650
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002651static inline struct nfs4_ol_stateid *
NeilBrown5ac049a2005-06-23 22:03:03 -07002652nfs4_alloc_stateid(void)
2653{
2654 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2655}
2656
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002657static inline int nfs4_access_to_access(u32 nfs4_access)
2658{
2659 int flags = 0;
2660
2661 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2662 flags |= NFSD_MAY_READ;
2663 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2664 flags |= NFSD_MAY_WRITE;
2665 return flags;
2666}
2667
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002668static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2669 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002670{
2671 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002672 int oflag = nfs4_access_to_omode(open->op_share_access);
2673 int access = nfs4_access_to_access(open->op_share_access);
2674
2675 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2676 * allowing it to break the lease and return EAGAIN leaves the
2677 * client unable to make progress in returning the delegation */
2678 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2679 access |= NFSD_MAY_NOT_BREAK_LEASE;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002680
2681 if (!fp->fi_fds[oflag]) {
2682 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2683 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002684 if (status)
2685 return status;
2686 }
2687 nfs4_file_get_access(fp, oflag);
2688
2689 return nfs_ok;
2690}
2691
Al Virob37ad282006-10-19 23:28:59 -07002692static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002693nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_ol_stateid **stpp,
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002694 struct nfs4_file *fp, struct svc_fh *cur_fh,
2695 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002697 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002698 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
NeilBrown5ac049a2005-06-23 22:03:03 -07002700 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (stp == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002702 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002704 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002705 if (status) {
2706 kmem_cache_free(stateid_slab, stp);
2707 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 *stpp = stp;
2710 return 0;
2711}
2712
Al Virob37ad282006-10-19 23:28:59 -07002713static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2715 struct nfsd4_open *open)
2716{
2717 struct iattr iattr = {
2718 .ia_valid = ATTR_SIZE,
2719 .ia_size = 0,
2720 };
2721 if (!open->op_truncate)
2722 return 0;
2723 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002724 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2726}
2727
Al Virob37ad282006-10-19 23:28:59 -07002728static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002729nfs4_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 -07002730{
J. Bruce Fields7d947842010-08-20 18:09:31 -04002731 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2732 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002733 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
J. Bruce Fields7d947842010-08-20 18:09:31 -04002735 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002736 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002737 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002738 if (status)
2739 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 status = nfsd4_truncate(rqstp, cur_fh, open);
2742 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002743 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002744 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002745 nfs4_file_put_access(fp, oflag);
2746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return status;
2748 }
2749 /* remember the open */
J. Bruce Fields24a01112010-05-18 20:01:35 -04002750 __set_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002751 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753 return nfs_ok;
2754}
2755
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757static void
NeilBrown37515172005-07-07 17:59:16 -07002758nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002760 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002761 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002764/* Should we give out recallable state?: */
2765static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2766{
2767 if (clp->cl_cb_state == NFSD4_CB_UP)
2768 return true;
2769 /*
2770 * In the sessions case, since we don't have to establish a
2771 * separate connection for callbacks, we assume it's OK
2772 * until we hear otherwise:
2773 */
2774 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2775}
2776
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002777static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2778{
2779 struct file_lock *fl;
2780
2781 fl = locks_alloc_lock();
2782 if (!fl)
2783 return NULL;
2784 locks_init_lock(fl);
2785 fl->fl_lmops = &nfsd_lease_mng_ops;
2786 fl->fl_flags = FL_LEASE;
2787 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2788 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002789 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002790 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002791 return fl;
2792}
2793
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002794static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2795{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002796 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002797 struct file_lock *fl;
2798 int status;
2799
2800 fl = nfs4_alloc_init_lease(dp, flag);
2801 if (!fl)
2802 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002803 fl->fl_file = find_readable_file(fp);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002804 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002805 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002806 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002807 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002808 locks_free_lock(fl);
2809 return -ENOMEM;
2810 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002811 fp->fi_lease = fl;
2812 fp->fi_deleg_file = fl->fl_file;
2813 get_file(fp->fi_deleg_file);
2814 atomic_set(&fp->fi_delegees, 1);
2815 list_add(&dp->dl_perfile, &fp->fi_delegations);
2816 return 0;
2817}
2818
2819static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2820{
2821 struct nfs4_file *fp = dp->dl_file;
2822
2823 if (!fp->fi_lease)
2824 return nfs4_setlease(dp, flag);
2825 spin_lock(&recall_lock);
2826 if (fp->fi_had_conflict) {
2827 spin_unlock(&recall_lock);
2828 return -EAGAIN;
2829 }
2830 atomic_inc(&fp->fi_delegees);
2831 list_add(&dp->dl_perfile, &fp->fi_delegations);
2832 spin_unlock(&recall_lock);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002833 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002834 return 0;
2835}
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837/*
2838 * Attempt to hand out a delegation.
2839 */
2840static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002841nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002844 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002845 int cb_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 int status, flag = 0;
2847
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002848 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002850 open->op_recall = 0;
2851 switch (open->op_claim_type) {
2852 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002853 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002854 open->op_recall = 1;
2855 flag = open->op_delegate_type;
2856 if (flag == NFS4_OPEN_DELEGATE_NONE)
2857 goto out;
2858 break;
2859 case NFS4_OPEN_CLAIM_NULL:
2860 /* Let's not give out any delegations till everyone's
2861 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002862 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002863 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002864 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07002865 goto out;
2866 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2867 flag = NFS4_OPEN_DELEGATE_WRITE;
2868 else
2869 flag = NFS4_OPEN_DELEGATE_READ;
2870 break;
2871 default:
2872 goto out;
2873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002875 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002876 if (dp == NULL)
2877 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002878 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002879 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002880 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002882 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002884 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002885 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886out:
NeilBrown7b190fe2005-06-23 22:03:23 -07002887 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2888 && flag == NFS4_OPEN_DELEGATE_NONE
2889 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002890 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 open->op_delegate_type = flag;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002892 return;
2893out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002894 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002895out_no_deleg:
2896 flag = NFS4_OPEN_DELEGATE_NONE;
2897 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
2900/*
2901 * called with nfs4_lock_state() held.
2902 */
Al Virob37ad282006-10-19 23:28:59 -07002903__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2905{
Andy Adamson66689582009-04-03 08:28:45 +03002906 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 struct nfs4_file *fp = NULL;
2908 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002909 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07002910 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002911 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913 status = nfserr_inval;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002914 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002915 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 goto out;
2917 /*
2918 * Lookup file; if found, lookup stateid and check open request,
2919 * and check for delegations in the process of being recalled.
2920 * If not found, create the nfs4_file struct
2921 */
2922 fp = find_file(ino);
2923 if (fp) {
2924 if ((status = nfs4_check_open(fp, open, &stp)))
2925 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002926 status = nfs4_check_deleg(fp, open, &dp);
2927 if (status)
2928 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07002930 status = nfserr_bad_stateid;
2931 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2932 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04002933 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 fp = alloc_init_file(ino);
2935 if (fp == NULL)
2936 goto out;
2937 }
2938
2939 /*
2940 * OPEN the file, or upgrade an existing OPEN.
2941 * If truncate fails, the OPEN fails.
2942 */
2943 if (stp) {
2944 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002945 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (status)
2947 goto out;
2948 } else {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002949 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07002950 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 goto out;
J. Bruce Fields881ea2b2011-09-06 17:20:34 -04002952 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 status = nfsd4_truncate(rqstp, current_fh, open);
2954 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05002955 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 goto out;
2957 }
2958 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002959 update_stateid(&stp->st_stid.sc_stateid);
2960 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002962 if (nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002963 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03002964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 /*
2966 * Attempt to hand out a delegation. No error return, because the
2967 * OPEN succeeds even if we fail.
2968 */
2969 nfs4_open_delegation(current_fh, open, stp);
2970
2971 status = nfs_ok;
2972
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002973 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002974 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975out:
NeilBrown13cd2182005-06-23 22:03:10 -07002976 if (fp)
2977 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07002978 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2979 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 /*
2981 * To finish the open response, we just need to set the rflags.
2982 */
2983 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002984 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03002985 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2987
2988 return status;
2989}
2990
Al Virob37ad282006-10-19 23:28:59 -07002991__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002992nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2993 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07002996 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 nfs4_lock_state();
2999 dprintk("process_renew(%08x/%08x): starting\n",
3000 clid->cl_boot, clid->cl_id);
3001 status = nfserr_stale_clientid;
3002 if (STALE_CLIENTID(clid))
3003 goto out;
3004 clp = find_confirmed_client(clid);
3005 status = nfserr_expired;
3006 if (clp == NULL) {
3007 /* We assume the client took too long to RENEW. */
3008 dprintk("nfsd4_renew: clientid not found!\n");
3009 goto out;
3010 }
3011 renew_client(clp);
3012 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003013 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003014 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 goto out;
3016 status = nfs_ok;
3017out:
3018 nfs4_unlock_state();
3019 return status;
3020}
3021
Daniel Mackc47d8322011-05-16 16:38:14 +02003022static struct lock_manager nfsd4_manager = {
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003023};
3024
NeilBrowna76b4312005-06-23 22:04:01 -07003025static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003026nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07003027{
3028 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07003029 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003030 locks_end_grace(&nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003031 /*
3032 * Now that every NFSv4 client has had the chance to recover and
3033 * to see the (possibly new, possibly shorter) lease time, we
3034 * can safely set the next grace time to the current lease time:
3035 */
3036 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003037}
3038
NeilBrownfd39ca92005-06-23 22:04:03 -07003039static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040nfs4_laundromat(void)
3041{
3042 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003043 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 struct nfs4_delegation *dp;
3045 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003046 time_t cutoff = get_seconds() - nfsd4_lease;
3047 time_t t, clientid_val = nfsd4_lease;
3048 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
3050 nfs4_lock_state();
3051
3052 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003053 if (locks_in_grace())
3054 nfsd4_end_grace();
Benny Halevy36acb662010-05-12 00:13:04 +03003055 INIT_LIST_HEAD(&reaplist);
3056 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 list_for_each_safe(pos, next, &client_lru) {
3058 clp = list_entry(pos, struct nfs4_client, cl_lru);
3059 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3060 t = clp->cl_time - cutoff;
3061 if (clientid_val > t)
3062 clientid_val = t;
3063 break;
3064 }
Benny Halevyd7682982010-05-12 00:13:54 +03003065 if (atomic_read(&clp->cl_refcount)) {
3066 dprintk("NFSD: client in use (clientid %08x)\n",
3067 clp->cl_clientid.cl_id);
3068 continue;
3069 }
3070 unhash_client_locked(clp);
3071 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003072 }
3073 spin_unlock(&client_lock);
3074 list_for_each_safe(pos, next, &reaplist) {
3075 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 dprintk("NFSD: purging unused client (clientid %08x)\n",
3077 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07003078 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 expire_client(clp);
3080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 spin_lock(&recall_lock);
3082 list_for_each_safe(pos, next, &del_recall_lru) {
3083 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3084 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3085 u = dp->dl_time - cutoff;
3086 if (test_val > u)
3087 test_val = u;
3088 break;
3089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 list_move(&dp->dl_recall_lru, &reaplist);
3091 }
3092 spin_unlock(&recall_lock);
3093 list_for_each_safe(pos, next, &reaplist) {
3094 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3095 list_del_init(&dp->dl_recall_lru);
3096 unhash_delegation(dp);
3097 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003098 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 list_for_each_safe(pos, next, &close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003100 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3101 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3102 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (test_val > u)
3104 test_val = u;
3105 break;
3106 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003107 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
3109 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3110 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3111 nfs4_unlock_state();
3112 return clientid_val;
3113}
3114
Harvey Harrisona254b242008-02-20 12:49:00 -08003115static struct workqueue_struct *laundry_wq;
3116static void laundromat_main(struct work_struct *);
3117static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3118
3119static void
David Howellsc4028952006-11-22 14:57:56 +00003120laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
3122 time_t t;
3123
3124 t = nfs4_laundromat();
3125 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07003126 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003129static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003130{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003131 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3132 return nfserr_bad_stateid;
3133 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134}
3135
3136static int
3137STALE_STATEID(stateid_t *stateid)
3138{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04003139 if (stateid->si_opaque.so_clid.cl_boot == boot_time)
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003140 return 0;
3141 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003142 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003143 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
3146static inline int
3147access_permit_read(unsigned long access_bmap)
3148{
3149 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3150 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3151 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3152}
3153
3154static inline int
3155access_permit_write(unsigned long access_bmap)
3156{
3157 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3158 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3159}
3160
3161static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003162__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
Al Virob37ad282006-10-19 23:28:59 -07003164 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
J. Bruce Fields02921912010-07-29 15:16:59 -04003166 /* For lock stateid's, we test the parent open, not the lock: */
3167 if (stp->st_openstp)
3168 stp = stp->st_openstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3170 goto out;
3171 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3172 goto out;
3173 status = nfs_ok;
3174out:
3175 return status;
3176}
3177
Al Virob37ad282006-10-19 23:28:59 -07003178static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3180{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003181 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003183 else if (locks_in_grace()) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003184 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 * conflicting state; so we must wait out the grace period. */
3186 return nfserr_grace;
3187 } else if (flags & WR_STATE)
3188 return nfs4_share_conflict(current_fh,
3189 NFS4_SHARE_DENY_WRITE);
3190 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3191 return nfs4_share_conflict(current_fh,
3192 NFS4_SHARE_DENY_READ);
3193}
3194
3195/*
3196 * Allow READ/WRITE during grace period on recovered state only for files
3197 * that are not able to provide mandatory locking.
3198 */
3199static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08003200grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003202 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
J. Bruce Fields81b82962011-08-23 11:03:29 -04003205/* Returns true iff a is later than b: */
3206static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3207{
3208 return (s32)a->si_generation - (s32)b->si_generation > 0;
3209}
3210
J. Bruce Fields28dde242011-08-22 10:07:12 -04003211static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003212{
Andy Adamson66689582009-04-03 08:28:45 +03003213 /*
3214 * When sessions are used the stateid generation number is ignored
3215 * when it is zero.
3216 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003217 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003218 return nfs_ok;
3219
3220 if (in->si_generation == ref->si_generation)
3221 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003222
J. Bruce Fields0836f582008-01-26 19:08:12 -05003223 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003224 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003225 return nfserr_bad_stateid;
3226 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003227 * However, we could see a stateid from the past, even from a
3228 * non-buggy client. For example, if the client sends a lock
3229 * while some IO is outstanding, the lock may bump si_generation
3230 * while the IO is still in flight. The client could avoid that
3231 * situation by waiting for responses on all the IO requests,
3232 * but better performance may result in retrying IO that
3233 * receives an old_stateid error if requests are rarely
3234 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003235 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003236 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003237}
3238
J. Bruce Fields28dde242011-08-22 10:07:12 -04003239__be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
Bryan Schumaker17456802011-07-13 10:50:48 -04003240{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003241 struct nfs4_stid *s;
3242 struct nfs4_ol_stateid *ols;
3243 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003244
3245 if (STALE_STATEID(stateid))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003246 return nfserr_stale_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04003247
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003248 s = find_stateid(stateid);
3249 if (!s)
3250 return nfserr_stale_stateid;
3251 status = check_stateid_generation(stateid, &s->sc_stateid, has_session);
Bryan Schumaker17456802011-07-13 10:50:48 -04003252 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003253 return status;
3254 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3255 return nfs_ok;
3256 ols = openlockstateid(s);
3257 if (ols->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003258 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003259 return nfserr_bad_stateid;
3260 return nfs_ok;
Bryan Schumaker17456802011-07-13 10:50:48 -04003261}
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263/*
3264* Checks for stateid operations
3265*/
Al Virob37ad282006-10-19 23:28:59 -07003266__be32
Benny Halevydd453df2009-04-03 08:28:41 +03003267nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3268 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003270 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003271 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003273 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07003275 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (filpp)
3278 *filpp = NULL;
3279
J. Bruce Fields18f82732009-02-21 15:23:01 -08003280 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return nfserr_grace;
3282
3283 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3284 return check_special_stateids(current_fh, stateid, flags);
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 status = nfserr_stale_stateid;
3287 if (STALE_STATEID(stateid))
3288 goto out;
3289
J. Bruce Fields33515142010-10-02 18:42:39 -04003290 /*
3291 * We assume that any stateid that has the current boot time,
3292 * but that we can't find, is expired:
3293 */
3294 status = nfserr_expired;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003295 s = find_stateid(stateid);
3296 if (!s)
3297 goto out;
3298 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3299 if (status)
3300 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003301 switch (s->sc_type) {
3302 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003303 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003304 status = nfs4_check_delegmode(dp, flags);
3305 if (status)
3306 goto out;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04003307 renew_client(dp->dl_stid.sc_client);
Dan Carpenter43b01782010-10-27 23:19:04 +02003308 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003309 *filpp = dp->dl_file->fi_deleg_file;
Dan Carpenter43b01782010-10-27 23:19:04 +02003310 BUG_ON(!*filpp);
3311 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003312 break;
3313 case NFS4_OPEN_STID:
3314 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003315 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003316 status = nfs4_check_fh(current_fh, stp);
3317 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003319 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003320 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003322 status = nfs4_check_openmode(stp, flags);
3323 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 goto out;
3325 renew_client(stp->st_stateowner->so_client);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003326 if (filpp) {
3327 if (flags & RD_STATE)
3328 *filpp = find_readable_file(stp->st_file);
3329 else
3330 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003331 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003332 break;
3333 default:
3334 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
3336 status = nfs_ok;
3337out:
3338 return status;
3339}
3340
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003341static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003342nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003343{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003344 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003345 return nfserr_locks_held;
3346 release_lock_stateid(stp);
3347 return nfs_ok;
3348}
3349
3350/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003351 * Test if the stateid is valid
3352 */
3353__be32
3354nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3355 struct nfsd4_test_stateid *test_stateid)
3356{
3357 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3358 return nfs_ok;
3359}
3360
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003361__be32
3362nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3363 struct nfsd4_free_stateid *free_stateid)
3364{
3365 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003366 struct nfs4_stid *s;
3367 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003368
3369 nfs4_lock_state();
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003370 s = find_stateid(stateid);
3371 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003372 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003373 switch (s->sc_type) {
3374 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003375 ret = nfserr_locks_held;
3376 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003377 case NFS4_OPEN_STID:
3378 case NFS4_LOCK_STID:
3379 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3380 if (ret)
3381 goto out;
3382 if (s->sc_type == NFS4_LOCK_STID)
3383 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3384 else
3385 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003386 break;
3387 default:
3388 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003389 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003390out:
3391 nfs4_unlock_state();
3392 return ret;
3393}
3394
NeilBrown4c4cd222005-07-07 17:59:27 -07003395static inline int
3396setlkflg (int type)
3397{
3398 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3399 RD_STATE : WR_STATE;
3400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003402static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid)
3403{
3404 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3405 return nfserr_bad_stateid;
3406 if (STALE_STATEID(stateid))
3407 return nfserr_stale_stateid;
3408 return nfs_ok;
3409}
3410
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003411static __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 -04003412{
3413 struct svc_fh *current_fh = &cstate->current_fh;
3414 struct nfs4_stateowner *sop = stp->st_stateowner;
3415 __be32 status;
3416
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003417 status = nfsd4_check_seqid(cstate, sop, seqid);
3418 if (status)
3419 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003420 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3421 /*
3422 * "Closed" stateid's exist *only* to return
3423 * nfserr_replay_me from the previous step.
3424 */
3425 return nfserr_bad_stateid;
3426 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3427 if (status)
3428 return status;
3429 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003430}
3431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432/*
3433 * Checks for sequence id mutating operations.
3434 */
Al Virob37ad282006-10-19 23:28:59 -07003435static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003436nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003437 stateid_t *stateid, char typemask,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003438 struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003440 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003442 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3443 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 *stpp = NULL;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003446 status = nfs4_nospecial_stateid_checks(stateid);
3447 if (status)
3448 return status;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003449 *stpp = find_ol_stateid_by_type(stateid, typemask);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003450 if (*stpp == NULL)
3451 return nfserr_expired;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003452 cstate->replay_owner = (*stpp)->st_stateowner;
3453 renew_client((*stpp)->st_stateowner->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003455 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3456}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003457
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003458static __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 -04003459{
3460 __be32 status;
3461 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003463 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003464 NFS4_OPEN_STID, stpp);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003465 if (status)
3466 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003467 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003468 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003469 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003470 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Al Virob37ad282006-10-19 23:28:59 -07003473__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003474nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003475 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
Al Virob37ad282006-10-19 23:28:59 -07003477 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003478 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003479 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
3481 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003482 (int)cstate->current_fh.fh_dentry->d_name.len,
3483 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003485 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003486 if (status)
3487 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
3489 nfs4_lock_state();
3490
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003491 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003492 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003493 NFS4_OPEN_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003494 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003495 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003496 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003497 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003498 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003499 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003500 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003501 update_stateid(&stp->st_stid.sc_stateid);
3502 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003503 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003504 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003505
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003506 nfsd4_create_clid_dir(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003507 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003509 if (!cstate->replay_owner)
3510 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 return status;
3512}
3513
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003514static inline void nfs4_file_downgrade(struct nfs4_ol_stateid *stp, unsigned int to_access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515{
3516 int i;
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 for (i = 1; i < 4; i++) {
J. Bruce Fields3d02fa22011-09-19 15:07:41 -04003519 if (test_bit(i, &stp->st_access_bmap)
3520 && ((i & to_access) != i)) {
3521 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(i));
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003522 __clear_bit(i, &stp->st_access_bmap);
3523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 }
3525}
3526
3527static void
3528reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3529{
3530 int i;
3531 for (i = 0; i < 4; i++) {
3532 if ((i & deny) != i)
3533 __clear_bit(i, bmap);
3534 }
3535}
3536
Al Virob37ad282006-10-19 23:28:59 -07003537__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003538nfsd4_open_downgrade(struct svc_rqst *rqstp,
3539 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003540 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
Al Virob37ad282006-10-19 23:28:59 -07003542 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003543 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003546 (int)cstate->current_fh.fh_dentry->d_name.len,
3547 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Andy Adamsond87a8ad2009-04-03 08:28:53 +03003549 if (!access_valid(od->od_share_access, cstate->minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07003550 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 return nfserr_inval;
3552
3553 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003554 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3555 &od->od_stateid, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003556 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 status = nfserr_inval;
3559 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3560 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3561 stp->st_access_bmap, od->od_share_access);
3562 goto out;
3563 }
3564 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3565 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3566 stp->st_deny_bmap, od->od_share_deny);
3567 goto out;
3568 }
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003569 nfs4_file_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3572
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003573 update_stateid(&stp->st_stid.sc_stateid);
3574 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 status = nfs_ok;
3576out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003577 if (!cstate->replay_owner)
3578 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return status;
3580}
3581
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003582void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3583{
3584 struct nfs4_openowner *oo;
3585 struct nfs4_ol_stateid *s;
3586
3587 if (!so->so_is_open_owner)
3588 return;
3589 oo = openowner(so);
3590 s = oo->oo_last_closed_stid;
3591 if (!s)
3592 return;
3593 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3594 /* Release the last_closed_stid on the next seqid bump: */
3595 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3596 return;
3597 }
3598 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003599 release_last_closed_stateid(oo);
3600}
3601
3602static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3603{
3604 unhash_open_stateid(s);
3605 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003606}
3607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608/*
3609 * nfs4_unlock_state() called after encode
3610 */
Al Virob37ad282006-10-19 23:28:59 -07003611__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003612nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003613 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614{
Al Virob37ad282006-10-19 23:28:59 -07003615 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003616 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003617 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
3619 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003620 (int)cstate->current_fh.fh_dentry->d_name.len,
3621 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
3623 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003624 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3625 &close->cl_stateid,
3626 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3627 &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003628 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003630 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 status = nfs_ok;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003632 update_stateid(&stp->st_stid.sc_stateid);
3633 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003635 nfsd4_close_open_stateid(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003636 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003637
3638 /* place unused nfs4_stateowners on so_close_lru list to be
3639 * released by the laundromat service after the lease period
3640 * to enable us to handle CLOSE replay
3641 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003642 if (list_empty(&oo->oo_owner.so_stateids))
3643 move_to_close_lru(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003645 if (!cstate->replay_owner)
3646 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 return status;
3648}
3649
Al Virob37ad282006-10-19 23:28:59 -07003650__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003651nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3652 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003654 struct nfs4_delegation *dp;
3655 stateid_t *stateid = &dr->dr_stateid;
3656 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07003657 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003659 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003660 return status;
3661 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
3663 nfs4_lock_state();
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003664 status = nfserr_bad_stateid;
3665 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3666 goto out;
3667 status = nfserr_stale_stateid;
3668 if (STALE_STATEID(stateid))
3669 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003670 status = nfserr_expired;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003671 dp = find_deleg_stateid(stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003672 if (!dp)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003673 goto out;
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003674 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003675 if (status)
3676 goto out;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04003677 renew_client(dp->dl_stid.sc_client);
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003678
3679 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003681 nfs4_unlock_state();
3682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return status;
3684}
3685
3686
3687/*
3688 * Lock owner state (byte-range locks)
3689 */
3690#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3691#define LOCK_HASH_BITS 8
3692#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3693#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3694
Benny Halevy87df4de2008-12-15 19:42:03 +02003695static inline u64
3696end_offset(u64 start, u64 len)
3697{
3698 u64 end;
3699
3700 end = start + len;
3701 return end >= start ? end: NFS4_MAX_UINT64;
3702}
3703
3704/* last octet in a range */
3705static inline u64
3706last_byte_offset(u64 start, u64 len)
3707{
3708 u64 end;
3709
3710 BUG_ON(!len);
3711 end = start + len;
3712 return end > start ? end - 1: NFS4_MAX_UINT64;
3713}
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715static inline unsigned int
3716lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3717 struct xdr_netobj *ownername)
3718{
3719 return (file_hashval(inode) + cl_id
3720 + opaque_hashval(ownername->data, ownername->len))
3721 & LOCK_HASH_MASK;
3722}
3723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726/*
3727 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3728 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3729 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3730 * locking, this prevents us from being completely protocol-compliant. The
3731 * real solution to this problem is to start using unsigned file offsets in
3732 * the VFS, but this is a very deep change!
3733 */
3734static inline void
3735nfs4_transform_lock_offset(struct file_lock *lock)
3736{
3737 if (lock->fl_start < 0)
3738 lock->fl_start = OFFSET_MAX;
3739 if (lock->fl_end < 0)
3740 lock->fl_end = OFFSET_MAX;
3741}
3742
NeilBrownd5b90262006-04-10 22:55:22 -07003743/* Hack!: For now, we're defining this just so we can use a pointer to it
3744 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003745static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003746};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
3748static inline void
3749nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3750{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003751 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
NeilBrownd5b90262006-04-10 22:55:22 -07003753 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003754 lo = (struct nfs4_lockowner *) fl->fl_owner;
3755 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3756 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003757 if (!deny->ld_owner.data)
3758 /* We just don't care that much */
3759 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003760 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3761 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003762 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003763nevermind:
3764 deny->ld_owner.len = 0;
3765 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003766 deny->ld_clientid.cl_boot = 0;
3767 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 }
3769 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003770 deny->ld_length = NFS4_MAX_UINT64;
3771 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3773 deny->ld_type = NFS4_READ_LT;
3774 if (fl->fl_type != F_RDLCK)
3775 deny->ld_type = NFS4_WRITE_LT;
3776}
3777
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003778static struct nfs4_lockowner *
3779find_lockowner_str(struct inode *inode, clientid_t *clid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 struct xdr_netobj *owner)
3781{
3782 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3783 struct nfs4_stateowner *op;
3784
3785 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003786 if (same_owner_str(op, owner, clid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003787 return lockowner(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
3789 return NULL;
3790}
3791
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003792static 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 -04003793{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003794 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3795 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003796}
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798/*
3799 * Alloc a lock owner structure.
3800 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003801 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 *
3803 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 */
3805
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003806static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003807alloc_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 -04003808 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003810 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3811 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003813 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3814 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07003815 /* It is the openowner seqid that will be incremented in encode in the
3816 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003817 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3818 hash_lockowner(lo, strhashval, clp, open_stp);
3819 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003822static struct nfs4_ol_stateid *
3823alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003825 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04003826 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
NeilBrown5ac049a2005-06-23 22:03:03 -07003828 stp = nfs4_alloc_stateid();
3829 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 goto out;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04003831 init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
NeilBrown8beefa22005-06-23 22:03:08 -07003832 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003833 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3834 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07003835 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05003837 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003839 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
3841out:
3842 return stp;
3843}
3844
NeilBrownfd39ca92005-06-23 22:04:03 -07003845static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846check_lock_length(u64 offset, u64 length)
3847{
Benny Halevy87df4de2008-12-15 19:42:03 +02003848 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 LOFF_OVERFLOW(offset, length)));
3850}
3851
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003852static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05003853{
3854 struct nfs4_file *fp = lock_stp->st_file;
3855 int oflag = nfs4_access_to_omode(access);
3856
3857 if (test_bit(access, &lock_stp->st_access_bmap))
3858 return;
3859 nfs4_file_get_access(fp, oflag);
3860 __set_bit(access, &lock_stp->st_access_bmap);
3861}
3862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863/*
3864 * LOCK operation
3865 */
Al Virob37ad282006-10-19 23:28:59 -07003866__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003867nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003868 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003870 struct nfs4_openowner *open_sop = NULL;
3871 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003872 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04003873 struct nfs4_file *fp;
3874 struct file *filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05003876 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07003877 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 unsigned int strhashval;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003879 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07003880 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
3882 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3883 (long long) lock->lk_offset,
3884 (long long) lock->lk_length);
3885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if (check_lock_length(lock->lk_offset, lock->lk_length))
3887 return nfserr_inval;
3888
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003889 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02003890 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08003891 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3892 return status;
3893 }
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 nfs4_lock_state();
3896
3897 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07003898 /*
3899 * Client indicates that this is a new lockowner.
3900 * Use open owner and open stateid to create lock owner and
3901 * lock stateid.
3902 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003903 struct nfs4_ol_stateid *open_stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03003906 if (!nfsd4_has_session(cstate) &&
3907 STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003911 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 lock->lk_new_open_seqid,
3913 &lock->lk_new_open_stateid,
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003914 &open_stp);
NeilBrown37515172005-07-07 17:59:16 -07003915 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003917 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003918 status = nfserr_bad_stateid;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003919 if (!nfsd4_has_session(cstate) &&
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003920 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003921 &lock->v.new.clientid))
3922 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 /* create lockowner and lock stateid */
3924 fp = open_stp->st_file;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003925 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3926 open_sop->oo_owner.so_client->cl_clientid.cl_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003928 /* XXX: Do we need to check for duplicate stateowners on
3929 * the same file, or should they just be allowed (and
3930 * create new stateids)? */
J. Bruce Fields3e772462011-08-10 19:07:33 -04003931 status = nfserr_jukebox;
Neil Brownb59e3c02005-09-13 01:25:38 -07003932 lock_sop = alloc_init_lock_stateowner(strhashval,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003933 open_sop->oo_owner.so_client, open_stp, lock);
Neil Brownb59e3c02005-09-13 01:25:38 -07003934 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07003936 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08003937 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 } else {
3940 /* lock (lock owner + lock stateid) already exists */
Benny Halevydd453df2009-04-03 08:28:41 +03003941 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003942 lock->lk_old_lock_seqid,
3943 &lock->lk_old_lock_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003944 NFS4_LOCK_STID, &lock_stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 if (status)
3946 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003947 lock_sop = lockowner(lock_stp->st_stateowner);
J. Bruce Fields7d947842010-08-20 18:09:31 -04003948 fp = lock_stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003950 /* lock_sop and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003952 lkflg = setlkflg(lock->lk_type);
3953 status = nfs4_check_openmode(lock_stp, lkflg);
3954 if (status)
3955 goto out;
3956
NeilBrown0dd395d2005-07-07 17:59:15 -07003957 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003958 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07003959 goto out;
3960 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003961 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07003962 goto out;
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 locks_init_lock(&file_lock);
3965 switch (lock->lk_type) {
3966 case NFS4_READ_LT:
3967 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05003968 filp = find_readable_file(lock_stp->st_file);
3969 if (filp)
3970 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 file_lock.fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05003972 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 case NFS4_WRITE_LT:
3974 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05003975 filp = find_writeable_file(lock_stp->st_file);
3976 if (filp)
3977 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 file_lock.fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05003979 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 default:
3981 status = nfserr_inval;
3982 goto out;
3983 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003984 if (!filp) {
3985 status = nfserr_openmode;
3986 goto out;
3987 }
Neil Brownb59e3c02005-09-13 01:25:38 -07003988 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 file_lock.fl_pid = current->tgid;
3990 file_lock.fl_file = filp;
3991 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07003992 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
3994 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02003995 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 nfs4_transform_lock_offset(&file_lock);
3997
3998 /*
3999 * Try to lock the file in the VFS.
4000 * Note: locks.c uses the BKL to protect the inode's lock list.
4001 */
4002
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004003 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004004 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004006 update_stateid(&lock_stp->st_stid.sc_stateid);
4007 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004009 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004010 break;
4011 case (EAGAIN): /* conflock holds conflicting lock */
4012 status = nfserr_denied;
4013 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4014 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4015 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 case (EDEADLK):
4017 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004018 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004019 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004020 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004021 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004022 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08004025 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004026 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004027 if (!cstate->replay_owner)
4028 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 return status;
4030}
4031
4032/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004033 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4034 * so we do a temporary open here just to get an open file to pass to
4035 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4036 * inode operation.)
4037 */
4038static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4039{
4040 struct file *file;
4041 int err;
4042
4043 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4044 if (err)
4045 return err;
4046 err = vfs_test_lock(file, lock);
4047 nfsd_close(file);
4048 return err;
4049}
4050
4051/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 * LOCKT operation
4053 */
Al Virob37ad282006-10-19 23:28:59 -07004054__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004055nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4056 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
4058 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 struct file_lock file_lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004060 struct nfs4_lockowner *lo;
Marc Eshelfd85b812006-11-28 16:26:41 -05004061 int error;
Al Virob37ad282006-10-19 23:28:59 -07004062 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004064 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return nfserr_grace;
4066
4067 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4068 return nfserr_inval;
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 nfs4_lock_state();
4071
4072 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03004073 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004076 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004079 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 locks_init_lock(&file_lock);
4081 switch (lockt->lt_type) {
4082 case NFS4_READ_LT:
4083 case NFS4_READW_LT:
4084 file_lock.fl_type = F_RDLCK;
4085 break;
4086 case NFS4_WRITE_LT:
4087 case NFS4_WRITEW_LT:
4088 file_lock.fl_type = F_WRLCK;
4089 break;
4090 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004091 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 status = nfserr_inval;
4093 goto out;
4094 }
4095
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004096 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4097 if (lo)
4098 file_lock.fl_owner = (fl_owner_t)lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 file_lock.fl_pid = current->tgid;
4100 file_lock.fl_flags = FL_POSIX;
4101
4102 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004103 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
4105 nfs4_transform_lock_offset(&file_lock);
4106
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004108 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05004109 if (error) {
4110 status = nfserrno(error);
4111 goto out;
4112 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004113 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004115 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
4117out:
4118 nfs4_unlock_state();
4119 return status;
4120}
4121
Al Virob37ad282006-10-19 23:28:59 -07004122__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004123nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004124 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004126 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 struct file *filp = NULL;
4128 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07004129 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004130 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
4132 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4133 (long long) locku->lu_offset,
4134 (long long) locku->lu_length);
4135
4136 if (check_lock_length(locku->lu_offset, locku->lu_length))
4137 return nfserr_inval;
4138
4139 nfs4_lock_state();
4140
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004141 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004142 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004143 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004145 filp = find_any_file(stp->st_file);
4146 if (!filp) {
4147 status = nfserr_lock_range;
4148 goto out;
4149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 BUG_ON(!filp);
4151 locks_init_lock(&file_lock);
4152 file_lock.fl_type = F_UNLCK;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004153 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 file_lock.fl_pid = current->tgid;
4155 file_lock.fl_file = filp;
4156 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004157 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 file_lock.fl_start = locku->lu_offset;
4159
Benny Halevy87df4de2008-12-15 19:42:03 +02004160 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 nfs4_transform_lock_offset(&file_lock);
4162
4163 /*
4164 * Try to unlock the file in the VFS.
4165 */
Marc Eshelfd85b812006-11-28 16:26:41 -05004166 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004167 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004168 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 goto out_nfserr;
4170 }
4171 /*
4172 * OK, unlock succeeded; the only thing left to do is update the stateid.
4173 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004174 update_stateid(&stp->st_stid.sc_stateid);
4175 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 nfs4_unlock_state();
4179 return status;
4180
4181out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004182 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 goto out;
4184}
4185
4186/*
4187 * returns
4188 * 1: locks held by lockowner
4189 * 0: no locks held by lockowner
4190 */
4191static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004192check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
4194 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004195 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 int status = 0;
4197
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004198 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004200 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 status = 1;
4202 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 }
4205out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004206 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 return status;
4208}
4209
Al Virob37ad282006-10-19 23:28:59 -07004210__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004211nfsd4_release_lockowner(struct svc_rqst *rqstp,
4212 struct nfsd4_compound_state *cstate,
4213 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
4215 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004216 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004217 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004218 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004220 struct list_head matches;
4221 int i;
Al Virob37ad282006-10-19 23:28:59 -07004222 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
4224 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4225 clid->cl_boot, clid->cl_id);
4226
4227 /* XXX check for lease expiration */
4228
4229 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07004230 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
4233 nfs4_lock_state();
4234
NeilBrown3e9e3db2005-06-23 22:04:20 -07004235 status = nfserr_locks_held;
4236 /* XXX: we're doing a linear search through all the lockowners.
4237 * Yipes! For now we'll just hope clients aren't really using
4238 * release_lockowner much, but eventually we have to fix these
4239 * data structures. */
4240 INIT_LIST_HEAD(&matches);
4241 for (i = 0; i < LOCK_HASH_SIZE; i++) {
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004242 list_for_each_entry(sop, &lock_ownerstr_hashtbl[i], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04004243 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004244 continue;
4245 list_for_each_entry(stp, &sop->so_stateids,
4246 st_perstateowner) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004247 lo = lockowner(sop);
4248 if (check_for_locks(stp->st_file, lo))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004249 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004250 list_add(&lo->lo_list, &matches);
NeilBrown3e9e3db2005-06-23 22:04:20 -07004251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004253 }
4254 /* Clients probably won't expect us to return with some (but not all)
4255 * of the lockowner state released; so don't release any until all
4256 * have been checked. */
4257 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004258 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004259 lo = list_entry(matches.next, struct nfs4_lockowner,
4260 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004261 /* unhash_stateowner deletes so_perclient only
4262 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004263 list_del(&lo->lo_list);
4264 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
4266out:
4267 nfs4_unlock_state();
4268 return status;
4269}
4270
4271static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004272alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273{
NeilBrowna55370a2005-06-23 22:03:52 -07004274 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
NeilBrownc7b9a452005-06-23 22:04:30 -07004277int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03004278nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07004279{
4280 unsigned int strhashval = clientstr_hashval(name);
4281 struct nfs4_client *clp;
4282
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004283 clp = find_confirmed_client_by_str(name, strhashval);
NeilBrownc7b9a452005-06-23 22:04:30 -07004284 return clp ? 1 : 0;
4285}
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287/*
4288 * failure => all reset bets are off, nfserr_no_grace...
4289 */
NeilBrown190e4fb2005-06-23 22:04:25 -07004290int
4291nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
4293 unsigned int strhashval;
4294 struct nfs4_client_reclaim *crp = NULL;
4295
NeilBrowna55370a2005-06-23 22:03:52 -07004296 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4297 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 if (!crp)
4299 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07004300 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 INIT_LIST_HEAD(&crp->cr_strhash);
4302 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07004303 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 reclaim_str_hashtbl_size++;
4305 return 1;
4306}
4307
4308static void
4309nfs4_release_reclaim(void)
4310{
4311 struct nfs4_client_reclaim *crp = NULL;
4312 int i;
4313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4315 while (!list_empty(&reclaim_str_hashtbl[i])) {
4316 crp = list_entry(reclaim_str_hashtbl[i].next,
4317 struct nfs4_client_reclaim, cr_strhash);
4318 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 kfree(crp);
4320 reclaim_str_hashtbl_size--;
4321 }
4322 }
4323 BUG_ON(reclaim_str_hashtbl_size);
4324}
4325
4326/*
4327 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07004328static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329nfs4_find_reclaim_client(clientid_t *clid)
4330{
4331 unsigned int strhashval;
4332 struct nfs4_client *clp;
4333 struct nfs4_client_reclaim *crp = NULL;
4334
4335
4336 /* find clientid in conf_id_hashtbl */
4337 clp = find_confirmed_client(clid);
4338 if (clp == NULL)
4339 return NULL;
4340
NeilBrowna55370a2005-06-23 22:03:52 -07004341 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4342 clp->cl_name.len, clp->cl_name.data,
4343 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07004346 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07004348 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return crp;
4350 }
4351 }
4352 return NULL;
4353}
4354
4355/*
4356* Called from OPEN. Look for clientid in reclaim list.
4357*/
Al Virob37ad282006-10-19 23:28:59 -07004358__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359nfs4_check_open_reclaim(clientid_t *clid)
4360{
NeilBrowndfc83562005-06-23 22:03:16 -07004361 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362}
4363
NeilBrownac4d8ff22005-06-23 22:03:30 -07004364/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004366int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004367nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004369 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004371 status = nfsd4_init_slabs();
4372 if (status)
4373 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4375 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4376 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4377 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4378 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
Wang Chen02cb2852009-04-24 15:41:57 +08004379 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 }
Marc Eshel5282fd72009-04-03 08:27:52 +03004381 for (i = 0; i < SESSION_HASH_SIZE; i++)
4382 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 for (i = 0; i < FILE_HASH_SIZE; i++) {
4384 INIT_LIST_HEAD(&file_hashtbl[i]);
4385 }
J. Bruce Fields506f2752011-08-11 18:50:18 -04004386 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4387 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 }
J. Bruce Fieldsb79abad2011-08-22 18:01:43 -04004389 for (i = 0; i < STATEID_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 INIT_LIST_HEAD(&stateid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 for (i = 0; i < LOCK_HASH_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 INIT_LIST_HEAD(&close_lru);
4396 INIT_LIST_HEAD(&client_lru);
4397 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004398 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004399 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07004400}
4401
NeilBrown190e4fb2005-06-23 22:04:25 -07004402static void
4403nfsd4_load_reboot_recovery_data(void)
4404{
4405 int status;
4406
NeilBrown0964a3d2005-06-23 22:04:32 -07004407 nfs4_lock_state();
J. Bruce Fields48483bf2011-08-26 20:40:28 -04004408 nfsd4_init_recdir();
NeilBrown190e4fb2005-06-23 22:04:25 -07004409 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07004410 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07004411 if (status)
4412 printk("NFSD: Failure reading reboot recovery data\n");
4413}
4414
Meelap Shahc2f1a552007-07-17 04:04:39 -07004415/*
4416 * Since the lifetime of a delegation isn't limited to that of an open, a
4417 * client may quite reasonably hang on to a delegation as long as it has
4418 * the inode cached. This becomes an obvious problem the first time a
4419 * client's inode cache approaches the size of the server's total memory.
4420 *
4421 * For now we avoid this problem by imposing a hard limit on the number
4422 * of delegations, which varies according to the server's memory size.
4423 */
4424static void
4425set_max_delegations(void)
4426{
4427 /*
4428 * Allow at most 4 delegations per megabyte of RAM. Quick
4429 * estimates suggest that in the worst case (where every delegation
4430 * is for a different inode), a delegation could take about 1.5K,
4431 * giving a worst case usage of about 6% of memory.
4432 */
4433 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4434}
4435
NeilBrownac4d8ff22005-06-23 22:03:30 -07004436/* initialization to perform when the nfsd service is started: */
4437
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004438static int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004439__nfs4_state_start(void)
4440{
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004441 int ret;
4442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004444 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07004445 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004446 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004447 ret = set_callback_cred();
4448 if (ret)
4449 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004450 laundry_wq = create_singlethread_workqueue("nfsd4");
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004451 if (laundry_wq == NULL)
4452 return -ENOMEM;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004453 ret = nfsd4_create_callback_queue();
4454 if (ret)
4455 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004456 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004457 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004458 return 0;
4459out_free_laundry:
4460 destroy_workqueue(laundry_wq);
4461 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004464int
NeilBrown76a35502005-06-23 22:03:26 -07004465nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466{
NeilBrown190e4fb2005-06-23 22:04:25 -07004467 nfsd4_load_reboot_recovery_data();
Jeff Layton4ad9a342010-07-19 16:50:04 -04004468 return __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469}
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471static void
4472__nfs4_state_shutdown(void)
4473{
4474 int i;
4475 struct nfs4_client *clp = NULL;
4476 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 struct list_head *pos, *next, reaplist;
4478
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4480 while (!list_empty(&conf_id_hashtbl[i])) {
4481 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4482 expire_client(clp);
4483 }
4484 while (!list_empty(&unconf_str_hashtbl[i])) {
4485 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4486 expire_client(clp);
4487 }
4488 }
4489 INIT_LIST_HEAD(&reaplist);
4490 spin_lock(&recall_lock);
4491 list_for_each_safe(pos, next, &del_recall_lru) {
4492 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4493 list_move(&dp->dl_recall_lru, &reaplist);
4494 }
4495 spin_unlock(&recall_lock);
4496 list_for_each_safe(pos, next, &reaplist) {
4497 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4498 list_del_init(&dp->dl_recall_lru);
4499 unhash_delegation(dp);
4500 }
4501
NeilBrown190e4fb2005-06-23 22:04:25 -07004502 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
4505void
4506nfs4_state_shutdown(void)
4507{
Tejun Heoafe2c512010-12-14 16:21:17 +01004508 cancel_delayed_work_sync(&laundromat_work);
NeilBrown5e8d5c22006-04-10 22:55:37 -07004509 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06004510 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 nfs4_lock_state();
4512 nfs4_release_reclaim();
4513 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 nfs4_unlock_state();
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004515 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516}