blob: c7d54f6a19c950c86bf85cd437efbe4eeaffe8e5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050041#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton363168b2009-08-14 12:57:56 -040042#include <linux/sunrpc/clnt.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020043#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050044#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define NFSDDBG_FACILITY NFSDDBG_PROC
47
48/* Globals */
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -050049time_t nfsd4_lease = 90; /* default lease time */
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -050050time_t nfsd4_grace = 90;
NeilBrownfd39ca92005-06-23 22:04:03 -070051static time_t boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static u32 current_ownerid = 1;
53static u32 current_fileid = 1;
54static u32 current_delegid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070055static stateid_t zerostateid; /* bits all 0 */
56static stateid_t onestateid; /* bits all 1 */
Andy Adamsonec6b5d72009-04-03 08:28:28 +030057static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070058
59#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
60#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* forward declarations */
NeilBrownfd39ca92005-06-23 22:04:03 -070063static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -040064static struct nfs4_stateid * search_for_stateid(stateid_t *stid);
65static struct nfs4_delegation * search_for_delegation(stateid_t *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -040067static int check_for_locks(struct nfs4_file *filp, struct nfs4_stateowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
J. Bruce Fields8b671b82009-02-22 14:51:34 -080069/* Locking: */
70
71/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080072static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
J. Bruce Fields8b671b82009-02-22 14:51:34 -080074/*
75 * Currently used for the del_recall_lru and file hash table. In an
76 * effort to decrease the scope of the client_mutex, this spinlock may
77 * eventually cover more:
78 */
79static DEFINE_SPINLOCK(recall_lock);
80
Christoph Lametere18b8902006-12-06 20:33:20 -080081static struct kmem_cache *stateowner_slab = NULL;
82static struct kmem_cache *file_slab = NULL;
83static struct kmem_cache *stateid_slab = NULL;
84static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086void
87nfs4_lock_state(void)
88{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080089 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92void
93nfs4_unlock_state(void)
94{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080095 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98static inline u32
99opaque_hashval(const void *ptr, int nbytes)
100{
101 unsigned char *cptr = (unsigned char *) ptr;
102
103 u32 x = 0;
104 while (nbytes--) {
105 x *= 37;
106 x += *cptr++;
107 }
108 return x;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static struct list_head del_recall_lru;
112
NeilBrown13cd2182005-06-23 22:03:10 -0700113static inline void
114put_nfs4_file(struct nfs4_file *fi)
115{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800116 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
117 list_del(&fi->fi_hash);
118 spin_unlock(&recall_lock);
119 iput(fi->fi_inode);
120 kmem_cache_free(file_slab, fi);
121 }
NeilBrown13cd2182005-06-23 22:03:10 -0700122}
123
124static inline void
125get_nfs4_file(struct nfs4_file *fi)
126{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800127 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700128}
129
NeilBrownef0f3392006-04-10 22:55:41 -0700130static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700131unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700132
133/*
134 * Open owner state (share locks)
135 */
136
137/* hash tables for nfs4_stateowner */
138#define OWNER_HASH_BITS 8
139#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
140#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
141
142#define ownerid_hashval(id) \
143 ((id) & OWNER_HASH_MASK)
144#define ownerstr_hashval(clientid, ownername) \
145 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
146
147static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
148static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
149
150/* hash table for nfs4_file */
151#define FILE_HASH_BITS 8
152#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800153
NeilBrownef0f3392006-04-10 22:55:41 -0700154/* hash table for (open)nfs4_stateid */
155#define STATEID_HASH_BITS 10
156#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
157#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
158
159#define file_hashval(x) \
160 hash_ptr(x, FILE_HASH_BITS)
161#define stateid_hashval(owner_id, file_id) \
162 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
163
164static struct list_head file_hashtbl[FILE_HASH_SIZE];
165static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
166
J. Bruce Fields998db522010-08-07 09:21:41 -0400167static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400168{
169 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
170 atomic_inc(&fp->fi_access[oflag]);
171}
172
J. Bruce Fields998db522010-08-07 09:21:41 -0400173static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
174{
175 if (oflag == O_RDWR) {
176 __nfs4_file_get_access(fp, O_RDONLY);
177 __nfs4_file_get_access(fp, O_WRONLY);
178 } else
179 __nfs4_file_get_access(fp, oflag);
180}
181
182static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400183{
184 if (fp->fi_fds[oflag]) {
185 fput(fp->fi_fds[oflag]);
186 fp->fi_fds[oflag] = NULL;
187 }
188}
189
J. Bruce Fields998db522010-08-07 09:21:41 -0400190static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400191{
192 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
193 nfs4_file_put_fd(fp, O_RDWR);
194 nfs4_file_put_fd(fp, oflag);
195 }
196}
197
J. Bruce Fields998db522010-08-07 09:21:41 -0400198static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
199{
200 if (oflag == O_RDWR) {
201 __nfs4_file_put_access(fp, O_RDONLY);
202 __nfs4_file_put_access(fp, O_WRONLY);
203 } else
204 __nfs4_file_put_access(fp, oflag);
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static struct nfs4_delegation *
208alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
209{
210 struct nfs4_delegation *dp;
211 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400214 /*
215 * Major work on the lease subsystem (for example, to support
216 * calbacks on stat) will be required before we can support
217 * write delegations properly.
218 */
219 if (type != NFS4_OPEN_DELEGATE_READ)
220 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700221 if (fp->fi_had_conflict)
222 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700223 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700224 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700225 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
226 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700228 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700229 INIT_LIST_HEAD(&dp->dl_perfile);
230 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 INIT_LIST_HEAD(&dp->dl_recall_lru);
232 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700233 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 dp->dl_type = type;
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -0400236 dp->dl_stateid.si_boot = boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 dp->dl_stateid.si_stateownerid = current_delegid++;
238 dp->dl_stateid.si_fileid = 0;
239 dp->dl_stateid.si_generation = 0;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500240 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 dp->dl_time = 0;
242 atomic_set(&dp->dl_count, 1);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500243 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return dp;
245}
246
247void
248nfs4_put_delegation(struct nfs4_delegation *dp)
249{
250 if (atomic_dec_and_test(&dp->dl_count)) {
251 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700252 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700253 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700254 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256}
257
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500258static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500260 if (atomic_dec_and_test(&fp->fi_delegees)) {
261 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
262 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400263 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500264 fp->fi_deleg_file = NULL;
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/* Called under the state lock. */
269static void
270unhash_delegation(struct nfs4_delegation *dp)
271{
NeilBrownea1da632005-06-23 22:04:17 -0700272 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500274 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 list_del_init(&dp->dl_recall_lru);
276 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500277 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 nfs4_put_delegation(dp);
279}
280
281/*
282 * SETCLIENTID state
283 */
284
Benny Halevy36acb662010-05-12 00:13:04 +0300285/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300286static DEFINE_SPINLOCK(client_lock);
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/* Hash tables for nfs4_clientid state */
289#define CLIENT_HASH_BITS 4
290#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
291#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
292
293#define clientid_hashval(id) \
294 ((id) & CLIENT_HASH_MASK)
NeilBrowna55370a2005-06-23 22:03:52 -0700295#define clientstr_hashval(name) \
296 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
298 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
299 * used in reboot/reset lease grace period processing
300 *
301 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
302 * setclientid_confirmed info.
303 *
304 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
305 * setclientid info.
306 *
307 * client_lru holds client queue ordered by nfs4_client.cl_time
308 * for lease renewal.
309 *
310 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
311 * for last close replay.
312 */
313static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
314static int reclaim_str_hashtbl_size = 0;
315static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
316static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
317static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
318static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
319static struct list_head client_lru;
320static struct list_head close_lru;
321
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400322/*
323 * We store the NONE, READ, WRITE, and BOTH bits separately in the
324 * st_{access,deny}_bmap field of the stateid, in order to track not
325 * only what share bits are currently in force, but also what
326 * combinations of share bits previous opens have used. This allows us
327 * to enforce the recommendation of rfc 3530 14.2.19 that the server
328 * return an error if the client attempt to downgrade to a combination
329 * of share bits not explicable by closing some of its previous opens.
330 *
331 * XXX: This enforcement is actually incomplete, since we don't keep
332 * track of access/deny bit combinations; so, e.g., we allow:
333 *
334 * OPEN allow read, deny write
335 * OPEN allow both, deny none
336 * DOWNGRADE allow read, deny none
337 *
338 * which we should reject.
339 */
340static void
341set_access(unsigned int *access, unsigned long bmap) {
342 int i;
343
344 *access = 0;
345 for (i = 1; i < 4; i++) {
346 if (test_bit(i, &bmap))
347 *access |= i;
348 }
349}
350
351static void
352set_deny(unsigned int *deny, unsigned long bmap) {
353 int i;
354
355 *deny = 0;
356 for (i = 0; i < 4; i++) {
357 if (test_bit(i, &bmap))
358 *deny |= i ;
359 }
360}
361
362static int
363test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
364 unsigned int access, deny;
365
366 set_access(&access, stp->st_access_bmap);
367 set_deny(&deny, stp->st_deny_bmap);
368 if ((access & open->op_share_deny) || (deny & open->op_share_access))
369 return 0;
370 return 1;
371}
372
373static int nfs4_access_to_omode(u32 access)
374{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400375 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400376 case NFS4_SHARE_ACCESS_READ:
377 return O_RDONLY;
378 case NFS4_SHARE_ACCESS_WRITE:
379 return O_WRONLY;
380 case NFS4_SHARE_ACCESS_BOTH:
381 return O_RDWR;
382 }
383 BUG();
384}
385
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500386static void unhash_generic_stateid(struct nfs4_stateid *stp)
387{
388 list_del(&stp->st_hash);
389 list_del(&stp->st_perfile);
390 list_del(&stp->st_perstateowner);
391}
392
393static void free_generic_stateid(struct nfs4_stateid *stp)
394{
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400395 int i;
J. Bruce Fields0997b172011-03-02 18:01:35 -0500396
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800397 if (stp->st_access_bmap) {
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400398 for (i = 1; i < 4; i++) {
399 if (test_bit(i, &stp->st_access_bmap))
400 nfs4_file_put_access(stp->st_file,
401 nfs4_access_to_omode(i));
402 }
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800403 }
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400404 put_nfs4_file(stp->st_file);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500405 kmem_cache_free(stateid_slab, stp);
406}
407
408static void release_lock_stateid(struct nfs4_stateid *stp)
409{
410 struct file *file;
411
412 unhash_generic_stateid(stp);
413 file = find_any_file(stp->st_file);
414 if (file)
415 locks_remove_posix(file, (fl_owner_t)stp->st_stateowner);
416 free_generic_stateid(stp);
417}
418
419static void unhash_lockowner(struct nfs4_stateowner *sop)
420{
421 struct nfs4_stateid *stp;
422
423 list_del(&sop->so_idhash);
424 list_del(&sop->so_strhash);
425 list_del(&sop->so_perstateid);
426 while (!list_empty(&sop->so_stateids)) {
427 stp = list_first_entry(&sop->so_stateids,
428 struct nfs4_stateid, st_perstateowner);
429 release_lock_stateid(stp);
430 }
431}
432
433static void release_lockowner(struct nfs4_stateowner *sop)
434{
435 unhash_lockowner(sop);
436 nfs4_put_stateowner(sop);
437}
438
439static void
440release_stateid_lockowners(struct nfs4_stateid *open_stp)
441{
442 struct nfs4_stateowner *lock_sop;
443
444 while (!list_empty(&open_stp->st_lockowners)) {
445 lock_sop = list_entry(open_stp->st_lockowners.next,
446 struct nfs4_stateowner, so_perstateid);
447 /* list_del(&open_stp->st_lockowners); */
448 BUG_ON(lock_sop->so_is_open_owner);
449 release_lockowner(lock_sop);
450 }
451}
452
J. Bruce Fields22839632009-01-11 14:27:17 -0500453static void release_open_stateid(struct nfs4_stateid *stp)
454{
455 unhash_generic_stateid(stp);
456 release_stateid_lockowners(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500457 free_generic_stateid(stp);
458}
459
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500460static void unhash_openowner(struct nfs4_stateowner *sop)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500461{
462 struct nfs4_stateid *stp;
463
464 list_del(&sop->so_idhash);
465 list_del(&sop->so_strhash);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500466 list_del(&sop->so_perclient);
467 list_del(&sop->so_perstateid); /* XXX: necessary? */
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500468 while (!list_empty(&sop->so_stateids)) {
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500469 stp = list_first_entry(&sop->so_stateids,
470 struct nfs4_stateid, st_perstateowner);
471 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500472 }
473}
474
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500475static void release_openowner(struct nfs4_stateowner *sop)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500476{
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500477 unhash_openowner(sop);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500478 list_del(&sop->so_close_lru);
479 nfs4_put_stateowner(sop);
480}
481
Marc Eshel5282fd72009-04-03 08:27:52 +0300482#define SESSION_HASH_SIZE 512
483static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
484
485static inline int
486hash_sessionid(struct nfs4_sessionid *sessionid)
487{
488 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
489
490 return sid->sequence % SESSION_HASH_SIZE;
491}
492
493static inline void
494dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
495{
496 u32 *ptr = (u32 *)(&sessionid->data[0]);
497 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
498}
499
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300500static void
501gen_sessionid(struct nfsd4_session *ses)
502{
503 struct nfs4_client *clp = ses->se_client;
504 struct nfsd4_sessionid *sid;
505
506 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
507 sid->clientid = clp->cl_clientid;
508 sid->sequence = current_sessionid++;
509 sid->reserved = 0;
510}
511
512/*
Andy Adamsona6496372009-08-28 08:45:01 -0400513 * The protocol defines ca_maxresponssize_cached to include the size of
514 * the rpc header, but all we need to cache is the data starting after
515 * the end of the initial SEQUENCE operation--the rest we regenerate
516 * each time. Therefore we can advertise a ca_maxresponssize_cached
517 * value that is the number of bytes in our cache plus a few additional
518 * bytes. In order to stay on the safe side, and not promise more than
519 * we can cache, those additional bytes must be the minimum possible: 24
520 * bytes of rpc header (xid through accept state, with AUTH_NULL
521 * verifier), 12 for the compound header (with zero-length tag), and 44
522 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300523 */
Andy Adamsona6496372009-08-28 08:45:01 -0400524#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
525
Andy Adamson557ce262009-08-28 08:45:04 -0400526static void
527free_session_slots(struct nfsd4_session *ses)
528{
529 int i;
530
531 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
532 kfree(ses->se_slots[i]);
533}
534
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400535/*
536 * We don't actually need to cache the rpc and session headers, so we
537 * can allocate a little less for each slot:
538 */
539static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
540{
541 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
542}
543
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400544static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300545{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400546 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
547 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300548
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400549 return size;
550}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300551
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400552/*
553 * XXX: If we run out of reserved DRC memory we could (up to a point)
554 * re-negotiate active sessions and reduce their slot usage to make
555 * rooom for new connections. For now we just fail the create session.
556 */
557static int nfsd4_get_drc_mem(int slotsize, u32 num)
558{
559 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300560
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400561 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400562
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400563 spin_lock(&nfsd_drc_lock);
564 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
565 nfsd_drc_max_mem - nfsd_drc_mem_used);
566 num = min_t(int, num, avail / slotsize);
567 nfsd_drc_mem_used += num * slotsize;
568 spin_unlock(&nfsd_drc_lock);
569
570 return num;
571}
572
573static void nfsd4_put_drc_mem(int slotsize, int num)
574{
575 spin_lock(&nfsd_drc_lock);
576 nfsd_drc_mem_used -= slotsize * num;
577 spin_unlock(&nfsd_drc_lock);
578}
579
580static struct nfsd4_session *alloc_session(int slotsize, int numslots)
581{
582 struct nfsd4_session *new;
583 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300584
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400585 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400586 + sizeof(struct nfsd4_session) > PAGE_SIZE);
587 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400588
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400589 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300590 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400591 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400592 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400593 for (i = 0; i < numslots; i++) {
594 mem = sizeof(struct nfsd4_slot) + slotsize;
595 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
596 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400597 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400598 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400599 return new;
600out_free:
601 while (i--)
602 kfree(new->se_slots[i]);
603 kfree(new);
604 return NULL;
605}
606
607static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
608{
609 u32 maxrpc = nfsd_serv->sv_max_mesg;
610
611 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800612 new->maxresp_cached = min_t(u32, req->maxresp_cached,
613 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400614 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
615 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
616 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
617}
618
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400619static void free_conn(struct nfsd4_conn *c)
620{
621 svc_xprt_put(c->cn_xprt);
622 kfree(c);
623}
624
625static void nfsd4_conn_lost(struct svc_xpt_user *u)
626{
627 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
628 struct nfs4_client *clp = c->cn_session->se_client;
629
630 spin_lock(&clp->cl_lock);
631 if (!list_empty(&c->cn_persession)) {
632 list_del(&c->cn_persession);
633 free_conn(c);
634 }
635 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500636 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400637}
638
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400639static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400640{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400641 struct nfsd4_conn *conn;
642
643 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
644 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400645 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400646 svc_xprt_get(rqstp->rq_xprt);
647 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400648 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400649 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
650 return conn;
651}
652
J. Bruce Fields328ead22010-09-29 16:11:06 -0400653static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
654{
655 conn->cn_session = ses;
656 list_add(&conn->cn_persession, &ses->se_conns);
657}
658
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400659static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
660{
661 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400662
663 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400664 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400665 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400666}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400667
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400668static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400669{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400670 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400671 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400672}
673
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400674static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400675{
676 struct nfsd4_conn *conn;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400677 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400678
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400679 conn = alloc_conn(rqstp, dir);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400680 if (!conn)
681 return nfserr_jukebox;
682 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400683 ret = nfsd4_register_conn(conn);
684 if (ret)
685 /* oops; xprt is already down: */
686 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400687 return nfs_ok;
688}
689
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400690static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
691{
692 u32 dir = NFS4_CDFC4_FORE;
693
694 if (ses->se_flags & SESSION4_BACK_CHAN)
695 dir |= NFS4_CDFC4_BACK;
696
697 return nfsd4_new_conn(rqstp, ses, dir);
698}
699
700/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400701static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400702{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400703 struct nfs4_client *clp = s->se_client;
704 struct nfsd4_conn *c;
705
706 spin_lock(&clp->cl_lock);
707 while (!list_empty(&s->se_conns)) {
708 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
709 list_del_init(&c->cn_persession);
710 spin_unlock(&clp->cl_lock);
711
712 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
713 free_conn(c);
714
715 spin_lock(&clp->cl_lock);
716 }
717 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400718}
719
720void free_session(struct kref *kref)
721{
722 struct nfsd4_session *ses;
723 int mem;
724
725 ses = container_of(kref, struct nfsd4_session, se_ref);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400726 nfsd4_del_conns(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400727 spin_lock(&nfsd_drc_lock);
728 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
729 nfsd_drc_mem_used -= mem;
730 spin_unlock(&nfsd_drc_lock);
731 free_session_slots(ses);
732 kfree(ses);
733}
734
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400735static 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 -0400736{
737 struct nfsd4_session *new;
738 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
739 int numslots, slotsize;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400740 int status;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400741 int idx;
742
743 /*
744 * Note decreasing slot size below client's request may
745 * make it difficult for client to function correctly, whereas
746 * decreasing the number of slots will (just?) affect
747 * performance. When short on memory we therefore prefer to
748 * decrease number of slots instead of their size.
749 */
750 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
751 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800752 if (numslots < 1)
753 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400754
755 new = alloc_session(slotsize, numslots);
756 if (!new) {
757 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400758 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400759 }
760 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
Andy Adamson557ce262009-08-28 08:45:04 -0400761
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300762 new->se_client = clp;
763 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300764
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400765 INIT_LIST_HEAD(&new->se_conns);
766
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400767 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300768 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400769 new->se_cb_prog = cses->callback_prog;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300770 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400771 idx = hash_sessionid(&new->se_sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300772 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300773 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400774 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300775 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400776 spin_unlock(&clp->cl_lock);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300777 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300778
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400779 status = nfsd4_new_conn_from_crses(rqstp, new);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400780 /* whoops: benny points out, status is ignored! (err, or bogus) */
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400781 if (status) {
782 free_session(&new->se_ref);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400783 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400784 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400785 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400786 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400787 /*
788 * This is a little silly; with sessions there's no real
789 * use for the callback address. Use the peer address
790 * as a reasonable default for now, but consider fixing
791 * the rpc client not to require an address in the
792 * future:
793 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400794 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
795 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400796 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400797 nfsd4_probe_callback(clp);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400798 return new;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300799}
800
Benny Halevy9089f1b2010-05-12 00:12:26 +0300801/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300802static struct nfsd4_session *
803find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
804{
805 struct nfsd4_session *elem;
806 int idx;
807
808 dump_sessionid(__func__, sessionid);
809 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300810 /* Search in the appropriate list */
811 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300812 if (!memcmp(elem->se_sessionid.data, sessionid->data,
813 NFS4_MAX_SESSIONID_LEN)) {
814 return elem;
815 }
816 }
817
818 dprintk("%s: session not found\n", __func__);
819 return NULL;
820}
821
Benny Halevy9089f1b2010-05-12 00:12:26 +0300822/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300823static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300824unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300825{
826 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400827 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300828 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400829 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +0300830}
831
Benny Halevy36acb662010-05-12 00:13:04 +0300832/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300834renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Benny Halevy07cd4902010-05-12 00:13:41 +0300836 if (is_client_expired(clp)) {
837 dprintk("%s: client (clientid %08x/%08x) already expired\n",
838 __func__,
839 clp->cl_clientid.cl_boot,
840 clp->cl_clientid.cl_id);
841 return;
842 }
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /*
845 * Move client to the end to the LRU list.
846 */
847 dprintk("renewing client (clientid %08x/%08x)\n",
848 clp->cl_clientid.cl_boot,
849 clp->cl_clientid.cl_id);
850 list_move_tail(&clp->cl_lru, &client_lru);
851 clp->cl_time = get_seconds();
852}
853
Benny Halevy36acb662010-05-12 00:13:04 +0300854static inline void
855renew_client(struct nfs4_client *clp)
856{
857 spin_lock(&client_lock);
858 renew_client_locked(clp);
859 spin_unlock(&client_lock);
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
863static int
864STALE_CLIENTID(clientid_t *clid)
865{
866 if (clid->cl_boot == boot_time)
867 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +0300868 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
869 clid->cl_boot, clid->cl_id, boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return 1;
871}
872
873/*
874 * XXX Should we use a slab cache ?
875 * This type of memory management is somewhat inefficient, but we use it
876 * anyway since SETCLIENTID is not a common operation.
877 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500878static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct nfs4_client *clp;
881
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500882 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
883 if (clp == NULL)
884 return NULL;
885 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
886 if (clp->cl_name.data == NULL) {
887 kfree(clp);
888 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500890 memcpy(clp->cl_name.data, name.data, name.len);
891 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return clp;
893}
894
895static inline void
896free_client(struct nfs4_client *clp)
897{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400898 while (!list_empty(&clp->cl_sessions)) {
899 struct nfsd4_session *ses;
900 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
901 se_perclnt);
902 list_del(&ses->se_perclnt);
903 nfsd4_put_session(ses);
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (clp->cl_cred.cr_group_info)
906 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500907 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 kfree(clp->cl_name.data);
909 kfree(clp);
910}
911
Benny Halevyd7682982010-05-12 00:13:54 +0300912void
913release_session_client(struct nfsd4_session *session)
914{
915 struct nfs4_client *clp = session->se_client;
916
917 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
918 return;
919 if (is_client_expired(clp)) {
920 free_client(clp);
921 session->se_client = NULL;
922 } else
923 renew_client_locked(clp);
924 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +0300925}
926
Benny Halevy84d38ac2010-05-12 00:13:16 +0300927/* must be called under the client_lock */
928static inline void
929unhash_client_locked(struct nfs4_client *clp)
930{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400931 struct nfsd4_session *ses;
932
Benny Halevy07cd4902010-05-12 00:13:41 +0300933 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300934 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400935 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400936 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
937 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400938 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941static void
942expire_client(struct nfs4_client *clp)
943{
944 struct nfs4_stateowner *sop;
945 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 struct list_head reaplist;
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 INIT_LIST_HEAD(&reaplist);
949 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700950 while (!list_empty(&clp->cl_delegations)) {
951 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -0700952 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 list_move(&dp->dl_recall_lru, &reaplist);
954 }
955 spin_unlock(&recall_lock);
956 while (!list_empty(&reaplist)) {
957 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
958 list_del_init(&dp->dl_recall_lru);
959 unhash_delegation(dp);
960 }
NeilBrownea1da632005-06-23 22:04:17 -0700961 while (!list_empty(&clp->cl_openowners)) {
962 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500963 release_openowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400965 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500966 if (clp->cl_cb_conn.cb_xprt)
967 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300968 list_del(&clp->cl_idhash);
969 list_del(&clp->cl_strhash);
970 spin_lock(&client_lock);
971 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +0300972 if (atomic_read(&clp->cl_refcount) == 0)
973 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300974 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500977static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
978{
979 memcpy(target->cl_verifier.data, source->data,
980 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500983static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
984{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
986 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
987}
988
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500989static void copy_cred(struct svc_cred *target, struct svc_cred *source)
990{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 target->cr_uid = source->cr_uid;
992 target->cr_gid = source->cr_gid;
993 target->cr_group_info = source->cr_group_info;
994 get_group_info(target->cr_group_info);
995}
996
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500997static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400998{
NeilBrowna55370a2005-06-23 22:03:52 -0700999 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001003same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1004{
1005 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001009same_clid(clientid_t *cl1, clientid_t *cl2)
1010{
1011 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/* XXX what about NGROUP */
1015static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001016same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1017{
1018 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001021static void gen_clid(struct nfs4_client *clp)
1022{
1023 static u32 current_clientid = 1;
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 clp->cl_clientid.cl_boot = boot_time;
1026 clp->cl_clientid.cl_id = current_clientid++;
1027}
1028
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001029static void gen_confirm(struct nfs4_client *clp)
1030{
1031 static u32 i;
1032 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001035 *p++ = get_seconds();
1036 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001039static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1040 struct svc_rqst *rqstp, nfs4_verifier *verf)
1041{
1042 struct nfs4_client *clp;
1043 struct sockaddr *sa = svc_addr(rqstp);
1044 char *princ;
1045
1046 clp = alloc_client(name);
1047 if (clp == NULL)
1048 return NULL;
1049
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001050 INIT_LIST_HEAD(&clp->cl_sessions);
1051
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001052 princ = svc_gss_principal(rqstp);
1053 if (princ) {
1054 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1055 if (clp->cl_principal == NULL) {
1056 free_client(clp);
1057 return NULL;
1058 }
1059 }
1060
1061 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Benny Halevy46583e22010-05-12 00:13:29 +03001062 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001063 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001064 INIT_LIST_HEAD(&clp->cl_idhash);
1065 INIT_LIST_HEAD(&clp->cl_strhash);
1066 INIT_LIST_HEAD(&clp->cl_openowners);
1067 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001068 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001069 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001070 spin_lock_init(&clp->cl_lock);
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001071 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
Benny Halevy07cd4902010-05-12 00:13:41 +03001072 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001073 clear_bit(0, &clp->cl_cb_slot_busy);
1074 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1075 copy_verf(clp, verf);
1076 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1077 clp->cl_flavor = rqstp->rq_flavor;
1078 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1079 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001080 clp->cl_cb_session = NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001081 return clp;
1082}
1083
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001084static int check_name(struct xdr_netobj name)
1085{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (name.len == 0)
1087 return 0;
1088 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001089 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return 0;
1091 }
1092 return 1;
1093}
1094
NeilBrownfd39ca92005-06-23 22:04:03 -07001095static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1097{
1098 unsigned int idhashval;
1099
1100 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1101 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1102 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001103 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
NeilBrownfd39ca92005-06-23 22:04:03 -07001106static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107move_to_confirmed(struct nfs4_client *clp)
1108{
1109 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1110 unsigned int strhashval;
1111
1112 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001113 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07001114 strhashval = clientstr_hashval(clp->cl_recdir);
Benny Halevy328efba2010-05-12 00:12:51 +03001115 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 renew_client(clp);
1117}
1118
1119static struct nfs4_client *
1120find_confirmed_client(clientid_t *clid)
1121{
1122 struct nfs4_client *clp;
1123 unsigned int idhashval = clientid_hashval(clid->cl_id);
1124
1125 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001126 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return clp;
1128 }
1129 return NULL;
1130}
1131
1132static struct nfs4_client *
1133find_unconfirmed_client(clientid_t *clid)
1134{
1135 struct nfs4_client *clp;
1136 unsigned int idhashval = clientid_hashval(clid->cl_id);
1137
1138 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001139 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return clp;
1141 }
1142 return NULL;
1143}
1144
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001145static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001146{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001147 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001148}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001149
NeilBrown28ce6052005-06-23 22:03:56 -07001150static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001151find_confirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001152{
1153 struct nfs4_client *clp;
1154
1155 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001156 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001157 return clp;
1158 }
1159 return NULL;
1160}
1161
1162static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001163find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001164{
1165 struct nfs4_client *clp;
1166
1167 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001168 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001169 return clp;
1170 }
1171 return NULL;
1172}
1173
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001174static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1175{
1176 switch (family) {
1177 case AF_INET:
1178 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1179 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1180 return;
1181 case AF_INET6:
1182 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1183 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1184 return;
1185 }
1186}
1187
NeilBrownfd39ca92005-06-23 22:04:03 -07001188static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001189gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001191 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001192 struct sockaddr *sa = svc_addr(rqstp);
1193 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001194 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Jeff Layton7077ecb2009-08-14 12:57:58 -04001196 /* Currently, we only support tcp and tcp6 for the callback channel */
1197 if (se->se_callback_netid_len == 3 &&
1198 !memcmp(se->se_callback_netid_val, "tcp", 3))
1199 expected_family = AF_INET;
1200 else if (se->se_callback_netid_len == 4 &&
1201 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1202 expected_family = AF_INET6;
1203 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 goto out_err;
1205
J. Bruce Fields07263f12010-05-31 19:09:40 -04001206 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001207 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001208 (struct sockaddr *)&conn->cb_addr,
1209 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001210
J. Bruce Fields07263f12010-05-31 19:09:40 -04001211 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001213
J. Bruce Fields07263f12010-05-31 19:09:40 -04001214 if (conn->cb_addr.ss_family == AF_INET6)
1215 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001216
J. Bruce Fields07263f12010-05-31 19:09:40 -04001217 conn->cb_prog = se->se_callback_prog;
1218 conn->cb_ident = se->se_callback_ident;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001219 rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return;
1221out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001222 conn->cb_addr.ss_family = AF_UNSPEC;
1223 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001224 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 "will not receive delegations\n",
1226 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return;
1229}
1230
Andy Adamson074fe892009-04-03 08:28:15 +03001231/*
Andy Adamson557ce262009-08-28 08:45:04 -04001232 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001233 */
1234void
1235nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1236{
Andy Adamson557ce262009-08-28 08:45:04 -04001237 struct nfsd4_slot *slot = resp->cstate.slot;
1238 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001239
Andy Adamson557ce262009-08-28 08:45:04 -04001240 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001241
Andy Adamson557ce262009-08-28 08:45:04 -04001242 slot->sl_opcnt = resp->opcnt;
1243 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001244
1245 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001246 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001247 return;
1248 }
Andy Adamson557ce262009-08-28 08:45:04 -04001249 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1250 base = (char *)resp->cstate.datap -
1251 (char *)resp->xbuf->head[0].iov_base;
1252 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1253 slot->sl_datalen))
1254 WARN("%s: sessions DRC could not cache compound\n", __func__);
1255 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001256}
1257
1258/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001259 * Encode the replay sequence operation from the slot values.
1260 * If cachethis is FALSE encode the uncached rep error on the next
1261 * operation which sets resp->p and increments resp->opcnt for
1262 * nfs4svc_encode_compoundres.
1263 *
Andy Adamson074fe892009-04-03 08:28:15 +03001264 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001265static __be32
1266nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1267 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001268{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001269 struct nfsd4_op *op;
1270 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001271
Andy Adamsonabfabf82009-07-23 19:02:18 -04001272 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
Andy Adamson557ce262009-08-28 08:45:04 -04001273 resp->opcnt, resp->cstate.slot->sl_cachethis);
Andy Adamsonabfabf82009-07-23 19:02:18 -04001274
1275 /* Encode the replayed sequence operation */
1276 op = &args->ops[resp->opcnt - 1];
1277 nfsd4_encode_operation(resp, op);
1278
1279 /* Return nfserr_retry_uncached_rep in next operation. */
Andy Adamson557ce262009-08-28 08:45:04 -04001280 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001281 op = &args->ops[resp->opcnt++];
1282 op->status = nfserr_retry_uncached_rep;
1283 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001284 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001285 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001286}
1287
1288/*
Andy Adamson557ce262009-08-28 08:45:04 -04001289 * The sequence operation is not cached because we can use the slot and
1290 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001291 */
1292__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001293nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1294 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001295{
Andy Adamson557ce262009-08-28 08:45:04 -04001296 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001297 __be32 status;
1298
Andy Adamson557ce262009-08-28 08:45:04 -04001299 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001300
Andy Adamsonabfabf82009-07-23 19:02:18 -04001301 /* Either returns 0 or nfserr_retry_uncached */
1302 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1303 if (status == nfserr_retry_uncached_rep)
1304 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001305
Andy Adamson557ce262009-08-28 08:45:04 -04001306 /* The sequence operation has been encoded, cstate->datap set. */
1307 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001308
Andy Adamson557ce262009-08-28 08:45:04 -04001309 resp->opcnt = slot->sl_opcnt;
1310 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1311 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001312
1313 return status;
1314}
1315
Andy Adamson0733d212009-04-03 08:28:01 +03001316/*
1317 * Set the exchange_id flags returned by the server.
1318 */
1319static void
1320nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1321{
1322 /* pNFS is not supported */
1323 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1324
1325 /* Referrals are supported, Migration is not. */
1326 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1327
1328 /* set the wire flags to return to client. */
1329 clid->flags = new->cl_exchange_flags;
1330}
1331
Al Virob37ad282006-10-19 23:28:59 -07001332__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001333nfsd4_exchange_id(struct svc_rqst *rqstp,
1334 struct nfsd4_compound_state *cstate,
1335 struct nfsd4_exchange_id *exid)
1336{
Andy Adamson0733d212009-04-03 08:28:01 +03001337 struct nfs4_client *unconf, *conf, *new;
1338 int status;
1339 unsigned int strhashval;
1340 char dname[HEXDIR_LEN];
Jeff Layton363168b2009-08-14 12:57:56 -04001341 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001342 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001343 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamson0733d212009-04-03 08:28:01 +03001344
Jeff Layton363168b2009-08-14 12:57:56 -04001345 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001346 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001347 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001348 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001349 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001350
1351 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1352 return nfserr_inval;
1353
1354 /* Currently only support SP4_NONE */
1355 switch (exid->spa_how) {
1356 case SP4_NONE:
1357 break;
1358 case SP4_SSV:
J. Bruce Fields044bc1d2010-11-12 14:36:06 -05001359 return nfserr_serverfault;
Andy Adamson0733d212009-04-03 08:28:01 +03001360 default:
1361 BUG(); /* checked by xdr code */
1362 case SP4_MACH_CRED:
1363 return nfserr_serverfault; /* no excuse :-/ */
1364 }
1365
1366 status = nfs4_make_rec_clidname(dname, &exid->clname);
1367
1368 if (status)
1369 goto error;
1370
1371 strhashval = clientstr_hashval(dname);
1372
1373 nfs4_lock_state();
1374 status = nfs_ok;
1375
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001376 conf = find_confirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001377 if (conf) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001378 if (!clp_used_exchangeid(conf)) {
1379 status = nfserr_clid_inuse; /* XXX: ? */
1380 goto out;
1381 }
Andy Adamson0733d212009-04-03 08:28:01 +03001382 if (!same_verf(&verf, &conf->cl_verifier)) {
1383 /* 18.35.4 case 8 */
1384 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1385 status = nfserr_not_same;
1386 goto out;
1387 }
1388 /* Client reboot: destroy old state */
1389 expire_client(conf);
1390 goto out_new;
1391 }
1392 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1393 /* 18.35.4 case 9 */
1394 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1395 status = nfserr_perm;
1396 goto out;
1397 }
1398 expire_client(conf);
1399 goto out_new;
1400 }
Andy Adamson0733d212009-04-03 08:28:01 +03001401 /*
1402 * Set bit when the owner id and verifier map to an already
1403 * confirmed client id (18.35.3).
1404 */
1405 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1406
1407 /*
1408 * Falling into 18.35.4 case 2, possible router replay.
1409 * Leave confirmed record intact and return same result.
1410 */
1411 copy_verf(conf, &verf);
1412 new = conf;
1413 goto out_copy;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001414 }
1415
1416 /* 18.35.4 case 7 */
1417 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1418 status = nfserr_noent;
1419 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001420 }
1421
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001422 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001423 if (unconf) {
1424 /*
1425 * Possible retry or client restart. Per 18.35.4 case 4,
1426 * a new unconfirmed record should be generated regardless
1427 * of whether any properties have changed.
1428 */
1429 expire_client(unconf);
1430 }
1431
1432out_new:
1433 /* Normal case */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001434 new = create_client(exid->clname, dname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001435 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001436 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001437 goto out;
1438 }
1439
Andy Adamson0733d212009-04-03 08:28:01 +03001440 gen_clid(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001441 add_to_unconfirmed(new, strhashval);
1442out_copy:
1443 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1444 exid->clientid.cl_id = new->cl_clientid.cl_id;
1445
Andy Adamson38eb76a2009-04-03 08:28:32 +03001446 exid->seqid = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001447 nfsd4_set_ex_flags(new, exid);
1448
1449 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001450 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001451 status = nfs_ok;
1452
1453out:
1454 nfs4_unlock_state();
1455error:
1456 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1457 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001458}
1459
Benny Halevyb85d4c02009-04-03 08:28:08 +03001460static int
Andy Adamson88e588d2009-07-23 19:02:15 -04001461check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001462{
Andy Adamson88e588d2009-07-23 19:02:15 -04001463 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1464 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001465
1466 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001467 if (slot_inuse) {
1468 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001469 return nfserr_jukebox;
1470 else
1471 return nfserr_seq_misordered;
1472 }
1473 /* Normal */
Andy Adamson88e588d2009-07-23 19:02:15 -04001474 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001475 return nfs_ok;
1476 /* Replay */
Andy Adamson88e588d2009-07-23 19:02:15 -04001477 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001478 return nfserr_replay_cache;
1479 /* Wraparound */
Andy Adamson88e588d2009-07-23 19:02:15 -04001480 if (seqid == 1 && (slot_seqid + 1) == 0)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001481 return nfs_ok;
1482 /* Misordered replay or misordered new request */
1483 return nfserr_seq_misordered;
1484}
1485
Andy Adamson49557cc2009-07-23 19:02:16 -04001486/*
1487 * Cache the create session result into the create session single DRC
1488 * slot cache by saving the xdr structure. sl_seqid has been set.
1489 * Do this for solo or embedded create session operations.
1490 */
1491static void
1492nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1493 struct nfsd4_clid_slot *slot, int nfserr)
1494{
1495 slot->sl_status = nfserr;
1496 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1497}
1498
1499static __be32
1500nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1501 struct nfsd4_clid_slot *slot)
1502{
1503 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1504 return slot->sl_status;
1505}
1506
Mi Jinlong1b74c252011-07-14 14:50:17 +08001507#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1508 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1509 1 + /* MIN tag is length with zero, only length */ \
1510 3 + /* version, opcount, opcode */ \
1511 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1512 /* seqid, slotID, slotID, cache */ \
1513 4 ) * sizeof(__be32))
1514
1515#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1516 2 + /* verifier: AUTH_NULL, length 0 */\
1517 1 + /* status */ \
1518 1 + /* MIN tag is length with zero, only length */ \
1519 3 + /* opcount, opcode, opstatus*/ \
1520 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1521 /* seqid, slotID, slotID, slotID, status */ \
1522 5 ) * sizeof(__be32))
1523
1524static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1525{
1526 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1527 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1528}
1529
Andy Adamson069b6ad2009-04-03 08:27:58 +03001530__be32
1531nfsd4_create_session(struct svc_rqst *rqstp,
1532 struct nfsd4_compound_state *cstate,
1533 struct nfsd4_create_session *cr_ses)
1534{
Jeff Layton363168b2009-08-14 12:57:56 -04001535 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001536 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001537 struct nfsd4_session *new;
Andy Adamson49557cc2009-07-23 19:02:16 -04001538 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001539 bool confirm_me = false;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001540 int status = 0;
1541
Mi Jinlonga62573d2011-03-23 17:57:07 +08001542 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1543 return nfserr_inval;
1544
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001545 nfs4_lock_state();
1546 unconf = find_unconfirmed_client(&cr_ses->clientid);
1547 conf = find_confirmed_client(&cr_ses->clientid);
1548
1549 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001550 cs_slot = &conf->cl_cs_slot;
1551 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001552 if (status == nfserr_replay_cache) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001553 dprintk("Got a create_session replay! seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001554 cs_slot->sl_seqid);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001555 /* Return the cached reply status */
Andy Adamson49557cc2009-07-23 19:02:16 -04001556 status = nfsd4_replay_create_session(cr_ses, cs_slot);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001557 goto out;
Andy Adamson49557cc2009-07-23 19:02:16 -04001558 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001559 status = nfserr_seq_misordered;
1560 dprintk("Sequence misordered!\n");
1561 dprintk("Expected seqid= %d but got seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001562 cs_slot->sl_seqid, cr_ses->seqid);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001563 goto out;
1564 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001565 } else if (unconf) {
1566 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001567 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001568 status = nfserr_clid_inuse;
1569 goto out;
1570 }
1571
Andy Adamson49557cc2009-07-23 19:02:16 -04001572 cs_slot = &unconf->cl_cs_slot;
1573 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001574 if (status) {
1575 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001576 status = nfserr_seq_misordered;
J. Bruce Fieldscd5b8142010-10-02 17:03:35 -04001577 goto out;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001578 }
1579
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001580 confirm_me = true;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001581 conf = unconf;
1582 } else {
1583 status = nfserr_stale_clientid;
1584 goto out;
1585 }
1586
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001587 /*
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001588 * XXX: we should probably set this at creation time, and check
1589 * for consistent minorversion use throughout:
1590 */
1591 conf->cl_minorversion = 1;
1592 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001593 * We do not support RDMA or persistent sessions
1594 */
1595 cr_ses->flags &= ~SESSION4_PERSIST;
1596 cr_ses->flags &= ~SESSION4_RDMA;
1597
Mi Jinlong1b74c252011-07-14 14:50:17 +08001598 status = nfserr_toosmall;
1599 if (check_forechannel_attrs(cr_ses->fore_channel))
1600 goto out;
1601
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001602 status = nfserr_jukebox;
1603 new = alloc_init_session(rqstp, conf, cr_ses);
1604 if (!new)
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001605 goto out;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001606 status = nfs_ok;
1607 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001608 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001609 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1610 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001611 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001612 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001613
Andy Adamson49557cc2009-07-23 19:02:16 -04001614 /* cache solo and embedded create sessions under the state lock */
1615 nfsd4_cache_create_session(cr_ses, cs_slot, status);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001616 if (confirm_me)
1617 move_to_confirmed(conf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001618out:
1619 nfs4_unlock_state();
1620 dprintk("%s returns %d\n", __func__, ntohl(status));
1621 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001622}
1623
J. Bruce Fields57716352010-04-21 12:27:19 -04001624static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1625{
1626 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1627 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1628
1629 return argp->opcnt == resp->opcnt;
1630}
1631
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001632static __be32 nfsd4_map_bcts_dir(u32 *dir)
1633{
1634 switch (*dir) {
1635 case NFS4_CDFC4_FORE:
1636 case NFS4_CDFC4_BACK:
1637 return nfs_ok;
1638 case NFS4_CDFC4_FORE_OR_BOTH:
1639 case NFS4_CDFC4_BACK_OR_BOTH:
1640 *dir = NFS4_CDFC4_BOTH;
1641 return nfs_ok;
1642 };
1643 return nfserr_inval;
1644}
1645
1646__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1647 struct nfsd4_compound_state *cstate,
1648 struct nfsd4_bind_conn_to_session *bcts)
1649{
1650 __be32 status;
1651
1652 if (!nfsd4_last_compound_op(rqstp))
1653 return nfserr_not_only_op;
1654 spin_lock(&client_lock);
1655 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1656 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1657 * client_lock iself: */
1658 if (cstate->session) {
1659 nfsd4_get_session(cstate->session);
1660 atomic_inc(&cstate->session->se_client->cl_refcount);
1661 }
1662 spin_unlock(&client_lock);
1663 if (!cstate->session)
1664 return nfserr_badsession;
1665
1666 status = nfsd4_map_bcts_dir(&bcts->dir);
Bryan Schumaker1db2b9d2011-04-27 15:47:15 -04001667 if (!status)
1668 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1669 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001670}
1671
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001672static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1673{
1674 if (!session)
1675 return 0;
1676 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1677}
1678
Andy Adamson069b6ad2009-04-03 08:27:58 +03001679__be32
1680nfsd4_destroy_session(struct svc_rqst *r,
1681 struct nfsd4_compound_state *cstate,
1682 struct nfsd4_destroy_session *sessionid)
1683{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001684 struct nfsd4_session *ses;
1685 u32 status = nfserr_badsession;
1686
1687 /* Notes:
1688 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1689 * - Should we return nfserr_back_chan_busy if waiting for
1690 * callbacks on to-be-destroyed session?
1691 * - Do we need to clear any callback info from previous session?
1692 */
1693
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001694 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001695 if (!nfsd4_last_compound_op(r))
1696 return nfserr_not_only_op;
1697 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001698 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001699 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001700 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1701 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001702 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001703 goto out;
1704 }
1705
1706 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001707 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001708
Benny Halevyab707e152010-05-12 00:14:06 +03001709 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001710 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001711 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001712
1713 nfsd4_del_conns(ses);
1714
Benny Halevye10e0cf2009-04-03 08:28:38 +03001715 nfsd4_put_session(ses);
1716 status = nfs_ok;
1717out:
1718 dprintk("%s returns %d\n", __func__, ntohl(status));
1719 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001720}
1721
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001722static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001723{
1724 struct nfsd4_conn *c;
1725
1726 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001727 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001728 return c;
1729 }
1730 }
1731 return NULL;
1732}
1733
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001734static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001735{
1736 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001737 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001738 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001739
1740 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001741 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001742 if (c) {
1743 spin_unlock(&clp->cl_lock);
1744 free_conn(new);
1745 return;
1746 }
1747 __nfsd4_hash_conn(new, ses);
1748 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001749 ret = nfsd4_register_conn(new);
1750 if (ret)
1751 /* oops; xprt is already down: */
1752 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001753 return;
1754}
1755
Mi Jinlong868b89c2011-04-27 09:09:58 +08001756static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1757{
1758 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1759
1760 return args->opcnt > session->se_fchannel.maxops;
1761}
1762
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001763static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1764 struct nfsd4_session *session)
1765{
1766 struct xdr_buf *xb = &rqstp->rq_arg;
1767
1768 return xb->len > session->se_fchannel.maxreq_sz;
1769}
1770
Andy Adamson069b6ad2009-04-03 08:27:58 +03001771__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001772nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001773 struct nfsd4_compound_state *cstate,
1774 struct nfsd4_sequence *seq)
1775{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001776 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001777 struct nfsd4_session *session;
1778 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001779 struct nfsd4_conn *conn;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001780 int status;
1781
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001782 if (resp->opcnt != 1)
1783 return nfserr_sequence_pos;
1784
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001785 /*
1786 * Will be either used or freed by nfsd4_sequence_check_conn
1787 * below.
1788 */
1789 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1790 if (!conn)
1791 return nfserr_jukebox;
1792
Benny Halevy9089f1b2010-05-12 00:12:26 +03001793 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001794 status = nfserr_badsession;
1795 session = find_in_sessionid_hashtbl(&seq->sessionid);
1796 if (!session)
1797 goto out;
1798
Mi Jinlong868b89c2011-04-27 09:09:58 +08001799 status = nfserr_too_many_ops;
1800 if (nfsd4_session_too_many_ops(rqstp, session))
1801 goto out;
1802
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001803 status = nfserr_req_too_big;
1804 if (nfsd4_request_too_big(rqstp, session))
1805 goto out;
1806
Benny Halevyb85d4c02009-04-03 08:28:08 +03001807 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03001808 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001809 goto out;
1810
Andy Adamson557ce262009-08-28 08:45:04 -04001811 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03001812 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1813
Andy Adamsona8dfdae2009-08-28 08:45:02 -04001814 /* We do not negotiate the number of slots yet, so set the
1815 * maxslots to the session maxreqs which is used to encode
1816 * sr_highest_slotid and the sr_target_slot id to maxslots */
1817 seq->maxslots = session->se_fchannel.maxreqs;
1818
Andy Adamson88e588d2009-07-23 19:02:15 -04001819 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001820 if (status == nfserr_replay_cache) {
1821 cstate->slot = slot;
1822 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001823 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04001824 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03001825 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03001826 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001827 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001828 }
1829 if (status)
1830 goto out;
1831
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001832 nfsd4_sequence_check_conn(conn, session);
1833 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001834
Benny Halevyb85d4c02009-04-03 08:28:08 +03001835 /* Success! bump slot seqid */
1836 slot->sl_inuse = true;
1837 slot->sl_seqid = seq->seqid;
Andy Adamson557ce262009-08-28 08:45:04 -04001838 slot->sl_cachethis = seq->cachethis;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001839
1840 cstate->slot = slot;
1841 cstate->session = session;
1842
Benny Halevyb85d4c02009-04-03 08:28:08 +03001843out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04001844 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001845 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001846 struct nfs4_client *clp = session->se_client;
1847
Benny Halevy36acb662010-05-12 00:13:04 +03001848 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001849 atomic_inc(&clp->cl_refcount);
1850 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1851 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001852 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001853 kfree(conn);
Benny Halevy36acb662010-05-12 00:13:04 +03001854 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001855 dprintk("%s: return %d\n", __func__, ntohl(status));
1856 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001857}
1858
1859__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001860nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1861{
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001862 int status = 0;
1863
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001864 if (rc->rca_one_fs) {
1865 if (!cstate->current_fh.fh_dentry)
1866 return nfserr_nofilehandle;
1867 /*
1868 * We don't take advantage of the rca_one_fs case.
1869 * That's OK, it's optional, we can safely ignore it.
1870 */
1871 return nfs_ok;
1872 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001873
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001874 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001875 status = nfserr_complete_already;
1876 if (cstate->session->se_client->cl_firststate)
1877 goto out;
1878
1879 status = nfserr_stale_clientid;
1880 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001881 /*
1882 * The following error isn't really legal.
1883 * But we only get here if the client just explicitly
1884 * destroyed the client. Surely it no longer cares what
1885 * error it gets back on an operation for the dead
1886 * client.
1887 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001888 goto out;
1889
1890 status = nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001891 nfsd4_create_clid_dir(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001892out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001893 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001894 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001895}
1896
1897__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001898nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1899 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct xdr_netobj clname = {
1902 .len = setclid->se_namelen,
1903 .data = setclid->se_name,
1904 };
1905 nfs4_verifier clverifier = setclid->se_verf;
1906 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07001907 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07001908 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -07001909 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -07001912 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
NeilBrowna55370a2005-06-23 22:03:52 -07001914 status = nfs4_make_rec_clidname(dname, &clname);
1915 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07001916 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 /*
1919 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1920 * We get here on a DRC miss.
1921 */
1922
NeilBrowna55370a2005-06-23 22:03:52 -07001923 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 nfs4_lock_state();
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001926 conf = find_confirmed_client_by_str(dname, strhashval);
NeilBrown28ce6052005-06-23 22:03:56 -07001927 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001928 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001930 if (clp_used_exchangeid(conf))
1931 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04001932 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04001933 char addr_str[INET6_ADDRSTRLEN];
1934 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1935 sizeof(addr_str));
1936 dprintk("NFSD: setclientid: string in use by client "
1937 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 goto out;
1939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001941 /*
1942 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1943 * has a description of SETCLIENTID request processing consisting
1944 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1945 */
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001946 unconf = find_unconfirmed_client_by_str(dname, strhashval);
J. Bruce Fields3e772462011-08-10 19:07:33 -04001947 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001949 /*
1950 * RFC 3530 14.2.33 CASE 4:
1951 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 */
1953 if (unconf)
1954 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001955 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07001956 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001959 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001961 * RFC 3530 14.2.33 CASE 1:
1962 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07001964 if (unconf) {
1965 /* Note this is removing unconfirmed {*x***},
1966 * which is stronger than RFC recommended {vxc**}.
1967 * This has the advantage that there is at most
1968 * one {*x***} in either list at any time.
1969 */
1970 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001972 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07001973 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 } else if (!unconf) {
1977 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001978 * RFC 3530 14.2.33 CASE 2:
1979 * probable client reboot; state will be removed if
1980 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001982 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07001983 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05001986 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001987 /*
1988 * RFC 3530 14.2.33 CASE 3:
1989 * probable client reboot; state will be removed if
1990 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 */
1992 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001993 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07001994 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001998 /*
1999 * XXX: we should probably set this at creation time, and check
2000 * for consistent minorversion use throughout:
2001 */
2002 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002003 gen_callback(new, setclid, rqstp);
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04002004 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2006 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2007 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2008 status = nfs_ok;
2009out:
2010 nfs4_unlock_state();
2011 return status;
2012}
2013
2014
2015/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002016 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2017 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2018 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 */
Al Virob37ad282006-10-19 23:28:59 -07002020__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002021nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2022 struct nfsd4_compound_state *cstate,
2023 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Jeff Layton363168b2009-08-14 12:57:56 -04002025 struct sockaddr *sa = svc_addr(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -07002026 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2028 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002029 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 if (STALE_CLIENTID(clid))
2032 return nfserr_stale_clientid;
2033 /*
2034 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2035 * We get here on a DRC miss.
2036 */
2037
2038 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002039
2040 conf = find_confirmed_client(clid);
2041 unconf = find_unconfirmed_client(clid);
2042
2043 status = nfserr_clid_inuse;
Jeff Layton363168b2009-08-14 12:57:56 -04002044 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002045 goto out;
Jeff Layton363168b2009-08-14 12:57:56 -04002046 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002047 goto out;
2048
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002049 /*
2050 * section 14.2.34 of RFC 3530 has a description of
2051 * SETCLIENTID_CONFIRM request processing consisting
2052 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2053 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -05002054 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002055 /*
2056 * RFC 3530 14.2.34 CASE 1:
2057 * callback update
2058 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002059 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 status = nfserr_clid_inuse;
2061 else {
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002062 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2063 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002064 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05002068 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002069 /*
2070 * RFC 3530 14.2.34 CASE 2:
2071 * probable retransmitted request; play it safe and
2072 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07002073 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002074 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07002076 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07002078 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002079 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002080 /*
2081 * RFC 3530 14.2.34 CASE 3:
2082 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07002083 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002084 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 status = nfserr_clid_inuse;
2086 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07002087 unsigned int hash =
2088 clientstr_hashval(unconf->cl_recdir);
2089 conf = find_confirmed_client_by_str(unconf->cl_recdir,
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002090 hash);
NeilBrown1a69c172005-06-23 22:04:08 -07002091 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07002092 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002093 expire_client(conf);
2094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07002096 conf = unconf;
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002097 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002098 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002100 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2101 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07002102 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002103 /*
2104 * RFC 3530 14.2.34 CASE 4:
2105 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07002106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07002108 } else {
NeilBrown08e89872005-06-23 22:04:11 -07002109 /* check that we have hit one of the cases...*/
2110 status = nfserr_clid_inuse;
2111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 nfs4_unlock_state();
2114 return status;
2115}
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117/* OPEN Share state helper functions */
2118static inline struct nfs4_file *
2119alloc_init_file(struct inode *ino)
2120{
2121 struct nfs4_file *fp;
2122 unsigned int hashval = file_hashval(ino);
2123
NeilBrowne60d4392005-06-23 22:03:01 -07002124 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2125 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002126 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07002128 INIT_LIST_HEAD(&fp->fi_stateids);
2129 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 fp->fi_inode = igrab(ino);
2131 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -07002132 fp->fi_had_conflict = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002133 fp->fi_lease = NULL;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002134 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2135 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Pavel Emelyanov47cee542010-05-17 20:00:37 +04002136 spin_lock(&recall_lock);
2137 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2138 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return fp;
2140 }
2141 return NULL;
2142}
2143
2144static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002145nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002146{
NeilBrowne60d4392005-06-23 22:03:01 -07002147 if (*slab == NULL)
2148 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002149 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002150 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002151}
2152
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002153void
NeilBrowne60d4392005-06-23 22:03:01 -07002154nfsd4_free_slabs(void)
2155{
2156 nfsd4_free_slab(&stateowner_slab);
2157 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002158 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002159 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162static int
2163nfsd4_init_slabs(void)
2164{
2165 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
Paul Mundt20c2df82007-07-20 10:11:58 +09002166 sizeof(struct nfs4_stateowner), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002167 if (stateowner_slab == NULL)
2168 goto out_nomem;
2169 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002170 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002171 if (file_slab == NULL)
2172 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002173 stateid_slab = kmem_cache_create("nfsd4_stateids",
Paul Mundt20c2df82007-07-20 10:11:58 +09002174 sizeof(struct nfs4_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002175 if (stateid_slab == NULL)
2176 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002177 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002178 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002179 if (deleg_slab == NULL)
2180 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002182out_nomem:
2183 nfsd4_free_slabs();
2184 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2185 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
2188void
2189nfs4_free_stateowner(struct kref *kref)
2190{
2191 struct nfs4_stateowner *sop =
2192 container_of(kref, struct nfs4_stateowner, so_ref);
2193 kfree(sop->so_owner.data);
2194 kmem_cache_free(stateowner_slab, sop);
2195}
2196
2197static inline struct nfs4_stateowner *
2198alloc_stateowner(struct xdr_netobj *owner)
2199{
2200 struct nfs4_stateowner *sop;
2201
2202 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
2203 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
2204 memcpy(sop->so_owner.data, owner->data, owner->len);
2205 sop->so_owner.len = owner->len;
2206 kref_init(&sop->so_ref);
2207 return sop;
2208 }
2209 kmem_cache_free(stateowner_slab, sop);
2210 }
2211 return NULL;
2212}
2213
2214static struct nfs4_stateowner *
2215alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2216 struct nfs4_stateowner *sop;
2217 struct nfs4_replay *rp;
2218 unsigned int idhashval;
2219
2220 if (!(sop = alloc_stateowner(&open->op_owner)))
2221 return NULL;
2222 idhashval = ownerid_hashval(current_ownerid);
2223 INIT_LIST_HEAD(&sop->so_idhash);
2224 INIT_LIST_HEAD(&sop->so_strhash);
2225 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07002226 INIT_LIST_HEAD(&sop->so_stateids);
2227 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 INIT_LIST_HEAD(&sop->so_close_lru);
2229 sop->so_time = 0;
2230 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
2231 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07002232 list_add(&sop->so_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 sop->so_is_open_owner = 1;
2234 sop->so_id = current_ownerid++;
2235 sop->so_client = clp;
2236 sop->so_seqid = open->op_seqid;
2237 sop->so_confirmed = 0;
2238 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08002239 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 rp->rp_buflen = 0;
2241 rp->rp_buf = rp->rp_ibuf;
2242 return sop;
2243}
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245static inline void
2246init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2247 struct nfs4_stateowner *sop = open->op_stateowner;
2248 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2249
2250 INIT_LIST_HEAD(&stp->st_hash);
NeilBrownea1da632005-06-23 22:04:17 -07002251 INIT_LIST_HEAD(&stp->st_perstateowner);
2252 INIT_LIST_HEAD(&stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 INIT_LIST_HEAD(&stp->st_perfile);
2254 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
NeilBrownea1da632005-06-23 22:04:17 -07002255 list_add(&stp->st_perstateowner, &sop->so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002256 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07002258 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 stp->st_file = fp;
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04002260 stp->st_stateid.si_boot = boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 stp->st_stateid.si_stateownerid = sop->so_id;
2262 stp->st_stateid.si_fileid = fp->fi_id;
2263 stp->st_stateid.si_generation = 0;
2264 stp->st_access_bmap = 0;
2265 stp->st_deny_bmap = 0;
Andy Adamson84459a12009-04-03 08:28:56 +03002266 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2267 &stp->st_access_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07002269 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
2272static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273move_to_close_lru(struct nfs4_stateowner *sop)
2274{
2275 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
2276
NeilBrown358dd552006-04-10 22:55:42 -07002277 list_move_tail(&sop->so_close_lru, &close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 sop->so_time = get_seconds();
2279}
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002282same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2283 clientid_t *clid)
2284{
2285 return (sop->so_owner.len == owner->len) &&
2286 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2287 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
2290static struct nfs4_stateowner *
2291find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2292{
2293 struct nfs4_stateowner *so = NULL;
2294
2295 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002296 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return so;
2298 }
2299 return NULL;
2300}
2301
2302/* search file_hashtbl[] for file */
2303static struct nfs4_file *
2304find_file(struct inode *ino)
2305{
2306 unsigned int hashval = file_hashval(ino);
2307 struct nfs4_file *fp;
2308
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002309 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002311 if (fp->fi_inode == ino) {
2312 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002313 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002317 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return NULL;
2319}
2320
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002321static inline int access_valid(u32 x, u32 minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002322{
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002323 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002324 return 0;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002325 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2326 return 0;
2327 x &= ~NFS4_SHARE_ACCESS_MASK;
2328 if (minorversion && x) {
2329 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2330 return 0;
2331 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2332 return 0;
2333 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2334 }
2335 if (x)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002336 return 0;
2337 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002338}
2339
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002340static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002341{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002342 /* Note: unlike access bits, deny bits may be zero. */
2343 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002344}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002346/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 * Called to check deny when READ with all zero stateid or
2348 * WRITE with all zero or all one stateid
2349 */
Al Virob37ad282006-10-19 23:28:59 -07002350static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2352{
2353 struct inode *ino = current_fh->fh_dentry->d_inode;
2354 struct nfs4_file *fp;
2355 struct nfs4_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002356 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 dprintk("NFSD: nfs4_share_conflict\n");
2359
2360 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002361 if (!fp)
2362 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002363 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002365 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2366 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2367 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
NeilBrown13cd2182005-06-23 22:03:10 -07002370 ret = nfs_ok;
2371out:
2372 put_nfs4_file(fp);
2373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002376static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 /* We're assuming the state code never drops its reference
2379 * without first removing the lease. Since we're in this lease
2380 * callback (and since the lease code is serialized by the kernel
2381 * lock) we know the server hasn't removed the lease yet, we know
2382 * it's safe to take a reference: */
2383 atomic_inc(&dp->dl_count);
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Arnd Bergmann460781b2010-11-17 16:26:56 +01002387 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 dp->dl_time = get_seconds();
2389
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002390 nfsd4_cb_recall(dp);
2391}
2392
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002393/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002394static void nfsd_break_deleg_cb(struct file_lock *fl)
2395{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002396 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2397 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002398
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002399 BUG_ON(!fp);
2400 /* We assume break_lease is only called once per lease: */
2401 BUG_ON(fp->fi_had_conflict);
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002402 /*
2403 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002404 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002405 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002406 */
2407 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002409 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002410 fp->fi_had_conflict = true;
2411 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2412 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002413 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416static
2417int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2418{
2419 if (arg & F_UNLCK)
2420 return lease_modify(onlist, arg);
2421 else
2422 return -EAGAIN;
2423}
2424
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002425static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002426 .lm_break = nfsd_break_deleg_cb,
2427 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428};
2429
2430
Al Virob37ad282006-10-19 23:28:59 -07002431__be32
Andy Adamson66689582009-04-03 08:28:45 +03002432nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2433 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 clientid_t *clientid = &open->op_clientid;
2436 struct nfs4_client *clp = NULL;
2437 unsigned int strhashval;
2438 struct nfs4_stateowner *sop = NULL;
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002441 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 if (STALE_CLIENTID(&open->op_clientid))
2444 return nfserr_stale_clientid;
2445
2446 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
2447 sop = find_openstateowner_str(strhashval, open);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002448 open->op_stateowner = sop;
2449 if (!sop) {
2450 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 clp = find_confirmed_client(clientid);
2452 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002453 return nfserr_expired;
2454 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
Andy Adamson66689582009-04-03 08:28:45 +03002456 /* When sessions are used, skip open sequenceid processing */
2457 if (nfsd4_has_session(cstate))
2458 goto renew;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002459 if (!sop->so_confirmed) {
2460 /* Replace unconfirmed owners without checking for replay. */
2461 clp = sop->so_client;
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05002462 release_openowner(sop);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002463 open->op_stateowner = NULL;
2464 goto renew;
2465 }
2466 if (open->op_seqid == sop->so_seqid - 1) {
2467 if (sop->so_replay.rp_buflen)
Al Viroa90b0612006-10-19 23:29:03 -07002468 return nfserr_replay_me;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002469 /* The original OPEN failed so spectacularly
2470 * that we don't even have replay data saved!
2471 * Therefore, we have no choice but to continue
2472 * processing this OPEN; presumably, we'll
2473 * fail again for the same reason.
2474 */
2475 dprintk("nfsd4_process_open1: replay with no replay cache\n");
2476 goto renew;
2477 }
2478 if (open->op_seqid != sop->so_seqid)
2479 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480renew:
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002481 if (open->op_stateowner == NULL) {
2482 sop = alloc_init_open_stateowner(strhashval, clp, open);
2483 if (sop == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002484 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002485 open->op_stateowner = sop;
2486 }
2487 list_del_init(&sop->so_close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 renew_client(sop->so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002489 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
Al Virob37ad282006-10-19 23:28:59 -07002492static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002493nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2494{
2495 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2496 return nfserr_openmode;
2497 else
2498 return nfs_ok;
2499}
2500
NeilBrown52f4fb42005-06-23 22:02:49 -07002501static struct nfs4_delegation *
2502find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2503{
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002504 struct nfs4_delegation *dp;
NeilBrown52f4fb42005-06-23 22:02:49 -07002505
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002506 spin_lock(&recall_lock);
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002507 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2508 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
2509 spin_unlock(&recall_lock);
2510 return dp;
2511 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002512 spin_unlock(&recall_lock);
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002513 return NULL;
NeilBrown52f4fb42005-06-23 22:02:49 -07002514}
2515
Daniel Mackc47d8322011-05-16 16:38:14 +02002516static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002517{
2518 share_access &= ~NFS4_SHARE_WANT_MASK;
2519
2520 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2521}
2522
Al Virob37ad282006-10-19 23:28:59 -07002523static __be32
NeilBrown567d9822005-06-23 22:02:53 -07002524nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2525 struct nfs4_delegation **dp)
2526{
2527 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002528 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002529
2530 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2531 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002532 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002533 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002534 status = nfs4_check_delegmode(*dp, flags);
2535 if (status)
2536 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002537out:
2538 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2539 return nfs_ok;
2540 if (status)
2541 return status;
2542 open->op_stateowner->so_confirmed = 1;
2543 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002544}
2545
Al Virob37ad282006-10-19 23:28:59 -07002546static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2548{
2549 struct nfs4_stateid *local;
Al Virob37ad282006-10-19 23:28:59 -07002550 __be32 status = nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 struct nfs4_stateowner *sop = open->op_stateowner;
2552
NeilBrown8beefa22005-06-23 22:03:08 -07002553 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /* ignore lock owners */
2555 if (local->st_stateowner->so_is_open_owner == 0)
2556 continue;
2557 /* remember if we have seen this open owner */
2558 if (local->st_stateowner == sop)
2559 *stpp = local;
2560 /* check for conflicting share reservations */
2561 if (!test_share(local, open))
2562 goto out;
2563 }
2564 status = 0;
2565out:
2566 return status;
2567}
2568
NeilBrown5ac049a2005-06-23 22:03:03 -07002569static inline struct nfs4_stateid *
2570nfs4_alloc_stateid(void)
2571{
2572 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2573}
2574
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002575static inline int nfs4_access_to_access(u32 nfs4_access)
2576{
2577 int flags = 0;
2578
2579 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2580 flags |= NFSD_MAY_READ;
2581 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2582 flags |= NFSD_MAY_WRITE;
2583 return flags;
2584}
2585
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002586static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2587 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002588{
2589 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002590 int oflag = nfs4_access_to_omode(open->op_share_access);
2591 int access = nfs4_access_to_access(open->op_share_access);
2592
2593 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2594 * allowing it to break the lease and return EAGAIN leaves the
2595 * client unable to make progress in returning the delegation */
2596 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2597 access |= NFSD_MAY_NOT_BREAK_LEASE;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002598
2599 if (!fp->fi_fds[oflag]) {
2600 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2601 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002602 if (status)
2603 return status;
2604 }
2605 nfs4_file_get_access(fp, oflag);
2606
2607 return nfs_ok;
2608}
2609
Al Virob37ad282006-10-19 23:28:59 -07002610static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002612 struct nfs4_file *fp, struct svc_fh *cur_fh,
2613 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
2615 struct nfs4_stateid *stp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002616 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
NeilBrown5ac049a2005-06-23 22:03:03 -07002618 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 if (stp == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002620 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002622 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002623 if (status) {
2624 kmem_cache_free(stateid_slab, stp);
2625 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 *stpp = stp;
2628 return 0;
2629}
2630
Al Virob37ad282006-10-19 23:28:59 -07002631static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2633 struct nfsd4_open *open)
2634{
2635 struct iattr iattr = {
2636 .ia_valid = ATTR_SIZE,
2637 .ia_size = 0,
2638 };
2639 if (!open->op_truncate)
2640 return 0;
2641 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002642 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2644}
2645
Al Virob37ad282006-10-19 23:28:59 -07002646static __be32
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002647nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
J. Bruce Fields7d947842010-08-20 18:09:31 -04002649 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2650 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002651 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
J. Bruce Fields7d947842010-08-20 18:09:31 -04002653 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002654 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002655 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002656 if (status)
2657 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 status = nfsd4_truncate(rqstp, cur_fh, open);
2660 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002661 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002662 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002663 nfs4_file_put_access(fp, oflag);
2664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return status;
2666 }
2667 /* remember the open */
J. Bruce Fields24a01112010-05-18 20:01:35 -04002668 __set_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002669 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 return nfs_ok;
2672}
2673
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675static void
NeilBrown37515172005-07-07 17:59:16 -07002676nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
NeilBrown37515172005-07-07 17:59:16 -07002678 open->op_stateowner->so_confirmed = 1;
2679 open->op_stateowner->so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002682/* Should we give out recallable state?: */
2683static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2684{
2685 if (clp->cl_cb_state == NFSD4_CB_UP)
2686 return true;
2687 /*
2688 * In the sessions case, since we don't have to establish a
2689 * separate connection for callbacks, we assume it's OK
2690 * until we hear otherwise:
2691 */
2692 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2693}
2694
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002695static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2696{
2697 struct file_lock *fl;
2698
2699 fl = locks_alloc_lock();
2700 if (!fl)
2701 return NULL;
2702 locks_init_lock(fl);
2703 fl->fl_lmops = &nfsd_lease_mng_ops;
2704 fl->fl_flags = FL_LEASE;
2705 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2706 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002707 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002708 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002709 return fl;
2710}
2711
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002712static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2713{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002714 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002715 struct file_lock *fl;
2716 int status;
2717
2718 fl = nfs4_alloc_init_lease(dp, flag);
2719 if (!fl)
2720 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002721 fl->fl_file = find_readable_file(fp);
2722 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2723 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002724 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002725 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002726 locks_free_lock(fl);
2727 return -ENOMEM;
2728 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002729 fp->fi_lease = fl;
2730 fp->fi_deleg_file = fl->fl_file;
2731 get_file(fp->fi_deleg_file);
2732 atomic_set(&fp->fi_delegees, 1);
2733 list_add(&dp->dl_perfile, &fp->fi_delegations);
2734 return 0;
2735}
2736
2737static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2738{
2739 struct nfs4_file *fp = dp->dl_file;
2740
2741 if (!fp->fi_lease)
2742 return nfs4_setlease(dp, flag);
2743 spin_lock(&recall_lock);
2744 if (fp->fi_had_conflict) {
2745 spin_unlock(&recall_lock);
2746 return -EAGAIN;
2747 }
2748 atomic_inc(&fp->fi_delegees);
2749 list_add(&dp->dl_perfile, &fp->fi_delegations);
2750 spin_unlock(&recall_lock);
2751 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002752 return 0;
2753}
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755/*
2756 * Attempt to hand out a delegation.
2757 */
2758static void
2759nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2760{
2761 struct nfs4_delegation *dp;
2762 struct nfs4_stateowner *sop = stp->st_stateowner;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002763 int cb_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 int status, flag = 0;
2765
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002766 cb_up = nfsd4_cb_channel_good(sop->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002768 open->op_recall = 0;
2769 switch (open->op_claim_type) {
2770 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002771 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002772 open->op_recall = 1;
2773 flag = open->op_delegate_type;
2774 if (flag == NFS4_OPEN_DELEGATE_NONE)
2775 goto out;
2776 break;
2777 case NFS4_OPEN_CLAIM_NULL:
2778 /* Let's not give out any delegations till everyone's
2779 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002780 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002781 goto out;
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002782 if (!cb_up || !sop->so_confirmed)
NeilBrown7b190fe2005-06-23 22:03:23 -07002783 goto out;
2784 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2785 flag = NFS4_OPEN_DELEGATE_WRITE;
2786 else
2787 flag = NFS4_OPEN_DELEGATE_READ;
2788 break;
2789 default:
2790 goto out;
2791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002794 if (dp == NULL)
2795 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002796 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002797 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002798 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2801
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002802 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2803 STATEID_VAL(&dp->dl_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804out:
NeilBrown7b190fe2005-06-23 22:03:23 -07002805 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2806 && flag == NFS4_OPEN_DELEGATE_NONE
2807 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002808 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 open->op_delegate_type = flag;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002810 return;
2811out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002812 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002813out_no_deleg:
2814 flag = NFS4_OPEN_DELEGATE_NONE;
2815 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
2818/*
2819 * called with nfs4_lock_state() held.
2820 */
Al Virob37ad282006-10-19 23:28:59 -07002821__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2823{
Andy Adamson66689582009-04-03 08:28:45 +03002824 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 struct nfs4_file *fp = NULL;
2826 struct inode *ino = current_fh->fh_dentry->d_inode;
2827 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07002828 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002829 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 status = nfserr_inval;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002832 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002833 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 goto out;
2835 /*
2836 * Lookup file; if found, lookup stateid and check open request,
2837 * and check for delegations in the process of being recalled.
2838 * If not found, create the nfs4_file struct
2839 */
2840 fp = find_file(ino);
2841 if (fp) {
2842 if ((status = nfs4_check_open(fp, open, &stp)))
2843 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002844 status = nfs4_check_deleg(fp, open, &dp);
2845 if (status)
2846 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07002848 status = nfserr_bad_stateid;
2849 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2850 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04002851 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 fp = alloc_init_file(ino);
2853 if (fp == NULL)
2854 goto out;
2855 }
2856
2857 /*
2858 * OPEN the file, or upgrade an existing OPEN.
2859 * If truncate fails, the OPEN fails.
2860 */
2861 if (stp) {
2862 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002863 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (status)
2865 goto out;
NeilBrown444c2c02005-07-07 17:59:22 -07002866 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 } else {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002868 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07002869 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 goto out;
2871 init_stateid(stp, fp, open);
2872 status = nfsd4_truncate(rqstp, current_fh, open);
2873 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05002874 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 goto out;
2876 }
Andy Adamson66689582009-04-03 08:28:45 +03002877 if (nfsd4_has_session(&resp->cstate))
2878 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
2880 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2881
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002882 if (nfsd4_has_session(&resp->cstate))
Andy Adamson66689582009-04-03 08:28:45 +03002883 open->op_stateowner->so_confirmed = 1;
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 /*
2886 * Attempt to hand out a delegation. No error return, because the
2887 * OPEN succeeds even if we fail.
2888 */
2889 nfs4_open_delegation(current_fh, open, stp);
2890
2891 status = nfs_ok;
2892
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002893 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2894 STATEID_VAL(&stp->st_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895out:
NeilBrown13cd2182005-06-23 22:03:10 -07002896 if (fp)
2897 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07002898 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2899 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 /*
2901 * To finish the open response, we just need to set the rflags.
2902 */
2903 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Andy Adamson66689582009-04-03 08:28:45 +03002904 if (!open->op_stateowner->so_confirmed &&
2905 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2907
2908 return status;
2909}
2910
Al Virob37ad282006-10-19 23:28:59 -07002911__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002912nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2913 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
2915 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07002916 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 nfs4_lock_state();
2919 dprintk("process_renew(%08x/%08x): starting\n",
2920 clid->cl_boot, clid->cl_id);
2921 status = nfserr_stale_clientid;
2922 if (STALE_CLIENTID(clid))
2923 goto out;
2924 clp = find_confirmed_client(clid);
2925 status = nfserr_expired;
2926 if (clp == NULL) {
2927 /* We assume the client took too long to RENEW. */
2928 dprintk("nfsd4_renew: clientid not found!\n");
2929 goto out;
2930 }
2931 renew_client(clp);
2932 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07002933 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04002934 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 goto out;
2936 status = nfs_ok;
2937out:
2938 nfs4_unlock_state();
2939 return status;
2940}
2941
Daniel Mackc47d8322011-05-16 16:38:14 +02002942static struct lock_manager nfsd4_manager = {
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002943};
2944
NeilBrowna76b4312005-06-23 22:04:01 -07002945static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002946nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07002947{
2948 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07002949 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002950 locks_end_grace(&nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05002951 /*
2952 * Now that every NFSv4 client has had the chance to recover and
2953 * to see the (possibly new, possibly shorter) lease time, we
2954 * can safely set the next grace time to the current lease time:
2955 */
2956 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07002957}
2958
NeilBrownfd39ca92005-06-23 22:04:03 -07002959static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960nfs4_laundromat(void)
2961{
2962 struct nfs4_client *clp;
2963 struct nfs4_stateowner *sop;
2964 struct nfs4_delegation *dp;
2965 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05002966 time_t cutoff = get_seconds() - nfsd4_lease;
2967 time_t t, clientid_val = nfsd4_lease;
2968 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 nfs4_lock_state();
2971
2972 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002973 if (locks_in_grace())
2974 nfsd4_end_grace();
Benny Halevy36acb662010-05-12 00:13:04 +03002975 INIT_LIST_HEAD(&reaplist);
2976 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 list_for_each_safe(pos, next, &client_lru) {
2978 clp = list_entry(pos, struct nfs4_client, cl_lru);
2979 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
2980 t = clp->cl_time - cutoff;
2981 if (clientid_val > t)
2982 clientid_val = t;
2983 break;
2984 }
Benny Halevyd7682982010-05-12 00:13:54 +03002985 if (atomic_read(&clp->cl_refcount)) {
2986 dprintk("NFSD: client in use (clientid %08x)\n",
2987 clp->cl_clientid.cl_id);
2988 continue;
2989 }
2990 unhash_client_locked(clp);
2991 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03002992 }
2993 spin_unlock(&client_lock);
2994 list_for_each_safe(pos, next, &reaplist) {
2995 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 dprintk("NFSD: purging unused client (clientid %08x)\n",
2997 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07002998 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 expire_client(clp);
3000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 spin_lock(&recall_lock);
3002 list_for_each_safe(pos, next, &del_recall_lru) {
3003 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3004 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3005 u = dp->dl_time - cutoff;
3006 if (test_val > u)
3007 test_val = u;
3008 break;
3009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 list_move(&dp->dl_recall_lru, &reaplist);
3011 }
3012 spin_unlock(&recall_lock);
3013 list_for_each_safe(pos, next, &reaplist) {
3014 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3015 list_del_init(&dp->dl_recall_lru);
3016 unhash_delegation(dp);
3017 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003018 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 list_for_each_safe(pos, next, &close_lru) {
3020 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3021 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
3022 u = sop->so_time - cutoff;
3023 if (test_val > u)
3024 test_val = u;
3025 break;
3026 }
3027 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
3028 sop->so_id);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05003029 release_openowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
3031 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3032 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3033 nfs4_unlock_state();
3034 return clientid_val;
3035}
3036
Harvey Harrisona254b242008-02-20 12:49:00 -08003037static struct workqueue_struct *laundry_wq;
3038static void laundromat_main(struct work_struct *);
3039static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3040
3041static void
David Howellsc4028952006-11-22 14:57:56 +00003042laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
3044 time_t t;
3045
3046 t = nfs4_laundromat();
3047 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07003048 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
NeilBrownfd39ca92005-06-23 22:04:03 -07003051static struct nfs4_stateowner *
NeilBrownf8816512005-07-07 17:59:25 -07003052search_close_lru(u32 st_id, int flags)
3053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 struct nfs4_stateowner *local = NULL;
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 if (flags & CLOSE_STATE) {
3057 list_for_each_entry(local, &close_lru, so_close_lru) {
3058 if (local->so_id == st_id)
3059 return local;
3060 }
3061 }
3062 return NULL;
3063}
3064
3065static inline int
3066nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
3067{
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003068 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069}
3070
3071static int
3072STALE_STATEID(stateid_t *stateid)
3073{
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003074 if (stateid->si_boot == boot_time)
3075 return 0;
3076 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003077 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003078 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
3081static inline int
3082access_permit_read(unsigned long access_bmap)
3083{
3084 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3085 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3086 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3087}
3088
3089static inline int
3090access_permit_write(unsigned long access_bmap)
3091{
3092 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3093 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3094}
3095
3096static
Al Virob37ad282006-10-19 23:28:59 -07003097__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Al Virob37ad282006-10-19 23:28:59 -07003099 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
J. Bruce Fields02921912010-07-29 15:16:59 -04003101 /* For lock stateid's, we test the parent open, not the lock: */
3102 if (stp->st_openstp)
3103 stp = stp->st_openstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3105 goto out;
3106 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3107 goto out;
3108 status = nfs_ok;
3109out:
3110 return status;
3111}
3112
Al Virob37ad282006-10-19 23:28:59 -07003113static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3115{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003116 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003118 else if (locks_in_grace()) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003119 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 * conflicting state; so we must wait out the grace period. */
3121 return nfserr_grace;
3122 } else if (flags & WR_STATE)
3123 return nfs4_share_conflict(current_fh,
3124 NFS4_SHARE_DENY_WRITE);
3125 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3126 return nfs4_share_conflict(current_fh,
3127 NFS4_SHARE_DENY_READ);
3128}
3129
3130/*
3131 * Allow READ/WRITE during grace period on recovered state only for files
3132 * that are not able to provide mandatory locking.
3133 */
3134static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08003135grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003137 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
Andy Adamson66689582009-04-03 08:28:45 +03003140static int check_stateid_generation(stateid_t *in, stateid_t *ref, int flags)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003141{
Andy Adamson66689582009-04-03 08:28:45 +03003142 /*
3143 * When sessions are used the stateid generation number is ignored
3144 * when it is zero.
3145 */
3146 if ((flags & HAS_SESSION) && in->si_generation == 0)
3147 goto out;
3148
J. Bruce Fields0836f582008-01-26 19:08:12 -05003149 /* If the client sends us a stateid from the future, it's buggy: */
3150 if (in->si_generation > ref->si_generation)
3151 return nfserr_bad_stateid;
3152 /*
3153 * The following, however, can happen. For example, if the
3154 * client sends an open and some IO at the same time, the open
3155 * may bump si_generation while the IO is still in flight.
3156 * Thanks to hard links and renames, the client never knows what
3157 * file an open will affect. So it could avoid that situation
3158 * only by serializing all opens and IO from the same open
3159 * owner. To recover from the old_stateid error, the client
3160 * will just have to retry the IO:
3161 */
3162 if (in->si_generation < ref->si_generation)
3163 return nfserr_old_stateid;
Andy Adamson66689582009-04-03 08:28:45 +03003164out:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003165 return nfs_ok;
3166}
3167
J. Bruce Fields3e633072009-02-21 13:17:19 -08003168static int is_delegation_stateid(stateid_t *stateid)
3169{
3170 return stateid->si_fileid == 0;
3171}
3172
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003173static int is_open_stateid(struct nfs4_stateid *stateid)
3174{
3175 return stateid->st_openstp == NULL;
3176}
3177
Bryan Schumaker17456802011-07-13 10:50:48 -04003178__be32 nfs4_validate_stateid(stateid_t *stateid, int flags)
3179{
3180 struct nfs4_stateid *stp = NULL;
3181 __be32 status = nfserr_stale_stateid;
3182
3183 if (STALE_STATEID(stateid))
3184 goto out;
3185
3186 status = nfserr_expired;
3187 stp = search_for_stateid(stateid);
3188 if (!stp)
3189 goto out;
3190 status = nfserr_bad_stateid;
3191
3192 if (!stp->st_stateowner->so_confirmed)
3193 goto out;
3194
3195 status = check_stateid_generation(stateid, &stp->st_stateid, flags);
3196 if (status)
3197 goto out;
3198
3199 status = nfs_ok;
3200out:
3201 return status;
3202}
3203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204/*
3205* Checks for stateid operations
3206*/
Al Virob37ad282006-10-19 23:28:59 -07003207__be32
Benny Halevydd453df2009-04-03 08:28:41 +03003208nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3209 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
3211 struct nfs4_stateid *stp = NULL;
3212 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003213 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07003215 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (filpp)
3218 *filpp = NULL;
3219
J. Bruce Fields18f82732009-02-21 15:23:01 -08003220 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return nfserr_grace;
3222
Andy Adamson66689582009-04-03 08:28:45 +03003223 if (nfsd4_has_session(cstate))
3224 flags |= HAS_SESSION;
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3227 return check_special_stateids(current_fh, stateid, flags);
3228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 status = nfserr_stale_stateid;
3230 if (STALE_STATEID(stateid))
3231 goto out;
3232
J. Bruce Fields33515142010-10-02 18:42:39 -04003233 /*
3234 * We assume that any stateid that has the current boot time,
3235 * but that we can't find, is expired:
3236 */
3237 status = nfserr_expired;
J. Bruce Fields3e633072009-02-21 13:17:19 -08003238 if (is_delegation_stateid(stateid)) {
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003239 dp = find_delegation_stateid(ino, stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003240 if (!dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 goto out;
Andy Adamson66689582009-04-03 08:28:45 +03003242 status = check_stateid_generation(stateid, &dp->dl_stateid,
3243 flags);
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08003244 if (status)
3245 goto out;
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003246 status = nfs4_check_delegmode(dp, flags);
3247 if (status)
3248 goto out;
3249 renew_client(dp->dl_client);
Dan Carpenter43b01782010-10-27 23:19:04 +02003250 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003251 *filpp = dp->dl_file->fi_deleg_file;
Dan Carpenter43b01782010-10-27 23:19:04 +02003252 BUG_ON(!*filpp);
3253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 } else { /* open or lock stateid */
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003255 stp = find_stateid(stateid, flags);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003256 if (!stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003258 status = nfserr_bad_stateid;
J. Bruce Fields6150ef02009-02-21 13:36:16 -08003259 if (nfs4_check_fh(current_fh, stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 goto out;
3261 if (!stp->st_stateowner->so_confirmed)
3262 goto out;
Andy Adamson66689582009-04-03 08:28:45 +03003263 status = check_stateid_generation(stateid, &stp->st_stateid,
3264 flags);
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08003265 if (status)
3266 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003267 status = nfs4_check_openmode(stp, flags);
3268 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 goto out;
3270 renew_client(stp->st_stateowner->so_client);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003271 if (filpp) {
3272 if (flags & RD_STATE)
3273 *filpp = find_readable_file(stp->st_file);
3274 else
3275 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278 status = nfs_ok;
3279out:
3280 return status;
3281}
3282
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003283static __be32
3284nfsd4_free_delegation_stateid(stateid_t *stateid)
3285{
3286 struct nfs4_delegation *dp = search_for_delegation(stateid);
3287 if (dp)
3288 return nfserr_locks_held;
3289 return nfserr_bad_stateid;
3290}
3291
3292static __be32
3293nfsd4_free_lock_stateid(struct nfs4_stateid *stp)
3294{
3295 if (check_for_locks(stp->st_file, stp->st_stateowner))
3296 return nfserr_locks_held;
3297 release_lock_stateid(stp);
3298 return nfs_ok;
3299}
3300
3301/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003302 * Test if the stateid is valid
3303 */
3304__be32
3305nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3306 struct nfsd4_test_stateid *test_stateid)
3307{
3308 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3309 return nfs_ok;
3310}
3311
3312/*
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003313 * Free a state id
3314 */
3315__be32
3316nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3317 struct nfsd4_free_stateid *free_stateid)
3318{
3319 stateid_t *stateid = &free_stateid->fr_stateid;
3320 struct nfs4_stateid *stp;
3321 __be32 ret;
3322
3323 nfs4_lock_state();
3324 if (is_delegation_stateid(stateid)) {
3325 ret = nfsd4_free_delegation_stateid(stateid);
3326 goto out;
3327 }
3328
3329 stp = search_for_stateid(stateid);
3330 if (!stp) {
3331 ret = nfserr_bad_stateid;
3332 goto out;
3333 }
3334 if (stateid->si_generation != 0) {
3335 if (stateid->si_generation < stp->st_stateid.si_generation) {
3336 ret = nfserr_old_stateid;
3337 goto out;
3338 }
3339 if (stateid->si_generation > stp->st_stateid.si_generation) {
3340 ret = nfserr_bad_stateid;
3341 goto out;
3342 }
3343 }
3344
3345 if (is_open_stateid(stp)) {
3346 ret = nfserr_locks_held;
3347 goto out;
3348 } else {
3349 ret = nfsd4_free_lock_stateid(stp);
3350 goto out;
3351 }
3352
3353out:
3354 nfs4_unlock_state();
3355 return ret;
3356}
3357
NeilBrown4c4cd222005-07-07 17:59:27 -07003358static inline int
3359setlkflg (int type)
3360{
3361 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3362 RD_STATE : WR_STATE;
3363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
3365/*
3366 * Checks for sequence id mutating operations.
3367 */
Al Virob37ad282006-10-19 23:28:59 -07003368static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003369nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3370 stateid_t *stateid, int flags,
3371 struct nfs4_stateowner **sopp,
3372 struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 struct nfs4_stateid *stp;
3375 struct nfs4_stateowner *sop;
Benny Halevydd453df2009-04-03 08:28:41 +03003376 struct svc_fh *current_fh = &cstate->current_fh;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003377 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003379 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3380 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 *stpp = NULL;
3383 *sopp = NULL;
3384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04003386 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07003387 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 }
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 if (STALE_STATEID(stateid))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003391 return nfserr_stale_stateid;
Andy Adamson66689582009-04-03 08:28:45 +03003392
3393 if (nfsd4_has_session(cstate))
3394 flags |= HAS_SESSION;
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 /*
3397 * We return BAD_STATEID if filehandle doesn't match stateid,
3398 * the confirmed flag is incorrecly set, or the generation
3399 * number is incorrect.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 */
NeilBrownf8816512005-07-07 17:59:25 -07003401 stp = find_stateid(stateid, flags);
3402 if (stp == NULL) {
3403 /*
3404 * Also, we should make sure this isn't just the result of
3405 * a replayed close:
3406 */
3407 sop = search_close_lru(stateid->si_stateownerid, flags);
J. Bruce Fields33515142010-10-02 18:42:39 -04003408 /* It's not stale; let's assume it's expired: */
NeilBrownf8816512005-07-07 17:59:25 -07003409 if (sop == NULL)
J. Bruce Fields33515142010-10-02 18:42:39 -04003410 return nfserr_expired;
NeilBrownf8816512005-07-07 17:59:25 -07003411 *sopp = sop;
3412 goto check_replay;
3413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003415 *stpp = stp;
3416 *sopp = sop = stp->st_stateowner;
3417
NeilBrown4c4cd222005-07-07 17:59:27 -07003418 if (lock) {
NeilBrown4c4cd222005-07-07 17:59:27 -07003419 clientid_t *lockclid = &lock->v.new.clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 struct nfs4_client *clp = sop->so_client;
NeilBrown4c4cd222005-07-07 17:59:27 -07003421 int lkflg = 0;
Al Virob37ad282006-10-19 23:28:59 -07003422 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
NeilBrown4c4cd222005-07-07 17:59:27 -07003424 lkflg = setlkflg(lock->lk_type);
3425
3426 if (lock->lk_is_new) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003427 if (!sop->so_is_open_owner)
3428 return nfserr_bad_stateid;
Andy Adamson60adfc52009-04-03 08:28:50 +03003429 if (!(flags & HAS_SESSION) &&
3430 !same_clid(&clp->cl_clientid, lockclid))
3431 return nfserr_bad_stateid;
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003432 /* stp is the open stateid */
3433 status = nfs4_check_openmode(stp, lkflg);
3434 if (status)
3435 return status;
3436 } else {
3437 /* stp is the lock stateid */
3438 status = nfs4_check_openmode(stp->st_openstp, lkflg);
3439 if (status)
3440 return status;
NeilBrown4c4cd222005-07-07 17:59:27 -07003441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
3443
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003444 if (nfs4_check_fh(current_fh, stp)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04003445 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07003446 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 }
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 /*
3450 * We now validate the seqid and stateid generation numbers.
3451 * For the moment, we ignore the possibility of
3452 * generation number wraparound.
3453 */
Andy Adamson66689582009-04-03 08:28:45 +03003454 if (!(flags & HAS_SESSION) && seqid != sop->so_seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 goto check_replay;
3456
NeilBrown3a4f98b2005-07-07 17:59:26 -07003457 if (sop->so_confirmed && flags & CONFIRM) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04003458 dprintk("NFSD: preprocess_seqid_op: expected"
NeilBrown3a4f98b2005-07-07 17:59:26 -07003459 " unconfirmed stateowner!\n");
3460 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07003462 if (!sop->so_confirmed && !(flags & CONFIRM)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04003463 dprintk("NFSD: preprocess_seqid_op: stateowner not"
NeilBrown3a4f98b2005-07-07 17:59:26 -07003464 " confirmed yet!\n");
3465 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 }
Andy Adamson66689582009-04-03 08:28:45 +03003467 status = check_stateid_generation(stateid, &stp->st_stateid, flags);
J. Bruce Fields0836f582008-01-26 19:08:12 -05003468 if (status)
3469 return status;
NeilBrown52fd0042005-07-07 17:59:24 -07003470 renew_client(sop->so_client);
NeilBrown3a4f98b2005-07-07 17:59:26 -07003471 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473check_replay:
NeilBrownbd9aac52005-07-07 17:59:19 -07003474 if (seqid == sop->so_seqid - 1) {
Neil Brown849823c2005-09-13 01:25:36 -07003475 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 /* indicate replay to calling function */
Al Viroa90b0612006-10-19 23:29:03 -07003477 return nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
J. Bruce Fields2fdada02007-07-27 16:10:37 -04003479 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
NeilBrown3a4f98b2005-07-07 17:59:26 -07003480 sop->so_seqid, seqid);
3481 *sopp = NULL;
3482 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Al Virob37ad282006-10-19 23:28:59 -07003485__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003486nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003487 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488{
Al Virob37ad282006-10-19 23:28:59 -07003489 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 struct nfs4_stateowner *sop;
3491 struct nfs4_stateid *stp;
3492
3493 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003494 (int)cstate->current_fh.fh_dentry->d_name.len,
3495 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003497 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003498 if (status)
3499 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
3501 nfs4_lock_state();
3502
Benny Halevydd453df2009-04-03 08:28:41 +03003503 if ((status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003504 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003505 CONFIRM | OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 &oc->oc_stateowner, &stp, NULL)))
3507 goto out;
3508
3509 sop = oc->oc_stateowner;
3510 sop->so_confirmed = 1;
3511 update_stateid(&stp->st_stateid);
3512 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003513 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3514 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003515
3516 nfsd4_create_clid_dir(sop->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517out:
Neil Brownf2327d92005-09-13 01:25:37 -07003518 if (oc->oc_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 nfs4_get_stateowner(oc->oc_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003520 cstate->replay_owner = oc->oc_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07003521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 nfs4_unlock_state();
3523 return status;
3524}
3525
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003526static inline void nfs4_file_downgrade(struct nfs4_stateid *stp, unsigned int to_access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527{
3528 int i;
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 for (i = 1; i < 4; i++) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003531 if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
3532 nfs4_file_put_access(stp->st_file, i);
3533 __clear_bit(i, &stp->st_access_bmap);
3534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 }
3536}
3537
3538static void
3539reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3540{
3541 int i;
3542 for (i = 0; i < 4; i++) {
3543 if ((i & deny) != i)
3544 __clear_bit(i, bmap);
3545 }
3546}
3547
Al Virob37ad282006-10-19 23:28:59 -07003548__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003549nfsd4_open_downgrade(struct svc_rqst *rqstp,
3550 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003551 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
Al Virob37ad282006-10-19 23:28:59 -07003553 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003557 (int)cstate->current_fh.fh_dentry->d_name.len,
3558 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Andy Adamsond87a8ad2009-04-03 08:28:53 +03003560 if (!access_valid(od->od_share_access, cstate->minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07003561 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 return nfserr_inval;
3563
3564 nfs4_lock_state();
Benny Halevydd453df2009-04-03 08:28:41 +03003565 if ((status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003566 od->od_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 &od->od_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003568 OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 &od->od_stateowner, &stp, NULL)))
3570 goto out;
3571
3572 status = nfserr_inval;
3573 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3574 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3575 stp->st_access_bmap, od->od_share_access);
3576 goto out;
3577 }
3578 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3579 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3580 stp->st_deny_bmap, od->od_share_deny);
3581 goto out;
3582 }
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003583 nfs4_file_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3586
3587 update_stateid(&stp->st_stateid);
3588 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
3589 status = nfs_ok;
3590out:
Neil Brownf2327d92005-09-13 01:25:37 -07003591 if (od->od_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 nfs4_get_stateowner(od->od_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003593 cstate->replay_owner = od->od_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07003594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 nfs4_unlock_state();
3596 return status;
3597}
3598
3599/*
3600 * nfs4_unlock_state() called after encode
3601 */
Al Virob37ad282006-10-19 23:28:59 -07003602__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003603nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003604 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Al Virob37ad282006-10-19 23:28:59 -07003606 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 struct nfs4_stateid *stp;
3608
3609 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003610 (int)cstate->current_fh.fh_dentry->d_name.len,
3611 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
3613 nfs4_lock_state();
3614 /* check close_lru for replay */
Benny Halevydd453df2009-04-03 08:28:41 +03003615 if ((status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003616 close->cl_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 &close->cl_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003618 OPEN_STATE | CLOSE_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 &close->cl_stateowner, &stp, NULL)))
3620 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 status = nfs_ok;
3622 update_stateid(&stp->st_stateid);
3623 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
3624
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003625 /* release_stateid() calls nfsd_close() if needed */
J. Bruce Fields22839632009-01-11 14:27:17 -05003626 release_open_stateid(stp);
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003627
3628 /* place unused nfs4_stateowners on so_close_lru list to be
3629 * released by the laundromat service after the lease period
3630 * to enable us to handle CLOSE replay
3631 */
3632 if (list_empty(&close->cl_stateowner->so_stateids))
3633 move_to_close_lru(close->cl_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634out:
Neil Brownf2327d92005-09-13 01:25:37 -07003635 if (close->cl_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 nfs4_get_stateowner(close->cl_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003637 cstate->replay_owner = close->cl_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07003638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 nfs4_unlock_state();
3640 return status;
3641}
3642
Al Virob37ad282006-10-19 23:28:59 -07003643__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003644nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3645 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003647 struct nfs4_delegation *dp;
3648 stateid_t *stateid = &dr->dr_stateid;
3649 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07003650 __be32 status;
Andy Adamson66689582009-04-03 08:28:45 +03003651 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003653 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003654 return status;
3655 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Andy Adamson66689582009-04-03 08:28:45 +03003657 if (nfsd4_has_session(cstate))
3658 flags |= HAS_SESSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 nfs4_lock_state();
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003660 status = nfserr_bad_stateid;
3661 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3662 goto out;
3663 status = nfserr_stale_stateid;
3664 if (STALE_STATEID(stateid))
3665 goto out;
J. Bruce Fields7e0f7cf2009-02-21 13:32:28 -08003666 status = nfserr_bad_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003667 if (!is_delegation_stateid(stateid))
3668 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003669 status = nfserr_expired;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003670 dp = find_delegation_stateid(inode, stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003671 if (!dp)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003672 goto out;
Andy Adamson66689582009-04-03 08:28:45 +03003673 status = check_stateid_generation(stateid, &dp->dl_stateid, flags);
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003674 if (status)
3675 goto out;
3676 renew_client(dp->dl_client);
3677
3678 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003680 nfs4_unlock_state();
3681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 return status;
3683}
3684
3685
3686/*
3687 * Lock owner state (byte-range locks)
3688 */
3689#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3690#define LOCK_HASH_BITS 8
3691#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3692#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3693
Benny Halevy87df4de2008-12-15 19:42:03 +02003694static inline u64
3695end_offset(u64 start, u64 len)
3696{
3697 u64 end;
3698
3699 end = start + len;
3700 return end >= start ? end: NFS4_MAX_UINT64;
3701}
3702
3703/* last octet in a range */
3704static inline u64
3705last_byte_offset(u64 start, u64 len)
3706{
3707 u64 end;
3708
3709 BUG_ON(!len);
3710 end = start + len;
3711 return end > start ? end - 1: NFS4_MAX_UINT64;
3712}
3713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714#define lockownerid_hashval(id) \
3715 ((id) & LOCK_HASH_MASK)
3716
3717static inline unsigned int
3718lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3719 struct xdr_netobj *ownername)
3720{
3721 return (file_hashval(inode) + cl_id
3722 + opaque_hashval(ownername->data, ownername->len))
3723 & LOCK_HASH_MASK;
3724}
3725
3726static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3727static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3728static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
3729
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003730static int
3731same_stateid(stateid_t *id_one, stateid_t *id_two)
3732{
3733 if (id_one->si_stateownerid != id_two->si_stateownerid)
3734 return 0;
3735 return id_one->si_fileid == id_two->si_fileid;
3736}
3737
NeilBrownfd39ca92005-06-23 22:04:03 -07003738static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739find_stateid(stateid_t *stid, int flags)
3740{
Krishna Kumar9346eff2008-10-20 11:44:28 +05303741 struct nfs4_stateid *local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 u32 st_id = stid->si_stateownerid;
3743 u32 f_id = stid->si_fileid;
3744 unsigned int hashval;
3745
3746 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
Krishna Kumar9346eff2008-10-20 11:44:28 +05303747 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 hashval = stateid_hashval(st_id, f_id);
3749 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
3750 if ((local->st_stateid.si_stateownerid == st_id) &&
3751 (local->st_stateid.si_fileid == f_id))
3752 return local;
3753 }
3754 }
Krishna Kumar9346eff2008-10-20 11:44:28 +05303755
3756 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 hashval = stateid_hashval(st_id, f_id);
3758 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
3759 if ((local->st_stateid.si_stateownerid == st_id) &&
3760 (local->st_stateid.si_fileid == f_id))
3761 return local;
3762 }
Neil Brown849823c2005-09-13 01:25:36 -07003763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 return NULL;
3765}
3766
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003767static struct nfs4_stateid *
3768search_for_stateid(stateid_t *stid)
3769{
3770 struct nfs4_stateid *local;
3771 unsigned int hashval = stateid_hashval(stid->si_stateownerid, stid->si_fileid);
3772
3773 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
3774 if (same_stateid(&local->st_stateid, stid))
3775 return local;
3776 }
3777
3778 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
3779 if (same_stateid(&local->st_stateid, stid))
3780 return local;
3781 }
3782 return NULL;
3783}
3784
3785static struct nfs4_delegation *
3786search_for_delegation(stateid_t *stid)
3787{
3788 struct nfs4_file *fp;
3789 struct nfs4_delegation *dp;
3790 struct list_head *pos;
3791 int i;
3792
3793 for (i = 0; i < FILE_HASH_SIZE; i++) {
3794 list_for_each_entry(fp, &file_hashtbl[i], fi_hash) {
3795 list_for_each(pos, &fp->fi_delegations) {
3796 dp = list_entry(pos, struct nfs4_delegation, dl_perfile);
3797 if (same_stateid(&dp->dl_stateid, stid))
3798 return dp;
3799 }
3800 }
3801 }
3802 return NULL;
3803}
3804
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805static struct nfs4_delegation *
3806find_delegation_stateid(struct inode *ino, stateid_t *stid)
3807{
NeilBrown13cd2182005-06-23 22:03:10 -07003808 struct nfs4_file *fp;
3809 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003811 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3812 STATEID_VAL(stid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07003815 if (!fp)
3816 return NULL;
3817 dl = find_delegation_file(fp, stid);
3818 put_nfs4_file(fp);
3819 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
3822/*
3823 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3824 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3825 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3826 * locking, this prevents us from being completely protocol-compliant. The
3827 * real solution to this problem is to start using unsigned file offsets in
3828 * the VFS, but this is a very deep change!
3829 */
3830static inline void
3831nfs4_transform_lock_offset(struct file_lock *lock)
3832{
3833 if (lock->fl_start < 0)
3834 lock->fl_start = OFFSET_MAX;
3835 if (lock->fl_end < 0)
3836 lock->fl_end = OFFSET_MAX;
3837}
3838
NeilBrownd5b90262006-04-10 22:55:22 -07003839/* Hack!: For now, we're defining this just so we can use a pointer to it
3840 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003841static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003842};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
3844static inline void
3845nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3846{
NeilBrownd5b90262006-04-10 22:55:22 -07003847 struct nfs4_stateowner *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
NeilBrownd5b90262006-04-10 22:55:22 -07003849 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3850 sop = (struct nfs4_stateowner *) fl->fl_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 kref_get(&sop->so_ref);
3852 deny->ld_sop = sop;
3853 deny->ld_clientid = sop->so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003854 } else {
3855 deny->ld_sop = NULL;
3856 deny->ld_clientid.cl_boot = 0;
3857 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 }
3859 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003860 deny->ld_length = NFS4_MAX_UINT64;
3861 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3863 deny->ld_type = NFS4_READ_LT;
3864 if (fl->fl_type != F_RDLCK)
3865 deny->ld_type = NFS4_WRITE_LT;
3866}
3867
3868static struct nfs4_stateowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869find_lockstateowner_str(struct inode *inode, clientid_t *clid,
3870 struct xdr_netobj *owner)
3871{
3872 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3873 struct nfs4_stateowner *op;
3874
3875 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003876 if (same_owner_str(op, owner, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 return op;
3878 }
3879 return NULL;
3880}
3881
3882/*
3883 * Alloc a lock owner structure.
3884 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003885 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 *
3887 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 */
3889
3890static struct nfs4_stateowner *
3891alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3892 struct nfs4_stateowner *sop;
3893 struct nfs4_replay *rp;
3894 unsigned int idhashval;
3895
3896 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
3897 return NULL;
3898 idhashval = lockownerid_hashval(current_ownerid);
3899 INIT_LIST_HEAD(&sop->so_idhash);
3900 INIT_LIST_HEAD(&sop->so_strhash);
3901 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07003902 INIT_LIST_HEAD(&sop->so_stateids);
3903 INIT_LIST_HEAD(&sop->so_perstateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
3905 sop->so_time = 0;
3906 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
3907 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07003908 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 sop->so_is_open_owner = 0;
3910 sop->so_id = current_ownerid++;
3911 sop->so_client = clp;
Neil Brownb59e3c02005-09-13 01:25:38 -07003912 /* It is the openowner seqid that will be incremented in encode in the
3913 * case of new lockowners; so increment the lock seqid manually: */
3914 sop->so_seqid = lock->lk_new_lock_seqid + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 sop->so_confirmed = 1;
3916 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08003917 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 rp->rp_buflen = 0;
3919 rp->rp_buf = rp->rp_ibuf;
3920 return sop;
3921}
3922
NeilBrownfd39ca92005-06-23 22:04:03 -07003923static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3925{
3926 struct nfs4_stateid *stp;
3927 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
3928
NeilBrown5ac049a2005-06-23 22:03:03 -07003929 stp = nfs4_alloc_stateid();
3930 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 goto out;
3932 INIT_LIST_HEAD(&stp->st_hash);
3933 INIT_LIST_HEAD(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07003934 INIT_LIST_HEAD(&stp->st_perstateowner);
3935 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07003937 list_add(&stp->st_perfile, &fp->fi_stateids);
NeilBrownea1da632005-06-23 22:04:17 -07003938 list_add(&stp->st_perstateowner, &sop->so_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07003940 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 stp->st_file = fp;
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003942 stp->st_stateid.si_boot = boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 stp->st_stateid.si_stateownerid = sop->so_id;
3944 stp->st_stateid.si_fileid = fp->fi_id;
3945 stp->st_stateid.si_generation = 0;
J. Bruce Fields0997b172011-03-02 18:01:35 -05003946 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003948 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
3950out:
3951 return stp;
3952}
3953
NeilBrownfd39ca92005-06-23 22:04:03 -07003954static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955check_lock_length(u64 offset, u64 length)
3956{
Benny Halevy87df4de2008-12-15 19:42:03 +02003957 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 LOFF_OVERFLOW(offset, length)));
3959}
3960
J. Bruce Fields0997b172011-03-02 18:01:35 -05003961static void get_lock_access(struct nfs4_stateid *lock_stp, u32 access)
3962{
3963 struct nfs4_file *fp = lock_stp->st_file;
3964 int oflag = nfs4_access_to_omode(access);
3965
3966 if (test_bit(access, &lock_stp->st_access_bmap))
3967 return;
3968 nfs4_file_get_access(fp, oflag);
3969 __set_bit(access, &lock_stp->st_access_bmap);
3970}
3971
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972/*
3973 * LOCK operation
3974 */
Al Virob37ad282006-10-19 23:28:59 -07003975__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003976nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003977 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978{
NeilBrown3e9e3db2005-06-23 22:04:20 -07003979 struct nfs4_stateowner *open_sop = NULL;
Neil Brownb59e3c02005-09-13 01:25:38 -07003980 struct nfs4_stateowner *lock_sop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 struct nfs4_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04003982 struct nfs4_file *fp;
3983 struct file *filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05003985 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07003986 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 unsigned int strhashval;
Al Virob8dd7b92006-10-19 23:29:01 -07003988 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
3990 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3991 (long long) lock->lk_offset,
3992 (long long) lock->lk_length);
3993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 if (check_lock_length(lock->lk_offset, lock->lk_length))
3995 return nfserr_inval;
3996
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003997 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02003998 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08003999 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4000 return status;
4001 }
4002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 nfs4_lock_state();
4004
4005 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07004006 /*
4007 * Client indicates that this is a new lockowner.
4008 * Use open owner and open stateid to create lock owner and
4009 * lock stateid.
4010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 struct nfs4_stateid *open_stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03004014 if (!nfsd4_has_session(cstate) &&
4015 STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 /* validate and update open stateid and open seqid */
Benny Halevydd453df2009-04-03 08:28:41 +03004019 status = nfs4_preprocess_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 lock->lk_new_open_seqid,
4021 &lock->lk_new_open_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05004022 OPEN_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08004023 &lock->lk_replay_owner, &open_stp,
Neil Brownb59e3c02005-09-13 01:25:38 -07004024 lock);
NeilBrown37515172005-07-07 17:59:16 -07004025 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08004027 open_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 /* create lockowner and lock stateid */
4029 fp = open_stp->st_file;
4030 strhashval = lock_ownerstr_hashval(fp->fi_inode,
4031 open_sop->so_client->cl_clientid.cl_id,
4032 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07004033 /* XXX: Do we need to check for duplicate stateowners on
4034 * the same file, or should they just be allowed (and
4035 * create new stateids)? */
J. Bruce Fields3e772462011-08-10 19:07:33 -04004036 status = nfserr_jukebox;
Neil Brownb59e3c02005-09-13 01:25:38 -07004037 lock_sop = alloc_init_lock_stateowner(strhashval,
4038 open_sop->so_client, open_stp, lock);
4039 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07004041 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08004042 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 } else {
4045 /* lock (lock owner + lock stateid) already exists */
Benny Halevydd453df2009-04-03 08:28:41 +03004046 status = nfs4_preprocess_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 lock->lk_old_lock_seqid,
4048 &lock->lk_old_lock_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05004049 LOCK_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08004050 &lock->lk_replay_owner, &lock_stp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 if (status)
4052 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08004053 lock_sop = lock->lk_replay_owner;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004054 fp = lock_stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 }
J. Bruce Fields3a655882006-01-18 17:43:19 -08004056 /* lock->lk_replay_owner and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
NeilBrown0dd395d2005-07-07 17:59:15 -07004058 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004059 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004060 goto out;
4061 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004062 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004063 goto out;
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 locks_init_lock(&file_lock);
4066 switch (lock->lk_type) {
4067 case NFS4_READ_LT:
4068 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004069 filp = find_readable_file(lock_stp->st_file);
4070 if (filp)
4071 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 file_lock.fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004073 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 case NFS4_WRITE_LT:
4075 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004076 filp = find_writeable_file(lock_stp->st_file);
4077 if (filp)
4078 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 file_lock.fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004080 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 default:
4082 status = nfserr_inval;
4083 goto out;
4084 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004085 if (!filp) {
4086 status = nfserr_openmode;
4087 goto out;
4088 }
Neil Brownb59e3c02005-09-13 01:25:38 -07004089 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 file_lock.fl_pid = current->tgid;
4091 file_lock.fl_file = filp;
4092 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004093 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
4095 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004096 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 nfs4_transform_lock_offset(&file_lock);
4098
4099 /*
4100 * Try to lock the file in the VFS.
4101 * Note: locks.c uses the BKL to protect the inode's lock list.
4102 */
4103
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004104 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004105 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 case 0: /* success! */
4107 update_stateid(&lock_stp->st_stateid);
4108 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
4109 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004110 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004111 break;
4112 case (EAGAIN): /* conflock holds conflicting lock */
4113 status = nfserr_denied;
4114 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4115 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4116 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 case (EDEADLK):
4118 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004119 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004120 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004121 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004122 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08004126 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004127 release_lockowner(lock_sop);
J. Bruce Fields3a655882006-01-18 17:43:19 -08004128 if (lock->lk_replay_owner) {
4129 nfs4_get_stateowner(lock->lk_replay_owner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004130 cstate->replay_owner = lock->lk_replay_owner;
Neil Brownf2327d92005-09-13 01:25:37 -07004131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 nfs4_unlock_state();
4133 return status;
4134}
4135
4136/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004137 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4138 * so we do a temporary open here just to get an open file to pass to
4139 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4140 * inode operation.)
4141 */
4142static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4143{
4144 struct file *file;
4145 int err;
4146
4147 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4148 if (err)
4149 return err;
4150 err = vfs_test_lock(file, lock);
4151 nfsd_close(file);
4152 return err;
4153}
4154
4155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 * LOCKT operation
4157 */
Al Virob37ad282006-10-19 23:28:59 -07004158__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004159nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4160 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
4162 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 struct file_lock file_lock;
Marc Eshelfd85b812006-11-28 16:26:41 -05004164 int error;
Al Virob37ad282006-10-19 23:28:59 -07004165 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004167 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 return nfserr_grace;
4169
4170 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4171 return nfserr_inval;
4172
4173 lockt->lt_stateowner = NULL;
4174 nfs4_lock_state();
4175
4176 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03004177 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004180 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004183 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 locks_init_lock(&file_lock);
4185 switch (lockt->lt_type) {
4186 case NFS4_READ_LT:
4187 case NFS4_READW_LT:
4188 file_lock.fl_type = F_RDLCK;
4189 break;
4190 case NFS4_WRITE_LT:
4191 case NFS4_WRITEW_LT:
4192 file_lock.fl_type = F_WRLCK;
4193 break;
4194 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004195 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 status = nfserr_inval;
4197 goto out;
4198 }
4199
4200 lockt->lt_stateowner = find_lockstateowner_str(inode,
4201 &lockt->lt_clientid, &lockt->lt_owner);
4202 if (lockt->lt_stateowner)
4203 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
4204 file_lock.fl_pid = current->tgid;
4205 file_lock.fl_flags = FL_POSIX;
4206
4207 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004208 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
4210 nfs4_transform_lock_offset(&file_lock);
4211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004213 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05004214 if (error) {
4215 status = nfserrno(error);
4216 goto out;
4217 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004218 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004220 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 }
4222out:
4223 nfs4_unlock_state();
4224 return status;
4225}
4226
Al Virob37ad282006-10-19 23:28:59 -07004227__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004228nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004229 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230{
4231 struct nfs4_stateid *stp;
4232 struct file *filp = NULL;
4233 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07004234 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004235 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4238 (long long) locku->lu_offset,
4239 (long long) locku->lu_length);
4240
4241 if (check_lock_length(locku->lu_offset, locku->lu_length))
4242 return nfserr_inval;
4243
4244 nfs4_lock_state();
4245
Benny Halevydd453df2009-04-03 08:28:41 +03004246 if ((status = nfs4_preprocess_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 locku->lu_seqid,
4248 &locku->lu_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05004249 LOCK_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 &locku->lu_stateowner, &stp, NULL)))
4251 goto out;
4252
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004253 filp = find_any_file(stp->st_file);
4254 if (!filp) {
4255 status = nfserr_lock_range;
4256 goto out;
4257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 BUG_ON(!filp);
4259 locks_init_lock(&file_lock);
4260 file_lock.fl_type = F_UNLCK;
4261 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
4262 file_lock.fl_pid = current->tgid;
4263 file_lock.fl_file = filp;
4264 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004265 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 file_lock.fl_start = locku->lu_offset;
4267
Benny Halevy87df4de2008-12-15 19:42:03 +02004268 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 nfs4_transform_lock_offset(&file_lock);
4270
4271 /*
4272 * Try to unlock the file in the VFS.
4273 */
Marc Eshelfd85b812006-11-28 16:26:41 -05004274 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004275 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004276 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 goto out_nfserr;
4278 }
4279 /*
4280 * OK, unlock succeeded; the only thing left to do is update the stateid.
4281 */
4282 update_stateid(&stp->st_stateid);
4283 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
4284
4285out:
Neil Brownf2327d92005-09-13 01:25:37 -07004286 if (locku->lu_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 nfs4_get_stateowner(locku->lu_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004288 cstate->replay_owner = locku->lu_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07004289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 nfs4_unlock_state();
4291 return status;
4292
4293out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004294 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 goto out;
4296}
4297
4298/*
4299 * returns
4300 * 1: locks held by lockowner
4301 * 0: no locks held by lockowner
4302 */
4303static int
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004304check_for_locks(struct nfs4_file *filp, struct nfs4_stateowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
4306 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004307 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 int status = 0;
4309
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004310 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004312 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 status = 1;
4314 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 }
4317out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004318 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return status;
4320}
4321
Al Virob37ad282006-10-19 23:28:59 -07004322__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004323nfsd4_release_lockowner(struct svc_rqst *rqstp,
4324 struct nfsd4_compound_state *cstate,
4325 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004328 struct nfs4_stateowner *sop;
4329 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004331 struct list_head matches;
4332 int i;
Al Virob37ad282006-10-19 23:28:59 -07004333 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
4335 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4336 clid->cl_boot, clid->cl_id);
4337
4338 /* XXX check for lease expiration */
4339
4340 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07004341 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
4344 nfs4_lock_state();
4345
NeilBrown3e9e3db2005-06-23 22:04:20 -07004346 status = nfserr_locks_held;
4347 /* XXX: we're doing a linear search through all the lockowners.
4348 * Yipes! For now we'll just hope clients aren't really using
4349 * release_lockowner much, but eventually we have to fix these
4350 * data structures. */
4351 INIT_LIST_HEAD(&matches);
4352 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4353 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04004354 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004355 continue;
4356 list_for_each_entry(stp, &sop->so_stateids,
4357 st_perstateowner) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004358 if (check_for_locks(stp->st_file, sop))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004359 goto out;
4360 /* Note: so_perclient unused for lockowners,
4361 * so it's OK to fool with here. */
4362 list_add(&sop->so_perclient, &matches);
4363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004365 }
4366 /* Clients probably won't expect us to return with some (but not all)
4367 * of the lockowner state released; so don't release any until all
4368 * have been checked. */
4369 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004370 while (!list_empty(&matches)) {
4371 sop = list_entry(matches.next, struct nfs4_stateowner,
4372 so_perclient);
4373 /* unhash_stateowner deletes so_perclient only
4374 * for openowners. */
4375 list_del(&sop->so_perclient);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004376 release_lockowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 }
4378out:
4379 nfs4_unlock_state();
4380 return status;
4381}
4382
4383static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004384alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
NeilBrowna55370a2005-06-23 22:03:52 -07004386 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387}
4388
NeilBrownc7b9a452005-06-23 22:04:30 -07004389int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03004390nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07004391{
4392 unsigned int strhashval = clientstr_hashval(name);
4393 struct nfs4_client *clp;
4394
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004395 clp = find_confirmed_client_by_str(name, strhashval);
NeilBrownc7b9a452005-06-23 22:04:30 -07004396 return clp ? 1 : 0;
4397}
4398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399/*
4400 * failure => all reset bets are off, nfserr_no_grace...
4401 */
NeilBrown190e4fb2005-06-23 22:04:25 -07004402int
4403nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404{
4405 unsigned int strhashval;
4406 struct nfs4_client_reclaim *crp = NULL;
4407
NeilBrowna55370a2005-06-23 22:03:52 -07004408 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4409 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 if (!crp)
4411 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07004412 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 INIT_LIST_HEAD(&crp->cr_strhash);
4414 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07004415 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 reclaim_str_hashtbl_size++;
4417 return 1;
4418}
4419
4420static void
4421nfs4_release_reclaim(void)
4422{
4423 struct nfs4_client_reclaim *crp = NULL;
4424 int i;
4425
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4427 while (!list_empty(&reclaim_str_hashtbl[i])) {
4428 crp = list_entry(reclaim_str_hashtbl[i].next,
4429 struct nfs4_client_reclaim, cr_strhash);
4430 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 kfree(crp);
4432 reclaim_str_hashtbl_size--;
4433 }
4434 }
4435 BUG_ON(reclaim_str_hashtbl_size);
4436}
4437
4438/*
4439 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07004440static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441nfs4_find_reclaim_client(clientid_t *clid)
4442{
4443 unsigned int strhashval;
4444 struct nfs4_client *clp;
4445 struct nfs4_client_reclaim *crp = NULL;
4446
4447
4448 /* find clientid in conf_id_hashtbl */
4449 clp = find_confirmed_client(clid);
4450 if (clp == NULL)
4451 return NULL;
4452
NeilBrowna55370a2005-06-23 22:03:52 -07004453 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4454 clp->cl_name.len, clp->cl_name.data,
4455 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07004458 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07004460 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 return crp;
4462 }
4463 }
4464 return NULL;
4465}
4466
4467/*
4468* Called from OPEN. Look for clientid in reclaim list.
4469*/
Al Virob37ad282006-10-19 23:28:59 -07004470__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471nfs4_check_open_reclaim(clientid_t *clid)
4472{
NeilBrowndfc83562005-06-23 22:03:16 -07004473 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474}
4475
NeilBrownac4d8ff22005-06-23 22:03:30 -07004476/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004478int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004479nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004481 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004483 status = nfsd4_init_slabs();
4484 if (status)
4485 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4487 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4488 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4489 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4490 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
Wang Chen02cb2852009-04-24 15:41:57 +08004491 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 }
Marc Eshel5282fd72009-04-03 08:27:52 +03004493 for (i = 0; i < SESSION_HASH_SIZE; i++)
4494 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 for (i = 0; i < FILE_HASH_SIZE; i++) {
4496 INIT_LIST_HEAD(&file_hashtbl[i]);
4497 }
4498 for (i = 0; i < OWNER_HASH_SIZE; i++) {
4499 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
4500 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
4501 }
4502 for (i = 0; i < STATEID_HASH_SIZE; i++) {
4503 INIT_LIST_HEAD(&stateid_hashtbl[i]);
4504 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
4505 }
4506 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4507 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4508 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 INIT_LIST_HEAD(&close_lru);
4512 INIT_LIST_HEAD(&client_lru);
4513 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004514 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004515 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07004516}
4517
NeilBrown190e4fb2005-06-23 22:04:25 -07004518static void
4519nfsd4_load_reboot_recovery_data(void)
4520{
4521 int status;
4522
NeilBrown0964a3d2005-06-23 22:04:32 -07004523 nfs4_lock_state();
J. Bruce Fields48483bf2011-08-26 20:40:28 -04004524 nfsd4_init_recdir();
NeilBrown190e4fb2005-06-23 22:04:25 -07004525 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07004526 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07004527 if (status)
4528 printk("NFSD: Failure reading reboot recovery data\n");
4529}
4530
Meelap Shahc2f1a552007-07-17 04:04:39 -07004531/*
4532 * Since the lifetime of a delegation isn't limited to that of an open, a
4533 * client may quite reasonably hang on to a delegation as long as it has
4534 * the inode cached. This becomes an obvious problem the first time a
4535 * client's inode cache approaches the size of the server's total memory.
4536 *
4537 * For now we avoid this problem by imposing a hard limit on the number
4538 * of delegations, which varies according to the server's memory size.
4539 */
4540static void
4541set_max_delegations(void)
4542{
4543 /*
4544 * Allow at most 4 delegations per megabyte of RAM. Quick
4545 * estimates suggest that in the worst case (where every delegation
4546 * is for a different inode), a delegation could take about 1.5K,
4547 * giving a worst case usage of about 6% of memory.
4548 */
4549 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4550}
4551
NeilBrownac4d8ff22005-06-23 22:03:30 -07004552/* initialization to perform when the nfsd service is started: */
4553
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004554static int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004555__nfs4_state_start(void)
4556{
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004557 int ret;
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004560 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07004561 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004562 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004563 ret = set_callback_cred();
4564 if (ret)
4565 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004566 laundry_wq = create_singlethread_workqueue("nfsd4");
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004567 if (laundry_wq == NULL)
4568 return -ENOMEM;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004569 ret = nfsd4_create_callback_queue();
4570 if (ret)
4571 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004572 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004573 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004574 return 0;
4575out_free_laundry:
4576 destroy_workqueue(laundry_wq);
4577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578}
4579
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004580int
NeilBrown76a35502005-06-23 22:03:26 -07004581nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
NeilBrown190e4fb2005-06-23 22:04:25 -07004583 nfsd4_load_reboot_recovery_data();
Jeff Layton4ad9a342010-07-19 16:50:04 -04004584 return __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585}
4586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587static void
4588__nfs4_state_shutdown(void)
4589{
4590 int i;
4591 struct nfs4_client *clp = NULL;
4592 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 struct list_head *pos, *next, reaplist;
4594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4596 while (!list_empty(&conf_id_hashtbl[i])) {
4597 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4598 expire_client(clp);
4599 }
4600 while (!list_empty(&unconf_str_hashtbl[i])) {
4601 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4602 expire_client(clp);
4603 }
4604 }
4605 INIT_LIST_HEAD(&reaplist);
4606 spin_lock(&recall_lock);
4607 list_for_each_safe(pos, next, &del_recall_lru) {
4608 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4609 list_move(&dp->dl_recall_lru, &reaplist);
4610 }
4611 spin_unlock(&recall_lock);
4612 list_for_each_safe(pos, next, &reaplist) {
4613 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4614 list_del_init(&dp->dl_recall_lru);
4615 unhash_delegation(dp);
4616 }
4617
NeilBrown190e4fb2005-06-23 22:04:25 -07004618 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619}
4620
4621void
4622nfs4_state_shutdown(void)
4623{
Tejun Heoafe2c512010-12-14 16:21:17 +01004624 cancel_delayed_work_sync(&laundromat_work);
NeilBrown5e8d5c22006-04-10 22:55:37 -07004625 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06004626 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 nfs4_lock_state();
4628 nfs4_release_reclaim();
4629 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 nfs4_unlock_state();
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004631 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}