blob: 48134635fc2fc3988885d4eb46b41aa680b15aae [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 */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -040063static struct nfs4_delegation * search_for_delegation(stateid_t *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040065static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
J. Bruce Fields8b671b82009-02-22 14:51:34 -080067/* Locking: */
68
69/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080070static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
J. Bruce Fields8b671b82009-02-22 14:51:34 -080072/*
73 * Currently used for the del_recall_lru and file hash table. In an
74 * effort to decrease the scope of the client_mutex, this spinlock may
75 * eventually cover more:
76 */
77static DEFINE_SPINLOCK(recall_lock);
78
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040079static struct kmem_cache *openowner_slab = NULL;
80static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080081static struct kmem_cache *file_slab = NULL;
82static struct kmem_cache *stateid_slab = NULL;
83static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085void
86nfs4_lock_state(void)
87{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080088 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91void
92nfs4_unlock_state(void)
93{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080094 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97static inline u32
98opaque_hashval(const void *ptr, int nbytes)
99{
100 unsigned char *cptr = (unsigned char *) ptr;
101
102 u32 x = 0;
103 while (nbytes--) {
104 x *= 37;
105 x += *cptr++;
106 }
107 return x;
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static struct list_head del_recall_lru;
111
NeilBrown13cd2182005-06-23 22:03:10 -0700112static inline void
113put_nfs4_file(struct nfs4_file *fi)
114{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800115 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
116 list_del(&fi->fi_hash);
117 spin_unlock(&recall_lock);
118 iput(fi->fi_inode);
119 kmem_cache_free(file_slab, fi);
120 }
NeilBrown13cd2182005-06-23 22:03:10 -0700121}
122
123static inline void
124get_nfs4_file(struct nfs4_file *fi)
125{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800126 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700127}
128
NeilBrownef0f3392006-04-10 22:55:41 -0700129static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700130unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700131
132/*
133 * Open owner state (share locks)
134 */
135
J. Bruce Fields506f2752011-08-11 18:50:18 -0400136/* hash tables for open owners */
137#define OPEN_OWNER_HASH_BITS 8
138#define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS)
139#define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700140
J. Bruce Fields506f2752011-08-11 18:50:18 -0400141static unsigned int open_ownerid_hashval(const u32 id)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400142{
J. Bruce Fields506f2752011-08-11 18:50:18 -0400143 return id & OPEN_OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400144}
145
J. Bruce Fields506f2752011-08-11 18:50:18 -0400146static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400147{
148 unsigned int ret;
149
150 ret = opaque_hashval(ownername->data, ownername->len);
151 ret += clientid;
J. Bruce Fields506f2752011-08-11 18:50:18 -0400152 return ret & OPEN_OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400153}
NeilBrownef0f3392006-04-10 22:55:41 -0700154
J. Bruce Fields506f2752011-08-11 18:50:18 -0400155static struct list_head open_ownerid_hashtbl[OPEN_OWNER_HASH_SIZE];
156static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700157
158/* hash table for nfs4_file */
159#define FILE_HASH_BITS 8
160#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800161
NeilBrownef0f3392006-04-10 22:55:41 -0700162/* hash table for (open)nfs4_stateid */
163#define STATEID_HASH_BITS 10
164#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
165#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
166
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400167static unsigned int file_hashval(struct inode *ino)
168{
169 /* XXX: why are we hashing on inode pointer, anyway? */
170 return hash_ptr(ino, FILE_HASH_BITS);
171}
172
173static unsigned int stateid_hashval(u32 owner_id, u32 file_id)
174{
175 return (owner_id + file_id) & STATEID_HASH_MASK;
176}
NeilBrownef0f3392006-04-10 22:55:41 -0700177
178static struct list_head file_hashtbl[FILE_HASH_SIZE];
179static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
180
J. Bruce Fields998db522010-08-07 09:21:41 -0400181static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400182{
183 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
184 atomic_inc(&fp->fi_access[oflag]);
185}
186
J. Bruce Fields998db522010-08-07 09:21:41 -0400187static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
188{
189 if (oflag == O_RDWR) {
190 __nfs4_file_get_access(fp, O_RDONLY);
191 __nfs4_file_get_access(fp, O_WRONLY);
192 } else
193 __nfs4_file_get_access(fp, oflag);
194}
195
196static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400197{
198 if (fp->fi_fds[oflag]) {
199 fput(fp->fi_fds[oflag]);
200 fp->fi_fds[oflag] = NULL;
201 }
202}
203
J. Bruce Fields998db522010-08-07 09:21:41 -0400204static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400205{
206 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
207 nfs4_file_put_fd(fp, O_RDWR);
208 nfs4_file_put_fd(fp, oflag);
209 }
210}
211
J. Bruce Fields998db522010-08-07 09:21:41 -0400212static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
213{
214 if (oflag == O_RDWR) {
215 __nfs4_file_put_access(fp, O_RDONLY);
216 __nfs4_file_put_access(fp, O_WRONLY);
217 } else
218 __nfs4_file_put_access(fp, oflag);
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static struct nfs4_delegation *
222alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
223{
224 struct nfs4_delegation *dp;
225 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400228 /*
229 * Major work on the lease subsystem (for example, to support
230 * calbacks on stat) will be required before we can support
231 * write delegations properly.
232 */
233 if (type != NFS4_OPEN_DELEGATE_READ)
234 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700235 if (fp->fi_had_conflict)
236 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700237 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700238 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700239 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
240 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700242 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700243 INIT_LIST_HEAD(&dp->dl_perfile);
244 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 INIT_LIST_HEAD(&dp->dl_recall_lru);
246 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700247 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 dp->dl_type = type;
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -0400250 dp->dl_stateid.si_boot = boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 dp->dl_stateid.si_stateownerid = current_delegid++;
252 dp->dl_stateid.si_fileid = 0;
J. Bruce Fields73997dc2011-08-31 15:47:21 -0400253 dp->dl_stateid.si_generation = 1;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500254 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dp->dl_time = 0;
256 atomic_set(&dp->dl_count, 1);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500257 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return dp;
259}
260
261void
262nfs4_put_delegation(struct nfs4_delegation *dp)
263{
264 if (atomic_dec_and_test(&dp->dl_count)) {
265 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700266 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700267 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700268 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270}
271
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500272static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500274 if (atomic_dec_and_test(&fp->fi_delegees)) {
275 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
276 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400277 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500278 fp->fi_deleg_file = NULL;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/* Called under the state lock. */
283static void
284unhash_delegation(struct nfs4_delegation *dp)
285{
NeilBrownea1da632005-06-23 22:04:17 -0700286 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500288 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 list_del_init(&dp->dl_recall_lru);
290 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500291 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 nfs4_put_delegation(dp);
293}
294
295/*
296 * SETCLIENTID state
297 */
298
Benny Halevy36acb662010-05-12 00:13:04 +0300299/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300300static DEFINE_SPINLOCK(client_lock);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/* Hash tables for nfs4_clientid state */
303#define CLIENT_HASH_BITS 4
304#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
305#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
306
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400307static unsigned int clientid_hashval(u32 id)
308{
309 return id & CLIENT_HASH_MASK;
310}
311
312static unsigned int clientstr_hashval(const char *name)
313{
314 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
318 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
319 * used in reboot/reset lease grace period processing
320 *
321 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
322 * setclientid_confirmed info.
323 *
324 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
325 * setclientid info.
326 *
327 * client_lru holds client queue ordered by nfs4_client.cl_time
328 * for lease renewal.
329 *
330 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
331 * for last close replay.
332 */
333static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
334static int reclaim_str_hashtbl_size = 0;
335static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
336static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
337static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
338static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
339static struct list_head client_lru;
340static struct list_head close_lru;
341
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400342/*
343 * We store the NONE, READ, WRITE, and BOTH bits separately in the
344 * st_{access,deny}_bmap field of the stateid, in order to track not
345 * only what share bits are currently in force, but also what
346 * combinations of share bits previous opens have used. This allows us
347 * to enforce the recommendation of rfc 3530 14.2.19 that the server
348 * return an error if the client attempt to downgrade to a combination
349 * of share bits not explicable by closing some of its previous opens.
350 *
351 * XXX: This enforcement is actually incomplete, since we don't keep
352 * track of access/deny bit combinations; so, e.g., we allow:
353 *
354 * OPEN allow read, deny write
355 * OPEN allow both, deny none
356 * DOWNGRADE allow read, deny none
357 *
358 * which we should reject.
359 */
360static void
361set_access(unsigned int *access, unsigned long bmap) {
362 int i;
363
364 *access = 0;
365 for (i = 1; i < 4; i++) {
366 if (test_bit(i, &bmap))
367 *access |= i;
368 }
369}
370
371static void
372set_deny(unsigned int *deny, unsigned long bmap) {
373 int i;
374
375 *deny = 0;
376 for (i = 0; i < 4; i++) {
377 if (test_bit(i, &bmap))
378 *deny |= i ;
379 }
380}
381
382static int
383test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
384 unsigned int access, deny;
385
386 set_access(&access, stp->st_access_bmap);
387 set_deny(&deny, stp->st_deny_bmap);
388 if ((access & open->op_share_deny) || (deny & open->op_share_access))
389 return 0;
390 return 1;
391}
392
393static int nfs4_access_to_omode(u32 access)
394{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400395 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400396 case NFS4_SHARE_ACCESS_READ:
397 return O_RDONLY;
398 case NFS4_SHARE_ACCESS_WRITE:
399 return O_WRONLY;
400 case NFS4_SHARE_ACCESS_BOTH:
401 return O_RDWR;
402 }
403 BUG();
404}
405
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500406static void unhash_generic_stateid(struct nfs4_stateid *stp)
407{
408 list_del(&stp->st_hash);
409 list_del(&stp->st_perfile);
410 list_del(&stp->st_perstateowner);
411}
412
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400413static void close_generic_stateid(struct nfs4_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500414{
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400415 int i;
J. Bruce Fields0997b172011-03-02 18:01:35 -0500416
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800417 if (stp->st_access_bmap) {
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400418 for (i = 1; i < 4; i++) {
419 if (test_bit(i, &stp->st_access_bmap))
420 nfs4_file_put_access(stp->st_file,
421 nfs4_access_to_omode(i));
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400422 __clear_bit(i, &stp->st_access_bmap);
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400423 }
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800424 }
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400425 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400426 stp->st_file = NULL;
427}
428
429static void free_generic_stateid(struct nfs4_stateid *stp)
430{
431 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500432 kmem_cache_free(stateid_slab, stp);
433}
434
435static void release_lock_stateid(struct nfs4_stateid *stp)
436{
437 struct file *file;
438
439 unhash_generic_stateid(stp);
440 file = find_any_file(stp->st_file);
441 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400442 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500443 free_generic_stateid(stp);
444}
445
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400446static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500447{
448 struct nfs4_stateid *stp;
449
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400450 list_del(&lo->lo_owner.so_idhash);
451 list_del(&lo->lo_owner.so_strhash);
452 list_del(&lo->lo_perstateid);
453 while (!list_empty(&lo->lo_owner.so_stateids)) {
454 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500455 struct nfs4_stateid, st_perstateowner);
456 release_lock_stateid(stp);
457 }
458}
459
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400460static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500461{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400462 unhash_lockowner(lo);
463 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500464}
465
466static void
467release_stateid_lockowners(struct nfs4_stateid *open_stp)
468{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400469 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500470
471 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400472 lo = list_entry(open_stp->st_lockowners.next,
473 struct nfs4_lockowner, lo_perstateid);
474 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500475 }
476}
477
J. Bruce Fields22839632009-01-11 14:27:17 -0500478static void release_open_stateid(struct nfs4_stateid *stp)
479{
480 unhash_generic_stateid(stp);
481 release_stateid_lockowners(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500482 free_generic_stateid(stp);
483}
484
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400485static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500486{
487 struct nfs4_stateid *stp;
488
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400489 list_del(&oo->oo_owner.so_idhash);
490 list_del(&oo->oo_owner.so_strhash);
491 list_del(&oo->oo_perclient);
492 while (!list_empty(&oo->oo_owner.so_stateids)) {
493 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500494 struct nfs4_stateid, st_perstateowner);
495 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500496 }
497}
498
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400499static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500500{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400501 unhash_openowner(oo);
502 list_del(&oo->oo_close_lru);
503 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500504}
505
Marc Eshel5282fd72009-04-03 08:27:52 +0300506#define SESSION_HASH_SIZE 512
507static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
508
509static inline int
510hash_sessionid(struct nfs4_sessionid *sessionid)
511{
512 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
513
514 return sid->sequence % SESSION_HASH_SIZE;
515}
516
517static inline void
518dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
519{
520 u32 *ptr = (u32 *)(&sessionid->data[0]);
521 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
522}
523
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300524static void
525gen_sessionid(struct nfsd4_session *ses)
526{
527 struct nfs4_client *clp = ses->se_client;
528 struct nfsd4_sessionid *sid;
529
530 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
531 sid->clientid = clp->cl_clientid;
532 sid->sequence = current_sessionid++;
533 sid->reserved = 0;
534}
535
536/*
Andy Adamsona6496372009-08-28 08:45:01 -0400537 * The protocol defines ca_maxresponssize_cached to include the size of
538 * the rpc header, but all we need to cache is the data starting after
539 * the end of the initial SEQUENCE operation--the rest we regenerate
540 * each time. Therefore we can advertise a ca_maxresponssize_cached
541 * value that is the number of bytes in our cache plus a few additional
542 * bytes. In order to stay on the safe side, and not promise more than
543 * we can cache, those additional bytes must be the minimum possible: 24
544 * bytes of rpc header (xid through accept state, with AUTH_NULL
545 * verifier), 12 for the compound header (with zero-length tag), and 44
546 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300547 */
Andy Adamsona6496372009-08-28 08:45:01 -0400548#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
549
Andy Adamson557ce262009-08-28 08:45:04 -0400550static void
551free_session_slots(struct nfsd4_session *ses)
552{
553 int i;
554
555 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
556 kfree(ses->se_slots[i]);
557}
558
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400559/*
560 * We don't actually need to cache the rpc and session headers, so we
561 * can allocate a little less for each slot:
562 */
563static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
564{
565 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
566}
567
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400568static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300569{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400570 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
571 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300572
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400573 return size;
574}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300575
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400576/*
577 * XXX: If we run out of reserved DRC memory we could (up to a point)
578 * re-negotiate active sessions and reduce their slot usage to make
579 * rooom for new connections. For now we just fail the create session.
580 */
581static int nfsd4_get_drc_mem(int slotsize, u32 num)
582{
583 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300584
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400585 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400586
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400587 spin_lock(&nfsd_drc_lock);
588 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
589 nfsd_drc_max_mem - nfsd_drc_mem_used);
590 num = min_t(int, num, avail / slotsize);
591 nfsd_drc_mem_used += num * slotsize;
592 spin_unlock(&nfsd_drc_lock);
593
594 return num;
595}
596
597static void nfsd4_put_drc_mem(int slotsize, int num)
598{
599 spin_lock(&nfsd_drc_lock);
600 nfsd_drc_mem_used -= slotsize * num;
601 spin_unlock(&nfsd_drc_lock);
602}
603
604static struct nfsd4_session *alloc_session(int slotsize, int numslots)
605{
606 struct nfsd4_session *new;
607 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300608
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400609 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400610 + sizeof(struct nfsd4_session) > PAGE_SIZE);
611 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400612
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400613 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300614 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400615 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400616 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400617 for (i = 0; i < numslots; i++) {
618 mem = sizeof(struct nfsd4_slot) + slotsize;
619 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
620 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400621 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400622 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400623 return new;
624out_free:
625 while (i--)
626 kfree(new->se_slots[i]);
627 kfree(new);
628 return NULL;
629}
630
631static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
632{
633 u32 maxrpc = nfsd_serv->sv_max_mesg;
634
635 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800636 new->maxresp_cached = min_t(u32, req->maxresp_cached,
637 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400638 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
639 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
640 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
641}
642
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400643static void free_conn(struct nfsd4_conn *c)
644{
645 svc_xprt_put(c->cn_xprt);
646 kfree(c);
647}
648
649static void nfsd4_conn_lost(struct svc_xpt_user *u)
650{
651 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
652 struct nfs4_client *clp = c->cn_session->se_client;
653
654 spin_lock(&clp->cl_lock);
655 if (!list_empty(&c->cn_persession)) {
656 list_del(&c->cn_persession);
657 free_conn(c);
658 }
659 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500660 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400661}
662
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400663static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400664{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400665 struct nfsd4_conn *conn;
666
667 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
668 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400669 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400670 svc_xprt_get(rqstp->rq_xprt);
671 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400672 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400673 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
674 return conn;
675}
676
J. Bruce Fields328ead22010-09-29 16:11:06 -0400677static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
678{
679 conn->cn_session = ses;
680 list_add(&conn->cn_persession, &ses->se_conns);
681}
682
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400683static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
684{
685 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400686
687 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400688 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400689 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400690}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400691
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400692static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400693{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400694 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400695 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400696}
697
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400698static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400699{
700 struct nfsd4_conn *conn;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400701 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400702
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400703 conn = alloc_conn(rqstp, dir);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400704 if (!conn)
705 return nfserr_jukebox;
706 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400707 ret = nfsd4_register_conn(conn);
708 if (ret)
709 /* oops; xprt is already down: */
710 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400711 return nfs_ok;
712}
713
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400714static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
715{
716 u32 dir = NFS4_CDFC4_FORE;
717
718 if (ses->se_flags & SESSION4_BACK_CHAN)
719 dir |= NFS4_CDFC4_BACK;
720
721 return nfsd4_new_conn(rqstp, ses, dir);
722}
723
724/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400725static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400726{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400727 struct nfs4_client *clp = s->se_client;
728 struct nfsd4_conn *c;
729
730 spin_lock(&clp->cl_lock);
731 while (!list_empty(&s->se_conns)) {
732 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
733 list_del_init(&c->cn_persession);
734 spin_unlock(&clp->cl_lock);
735
736 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
737 free_conn(c);
738
739 spin_lock(&clp->cl_lock);
740 }
741 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400742}
743
744void free_session(struct kref *kref)
745{
746 struct nfsd4_session *ses;
747 int mem;
748
749 ses = container_of(kref, struct nfsd4_session, se_ref);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400750 nfsd4_del_conns(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400751 spin_lock(&nfsd_drc_lock);
752 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
753 nfsd_drc_mem_used -= mem;
754 spin_unlock(&nfsd_drc_lock);
755 free_session_slots(ses);
756 kfree(ses);
757}
758
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400759static 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 -0400760{
761 struct nfsd4_session *new;
762 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
763 int numslots, slotsize;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400764 int status;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400765 int idx;
766
767 /*
768 * Note decreasing slot size below client's request may
769 * make it difficult for client to function correctly, whereas
770 * decreasing the number of slots will (just?) affect
771 * performance. When short on memory we therefore prefer to
772 * decrease number of slots instead of their size.
773 */
774 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
775 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800776 if (numslots < 1)
777 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400778
779 new = alloc_session(slotsize, numslots);
780 if (!new) {
781 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400782 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400783 }
784 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
Andy Adamson557ce262009-08-28 08:45:04 -0400785
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300786 new->se_client = clp;
787 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300788
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400789 INIT_LIST_HEAD(&new->se_conns);
790
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400791 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300792 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400793 new->se_cb_prog = cses->callback_prog;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300794 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400795 idx = hash_sessionid(&new->se_sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300796 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300797 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400798 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300799 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400800 spin_unlock(&clp->cl_lock);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300801 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300802
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400803 status = nfsd4_new_conn_from_crses(rqstp, new);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400804 /* whoops: benny points out, status is ignored! (err, or bogus) */
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400805 if (status) {
806 free_session(&new->se_ref);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400807 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400808 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400809 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400810 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400811 /*
812 * This is a little silly; with sessions there's no real
813 * use for the callback address. Use the peer address
814 * as a reasonable default for now, but consider fixing
815 * the rpc client not to require an address in the
816 * future:
817 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400818 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
819 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400820 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400821 nfsd4_probe_callback(clp);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400822 return new;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300823}
824
Benny Halevy9089f1b2010-05-12 00:12:26 +0300825/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300826static struct nfsd4_session *
827find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
828{
829 struct nfsd4_session *elem;
830 int idx;
831
832 dump_sessionid(__func__, sessionid);
833 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300834 /* Search in the appropriate list */
835 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300836 if (!memcmp(elem->se_sessionid.data, sessionid->data,
837 NFS4_MAX_SESSIONID_LEN)) {
838 return elem;
839 }
840 }
841
842 dprintk("%s: session not found\n", __func__);
843 return NULL;
844}
845
Benny Halevy9089f1b2010-05-12 00:12:26 +0300846/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300847static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300848unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300849{
850 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400851 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300852 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400853 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +0300854}
855
Benny Halevy36acb662010-05-12 00:13:04 +0300856/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300858renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Benny Halevy07cd4902010-05-12 00:13:41 +0300860 if (is_client_expired(clp)) {
861 dprintk("%s: client (clientid %08x/%08x) already expired\n",
862 __func__,
863 clp->cl_clientid.cl_boot,
864 clp->cl_clientid.cl_id);
865 return;
866 }
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /*
869 * Move client to the end to the LRU list.
870 */
871 dprintk("renewing client (clientid %08x/%08x)\n",
872 clp->cl_clientid.cl_boot,
873 clp->cl_clientid.cl_id);
874 list_move_tail(&clp->cl_lru, &client_lru);
875 clp->cl_time = get_seconds();
876}
877
Benny Halevy36acb662010-05-12 00:13:04 +0300878static inline void
879renew_client(struct nfs4_client *clp)
880{
881 spin_lock(&client_lock);
882 renew_client_locked(clp);
883 spin_unlock(&client_lock);
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
887static int
888STALE_CLIENTID(clientid_t *clid)
889{
890 if (clid->cl_boot == boot_time)
891 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +0300892 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
893 clid->cl_boot, clid->cl_id, boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return 1;
895}
896
897/*
898 * XXX Should we use a slab cache ?
899 * This type of memory management is somewhat inefficient, but we use it
900 * anyway since SETCLIENTID is not a common operation.
901 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500902static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct nfs4_client *clp;
905
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500906 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
907 if (clp == NULL)
908 return NULL;
909 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
910 if (clp->cl_name.data == NULL) {
911 kfree(clp);
912 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500914 memcpy(clp->cl_name.data, name.data, name.len);
915 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return clp;
917}
918
919static inline void
920free_client(struct nfs4_client *clp)
921{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400922 while (!list_empty(&clp->cl_sessions)) {
923 struct nfsd4_session *ses;
924 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
925 se_perclnt);
926 list_del(&ses->se_perclnt);
927 nfsd4_put_session(ses);
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (clp->cl_cred.cr_group_info)
930 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500931 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 kfree(clp->cl_name.data);
933 kfree(clp);
934}
935
Benny Halevyd7682982010-05-12 00:13:54 +0300936void
937release_session_client(struct nfsd4_session *session)
938{
939 struct nfs4_client *clp = session->se_client;
940
941 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
942 return;
943 if (is_client_expired(clp)) {
944 free_client(clp);
945 session->se_client = NULL;
946 } else
947 renew_client_locked(clp);
948 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +0300949}
950
Benny Halevy84d38ac2010-05-12 00:13:16 +0300951/* must be called under the client_lock */
952static inline void
953unhash_client_locked(struct nfs4_client *clp)
954{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400955 struct nfsd4_session *ses;
956
Benny Halevy07cd4902010-05-12 00:13:41 +0300957 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300958 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400959 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400960 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
961 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400962 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static void
966expire_client(struct nfs4_client *clp)
967{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400968 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct list_head reaplist;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 INIT_LIST_HEAD(&reaplist);
973 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700974 while (!list_empty(&clp->cl_delegations)) {
975 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -0700976 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 list_move(&dp->dl_recall_lru, &reaplist);
978 }
979 spin_unlock(&recall_lock);
980 while (!list_empty(&reaplist)) {
981 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
982 list_del_init(&dp->dl_recall_lru);
983 unhash_delegation(dp);
984 }
NeilBrownea1da632005-06-23 22:04:17 -0700985 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400986 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
987 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400989 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500990 if (clp->cl_cb_conn.cb_xprt)
991 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300992 list_del(&clp->cl_idhash);
993 list_del(&clp->cl_strhash);
994 spin_lock(&client_lock);
995 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +0300996 if (atomic_read(&clp->cl_refcount) == 0)
997 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300998 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001001static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1002{
1003 memcpy(target->cl_verifier.data, source->data,
1004 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001007static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1008{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1010 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1011}
1012
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001013static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1014{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 target->cr_uid = source->cr_uid;
1016 target->cr_gid = source->cr_gid;
1017 target->cr_group_info = source->cr_group_info;
1018 get_group_info(target->cr_group_info);
1019}
1020
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001021static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001022{
NeilBrowna55370a2005-06-23 22:03:52 -07001023 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001027same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1028{
1029 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001033same_clid(clientid_t *cl1, clientid_t *cl2)
1034{
1035 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/* XXX what about NGROUP */
1039static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001040same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1041{
1042 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001045static void gen_clid(struct nfs4_client *clp)
1046{
1047 static u32 current_clientid = 1;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 clp->cl_clientid.cl_boot = boot_time;
1050 clp->cl_clientid.cl_id = current_clientid++;
1051}
1052
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001053static void gen_confirm(struct nfs4_client *clp)
1054{
1055 static u32 i;
1056 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001059 *p++ = get_seconds();
1060 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
J. Bruce Fields4581d142011-09-06 14:56:09 -04001063static int
1064same_stateid(stateid_t *id_one, stateid_t *id_two)
1065{
1066 if (id_one->si_stateownerid != id_two->si_stateownerid)
1067 return 0;
1068 return id_one->si_fileid == id_two->si_fileid;
1069}
1070
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001071static struct nfs4_stateid *find_stateid(stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001072{
1073 struct nfs4_stateid *s;
1074 unsigned int hashval;
1075
1076 hashval = stateid_hashval(t->si_stateownerid, t->si_fileid);
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001077 list_for_each_entry(s, &stateid_hashtbl[hashval], st_hash)
1078 if (same_stateid(&s->st_stateid, t))
1079 return s;
1080 return NULL;
1081}
1082
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04001083static struct nfs4_stateid *find_stateid_by_type(stateid_t *t, char typemask)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001084{
1085 struct nfs4_stateid *s;
1086
1087 s = find_stateid(t);
1088 if (!s)
1089 return NULL;
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04001090 if (typemask & s->st_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001091 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001092 return NULL;
1093}
1094
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001095static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1096 struct svc_rqst *rqstp, nfs4_verifier *verf)
1097{
1098 struct nfs4_client *clp;
1099 struct sockaddr *sa = svc_addr(rqstp);
1100 char *princ;
1101
1102 clp = alloc_client(name);
1103 if (clp == NULL)
1104 return NULL;
1105
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001106 INIT_LIST_HEAD(&clp->cl_sessions);
1107
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001108 princ = svc_gss_principal(rqstp);
1109 if (princ) {
1110 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1111 if (clp->cl_principal == NULL) {
1112 free_client(clp);
1113 return NULL;
1114 }
1115 }
1116
1117 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Benny Halevy46583e22010-05-12 00:13:29 +03001118 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001119 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001120 INIT_LIST_HEAD(&clp->cl_idhash);
1121 INIT_LIST_HEAD(&clp->cl_strhash);
1122 INIT_LIST_HEAD(&clp->cl_openowners);
1123 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001124 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001125 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001126 spin_lock_init(&clp->cl_lock);
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001127 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
Benny Halevy07cd4902010-05-12 00:13:41 +03001128 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001129 clear_bit(0, &clp->cl_cb_slot_busy);
1130 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1131 copy_verf(clp, verf);
1132 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1133 clp->cl_flavor = rqstp->rq_flavor;
1134 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1135 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001136 clp->cl_cb_session = NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001137 return clp;
1138}
1139
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001140static int check_name(struct xdr_netobj name)
1141{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (name.len == 0)
1143 return 0;
1144 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001145 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
1147 }
1148 return 1;
1149}
1150
NeilBrownfd39ca92005-06-23 22:04:03 -07001151static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1153{
1154 unsigned int idhashval;
1155
1156 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1157 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1158 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001159 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
NeilBrownfd39ca92005-06-23 22:04:03 -07001162static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163move_to_confirmed(struct nfs4_client *clp)
1164{
1165 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1166 unsigned int strhashval;
1167
1168 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001169 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07001170 strhashval = clientstr_hashval(clp->cl_recdir);
Benny Halevy328efba2010-05-12 00:12:51 +03001171 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 renew_client(clp);
1173}
1174
1175static struct nfs4_client *
1176find_confirmed_client(clientid_t *clid)
1177{
1178 struct nfs4_client *clp;
1179 unsigned int idhashval = clientid_hashval(clid->cl_id);
1180
1181 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001182 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return clp;
1184 }
1185 return NULL;
1186}
1187
1188static struct nfs4_client *
1189find_unconfirmed_client(clientid_t *clid)
1190{
1191 struct nfs4_client *clp;
1192 unsigned int idhashval = clientid_hashval(clid->cl_id);
1193
1194 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001195 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return clp;
1197 }
1198 return NULL;
1199}
1200
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001201static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001202{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001203 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001204}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001205
NeilBrown28ce6052005-06-23 22:03:56 -07001206static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001207find_confirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001208{
1209 struct nfs4_client *clp;
1210
1211 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001212 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001213 return clp;
1214 }
1215 return NULL;
1216}
1217
1218static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001219find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001220{
1221 struct nfs4_client *clp;
1222
1223 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001224 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001225 return clp;
1226 }
1227 return NULL;
1228}
1229
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001230static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1231{
1232 switch (family) {
1233 case AF_INET:
1234 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1235 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1236 return;
1237 case AF_INET6:
1238 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1239 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1240 return;
1241 }
1242}
1243
NeilBrownfd39ca92005-06-23 22:04:03 -07001244static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001245gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001247 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001248 struct sockaddr *sa = svc_addr(rqstp);
1249 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001250 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jeff Layton7077ecb2009-08-14 12:57:58 -04001252 /* Currently, we only support tcp and tcp6 for the callback channel */
1253 if (se->se_callback_netid_len == 3 &&
1254 !memcmp(se->se_callback_netid_val, "tcp", 3))
1255 expected_family = AF_INET;
1256 else if (se->se_callback_netid_len == 4 &&
1257 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1258 expected_family = AF_INET6;
1259 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto out_err;
1261
J. Bruce Fields07263f12010-05-31 19:09:40 -04001262 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001263 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001264 (struct sockaddr *)&conn->cb_addr,
1265 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001266
J. Bruce Fields07263f12010-05-31 19:09:40 -04001267 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001269
J. Bruce Fields07263f12010-05-31 19:09:40 -04001270 if (conn->cb_addr.ss_family == AF_INET6)
1271 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001272
J. Bruce Fields07263f12010-05-31 19:09:40 -04001273 conn->cb_prog = se->se_callback_prog;
1274 conn->cb_ident = se->se_callback_ident;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001275 rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return;
1277out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001278 conn->cb_addr.ss_family = AF_UNSPEC;
1279 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001280 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 "will not receive delegations\n",
1282 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return;
1285}
1286
Andy Adamson074fe892009-04-03 08:28:15 +03001287/*
Andy Adamson557ce262009-08-28 08:45:04 -04001288 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001289 */
1290void
1291nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1292{
Andy Adamson557ce262009-08-28 08:45:04 -04001293 struct nfsd4_slot *slot = resp->cstate.slot;
1294 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001295
Andy Adamson557ce262009-08-28 08:45:04 -04001296 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001297
Andy Adamson557ce262009-08-28 08:45:04 -04001298 slot->sl_opcnt = resp->opcnt;
1299 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001300
1301 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001302 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001303 return;
1304 }
Andy Adamson557ce262009-08-28 08:45:04 -04001305 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1306 base = (char *)resp->cstate.datap -
1307 (char *)resp->xbuf->head[0].iov_base;
1308 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1309 slot->sl_datalen))
1310 WARN("%s: sessions DRC could not cache compound\n", __func__);
1311 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001312}
1313
1314/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001315 * Encode the replay sequence operation from the slot values.
1316 * If cachethis is FALSE encode the uncached rep error on the next
1317 * operation which sets resp->p and increments resp->opcnt for
1318 * nfs4svc_encode_compoundres.
1319 *
Andy Adamson074fe892009-04-03 08:28:15 +03001320 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001321static __be32
1322nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1323 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001324{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001325 struct nfsd4_op *op;
1326 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001327
Andy Adamsonabfabf82009-07-23 19:02:18 -04001328 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
Andy Adamson557ce262009-08-28 08:45:04 -04001329 resp->opcnt, resp->cstate.slot->sl_cachethis);
Andy Adamsonabfabf82009-07-23 19:02:18 -04001330
1331 /* Encode the replayed sequence operation */
1332 op = &args->ops[resp->opcnt - 1];
1333 nfsd4_encode_operation(resp, op);
1334
1335 /* Return nfserr_retry_uncached_rep in next operation. */
Andy Adamson557ce262009-08-28 08:45:04 -04001336 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001337 op = &args->ops[resp->opcnt++];
1338 op->status = nfserr_retry_uncached_rep;
1339 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001340 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001341 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001342}
1343
1344/*
Andy Adamson557ce262009-08-28 08:45:04 -04001345 * The sequence operation is not cached because we can use the slot and
1346 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001347 */
1348__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001349nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1350 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001351{
Andy Adamson557ce262009-08-28 08:45:04 -04001352 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001353 __be32 status;
1354
Andy Adamson557ce262009-08-28 08:45:04 -04001355 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001356
Andy Adamsonabfabf82009-07-23 19:02:18 -04001357 /* Either returns 0 or nfserr_retry_uncached */
1358 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1359 if (status == nfserr_retry_uncached_rep)
1360 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001361
Andy Adamson557ce262009-08-28 08:45:04 -04001362 /* The sequence operation has been encoded, cstate->datap set. */
1363 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001364
Andy Adamson557ce262009-08-28 08:45:04 -04001365 resp->opcnt = slot->sl_opcnt;
1366 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1367 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001368
1369 return status;
1370}
1371
Andy Adamson0733d212009-04-03 08:28:01 +03001372/*
1373 * Set the exchange_id flags returned by the server.
1374 */
1375static void
1376nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1377{
1378 /* pNFS is not supported */
1379 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1380
1381 /* Referrals are supported, Migration is not. */
1382 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1383
1384 /* set the wire flags to return to client. */
1385 clid->flags = new->cl_exchange_flags;
1386}
1387
Al Virob37ad282006-10-19 23:28:59 -07001388__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001389nfsd4_exchange_id(struct svc_rqst *rqstp,
1390 struct nfsd4_compound_state *cstate,
1391 struct nfsd4_exchange_id *exid)
1392{
Andy Adamson0733d212009-04-03 08:28:01 +03001393 struct nfs4_client *unconf, *conf, *new;
1394 int status;
1395 unsigned int strhashval;
1396 char dname[HEXDIR_LEN];
Jeff Layton363168b2009-08-14 12:57:56 -04001397 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001398 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001399 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamson0733d212009-04-03 08:28:01 +03001400
Jeff Layton363168b2009-08-14 12:57:56 -04001401 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001402 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001403 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001404 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001405 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001406
1407 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1408 return nfserr_inval;
1409
1410 /* Currently only support SP4_NONE */
1411 switch (exid->spa_how) {
1412 case SP4_NONE:
1413 break;
1414 case SP4_SSV:
J. Bruce Fields044bc1d2010-11-12 14:36:06 -05001415 return nfserr_serverfault;
Andy Adamson0733d212009-04-03 08:28:01 +03001416 default:
1417 BUG(); /* checked by xdr code */
1418 case SP4_MACH_CRED:
1419 return nfserr_serverfault; /* no excuse :-/ */
1420 }
1421
1422 status = nfs4_make_rec_clidname(dname, &exid->clname);
1423
1424 if (status)
1425 goto error;
1426
1427 strhashval = clientstr_hashval(dname);
1428
1429 nfs4_lock_state();
1430 status = nfs_ok;
1431
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001432 conf = find_confirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001433 if (conf) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001434 if (!clp_used_exchangeid(conf)) {
1435 status = nfserr_clid_inuse; /* XXX: ? */
1436 goto out;
1437 }
Andy Adamson0733d212009-04-03 08:28:01 +03001438 if (!same_verf(&verf, &conf->cl_verifier)) {
1439 /* 18.35.4 case 8 */
1440 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1441 status = nfserr_not_same;
1442 goto out;
1443 }
1444 /* Client reboot: destroy old state */
1445 expire_client(conf);
1446 goto out_new;
1447 }
1448 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1449 /* 18.35.4 case 9 */
1450 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1451 status = nfserr_perm;
1452 goto out;
1453 }
1454 expire_client(conf);
1455 goto out_new;
1456 }
Andy Adamson0733d212009-04-03 08:28:01 +03001457 /*
1458 * Set bit when the owner id and verifier map to an already
1459 * confirmed client id (18.35.3).
1460 */
1461 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1462
1463 /*
1464 * Falling into 18.35.4 case 2, possible router replay.
1465 * Leave confirmed record intact and return same result.
1466 */
1467 copy_verf(conf, &verf);
1468 new = conf;
1469 goto out_copy;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001470 }
1471
1472 /* 18.35.4 case 7 */
1473 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1474 status = nfserr_noent;
1475 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001476 }
1477
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001478 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001479 if (unconf) {
1480 /*
1481 * Possible retry or client restart. Per 18.35.4 case 4,
1482 * a new unconfirmed record should be generated regardless
1483 * of whether any properties have changed.
1484 */
1485 expire_client(unconf);
1486 }
1487
1488out_new:
1489 /* Normal case */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001490 new = create_client(exid->clname, dname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001491 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001492 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001493 goto out;
1494 }
1495
Andy Adamson0733d212009-04-03 08:28:01 +03001496 gen_clid(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001497 add_to_unconfirmed(new, strhashval);
1498out_copy:
1499 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1500 exid->clientid.cl_id = new->cl_clientid.cl_id;
1501
Andy Adamson38eb76a2009-04-03 08:28:32 +03001502 exid->seqid = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001503 nfsd4_set_ex_flags(new, exid);
1504
1505 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001506 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001507 status = nfs_ok;
1508
1509out:
1510 nfs4_unlock_state();
1511error:
1512 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1513 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001514}
1515
Benny Halevyb85d4c02009-04-03 08:28:08 +03001516static int
Andy Adamson88e588d2009-07-23 19:02:15 -04001517check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001518{
Andy Adamson88e588d2009-07-23 19:02:15 -04001519 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1520 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001521
1522 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001523 if (slot_inuse) {
1524 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001525 return nfserr_jukebox;
1526 else
1527 return nfserr_seq_misordered;
1528 }
1529 /* Normal */
Andy Adamson88e588d2009-07-23 19:02:15 -04001530 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001531 return nfs_ok;
1532 /* Replay */
Andy Adamson88e588d2009-07-23 19:02:15 -04001533 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001534 return nfserr_replay_cache;
1535 /* Wraparound */
Andy Adamson88e588d2009-07-23 19:02:15 -04001536 if (seqid == 1 && (slot_seqid + 1) == 0)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001537 return nfs_ok;
1538 /* Misordered replay or misordered new request */
1539 return nfserr_seq_misordered;
1540}
1541
Andy Adamson49557cc2009-07-23 19:02:16 -04001542/*
1543 * Cache the create session result into the create session single DRC
1544 * slot cache by saving the xdr structure. sl_seqid has been set.
1545 * Do this for solo or embedded create session operations.
1546 */
1547static void
1548nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1549 struct nfsd4_clid_slot *slot, int nfserr)
1550{
1551 slot->sl_status = nfserr;
1552 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1553}
1554
1555static __be32
1556nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1557 struct nfsd4_clid_slot *slot)
1558{
1559 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1560 return slot->sl_status;
1561}
1562
Mi Jinlong1b74c252011-07-14 14:50:17 +08001563#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1564 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1565 1 + /* MIN tag is length with zero, only length */ \
1566 3 + /* version, opcount, opcode */ \
1567 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1568 /* seqid, slotID, slotID, cache */ \
1569 4 ) * sizeof(__be32))
1570
1571#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1572 2 + /* verifier: AUTH_NULL, length 0 */\
1573 1 + /* status */ \
1574 1 + /* MIN tag is length with zero, only length */ \
1575 3 + /* opcount, opcode, opstatus*/ \
1576 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1577 /* seqid, slotID, slotID, slotID, status */ \
1578 5 ) * sizeof(__be32))
1579
1580static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1581{
1582 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1583 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1584}
1585
Andy Adamson069b6ad2009-04-03 08:27:58 +03001586__be32
1587nfsd4_create_session(struct svc_rqst *rqstp,
1588 struct nfsd4_compound_state *cstate,
1589 struct nfsd4_create_session *cr_ses)
1590{
Jeff Layton363168b2009-08-14 12:57:56 -04001591 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001592 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001593 struct nfsd4_session *new;
Andy Adamson49557cc2009-07-23 19:02:16 -04001594 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001595 bool confirm_me = false;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001596 int status = 0;
1597
Mi Jinlonga62573d2011-03-23 17:57:07 +08001598 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1599 return nfserr_inval;
1600
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001601 nfs4_lock_state();
1602 unconf = find_unconfirmed_client(&cr_ses->clientid);
1603 conf = find_confirmed_client(&cr_ses->clientid);
1604
1605 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001606 cs_slot = &conf->cl_cs_slot;
1607 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001608 if (status == nfserr_replay_cache) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001609 dprintk("Got a create_session replay! seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001610 cs_slot->sl_seqid);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001611 /* Return the cached reply status */
Andy Adamson49557cc2009-07-23 19:02:16 -04001612 status = nfsd4_replay_create_session(cr_ses, cs_slot);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001613 goto out;
Andy Adamson49557cc2009-07-23 19:02:16 -04001614 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001615 status = nfserr_seq_misordered;
1616 dprintk("Sequence misordered!\n");
1617 dprintk("Expected seqid= %d but got seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001618 cs_slot->sl_seqid, cr_ses->seqid);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001619 goto out;
1620 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001621 } else if (unconf) {
1622 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001623 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001624 status = nfserr_clid_inuse;
1625 goto out;
1626 }
1627
Andy Adamson49557cc2009-07-23 19:02:16 -04001628 cs_slot = &unconf->cl_cs_slot;
1629 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001630 if (status) {
1631 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001632 status = nfserr_seq_misordered;
J. Bruce Fieldscd5b8142010-10-02 17:03:35 -04001633 goto out;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001634 }
1635
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001636 confirm_me = true;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001637 conf = unconf;
1638 } else {
1639 status = nfserr_stale_clientid;
1640 goto out;
1641 }
1642
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001643 /*
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001644 * XXX: we should probably set this at creation time, and check
1645 * for consistent minorversion use throughout:
1646 */
1647 conf->cl_minorversion = 1;
1648 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001649 * We do not support RDMA or persistent sessions
1650 */
1651 cr_ses->flags &= ~SESSION4_PERSIST;
1652 cr_ses->flags &= ~SESSION4_RDMA;
1653
Mi Jinlong1b74c252011-07-14 14:50:17 +08001654 status = nfserr_toosmall;
1655 if (check_forechannel_attrs(cr_ses->fore_channel))
1656 goto out;
1657
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001658 status = nfserr_jukebox;
1659 new = alloc_init_session(rqstp, conf, cr_ses);
1660 if (!new)
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001661 goto out;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001662 status = nfs_ok;
1663 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001664 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001665 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1666 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001667 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001668 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001669
Andy Adamson49557cc2009-07-23 19:02:16 -04001670 /* cache solo and embedded create sessions under the state lock */
1671 nfsd4_cache_create_session(cr_ses, cs_slot, status);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001672 if (confirm_me)
1673 move_to_confirmed(conf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001674out:
1675 nfs4_unlock_state();
1676 dprintk("%s returns %d\n", __func__, ntohl(status));
1677 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001678}
1679
J. Bruce Fields57716352010-04-21 12:27:19 -04001680static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1681{
1682 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1683 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1684
1685 return argp->opcnt == resp->opcnt;
1686}
1687
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001688static __be32 nfsd4_map_bcts_dir(u32 *dir)
1689{
1690 switch (*dir) {
1691 case NFS4_CDFC4_FORE:
1692 case NFS4_CDFC4_BACK:
1693 return nfs_ok;
1694 case NFS4_CDFC4_FORE_OR_BOTH:
1695 case NFS4_CDFC4_BACK_OR_BOTH:
1696 *dir = NFS4_CDFC4_BOTH;
1697 return nfs_ok;
1698 };
1699 return nfserr_inval;
1700}
1701
1702__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1703 struct nfsd4_compound_state *cstate,
1704 struct nfsd4_bind_conn_to_session *bcts)
1705{
1706 __be32 status;
1707
1708 if (!nfsd4_last_compound_op(rqstp))
1709 return nfserr_not_only_op;
1710 spin_lock(&client_lock);
1711 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1712 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1713 * client_lock iself: */
1714 if (cstate->session) {
1715 nfsd4_get_session(cstate->session);
1716 atomic_inc(&cstate->session->se_client->cl_refcount);
1717 }
1718 spin_unlock(&client_lock);
1719 if (!cstate->session)
1720 return nfserr_badsession;
1721
1722 status = nfsd4_map_bcts_dir(&bcts->dir);
Bryan Schumaker1db2b9d2011-04-27 15:47:15 -04001723 if (!status)
1724 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1725 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001726}
1727
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001728static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1729{
1730 if (!session)
1731 return 0;
1732 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1733}
1734
Andy Adamson069b6ad2009-04-03 08:27:58 +03001735__be32
1736nfsd4_destroy_session(struct svc_rqst *r,
1737 struct nfsd4_compound_state *cstate,
1738 struct nfsd4_destroy_session *sessionid)
1739{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001740 struct nfsd4_session *ses;
1741 u32 status = nfserr_badsession;
1742
1743 /* Notes:
1744 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1745 * - Should we return nfserr_back_chan_busy if waiting for
1746 * callbacks on to-be-destroyed session?
1747 * - Do we need to clear any callback info from previous session?
1748 */
1749
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001750 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001751 if (!nfsd4_last_compound_op(r))
1752 return nfserr_not_only_op;
1753 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001754 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001755 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001756 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1757 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001758 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001759 goto out;
1760 }
1761
1762 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001763 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001764
Benny Halevyab707e152010-05-12 00:14:06 +03001765 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001766 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001767 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001768
1769 nfsd4_del_conns(ses);
1770
Benny Halevye10e0cf2009-04-03 08:28:38 +03001771 nfsd4_put_session(ses);
1772 status = nfs_ok;
1773out:
1774 dprintk("%s returns %d\n", __func__, ntohl(status));
1775 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001776}
1777
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001778static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001779{
1780 struct nfsd4_conn *c;
1781
1782 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001783 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001784 return c;
1785 }
1786 }
1787 return NULL;
1788}
1789
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001790static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001791{
1792 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001793 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001794 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001795
1796 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001797 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001798 if (c) {
1799 spin_unlock(&clp->cl_lock);
1800 free_conn(new);
1801 return;
1802 }
1803 __nfsd4_hash_conn(new, ses);
1804 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001805 ret = nfsd4_register_conn(new);
1806 if (ret)
1807 /* oops; xprt is already down: */
1808 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001809 return;
1810}
1811
Mi Jinlong868b89c2011-04-27 09:09:58 +08001812static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1813{
1814 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1815
1816 return args->opcnt > session->se_fchannel.maxops;
1817}
1818
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001819static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1820 struct nfsd4_session *session)
1821{
1822 struct xdr_buf *xb = &rqstp->rq_arg;
1823
1824 return xb->len > session->se_fchannel.maxreq_sz;
1825}
1826
Andy Adamson069b6ad2009-04-03 08:27:58 +03001827__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001828nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001829 struct nfsd4_compound_state *cstate,
1830 struct nfsd4_sequence *seq)
1831{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001832 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001833 struct nfsd4_session *session;
1834 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001835 struct nfsd4_conn *conn;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001836 int status;
1837
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001838 if (resp->opcnt != 1)
1839 return nfserr_sequence_pos;
1840
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001841 /*
1842 * Will be either used or freed by nfsd4_sequence_check_conn
1843 * below.
1844 */
1845 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1846 if (!conn)
1847 return nfserr_jukebox;
1848
Benny Halevy9089f1b2010-05-12 00:12:26 +03001849 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001850 status = nfserr_badsession;
1851 session = find_in_sessionid_hashtbl(&seq->sessionid);
1852 if (!session)
1853 goto out;
1854
Mi Jinlong868b89c2011-04-27 09:09:58 +08001855 status = nfserr_too_many_ops;
1856 if (nfsd4_session_too_many_ops(rqstp, session))
1857 goto out;
1858
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001859 status = nfserr_req_too_big;
1860 if (nfsd4_request_too_big(rqstp, session))
1861 goto out;
1862
Benny Halevyb85d4c02009-04-03 08:28:08 +03001863 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03001864 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001865 goto out;
1866
Andy Adamson557ce262009-08-28 08:45:04 -04001867 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03001868 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1869
Andy Adamsona8dfdae2009-08-28 08:45:02 -04001870 /* We do not negotiate the number of slots yet, so set the
1871 * maxslots to the session maxreqs which is used to encode
1872 * sr_highest_slotid and the sr_target_slot id to maxslots */
1873 seq->maxslots = session->se_fchannel.maxreqs;
1874
Andy Adamson88e588d2009-07-23 19:02:15 -04001875 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001876 if (status == nfserr_replay_cache) {
1877 cstate->slot = slot;
1878 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001879 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04001880 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03001881 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03001882 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001883 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001884 }
1885 if (status)
1886 goto out;
1887
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001888 nfsd4_sequence_check_conn(conn, session);
1889 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001890
Benny Halevyb85d4c02009-04-03 08:28:08 +03001891 /* Success! bump slot seqid */
1892 slot->sl_inuse = true;
1893 slot->sl_seqid = seq->seqid;
Andy Adamson557ce262009-08-28 08:45:04 -04001894 slot->sl_cachethis = seq->cachethis;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001895
1896 cstate->slot = slot;
1897 cstate->session = session;
1898
Benny Halevyb85d4c02009-04-03 08:28:08 +03001899out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04001900 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001901 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001902 struct nfs4_client *clp = session->se_client;
1903
Benny Halevy36acb662010-05-12 00:13:04 +03001904 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001905 atomic_inc(&clp->cl_refcount);
1906 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1907 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001908 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001909 kfree(conn);
Benny Halevy36acb662010-05-12 00:13:04 +03001910 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001911 dprintk("%s: return %d\n", __func__, ntohl(status));
1912 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001913}
1914
1915__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001916nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1917{
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001918 int status = 0;
1919
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001920 if (rc->rca_one_fs) {
1921 if (!cstate->current_fh.fh_dentry)
1922 return nfserr_nofilehandle;
1923 /*
1924 * We don't take advantage of the rca_one_fs case.
1925 * That's OK, it's optional, we can safely ignore it.
1926 */
1927 return nfs_ok;
1928 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001929
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001930 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001931 status = nfserr_complete_already;
1932 if (cstate->session->se_client->cl_firststate)
1933 goto out;
1934
1935 status = nfserr_stale_clientid;
1936 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001937 /*
1938 * The following error isn't really legal.
1939 * But we only get here if the client just explicitly
1940 * destroyed the client. Surely it no longer cares what
1941 * error it gets back on an operation for the dead
1942 * client.
1943 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001944 goto out;
1945
1946 status = nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001947 nfsd4_create_clid_dir(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001948out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001949 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001950 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001951}
1952
1953__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001954nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1955 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 struct xdr_netobj clname = {
1958 .len = setclid->se_namelen,
1959 .data = setclid->se_name,
1960 };
1961 nfs4_verifier clverifier = setclid->se_verf;
1962 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07001963 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07001964 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -07001965 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -07001968 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
NeilBrowna55370a2005-06-23 22:03:52 -07001970 status = nfs4_make_rec_clidname(dname, &clname);
1971 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07001972 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
1975 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1976 * We get here on a DRC miss.
1977 */
1978
NeilBrowna55370a2005-06-23 22:03:52 -07001979 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 nfs4_lock_state();
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001982 conf = find_confirmed_client_by_str(dname, strhashval);
NeilBrown28ce6052005-06-23 22:03:56 -07001983 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001984 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001986 if (clp_used_exchangeid(conf))
1987 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04001988 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04001989 char addr_str[INET6_ADDRSTRLEN];
1990 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1991 sizeof(addr_str));
1992 dprintk("NFSD: setclientid: string in use by client "
1993 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto out;
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001997 /*
1998 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1999 * has a description of SETCLIENTID request processing consisting
2000 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2001 */
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002002 unconf = find_unconfirmed_client_by_str(dname, strhashval);
J. Bruce Fields3e772462011-08-10 19:07:33 -04002003 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002005 /*
2006 * RFC 3530 14.2.33 CASE 4:
2007 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 */
2009 if (unconf)
2010 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002011 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002012 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002015 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002017 * RFC 3530 14.2.33 CASE 1:
2018 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07002020 if (unconf) {
2021 /* Note this is removing unconfirmed {*x***},
2022 * which is stronger than RFC recommended {vxc**}.
2023 * This has the advantage that there is at most
2024 * one {*x***} in either list at any time.
2025 */
2026 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002028 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002029 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 } else if (!unconf) {
2033 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002034 * RFC 3530 14.2.33 CASE 2:
2035 * probable client reboot; state will be removed if
2036 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002038 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002039 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05002042 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002043 /*
2044 * RFC 3530 14.2.33 CASE 3:
2045 * probable client reboot; state will be removed if
2046 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 */
2048 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002049 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002050 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002054 /*
2055 * XXX: we should probably set this at creation time, and check
2056 * for consistent minorversion use throughout:
2057 */
2058 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002059 gen_callback(new, setclid, rqstp);
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04002060 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2062 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2063 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2064 status = nfs_ok;
2065out:
2066 nfs4_unlock_state();
2067 return status;
2068}
2069
2070
2071/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002072 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2073 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2074 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 */
Al Virob37ad282006-10-19 23:28:59 -07002076__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002077nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2078 struct nfsd4_compound_state *cstate,
2079 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Jeff Layton363168b2009-08-14 12:57:56 -04002081 struct sockaddr *sa = svc_addr(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -07002082 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2084 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002085 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 if (STALE_CLIENTID(clid))
2088 return nfserr_stale_clientid;
2089 /*
2090 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2091 * We get here on a DRC miss.
2092 */
2093
2094 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002095
2096 conf = find_confirmed_client(clid);
2097 unconf = find_unconfirmed_client(clid);
2098
2099 status = nfserr_clid_inuse;
Jeff Layton363168b2009-08-14 12:57:56 -04002100 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002101 goto out;
Jeff Layton363168b2009-08-14 12:57:56 -04002102 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002103 goto out;
2104
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002105 /*
2106 * section 14.2.34 of RFC 3530 has a description of
2107 * SETCLIENTID_CONFIRM request processing consisting
2108 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2109 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -05002110 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002111 /*
2112 * RFC 3530 14.2.34 CASE 1:
2113 * callback update
2114 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002115 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 status = nfserr_clid_inuse;
2117 else {
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002118 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2119 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002120 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05002124 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002125 /*
2126 * RFC 3530 14.2.34 CASE 2:
2127 * probable retransmitted request; play it safe and
2128 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07002129 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002130 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07002132 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07002134 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002135 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002136 /*
2137 * RFC 3530 14.2.34 CASE 3:
2138 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07002139 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002140 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 status = nfserr_clid_inuse;
2142 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07002143 unsigned int hash =
2144 clientstr_hashval(unconf->cl_recdir);
2145 conf = find_confirmed_client_by_str(unconf->cl_recdir,
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002146 hash);
NeilBrown1a69c172005-06-23 22:04:08 -07002147 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07002148 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002149 expire_client(conf);
2150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07002152 conf = unconf;
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002153 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002154 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002156 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2157 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07002158 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002159 /*
2160 * RFC 3530 14.2.34 CASE 4:
2161 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07002162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07002164 } else {
NeilBrown08e89872005-06-23 22:04:11 -07002165 /* check that we have hit one of the cases...*/
2166 status = nfserr_clid_inuse;
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 nfs4_unlock_state();
2170 return status;
2171}
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173/* OPEN Share state helper functions */
2174static inline struct nfs4_file *
2175alloc_init_file(struct inode *ino)
2176{
2177 struct nfs4_file *fp;
2178 unsigned int hashval = file_hashval(ino);
2179
NeilBrowne60d4392005-06-23 22:03:01 -07002180 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2181 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002182 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07002184 INIT_LIST_HEAD(&fp->fi_stateids);
2185 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 fp->fi_inode = igrab(ino);
2187 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -07002188 fp->fi_had_conflict = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002189 fp->fi_lease = NULL;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002190 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2191 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Pavel Emelyanov47cee542010-05-17 20:00:37 +04002192 spin_lock(&recall_lock);
2193 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2194 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return fp;
2196 }
2197 return NULL;
2198}
2199
2200static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002201nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002202{
NeilBrowne60d4392005-06-23 22:03:01 -07002203 if (*slab == NULL)
2204 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002205 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002206 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002207}
2208
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002209void
NeilBrowne60d4392005-06-23 22:03:01 -07002210nfsd4_free_slabs(void)
2211{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002212 nfsd4_free_slab(&openowner_slab);
2213 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002214 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002215 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002216 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002217}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219static int
2220nfsd4_init_slabs(void)
2221{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002222 openowner_slab = kmem_cache_create("nfsd4_openowners",
2223 sizeof(struct nfs4_openowner), 0, 0, NULL);
2224 if (openowner_slab == NULL)
2225 goto out_nomem;
2226 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2227 sizeof(struct nfs4_openowner), 0, 0, NULL);
2228 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002229 goto out_nomem;
2230 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002231 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002232 if (file_slab == NULL)
2233 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002234 stateid_slab = kmem_cache_create("nfsd4_stateids",
Paul Mundt20c2df82007-07-20 10:11:58 +09002235 sizeof(struct nfs4_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002236 if (stateid_slab == NULL)
2237 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002238 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002239 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002240 if (deleg_slab == NULL)
2241 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002243out_nomem:
2244 nfsd4_free_slabs();
2245 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2246 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002249void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002251 kfree(oo->oo_owner.so_owner.data);
2252 kmem_cache_free(openowner_slab, oo);
2253}
2254
2255void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2256{
2257 kfree(lo->lo_owner.so_owner.data);
2258 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002261static void init_nfs4_replay(struct nfs4_replay *rp)
2262{
2263 rp->rp_status = nfserr_serverfault;
2264 rp->rp_buflen = 0;
2265 rp->rp_buf = rp->rp_ibuf;
2266}
2267
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002268static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 struct nfs4_stateowner *sop;
2271
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002272 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002273 if (!sop)
2274 return NULL;
2275
2276 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2277 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002278 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002279 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002281 sop->so_owner.len = owner->len;
2282
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002283 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002284 sop->so_id = current_ownerid++;
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002285 sop->so_client = clp;
2286 init_nfs4_replay(&sop->so_replay);
2287 return sop;
2288}
2289
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002290static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002291{
2292 unsigned int idhashval;
2293
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002294 idhashval = open_ownerid_hashval(oo->oo_owner.so_id);
2295 list_add(&oo->oo_owner.so_idhash, &open_ownerid_hashtbl[idhashval]);
2296 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2297 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002300static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002302 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002304 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2305 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002307 oo->oo_owner.so_is_open_owner = 1;
2308 oo->oo_owner.so_seqid = open->op_seqid;
2309 oo->oo_confirmed = 0;
2310 oo->oo_time = 0;
2311 INIT_LIST_HEAD(&oo->oo_close_lru);
2312 hash_openowner(oo, clp, strhashval);
2313 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316static inline void
2317init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002318 struct nfs4_openowner *oo = open->op_openowner;
2319 unsigned int hashval = stateid_hashval(oo->oo_owner.so_id, fp->fi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 INIT_LIST_HEAD(&stp->st_hash);
NeilBrownea1da632005-06-23 22:04:17 -07002322 INIT_LIST_HEAD(&stp->st_perstateowner);
2323 INIT_LIST_HEAD(&stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 INIT_LIST_HEAD(&stp->st_perfile);
2325 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002326 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002327 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fields5fa0bbb2011-08-31 15:25:46 -04002328 stp->st_type = NFS4_OPEN_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002329 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002330 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 stp->st_file = fp;
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04002332 stp->st_stateid.si_boot = boot_time;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002333 stp->st_stateid.si_stateownerid = oo->oo_owner.so_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 stp->st_stateid.si_fileid = fp->fi_id;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04002335 /* note will be incremented before first return to client: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 stp->st_stateid.si_generation = 0;
2337 stp->st_access_bmap = 0;
2338 stp->st_deny_bmap = 0;
Andy Adamson84459a12009-04-03 08:28:56 +03002339 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2340 &stp->st_access_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07002342 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
2345static void
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002346move_to_close_lru(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002348 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002350 list_move_tail(&oo->oo_close_lru, &close_lru);
2351 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002355same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2356 clientid_t *clid)
2357{
2358 return (sop->so_owner.len == owner->len) &&
2359 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2360 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002363static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2365{
2366 struct nfs4_stateowner *so = NULL;
2367
J. Bruce Fields506f2752011-08-11 18:50:18 -04002368 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002369 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002370 return container_of(so, struct nfs4_openowner, oo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
2372 return NULL;
2373}
2374
2375/* search file_hashtbl[] for file */
2376static struct nfs4_file *
2377find_file(struct inode *ino)
2378{
2379 unsigned int hashval = file_hashval(ino);
2380 struct nfs4_file *fp;
2381
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002382 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002384 if (fp->fi_inode == ino) {
2385 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002386 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002390 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return NULL;
2392}
2393
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002394static inline int access_valid(u32 x, u32 minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002395{
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002396 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002397 return 0;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002398 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2399 return 0;
2400 x &= ~NFS4_SHARE_ACCESS_MASK;
2401 if (minorversion && x) {
2402 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2403 return 0;
2404 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2405 return 0;
2406 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2407 }
2408 if (x)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002409 return 0;
2410 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002411}
2412
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002413static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002414{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002415 /* Note: unlike access bits, deny bits may be zero. */
2416 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002417}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002419/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 * Called to check deny when READ with all zero stateid or
2421 * WRITE with all zero or all one stateid
2422 */
Al Virob37ad282006-10-19 23:28:59 -07002423static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2425{
2426 struct inode *ino = current_fh->fh_dentry->d_inode;
2427 struct nfs4_file *fp;
2428 struct nfs4_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002429 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 dprintk("NFSD: nfs4_share_conflict\n");
2432
2433 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002434 if (!fp)
2435 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002436 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002438 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2439 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2440 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
NeilBrown13cd2182005-06-23 22:03:10 -07002443 ret = nfs_ok;
2444out:
2445 put_nfs4_file(fp);
2446 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002449static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* We're assuming the state code never drops its reference
2452 * without first removing the lease. Since we're in this lease
2453 * callback (and since the lease code is serialized by the kernel
2454 * lock) we know the server hasn't removed the lease yet, we know
2455 * it's safe to take a reference: */
2456 atomic_inc(&dp->dl_count);
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Arnd Bergmann460781b2010-11-17 16:26:56 +01002460 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 dp->dl_time = get_seconds();
2462
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002463 nfsd4_cb_recall(dp);
2464}
2465
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002466/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002467static void nfsd_break_deleg_cb(struct file_lock *fl)
2468{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002469 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2470 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002471
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002472 BUG_ON(!fp);
2473 /* We assume break_lease is only called once per lease: */
2474 BUG_ON(fp->fi_had_conflict);
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002475 /*
2476 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002477 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002478 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002479 */
2480 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002482 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002483 fp->fi_had_conflict = true;
2484 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2485 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002486 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489static
2490int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2491{
2492 if (arg & F_UNLCK)
2493 return lease_modify(onlist, arg);
2494 else
2495 return -EAGAIN;
2496}
2497
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002498static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002499 .lm_break = nfsd_break_deleg_cb,
2500 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501};
2502
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002503static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2504{
2505 if (nfsd4_has_session(cstate))
2506 return nfs_ok;
2507 if (seqid == so->so_seqid - 1)
2508 return nfserr_replay_me;
2509 if (seqid == so->so_seqid)
2510 return nfs_ok;
2511 return nfserr_bad_seqid;
2512}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Al Virob37ad282006-10-19 23:28:59 -07002514__be32
Andy Adamson66689582009-04-03 08:28:45 +03002515nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2516 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 clientid_t *clientid = &open->op_clientid;
2519 struct nfs4_client *clp = NULL;
2520 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002521 struct nfs4_openowner *oo = NULL;
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002522 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002525 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 if (STALE_CLIENTID(&open->op_clientid))
2528 return nfserr_stale_clientid;
2529
J. Bruce Fields506f2752011-08-11 18:50:18 -04002530 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002531 oo = find_openstateowner_str(strhashval, open);
2532 open->op_openowner = oo;
2533 if (!oo) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002534 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 clp = find_confirmed_client(clientid);
2536 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002537 return nfserr_expired;
2538 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002540 if (!oo->oo_confirmed) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002541 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002542 clp = oo->oo_owner.so_client;
2543 release_openowner(oo);
2544 open->op_openowner = NULL;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002545 goto renew;
2546 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002547 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002548 if (status)
2549 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550renew:
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002551 if (open->op_openowner == NULL) {
2552 oo = alloc_init_open_stateowner(strhashval, clp, open);
2553 if (oo == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002554 return nfserr_jukebox;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002555 open->op_openowner = oo;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002556 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002557 list_del_init(&oo->oo_close_lru);
2558 renew_client(oo->oo_owner.so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002559 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Al Virob37ad282006-10-19 23:28:59 -07002562static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002563nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2564{
2565 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2566 return nfserr_openmode;
2567 else
2568 return nfs_ok;
2569}
2570
NeilBrown52f4fb42005-06-23 22:02:49 -07002571static struct nfs4_delegation *
2572find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2573{
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002574 struct nfs4_delegation *dp;
NeilBrown52f4fb42005-06-23 22:02:49 -07002575
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002576 spin_lock(&recall_lock);
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002577 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2578 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
2579 spin_unlock(&recall_lock);
2580 return dp;
2581 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002582 spin_unlock(&recall_lock);
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002583 return NULL;
NeilBrown52f4fb42005-06-23 22:02:49 -07002584}
2585
Daniel Mackc47d8322011-05-16 16:38:14 +02002586static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002587{
2588 share_access &= ~NFS4_SHARE_WANT_MASK;
2589
2590 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2591}
2592
Al Virob37ad282006-10-19 23:28:59 -07002593static __be32
NeilBrown567d9822005-06-23 22:02:53 -07002594nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2595 struct nfs4_delegation **dp)
2596{
2597 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002598 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002599
2600 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2601 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002602 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002603 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002604 status = nfs4_check_delegmode(*dp, flags);
2605 if (status)
2606 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002607out:
2608 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2609 return nfs_ok;
2610 if (status)
2611 return status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002612 open->op_openowner->oo_confirmed = 1;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002613 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002614}
2615
Al Virob37ad282006-10-19 23:28:59 -07002616static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2618{
2619 struct nfs4_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002620 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
NeilBrown8beefa22005-06-23 22:03:08 -07002622 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 /* ignore lock owners */
2624 if (local->st_stateowner->so_is_open_owner == 0)
2625 continue;
2626 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002627 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 *stpp = local;
2629 /* check for conflicting share reservations */
2630 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002631 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002633 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
NeilBrown5ac049a2005-06-23 22:03:03 -07002636static inline struct nfs4_stateid *
2637nfs4_alloc_stateid(void)
2638{
2639 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2640}
2641
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002642static inline int nfs4_access_to_access(u32 nfs4_access)
2643{
2644 int flags = 0;
2645
2646 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2647 flags |= NFSD_MAY_READ;
2648 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2649 flags |= NFSD_MAY_WRITE;
2650 return flags;
2651}
2652
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002653static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2654 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002655{
2656 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002657 int oflag = nfs4_access_to_omode(open->op_share_access);
2658 int access = nfs4_access_to_access(open->op_share_access);
2659
2660 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2661 * allowing it to break the lease and return EAGAIN leaves the
2662 * client unable to make progress in returning the delegation */
2663 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2664 access |= NFSD_MAY_NOT_BREAK_LEASE;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002665
2666 if (!fp->fi_fds[oflag]) {
2667 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2668 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002669 if (status)
2670 return status;
2671 }
2672 nfs4_file_get_access(fp, oflag);
2673
2674 return nfs_ok;
2675}
2676
Al Virob37ad282006-10-19 23:28:59 -07002677static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002679 struct nfs4_file *fp, struct svc_fh *cur_fh,
2680 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
2682 struct nfs4_stateid *stp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002683 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
NeilBrown5ac049a2005-06-23 22:03:03 -07002685 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (stp == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002687 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002689 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002690 if (status) {
2691 kmem_cache_free(stateid_slab, stp);
2692 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 *stpp = stp;
2695 return 0;
2696}
2697
Al Virob37ad282006-10-19 23:28:59 -07002698static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2700 struct nfsd4_open *open)
2701{
2702 struct iattr iattr = {
2703 .ia_valid = ATTR_SIZE,
2704 .ia_size = 0,
2705 };
2706 if (!open->op_truncate)
2707 return 0;
2708 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002709 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2711}
2712
Al Virob37ad282006-10-19 23:28:59 -07002713static __be32
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002714nfs4_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 -07002715{
J. Bruce Fields7d947842010-08-20 18:09:31 -04002716 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2717 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002718 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
J. Bruce Fields7d947842010-08-20 18:09:31 -04002720 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002721 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002722 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002723 if (status)
2724 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 status = nfsd4_truncate(rqstp, cur_fh, open);
2727 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002728 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002729 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002730 nfs4_file_put_access(fp, oflag);
2731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return status;
2733 }
2734 /* remember the open */
J. Bruce Fields24a01112010-05-18 20:01:35 -04002735 __set_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002736 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 return nfs_ok;
2739}
2740
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742static void
NeilBrown37515172005-07-07 17:59:16 -07002743nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002745 open->op_openowner->oo_confirmed = 1;
2746 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002749/* Should we give out recallable state?: */
2750static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2751{
2752 if (clp->cl_cb_state == NFSD4_CB_UP)
2753 return true;
2754 /*
2755 * In the sessions case, since we don't have to establish a
2756 * separate connection for callbacks, we assume it's OK
2757 * until we hear otherwise:
2758 */
2759 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2760}
2761
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002762static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2763{
2764 struct file_lock *fl;
2765
2766 fl = locks_alloc_lock();
2767 if (!fl)
2768 return NULL;
2769 locks_init_lock(fl);
2770 fl->fl_lmops = &nfsd_lease_mng_ops;
2771 fl->fl_flags = FL_LEASE;
2772 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2773 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002774 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002775 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002776 return fl;
2777}
2778
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002779static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2780{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002781 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002782 struct file_lock *fl;
2783 int status;
2784
2785 fl = nfs4_alloc_init_lease(dp, flag);
2786 if (!fl)
2787 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002788 fl->fl_file = find_readable_file(fp);
2789 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2790 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002791 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002792 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002793 locks_free_lock(fl);
2794 return -ENOMEM;
2795 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002796 fp->fi_lease = fl;
2797 fp->fi_deleg_file = fl->fl_file;
2798 get_file(fp->fi_deleg_file);
2799 atomic_set(&fp->fi_delegees, 1);
2800 list_add(&dp->dl_perfile, &fp->fi_delegations);
2801 return 0;
2802}
2803
2804static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2805{
2806 struct nfs4_file *fp = dp->dl_file;
2807
2808 if (!fp->fi_lease)
2809 return nfs4_setlease(dp, flag);
2810 spin_lock(&recall_lock);
2811 if (fp->fi_had_conflict) {
2812 spin_unlock(&recall_lock);
2813 return -EAGAIN;
2814 }
2815 atomic_inc(&fp->fi_delegees);
2816 list_add(&dp->dl_perfile, &fp->fi_delegations);
2817 spin_unlock(&recall_lock);
2818 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002819 return 0;
2820}
2821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822/*
2823 * Attempt to hand out a delegation.
2824 */
2825static void
2826nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2827{
2828 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002829 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002830 int cb_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 int status, flag = 0;
2832
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002833 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002835 open->op_recall = 0;
2836 switch (open->op_claim_type) {
2837 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002838 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002839 open->op_recall = 1;
2840 flag = open->op_delegate_type;
2841 if (flag == NFS4_OPEN_DELEGATE_NONE)
2842 goto out;
2843 break;
2844 case NFS4_OPEN_CLAIM_NULL:
2845 /* Let's not give out any delegations till everyone's
2846 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002847 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002848 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002849 if (!cb_up || !oo->oo_confirmed)
NeilBrown7b190fe2005-06-23 22:03:23 -07002850 goto out;
2851 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2852 flag = NFS4_OPEN_DELEGATE_WRITE;
2853 else
2854 flag = NFS4_OPEN_DELEGATE_READ;
2855 break;
2856 default:
2857 goto out;
2858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002860 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002861 if (dp == NULL)
2862 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002863 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002864 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002865 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
2867 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2868
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002869 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2870 STATEID_VAL(&dp->dl_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871out:
NeilBrown7b190fe2005-06-23 22:03:23 -07002872 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2873 && flag == NFS4_OPEN_DELEGATE_NONE
2874 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002875 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 open->op_delegate_type = flag;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002877 return;
2878out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002879 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002880out_no_deleg:
2881 flag = NFS4_OPEN_DELEGATE_NONE;
2882 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
2885/*
2886 * called with nfs4_lock_state() held.
2887 */
Al Virob37ad282006-10-19 23:28:59 -07002888__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2890{
Andy Adamson66689582009-04-03 08:28:45 +03002891 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 struct nfs4_file *fp = NULL;
2893 struct inode *ino = current_fh->fh_dentry->d_inode;
2894 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07002895 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002896 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 status = nfserr_inval;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002899 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002900 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 goto out;
2902 /*
2903 * Lookup file; if found, lookup stateid and check open request,
2904 * and check for delegations in the process of being recalled.
2905 * If not found, create the nfs4_file struct
2906 */
2907 fp = find_file(ino);
2908 if (fp) {
2909 if ((status = nfs4_check_open(fp, open, &stp)))
2910 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002911 status = nfs4_check_deleg(fp, open, &dp);
2912 if (status)
2913 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07002915 status = nfserr_bad_stateid;
2916 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2917 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04002918 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 fp = alloc_init_file(ino);
2920 if (fp == NULL)
2921 goto out;
2922 }
2923
2924 /*
2925 * OPEN the file, or upgrade an existing OPEN.
2926 * If truncate fails, the OPEN fails.
2927 */
2928 if (stp) {
2929 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002930 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (status)
2932 goto out;
2933 } else {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002934 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07002935 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 goto out;
2937 init_stateid(stp, fp, open);
2938 status = nfsd4_truncate(rqstp, current_fh, open);
2939 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05002940 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 goto out;
2942 }
2943 }
J. Bruce Fields73997dc2011-08-31 15:47:21 -04002944 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2946
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002947 if (nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002948 open->op_openowner->oo_confirmed = 1;
Andy Adamson66689582009-04-03 08:28:45 +03002949
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 /*
2951 * Attempt to hand out a delegation. No error return, because the
2952 * OPEN succeeds even if we fail.
2953 */
2954 nfs4_open_delegation(current_fh, open, stp);
2955
2956 status = nfs_ok;
2957
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002958 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2959 STATEID_VAL(&stp->st_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960out:
NeilBrown13cd2182005-06-23 22:03:10 -07002961 if (fp)
2962 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07002963 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2964 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 /*
2966 * To finish the open response, we just need to set the rflags.
2967 */
2968 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002969 if (!open->op_openowner->oo_confirmed &&
Andy Adamson66689582009-04-03 08:28:45 +03002970 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2972
2973 return status;
2974}
2975
Al Virob37ad282006-10-19 23:28:59 -07002976__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002977nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2978 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
2980 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07002981 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 nfs4_lock_state();
2984 dprintk("process_renew(%08x/%08x): starting\n",
2985 clid->cl_boot, clid->cl_id);
2986 status = nfserr_stale_clientid;
2987 if (STALE_CLIENTID(clid))
2988 goto out;
2989 clp = find_confirmed_client(clid);
2990 status = nfserr_expired;
2991 if (clp == NULL) {
2992 /* We assume the client took too long to RENEW. */
2993 dprintk("nfsd4_renew: clientid not found!\n");
2994 goto out;
2995 }
2996 renew_client(clp);
2997 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07002998 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04002999 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 goto out;
3001 status = nfs_ok;
3002out:
3003 nfs4_unlock_state();
3004 return status;
3005}
3006
Daniel Mackc47d8322011-05-16 16:38:14 +02003007static struct lock_manager nfsd4_manager = {
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003008};
3009
NeilBrowna76b4312005-06-23 22:04:01 -07003010static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003011nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07003012{
3013 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07003014 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003015 locks_end_grace(&nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003016 /*
3017 * Now that every NFSv4 client has had the chance to recover and
3018 * to see the (possibly new, possibly shorter) lease time, we
3019 * can safely set the next grace time to the current lease time:
3020 */
3021 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003022}
3023
NeilBrownfd39ca92005-06-23 22:04:03 -07003024static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025nfs4_laundromat(void)
3026{
3027 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003028 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 struct nfs4_delegation *dp;
3030 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003031 time_t cutoff = get_seconds() - nfsd4_lease;
3032 time_t t, clientid_val = nfsd4_lease;
3033 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 nfs4_lock_state();
3036
3037 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003038 if (locks_in_grace())
3039 nfsd4_end_grace();
Benny Halevy36acb662010-05-12 00:13:04 +03003040 INIT_LIST_HEAD(&reaplist);
3041 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 list_for_each_safe(pos, next, &client_lru) {
3043 clp = list_entry(pos, struct nfs4_client, cl_lru);
3044 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3045 t = clp->cl_time - cutoff;
3046 if (clientid_val > t)
3047 clientid_val = t;
3048 break;
3049 }
Benny Halevyd7682982010-05-12 00:13:54 +03003050 if (atomic_read(&clp->cl_refcount)) {
3051 dprintk("NFSD: client in use (clientid %08x)\n",
3052 clp->cl_clientid.cl_id);
3053 continue;
3054 }
3055 unhash_client_locked(clp);
3056 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003057 }
3058 spin_unlock(&client_lock);
3059 list_for_each_safe(pos, next, &reaplist) {
3060 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 dprintk("NFSD: purging unused client (clientid %08x)\n",
3062 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07003063 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 expire_client(clp);
3065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 spin_lock(&recall_lock);
3067 list_for_each_safe(pos, next, &del_recall_lru) {
3068 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3069 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3070 u = dp->dl_time - cutoff;
3071 if (test_val > u)
3072 test_val = u;
3073 break;
3074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 list_move(&dp->dl_recall_lru, &reaplist);
3076 }
3077 spin_unlock(&recall_lock);
3078 list_for_each_safe(pos, next, &reaplist) {
3079 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3080 list_del_init(&dp->dl_recall_lru);
3081 unhash_delegation(dp);
3082 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003083 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 list_for_each_safe(pos, next, &close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003085 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3086 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3087 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 if (test_val > u)
3089 test_val = u;
3090 break;
3091 }
3092 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003093 oo->oo_owner.so_id);
3094 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3097 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3098 nfs4_unlock_state();
3099 return clientid_val;
3100}
3101
Harvey Harrisona254b242008-02-20 12:49:00 -08003102static struct workqueue_struct *laundry_wq;
3103static void laundromat_main(struct work_struct *);
3104static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3105
3106static void
David Howellsc4028952006-11-22 14:57:56 +00003107laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
3109 time_t t;
3110
3111 t = nfs4_laundromat();
3112 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07003113 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
3115
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003116static struct nfs4_openowner * search_close_lru(u32 st_id)
NeilBrownf8816512005-07-07 17:59:25 -07003117{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003118 struct nfs4_openowner *local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003120 list_for_each_entry(local, &close_lru, oo_close_lru) {
3121 if (local->oo_owner.so_id == st_id)
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003122 return local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 }
3124 return NULL;
3125}
3126
3127static inline int
3128nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
3129{
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003130 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
3133static int
3134STALE_STATEID(stateid_t *stateid)
3135{
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003136 if (stateid->si_boot == boot_time)
3137 return 0;
3138 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003139 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003140 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
3143static inline int
3144access_permit_read(unsigned long access_bmap)
3145{
3146 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3147 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3148 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3149}
3150
3151static inline int
3152access_permit_write(unsigned long access_bmap)
3153{
3154 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3155 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3156}
3157
3158static
Al Virob37ad282006-10-19 23:28:59 -07003159__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Al Virob37ad282006-10-19 23:28:59 -07003161 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
J. Bruce Fields02921912010-07-29 15:16:59 -04003163 /* For lock stateid's, we test the parent open, not the lock: */
3164 if (stp->st_openstp)
3165 stp = stp->st_openstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3167 goto out;
3168 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3169 goto out;
3170 status = nfs_ok;
3171out:
3172 return status;
3173}
3174
Al Virob37ad282006-10-19 23:28:59 -07003175static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3177{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003178 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003180 else if (locks_in_grace()) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003181 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 * conflicting state; so we must wait out the grace period. */
3183 return nfserr_grace;
3184 } else if (flags & WR_STATE)
3185 return nfs4_share_conflict(current_fh,
3186 NFS4_SHARE_DENY_WRITE);
3187 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3188 return nfs4_share_conflict(current_fh,
3189 NFS4_SHARE_DENY_READ);
3190}
3191
3192/*
3193 * Allow READ/WRITE during grace period on recovered state only for files
3194 * that are not able to provide mandatory locking.
3195 */
3196static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08003197grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003199 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
3201
J. Bruce Fields81b82962011-08-23 11:03:29 -04003202/* Returns true iff a is later than b: */
3203static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3204{
3205 return (s32)a->si_generation - (s32)b->si_generation > 0;
3206}
3207
J. Bruce Fields28dde242011-08-22 10:07:12 -04003208static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003209{
Andy Adamson66689582009-04-03 08:28:45 +03003210 /*
3211 * When sessions are used the stateid generation number is ignored
3212 * when it is zero.
3213 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003214 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003215 return nfs_ok;
3216
3217 if (in->si_generation == ref->si_generation)
3218 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003219
J. Bruce Fields0836f582008-01-26 19:08:12 -05003220 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003221 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003222 return nfserr_bad_stateid;
3223 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003224 * However, we could see a stateid from the past, even from a
3225 * non-buggy client. For example, if the client sends a lock
3226 * while some IO is outstanding, the lock may bump si_generation
3227 * while the IO is still in flight. The client could avoid that
3228 * situation by waiting for responses on all the IO requests,
3229 * but better performance may result in retrying IO that
3230 * receives an old_stateid error if requests are rarely
3231 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003232 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003233 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003234}
3235
J. Bruce Fields3e633072009-02-21 13:17:19 -08003236static int is_delegation_stateid(stateid_t *stateid)
3237{
3238 return stateid->si_fileid == 0;
3239}
3240
J. Bruce Fields28dde242011-08-22 10:07:12 -04003241__be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
Bryan Schumaker17456802011-07-13 10:50:48 -04003242{
3243 struct nfs4_stateid *stp = NULL;
3244 __be32 status = nfserr_stale_stateid;
3245
3246 if (STALE_STATEID(stateid))
3247 goto out;
3248
3249 status = nfserr_expired;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04003250 stp = find_stateid(stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04003251 if (!stp)
3252 goto out;
3253 status = nfserr_bad_stateid;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003254 if (stp->st_stateowner->so_is_open_owner
3255 && !openowner(stp->st_stateowner)->oo_confirmed)
Bryan Schumaker17456802011-07-13 10:50:48 -04003256 goto out;
3257
J. Bruce Fields28dde242011-08-22 10:07:12 -04003258 status = check_stateid_generation(stateid, &stp->st_stateid, has_session);
Bryan Schumaker17456802011-07-13 10:50:48 -04003259 if (status)
3260 goto out;
3261
3262 status = nfs_ok;
3263out:
3264 return status;
3265}
3266
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267/*
3268* Checks for stateid operations
3269*/
Al Virob37ad282006-10-19 23:28:59 -07003270__be32
Benny Halevydd453df2009-04-03 08:28:41 +03003271nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3272 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
3274 struct nfs4_stateid *stp = NULL;
3275 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003276 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07003278 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 if (filpp)
3281 *filpp = NULL;
3282
J. Bruce Fields18f82732009-02-21 15:23:01 -08003283 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 return nfserr_grace;
3285
3286 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3287 return check_special_stateids(current_fh, stateid, flags);
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 status = nfserr_stale_stateid;
3290 if (STALE_STATEID(stateid))
3291 goto out;
3292
J. Bruce Fields33515142010-10-02 18:42:39 -04003293 /*
3294 * We assume that any stateid that has the current boot time,
3295 * but that we can't find, is expired:
3296 */
3297 status = nfserr_expired;
J. Bruce Fields3e633072009-02-21 13:17:19 -08003298 if (is_delegation_stateid(stateid)) {
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003299 dp = find_delegation_stateid(ino, stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003300 if (!dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 goto out;
J. Bruce Fields28dde242011-08-22 10:07:12 -04003302 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08003303 if (status)
3304 goto out;
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003305 status = nfs4_check_delegmode(dp, flags);
3306 if (status)
3307 goto out;
3308 renew_client(dp->dl_client);
Dan Carpenter43b01782010-10-27 23:19:04 +02003309 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003310 *filpp = dp->dl_file->fi_deleg_file;
Dan Carpenter43b01782010-10-27 23:19:04 +02003311 BUG_ON(!*filpp);
3312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 } else { /* open or lock stateid */
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04003314 stp = find_stateid(stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003315 if (!stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003317 status = nfserr_bad_stateid;
J. Bruce Fields6150ef02009-02-21 13:36:16 -08003318 if (nfs4_check_fh(current_fh, stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003320 if (stp->st_stateowner->so_is_open_owner
3321 && !openowner(stp->st_stateowner)->oo_confirmed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 goto out;
Andy Adamson66689582009-04-03 08:28:45 +03003323 status = check_stateid_generation(stateid, &stp->st_stateid,
J. Bruce Fields28dde242011-08-22 10:07:12 -04003324 nfsd4_has_session(cstate));
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08003325 if (status)
3326 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003327 status = nfs4_check_openmode(stp, flags);
3328 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 goto out;
3330 renew_client(stp->st_stateowner->so_client);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003331 if (filpp) {
3332 if (flags & RD_STATE)
3333 *filpp = find_readable_file(stp->st_file);
3334 else
3335 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 }
3338 status = nfs_ok;
3339out:
3340 return status;
3341}
3342
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003343static __be32
3344nfsd4_free_delegation_stateid(stateid_t *stateid)
3345{
3346 struct nfs4_delegation *dp = search_for_delegation(stateid);
3347 if (dp)
3348 return nfserr_locks_held;
3349 return nfserr_bad_stateid;
3350}
3351
3352static __be32
3353nfsd4_free_lock_stateid(struct nfs4_stateid *stp)
3354{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003355 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003356 return nfserr_locks_held;
3357 release_lock_stateid(stp);
3358 return nfs_ok;
3359}
3360
3361/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003362 * Test if the stateid is valid
3363 */
3364__be32
3365nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3366 struct nfsd4_test_stateid *test_stateid)
3367{
3368 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3369 return nfs_ok;
3370}
3371
3372/*
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003373 * Free a state id
3374 */
3375__be32
3376nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3377 struct nfsd4_free_stateid *free_stateid)
3378{
3379 stateid_t *stateid = &free_stateid->fr_stateid;
3380 struct nfs4_stateid *stp;
3381 __be32 ret;
3382
3383 nfs4_lock_state();
3384 if (is_delegation_stateid(stateid)) {
3385 ret = nfsd4_free_delegation_stateid(stateid);
3386 goto out;
3387 }
3388
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04003389 stp = find_stateid(stateid);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003390 if (!stp) {
3391 ret = nfserr_bad_stateid;
3392 goto out;
3393 }
J. Bruce Fields81b82962011-08-23 11:03:29 -04003394 ret = check_stateid_generation(stateid, &stp->st_stateid, 1);
3395 if (ret)
3396 goto out;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003397
J. Bruce Fields5fa0bbb2011-08-31 15:25:46 -04003398 if (stp->st_type == NFS4_OPEN_STID) {
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003399 ret = nfserr_locks_held;
3400 goto out;
3401 } else {
3402 ret = nfsd4_free_lock_stateid(stp);
3403 goto out;
3404 }
3405
3406out:
3407 nfs4_unlock_state();
3408 return ret;
3409}
3410
NeilBrown4c4cd222005-07-07 17:59:27 -07003411static inline int
3412setlkflg (int type)
3413{
3414 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3415 RD_STATE : WR_STATE;
3416}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003418static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid)
3419{
3420 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3421 return nfserr_bad_stateid;
3422 if (STALE_STATEID(stateid))
3423 return nfserr_stale_stateid;
3424 return nfs_ok;
3425}
3426
3427static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_stateid *stp)
3428{
3429 struct svc_fh *current_fh = &cstate->current_fh;
3430 struct nfs4_stateowner *sop = stp->st_stateowner;
3431 __be32 status;
3432
3433 if (nfs4_check_fh(current_fh, stp))
3434 return nfserr_bad_stateid;
3435 status = nfsd4_check_seqid(cstate, sop, seqid);
3436 if (status)
3437 return status;
3438 return check_stateid_generation(stateid, &stp->st_stateid, nfsd4_has_session(cstate));
3439}
3440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441/*
3442 * Checks for sequence id mutating operations.
3443 */
Al Virob37ad282006-10-19 23:28:59 -07003444static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003445nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003446 stateid_t *stateid, char typemask,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003447 struct nfs4_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003449 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003451 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3452 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 *stpp = NULL;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003455 status = nfs4_nospecial_stateid_checks(stateid);
3456 if (status)
3457 return status;
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003458 *stpp = find_stateid_by_type(stateid, typemask);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003459 if (*stpp == NULL)
3460 return nfserr_expired;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003461 cstate->replay_owner = (*stpp)->st_stateowner;
3462 renew_client((*stpp)->st_stateowner->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003464 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3465}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003466
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003467static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_stateid **stpp)
3468{
3469 __be32 status;
3470 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003472 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003473 NFS4_OPEN_STID, stpp);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003474 if (status)
3475 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003476 oo = openowner((*stpp)->st_stateowner);
3477 if (!oo->oo_confirmed)
NeilBrown3a4f98b2005-07-07 17:59:26 -07003478 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003479 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480}
3481
Al Virob37ad282006-10-19 23:28:59 -07003482__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003483nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003484 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485{
Al Virob37ad282006-10-19 23:28:59 -07003486 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003487 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 struct nfs4_stateid *stp;
3489
3490 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003491 (int)cstate->current_fh.fh_dentry->d_name.len,
3492 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003494 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003495 if (status)
3496 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
3498 nfs4_lock_state();
3499
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003500 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003501 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003502 NFS4_OPEN_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003503 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003504 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003505 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003506 status = nfserr_bad_stateid;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003507 if (oo->oo_confirmed)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003508 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003509 oo->oo_confirmed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 update_stateid(&stp->st_stateid);
3511 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003512 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3513 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003514
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003515 nfsd4_create_clid_dir(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003516 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003518 if (!cstate->replay_owner)
3519 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 return status;
3521}
3522
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003523static inline void nfs4_file_downgrade(struct nfs4_stateid *stp, unsigned int to_access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524{
3525 int i;
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 for (i = 1; i < 4; i++) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003528 if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
3529 nfs4_file_put_access(stp->st_file, i);
3530 __clear_bit(i, &stp->st_access_bmap);
3531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 }
3533}
3534
3535static void
3536reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3537{
3538 int i;
3539 for (i = 0; i < 4; i++) {
3540 if ((i & deny) != i)
3541 __clear_bit(i, bmap);
3542 }
3543}
3544
Al Virob37ad282006-10-19 23:28:59 -07003545__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003546nfsd4_open_downgrade(struct svc_rqst *rqstp,
3547 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003548 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
Al Virob37ad282006-10-19 23:28:59 -07003550 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003554 (int)cstate->current_fh.fh_dentry->d_name.len,
3555 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Andy Adamsond87a8ad2009-04-03 08:28:53 +03003557 if (!access_valid(od->od_share_access, cstate->minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07003558 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return nfserr_inval;
3560
3561 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003562 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3563 &od->od_stateid, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003564 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 status = nfserr_inval;
3567 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3568 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3569 stp->st_access_bmap, od->od_share_access);
3570 goto out;
3571 }
3572 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3573 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3574 stp->st_deny_bmap, od->od_share_deny);
3575 goto out;
3576 }
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003577 nfs4_file_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3580
3581 update_stateid(&stp->st_stateid);
3582 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
3583 status = nfs_ok;
3584out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003585 if (!cstate->replay_owner)
3586 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 return status;
3588}
3589
3590/*
3591 * nfs4_unlock_state() called after encode
3592 */
Al Virob37ad282006-10-19 23:28:59 -07003593__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003594nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003595 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
Al Virob37ad282006-10-19 23:28:59 -07003597 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003598 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 struct nfs4_stateid *stp;
3600
3601 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003602 (int)cstate->current_fh.fh_dentry->d_name.len,
3603 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
3605 nfs4_lock_state();
3606 /* check close_lru for replay */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003607 status = nfs4_preprocess_confirmed_seqid_op(cstate, close->cl_seqid,
3608 &close->cl_stateid, &stp);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003609 if (stp == NULL && status == nfserr_expired) {
3610 /*
3611 * Also, we should make sure this isn't just the result of
3612 * a replayed close:
3613 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003614 oo = search_close_lru(close->cl_stateid.si_stateownerid);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003615 /* It's not stale; let's assume it's expired: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003616 if (oo == NULL)
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003617 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003618 cstate->replay_owner = &oo->oo_owner;
3619 status = nfsd4_check_seqid(cstate, &oo->oo_owner, close->cl_seqid);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003620 if (status)
3621 goto out;
3622 status = nfserr_bad_seqid;
3623 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003624 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003626 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 status = nfs_ok;
3628 update_stateid(&stp->st_stateid);
3629 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
3630
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003631 /* release_stateid() calls nfsd_close() if needed */
J. Bruce Fields22839632009-01-11 14:27:17 -05003632 release_open_stateid(stp);
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003633
3634 /* place unused nfs4_stateowners on so_close_lru list to be
3635 * released by the laundromat service after the lease period
3636 * to enable us to handle CLOSE replay
3637 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003638 if (list_empty(&oo->oo_owner.so_stateids))
3639 move_to_close_lru(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003641 if (!cstate->replay_owner)
3642 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 return status;
3644}
3645
Al Virob37ad282006-10-19 23:28:59 -07003646__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003647nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3648 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003650 struct nfs4_delegation *dp;
3651 stateid_t *stateid = &dr->dr_stateid;
3652 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07003653 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003655 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003656 return status;
3657 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
3659 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;
J. Bruce Fields28dde242011-08-22 10:07:12 -04003673 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
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
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04003714static unsigned int lockownerid_hashval(u32 id)
3715{
3716 return id & LOCK_HASH_MASK;
3717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719static inline unsigned int
3720lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3721 struct xdr_netobj *ownername)
3722{
3723 return (file_hashval(inode) + cl_id
3724 + opaque_hashval(ownername->data, ownername->len))
3725 & LOCK_HASH_MASK;
3726}
3727
3728static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3729static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003731static struct nfs4_delegation *
3732search_for_delegation(stateid_t *stid)
3733{
3734 struct nfs4_file *fp;
3735 struct nfs4_delegation *dp;
3736 struct list_head *pos;
3737 int i;
3738
3739 for (i = 0; i < FILE_HASH_SIZE; i++) {
3740 list_for_each_entry(fp, &file_hashtbl[i], fi_hash) {
3741 list_for_each(pos, &fp->fi_delegations) {
3742 dp = list_entry(pos, struct nfs4_delegation, dl_perfile);
3743 if (same_stateid(&dp->dl_stateid, stid))
3744 return dp;
3745 }
3746 }
3747 }
3748 return NULL;
3749}
3750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751static struct nfs4_delegation *
3752find_delegation_stateid(struct inode *ino, stateid_t *stid)
3753{
NeilBrown13cd2182005-06-23 22:03:10 -07003754 struct nfs4_file *fp;
3755 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003757 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3758 STATEID_VAL(stid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07003761 if (!fp)
3762 return NULL;
3763 dl = find_delegation_file(fp, stid);
3764 put_nfs4_file(fp);
3765 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
3768/*
3769 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3770 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3771 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3772 * locking, this prevents us from being completely protocol-compliant. The
3773 * real solution to this problem is to start using unsigned file offsets in
3774 * the VFS, but this is a very deep change!
3775 */
3776static inline void
3777nfs4_transform_lock_offset(struct file_lock *lock)
3778{
3779 if (lock->fl_start < 0)
3780 lock->fl_start = OFFSET_MAX;
3781 if (lock->fl_end < 0)
3782 lock->fl_end = OFFSET_MAX;
3783}
3784
NeilBrownd5b90262006-04-10 22:55:22 -07003785/* Hack!: For now, we're defining this just so we can use a pointer to it
3786 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003787static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003788};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790static inline void
3791nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3792{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003793 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
NeilBrownd5b90262006-04-10 22:55:22 -07003795 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003796 lo = (struct nfs4_lockowner *) fl->fl_owner;
3797 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3798 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003799 if (!deny->ld_owner.data)
3800 /* We just don't care that much */
3801 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003802 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3803 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003804 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003805nevermind:
3806 deny->ld_owner.len = 0;
3807 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003808 deny->ld_clientid.cl_boot = 0;
3809 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 }
3811 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003812 deny->ld_length = NFS4_MAX_UINT64;
3813 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3815 deny->ld_type = NFS4_READ_LT;
3816 if (fl->fl_type != F_RDLCK)
3817 deny->ld_type = NFS4_WRITE_LT;
3818}
3819
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003820static struct nfs4_lockowner *
3821find_lockowner_str(struct inode *inode, clientid_t *clid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 struct xdr_netobj *owner)
3823{
3824 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3825 struct nfs4_stateowner *op;
3826
3827 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003828 if (same_owner_str(op, owner, clid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003829 return lockowner(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 }
3831 return NULL;
3832}
3833
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003834static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003835{
3836 unsigned int idhashval;
3837
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003838 idhashval = lockownerid_hashval(lo->lo_owner.so_id);
3839 list_add(&lo->lo_owner.so_idhash, &lock_ownerid_hashtbl[idhashval]);
3840 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3841 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003842}
3843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844/*
3845 * Alloc a lock owner structure.
3846 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003847 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 *
3849 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 */
3851
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003852static struct nfs4_lockowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003854 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003856 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3857 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003859 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3860 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07003861 /* It is the openowner seqid that will be incremented in encode in the
3862 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003863 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3864 hash_lockowner(lo, strhashval, clp, open_stp);
3865 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866}
3867
NeilBrownfd39ca92005-06-23 22:04:03 -07003868static struct nfs4_stateid *
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003869alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
3871 struct nfs4_stateid *stp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003872 unsigned int hashval = stateid_hashval(lo->lo_owner.so_id, fp->fi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
NeilBrown5ac049a2005-06-23 22:03:03 -07003874 stp = nfs4_alloc_stateid();
3875 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 goto out;
3877 INIT_LIST_HEAD(&stp->st_hash);
3878 INIT_LIST_HEAD(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07003879 INIT_LIST_HEAD(&stp->st_perstateowner);
3880 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
J. Bruce Fieldsb79abad2011-08-22 18:01:43 -04003881 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07003882 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003883 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3884 stp->st_stateowner = &lo->lo_owner;
J. Bruce Fields5fa0bbb2011-08-31 15:25:46 -04003885 stp->st_type = NFS4_LOCK_STID;
NeilBrown13cd2182005-06-23 22:03:10 -07003886 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 stp->st_file = fp;
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003888 stp->st_stateid.si_boot = boot_time;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003889 stp->st_stateid.si_stateownerid = lo->lo_owner.so_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 stp->st_stateid.si_fileid = fp->fi_id;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04003891 /* note will be incremented before first return to client: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 stp->st_stateid.si_generation = 0;
J. Bruce Fields0997b172011-03-02 18:01:35 -05003893 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003895 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
3897out:
3898 return stp;
3899}
3900
NeilBrownfd39ca92005-06-23 22:04:03 -07003901static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902check_lock_length(u64 offset, u64 length)
3903{
Benny Halevy87df4de2008-12-15 19:42:03 +02003904 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 LOFF_OVERFLOW(offset, length)));
3906}
3907
J. Bruce Fields0997b172011-03-02 18:01:35 -05003908static void get_lock_access(struct nfs4_stateid *lock_stp, u32 access)
3909{
3910 struct nfs4_file *fp = lock_stp->st_file;
3911 int oflag = nfs4_access_to_omode(access);
3912
3913 if (test_bit(access, &lock_stp->st_access_bmap))
3914 return;
3915 nfs4_file_get_access(fp, oflag);
3916 __set_bit(access, &lock_stp->st_access_bmap);
3917}
3918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919/*
3920 * LOCK operation
3921 */
Al Virob37ad282006-10-19 23:28:59 -07003922__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003923nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003924 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003926 struct nfs4_openowner *open_sop = NULL;
3927 struct nfs4_lockowner *lock_sop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 struct nfs4_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04003929 struct nfs4_file *fp;
3930 struct file *filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05003932 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07003933 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 unsigned int strhashval;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003935 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07003936 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
3938 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3939 (long long) lock->lk_offset,
3940 (long long) lock->lk_length);
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 if (check_lock_length(lock->lk_offset, lock->lk_length))
3943 return nfserr_inval;
3944
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003945 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02003946 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08003947 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3948 return status;
3949 }
3950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 nfs4_lock_state();
3952
3953 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07003954 /*
3955 * Client indicates that this is a new lockowner.
3956 * Use open owner and open stateid to create lock owner and
3957 * lock stateid.
3958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 struct nfs4_stateid *open_stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
3961 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03003962 if (!nfsd4_has_session(cstate) &&
3963 STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003967 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 lock->lk_new_open_seqid,
3969 &lock->lk_new_open_stateid,
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003970 &open_stp);
NeilBrown37515172005-07-07 17:59:16 -07003971 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003973 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003974 status = nfserr_bad_stateid;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003975 if (!nfsd4_has_session(cstate) &&
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003976 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003977 &lock->v.new.clientid))
3978 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 /* create lockowner and lock stateid */
3980 fp = open_stp->st_file;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003981 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3982 open_sop->oo_owner.so_client->cl_clientid.cl_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003984 /* XXX: Do we need to check for duplicate stateowners on
3985 * the same file, or should they just be allowed (and
3986 * create new stateids)? */
J. Bruce Fields3e772462011-08-10 19:07:33 -04003987 status = nfserr_jukebox;
Neil Brownb59e3c02005-09-13 01:25:38 -07003988 lock_sop = alloc_init_lock_stateowner(strhashval,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003989 open_sop->oo_owner.so_client, open_stp, lock);
Neil Brownb59e3c02005-09-13 01:25:38 -07003990 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07003992 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08003993 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 } else {
3996 /* lock (lock owner + lock stateid) already exists */
Benny Halevydd453df2009-04-03 08:28:41 +03003997 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003998 lock->lk_old_lock_seqid,
3999 &lock->lk_old_lock_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004000 NFS4_LOCK_STID, &lock_stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 if (status)
4002 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004003 lock_sop = lockowner(lock_stp->st_stateowner);
J. Bruce Fields7d947842010-08-20 18:09:31 -04004004 fp = lock_stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004006 /* lock_sop and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004008 lkflg = setlkflg(lock->lk_type);
4009 status = nfs4_check_openmode(lock_stp, lkflg);
4010 if (status)
4011 goto out;
4012
NeilBrown0dd395d2005-07-07 17:59:15 -07004013 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004014 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004015 goto out;
4016 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004017 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004018 goto out;
4019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 locks_init_lock(&file_lock);
4021 switch (lock->lk_type) {
4022 case NFS4_READ_LT:
4023 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004024 filp = find_readable_file(lock_stp->st_file);
4025 if (filp)
4026 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 file_lock.fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 case NFS4_WRITE_LT:
4030 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004031 filp = find_writeable_file(lock_stp->st_file);
4032 if (filp)
4033 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 file_lock.fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 default:
4037 status = nfserr_inval;
4038 goto out;
4039 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004040 if (!filp) {
4041 status = nfserr_openmode;
4042 goto out;
4043 }
Neil Brownb59e3c02005-09-13 01:25:38 -07004044 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 file_lock.fl_pid = current->tgid;
4046 file_lock.fl_file = filp;
4047 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004048 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
4050 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004051 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 nfs4_transform_lock_offset(&file_lock);
4053
4054 /*
4055 * Try to lock the file in the VFS.
4056 * Note: locks.c uses the BKL to protect the inode's lock list.
4057 */
4058
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004059 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004060 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 case 0: /* success! */
4062 update_stateid(&lock_stp->st_stateid);
4063 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
4064 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004065 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004066 break;
4067 case (EAGAIN): /* conflock holds conflicting lock */
4068 status = nfserr_denied;
4069 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4070 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 case (EDEADLK):
4073 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004074 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004075 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004076 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004077 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08004081 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004082 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004083 if (!cstate->replay_owner)
4084 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return status;
4086}
4087
4088/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004089 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4090 * so we do a temporary open here just to get an open file to pass to
4091 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4092 * inode operation.)
4093 */
4094static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4095{
4096 struct file *file;
4097 int err;
4098
4099 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4100 if (err)
4101 return err;
4102 err = vfs_test_lock(file, lock);
4103 nfsd_close(file);
4104 return err;
4105}
4106
4107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 * LOCKT operation
4109 */
Al Virob37ad282006-10-19 23:28:59 -07004110__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004111nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4112 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
4114 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 struct file_lock file_lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004116 struct nfs4_lockowner *lo;
Marc Eshelfd85b812006-11-28 16:26:41 -05004117 int error;
Al Virob37ad282006-10-19 23:28:59 -07004118 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004120 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return nfserr_grace;
4122
4123 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4124 return nfserr_inval;
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 nfs4_lock_state();
4127
4128 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03004129 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004132 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004135 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 locks_init_lock(&file_lock);
4137 switch (lockt->lt_type) {
4138 case NFS4_READ_LT:
4139 case NFS4_READW_LT:
4140 file_lock.fl_type = F_RDLCK;
4141 break;
4142 case NFS4_WRITE_LT:
4143 case NFS4_WRITEW_LT:
4144 file_lock.fl_type = F_WRLCK;
4145 break;
4146 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004147 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 status = nfserr_inval;
4149 goto out;
4150 }
4151
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004152 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4153 if (lo)
4154 file_lock.fl_owner = (fl_owner_t)lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 file_lock.fl_pid = current->tgid;
4156 file_lock.fl_flags = FL_POSIX;
4157
4158 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004159 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
4161 nfs4_transform_lock_offset(&file_lock);
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004164 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05004165 if (error) {
4166 status = nfserrno(error);
4167 goto out;
4168 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004169 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004171 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 }
4173out:
4174 nfs4_unlock_state();
4175 return status;
4176}
4177
Al Virob37ad282006-10-19 23:28:59 -07004178__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004179nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004180 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
4182 struct nfs4_stateid *stp;
4183 struct file *filp = NULL;
4184 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07004185 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004186 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
4188 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4189 (long long) locku->lu_offset,
4190 (long long) locku->lu_length);
4191
4192 if (check_lock_length(locku->lu_offset, locku->lu_length))
4193 return nfserr_inval;
4194
4195 nfs4_lock_state();
4196
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004197 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004198 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004199 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004201 filp = find_any_file(stp->st_file);
4202 if (!filp) {
4203 status = nfserr_lock_range;
4204 goto out;
4205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 BUG_ON(!filp);
4207 locks_init_lock(&file_lock);
4208 file_lock.fl_type = F_UNLCK;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004209 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 file_lock.fl_pid = current->tgid;
4211 file_lock.fl_file = filp;
4212 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004213 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 file_lock.fl_start = locku->lu_offset;
4215
Benny Halevy87df4de2008-12-15 19:42:03 +02004216 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 nfs4_transform_lock_offset(&file_lock);
4218
4219 /*
4220 * Try to unlock the file in the VFS.
4221 */
Marc Eshelfd85b812006-11-28 16:26:41 -05004222 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004223 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004224 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 goto out_nfserr;
4226 }
4227 /*
4228 * OK, unlock succeeded; the only thing left to do is update the stateid.
4229 */
4230 update_stateid(&stp->st_stateid);
4231 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
4232
4233out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 nfs4_unlock_state();
4235 return status;
4236
4237out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004238 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 goto out;
4240}
4241
4242/*
4243 * returns
4244 * 1: locks held by lockowner
4245 * 0: no locks held by lockowner
4246 */
4247static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004248check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
4250 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004251 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 int status = 0;
4253
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004254 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004256 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 status = 1;
4258 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
4261out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004262 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 return status;
4264}
4265
Al Virob37ad282006-10-19 23:28:59 -07004266__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004267nfsd4_release_lockowner(struct svc_rqst *rqstp,
4268 struct nfsd4_compound_state *cstate,
4269 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
4271 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004272 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004273 struct nfs4_lockowner *lo;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004274 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004276 struct list_head matches;
4277 int i;
Al Virob37ad282006-10-19 23:28:59 -07004278 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
4280 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4281 clid->cl_boot, clid->cl_id);
4282
4283 /* XXX check for lease expiration */
4284
4285 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07004286 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289 nfs4_lock_state();
4290
NeilBrown3e9e3db2005-06-23 22:04:20 -07004291 status = nfserr_locks_held;
4292 /* XXX: we're doing a linear search through all the lockowners.
4293 * Yipes! For now we'll just hope clients aren't really using
4294 * release_lockowner much, but eventually we have to fix these
4295 * data structures. */
4296 INIT_LIST_HEAD(&matches);
4297 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4298 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04004299 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004300 continue;
4301 list_for_each_entry(stp, &sop->so_stateids,
4302 st_perstateowner) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004303 lo = lockowner(sop);
4304 if (check_for_locks(stp->st_file, lo))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004305 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004306 list_add(&lo->lo_list, &matches);
NeilBrown3e9e3db2005-06-23 22:04:20 -07004307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004309 }
4310 /* Clients probably won't expect us to return with some (but not all)
4311 * of the lockowner state released; so don't release any until all
4312 * have been checked. */
4313 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004314 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004315 lo = list_entry(matches.next, struct nfs4_lockowner,
4316 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004317 /* unhash_stateowner deletes so_perclient only
4318 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004319 list_del(&lo->lo_list);
4320 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 }
4322out:
4323 nfs4_unlock_state();
4324 return status;
4325}
4326
4327static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004328alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
NeilBrowna55370a2005-06-23 22:03:52 -07004330 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331}
4332
NeilBrownc7b9a452005-06-23 22:04:30 -07004333int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03004334nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07004335{
4336 unsigned int strhashval = clientstr_hashval(name);
4337 struct nfs4_client *clp;
4338
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004339 clp = find_confirmed_client_by_str(name, strhashval);
NeilBrownc7b9a452005-06-23 22:04:30 -07004340 return clp ? 1 : 0;
4341}
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343/*
4344 * failure => all reset bets are off, nfserr_no_grace...
4345 */
NeilBrown190e4fb2005-06-23 22:04:25 -07004346int
4347nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
4349 unsigned int strhashval;
4350 struct nfs4_client_reclaim *crp = NULL;
4351
NeilBrowna55370a2005-06-23 22:03:52 -07004352 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4353 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 if (!crp)
4355 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07004356 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 INIT_LIST_HEAD(&crp->cr_strhash);
4358 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07004359 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 reclaim_str_hashtbl_size++;
4361 return 1;
4362}
4363
4364static void
4365nfs4_release_reclaim(void)
4366{
4367 struct nfs4_client_reclaim *crp = NULL;
4368 int i;
4369
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4371 while (!list_empty(&reclaim_str_hashtbl[i])) {
4372 crp = list_entry(reclaim_str_hashtbl[i].next,
4373 struct nfs4_client_reclaim, cr_strhash);
4374 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 kfree(crp);
4376 reclaim_str_hashtbl_size--;
4377 }
4378 }
4379 BUG_ON(reclaim_str_hashtbl_size);
4380}
4381
4382/*
4383 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07004384static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385nfs4_find_reclaim_client(clientid_t *clid)
4386{
4387 unsigned int strhashval;
4388 struct nfs4_client *clp;
4389 struct nfs4_client_reclaim *crp = NULL;
4390
4391
4392 /* find clientid in conf_id_hashtbl */
4393 clp = find_confirmed_client(clid);
4394 if (clp == NULL)
4395 return NULL;
4396
NeilBrowna55370a2005-06-23 22:03:52 -07004397 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4398 clp->cl_name.len, clp->cl_name.data,
4399 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07004402 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07004404 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 return crp;
4406 }
4407 }
4408 return NULL;
4409}
4410
4411/*
4412* Called from OPEN. Look for clientid in reclaim list.
4413*/
Al Virob37ad282006-10-19 23:28:59 -07004414__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415nfs4_check_open_reclaim(clientid_t *clid)
4416{
NeilBrowndfc83562005-06-23 22:03:16 -07004417 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
NeilBrownac4d8ff22005-06-23 22:03:30 -07004420/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004422int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004423nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004425 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004427 status = nfsd4_init_slabs();
4428 if (status)
4429 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4431 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4432 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4433 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4434 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
Wang Chen02cb2852009-04-24 15:41:57 +08004435 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
Marc Eshel5282fd72009-04-03 08:27:52 +03004437 for (i = 0; i < SESSION_HASH_SIZE; i++)
4438 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 for (i = 0; i < FILE_HASH_SIZE; i++) {
4440 INIT_LIST_HEAD(&file_hashtbl[i]);
4441 }
J. Bruce Fields506f2752011-08-11 18:50:18 -04004442 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4443 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
4444 INIT_LIST_HEAD(&open_ownerid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
J. Bruce Fieldsb79abad2011-08-22 18:01:43 -04004446 for (i = 0; i < STATEID_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 INIT_LIST_HEAD(&stateid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4449 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4450 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 INIT_LIST_HEAD(&close_lru);
4454 INIT_LIST_HEAD(&client_lru);
4455 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004456 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004457 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07004458}
4459
NeilBrown190e4fb2005-06-23 22:04:25 -07004460static void
4461nfsd4_load_reboot_recovery_data(void)
4462{
4463 int status;
4464
NeilBrown0964a3d2005-06-23 22:04:32 -07004465 nfs4_lock_state();
J. Bruce Fields48483bf2011-08-26 20:40:28 -04004466 nfsd4_init_recdir();
NeilBrown190e4fb2005-06-23 22:04:25 -07004467 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07004468 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07004469 if (status)
4470 printk("NFSD: Failure reading reboot recovery data\n");
4471}
4472
Meelap Shahc2f1a552007-07-17 04:04:39 -07004473/*
4474 * Since the lifetime of a delegation isn't limited to that of an open, a
4475 * client may quite reasonably hang on to a delegation as long as it has
4476 * the inode cached. This becomes an obvious problem the first time a
4477 * client's inode cache approaches the size of the server's total memory.
4478 *
4479 * For now we avoid this problem by imposing a hard limit on the number
4480 * of delegations, which varies according to the server's memory size.
4481 */
4482static void
4483set_max_delegations(void)
4484{
4485 /*
4486 * Allow at most 4 delegations per megabyte of RAM. Quick
4487 * estimates suggest that in the worst case (where every delegation
4488 * is for a different inode), a delegation could take about 1.5K,
4489 * giving a worst case usage of about 6% of memory.
4490 */
4491 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4492}
4493
NeilBrownac4d8ff22005-06-23 22:03:30 -07004494/* initialization to perform when the nfsd service is started: */
4495
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004496static int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004497__nfs4_state_start(void)
4498{
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004499 int ret;
4500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004502 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07004503 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004504 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004505 ret = set_callback_cred();
4506 if (ret)
4507 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004508 laundry_wq = create_singlethread_workqueue("nfsd4");
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004509 if (laundry_wq == NULL)
4510 return -ENOMEM;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004511 ret = nfsd4_create_callback_queue();
4512 if (ret)
4513 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004514 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004515 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004516 return 0;
4517out_free_laundry:
4518 destroy_workqueue(laundry_wq);
4519 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004522int
NeilBrown76a35502005-06-23 22:03:26 -07004523nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
NeilBrown190e4fb2005-06-23 22:04:25 -07004525 nfsd4_load_reboot_recovery_data();
Jeff Layton4ad9a342010-07-19 16:50:04 -04004526 return __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527}
4528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529static void
4530__nfs4_state_shutdown(void)
4531{
4532 int i;
4533 struct nfs4_client *clp = NULL;
4534 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 struct list_head *pos, *next, reaplist;
4536
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4538 while (!list_empty(&conf_id_hashtbl[i])) {
4539 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4540 expire_client(clp);
4541 }
4542 while (!list_empty(&unconf_str_hashtbl[i])) {
4543 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4544 expire_client(clp);
4545 }
4546 }
4547 INIT_LIST_HEAD(&reaplist);
4548 spin_lock(&recall_lock);
4549 list_for_each_safe(pos, next, &del_recall_lru) {
4550 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4551 list_move(&dp->dl_recall_lru, &reaplist);
4552 }
4553 spin_unlock(&recall_lock);
4554 list_for_each_safe(pos, next, &reaplist) {
4555 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4556 list_del_init(&dp->dl_recall_lru);
4557 unhash_delegation(dp);
4558 }
4559
NeilBrown190e4fb2005-06-23 22:04:25 -07004560 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561}
4562
4563void
4564nfs4_state_shutdown(void)
4565{
Tejun Heoafe2c512010-12-14 16:21:17 +01004566 cancel_delayed_work_sync(&laundromat_work);
NeilBrown5e8d5c22006-04-10 22:55:37 -07004567 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06004568 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 nfs4_lock_state();
4570 nfs4_release_reclaim();
4571 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 nfs4_unlock_state();
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004573 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574}