blob: f358262a95f98b0d6de29d3b13b4e7749c804dc9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
NeilBrown8478eaa2014-09-18 16:09:27 +100080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *, long *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
David Quigleyaa9c2662013-05-22 12:50:44 -0400108 err = security_dentry_init_security(dentry, sattr->ia_mode,
109 &dentry->d_name, (void **)&label->label, &label->len);
110 if (err == 0)
111 return label;
112
113 return NULL;
114}
115static inline void
116nfs4_label_release_security(struct nfs4_label *label)
117{
118 if (label)
119 security_release_secctx(label->label, label->len);
120}
121static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
122{
123 if (label)
124 return server->attr_bitmask;
125
126 return server->attr_bitmask_nl;
127}
128#else
129static inline struct nfs4_label *
130nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
131 struct iattr *sattr, struct nfs4_label *l)
132{ return NULL; }
133static inline void
134nfs4_label_release_security(struct nfs4_label *label)
135{ return; }
136static inline u32 *
137nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138{ return server->attr_bitmask; }
139#endif
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500142static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Trond Myklebust52567b02009-10-23 14:46:42 -0400144 if (err >= -1000)
145 return err;
146 switch (err) {
147 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500148 case -NFS4ERR_LAYOUTTRYLATER:
149 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400150 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000151 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400152 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000153 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800154 case -NFS4ERR_BADOWNER:
155 case -NFS4ERR_BADNAME:
156 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400157 case -NFS4ERR_SHARE_DENIED:
158 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400159 case -NFS4ERR_MINOR_VERS_MISMATCH:
160 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400161 case -NFS4ERR_FILE_OPEN:
162 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400163 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700165 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/*
172 * This is our standard bitmap for GETATTR requests.
173 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400174const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 FATTR4_WORD0_TYPE
176 | FATTR4_WORD0_CHANGE
177 | FATTR4_WORD0_SIZE
178 | FATTR4_WORD0_FSID
179 | FATTR4_WORD0_FILEID,
180 FATTR4_WORD1_MODE
181 | FATTR4_WORD1_NUMLINKS
182 | FATTR4_WORD1_OWNER
183 | FATTR4_WORD1_OWNER_GROUP
184 | FATTR4_WORD1_RAWDEV
185 | FATTR4_WORD1_SPACE_USED
186 | FATTR4_WORD1_TIME_ACCESS
187 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400188 | FATTR4_WORD1_TIME_MODIFY,
189#ifdef CONFIG_NFS_V4_SECURITY_LABEL
190 FATTR4_WORD2_SECURITY_LABEL
191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
Trond Myklebust1549210f2012-06-05 09:16:47 -0400194static const u32 nfs4_pnfs_open_bitmap[3] = {
195 FATTR4_WORD0_TYPE
196 | FATTR4_WORD0_CHANGE
197 | FATTR4_WORD0_SIZE
198 | FATTR4_WORD0_FSID
199 | FATTR4_WORD0_FILEID,
200 FATTR4_WORD1_MODE
201 | FATTR4_WORD1_NUMLINKS
202 | FATTR4_WORD1_OWNER
203 | FATTR4_WORD1_OWNER_GROUP
204 | FATTR4_WORD1_RAWDEV
205 | FATTR4_WORD1_SPACE_USED
206 | FATTR4_WORD1_TIME_ACCESS
207 | FATTR4_WORD1_TIME_METADATA
208 | FATTR4_WORD1_TIME_MODIFY,
209 FATTR4_WORD2_MDSTHRESHOLD
210};
211
Andy Adamsone23008e2012-10-02 21:07:32 -0400212static const u32 nfs4_open_noattr_bitmap[3] = {
213 FATTR4_WORD0_TYPE
214 | FATTR4_WORD0_CHANGE
215 | FATTR4_WORD0_FILEID,
216};
217
David Quigleya09df2c2013-05-22 12:50:41 -0400218const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 FATTR4_WORD0_FILES_AVAIL
220 | FATTR4_WORD0_FILES_FREE
221 | FATTR4_WORD0_FILES_TOTAL,
222 FATTR4_WORD1_SPACE_AVAIL
223 | FATTR4_WORD1_SPACE_FREE
224 | FATTR4_WORD1_SPACE_TOTAL
225};
226
David Quigleya09df2c2013-05-22 12:50:41 -0400227const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 FATTR4_WORD0_MAXLINK
229 | FATTR4_WORD0_MAXNAME,
230 0
231};
232
Fred Isamandae100c2011-07-30 20:52:37 -0400233const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 | FATTR4_WORD0_MAXREAD
235 | FATTR4_WORD0_MAXWRITE
236 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700237 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400238 | FATTR4_WORD1_FS_LAYOUT_TYPES,
239 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400243 FATTR4_WORD0_TYPE
244 | FATTR4_WORD0_CHANGE
245 | FATTR4_WORD0_SIZE
246 | FATTR4_WORD0_FSID
247 | FATTR4_WORD0_FILEID
248 | FATTR4_WORD0_FS_LOCATIONS,
249 FATTR4_WORD1_MODE
250 | FATTR4_WORD1_NUMLINKS
251 | FATTR4_WORD1_OWNER
252 | FATTR4_WORD1_OWNER_GROUP
253 | FATTR4_WORD1_RAWDEV
254 | FATTR4_WORD1_SPACE_USED
255 | FATTR4_WORD1_TIME_ACCESS
256 | FATTR4_WORD1_TIME_METADATA
257 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400258 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400259};
260
Al Virobc4785c2006-10-19 23:28:51 -0700261static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct nfs4_readdir_arg *readdir)
263{
Al Viro0dbb4c62006-10-19 23:28:49 -0700264 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000267 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
269 return;
270 }
271
272 readdir->cookie = 0;
273 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
274 if (cookie == 2)
275 return;
276
277 /*
278 * NFSv4 servers do not return entries for '.' and '..'
279 * Therefore, we fake these entries here. We let '.'
280 * have cookie 0 and '..' have cookie 1. Note that
281 * when talking to the server, we always send cookie 0
282 * instead of 1 or 2.
283 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800284 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (cookie == 0) {
287 *p++ = xdr_one; /* next */
288 *p++ = xdr_zero; /* cookie, first word */
289 *p++ = xdr_one; /* cookie, second word */
290 *p++ = xdr_one; /* entry len */
291 memcpy(p, ".\0\0\0", 4); /* entry */
292 p++;
293 *p++ = xdr_one; /* bitmap length */
294 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
295 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400296 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
299 *p++ = xdr_one; /* next */
300 *p++ = xdr_zero; /* cookie, first word */
301 *p++ = xdr_two; /* cookie, second word */
302 *p++ = xdr_two; /* entry len */
303 memcpy(p, "..\0\0", 4); /* entry */
304 p++;
305 *p++ = xdr_one; /* bitmap length */
306 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
307 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400308 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 readdir->pgbase = (char *)p - (char *)start;
311 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800312 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
NeilBrown8478eaa2014-09-18 16:09:27 +1000315static long nfs4_update_delay(long *timeout)
316{
317 long ret;
318 if (!timeout)
319 return NFS4_POLL_RETRY_MAX;
320 if (*timeout <= 0)
321 *timeout = NFS4_POLL_RETRY_MIN;
322 if (*timeout > NFS4_POLL_RETRY_MAX)
323 *timeout = NFS4_POLL_RETRY_MAX;
324 ret = *timeout;
325 *timeout <<= 1;
326 return ret;
327}
328
Trond Myklebust65de8722008-12-23 15:21:44 -0500329static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
330{
331 int res = 0;
332
333 might_sleep();
334
NeilBrown8478eaa2014-09-18 16:09:27 +1000335 freezable_schedule_timeout_killable_unsafe(
336 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 if (fatal_signal_pending(current))
338 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 return res;
340}
341
342/* This is the error handling routine for processes that are allowed
343 * to sleep.
344 */
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500345int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500346{
347 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500348 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500349 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500350 int ret = errorcode;
351
352 exception->retry = 0;
353 switch(errorcode) {
354 case 0:
355 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500356 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400357 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400358 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500359 exception->retry = 1;
360 return 0;
361 }
362 if (state == NULL)
363 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400364 ret = nfs4_schedule_stateid_recovery(server, state);
365 if (ret < 0)
366 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500367 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500368 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500369 case -NFS4ERR_ADMIN_REVOKED:
370 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500371 if (state == NULL)
372 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400373 ret = nfs4_schedule_stateid_recovery(server, state);
374 if (ret < 0)
375 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500376 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400377 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400378 if (state != NULL) {
379 ret = nfs4_schedule_stateid_recovery(server, state);
380 if (ret < 0)
381 break;
382 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500384 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500385 nfs4_schedule_lease_recovery(clp);
386 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400387 case -NFS4ERR_MOVED:
388 ret = nfs4_schedule_migration_recovery(server);
389 if (ret < 0)
390 break;
391 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400392 case -NFS4ERR_LEASE_MOVED:
393 nfs4_schedule_lease_moved_recovery(clp);
394 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500395#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400396 case -NFS4ERR_BADSESSION:
397 case -NFS4ERR_BADSLOT:
398 case -NFS4ERR_BAD_HIGH_SLOT:
399 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
400 case -NFS4ERR_DEADSESSION:
401 case -NFS4ERR_SEQ_FALSE_RETRY:
402 case -NFS4ERR_SEQ_MISORDERED:
403 dprintk("%s ERROR: %d Reset session\n", __func__,
404 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400405 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400406 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500407#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500408 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500409 if (exception->timeout > HZ) {
410 /* We have retried a decent amount, time to
411 * fail
412 */
413 ret = -EBUSY;
414 break;
415 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500416 case -NFS4ERR_GRACE:
417 case -NFS4ERR_DELAY:
418 ret = nfs4_delay(server->client, &exception->timeout);
419 if (ret != 0)
420 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400421 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500422 case -NFS4ERR_OLD_STATEID:
423 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800424 break;
425 case -NFS4ERR_BADOWNER:
426 /* The following works around a Linux server bug! */
427 case -NFS4ERR_BADNAME:
428 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
429 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
430 exception->retry = 1;
431 printk(KERN_WARNING "NFS: v4 server %s "
432 "does not accept raw "
433 "uid/gids. "
434 "Reenabling the idmapper.\n",
435 server->nfs_client->cl_hostname);
436 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 }
438 /* We failed to handle the error */
439 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500440wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500441 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400442 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
443 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500444 if (ret == 0)
445 exception->retry = 1;
446 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500447}
448
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400449/*
450 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
451 * or 'false' otherwise.
452 */
453static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
454{
455 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
456
457 if (flavor == RPC_AUTH_GSS_KRB5I ||
458 flavor == RPC_AUTH_GSS_KRB5P)
459 return true;
460
461 return false;
462}
Trond Myklebust65de8722008-12-23 15:21:44 -0500463
Trond Myklebust452e9352010-07-31 14:29:06 -0400464static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 spin_lock(&clp->cl_lock);
467 if (time_before(clp->cl_last_renewal,timestamp))
468 clp->cl_last_renewal = timestamp;
469 spin_unlock(&clp->cl_lock);
470}
471
Trond Myklebust452e9352010-07-31 14:29:06 -0400472static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
473{
474 do_renew_lease(server->nfs_client, timestamp);
475}
476
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400477struct nfs4_call_sync_data {
478 const struct nfs_server *seq_server;
479 struct nfs4_sequence_args *seq_args;
480 struct nfs4_sequence_res *seq_res;
481};
482
Chuck Levera9c92d62013-08-09 12:48:18 -0400483static void nfs4_init_sequence(struct nfs4_sequence_args *args,
484 struct nfs4_sequence_res *res, int cache_reply)
485{
486 args->sa_slot = NULL;
487 args->sa_cache_this = cache_reply;
488 args->sa_privileged = 0;
489
490 res->sr_slot = NULL;
491}
492
493static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
494{
495 args->sa_privileged = 1;
496}
497
Peng Taocb04ad22014-06-11 05:24:15 +0800498int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
499 struct nfs4_sequence_args *args,
500 struct nfs4_sequence_res *res,
501 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400502{
Chuck Lever3bd23842013-08-09 12:49:19 -0400503 struct nfs4_slot *slot;
504
505 /* slot already allocated? */
506 if (res->sr_slot != NULL)
507 goto out_start;
508
509 spin_lock(&tbl->slot_tbl_lock);
510 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
511 goto out_sleep;
512
513 slot = nfs4_alloc_slot(tbl);
514 if (IS_ERR(slot)) {
515 if (slot == ERR_PTR(-ENOMEM))
516 task->tk_timeout = HZ >> 2;
517 goto out_sleep;
518 }
519 spin_unlock(&tbl->slot_tbl_lock);
520
521 args->sa_slot = slot;
522 res->sr_slot = slot;
523
524out_start:
525 rpc_call_start(task);
526 return 0;
527
528out_sleep:
529 if (args->sa_privileged)
530 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
531 NULL, RPC_PRIORITY_PRIVILEGED);
532 else
533 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
534 spin_unlock(&tbl->slot_tbl_lock);
535 return -EAGAIN;
536}
Peng Taocb04ad22014-06-11 05:24:15 +0800537EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400538
539static int nfs40_sequence_done(struct rpc_task *task,
540 struct nfs4_sequence_res *res)
541{
542 struct nfs4_slot *slot = res->sr_slot;
543 struct nfs4_slot_table *tbl;
544
Trond Myklebustcab92c12014-01-29 12:12:15 -0500545 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400546 goto out;
547
548 tbl = slot->table;
549 spin_lock(&tbl->slot_tbl_lock);
550 if (!nfs41_wake_and_assign_slot(tbl, slot))
551 nfs4_free_slot(tbl, slot);
552 spin_unlock(&tbl->slot_tbl_lock);
553
554 res->sr_slot = NULL;
555out:
556 return 1;
557}
558
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400559#if defined(CONFIG_NFS_V4_1)
560
Trond Myklebustd185a332010-06-16 09:52:25 -0400561static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400562{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500563 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400564 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500565 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500566 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400567
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500568 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500569 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500570
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500571 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500572 /* Be nice to the server: try to ensure that the last transmitted
573 * value for highest_user_slotid <= target_highest_slotid
574 */
575 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
576 send_new_highest_used_slotid = true;
577
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500578 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500579 send_new_highest_used_slotid = false;
580 goto out_unlock;
581 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500582 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500583
584 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
585 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500586out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500587 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400588 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500589 if (send_new_highest_used_slotid)
590 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400591}
592
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500593int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400594{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500595 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500596 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400597 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500598 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500599 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400600
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500601 if (slot == NULL)
602 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400603 /* don't increment the sequence number if the task wasn't sent */
604 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400605 goto out;
606
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500607 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500608
Trond Myklebustac20d162012-12-15 15:36:07 -0500609 if (slot->interrupted) {
610 slot->interrupted = 0;
611 interrupted = true;
612 }
613
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400614 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500615 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400616 switch (res->sr_status) {
617 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400618 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500619 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500620 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500621 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500622 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500623 if (res->sr_status_flags != 0)
624 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500625 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400626 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500627 case 1:
628 /*
629 * sr_status remains 1 if an RPC level error occurred.
630 * The server may or may not have processed the sequence
631 * operation..
632 * Mark the slot as having hosted an interrupted RPC call.
633 */
634 slot->interrupted = 1;
635 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400636 case -NFS4ERR_DELAY:
637 /* The server detected a resend of the RPC call and
638 * returned NFS4ERR_DELAY as per Section 2.10.6.2
639 * of RFC5661.
640 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500641 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400642 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500643 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500644 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400645 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500646 case -NFS4ERR_BADSLOT:
647 /*
648 * The slot id we used was probably retired. Try again
649 * using a different slot id.
650 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500651 goto retry_nowait;
652 case -NFS4ERR_SEQ_MISORDERED:
653 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500654 * Was the last operation on this sequence interrupted?
655 * If so, retry after bumping the sequence number.
656 */
657 if (interrupted) {
658 ++slot->seq_nr;
659 goto retry_nowait;
660 }
661 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500662 * Could this slot have been previously retired?
663 * If so, then the server may be expecting seq_nr = 1!
664 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500665 if (slot->seq_nr != 1) {
666 slot->seq_nr = 1;
667 goto retry_nowait;
668 }
669 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500670 case -NFS4ERR_SEQ_FALSE_RETRY:
671 ++slot->seq_nr;
672 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400673 default:
674 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500675 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400676 }
677out:
678 /* The session may be reset by one of the error handlers. */
679 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400680 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500681out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500682 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500683retry_nowait:
684 if (rpc_restart_call_prepare(task)) {
685 task->tk_status = 0;
686 ret = 0;
687 }
688 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400689out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400690 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400691 goto out;
692 rpc_delay(task, NFS4_POLL_RETRY_MAX);
693 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400694}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500695EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400696
Peng Tao2c4b1312014-06-11 05:24:16 +0800697int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400698{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500699 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400700 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400701 if (!res->sr_slot->table->session)
702 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400703 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400704}
Peng Tao2c4b1312014-06-11 05:24:16 +0800705EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400706
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000707int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 struct nfs4_sequence_args *args,
709 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400710 struct rpc_task *task)
711{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400712 struct nfs4_slot *slot;
713 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400714
715 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400716 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400717 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400718 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400719
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400720 tbl = &session->fc_slot_table;
721
Trond Myklebust69d206b2012-11-22 13:21:02 -0500722 task->tk_timeout = 0;
723
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400724 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400725 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400726 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500727 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500728 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400729 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400730 }
731
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500732 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500733 if (IS_ERR(slot)) {
734 /* If out of memory, try again in 1/4 second */
735 if (slot == ERR_PTR(-ENOMEM))
736 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400737 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400738 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400739 }
740 spin_unlock(&tbl->slot_tbl_lock);
741
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500742 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400743
Chuck Levere8d92382013-08-09 12:47:51 -0400744 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500745 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400746
Benny Halevydfb4f3092010-09-24 09:17:01 -0400747 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500748 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400749 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400750 /*
751 * sr_status is only set in decode_sequence, and so will remain
752 * set to 1 if an rpc level failure occurs.
753 */
754 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400755 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400756out_success:
757 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400758 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400759out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400760 /* Privileged tasks are queued with top priority */
761 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400762 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
763 NULL, RPC_PRIORITY_PRIVILEGED);
764 else
765 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400766 spin_unlock(&tbl->slot_tbl_lock);
767 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400768}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000769EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400770
Chuck Lever5a580e02013-08-09 12:48:09 -0400771static int nfs4_setup_sequence(const struct nfs_server *server,
772 struct nfs4_sequence_args *args,
773 struct nfs4_sequence_res *res,
774 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400775{
Trond Myklebust035168a2010-06-16 09:52:26 -0400776 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400777 int ret = 0;
778
Chuck Lever3bd23842013-08-09 12:49:19 -0400779 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800780 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
781 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400782
Chuck Levere8d92382013-08-09 12:47:51 -0400783 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400784 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400785 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400786
Trond Myklebust9d12b212012-01-17 22:04:25 -0500787 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400788
Andy Adamsonce5039c2009-04-01 09:22:13 -0400789 dprintk("<-- %s status=%d\n", __func__, ret);
790 return ret;
791}
792
Andy Adamsonce5039c2009-04-01 09:22:13 -0400793static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
794{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400795 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400796 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400797
Trond Myklebust035168a2010-06-16 09:52:26 -0400798 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
799
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400800 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400801}
802
Andy Adamson69ab40c2009-04-01 09:22:19 -0400803static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
804{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400805 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400806
Trond Myklebust14516c32010-07-31 14:29:06 -0400807 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400808}
809
Trond Myklebust17280172012-03-11 13:11:00 -0400810static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400811 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400812 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400813};
814
Chuck Lever3bd23842013-08-09 12:49:19 -0400815#else /* !CONFIG_NFS_V4_1 */
816
Chuck Lever5a580e02013-08-09 12:48:09 -0400817static int nfs4_setup_sequence(const struct nfs_server *server,
818 struct nfs4_sequence_args *args,
819 struct nfs4_sequence_res *res,
820 struct rpc_task *task)
821{
Peng Taocb04ad22014-06-11 05:24:15 +0800822 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
823 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400824}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400825
Peng Tao2c4b1312014-06-11 05:24:16 +0800826int nfs4_sequence_done(struct rpc_task *task,
827 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400828{
Chuck Lever3bd23842013-08-09 12:49:19 -0400829 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400830}
Peng Tao2c4b1312014-06-11 05:24:16 +0800831EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400832
833#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400834
Chuck Lever9915ea72013-08-09 12:48:27 -0400835static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
836{
837 struct nfs4_call_sync_data *data = calldata;
838 nfs4_setup_sequence(data->seq_server,
839 data->seq_args, data->seq_res, task);
840}
841
842static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
843{
844 struct nfs4_call_sync_data *data = calldata;
845 nfs4_sequence_done(task, data->seq_res);
846}
847
848static const struct rpc_call_ops nfs40_call_sync_ops = {
849 .rpc_call_prepare = nfs40_call_sync_prepare,
850 .rpc_call_done = nfs40_call_sync_done,
851};
852
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400853static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400854 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500855 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100856 struct nfs4_sequence_args *args,
857 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400858{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 int ret;
860 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400861 struct nfs_client *clp = server->nfs_client;
862 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100863 .seq_server = server,
864 .seq_args = args,
865 .seq_res = res,
866 };
867 struct rpc_task_setup task_setup = {
868 .rpc_client = clnt,
869 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400870 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100871 .callback_data = &data
872 };
873
874 task = rpc_run_task(&task_setup);
875 if (IS_ERR(task))
876 ret = PTR_ERR(task);
877 else {
878 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400879 rpc_put_task(task);
880 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 return ret;
882}
883
Bryan Schumaker7c513052011-03-24 17:12:24 +0000884int nfs4_call_sync(struct rpc_clnt *clnt,
885 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000886 struct rpc_message *msg,
887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
889 int cache_reply)
890{
Chuck Levera9c92d62013-08-09 12:48:18 -0400891 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400892 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000893}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
896{
897 struct nfs_inode *nfsi = NFS_I(dir);
898
899 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400900 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700901 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700903 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000904 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_unlock(&dir->i_lock);
906}
907
908struct nfs4_opendata {
909 struct kref kref;
910 struct nfs_openargs o_arg;
911 struct nfs_openres o_res;
912 struct nfs_open_confirmargs c_arg;
913 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500914 struct nfs4_string owner_name;
915 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400917 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400919 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct nfs4_state_owner *owner;
921 struct nfs4_state *state;
922 struct iattr attrs;
923 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400924 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400925 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400926 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400927 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int cancelled;
929};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400930
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400931static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
932 int err, struct nfs4_exception *exception)
933{
934 if (err != -EINVAL)
935 return false;
936 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
937 return false;
938 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
939 exception->retry = 1;
940 return true;
941}
942
943static enum open_claim_type4
944nfs4_map_atomic_open_claim(struct nfs_server *server,
945 enum open_claim_type4 claim)
946{
947 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
948 return claim;
949 switch (claim) {
950 default:
951 return claim;
952 case NFS4_OPEN_CLAIM_FH:
953 return NFS4_OPEN_CLAIM_NULL;
954 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
955 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
956 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
957 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
958 }
959}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100962{
963 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400964 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100965 p->o_res.seqid = p->o_arg.seqid;
966 p->c_res.seqid = p->c_arg.seqid;
967 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400968 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500970 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971}
972
Al Viro82a2c1b2011-06-22 18:30:55 -0400973static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100974 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400975 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400976 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400977 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400978 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100979{
Al Viro82a2c1b2011-06-22 18:30:55 -0400980 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100981 struct inode *dir = parent->d_inode;
982 struct nfs_server *server = NFS_SERVER(dir);
983 struct nfs4_opendata *p;
984
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400985 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986 if (p == NULL)
987 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400988
989 p->f_label = nfs4_label_alloc(server, gfp_mask);
990 if (IS_ERR(p->f_label))
991 goto err_free_p;
992
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400993 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100994 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400995 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400996 nfs_sb_active(dentry->d_sb);
997 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100998 p->dir = parent;
999 p->owner = sp;
1000 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 p->o_arg.open_flags = flags;
1002 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001003 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1004 * will return permission denied for all bits until close */
1005 if (!(flags & O_EXCL)) {
1006 /* ask server to check for all possible rights as results
1007 * are cached */
1008 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1009 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001010 }
David Howells7539bba2006-08-22 20:06:09 -04001011 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001012 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1013 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001014 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001015 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001016 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001017 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001018 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001019 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1020 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001021 case NFS4_OPEN_CLAIM_NULL:
1022 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1023 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1024 p->o_arg.fh = NFS_FH(dir);
1025 break;
1026 case NFS4_OPEN_CLAIM_PREVIOUS:
1027 case NFS4_OPEN_CLAIM_FH:
1028 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1029 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1030 p->o_arg.fh = NFS_FH(dentry->d_inode);
1031 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001032 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001033 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001034
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001035 p->o_arg.u.attrs = &p->attrs;
1036 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001037
1038 verf[0] = jiffies;
1039 verf[1] = current->pid;
1040 memcpy(p->o_arg.u.verifier.data, verf,
1041 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001042 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001043 p->c_arg.fh = &p->o_res.fh;
1044 p->c_arg.stateid = &p->o_res.stateid;
1045 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001046 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001047 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001049
1050err_free_label:
1051 nfs4_label_free(p->f_label);
1052err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001053 kfree(p);
1054err:
1055 dput(parent);
1056 return NULL;
1057}
1058
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001059static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001060{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001061 struct nfs4_opendata *p = container_of(kref,
1062 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001063 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001064
1065 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001066 if (p->state != NULL)
1067 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001068 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001069
1070 nfs4_label_free(p->f_label);
1071
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001072 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001073 dput(p->dentry);
1074 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001075 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001076 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001077 kfree(p);
1078}
1079
1080static void nfs4_opendata_put(struct nfs4_opendata *p)
1081{
1082 if (p != NULL)
1083 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001084}
1085
Trond Myklebust06f814a2006-01-03 09:55:07 +01001086static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1087{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001088 int ret;
1089
Trond Myklebust06f814a2006-01-03 09:55:07 +01001090 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001091 return ret;
1092}
1093
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001094static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001095{
1096 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001097
Trond Myklebust536e43d2012-01-17 22:04:26 -05001098 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001099 goto out;
1100 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001101 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001102 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1103 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001104 break;
1105 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001106 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1107 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001108 break;
1109 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001110 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1111 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001112 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001113out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001114 return ret;
1115}
1116
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001117static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001119 if (delegation == NULL)
1120 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001121 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001123 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1124 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001125 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001126 return 1;
1127}
1128
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001129static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001130{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001131 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001132 case FMODE_WRITE:
1133 state->n_wronly++;
1134 break;
1135 case FMODE_READ:
1136 state->n_rdonly++;
1137 break;
1138 case FMODE_READ|FMODE_WRITE:
1139 state->n_rdwr++;
1140 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001142}
1143
Trond Myklebust4f14c192014-02-12 19:15:06 -05001144static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001145{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001146 struct nfs_client *clp = state->owner->so_server->nfs_client;
1147 bool need_recover = false;
1148
1149 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1150 need_recover = true;
1151 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1152 need_recover = true;
1153 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1154 need_recover = true;
1155 if (need_recover)
1156 nfs4_state_mark_reclaim_nograce(clp, state);
1157}
1158
Trond Myklebuste999e802014-02-10 18:20:47 -05001159static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1160 nfs4_stateid *stateid)
1161{
1162 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1163 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001164 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1165 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001166 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001167 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001168 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1169 return true;
1170 return false;
1171}
1172
Trond Myklebust226056c2014-02-11 10:41:07 -05001173static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1174 nfs4_stateid *stateid, fmode_t fmode)
1175{
1176 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1177 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1178 case FMODE_WRITE:
1179 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1180 break;
1181 case FMODE_READ:
1182 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1183 break;
1184 case 0:
1185 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1186 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1187 clear_bit(NFS_OPEN_STATE, &state->flags);
1188 }
1189 if (stateid == NULL)
1190 return;
1191 if (!nfs_need_update_open_stateid(state, stateid))
1192 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001193 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001194 nfs4_stateid_copy(&state->stateid, stateid);
1195 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001196}
1197
1198static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1199{
1200 write_seqlock(&state->seqlock);
1201 nfs_clear_open_stateid_locked(state, stateid, fmode);
1202 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001203 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1204 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001205}
1206
Trond Myklebust003707c2007-07-05 18:07:55 -04001207static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1208{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001209 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001210 case FMODE_READ:
1211 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1212 break;
1213 case FMODE_WRITE:
1214 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1215 break;
1216 case FMODE_READ|FMODE_WRITE:
1217 set_bit(NFS_O_RDWR_STATE, &state->flags);
1218 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001219 if (!nfs_need_update_open_stateid(state, stateid))
1220 return;
1221 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1222 nfs4_stateid_copy(&state->stateid, stateid);
1223 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001224}
1225
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001226static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001228 /*
1229 * Protect the call to nfs4_state_set_mode_locked and
1230 * serialise the stateid update
1231 */
1232 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001233 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001234 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001235 set_bit(NFS_DELEGATED_STATE, &state->flags);
1236 }
1237 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001238 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001239 write_sequnlock(&state->seqlock);
1240 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001241 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001242 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001246{
1247 struct nfs_inode *nfsi = NFS_I(state->inode);
1248 struct nfs_delegation *deleg_cur;
1249 int ret = 0;
1250
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001251 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001252
1253 rcu_read_lock();
1254 deleg_cur = rcu_dereference(nfsi->delegation);
1255 if (deleg_cur == NULL)
1256 goto no_delegation;
1257
1258 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001259 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001260 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001261 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001262 goto no_delegation_unlock;
1263
1264 if (delegation == NULL)
1265 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001266 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001267 goto no_delegation_unlock;
1268
Trond Myklebustb7391f42008-12-23 15:21:52 -05001269 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001270 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001271 ret = 1;
1272no_delegation_unlock:
1273 spin_unlock(&deleg_cur->lock);
1274no_delegation:
1275 rcu_read_unlock();
1276
1277 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001279 ret = 1;
1280 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001281 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1282 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001283
1284 return ret;
1285}
1286
1287
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001288static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001289{
1290 struct nfs_delegation *delegation;
1291
1292 rcu_read_lock();
1293 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001294 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001295 rcu_read_unlock();
1296 return;
1297 }
1298 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001299 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001300}
1301
Trond Myklebust6ee41262007-07-08 14:11:36 -04001302static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001303{
1304 struct nfs4_state *state = opendata->state;
1305 struct nfs_inode *nfsi = NFS_I(state->inode);
1306 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001307 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001308 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001309 nfs4_stateid stateid;
1310 int ret = -EAGAIN;
1311
Trond Myklebustaac00a82007-07-05 19:02:21 -04001312 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001313 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001314 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001315 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001317 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001318 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001319 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001320 rcu_read_lock();
1321 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001322 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001323 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001324 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001325 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001326 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001327 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001328 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001329 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001330 if (!opendata->is_recover) {
1331 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1332 if (ret != 0)
1333 goto out;
1334 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001335 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001336
1337 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001338 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001339 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001340 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001341out:
1342 return ERR_PTR(ret);
1343out_return_state:
1344 atomic_inc(&state->count);
1345 return state;
1346}
1347
Andy Adamsone23008e2012-10-02 21:07:32 -04001348static void
1349nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1350{
1351 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1352 struct nfs_delegation *delegation;
1353 int delegation_flags = 0;
1354
1355 rcu_read_lock();
1356 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1357 if (delegation)
1358 delegation_flags = delegation->flags;
1359 rcu_read_unlock();
1360 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1361 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1362 "returning a delegation for "
1363 "OPEN(CLAIM_DELEGATE_CUR)\n",
1364 clp->cl_hostname);
1365 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1366 nfs_inode_set_delegation(state->inode,
1367 data->owner->so_cred,
1368 &data->o_res);
1369 else
1370 nfs_inode_reclaim_delegation(state->inode,
1371 data->owner->so_cred,
1372 &data->o_res);
1373}
1374
1375/*
1376 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1377 * and update the nfs4_state.
1378 */
1379static struct nfs4_state *
1380_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1381{
1382 struct inode *inode = data->state->inode;
1383 struct nfs4_state *state = data->state;
1384 int ret;
1385
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001386 if (!data->rpc_done) {
1387 if (data->rpc_status) {
1388 ret = data->rpc_status;
1389 goto err;
1390 }
1391 /* cached opens have already been processed */
1392 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001393 }
1394
Andy Adamsone23008e2012-10-02 21:07:32 -04001395 ret = nfs_refresh_inode(inode, &data->f_attr);
1396 if (ret)
1397 goto err;
1398
1399 if (data->o_res.delegation_type != 0)
1400 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001401update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001402 update_open_stateid(state, &data->o_res.stateid, NULL,
1403 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001404 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001405
1406 return state;
1407err:
1408 return ERR_PTR(ret);
1409
1410}
1411
1412static struct nfs4_state *
1413_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001414{
1415 struct inode *inode;
1416 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001417 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418
Trond Myklebustaac00a82007-07-05 19:02:21 -04001419 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001420 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001421 goto out;
1422 }
1423
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001424 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001425 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001426 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001427 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001428 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001429 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001430 goto err;
1431 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432 state = nfs4_get_open_state(inode, data->owner);
1433 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001434 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001435 if (data->o_res.delegation_type != 0)
1436 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001437 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001438 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001440out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001441 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001443err_put_inode:
1444 iput(inode);
1445err:
1446 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447}
1448
Andy Adamsone23008e2012-10-02 21:07:32 -04001449static struct nfs4_state *
1450nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1451{
1452 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1453 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1454 return _nfs4_opendata_to_nfs4_state(data);
1455}
1456
Trond Myklebust864472e2006-01-03 09:55:15 +01001457static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1458{
1459 struct nfs_inode *nfsi = NFS_I(state->inode);
1460 struct nfs_open_context *ctx;
1461
1462 spin_lock(&state->inode->i_lock);
1463 list_for_each_entry(ctx, &nfsi->open_files, list) {
1464 if (ctx->state != state)
1465 continue;
1466 get_nfs_open_context(ctx);
1467 spin_unlock(&state->inode->i_lock);
1468 return ctx;
1469 }
1470 spin_unlock(&state->inode->i_lock);
1471 return ERR_PTR(-ENOENT);
1472}
1473
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001474static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1475 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001476{
1477 struct nfs4_opendata *opendata;
1478
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001479 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001480 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001481 if (opendata == NULL)
1482 return ERR_PTR(-ENOMEM);
1483 opendata->state = state;
1484 atomic_inc(&state->count);
1485 return opendata;
1486}
1487
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001488static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001489{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001490 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001491 int ret;
1492
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001493 opendata->o_arg.open_flags = 0;
1494 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001495 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1496 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1497 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001498 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001499 if (ret != 0)
1500 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001501 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001502 if (IS_ERR(newstate))
1503 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001504 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001505 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001506 return 0;
1507}
1508
1509static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1510{
Trond Myklebust864472e2006-01-03 09:55:15 +01001511 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001512 int ret;
1513
Trond Myklebust4f14c192014-02-12 19:15:06 -05001514 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1515 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1516 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1517 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001518 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001519 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001520 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001521 smp_rmb();
1522 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001523 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001524 if (ret != 0)
1525 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001526 if (newstate != state)
1527 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001528 }
1529 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001530 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001531 if (ret != 0)
1532 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001533 if (newstate != state)
1534 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001535 }
1536 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001537 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001538 if (ret != 0)
1539 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001540 if (newstate != state)
1541 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001542 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001543 /*
1544 * We may have performed cached opens for all three recoveries.
1545 * Check if we need to update the current stateid.
1546 */
1547 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001548 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001549 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001550 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001551 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001552 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001553 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001554 return 0;
1555}
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557/*
1558 * OPEN_RECLAIM:
1559 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001561static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001563 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001564 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001565 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 int status;
1567
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001568 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1569 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001570 if (IS_ERR(opendata))
1571 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001572 rcu_read_lock();
1573 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001574 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001575 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001576 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001577 opendata->o_arg.u.delegation_type = delegation_type;
1578 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001579 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return status;
1581}
1582
Trond Myklebust539cd032007-06-05 11:46:42 -04001583static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct nfs_server *server = NFS_SERVER(state->inode);
1586 struct nfs4_exception exception = { };
1587 int err;
1588 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001589 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001590 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001591 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1592 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001593 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001594 break;
1595 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 } while (exception.retry);
1597 return err;
1598}
1599
Trond Myklebust864472e2006-01-03 09:55:15 +01001600static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1601{
1602 struct nfs_open_context *ctx;
1603 int ret;
1604
1605 ctx = nfs4_state_find_open_context(state);
1606 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001607 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001608 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001609 put_nfs_open_context(ctx);
1610 return ret;
1611}
1612
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001613static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001615 switch (err) {
1616 default:
1617 printk(KERN_ERR "NFS: %s: unhandled error "
1618 "%d.\n", __func__, err);
1619 case 0:
1620 case -ENOENT:
1621 case -ESTALE:
1622 break;
1623 case -NFS4ERR_BADSESSION:
1624 case -NFS4ERR_BADSLOT:
1625 case -NFS4ERR_BAD_HIGH_SLOT:
1626 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1627 case -NFS4ERR_DEADSESSION:
1628 set_bit(NFS_DELEGATED_STATE, &state->flags);
1629 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1630 return -EAGAIN;
1631 case -NFS4ERR_STALE_CLIENTID:
1632 case -NFS4ERR_STALE_STATEID:
1633 set_bit(NFS_DELEGATED_STATE, &state->flags);
1634 case -NFS4ERR_EXPIRED:
1635 /* Don't recall a delegation if it was lost */
1636 nfs4_schedule_lease_recovery(server->nfs_client);
1637 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001638 case -NFS4ERR_MOVED:
1639 nfs4_schedule_migration_recovery(server);
1640 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001641 case -NFS4ERR_LEASE_MOVED:
1642 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1643 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001644 case -NFS4ERR_DELEG_REVOKED:
1645 case -NFS4ERR_ADMIN_REVOKED:
1646 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001647 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001648 nfs_inode_find_state_and_recover(state->inode,
1649 stateid);
1650 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001651 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001652 case -NFS4ERR_DELAY:
1653 case -NFS4ERR_GRACE:
1654 set_bit(NFS_DELEGATED_STATE, &state->flags);
1655 ssleep(1);
1656 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001657 case -ENOMEM:
1658 case -NFS4ERR_DENIED:
1659 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1660 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return err;
1663}
1664
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001665int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1666{
1667 struct nfs_server *server = NFS_SERVER(state->inode);
1668 struct nfs4_opendata *opendata;
1669 int err;
1670
1671 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1672 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1673 if (IS_ERR(opendata))
1674 return PTR_ERR(opendata);
1675 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1676 err = nfs4_open_recover(opendata, state);
1677 nfs4_opendata_put(opendata);
1678 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1679}
1680
Chuck Leverbe05c862013-08-09 12:49:47 -04001681static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1682{
1683 struct nfs4_opendata *data = calldata;
1684
Peng Taocb04ad22014-06-11 05:24:15 +08001685 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1686 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001687}
1688
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001689static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1690{
1691 struct nfs4_opendata *data = calldata;
1692
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001693 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001694
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001695 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001696 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001697 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001698 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001699 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001700 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001701 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001702}
1703
1704static void nfs4_open_confirm_release(void *calldata)
1705{
1706 struct nfs4_opendata *data = calldata;
1707 struct nfs4_state *state = NULL;
1708
1709 /* If this request hasn't been cancelled, do nothing */
1710 if (data->cancelled == 0)
1711 goto out_free;
1712 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001713 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001714 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001715 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001716 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001717 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001718out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001719 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001720}
1721
1722static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001723 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001724 .rpc_call_done = nfs4_open_confirm_done,
1725 .rpc_release = nfs4_open_confirm_release,
1726};
1727
1728/*
1729 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1730 */
1731static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1732{
1733 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1734 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001735 struct rpc_message msg = {
1736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1737 .rpc_argp = &data->c_arg,
1738 .rpc_resp = &data->c_res,
1739 .rpc_cred = data->owner->so_cred,
1740 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001741 struct rpc_task_setup task_setup_data = {
1742 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001743 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001744 .callback_ops = &nfs4_open_confirm_ops,
1745 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001746 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001747 .flags = RPC_TASK_ASYNC,
1748 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 int status;
1750
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001751 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001752 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001753 data->rpc_done = 0;
1754 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001755 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001756 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001757 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001758 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001759 status = nfs4_wait_for_completion_rpc_task(task);
1760 if (status != 0) {
1761 data->cancelled = 1;
1762 smp_wmb();
1763 } else
1764 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001765 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return status;
1767}
1768
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001769static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 struct nfs4_opendata *data = calldata;
1772 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001773 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001774
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001775 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001776 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001777 /*
1778 * Check if we still need to send an OPEN call, or if we can use
1779 * a delegation instead.
1780 */
1781 if (data->state != NULL) {
1782 struct nfs_delegation *delegation;
1783
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001784 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001785 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001786 rcu_read_lock();
1787 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001788 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001789 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001790 can_open_delegated(delegation, data->o_arg.fmode))
1791 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001792 rcu_read_unlock();
1793 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001794 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001795 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001796 switch (data->o_arg.claim) {
1797 case NFS4_OPEN_CLAIM_PREVIOUS:
1798 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1799 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001800 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001801 case NFS4_OPEN_CLAIM_FH:
1802 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001803 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1804 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001805 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001806 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001807 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001808 &data->o_res.seq_res,
1809 task) != 0)
1810 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001811
1812 /* Set the create mode (note dependency on the session type) */
1813 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1814 if (data->o_arg.open_flags & O_EXCL) {
1815 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1816 if (nfs4_has_persistent_session(clp))
1817 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1818 else if (clp->cl_mvops->minor_version > 0)
1819 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1820 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001821 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001822unlock_no_action:
1823 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001824out_no_action:
1825 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001826out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001827 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001828}
1829
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001830static void nfs4_open_done(struct rpc_task *task, void *calldata)
1831{
1832 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001834 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001835
Trond Myklebust14516c32010-07-31 14:29:06 -04001836 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1837 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001838
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001839 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001840 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1841 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001842 case S_IFREG:
1843 break;
1844 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001846 break;
1847 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001848 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001849 break;
1850 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001851 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001852 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001853 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001854 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001855 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1856 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001857 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001858 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001859}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001860
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001861static void nfs4_open_release(void *calldata)
1862{
1863 struct nfs4_opendata *data = calldata;
1864 struct nfs4_state *state = NULL;
1865
1866 /* If this request hasn't been cancelled, do nothing */
1867 if (data->cancelled == 0)
1868 goto out_free;
1869 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001870 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001871 goto out_free;
1872 /* In case we need an open_confirm, no cleanup! */
1873 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1874 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001875 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001876 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001877 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001878out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001879 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001880}
1881
1882static const struct rpc_call_ops nfs4_open_ops = {
1883 .rpc_call_prepare = nfs4_open_prepare,
1884 .rpc_call_done = nfs4_open_done,
1885 .rpc_release = nfs4_open_release,
1886};
1887
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001888static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001889{
1890 struct inode *dir = data->dir->d_inode;
1891 struct nfs_server *server = NFS_SERVER(dir);
1892 struct nfs_openargs *o_arg = &data->o_arg;
1893 struct nfs_openres *o_res = &data->o_res;
1894 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001895 struct rpc_message msg = {
1896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1897 .rpc_argp = o_arg,
1898 .rpc_resp = o_res,
1899 .rpc_cred = data->owner->so_cred,
1900 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001901 struct rpc_task_setup task_setup_data = {
1902 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001903 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001904 .callback_ops = &nfs4_open_ops,
1905 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001906 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001907 .flags = RPC_TASK_ASYNC,
1908 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001909 int status;
1910
Chuck Levera9c92d62013-08-09 12:48:18 -04001911 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001912 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001913 data->rpc_done = 0;
1914 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001915 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001916 data->is_recover = 0;
1917 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001918 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001919 data->is_recover = 1;
1920 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001921 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001922 if (IS_ERR(task))
1923 return PTR_ERR(task);
1924 status = nfs4_wait_for_completion_rpc_task(task);
1925 if (status != 0) {
1926 data->cancelled = 1;
1927 smp_wmb();
1928 } else
1929 status = data->rpc_status;
1930 rpc_put_task(task);
1931
1932 return status;
1933}
1934
1935static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1936{
1937 struct inode *dir = data->dir->d_inode;
1938 struct nfs_openres *o_res = &data->o_res;
1939 int status;
1940
1941 status = nfs4_run_open_task(data, 1);
1942 if (status != 0 || !data->rpc_done)
1943 return status;
1944
Trond Myklebust6926afd2012-01-07 13:22:46 -05001945 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1946
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001947 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1948 status = _nfs4_proc_open_confirm(data);
1949 if (status != 0)
1950 return status;
1951 }
1952
1953 return status;
1954}
1955
Trond Myklebustf3792d62014-07-10 08:54:32 -04001956/*
1957 * Additional permission checks in order to distinguish between an
1958 * open for read, and an open for execute. This works around the
1959 * fact that NFSv4 OPEN treats read and execute permissions as being
1960 * the same.
1961 * Note that in the non-execute case, we want to turn off permission
1962 * checking if we just created a new file (POSIX open() semantics).
1963 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001964static int nfs4_opendata_access(struct rpc_cred *cred,
1965 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001966 struct nfs4_state *state, fmode_t fmode,
1967 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001968{
1969 struct nfs_access_entry cache;
1970 u32 mask;
1971
1972 /* access call failed or for some reason the server doesn't
1973 * support any access modes -- defer access call until later */
1974 if (opendata->o_res.access_supported == 0)
1975 return 0;
1976
1977 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04001978 /*
1979 * Use openflags to check for exec, because fmode won't
1980 * always have FMODE_EXEC set when file open for exec.
1981 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001982 if (openflags & __FMODE_EXEC) {
1983 /* ONLY check for exec rights */
1984 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04001985 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001986 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001987
1988 cache.cred = cred;
1989 cache.jiffies = jiffies;
1990 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1991 nfs_access_add_cache(state->inode, &cache);
1992
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001993 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001994 return 0;
1995
1996 /* even though OPEN succeeded, access is denied. Close the file */
1997 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001998 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001999}
2000
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002001/*
2002 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2003 */
2004static int _nfs4_proc_open(struct nfs4_opendata *data)
2005{
2006 struct inode *dir = data->dir->d_inode;
2007 struct nfs_server *server = NFS_SERVER(dir);
2008 struct nfs_openargs *o_arg = &data->o_arg;
2009 struct nfs_openres *o_res = &data->o_res;
2010 int status;
2011
2012 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002013 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002014 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002015 if (status != 0) {
2016 if (status == -NFS4ERR_BADNAME &&
2017 !(o_arg->open_flags & O_CREAT))
2018 return -ENOENT;
2019 return status;
2020 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002021
Trond Myklebust6926afd2012-01-07 13:22:46 -05002022 nfs_fattr_map_and_free_names(server, &data->f_attr);
2023
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002024 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002025 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002026 if (o_arg->open_flags & O_EXCL)
2027 data->file_created = 1;
2028 else if (o_res->cinfo.before != o_res->cinfo.after)
2029 data->file_created = 1;
2030 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002031 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2032 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002034 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002036 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
2038 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002039 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
Andy Adamsond83217c2011-03-01 01:34:17 +00002043static int nfs4_recover_expired_lease(struct nfs_server *server)
2044{
2045 return nfs4_client_recover_expired_lease(server->nfs_client);
2046}
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048/*
2049 * OPEN_EXPIRED:
2050 * reclaim state on the server after a network partition.
2051 * Assumes caller holds the appropriate lock
2052 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002053static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002055 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002056 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002058 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002059 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002060 if (IS_ERR(opendata))
2061 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002062 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002063 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002064 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002065 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002066 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002069static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002070{
Trond Myklebust539cd032007-06-05 11:46:42 -04002071 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002072 struct nfs4_exception exception = { };
2073 int err;
2074
2075 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002076 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002077 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002078 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2079 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002080 switch (err) {
2081 default:
2082 goto out;
2083 case -NFS4ERR_GRACE:
2084 case -NFS4ERR_DELAY:
2085 nfs4_handle_exception(server, err, &exception);
2086 err = 0;
2087 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002088 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002089out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002090 return err;
2091}
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2094{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002096 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Trond Myklebust864472e2006-01-03 09:55:15 +01002098 ctx = nfs4_state_find_open_context(state);
2099 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002100 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002101 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002102 put_nfs_open_context(ctx);
2103 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002106static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2107{
2108 nfs_remove_bad_delegation(state->inode);
2109 write_seqlock(&state->seqlock);
2110 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2111 write_sequnlock(&state->seqlock);
2112 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2113}
2114
2115static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2116{
2117 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2118 nfs_finish_clear_delegation_stateid(state);
2119}
2120
2121static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2122{
2123 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2124 nfs40_clear_delegation_stateid(state);
2125 return nfs4_open_expired(sp, state);
2126}
2127
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002128#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002129static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002130{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002131 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002132 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002133 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002134 struct rpc_cred *cred;
2135 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002136
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002137 /* Get the delegation credential for use by test/free_stateid */
2138 rcu_read_lock();
2139 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002140 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002141 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002142 return;
2143 }
2144
2145 nfs4_stateid_copy(&stateid, &delegation->stateid);
2146 cred = get_rpccred(delegation->cred);
2147 rcu_read_unlock();
2148 status = nfs41_test_stateid(server, &stateid, cred);
2149 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002150
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002151 if (status != NFS_OK) {
2152 /* Free the stateid unless the server explicitly
2153 * informs us the stateid is unrecognized. */
2154 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002155 nfs41_free_stateid(server, &stateid, cred);
2156 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002157 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002158
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002159 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002160}
2161
2162/**
2163 * nfs41_check_open_stateid - possibly free an open stateid
2164 *
2165 * @state: NFSv4 state for an inode
2166 *
2167 * Returns NFS_OK if recovery for this stateid is now finished.
2168 * Otherwise a negative NFS4ERR value is returned.
2169 */
2170static int nfs41_check_open_stateid(struct nfs4_state *state)
2171{
2172 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002173 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002174 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002175 int status;
2176
2177 /* If a state reset has been done, test_stateid is unneeded */
2178 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2179 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2180 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2181 return -NFS4ERR_BAD_STATEID;
2182
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002183 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002184 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002185 if (status != NFS_OK) {
2186 /* Free the stateid unless the server explicitly
2187 * informs us the stateid is unrecognized. */
2188 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002189 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002190
2191 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2192 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2193 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002194 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002195 }
2196 return status;
2197}
2198
2199static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2200{
Chuck Levereb64cf92012-07-11 16:30:05 -04002201 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002202
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002203 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002204 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002205 if (status != NFS_OK)
2206 status = nfs4_open_expired(sp, state);
2207 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002208}
2209#endif
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002212 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2213 * fields corresponding to attributes that were used to store the verifier.
2214 * Make sure we clobber those fields in the later setattr call
2215 */
2216static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2217{
2218 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2219 !(sattr->ia_valid & ATTR_ATIME_SET))
2220 sattr->ia_valid |= ATTR_ATIME;
2221
2222 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2223 !(sattr->ia_valid & ATTR_MTIME_SET))
2224 sattr->ia_valid |= ATTR_MTIME;
2225}
2226
Trond Myklebustc21443c2013-02-07 14:26:21 -05002227static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2228 fmode_t fmode,
2229 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002230 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002231{
2232 struct nfs4_state_owner *sp = opendata->owner;
2233 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002234 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002235 struct nfs4_state *state;
2236 unsigned int seq;
2237 int ret;
2238
2239 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2240
2241 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002242 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002243 goto out;
2244
2245 state = nfs4_opendata_to_nfs4_state(opendata);
2246 ret = PTR_ERR(state);
2247 if (IS_ERR(state))
2248 goto out;
2249 if (server->caps & NFS_CAP_POSIX_LOCK)
2250 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2251
Trond Myklebust275bb302013-05-29 13:11:28 -04002252 dentry = opendata->dentry;
2253 if (dentry->d_inode == NULL) {
2254 /* FIXME: Is this d_drop() ever needed? */
2255 d_drop(dentry);
2256 dentry = d_add_unique(dentry, igrab(state->inode));
2257 if (dentry == NULL) {
2258 dentry = opendata->dentry;
2259 } else if (dentry != ctx->dentry) {
2260 dput(ctx->dentry);
2261 ctx->dentry = dget(dentry);
2262 }
2263 nfs_set_verifier(dentry,
2264 nfs_save_change_attribute(opendata->dir->d_inode));
2265 }
2266
Trond Myklebustc21443c2013-02-07 14:26:21 -05002267 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2268 if (ret != 0)
2269 goto out;
2270
Trond Myklebust3efb9722013-05-29 13:17:04 -04002271 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002272 if (dentry->d_inode == state->inode) {
2273 nfs_inode_attach_open_context(ctx);
2274 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2275 nfs4_schedule_stateid_recovery(server, state);
2276 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002277out:
2278 return ret;
2279}
2280
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002281/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002282 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 */
Andy Adamson82be4172012-05-23 05:02:35 -04002284static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002285 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002286 int flags,
2287 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002288 struct nfs4_label *label,
2289 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 struct nfs4_state_owner *sp;
2292 struct nfs4_state *state = NULL;
2293 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002294 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002295 struct dentry *dentry = ctx->dentry;
2296 struct rpc_cred *cred = ctx->cred;
2297 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2298 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002299 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002300 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002301 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002305 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2306 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2308 goto out_err;
2309 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002310 status = nfs4_recover_expired_lease(server);
2311 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002312 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002313 if (dentry->d_inode != NULL)
2314 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002315 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002316 if (dentry->d_inode)
2317 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002318 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002319 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002320 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002321 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
David Quigley14c43f72013-05-22 12:50:43 -04002323 if (label) {
2324 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2325 if (IS_ERR(olabel)) {
2326 status = PTR_ERR(olabel);
2327 goto err_opendata_put;
2328 }
2329 }
2330
Trond Myklebuste911b812014-03-26 13:24:37 -07002331 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2332 if (!opendata->f_attr.mdsthreshold) {
2333 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2334 if (!opendata->f_attr.mdsthreshold)
2335 goto err_free_label;
2336 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002337 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002338 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002339 if (dentry->d_inode != NULL)
2340 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002341
Trond Myklebust3efb9722013-05-29 13:17:04 -04002342 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002343 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002344 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002345 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002346
Trond Myklebust549b19c2013-04-16 18:42:34 -04002347 if ((opendata->o_arg.open_flags & O_EXCL) &&
2348 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002349 nfs4_exclusive_attrset(opendata, sattr);
2350
2351 nfs_fattr_init(opendata->o_res.f_attr);
2352 status = nfs4_do_setattr(state->inode, cred,
2353 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002354 state, label, olabel);
2355 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002356 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002357 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002358 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002359 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002360 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002361 if (opendata->file_created)
2362 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002363
Trond Myklebuste911b812014-03-26 13:24:37 -07002364 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002365 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002366 opendata->f_attr.mdsthreshold = NULL;
2367 }
Andy Adamson82be4172012-05-23 05:02:35 -04002368
David Quigley14c43f72013-05-22 12:50:43 -04002369 nfs4_label_free(olabel);
2370
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002371 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002374err_free_label:
2375 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002376err_opendata_put:
2377 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002378err_put_state_owner:
2379 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return status;
2382}
2383
2384
Andy Adamson82be4172012-05-23 05:02:35 -04002385static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002386 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002387 int flags,
2388 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002389 struct nfs4_label *label,
2390 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002392 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 struct nfs4_exception exception = { };
2394 struct nfs4_state *res;
2395 int status;
2396
2397 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002398 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002399 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002400 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (status == 0)
2402 break;
2403 /* NOTE: BAD_SEQID means the server and client disagree about the
2404 * book-keeping w.r.t. state-changing operations
2405 * (OPEN/CLOSE/LOCK/LOCKU...)
2406 * It is actually a sign of a bug on the client or on the server.
2407 *
2408 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002409 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 * have unhashed the old state_owner for us, and that we can
2411 * therefore safely retry using a new one. We should still warn
2412 * the user though...
2413 */
2414 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002415 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002416 " returned a bad sequence-id error!\n",
2417 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 exception.retry = 1;
2419 continue;
2420 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002421 /*
2422 * BAD_STATEID on OPEN means that the server cancelled our
2423 * state before it received the OPEN_CONFIRM.
2424 * Recover by retrying the request as per the discussion
2425 * on Page 181 of RFC3530.
2426 */
2427 if (status == -NFS4ERR_BAD_STATEID) {
2428 exception.retry = 1;
2429 continue;
2430 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002431 if (status == -EAGAIN) {
2432 /* We must have found a delegation */
2433 exception.retry = 1;
2434 continue;
2435 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002436 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2437 continue;
2438 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 status, &exception));
2440 } while (exception.retry);
2441 return res;
2442}
2443
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002444static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2445 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002446 struct nfs4_state *state, struct nfs4_label *ilabel,
2447 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002449 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002451 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 .iap = sattr,
2453 .server = server,
2454 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002455 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 };
2457 struct nfs_setattrres res = {
2458 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002459 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 .server = server,
2461 };
2462 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2464 .rpc_argp = &arg,
2465 .rpc_resp = &res,
2466 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002468 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002469 fmode_t fmode;
2470 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002471 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
David Quigleyaa9c2662013-05-22 12:50:44 -04002473 arg.bitmask = nfs4_bitmask(server, ilabel);
2474 if (ilabel)
2475 arg.bitmask = nfs4_bitmask(server, olabel);
2476
Trond Myklebust0e574af2005-10-27 22:12:38 -04002477 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Trond Myklebustee3ae842013-04-29 10:35:36 -04002479 /* Servers should only apply open mode checks for file size changes */
2480 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2481 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2482
2483 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2484 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002485 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002486 struct nfs_lockowner lockowner = {
2487 .l_owner = current->files,
2488 .l_pid = current->tgid,
2489 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002490 if (!nfs4_valid_open_stateid(state))
2491 return -EBADF;
2492 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2493 &lockowner) == -EIO)
2494 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002495 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002496 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Bryan Schumaker7c513052011-03-24 17:12:24 +00002498 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002499 if (status == 0 && state != NULL)
2500 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002501 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002504static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2505 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002506 struct nfs4_state *state, struct nfs4_label *ilabel,
2507 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002509 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002510 struct nfs4_exception exception = {
2511 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002512 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002513 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 int err;
2515 do {
David Quigley1775fd32013-05-22 12:50:42 -04002516 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002517 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002518 switch (err) {
2519 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002520 if (!(sattr->ia_valid & ATTR_SIZE)) {
2521 pr_warn_once("NFSv4: server %s is incorrectly "
2522 "applying open mode checks to "
2523 "a SETATTR that is not "
2524 "changing file size.\n",
2525 server->nfs_client->cl_hostname);
2526 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002527 if (state && !(state->state & FMODE_WRITE)) {
2528 err = -EBADF;
2529 if (sattr->ia_valid & ATTR_OPEN)
2530 err = -EACCES;
2531 goto out;
2532 }
2533 }
2534 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002536out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return err;
2538}
2539
2540struct nfs4_closedata {
2541 struct inode *inode;
2542 struct nfs4_state *state;
2543 struct nfs_closeargs arg;
2544 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002545 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002546 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002547 bool roc;
2548 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549};
2550
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002551static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002552{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002553 struct nfs4_closedata *calldata = data;
2554 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002555 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002556
Fred Isamanf7e89172011-01-06 11:36:32 +00002557 if (calldata->roc)
2558 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002559 nfs4_put_open_state(calldata->state);
2560 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002561 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002562 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002563 kfree(calldata);
2564}
2565
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002566static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002568 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002571 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Chuck Levera3ca5652012-03-01 17:00:40 -05002573 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002574 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2575 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002576 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 /* hmm. we are done with the inode, and in the process of freeing
2578 * the state_owner. we keep this around to process errors
2579 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 switch (task->tk_status) {
2581 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002582 res_stateid = &calldata->res.stateid;
2583 if (calldata->arg.fmode == 0 && calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002584 pnfs_roc_set_barrier(state->inode,
2585 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002586 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002587 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002588 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002590 case -NFS4ERR_OLD_STATEID:
2591 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002593 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002596 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002597 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002598 goto out_release;
2599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
Trond Myklebust412f6c42014-08-25 22:09:08 -04002601 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002602out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002603 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002604 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002605 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002608static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002610 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002611 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002612 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002613 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002614 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002615
Chuck Levera3ca5652012-03-01 17:00:40 -05002616 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002617 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002618 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002619
Trond Myklebust88069f72009-12-08 08:33:16 -05002620 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002621 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002622 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2623 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2624 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04002625 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002626 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002627 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002628 if (state->n_rdonly == 0)
2629 call_close |= is_rdonly;
2630 else if (is_rdonly)
2631 calldata->arg.fmode |= FMODE_READ;
2632 if (state->n_wronly == 0)
2633 call_close |= is_wronly;
2634 else if (is_wronly)
2635 calldata->arg.fmode |= FMODE_WRITE;
2636 } else if (is_rdwr)
2637 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2638
2639 if (calldata->arg.fmode == 0)
2640 call_close |= is_rdwr;
2641
Trond Myklebust5d422302013-03-14 16:57:48 -04002642 if (!nfs4_valid_open_stateid(state))
2643 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002644 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002645
2646 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002647 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002648 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002649 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002650
Fred Isamanf7e89172011-01-06 11:36:32 +00002651 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002652 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002653 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002654 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2655 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002656 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002657 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002658 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002659
Trond Myklebust516a6af2005-10-27 22:12:41 -04002660 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002661 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002662 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002663 &calldata->arg.seq_args,
2664 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002665 task) != 0)
2666 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002667 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002668 return;
2669out_no_action:
2670 task->tk_action = NULL;
2671out_wait:
2672 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002675static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002676 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002677 .rpc_call_done = nfs4_close_done,
2678 .rpc_release = nfs4_free_closedata,
2679};
2680
Peng Taofe08c542014-07-03 13:05:01 +08002681static bool nfs4_state_has_opener(struct nfs4_state *state)
2682{
2683 /* first check existing openers */
2684 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 &&
2685 state->n_rdonly != 0)
2686 return true;
2687
2688 if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 &&
2689 state->n_wronly != 0)
2690 return true;
2691
2692 if (test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 &&
2693 state->n_rdwr != 0)
2694 return true;
2695
2696 return false;
2697}
2698
2699static bool nfs4_roc(struct inode *inode)
2700{
2701 struct nfs_inode *nfsi = NFS_I(inode);
2702 struct nfs_open_context *ctx;
2703 struct nfs4_state *state;
2704
2705 spin_lock(&inode->i_lock);
2706 list_for_each_entry(ctx, &nfsi->open_files, list) {
2707 state = ctx->state;
2708 if (state == NULL)
2709 continue;
2710 if (nfs4_state_has_opener(state)) {
2711 spin_unlock(&inode->i_lock);
2712 return false;
2713 }
2714 }
2715 spin_unlock(&inode->i_lock);
2716
2717 if (nfs4_check_delegation(inode, FMODE_READ))
2718 return false;
2719
2720 return pnfs_roc(inode);
2721}
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723/*
2724 * It is possible for data to be read/written from a mem-mapped file
2725 * after the sys_close call (which hits the vfs layer as a flush).
2726 * This means that we can't safely call nfsv4 close on a file until
2727 * the inode is cleared. This in turn means that we are not good
2728 * NFSv4 citizens - we do not indicate to the server to update the file's
2729 * share state even when we are done with one of the three share
2730 * stateid's in the inode.
2731 *
2732 * NOTE: Caller must be holding the sp->so_owner semaphore!
2733 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002734int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002736 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002738 struct nfs4_state_owner *sp = state->owner;
2739 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002740 struct rpc_message msg = {
2741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2742 .rpc_cred = state->owner->so_cred,
2743 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002744 struct rpc_task_setup task_setup_data = {
2745 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002746 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002747 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002748 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002749 .flags = RPC_TASK_ASYNC,
2750 };
Trond Myklebust95121352005-10-18 14:20:12 -07002751 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002753 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2754 &task_setup_data.rpc_client, &msg);
2755
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002756 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002758 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002759 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002760 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002762 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002763 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002765 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002766 if (calldata->arg.seqid == NULL)
2767 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002768 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002769 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002770 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002771 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002772 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002773 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002774 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002775
Trond Myklebust1174dd12010-12-21 10:52:24 -05002776 msg.rpc_argp = &calldata->arg;
2777 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002778 task_setup_data.callback_data = calldata;
2779 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002780 if (IS_ERR(task))
2781 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002782 status = 0;
2783 if (wait)
2784 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002785 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002786 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002787out_free_calldata:
2788 kfree(calldata);
2789out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002790 nfs4_put_open_state(state);
2791 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002792 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
Trond Myklebust2b484292010-09-17 10:56:51 -04002795static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002796nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2797 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002800 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2801
2802 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Trond Myklebust565277f2007-10-15 18:17:53 -04002804 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002805 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002806
2807 nfs4_label_release_security(label);
2808
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002809 if (IS_ERR(state))
2810 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002811 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
Trond Myklebust1185a552009-12-03 15:54:02 -05002814static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002815{
2816 if (ctx->state == NULL)
2817 return;
2818 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002819 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002820 else
Al Viro643168c2011-06-22 18:20:23 -04002821 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Trond Myklebustb944dba2013-11-04 15:20:20 -05002824#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2825#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002826#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2829{
Benny Halevy43652ad2009-04-01 09:21:54 -04002830 struct nfs4_server_caps_arg args = {
2831 .fhandle = fhandle,
2832 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 struct nfs4_server_caps_res res = {};
2834 struct rpc_message msg = {
2835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002836 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 .rpc_resp = &res,
2838 };
2839 int status;
2840
Bryan Schumaker7c513052011-03-24 17:12:24 +00002841 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002843 /* Sanity check the server answers */
2844 switch (server->nfs_client->cl_minorversion) {
2845 case 0:
2846 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2847 res.attr_bitmask[2] = 0;
2848 break;
2849 case 1:
2850 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2851 break;
2852 case 2:
2853 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002856 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2857 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2858 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2859 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002860 NFS_CAP_CTIME|NFS_CAP_MTIME|
2861 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002862 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2863 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 server->caps |= NFS_CAP_ACLS;
2865 if (res.has_links != 0)
2866 server->caps |= NFS_CAP_HARDLINKS;
2867 if (res.has_symlinks != 0)
2868 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002869 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2870 server->caps |= NFS_CAP_FILEID;
2871 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2872 server->caps |= NFS_CAP_MODE;
2873 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2874 server->caps |= NFS_CAP_NLINK;
2875 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2876 server->caps |= NFS_CAP_OWNER;
2877 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2878 server->caps |= NFS_CAP_OWNER_GROUP;
2879 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2880 server->caps |= NFS_CAP_ATIME;
2881 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2882 server->caps |= NFS_CAP_CTIME;
2883 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2884 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002885#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2886 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2887 server->caps |= NFS_CAP_SECURITY_LABEL;
2888#endif
2889 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2890 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002891 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002892
Trond Myklebusta65318b2009-03-11 14:10:28 -04002893 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2894 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2895 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002896 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002898 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return status;
2902}
2903
Trond Myklebust55a97592006-06-09 09:34:19 -04002904int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908 do {
2909 err = nfs4_handle_exception(server,
2910 _nfs4_server_capabilities(server, fhandle),
2911 &exception);
2912 } while (exception.retry);
2913 return err;
2914}
2915
2916static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2917 struct nfs_fsinfo *info)
2918{
David Quigleyaa9c2662013-05-22 12:50:44 -04002919 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002921 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 };
2923 struct nfs4_lookup_res res = {
2924 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002925 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 .fh = fhandle,
2927 };
2928 struct rpc_message msg = {
2929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2930 .rpc_argp = &args,
2931 .rpc_resp = &res,
2932 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002933
David Quigleyaa9c2662013-05-22 12:50:44 -04002934 bitmask[0] = nfs4_fattr_bitmap[0];
2935 bitmask[1] = nfs4_fattr_bitmap[1];
2936 /*
2937 * Process the label in the upcoming getfattr
2938 */
2939 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2940
Trond Myklebust0e574af2005-10-27 22:12:38 -04002941 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002942 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943}
2944
2945static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2946 struct nfs_fsinfo *info)
2947{
2948 struct nfs4_exception exception = { };
2949 int err;
2950 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002951 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002952 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002953 switch (err) {
2954 case 0:
2955 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002956 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002957 default:
2958 err = nfs4_handle_exception(server, err, &exception);
2959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002961out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return err;
2963}
2964
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002965static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2966 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2967{
Trond Myklebustc2190662013-08-26 19:23:04 -04002968 struct rpc_auth_create_args auth_args = {
2969 .pseudoflavor = flavor,
2970 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002971 struct rpc_auth *auth;
2972 int ret;
2973
Trond Myklebustc2190662013-08-26 19:23:04 -04002974 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002975 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002976 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002977 goto out;
2978 }
2979 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002980out:
2981 return ret;
2982}
2983
Chuck Lever9a744ba2013-03-16 15:56:02 -04002984/*
2985 * Retry pseudoroot lookup with various security flavors. We do this when:
2986 *
2987 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2988 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2989 *
2990 * Returns zero on success, or a negative NFS4ERR value, or a
2991 * negative errno value.
2992 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002993static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002994 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002996 /* Per 3530bis 15.33.5 */
2997 static const rpc_authflavor_t flav_array[] = {
2998 RPC_AUTH_GSS_KRB5P,
2999 RPC_AUTH_GSS_KRB5I,
3000 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003001 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003002 RPC_AUTH_NULL,
3003 };
3004 int status = -EPERM;
3005 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003007 if (server->auth_info.flavor_len > 0) {
3008 /* try each flavor specified by user */
3009 for (i = 0; i < server->auth_info.flavor_len; i++) {
3010 status = nfs4_lookup_root_sec(server, fhandle, info,
3011 server->auth_info.flavors[i]);
3012 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3013 continue;
3014 break;
3015 }
3016 } else {
3017 /* no flavors specified by user, try default list */
3018 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3019 status = nfs4_lookup_root_sec(server, fhandle, info,
3020 flav_array[i]);
3021 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3022 continue;
3023 break;
3024 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003025 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003026
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003027 /*
3028 * -EACCESS could mean that the user doesn't have correct permissions
3029 * to access the mount. It could also mean that we tried to mount
3030 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3031 * existing mount programs don't handle -EACCES very well so it should
3032 * be mapped to -EPERM instead.
3033 */
3034 if (status == -EACCES)
3035 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003036 return status;
3037}
3038
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003039static int nfs4_do_find_root_sec(struct nfs_server *server,
3040 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3041{
3042 int mv = server->nfs_client->cl_minorversion;
3043 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3044}
3045
3046/**
3047 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3048 * @server: initialized nfs_server handle
3049 * @fhandle: we fill in the pseudo-fs root file handle
3050 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003051 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003052 *
3053 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003054 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003055int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003056 struct nfs_fsinfo *info,
3057 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003058{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003059 int status;
3060
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003061 switch (auth_probe) {
3062 case false:
3063 status = nfs4_lookup_root(server, fhandle, info);
3064 if (status != -NFS4ERR_WRONGSEC)
3065 break;
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003066 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003067 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003068 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (status == 0)
3071 status = nfs4_server_capabilities(server, fhandle);
3072 if (status == 0)
3073 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003074
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003075 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
Bryan Schumakerbae36242012-05-10 15:07:31 -04003078static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3079 struct nfs_fsinfo *info)
3080{
3081 int error;
3082 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003083 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003084
3085 error = nfs4_server_capabilities(server, mntfh);
3086 if (error < 0) {
3087 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3088 return error;
3089 }
3090
David Quigley14c43f72013-05-22 12:50:43 -04003091 label = nfs4_label_alloc(server, GFP_KERNEL);
3092 if (IS_ERR(label))
3093 return PTR_ERR(label);
3094
David Quigley1775fd32013-05-22 12:50:42 -04003095 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003096 if (error < 0) {
3097 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003098 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003099 }
3100
3101 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3102 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3103 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3104
David Quigley14c43f72013-05-22 12:50:43 -04003105err_free_label:
3106 nfs4_label_free(label);
3107
Bryan Schumakerbae36242012-05-10 15:07:31 -04003108 return error;
3109}
3110
Manoj Naik6b97fd32006-06-09 09:34:29 -04003111/*
3112 * Get locations and (maybe) other attributes of a referral.
3113 * Note that we'll actually follow the referral later when
3114 * we detect fsid mismatch in inode revalidation
3115 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003116static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3117 const struct qstr *name, struct nfs_fattr *fattr,
3118 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003119{
3120 int status = -ENOMEM;
3121 struct page *page = NULL;
3122 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003123
3124 page = alloc_page(GFP_KERNEL);
3125 if (page == NULL)
3126 goto out;
3127 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3128 if (locations == NULL)
3129 goto out;
3130
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003131 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003132 if (status != 0)
3133 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003134
3135 /*
3136 * If the fsid didn't change, this is a migration event, not a
3137 * referral. Cause us to drop into the exception handler, which
3138 * will kick off migration recovery.
3139 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003140 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003141 dprintk("%s: server did not return a different fsid for"
3142 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003143 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003144 goto out;
3145 }
Andy Adamson533eb462011-06-13 18:25:56 -04003146 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3147 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003148
Andy Adamson533eb462011-06-13 18:25:56 -04003149 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003150 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003151 memset(fhandle, 0, sizeof(struct nfs_fh));
3152out:
3153 if (page)
3154 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003155 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003156 return status;
3157}
3158
David Quigley1775fd32013-05-22 12:50:42 -04003159static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3160 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 struct nfs4_getattr_arg args = {
3163 .fh = fhandle,
3164 .bitmask = server->attr_bitmask,
3165 };
3166 struct nfs4_getattr_res res = {
3167 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003168 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 .server = server,
3170 };
3171 struct rpc_message msg = {
3172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3173 .rpc_argp = &args,
3174 .rpc_resp = &res,
3175 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003176
3177 args.bitmask = nfs4_bitmask(server, label);
3178
Trond Myklebust0e574af2005-10-27 22:12:38 -04003179 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003180 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
David Quigley1775fd32013-05-22 12:50:42 -04003183static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3184 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
3186 struct nfs4_exception exception = { };
3187 int err;
3188 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003189 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3190 trace_nfs4_getattr(server, fhandle, fattr, err);
3191 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 &exception);
3193 } while (exception.retry);
3194 return err;
3195}
3196
3197/*
3198 * The file is not closed if it is opened due to the a request to change
3199 * the size of the file. The open call will not be needed once the
3200 * VFS layer lookup-intents are implemented.
3201 *
3202 * Close is called when the inode is destroyed.
3203 * If we haven't opened the file for O_WRONLY, we
3204 * need to in the size_change case to obtain a stateid.
3205 *
3206 * Got race?
3207 * Because OPEN is always done by name in nfsv4, it is
3208 * possible that we opened a different file by the same
3209 * name. We can recognize this race condition, but we
3210 * can't do anything about it besides returning an error.
3211 *
3212 * This will be fixed with VFS changes (lookup-intent).
3213 */
3214static int
3215nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3216 struct iattr *sattr)
3217{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003218 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003219 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003220 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003221 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 int status;
3223
Peng Tao88ac8152014-09-12 11:04:10 +08003224 if (pnfs_ld_layoutret_on_setattr(inode) &&
3225 sattr->ia_valid & ATTR_SIZE &&
3226 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003227 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003228
Trond Myklebust0e574af2005-10-27 22:12:38 -04003229 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Andy Adamson26699402012-05-30 16:12:24 -04003231 /* Deal with open(O_TRUNC) */
3232 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003233 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003234
3235 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003236 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003237 return 0;
3238
Trond Myklebustd5308382005-11-04 15:33:38 -05003239 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003240 if (sattr->ia_valid & ATTR_FILE) {
3241 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003242
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003243 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003244 if (ctx) {
3245 cred = ctx->cred;
3246 state = ctx->state;
3247 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003248 }
3249
David Quigley14c43f72013-05-22 12:50:43 -04003250 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3251 if (IS_ERR(label))
3252 return PTR_ERR(label);
3253
3254 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003255 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003256 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003257 nfs_setsecurity(inode, fattr, label);
3258 }
David Quigley14c43f72013-05-22 12:50:43 -04003259 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return status;
3261}
3262
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003263static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3264 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003265 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003266{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003267 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003268 int status;
3269 struct nfs4_lookup_arg args = {
3270 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003271 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003272 .name = name,
3273 };
3274 struct nfs4_lookup_res res = {
3275 .server = server,
3276 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003277 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003278 .fh = fhandle,
3279 };
3280 struct rpc_message msg = {
3281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3282 .rpc_argp = &args,
3283 .rpc_resp = &res,
3284 };
3285
David Quigleyaa9c2662013-05-22 12:50:44 -04003286 args.bitmask = nfs4_bitmask(server, label);
3287
David Howells2b3de442006-08-22 20:06:09 -04003288 nfs_fattr_init(fattr);
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003291 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 dprintk("NFS reply lookup: %d\n", status);
3293 return status;
3294}
3295
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003296static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003297{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003298 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003299 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003300 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3301 fattr->nlink = 2;
3302}
3303
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003304static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3305 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003306 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003307{
3308 struct nfs4_exception exception = { };
3309 struct rpc_clnt *client = *clnt;
3310 int err;
3311 do {
David Quigley1775fd32013-05-22 12:50:42 -04003312 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003313 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003314 switch (err) {
3315 case -NFS4ERR_BADNAME:
3316 err = -ENOENT;
3317 goto out;
3318 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003319 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003320 goto out;
3321 case -NFS4ERR_WRONGSEC:
3322 err = -EPERM;
3323 if (client != *clnt)
3324 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003325 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003326 if (IS_ERR(client))
3327 return PTR_ERR(client);
3328
3329 exception.retry = 1;
3330 break;
3331 default:
3332 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3333 }
3334 } while (exception.retry);
3335
3336out:
3337 if (err == 0)
3338 *clnt = client;
3339 else if (client != *clnt)
3340 rpc_shutdown_client(client);
3341
3342 return err;
3343}
3344
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003345static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003346 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3347 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003349 int status;
3350 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003351
David Quigley1775fd32013-05-22 12:50:42 -04003352 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003353 if (client != NFS_CLIENT(dir)) {
3354 rpc_shutdown_client(client);
3355 nfs_fixup_secinfo_attributes(fattr);
3356 }
3357 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003360struct rpc_clnt *
3361nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3362 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3363{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003364 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003365 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003366
David Quigley1775fd32013-05-22 12:50:42 -04003367 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003368 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003369 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003370 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003371}
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3374{
Trond Myklebust76b32992007-08-10 17:45:11 -04003375 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 struct nfs4_accessargs args = {
3377 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003378 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003380 struct nfs4_accessres res = {
3381 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3385 .rpc_argp = &args,
3386 .rpc_resp = &res,
3387 .rpc_cred = entry->cred,
3388 };
3389 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003390 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
3392 /*
3393 * Determine which access bits we want to ask for...
3394 */
3395 if (mode & MAY_READ)
3396 args.access |= NFS4_ACCESS_READ;
3397 if (S_ISDIR(inode->i_mode)) {
3398 if (mode & MAY_WRITE)
3399 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3400 if (mode & MAY_EXEC)
3401 args.access |= NFS4_ACCESS_LOOKUP;
3402 } else {
3403 if (mode & MAY_WRITE)
3404 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3405 if (mode & MAY_EXEC)
3406 args.access |= NFS4_ACCESS_EXECUTE;
3407 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003408
3409 res.fattr = nfs_alloc_fattr();
3410 if (res.fattr == NULL)
3411 return -ENOMEM;
3412
Bryan Schumaker7c513052011-03-24 17:12:24 +00003413 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003415 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003416 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003418 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return status;
3420}
3421
3422static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3423{
3424 struct nfs4_exception exception = { };
3425 int err;
3426 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003427 err = _nfs4_proc_access(inode, entry);
3428 trace_nfs4_access(inode, err);
3429 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 &exception);
3431 } while (exception.retry);
3432 return err;
3433}
3434
3435/*
3436 * TODO: For the time being, we don't try to get any attributes
3437 * along with any of the zero-copy operations READ, READDIR,
3438 * READLINK, WRITE.
3439 *
3440 * In the case of the first three, we want to put the GETATTR
3441 * after the read-type operation -- this is because it is hard
3442 * to predict the length of a GETATTR response in v4, and thus
3443 * align the READ data correctly. This means that the GETATTR
3444 * may end up partially falling into the page cache, and we should
3445 * shift it into the 'tail' of the xdr_buf before processing.
3446 * To do this efficiently, we need to know the total length
3447 * of data received, which doesn't seem to be available outside
3448 * of the RPC layer.
3449 *
3450 * In the case of WRITE, we also want to put the GETATTR after
3451 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003452 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 *
3454 * Both of these changes to the XDR layer would in fact be quite
3455 * minor, but I decided to leave them for a subsequent patch.
3456 */
3457static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3458 unsigned int pgbase, unsigned int pglen)
3459{
3460 struct nfs4_readlink args = {
3461 .fh = NFS_FH(inode),
3462 .pgbase = pgbase,
3463 .pglen = pglen,
3464 .pages = &page,
3465 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003466 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 struct rpc_message msg = {
3468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3469 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003470 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 };
3472
Bryan Schumaker7c513052011-03-24 17:12:24 +00003473 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
3476static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3477 unsigned int pgbase, unsigned int pglen)
3478{
3479 struct nfs4_exception exception = { };
3480 int err;
3481 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003482 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3483 trace_nfs4_readlink(inode, err);
3484 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 &exception);
3486 } while (exception.retry);
3487 return err;
3488}
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003491 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493static int
3494nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003495 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
David Quigleyaa9c2662013-05-22 12:50:44 -04003497 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003498 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003500 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 int status = 0;
3502
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003503 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3504 if (IS_ERR(ctx))
3505 return PTR_ERR(ctx);
3506
David Quigleyaa9c2662013-05-22 12:50:44 -04003507 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3508
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003509 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003510 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (IS_ERR(state)) {
3512 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003516 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003517 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 return status;
3519}
3520
3521static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3522{
Trond Myklebust16e42952005-10-27 22:12:44 -04003523 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003524 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003526 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003528 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003529 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003530 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3533 .rpc_argp = &args,
3534 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003536 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003537
Bryan Schumaker7c513052011-03-24 17:12:24 +00003538 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003539 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003540 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 return status;
3542}
3543
3544static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3545{
3546 struct nfs4_exception exception = { };
3547 int err;
3548 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003549 err = _nfs4_proc_remove(dir, name);
3550 trace_nfs4_remove(dir, name, err);
3551 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 &exception);
3553 } while (exception.retry);
3554 return err;
3555}
3556
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003557static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003559 struct nfs_server *server = NFS_SERVER(dir);
3560 struct nfs_removeargs *args = msg->rpc_argp;
3561 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003563 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003565 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003566
3567 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003570static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3571{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003572 nfs4_setup_sequence(NFS_SERVER(data->dir),
3573 &data->args.seq_args,
3574 &data->res.seq_res,
3575 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003578static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003580 struct nfs_unlinkdata *data = task->tk_calldata;
3581 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003582
Trond Myklebust14516c32010-07-31 14:29:06 -04003583 if (!nfs4_sequence_done(task, &res->seq_res))
3584 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003585 if (nfs4_async_handle_error(task, res->server, NULL,
3586 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003587 return 0;
3588 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003589 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
3591
Jeff Laytond3d41522010-09-17 17:31:57 -04003592static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3593{
3594 struct nfs_server *server = NFS_SERVER(dir);
3595 struct nfs_renameargs *arg = msg->rpc_argp;
3596 struct nfs_renameres *res = msg->rpc_resp;
3597
3598 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003599 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003600 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003601}
3602
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003603static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3604{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003605 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3606 &data->args.seq_args,
3607 &data->res.seq_res,
3608 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003609}
3610
3611static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3612 struct inode *new_dir)
3613{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003614 struct nfs_renamedata *data = task->tk_calldata;
3615 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003616
3617 if (!nfs4_sequence_done(task, &res->seq_res))
3618 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003619 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003620 return 0;
3621
3622 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003623 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003624 return 1;
3625}
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3628{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003629 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 struct nfs4_link_arg arg = {
3631 .fh = NFS_FH(inode),
3632 .dir_fh = NFS_FH(dir),
3633 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003634 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003636 struct nfs4_link_res res = {
3637 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003638 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003639 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 struct rpc_message msg = {
3641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3642 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003643 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003645 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Trond Myklebust136f2622010-04-16 16:22:49 -04003647 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003648 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003649 goto out;
3650
David Quigley14c43f72013-05-22 12:50:43 -04003651 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3652 if (IS_ERR(res.label)) {
3653 status = PTR_ERR(res.label);
3654 goto out;
3655 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003656 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003657
Bryan Schumaker7c513052011-03-24 17:12:24 +00003658 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003659 if (!status) {
3660 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003661 status = nfs_post_op_update_inode(inode, res.fattr);
3662 if (!status)
3663 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003664 }
David Quigley14c43f72013-05-22 12:50:43 -04003665
3666
3667 nfs4_label_free(res.label);
3668
Trond Myklebust136f2622010-04-16 16:22:49 -04003669out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003670 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return status;
3672}
3673
3674static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3675{
3676 struct nfs4_exception exception = { };
3677 int err;
3678 do {
3679 err = nfs4_handle_exception(NFS_SERVER(inode),
3680 _nfs4_proc_link(inode, dir, name),
3681 &exception);
3682 } while (exception.retry);
3683 return err;
3684}
3685
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003686struct nfs4_createdata {
3687 struct rpc_message msg;
3688 struct nfs4_create_arg arg;
3689 struct nfs4_create_res res;
3690 struct nfs_fh fh;
3691 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003692 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003693};
3694
3695static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3696 struct qstr *name, struct iattr *sattr, u32 ftype)
3697{
3698 struct nfs4_createdata *data;
3699
3700 data = kzalloc(sizeof(*data), GFP_KERNEL);
3701 if (data != NULL) {
3702 struct nfs_server *server = NFS_SERVER(dir);
3703
David Quigley14c43f72013-05-22 12:50:43 -04003704 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3705 if (IS_ERR(data->label))
3706 goto out_free;
3707
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003708 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3709 data->msg.rpc_argp = &data->arg;
3710 data->msg.rpc_resp = &data->res;
3711 data->arg.dir_fh = NFS_FH(dir);
3712 data->arg.server = server;
3713 data->arg.name = name;
3714 data->arg.attrs = sattr;
3715 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003716 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003717 data->res.server = server;
3718 data->res.fh = &data->fh;
3719 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003720 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003721 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003722 }
3723 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003724out_free:
3725 kfree(data);
3726 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003727}
3728
3729static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3730{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003731 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003732 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003733 if (status == 0) {
3734 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003735 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003736 }
3737 return status;
3738}
3739
3740static void nfs4_free_createdata(struct nfs4_createdata *data)
3741{
David Quigley14c43f72013-05-22 12:50:43 -04003742 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003743 kfree(data);
3744}
3745
Chuck Lever4f390c12006-08-22 20:06:22 -04003746static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003747 struct page *page, unsigned int len, struct iattr *sattr,
3748 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003750 struct nfs4_createdata *data;
3751 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Chuck Lever94a6d752006-08-22 20:06:23 -04003753 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003754 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003755
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003756 status = -ENOMEM;
3757 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3758 if (data == NULL)
3759 goto out;
3760
3761 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3762 data->arg.u.symlink.pages = &page;
3763 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003764 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003766 status = nfs4_do_create(dir, dentry, data);
3767
3768 nfs4_free_createdata(data);
3769out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 return status;
3771}
3772
Chuck Lever4f390c12006-08-22 20:06:22 -04003773static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003774 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
3776 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003777 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003779
3780 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003783 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3784 trace_nfs4_symlink(dir, &dentry->d_name, err);
3785 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 &exception);
3787 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003788
3789 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return err;
3791}
3792
3793static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003794 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003796 struct nfs4_createdata *data;
3797 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003799 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3800 if (data == NULL)
3801 goto out;
3802
David Quigley1775fd32013-05-22 12:50:42 -04003803 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003804 status = nfs4_do_create(dir, dentry, data);
3805
3806 nfs4_free_createdata(data);
3807out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 return status;
3809}
3810
3811static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3812 struct iattr *sattr)
3813{
3814 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003815 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003817
David Quigleyaa9c2662013-05-22 12:50:44 -04003818 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3819
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003820 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003822 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3823 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3824 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 &exception);
3826 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003827 nfs4_label_release_security(label);
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 return err;
3830}
3831
3832static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003833 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
3835 struct inode *dir = dentry->d_inode;
3836 struct nfs4_readdir_arg args = {
3837 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003838 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 .pgbase = 0,
3840 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003841 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003842 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 };
3844 struct nfs4_readdir_res res;
3845 struct rpc_message msg = {
3846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3847 .rpc_argp = &args,
3848 .rpc_resp = &res,
3849 .rpc_cred = cred,
3850 };
3851 int status;
3852
Al Viro6de14722013-09-16 10:53:17 -04003853 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3854 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003855 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003856 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003858 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05003859 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003860 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003861 status += args.pgbase;
3862 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003863
3864 nfs_invalidate_atime(dir);
3865
Harvey Harrison3110ff82008-05-02 13:42:44 -07003866 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 return status;
3868}
3869
3870static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003871 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
3873 struct nfs4_exception exception = { };
3874 int err;
3875 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003876 err = _nfs4_proc_readdir(dentry, cred, cookie,
3877 pages, count, plus);
3878 trace_nfs4_readdir(dentry->d_inode, err);
3879 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 &exception);
3881 } while (exception.retry);
3882 return err;
3883}
3884
3885static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003886 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003888 struct nfs4_createdata *data;
3889 int mode = sattr->ia_mode;
3890 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003892 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3893 if (data == NULL)
3894 goto out;
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003897 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003899 data->arg.ftype = NF4BLK;
3900 data->arg.u.device.specdata1 = MAJOR(rdev);
3901 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
3903 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003904 data->arg.ftype = NF4CHR;
3905 data->arg.u.device.specdata1 = MAJOR(rdev);
3906 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003907 } else if (!S_ISSOCK(mode)) {
3908 status = -EINVAL;
3909 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
David Quigley1775fd32013-05-22 12:50:42 -04003911
David Quigleyaa9c2662013-05-22 12:50:44 -04003912 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003913 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003914out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003915 nfs4_free_createdata(data);
3916out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 return status;
3918}
3919
3920static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3921 struct iattr *sattr, dev_t rdev)
3922{
3923 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003924 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003926
David Quigleyaa9c2662013-05-22 12:50:44 -04003927 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3928
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003929 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003931 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3932 trace_nfs4_mknod(dir, &dentry->d_name, err);
3933 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 &exception);
3935 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003936
3937 nfs4_label_release_security(label);
3938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 return err;
3940}
3941
3942static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3943 struct nfs_fsstat *fsstat)
3944{
3945 struct nfs4_statfs_arg args = {
3946 .fh = fhandle,
3947 .bitmask = server->attr_bitmask,
3948 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003949 struct nfs4_statfs_res res = {
3950 .fsstat = fsstat,
3951 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 struct rpc_message msg = {
3953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3954 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003955 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 };
3957
Trond Myklebust0e574af2005-10-27 22:12:38 -04003958 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003959 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960}
3961
3962static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3963{
3964 struct nfs4_exception exception = { };
3965 int err;
3966 do {
3967 err = nfs4_handle_exception(server,
3968 _nfs4_proc_statfs(server, fhandle, fsstat),
3969 &exception);
3970 } while (exception.retry);
3971 return err;
3972}
3973
3974static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3975 struct nfs_fsinfo *fsinfo)
3976{
3977 struct nfs4_fsinfo_arg args = {
3978 .fh = fhandle,
3979 .bitmask = server->attr_bitmask,
3980 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003981 struct nfs4_fsinfo_res res = {
3982 .fsinfo = fsinfo,
3983 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 struct rpc_message msg = {
3985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3986 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003987 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 };
3989
Bryan Schumaker7c513052011-03-24 17:12:24 +00003990 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
3993static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3994{
3995 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003996 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 int err;
3998
3999 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004000 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004001 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004002 if (err == 0) {
4003 struct nfs_client *clp = server->nfs_client;
4004
4005 spin_lock(&clp->cl_lock);
4006 clp->cl_lease_time = fsinfo->lease_time * HZ;
4007 clp->cl_last_renewal = now;
4008 spin_unlock(&clp->cl_lock);
4009 break;
4010 }
4011 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 } while (exception.retry);
4013 return err;
4014}
4015
4016static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4017{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004018 int error;
4019
Trond Myklebust0e574af2005-10-27 22:12:38 -04004020 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004021 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004022 if (error == 0) {
4023 /* block layout checks this! */
4024 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004025 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004026 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004027
4028 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029}
4030
4031static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4032 struct nfs_pathconf *pathconf)
4033{
4034 struct nfs4_pathconf_arg args = {
4035 .fh = fhandle,
4036 .bitmask = server->attr_bitmask,
4037 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004038 struct nfs4_pathconf_res res = {
4039 .pathconf = pathconf,
4040 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 struct rpc_message msg = {
4042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4043 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004044 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 };
4046
4047 /* None of the pathconf attributes are mandatory to implement */
4048 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4049 memset(pathconf, 0, sizeof(*pathconf));
4050 return 0;
4051 }
4052
Trond Myklebust0e574af2005-10-27 22:12:38 -04004053 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004054 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
4057static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4058 struct nfs_pathconf *pathconf)
4059{
4060 struct nfs4_exception exception = { };
4061 int err;
4062
4063 do {
4064 err = nfs4_handle_exception(server,
4065 _nfs4_proc_pathconf(server, fhandle, pathconf),
4066 &exception);
4067 } while (exception.retry);
4068 return err;
4069}
4070
Trond Myklebust5521abf2013-03-16 20:54:34 -04004071int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004072 const struct nfs_open_context *ctx,
4073 const struct nfs_lock_context *l_ctx,
4074 fmode_t fmode)
4075{
4076 const struct nfs_lockowner *lockowner = NULL;
4077
4078 if (l_ctx != NULL)
4079 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004080 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004081}
4082EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4083
Trond Myklebust5521abf2013-03-16 20:54:34 -04004084static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4085 const struct nfs_open_context *ctx,
4086 const struct nfs_lock_context *l_ctx,
4087 fmode_t fmode)
4088{
4089 nfs4_stateid current_stateid;
4090
Trond Myklebuste1253be2014-03-05 08:44:23 -05004091 /* If the current stateid represents a lost lock, then exit */
4092 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4093 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004094 return nfs4_stateid_match(stateid, &current_stateid);
4095}
4096
4097static bool nfs4_error_stateid_expired(int err)
4098{
4099 switch (err) {
4100 case -NFS4ERR_DELEG_REVOKED:
4101 case -NFS4ERR_ADMIN_REVOKED:
4102 case -NFS4ERR_BAD_STATEID:
4103 case -NFS4ERR_STALE_STATEID:
4104 case -NFS4ERR_OLD_STATEID:
4105 case -NFS4ERR_OPENMODE:
4106 case -NFS4ERR_EXPIRED:
4107 return true;
4108 }
4109 return false;
4110}
4111
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004112void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004113{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004114 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004115}
4116
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004117static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004119 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004121 trace_nfs4_read(hdr, task->tk_status);
4122 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004123 hdr->args.context->state,
4124 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004125 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004126 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004128
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004129 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004131 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
Trond Myklebust5521abf2013-03-16 20:54:34 -04004135static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004136 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004137{
4138
4139 if (!nfs4_error_stateid_expired(task->tk_status) ||
4140 nfs4_stateid_is_current(&args->stateid,
4141 args->context,
4142 args->lock_context,
4143 FMODE_READ))
4144 return false;
4145 rpc_restart_call_prepare(task);
4146 return true;
4147}
4148
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004149static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004150{
4151
4152 dprintk("--> %s\n", __func__);
4153
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004154 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004155 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004156 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004157 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004158 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4159 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004160}
4161
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004162static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4163 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004165 hdr->timestamp = jiffies;
4166 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004167 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004168 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169}
4170
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004171static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4172 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004173{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004174 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4175 &hdr->args.seq_args,
4176 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004177 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004178 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004179 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4180 hdr->args.lock_context,
4181 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004182 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004183 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004184 return -EIO;
4185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
4187
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004188static int nfs4_write_done_cb(struct rpc_task *task,
4189 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004191 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004192
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004193 trace_nfs4_write(hdr, task->tk_status);
4194 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004195 hdr->args.context->state,
4196 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004197 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004198 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004200 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004201 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4202 nfs_post_op_update_inode_force_wcc(inode, &hdr->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004203 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205}
4206
Trond Myklebust5521abf2013-03-16 20:54:34 -04004207static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004208 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004209{
4210
4211 if (!nfs4_error_stateid_expired(task->tk_status) ||
4212 nfs4_stateid_is_current(&args->stateid,
4213 args->context,
4214 args->lock_context,
4215 FMODE_WRITE))
4216 return false;
4217 rpc_restart_call_prepare(task);
4218 return true;
4219}
4220
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004221static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004222{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004223 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004224 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004225 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004226 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004227 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4228 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004229}
4230
Trond Myklebust5a37f852012-04-28 14:55:16 -04004231static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004232bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004233{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004234 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004235 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004236 return false;
4237 /* Otherwise, request attributes if and only if we don't hold
4238 * a delegation
4239 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004240 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004241}
Fred Isamana69aef12011-03-03 15:13:47 +00004242
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004243static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4244 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004246 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004247
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004248 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4249 hdr->args.bitmask = NULL;
4250 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004251 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004252 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004253
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004254 if (!hdr->pgio_done_cb)
4255 hdr->pgio_done_cb = nfs4_write_done_cb;
4256 hdr->res.server = server;
4257 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004259 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004260 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261}
4262
Fred Isaman0b7c0152012-04-20 14:47:39 -04004263static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4264{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004265 nfs4_setup_sequence(NFS_SERVER(data->inode),
4266 &data->args.seq_args,
4267 &data->res.seq_res,
4268 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269}
4270
Fred Isaman0b7c0152012-04-20 14:47:39 -04004271static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004274
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004275 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004276 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4277 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004278 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004279 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282}
4283
Fred Isaman0b7c0152012-04-20 14:47:39 -04004284static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004285{
4286 if (!nfs4_sequence_done(task, &data->res.seq_res))
4287 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004288 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004289}
4290
Fred Isaman0b7c0152012-04-20 14:47:39 -04004291static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004293 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004294
Fred Isaman0b7c0152012-04-20 14:47:39 -04004295 if (data->commit_done_cb == NULL)
4296 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004297 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004298 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004299 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300}
4301
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004302struct nfs4_renewdata {
4303 struct nfs_client *client;
4304 unsigned long timestamp;
4305};
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307/*
4308 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4309 * standalone procedure for queueing an asynchronous RENEW.
4310 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004311static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004312{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004313 struct nfs4_renewdata *data = calldata;
4314 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004315
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004316 if (atomic_read(&clp->cl_count) > 1)
4317 nfs4_schedule_state_renewal(clp);
4318 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004319 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004320}
4321
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004322static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004324 struct nfs4_renewdata *data = calldata;
4325 struct nfs_client *clp = data->client;
4326 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004328 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004329 switch (task->tk_status) {
4330 case 0:
4331 break;
4332 case -NFS4ERR_LEASE_MOVED:
4333 nfs4_schedule_lease_moved_recovery(clp);
4334 break;
4335 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004336 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004337 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4338 return;
4339 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004340 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004341 return;
4342 }
4343 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004345 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346}
4347
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004348static const struct rpc_call_ops nfs4_renew_ops = {
4349 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004350 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004351};
4352
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004353static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
4355 struct rpc_message msg = {
4356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4357 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004358 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004360 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004362 if (renew_flags == 0)
4363 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004364 if (!atomic_inc_not_zero(&clp->cl_count))
4365 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004366 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004367 if (data == NULL)
4368 return -ENOMEM;
4369 data->client = clp;
4370 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004371 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004372 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373}
4374
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004375static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
4377 struct rpc_message msg = {
4378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4379 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004380 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 };
4382 unsigned long now = jiffies;
4383 int status;
4384
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004385 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 if (status < 0)
4387 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004388 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return 0;
4390}
4391
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004392static inline int nfs4_server_supports_acls(struct nfs_server *server)
4393{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004394 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004395}
4396
Trond Myklebust21f498c2012-08-24 10:59:25 -04004397/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4398 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004399 * the stack.
4400 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004401#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004402
Neil Hormane9e3d722011-03-04 19:26:03 -05004403static int buf_to_pages_noslab(const void *buf, size_t buflen,
4404 struct page **pages, unsigned int *pgbase)
4405{
4406 struct page *newpage, **spages;
4407 int rc = 0;
4408 size_t len;
4409 spages = pages;
4410
4411 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004412 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004413 newpage = alloc_page(GFP_KERNEL);
4414
4415 if (newpage == NULL)
4416 goto unwind;
4417 memcpy(page_address(newpage), buf, len);
4418 buf += len;
4419 buflen -= len;
4420 *pages++ = newpage;
4421 rc++;
4422 } while (buflen != 0);
4423
4424 return rc;
4425
4426unwind:
4427 for(; rc > 0; rc--)
4428 __free_page(spages[rc-1]);
4429 return -ENOMEM;
4430}
4431
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004432struct nfs4_cached_acl {
4433 int cached;
4434 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004435 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004436};
4437
4438static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004439{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004440 struct nfs_inode *nfsi = NFS_I(inode);
4441
4442 spin_lock(&inode->i_lock);
4443 kfree(nfsi->nfs4_acl);
4444 nfsi->nfs4_acl = acl;
4445 spin_unlock(&inode->i_lock);
4446}
4447
4448static void nfs4_zap_acl_attr(struct inode *inode)
4449{
4450 nfs4_set_cached_acl(inode, NULL);
4451}
4452
4453static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4454{
4455 struct nfs_inode *nfsi = NFS_I(inode);
4456 struct nfs4_cached_acl *acl;
4457 int ret = -ENOENT;
4458
4459 spin_lock(&inode->i_lock);
4460 acl = nfsi->nfs4_acl;
4461 if (acl == NULL)
4462 goto out;
4463 if (buf == NULL) /* user is just asking for length */
4464 goto out_len;
4465 if (acl->cached == 0)
4466 goto out;
4467 ret = -ERANGE; /* see getxattr(2) man page */
4468 if (acl->len > buflen)
4469 goto out;
4470 memcpy(buf, acl->data, acl->len);
4471out_len:
4472 ret = acl->len;
4473out:
4474 spin_unlock(&inode->i_lock);
4475 return ret;
4476}
4477
Sachin Prabhu5794d212012-04-17 14:36:40 +01004478static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004479{
4480 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004481 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004482
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004483 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004484 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004485 if (acl == NULL)
4486 goto out;
4487 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004488 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004489 } else {
4490 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4491 if (acl == NULL)
4492 goto out;
4493 acl->cached = 0;
4494 }
4495 acl->len = acl_len;
4496out:
4497 nfs4_set_cached_acl(inode, acl);
4498}
4499
Andy Adamsonbf118a32011-12-07 11:55:27 -05004500/*
4501 * The getxattr API returns the required buffer length when called with a
4502 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4503 * the required buf. On a NULL buf, we send a page of data to the server
4504 * guessing that the ACL request can be serviced by a page. If so, we cache
4505 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4506 * the cache. If not so, we throw away the page, and cache the required
4507 * length. The next getxattr call will then produce another round trip to
4508 * the server, this time with the input buf of the required size.
4509 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004510static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004511{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004512 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004513 struct nfs_getaclargs args = {
4514 .fh = NFS_FH(inode),
4515 .acl_pages = pages,
4516 .acl_len = buflen,
4517 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004518 struct nfs_getaclres res = {
4519 .acl_len = buflen,
4520 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004521 struct rpc_message msg = {
4522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4523 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004524 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004525 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004526 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4527 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004528
Andy Adamsonbf118a32011-12-07 11:55:27 -05004529 /* As long as we're doing a round trip to the server anyway,
4530 * let's be prepared for a page of acl data. */
4531 if (npages == 0)
4532 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004533 if (npages > ARRAY_SIZE(pages))
4534 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004535
Andy Adamsonbf118a32011-12-07 11:55:27 -05004536 for (i = 0; i < npages; i++) {
4537 pages[i] = alloc_page(GFP_KERNEL);
4538 if (!pages[i])
4539 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004540 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004541
4542 /* for decoding across pages */
4543 res.acl_scratch = alloc_page(GFP_KERNEL);
4544 if (!res.acl_scratch)
4545 goto out_free;
4546
Andy Adamsonbf118a32011-12-07 11:55:27 -05004547 args.acl_len = npages * PAGE_SIZE;
4548 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004549
Peng Taode040be2012-01-10 22:42:47 +08004550 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004551 __func__, buf, buflen, npages, args.acl_len);
4552 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4553 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004554 if (ret)
4555 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004556
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004557 /* Handle the case where the passed-in buffer is too short */
4558 if (res.acl_flags & NFS4_ACL_TRUNC) {
4559 /* Did the user only issue a request for the acl length? */
4560 if (buf == NULL)
4561 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004562 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004563 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004564 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004565 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004566 if (buf) {
4567 if (res.acl_len > buflen) {
4568 ret = -ERANGE;
4569 goto out_free;
4570 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004571 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004572 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004573out_ok:
4574 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004575out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004576 for (i = 0; i < npages; i++)
4577 if (pages[i])
4578 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004579 if (res.acl_scratch)
4580 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004581 return ret;
4582}
4583
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004584static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4585{
4586 struct nfs4_exception exception = { };
4587 ssize_t ret;
4588 do {
4589 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004590 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004591 if (ret >= 0)
4592 break;
4593 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4594 } while (exception.retry);
4595 return ret;
4596}
4597
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004598static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4599{
4600 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004601 int ret;
4602
4603 if (!nfs4_server_supports_acls(server))
4604 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004605 ret = nfs_revalidate_inode(server, inode);
4606 if (ret < 0)
4607 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004608 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4609 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004610 ret = nfs4_read_cached_acl(inode, buf, buflen);
4611 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004612 /* -ENOENT is returned if there is no ACL or if there is an ACL
4613 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004614 return ret;
4615 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004616}
4617
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004618static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004619{
4620 struct nfs_server *server = NFS_SERVER(inode);
4621 struct page *pages[NFS4ACL_MAXPAGES];
4622 struct nfs_setaclargs arg = {
4623 .fh = NFS_FH(inode),
4624 .acl_pages = pages,
4625 .acl_len = buflen,
4626 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004627 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004628 struct rpc_message msg = {
4629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4630 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004631 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004632 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004633 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004634 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004635
4636 if (!nfs4_server_supports_acls(server))
4637 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004638 if (npages > ARRAY_SIZE(pages))
4639 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004640 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4641 if (i < 0)
4642 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004643 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004644 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004645
4646 /*
4647 * Free each page after tx, so the only ref left is
4648 * held by the network stack
4649 */
4650 for (; i > 0; i--)
4651 put_page(pages[i-1]);
4652
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004653 /*
4654 * Acl update can result in inode attribute update.
4655 * so mark the attribute cache invalid.
4656 */
4657 spin_lock(&inode->i_lock);
4658 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4659 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004660 nfs_access_zap_cache(inode);
4661 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004662 return ret;
4663}
4664
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004665static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4666{
4667 struct nfs4_exception exception = { };
4668 int err;
4669 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004670 err = __nfs4_proc_set_acl(inode, buf, buflen);
4671 trace_nfs4_set_acl(inode, err);
4672 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004673 &exception);
4674 } while (exception.retry);
4675 return err;
4676}
4677
David Quigleyaa9c2662013-05-22 12:50:44 -04004678#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4679static int _nfs4_get_security_label(struct inode *inode, void *buf,
4680 size_t buflen)
4681{
4682 struct nfs_server *server = NFS_SERVER(inode);
4683 struct nfs_fattr fattr;
4684 struct nfs4_label label = {0, 0, buflen, buf};
4685
4686 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004687 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004688 .fh = NFS_FH(inode),
4689 .bitmask = bitmask,
4690 };
4691 struct nfs4_getattr_res res = {
4692 .fattr = &fattr,
4693 .label = &label,
4694 .server = server,
4695 };
4696 struct rpc_message msg = {
4697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004698 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004699 .rpc_resp = &res,
4700 };
4701 int ret;
4702
4703 nfs_fattr_init(&fattr);
4704
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004705 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004706 if (ret)
4707 return ret;
4708 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4709 return -ENOENT;
4710 if (buflen < label.len)
4711 return -ERANGE;
4712 return 0;
4713}
4714
4715static int nfs4_get_security_label(struct inode *inode, void *buf,
4716 size_t buflen)
4717{
4718 struct nfs4_exception exception = { };
4719 int err;
4720
4721 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4722 return -EOPNOTSUPP;
4723
4724 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004725 err = _nfs4_get_security_label(inode, buf, buflen);
4726 trace_nfs4_get_security_label(inode, err);
4727 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004728 &exception);
4729 } while (exception.retry);
4730 return err;
4731}
4732
4733static int _nfs4_do_set_security_label(struct inode *inode,
4734 struct nfs4_label *ilabel,
4735 struct nfs_fattr *fattr,
4736 struct nfs4_label *olabel)
4737{
4738
4739 struct iattr sattr = {0};
4740 struct nfs_server *server = NFS_SERVER(inode);
4741 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004742 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004743 .fh = NFS_FH(inode),
4744 .iap = &sattr,
4745 .server = server,
4746 .bitmask = bitmask,
4747 .label = ilabel,
4748 };
4749 struct nfs_setattrres res = {
4750 .fattr = fattr,
4751 .label = olabel,
4752 .server = server,
4753 };
4754 struct rpc_message msg = {
4755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004756 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004757 .rpc_resp = &res,
4758 };
4759 int status;
4760
Jeff Layton12207f62013-11-01 10:49:32 -04004761 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004762
Jeff Layton12207f62013-11-01 10:49:32 -04004763 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004764 if (status)
4765 dprintk("%s failed: %d\n", __func__, status);
4766
4767 return status;
4768}
4769
4770static int nfs4_do_set_security_label(struct inode *inode,
4771 struct nfs4_label *ilabel,
4772 struct nfs_fattr *fattr,
4773 struct nfs4_label *olabel)
4774{
4775 struct nfs4_exception exception = { };
4776 int err;
4777
4778 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004779 err = _nfs4_do_set_security_label(inode, ilabel,
4780 fattr, olabel);
4781 trace_nfs4_set_security_label(inode, err);
4782 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004783 &exception);
4784 } while (exception.retry);
4785 return err;
4786}
4787
4788static int
4789nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4790{
4791 struct nfs4_label ilabel, *olabel = NULL;
4792 struct nfs_fattr fattr;
4793 struct rpc_cred *cred;
4794 struct inode *inode = dentry->d_inode;
4795 int status;
4796
4797 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4798 return -EOPNOTSUPP;
4799
4800 nfs_fattr_init(&fattr);
4801
4802 ilabel.pi = 0;
4803 ilabel.lfs = 0;
4804 ilabel.label = (char *)buf;
4805 ilabel.len = buflen;
4806
4807 cred = rpc_lookup_cred();
4808 if (IS_ERR(cred))
4809 return PTR_ERR(cred);
4810
4811 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4812 if (IS_ERR(olabel)) {
4813 status = -PTR_ERR(olabel);
4814 goto out;
4815 }
4816
4817 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4818 if (status == 0)
4819 nfs_setsecurity(inode, &fattr, olabel);
4820
4821 nfs4_label_free(olabel);
4822out:
4823 put_rpccred(cred);
4824 return status;
4825}
4826#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4827
4828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004830nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4831 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004833 struct nfs_client *clp = server->nfs_client;
4834
4835 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 return 0;
4837 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004838 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004839 case -NFS4ERR_ADMIN_REVOKED:
4840 case -NFS4ERR_BAD_STATEID:
4841 case -NFS4ERR_OPENMODE:
4842 if (state == NULL)
4843 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004844 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004845 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004846 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004847 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004848 if (state != NULL) {
4849 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004850 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004853 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004854 nfs4_schedule_lease_recovery(clp);
4855 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004856 case -NFS4ERR_MOVED:
4857 if (nfs4_schedule_migration_recovery(server) < 0)
4858 goto recovery_failed;
4859 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004860 case -NFS4ERR_LEASE_MOVED:
4861 nfs4_schedule_lease_moved_recovery(clp);
4862 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004863#if defined(CONFIG_NFS_V4_1)
4864 case -NFS4ERR_BADSESSION:
4865 case -NFS4ERR_BADSLOT:
4866 case -NFS4ERR_BAD_HIGH_SLOT:
4867 case -NFS4ERR_DEADSESSION:
4868 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4869 case -NFS4ERR_SEQ_FALSE_RETRY:
4870 case -NFS4ERR_SEQ_MISORDERED:
4871 dprintk("%s ERROR %d, Reset session\n", __func__,
4872 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004873 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004874 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004875#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004877 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10004878 rpc_delay(task, nfs4_update_delay(timeout));
4879 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05004880 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004882 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004884 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 }
4886 task->tk_status = nfs4_map_errors(task->tk_status);
4887 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004888recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004889 task->tk_status = -EIO;
4890 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004891wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004892 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004893 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4894 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004895 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4896 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004897restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004898 task->tk_status = 0;
4899 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
Chuck Leverf0920752012-05-21 22:45:41 -04004902static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4903 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004904{
4905 __be32 verf[2];
4906
Chuck Lever2c820d92012-05-21 22:45:33 -04004907 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4908 /* An impossible timestamp guarantees this value
4909 * will never match a generated boot time. */
4910 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004911 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004912 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004913 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004914 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4915 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004916 }
Chuck Levercd937102012-03-02 17:14:31 -05004917 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4918}
4919
Chuck Levere984a552012-09-14 17:24:21 -04004920static unsigned int
Trond Myklebustceb3a162015-01-03 15:16:04 -05004921nfs4_init_nonuniform_client_string(struct nfs_client *clp,
Chuck Levere984a552012-09-14 17:24:21 -04004922 char *buf, size_t len)
4923{
4924 unsigned int result;
4925
Trond Myklebustceb3a162015-01-03 15:16:04 -05004926 if (clp->cl_owner_id != NULL)
4927 return strlcpy(buf, clp->cl_owner_id, len);
4928
Chuck Levere984a552012-09-14 17:24:21 -04004929 rcu_read_lock();
4930 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4931 clp->cl_ipaddr,
4932 rpc_peeraddr2str(clp->cl_rpcclient,
4933 RPC_DISPLAY_ADDR),
4934 rpc_peeraddr2str(clp->cl_rpcclient,
4935 RPC_DISPLAY_PROTO));
4936 rcu_read_unlock();
Trond Myklebustceb3a162015-01-03 15:16:04 -05004937 clp->cl_owner_id = kstrdup(buf, GFP_KERNEL);
Chuck Levere984a552012-09-14 17:24:21 -04004938 return result;
4939}
4940
4941static unsigned int
Trond Myklebustceb3a162015-01-03 15:16:04 -05004942nfs4_init_uniform_client_string(struct nfs_client *clp,
Chuck Levere984a552012-09-14 17:24:21 -04004943 char *buf, size_t len)
4944{
Trond Myklebust55b59292013-07-24 16:36:35 -04004945 const char *nodename = clp->cl_rpcclient->cl_nodename;
Trond Myklebustceb3a162015-01-03 15:16:04 -05004946 unsigned int result;
4947
4948 if (clp->cl_owner_id != NULL)
4949 return strlcpy(buf, clp->cl_owner_id, len);
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004950
4951 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebustceb3a162015-01-03 15:16:04 -05004952 result = scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
Trond Myklebust55b59292013-07-24 16:36:35 -04004953 clp->rpc_ops->version,
4954 clp->cl_minorversion,
4955 nfs4_client_id_uniquifier,
4956 nodename);
Trond Myklebustceb3a162015-01-03 15:16:04 -05004957 else
4958 result = scnprintf(buf, len, "Linux NFSv%u.%u %s",
Chuck Levere984a552012-09-14 17:24:21 -04004959 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004960 nodename);
Trond Myklebustceb3a162015-01-03 15:16:04 -05004961 clp->cl_owner_id = kstrdup(buf, GFP_KERNEL);
4962 return result;
Chuck Levere984a552012-09-14 17:24:21 -04004963}
4964
Chuck Lever706cb8d2014-03-12 12:51:47 -04004965/*
4966 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4967 * services. Advertise one based on the address family of the
4968 * clientaddr.
4969 */
4970static unsigned int
4971nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4972{
4973 if (strchr(clp->cl_ipaddr, ':') != NULL)
4974 return scnprintf(buf, len, "tcp6");
4975 else
4976 return scnprintf(buf, len, "tcp");
4977}
4978
Jeff Laytonf11b2a12014-06-21 20:52:17 -04004979static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
4980{
4981 struct nfs4_setclientid *sc = calldata;
4982
4983 if (task->tk_status == 0)
4984 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
4985}
4986
4987static const struct rpc_call_ops nfs4_setclientid_ops = {
4988 .rpc_call_done = nfs4_setclientid_done,
4989};
4990
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004991/**
4992 * nfs4_proc_setclientid - Negotiate client ID
4993 * @clp: state data structure
4994 * @program: RPC program for NFSv4 callback service
4995 * @port: IP port number for NFS4 callback service
4996 * @cred: RPC credential to use for this call
4997 * @res: where to place the result
4998 *
4999 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5000 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005001int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5002 unsigned short port, struct rpc_cred *cred,
5003 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004{
5005 nfs4_verifier sc_verifier;
5006 struct nfs4_setclientid setclientid = {
5007 .sc_verifier = &sc_verifier,
5008 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00005009 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 };
5011 struct rpc_message msg = {
5012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5013 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005014 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005015 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005017 struct rpc_task *task;
5018 struct rpc_task_setup task_setup_data = {
5019 .rpc_client = clp->cl_rpcclient,
5020 .rpc_message = &msg,
5021 .callback_ops = &nfs4_setclientid_ops,
5022 .callback_data = &setclientid,
5023 .flags = RPC_TASK_TIMEOUT,
5024 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005025 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Chuck Leverde734832012-07-11 16:30:50 -04005027 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005028 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005029 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5030 setclientid.sc_name_len =
5031 nfs4_init_uniform_client_string(clp,
5032 setclientid.sc_name,
5033 sizeof(setclientid.sc_name));
5034 else
5035 setclientid.sc_name_len =
5036 nfs4_init_nonuniform_client_string(clp,
5037 setclientid.sc_name,
5038 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04005039 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005040 setclientid.sc_netid_len =
5041 nfs4_init_callback_netid(clp,
5042 setclientid.sc_netid,
5043 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005044 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005045 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 clp->cl_ipaddr, port >> 8, port & 255);
5047
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005048 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
5049 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5050 setclientid.sc_name_len, setclientid.sc_name);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005051 task = rpc_run_task(&task_setup_data);
5052 if (IS_ERR(task)) {
5053 status = PTR_ERR(task);
5054 goto out;
5055 }
5056 status = task->tk_status;
5057 if (setclientid.sc_cred) {
5058 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5059 put_rpccred(setclientid.sc_cred);
5060 }
5061 rpc_put_task(task);
5062out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005063 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005064 dprintk("NFS reply setclientid: %d\n", status);
5065 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066}
5067
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005068/**
5069 * nfs4_proc_setclientid_confirm - Confirm client ID
5070 * @clp: state data structure
5071 * @res: result of a previous SETCLIENTID
5072 * @cred: RPC credential to use for this call
5073 *
5074 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5075 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005076int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005077 struct nfs4_setclientid_res *arg,
5078 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 struct rpc_message msg = {
5081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005082 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005083 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 int status;
5086
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005087 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5088 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5089 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005090 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005091 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005092 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 return status;
5094}
5095
Trond Myklebustfe650402006-01-03 09:55:18 +01005096struct nfs4_delegreturndata {
5097 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005098 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005099 struct nfs_fh fh;
5100 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005101 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005102 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005103 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005104 struct inode *inode;
5105 bool roc;
5106 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005107};
5108
Trond Myklebustfe650402006-01-03 09:55:18 +01005109static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5110{
5111 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005112
Trond Myklebust14516c32010-07-31 14:29:06 -04005113 if (!nfs4_sequence_done(task, &data->res.seq_res))
5114 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005115
Trond Myklebustca8acf82013-08-13 10:36:56 -04005116 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005117 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005118 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005119 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005120 case -NFS4ERR_ADMIN_REVOKED:
5121 case -NFS4ERR_DELEG_REVOKED:
5122 case -NFS4ERR_BAD_STATEID:
5123 case -NFS4ERR_OLD_STATEID:
5124 case -NFS4ERR_STALE_STATEID:
5125 case -NFS4ERR_EXPIRED:
5126 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005127 if (data->roc)
5128 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005129 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005130 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005131 if (nfs4_async_handle_error(task, data->res.server,
5132 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005133 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005134 return;
5135 }
5136 }
5137 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005138}
5139
5140static void nfs4_delegreturn_release(void *calldata)
5141{
Peng Tao039b7562014-07-03 13:05:02 +08005142 struct nfs4_delegreturndata *data = calldata;
5143
5144 if (data->roc)
5145 pnfs_roc_release(data->inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005146 kfree(calldata);
5147}
5148
Andy Adamson938e1012009-04-01 09:22:28 -04005149static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5150{
5151 struct nfs4_delegreturndata *d_data;
5152
5153 d_data = (struct nfs4_delegreturndata *)data;
5154
Peng Tao039b7562014-07-03 13:05:02 +08005155 if (d_data->roc &&
5156 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5157 return;
5158
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005159 nfs4_setup_sequence(d_data->res.server,
5160 &d_data->args.seq_args,
5161 &d_data->res.seq_res,
5162 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005163}
Andy Adamson938e1012009-04-01 09:22:28 -04005164
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005165static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005166 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005167 .rpc_call_done = nfs4_delegreturn_done,
5168 .rpc_release = nfs4_delegreturn_release,
5169};
5170
Trond Myklebuste6f81072008-01-24 18:14:34 -05005171static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005172{
5173 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005174 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005175 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005176 struct rpc_message msg = {
5177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5178 .rpc_cred = cred,
5179 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005180 struct rpc_task_setup task_setup_data = {
5181 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005182 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005183 .callback_ops = &nfs4_delegreturn_ops,
5184 .flags = RPC_TASK_ASYNC,
5185 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005186 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005187
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005188 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005189 if (data == NULL)
5190 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005191 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005192 data->args.fhandle = &data->fh;
5193 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005194 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005195 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005196 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005197 data->res.fattr = &data->fattr;
5198 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005199 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005200 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005201 data->rpc_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005202 data->inode = inode;
5203 data->roc = list_empty(&NFS_I(inode)->open_files) ?
5204 pnfs_roc(inode) : false;
Trond Myklebustfe650402006-01-03 09:55:18 +01005205
Trond Myklebustc970aa82007-07-14 15:39:59 -04005206 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005207 msg.rpc_argp = &data->args;
5208 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005209 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005210 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005211 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005212 if (!issync)
5213 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005214 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005215 if (status != 0)
5216 goto out;
5217 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005218 if (status == 0)
5219 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5220 else
5221 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005222out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005223 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005224 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225}
5226
Trond Myklebuste6f81072008-01-24 18:14:34 -05005227int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228{
5229 struct nfs_server *server = NFS_SERVER(inode);
5230 struct nfs4_exception exception = { };
5231 int err;
5232 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005233 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005234 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 switch (err) {
5236 case -NFS4ERR_STALE_STATEID:
5237 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 case 0:
5239 return 0;
5240 }
5241 err = nfs4_handle_exception(server, err, &exception);
5242 } while (exception.retry);
5243 return err;
5244}
5245
5246#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5247#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5248
5249/*
5250 * sleep, with exponential backoff, and retry the LOCK operation.
5251 */
5252static unsigned long
5253nfs4_set_lock_task_retry(unsigned long timeout)
5254{
Colin Cross416ad3c2013-05-06 23:50:06 +00005255 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 timeout <<= 1;
5257 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5258 return NFS4_LOCK_MAXTIMEOUT;
5259 return timeout;
5260}
5261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5263{
5264 struct inode *inode = state->inode;
5265 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005266 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005267 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005269 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005271 struct nfs_lockt_res res = {
5272 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 };
5274 struct rpc_message msg = {
5275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5276 .rpc_argp = &arg,
5277 .rpc_resp = &res,
5278 .rpc_cred = state->owner->so_cred,
5279 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 struct nfs4_lock_state *lsp;
5281 int status;
5282
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005283 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005284 status = nfs4_set_lock_state(state, request);
5285 if (status != 0)
5286 goto out;
5287 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005288 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005289 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005290 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005291 switch (status) {
5292 case 0:
5293 request->fl_type = F_UNLCK;
5294 break;
5295 case -NFS4ERR_DENIED:
5296 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005298 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005299 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005300out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 return status;
5302}
5303
5304static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5305{
5306 struct nfs4_exception exception = { };
5307 int err;
5308
5309 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005310 err = _nfs4_proc_getlk(state, cmd, request);
5311 trace_nfs4_get_lock(request, state, cmd, err);
5312 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 &exception);
5314 } while (exception.retry);
5315 return err;
5316}
5317
5318static int do_vfs_lock(struct file *file, struct file_lock *fl)
5319{
5320 int res = 0;
5321 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5322 case FL_POSIX:
5323 res = posix_lock_file_wait(file, fl);
5324 break;
5325 case FL_FLOCK:
5326 res = flock_lock_file_wait(file, fl);
5327 break;
5328 default:
5329 BUG();
5330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 return res;
5332}
5333
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005334struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005335 struct nfs_locku_args arg;
5336 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005337 struct nfs4_lock_state *lsp;
5338 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005339 struct file_lock fl;
5340 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005341 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005342};
5343
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005344static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5345 struct nfs_open_context *ctx,
5346 struct nfs4_lock_state *lsp,
5347 struct nfs_seqid *seqid)
5348{
5349 struct nfs4_unlockdata *p;
5350 struct inode *inode = lsp->ls_state->inode;
5351
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005352 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005353 if (p == NULL)
5354 return NULL;
5355 p->arg.fh = NFS_FH(inode);
5356 p->arg.fl = &p->fl;
5357 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005358 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005359 p->arg.stateid = &lsp->ls_stateid;
5360 p->lsp = lsp;
5361 atomic_inc(&lsp->ls_count);
5362 /* Ensure we don't close file until we're done freeing locks! */
5363 p->ctx = get_nfs_open_context(ctx);
5364 memcpy(&p->fl, fl, sizeof(p->fl));
5365 p->server = NFS_SERVER(inode);
5366 return p;
5367}
5368
Trond Myklebust06f814a2006-01-03 09:55:07 +01005369static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005370{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005371 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005372 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005373 nfs4_put_lock_state(calldata->lsp);
5374 put_nfs_open_context(calldata->ctx);
5375 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005376}
5377
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005378static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005379{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005380 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005381
Trond Myklebust14516c32010-07-31 14:29:06 -04005382 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5383 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005384 switch (task->tk_status) {
5385 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005386 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5387 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005388 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005389 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005390 case -NFS4ERR_BAD_STATEID:
5391 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005392 case -NFS4ERR_STALE_STATEID:
5393 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005394 break;
5395 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005396 if (nfs4_async_handle_error(task, calldata->server,
5397 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005398 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005399 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005400 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005401}
5402
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005403static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005404{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005405 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005407 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005408 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005409 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005410 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005411 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005412 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005413 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005414 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005415 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005416 &calldata->res.seq_res,
5417 task) != 0)
5418 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005419 return;
5420out_no_action:
5421 task->tk_action = NULL;
5422out_wait:
5423 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424}
5425
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005426static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005427 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005428 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005429 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005430};
5431
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005432static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5433 struct nfs_open_context *ctx,
5434 struct nfs4_lock_state *lsp,
5435 struct nfs_seqid *seqid)
5436{
5437 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005438 struct rpc_message msg = {
5439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5440 .rpc_cred = ctx->cred,
5441 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005442 struct rpc_task_setup task_setup_data = {
5443 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005444 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005445 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005446 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005447 .flags = RPC_TASK_ASYNC,
5448 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005449
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005450 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5451 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5452
Frank Filz137d6ac2007-07-09 15:32:29 -07005453 /* Ensure this is an unlock - when canceling a lock, the
5454 * canceled lock is passed in, and it won't be an unlock.
5455 */
5456 fl->fl_type = F_UNLCK;
5457
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005458 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5459 if (data == NULL) {
5460 nfs_free_seqid(seqid);
5461 return ERR_PTR(-ENOMEM);
5462 }
5463
Chuck Levera9c92d62013-08-09 12:48:18 -04005464 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005465 msg.rpc_argp = &data->arg;
5466 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005467 task_setup_data.callback_data = data;
5468 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005469}
5470
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5472{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005473 struct inode *inode = state->inode;
5474 struct nfs4_state_owner *sp = state->owner;
5475 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005476 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005477 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005478 struct rpc_task *task;
5479 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005480 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
Trond Myklebust9b073572006-06-29 16:38:34 -04005482 status = nfs4_set_lock_state(state, request);
5483 /* Unlock _before_ we do the RPC call */
5484 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005485 /* Exclude nfs_delegation_claim_locks() */
5486 mutex_lock(&sp->so_delegreturn_mutex);
5487 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005488 down_read(&nfsi->rwsem);
5489 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5490 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005491 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005492 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005493 }
5494 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005495 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005496 if (status != 0)
5497 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005498 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005499 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005500 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5501 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005502 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005503 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005504 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005505 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005506 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005507 status = PTR_ERR(task);
5508 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005509 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005510 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005511 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005512out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005513 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005514 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005515 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516}
5517
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005518struct nfs4_lockdata {
5519 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005520 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005521 struct nfs4_lock_state *lsp;
5522 struct nfs_open_context *ctx;
5523 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005524 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005525 int rpc_status;
5526 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005527 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005528};
5529
5530static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005531 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5532 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005533{
5534 struct nfs4_lockdata *p;
5535 struct inode *inode = lsp->ls_state->inode;
5536 struct nfs_server *server = NFS_SERVER(inode);
5537
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005538 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005539 if (p == NULL)
5540 return NULL;
5541
5542 p->arg.fh = NFS_FH(inode);
5543 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005544 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005545 if (p->arg.open_seqid == NULL)
5546 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005547 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005548 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005549 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005550 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005551 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005552 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005553 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005554 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005555 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005556 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005557 atomic_inc(&lsp->ls_count);
5558 p->ctx = get_nfs_open_context(ctx);
5559 memcpy(&p->fl, fl, sizeof(p->fl));
5560 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005561out_free_seqid:
5562 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005563out_free:
5564 kfree(p);
5565 return NULL;
5566}
5567
5568static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5569{
5570 struct nfs4_lockdata *data = calldata;
5571 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Harvey Harrison3110ff82008-05-02 13:42:44 -07005573 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005574 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005575 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005576 /* Do we need to do an open_to_lock_owner? */
5577 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005578 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005579 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005580 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005581 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005582 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005583 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005584 } else
5585 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005586 if (!nfs4_valid_open_stateid(state)) {
5587 data->rpc_status = -EBADF;
5588 task->tk_action = NULL;
5589 goto out_release_open_seqid;
5590 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005591 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005592 if (nfs4_setup_sequence(data->server,
5593 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005594 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005595 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005596 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005597out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005598 nfs_release_seqid(data->arg.open_seqid);
5599out_release_lock_seqid:
5600 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005601out_wait:
5602 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005603 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005604}
5605
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005606static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5607{
5608 struct nfs4_lockdata *data = calldata;
5609
Harvey Harrison3110ff82008-05-02 13:42:44 -07005610 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005611
Trond Myklebust14516c32010-07-31 14:29:06 -04005612 if (!nfs4_sequence_done(task, &data->res.seq_res))
5613 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005614
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005615 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005616 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005617 if (data->rpc_status == 0)
5618 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5619 else
5620 goto out;
5621 }
5622 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005623 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005624 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005625 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005626 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005627out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005628 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005629}
5630
5631static void nfs4_lock_release(void *calldata)
5632{
5633 struct nfs4_lockdata *data = calldata;
5634
Harvey Harrison3110ff82008-05-02 13:42:44 -07005635 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005636 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005637 if (data->cancelled != 0) {
5638 struct rpc_task *task;
5639 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5640 data->arg.lock_seqid);
5641 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005642 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005643 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005644 } else
5645 nfs_free_seqid(data->arg.lock_seqid);
5646 nfs4_put_lock_state(data->lsp);
5647 put_nfs_open_context(data->ctx);
5648 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005649 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005650}
5651
5652static const struct rpc_call_ops nfs4_lock_ops = {
5653 .rpc_call_prepare = nfs4_lock_prepare,
5654 .rpc_call_done = nfs4_lock_done,
5655 .rpc_release = nfs4_lock_release,
5656};
5657
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005658static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5659{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005660 switch (error) {
5661 case -NFS4ERR_ADMIN_REVOKED:
5662 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005663 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005664 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005665 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005666 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005667 break;
5668 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005669 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005670 case -NFS4ERR_EXPIRED:
5671 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005672 };
5673}
5674
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005675static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005676{
5677 struct nfs4_lockdata *data;
5678 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005679 struct rpc_message msg = {
5680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5681 .rpc_cred = state->owner->so_cred,
5682 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005683 struct rpc_task_setup task_setup_data = {
5684 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005685 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005686 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005687 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005688 .flags = RPC_TASK_ASYNC,
5689 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005690 int ret;
5691
Harvey Harrison3110ff82008-05-02 13:42:44 -07005692 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005693 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005694 fl->fl_u.nfs4_fl.owner,
5695 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005696 if (data == NULL)
5697 return -ENOMEM;
5698 if (IS_SETLKW(cmd))
5699 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005700 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005701 msg.rpc_argp = &data->arg;
5702 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005703 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005704 if (recovery_type > NFS_LOCK_NEW) {
5705 if (recovery_type == NFS_LOCK_RECLAIM)
5706 data->arg.reclaim = NFS_LOCK_RECLAIM;
5707 nfs4_set_sequence_privileged(&data->arg.seq_args);
5708 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005709 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005710 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005711 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005712 ret = nfs4_wait_for_completion_rpc_task(task);
5713 if (ret == 0) {
5714 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005715 if (ret)
5716 nfs4_handle_setlk_error(data->server, data->lsp,
5717 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005718 } else
5719 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005720 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005721 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005722 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723}
5724
5725static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5726{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005727 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005728 struct nfs4_exception exception = {
5729 .inode = state->inode,
5730 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005731 int err;
5732
5733 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005734 /* Cache the lock if possible... */
5735 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5736 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005737 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005738 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005739 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005740 break;
5741 nfs4_handle_exception(server, err, &exception);
5742 } while (exception.retry);
5743 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744}
5745
5746static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5747{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005748 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005749 struct nfs4_exception exception = {
5750 .inode = state->inode,
5751 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005752 int err;
5753
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005754 err = nfs4_set_lock_state(state, request);
5755 if (err != 0)
5756 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005757 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005758 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5759 return 0;
5760 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005761 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005762 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5763 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005764 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005765 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005766 switch (err) {
5767 default:
5768 goto out;
5769 case -NFS4ERR_GRACE:
5770 case -NFS4ERR_DELAY:
5771 nfs4_handle_exception(server, err, &exception);
5772 err = 0;
5773 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005774 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005775out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005776 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777}
5778
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005779#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005780/**
5781 * nfs41_check_expired_locks - possibly free a lock stateid
5782 *
5783 * @state: NFSv4 state for an inode
5784 *
5785 * Returns NFS_OK if recovery for this stateid is now finished.
5786 * Otherwise a negative NFS4ERR value is returned.
5787 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005788static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005789{
Chuck Levereb64cf92012-07-11 16:30:05 -04005790 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005791 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005792 struct nfs_server *server = NFS_SERVER(state->inode);
5793
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005794 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005795 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005796 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5797
5798 status = nfs41_test_stateid(server,
5799 &lsp->ls_stateid,
5800 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005801 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005802 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005803 /* Free the stateid unless the server
5804 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005805 if (status != -NFS4ERR_BAD_STATEID)
5806 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005807 &lsp->ls_stateid,
5808 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005809 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005810 ret = status;
5811 }
5812 }
5813 };
5814
5815 return ret;
5816}
5817
5818static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5819{
5820 int status = NFS_OK;
5821
5822 if (test_bit(LK_STATE_IN_USE, &state->flags))
5823 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005824 if (status != NFS_OK)
5825 status = nfs4_lock_expired(state, request);
5826 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005827}
5828#endif
5829
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5831{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005832 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005833 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005834 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005835 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005836 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005838 if ((fl_flags & FL_POSIX) &&
5839 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5840 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005841 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005842 status = nfs4_set_lock_state(state, request);
5843 if (status != 0)
5844 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005845 request->fl_flags |= FL_ACCESS;
5846 status = do_vfs_lock(request->fl_file, request);
5847 if (status < 0)
5848 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005849 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005850 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005851 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005852 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005853 request->fl_flags = fl_flags & ~FL_SLEEP;
5854 status = do_vfs_lock(request->fl_file, request);
5855 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005856 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005857 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5858 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005859 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005860 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005861 goto out;
5862 down_read(&nfsi->rwsem);
5863 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5864 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005865 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005866 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005867 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005868 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005869 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005870 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5871 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005872out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005873 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005874out:
5875 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 return status;
5877}
5878
5879static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5880{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005881 struct nfs4_exception exception = {
5882 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005883 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005884 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 int err;
5886
5887 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005888 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005889 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005890 if (err == -NFS4ERR_DENIED)
5891 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005893 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 } while (exception.retry);
5895 return err;
5896}
5897
5898static int
5899nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5900{
5901 struct nfs_open_context *ctx;
5902 struct nfs4_state *state;
5903 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5904 int status;
5905
5906 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005907 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 state = ctx->state;
5909
5910 if (request->fl_start < 0 || request->fl_end < 0)
5911 return -EINVAL;
5912
Trond Myklebustd9531262009-07-21 19:22:38 -04005913 if (IS_GETLK(cmd)) {
5914 if (state != NULL)
5915 return nfs4_proc_getlk(state, F_GETLK, request);
5916 return 0;
5917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
5919 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5920 return -EINVAL;
5921
Trond Myklebustd9531262009-07-21 19:22:38 -04005922 if (request->fl_type == F_UNLCK) {
5923 if (state != NULL)
5924 return nfs4_proc_unlck(state, cmd, request);
5925 return 0;
5926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
Trond Myklebustd9531262009-07-21 19:22:38 -04005928 if (state == NULL)
5929 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005930 /*
5931 * Don't rely on the VFS having checked the file open mode,
5932 * since it won't do this for flock() locks.
5933 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005934 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005935 case F_RDLCK:
5936 if (!(filp->f_mode & FMODE_READ))
5937 return -EBADF;
5938 break;
5939 case F_WRLCK:
5940 if (!(filp->f_mode & FMODE_WRITE))
5941 return -EBADF;
5942 }
5943
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 do {
5945 status = nfs4_proc_setlk(state, cmd, request);
5946 if ((status != -EAGAIN) || IS_SETLK(cmd))
5947 break;
5948 timeout = nfs4_set_lock_task_retry(timeout);
5949 status = -ERESTARTSYS;
5950 if (signalled())
5951 break;
5952 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 return status;
5954}
5955
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005956int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005957{
5958 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005959 int err;
5960
5961 err = nfs4_set_lock_state(state, fl);
5962 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005963 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005964 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005965 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005966}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005967
Trond Myklebustcf470c32012-03-07 13:49:12 -05005968struct nfs_release_lockowner_data {
5969 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005970 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005971 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005972 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04005973 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005974};
5975
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005976static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5977{
5978 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08005979 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08005980 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
5981 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08005982 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04005983 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005984}
5985
5986static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5987{
5988 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04005989 struct nfs_server *server = data->server;
5990
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005991 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04005992
5993 switch (task->tk_status) {
5994 case 0:
5995 renew_lease(server, data->timestamp);
5996 break;
5997 case -NFS4ERR_STALE_CLIENTID:
5998 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08005999 nfs4_schedule_lease_recovery(server->nfs_client);
6000 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006001 case -NFS4ERR_LEASE_MOVED:
6002 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006003 if (nfs4_async_handle_error(task, server,
6004 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006005 rpc_restart_call_prepare(task);
6006 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006007}
6008
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006009static void nfs4_release_lockowner_release(void *calldata)
6010{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006011 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006012 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006013 kfree(calldata);
6014}
6015
Trond Myklebust17280172012-03-11 13:11:00 -04006016static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006017 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6018 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006019 .rpc_release = nfs4_release_lockowner_release,
6020};
6021
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006022static void
6023nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006024{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006025 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006026 struct rpc_message msg = {
6027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6028 };
6029
6030 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006031 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006032
Trond Myklebustcf470c32012-03-07 13:49:12 -05006033 data = kmalloc(sizeof(*data), GFP_NOFS);
6034 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006035 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006036 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006037 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006038 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6039 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6040 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006041
Trond Myklebustcf470c32012-03-07 13:49:12 -05006042 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006043 msg.rpc_resp = &data->res;
6044 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006045 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006046}
6047
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006048#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6049
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006050static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6051 const void *buf, size_t buflen,
6052 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006053{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006054 if (strcmp(key, "") != 0)
6055 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006056
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006057 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006058}
6059
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006060static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6061 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006062{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006063 if (strcmp(key, "") != 0)
6064 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006065
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006066 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006067}
6068
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006069static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6070 size_t list_len, const char *name,
6071 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006072{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006073 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006074
J. Bruce Fields096455a2006-03-20 23:23:42 -05006075 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
6076 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006077
6078 if (list && len <= list_len)
6079 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006080 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006081}
6082
David Quigleyc9bccef2013-05-22 12:50:45 -04006083#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6084static inline int nfs4_server_supports_labels(struct nfs_server *server)
6085{
6086 return server->caps & NFS_CAP_SECURITY_LABEL;
6087}
6088
6089static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6090 const void *buf, size_t buflen,
6091 int flags, int type)
6092{
6093 if (security_ismaclabel(key))
6094 return nfs4_set_security_label(dentry, buf, buflen);
6095
6096 return -EOPNOTSUPP;
6097}
6098
6099static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6100 void *buf, size_t buflen, int type)
6101{
6102 if (security_ismaclabel(key))
6103 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
6104 return -EOPNOTSUPP;
6105}
6106
6107static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6108 size_t list_len, const char *name,
6109 size_t name_len, int type)
6110{
6111 size_t len = 0;
6112
6113 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
6114 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
6115 if (list && len <= list_len)
6116 security_inode_listsecurity(dentry->d_inode, list, len);
6117 }
6118 return len;
6119}
6120
6121static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6122 .prefix = XATTR_SECURITY_PREFIX,
6123 .list = nfs4_xattr_list_nfs4_label,
6124 .get = nfs4_xattr_get_nfs4_label,
6125 .set = nfs4_xattr_set_nfs4_label,
6126};
6127#endif
6128
6129
Andy Adamson533eb462011-06-13 18:25:56 -04006130/*
6131 * nfs_fhget will use either the mounted_on_fileid or the fileid
6132 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006133static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6134{
Andy Adamson533eb462011-06-13 18:25:56 -04006135 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6136 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6137 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006138 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006139 return;
6140
6141 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006142 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006143 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6144 fattr->nlink = 2;
6145}
6146
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006147static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6148 const struct qstr *name,
6149 struct nfs4_fs_locations *fs_locations,
6150 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006151{
6152 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006153 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006154 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006155 };
6156 struct nfs4_fs_locations_arg args = {
6157 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006158 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006159 .page = page,
6160 .bitmask = bitmask,
6161 };
Benny Halevy22958462009-04-01 09:22:02 -04006162 struct nfs4_fs_locations_res res = {
6163 .fs_locations = fs_locations,
6164 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006165 struct rpc_message msg = {
6166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6167 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006168 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006169 };
6170 int status;
6171
Harvey Harrison3110ff82008-05-02 13:42:44 -07006172 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006173
6174 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6175 * is not supported */
6176 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6177 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6178 else
6179 bitmask[0] |= FATTR4_WORD0_FILEID;
6180
Trond Myklebustc228fd32007-01-13 02:28:11 -05006181 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006182 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006183 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006184 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006185 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006186 return status;
6187}
6188
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006189int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6190 const struct qstr *name,
6191 struct nfs4_fs_locations *fs_locations,
6192 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006193{
6194 struct nfs4_exception exception = { };
6195 int err;
6196 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006197 err = _nfs4_proc_fs_locations(client, dir, name,
6198 fs_locations, page);
6199 trace_nfs4_get_fs_locations(dir, name, err);
6200 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006201 &exception);
6202 } while (exception.retry);
6203 return err;
6204}
6205
Chuck Leverb03d7352013-10-17 14:12:50 -04006206/*
6207 * This operation also signals the server that this client is
6208 * performing migration recovery. The server can stop returning
6209 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6210 * appended to this compound to identify the client ID which is
6211 * performing recovery.
6212 */
6213static int _nfs40_proc_get_locations(struct inode *inode,
6214 struct nfs4_fs_locations *locations,
6215 struct page *page, struct rpc_cred *cred)
6216{
6217 struct nfs_server *server = NFS_SERVER(inode);
6218 struct rpc_clnt *clnt = server->client;
6219 u32 bitmask[2] = {
6220 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6221 };
6222 struct nfs4_fs_locations_arg args = {
6223 .clientid = server->nfs_client->cl_clientid,
6224 .fh = NFS_FH(inode),
6225 .page = page,
6226 .bitmask = bitmask,
6227 .migration = 1, /* skip LOOKUP */
6228 .renew = 1, /* append RENEW */
6229 };
6230 struct nfs4_fs_locations_res res = {
6231 .fs_locations = locations,
6232 .migration = 1,
6233 .renew = 1,
6234 };
6235 struct rpc_message msg = {
6236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6237 .rpc_argp = &args,
6238 .rpc_resp = &res,
6239 .rpc_cred = cred,
6240 };
6241 unsigned long now = jiffies;
6242 int status;
6243
6244 nfs_fattr_init(&locations->fattr);
6245 locations->server = server;
6246 locations->nlocations = 0;
6247
6248 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6249 nfs4_set_sequence_privileged(&args.seq_args);
6250 status = nfs4_call_sync_sequence(clnt, server, &msg,
6251 &args.seq_args, &res.seq_res);
6252 if (status)
6253 return status;
6254
6255 renew_lease(server, now);
6256 return 0;
6257}
6258
6259#ifdef CONFIG_NFS_V4_1
6260
6261/*
6262 * This operation also signals the server that this client is
6263 * performing migration recovery. The server can stop asserting
6264 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6265 * performing this operation is identified in the SEQUENCE
6266 * operation in this compound.
6267 *
6268 * When the client supports GETATTR(fs_locations_info), it can
6269 * be plumbed in here.
6270 */
6271static int _nfs41_proc_get_locations(struct inode *inode,
6272 struct nfs4_fs_locations *locations,
6273 struct page *page, struct rpc_cred *cred)
6274{
6275 struct nfs_server *server = NFS_SERVER(inode);
6276 struct rpc_clnt *clnt = server->client;
6277 u32 bitmask[2] = {
6278 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6279 };
6280 struct nfs4_fs_locations_arg args = {
6281 .fh = NFS_FH(inode),
6282 .page = page,
6283 .bitmask = bitmask,
6284 .migration = 1, /* skip LOOKUP */
6285 };
6286 struct nfs4_fs_locations_res res = {
6287 .fs_locations = locations,
6288 .migration = 1,
6289 };
6290 struct rpc_message msg = {
6291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6292 .rpc_argp = &args,
6293 .rpc_resp = &res,
6294 .rpc_cred = cred,
6295 };
6296 int status;
6297
6298 nfs_fattr_init(&locations->fattr);
6299 locations->server = server;
6300 locations->nlocations = 0;
6301
6302 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6303 nfs4_set_sequence_privileged(&args.seq_args);
6304 status = nfs4_call_sync_sequence(clnt, server, &msg,
6305 &args.seq_args, &res.seq_res);
6306 if (status == NFS4_OK &&
6307 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6308 status = -NFS4ERR_LEASE_MOVED;
6309 return status;
6310}
6311
6312#endif /* CONFIG_NFS_V4_1 */
6313
6314/**
6315 * nfs4_proc_get_locations - discover locations for a migrated FSID
6316 * @inode: inode on FSID that is migrating
6317 * @locations: result of query
6318 * @page: buffer
6319 * @cred: credential to use for this operation
6320 *
6321 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6322 * operation failed, or a negative errno if a local error occurred.
6323 *
6324 * On success, "locations" is filled in, but if the server has
6325 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6326 * asserted.
6327 *
6328 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6329 * from this client that require migration recovery.
6330 */
6331int nfs4_proc_get_locations(struct inode *inode,
6332 struct nfs4_fs_locations *locations,
6333 struct page *page, struct rpc_cred *cred)
6334{
6335 struct nfs_server *server = NFS_SERVER(inode);
6336 struct nfs_client *clp = server->nfs_client;
6337 const struct nfs4_mig_recovery_ops *ops =
6338 clp->cl_mvops->mig_recovery_ops;
6339 struct nfs4_exception exception = { };
6340 int status;
6341
6342 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6343 (unsigned long long)server->fsid.major,
6344 (unsigned long long)server->fsid.minor,
6345 clp->cl_hostname);
6346 nfs_display_fhandle(NFS_FH(inode), __func__);
6347
6348 do {
6349 status = ops->get_locations(inode, locations, page, cred);
6350 if (status != -NFS4ERR_DELAY)
6351 break;
6352 nfs4_handle_exception(server, status, &exception);
6353 } while (exception.retry);
6354 return status;
6355}
6356
Chuck Lever44c99932013-10-17 14:13:30 -04006357/*
6358 * This operation also signals the server that this client is
6359 * performing "lease moved" recovery. The server can stop
6360 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6361 * is appended to this compound to identify the client ID which is
6362 * performing recovery.
6363 */
6364static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6365{
6366 struct nfs_server *server = NFS_SERVER(inode);
6367 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6368 struct rpc_clnt *clnt = server->client;
6369 struct nfs4_fsid_present_arg args = {
6370 .fh = NFS_FH(inode),
6371 .clientid = clp->cl_clientid,
6372 .renew = 1, /* append RENEW */
6373 };
6374 struct nfs4_fsid_present_res res = {
6375 .renew = 1,
6376 };
6377 struct rpc_message msg = {
6378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6379 .rpc_argp = &args,
6380 .rpc_resp = &res,
6381 .rpc_cred = cred,
6382 };
6383 unsigned long now = jiffies;
6384 int status;
6385
6386 res.fh = nfs_alloc_fhandle();
6387 if (res.fh == NULL)
6388 return -ENOMEM;
6389
6390 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6391 nfs4_set_sequence_privileged(&args.seq_args);
6392 status = nfs4_call_sync_sequence(clnt, server, &msg,
6393 &args.seq_args, &res.seq_res);
6394 nfs_free_fhandle(res.fh);
6395 if (status)
6396 return status;
6397
6398 do_renew_lease(clp, now);
6399 return 0;
6400}
6401
6402#ifdef CONFIG_NFS_V4_1
6403
6404/*
6405 * This operation also signals the server that this client is
6406 * performing "lease moved" recovery. The server can stop asserting
6407 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6408 * this operation is identified in the SEQUENCE operation in this
6409 * compound.
6410 */
6411static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6412{
6413 struct nfs_server *server = NFS_SERVER(inode);
6414 struct rpc_clnt *clnt = server->client;
6415 struct nfs4_fsid_present_arg args = {
6416 .fh = NFS_FH(inode),
6417 };
6418 struct nfs4_fsid_present_res res = {
6419 };
6420 struct rpc_message msg = {
6421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6422 .rpc_argp = &args,
6423 .rpc_resp = &res,
6424 .rpc_cred = cred,
6425 };
6426 int status;
6427
6428 res.fh = nfs_alloc_fhandle();
6429 if (res.fh == NULL)
6430 return -ENOMEM;
6431
6432 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6433 nfs4_set_sequence_privileged(&args.seq_args);
6434 status = nfs4_call_sync_sequence(clnt, server, &msg,
6435 &args.seq_args, &res.seq_res);
6436 nfs_free_fhandle(res.fh);
6437 if (status == NFS4_OK &&
6438 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6439 status = -NFS4ERR_LEASE_MOVED;
6440 return status;
6441}
6442
6443#endif /* CONFIG_NFS_V4_1 */
6444
6445/**
6446 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6447 * @inode: inode on FSID to check
6448 * @cred: credential to use for this operation
6449 *
6450 * Server indicates whether the FSID is present, moved, or not
6451 * recognized. This operation is necessary to clear a LEASE_MOVED
6452 * condition for this client ID.
6453 *
6454 * Returns NFS4_OK if the FSID is present on this server,
6455 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6456 * NFS4ERR code if some error occurred on the server, or a
6457 * negative errno if a local failure occurred.
6458 */
6459int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6460{
6461 struct nfs_server *server = NFS_SERVER(inode);
6462 struct nfs_client *clp = server->nfs_client;
6463 const struct nfs4_mig_recovery_ops *ops =
6464 clp->cl_mvops->mig_recovery_ops;
6465 struct nfs4_exception exception = { };
6466 int status;
6467
6468 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6469 (unsigned long long)server->fsid.major,
6470 (unsigned long long)server->fsid.minor,
6471 clp->cl_hostname);
6472 nfs_display_fhandle(NFS_FH(inode), __func__);
6473
6474 do {
6475 status = ops->fsid_present(inode, cred);
6476 if (status != -NFS4ERR_DELAY)
6477 break;
6478 nfs4_handle_exception(server, status, &exception);
6479 } while (exception.retry);
6480 return status;
6481}
6482
Andy Adamson5ec16a82013-08-08 10:57:55 -04006483/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006484 * If 'use_integrity' is true and the state managment nfs_client
6485 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6486 * and the machine credential as per RFC3530bis and RFC5661 Security
6487 * Considerations sections. Otherwise, just use the user cred with the
6488 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006489 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006490static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006491{
6492 int status;
6493 struct nfs4_secinfo_arg args = {
6494 .dir_fh = NFS_FH(dir),
6495 .name = name,
6496 };
6497 struct nfs4_secinfo_res res = {
6498 .flavors = flavors,
6499 };
6500 struct rpc_message msg = {
6501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6502 .rpc_argp = &args,
6503 .rpc_resp = &res,
6504 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006505 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006506 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006507
6508 if (use_integrity) {
6509 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006510 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6511 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006512 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006513
6514 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006515
6516 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6517 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6518
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006519 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6520 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006521 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006522
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006523 if (cred)
6524 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006525
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006526 return status;
6527}
6528
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006529int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6530 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006531{
6532 struct nfs4_exception exception = { };
6533 int err;
6534 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006535 err = -NFS4ERR_WRONGSEC;
6536
6537 /* try to use integrity protection with machine cred */
6538 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6539 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6540
6541 /*
6542 * if unable to use integrity protection, or SECINFO with
6543 * integrity protection returns NFS4ERR_WRONGSEC (which is
6544 * disallowed by spec, but exists in deployed servers) use
6545 * the current filesystem's rpc_client and the user cred.
6546 */
6547 if (err == -NFS4ERR_WRONGSEC)
6548 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6549
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006550 trace_nfs4_secinfo(dir, name, err);
6551 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006552 &exception);
6553 } while (exception.retry);
6554 return err;
6555}
6556
Andy Adamson557134a2009-04-01 09:21:53 -04006557#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006558/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006559 * Check the exchange flags returned by the server for invalid flags, having
6560 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6561 * DS flags set.
6562 */
6563static int nfs4_check_cl_exchange_flags(u32 flags)
6564{
6565 if (flags & ~EXCHGID4_FLAG_MASK_R)
6566 goto out_inval;
6567 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6568 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6569 goto out_inval;
6570 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6571 goto out_inval;
6572 return NFS_OK;
6573out_inval:
6574 return -NFS4ERR_INVAL;
6575}
6576
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006577static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006578nfs41_same_server_scope(struct nfs41_server_scope *a,
6579 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006580{
6581 if (a->server_scope_sz == b->server_scope_sz &&
6582 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6583 return true;
6584
6585 return false;
6586}
6587
Andy Adamson357f54d2010-12-14 10:11:57 -05006588/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006589 * nfs4_proc_bind_conn_to_session()
6590 *
6591 * The 4.1 client currently uses the same TCP connection for the
6592 * fore and backchannel.
6593 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006594int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006595{
6596 int status;
6597 struct nfs41_bind_conn_to_session_res res;
6598 struct rpc_message msg = {
6599 .rpc_proc =
6600 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6601 .rpc_argp = clp,
6602 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006603 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006604 };
6605
6606 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006607
6608 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6609 if (unlikely(res.session == NULL)) {
6610 status = -ENOMEM;
6611 goto out;
6612 }
6613
6614 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006615 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006616 if (status == 0) {
6617 if (memcmp(res.session->sess_id.data,
6618 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6619 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6620 status = -EIO;
6621 goto out_session;
6622 }
6623 if (res.dir != NFS4_CDFS4_BOTH) {
6624 dprintk("NFS: %s: Unexpected direction from server\n",
6625 __func__);
6626 status = -EIO;
6627 goto out_session;
6628 }
6629 if (res.use_conn_in_rdma_mode) {
6630 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6631 __func__);
6632 status = -EIO;
6633 goto out_session;
6634 }
6635 }
6636out_session:
6637 kfree(res.session);
6638out:
6639 dprintk("<-- %s status= %d\n", __func__, status);
6640 return status;
6641}
6642
6643/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006644 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6645 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006646 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006647static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6648 .how = SP4_MACH_CRED,
6649 .enforce.u.words = {
6650 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6651 1 << (OP_EXCHANGE_ID - 32) |
6652 1 << (OP_CREATE_SESSION - 32) |
6653 1 << (OP_DESTROY_SESSION - 32) |
6654 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006655 },
6656 .allow.u.words = {
6657 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006658 1 << (OP_LOCKU) |
6659 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006660 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006661 1 << (OP_SECINFO_NO_NAME - 32) |
6662 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006663 1 << (OP_FREE_STATEID - 32) |
6664 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006665 }
6666};
6667
6668/*
6669 * Select the state protection mode for client `clp' given the server results
6670 * from exchange_id in `sp'.
6671 *
6672 * Returns 0 on success, negative errno otherwise.
6673 */
6674static int nfs4_sp4_select_mode(struct nfs_client *clp,
6675 struct nfs41_state_protection *sp)
6676{
6677 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6678 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6679 1 << (OP_EXCHANGE_ID - 32) |
6680 1 << (OP_CREATE_SESSION - 32) |
6681 1 << (OP_DESTROY_SESSION - 32) |
6682 1 << (OP_DESTROY_CLIENTID - 32)
6683 };
6684 unsigned int i;
6685
6686 if (sp->how == SP4_MACH_CRED) {
6687 /* Print state protect result */
6688 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6689 for (i = 0; i <= LAST_NFS4_OP; i++) {
6690 if (test_bit(i, sp->enforce.u.longs))
6691 dfprintk(MOUNT, " enforce op %d\n", i);
6692 if (test_bit(i, sp->allow.u.longs))
6693 dfprintk(MOUNT, " allow op %d\n", i);
6694 }
6695
6696 /* make sure nothing is on enforce list that isn't supported */
6697 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6698 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6699 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6700 return -EINVAL;
6701 }
6702 }
6703
6704 /*
6705 * Minimal mode - state operations are allowed to use machine
6706 * credential. Note this already happens by default, so the
6707 * client doesn't have to do anything more than the negotiation.
6708 *
6709 * NOTE: we don't care if EXCHANGE_ID is in the list -
6710 * we're already using the machine cred for exchange_id
6711 * and will never use a different cred.
6712 */
6713 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6714 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6715 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6716 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6717 dfprintk(MOUNT, "sp4_mach_cred:\n");
6718 dfprintk(MOUNT, " minimal mode enabled\n");
6719 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6720 } else {
6721 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6722 return -EINVAL;
6723 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006724
6725 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6726 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6727 dfprintk(MOUNT, " cleanup mode enabled\n");
6728 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6729 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006730
6731 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6732 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6733 dfprintk(MOUNT, " secinfo mode enabled\n");
6734 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6735 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006736
6737 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6738 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6739 dfprintk(MOUNT, " stateid mode enabled\n");
6740 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6741 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006742
6743 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6744 dfprintk(MOUNT, " write mode enabled\n");
6745 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6746 }
6747
6748 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6749 dfprintk(MOUNT, " commit mode enabled\n");
6750 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6751 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006752 }
6753
6754 return 0;
6755}
6756
6757/*
6758 * _nfs4_proc_exchange_id()
6759 *
6760 * Wrapper for EXCHANGE_ID operation.
6761 */
6762static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6763 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006764{
6765 nfs4_verifier verifier;
6766 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006767 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006768 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006769#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006770 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006771 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6772 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6773#else
6774 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6775 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6776#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006777 };
6778 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006779 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006780 };
6781 int status;
6782 struct rpc_message msg = {
6783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6784 .rpc_argp = &args,
6785 .rpc_resp = &res,
6786 .rpc_cred = cred,
6787 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006788
Chuck Leverf0920752012-05-21 22:45:41 -04006789 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006790 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6791 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006792 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6793 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6794 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006795
Chuck Leveracdeb692012-05-21 22:46:16 -04006796 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006797 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006798 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006799 status = -ENOMEM;
6800 goto out;
6801 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006802
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006803 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006804 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006805 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006806 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006807 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006808 }
6809
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006810 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006811 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006812 status = -ENOMEM;
6813 goto out_server_scope;
6814 }
6815
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006816 switch (sp4_how) {
6817 case SP4_NONE:
6818 args.state_protect.how = SP4_NONE;
6819 break;
6820
6821 case SP4_MACH_CRED:
6822 args.state_protect = nfs4_sp4_mach_cred_request;
6823 break;
6824
6825 default:
6826 /* unsupported! */
6827 WARN_ON_ONCE(1);
6828 status = -EINVAL;
6829 goto out_server_scope;
6830 }
6831
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006832 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006833 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006834 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006835 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006836
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006837 if (status == 0)
6838 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6839
Chuck Lever177313f2012-05-21 22:44:58 -04006840 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006841 clp->cl_clientid = res.clientid;
6842 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6843 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6844 clp->cl_seqid = res.seqid;
6845
Chuck Leveracdeb692012-05-21 22:46:16 -04006846 kfree(clp->cl_serverowner);
6847 clp->cl_serverowner = res.server_owner;
6848 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006849
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006850 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006851 kfree(clp->cl_implid);
6852 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006853
Chuck Lever177313f2012-05-21 22:44:58 -04006854 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006855 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006856 res.server_scope)) {
6857 dprintk("%s: server_scope mismatch detected\n",
6858 __func__);
6859 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006860 kfree(clp->cl_serverscope);
6861 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006862 }
6863
Chuck Lever177313f2012-05-21 22:44:58 -04006864 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006865 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006866 goto out;
6867 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006868 } else
6869 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006870
Chuck Leveracdeb692012-05-21 22:46:16 -04006871out_server_owner:
6872 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006873out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006874 kfree(res.server_scope);
6875out:
Chuck Lever177313f2012-05-21 22:44:58 -04006876 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006877 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006878 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006879 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006880 clp->cl_implid->date.seconds,
6881 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006882 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006883 return status;
6884}
6885
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006886/*
6887 * nfs4_proc_exchange_id()
6888 *
6889 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6890 *
6891 * Since the clientid has expired, all compounds using sessions
6892 * associated with the stale clientid will be returning
6893 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6894 * be in some phase of session reset.
6895 *
6896 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6897 */
6898int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6899{
6900 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6901 int status;
6902
6903 /* try SP4_MACH_CRED if krb5i/p */
6904 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6905 authflavor == RPC_AUTH_GSS_KRB5P) {
6906 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6907 if (!status)
6908 return 0;
6909 }
6910
6911 /* try SP4_NONE */
6912 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6913}
6914
Trond Myklebust66245532012-05-25 17:18:09 -04006915static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6916 struct rpc_cred *cred)
6917{
6918 struct rpc_message msg = {
6919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6920 .rpc_argp = clp,
6921 .rpc_cred = cred,
6922 };
6923 int status;
6924
6925 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006926 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006927 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006928 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006929 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6930 return status;
6931}
6932
6933static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6934 struct rpc_cred *cred)
6935{
6936 unsigned int loop;
6937 int ret;
6938
6939 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6940 ret = _nfs4_proc_destroy_clientid(clp, cred);
6941 switch (ret) {
6942 case -NFS4ERR_DELAY:
6943 case -NFS4ERR_CLIENTID_BUSY:
6944 ssleep(1);
6945 break;
6946 default:
6947 return ret;
6948 }
6949 }
6950 return 0;
6951}
6952
6953int nfs4_destroy_clientid(struct nfs_client *clp)
6954{
6955 struct rpc_cred *cred;
6956 int ret = 0;
6957
6958 if (clp->cl_mvops->minor_version < 1)
6959 goto out;
6960 if (clp->cl_exchange_flags == 0)
6961 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006962 if (clp->cl_preserve_clid)
6963 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006964 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006965 ret = nfs4_proc_destroy_clientid(clp, cred);
6966 if (cred)
6967 put_rpccred(cred);
6968 switch (ret) {
6969 case 0:
6970 case -NFS4ERR_STALE_CLIENTID:
6971 clp->cl_exchange_flags = 0;
6972 }
6973out:
6974 return ret;
6975}
6976
Andy Adamson2050f0c2009-04-01 09:22:30 -04006977struct nfs4_get_lease_time_data {
6978 struct nfs4_get_lease_time_args *args;
6979 struct nfs4_get_lease_time_res *res;
6980 struct nfs_client *clp;
6981};
6982
6983static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6984 void *calldata)
6985{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006986 struct nfs4_get_lease_time_data *data =
6987 (struct nfs4_get_lease_time_data *)calldata;
6988
6989 dprintk("--> %s\n", __func__);
6990 /* just setup sequence, do not trigger session recovery
6991 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006992 nfs41_setup_sequence(data->clp->cl_session,
6993 &data->args->la_seq_args,
6994 &data->res->lr_seq_res,
6995 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006996 dprintk("<-- %s\n", __func__);
6997}
6998
6999/*
7000 * Called from nfs4_state_manager thread for session setup, so don't recover
7001 * from sequence operation or clientid errors.
7002 */
7003static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7004{
7005 struct nfs4_get_lease_time_data *data =
7006 (struct nfs4_get_lease_time_data *)calldata;
7007
7008 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007009 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7010 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007011 switch (task->tk_status) {
7012 case -NFS4ERR_DELAY:
7013 case -NFS4ERR_GRACE:
7014 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7015 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7016 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007017 /* fall through */
7018 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007019 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007020 return;
7021 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007022 dprintk("<-- %s\n", __func__);
7023}
7024
Trond Myklebust17280172012-03-11 13:11:00 -04007025static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007026 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7027 .rpc_call_done = nfs4_get_lease_time_done,
7028};
7029
7030int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7031{
7032 struct rpc_task *task;
7033 struct nfs4_get_lease_time_args args;
7034 struct nfs4_get_lease_time_res res = {
7035 .lr_fsinfo = fsinfo,
7036 };
7037 struct nfs4_get_lease_time_data data = {
7038 .args = &args,
7039 .res = &res,
7040 .clp = clp,
7041 };
7042 struct rpc_message msg = {
7043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7044 .rpc_argp = &args,
7045 .rpc_resp = &res,
7046 };
7047 struct rpc_task_setup task_setup = {
7048 .rpc_client = clp->cl_rpcclient,
7049 .rpc_message = &msg,
7050 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007051 .callback_data = &data,
7052 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007053 };
7054 int status;
7055
Chuck Levera9c92d62013-08-09 12:48:18 -04007056 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007057 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007058 dprintk("--> %s\n", __func__);
7059 task = rpc_run_task(&task_setup);
7060
7061 if (IS_ERR(task))
7062 status = PTR_ERR(task);
7063 else {
7064 status = task->tk_status;
7065 rpc_put_task(task);
7066 }
7067 dprintk("<-- %s return %d\n", __func__, status);
7068
7069 return status;
7070}
7071
Andy Adamsonfc931582009-04-01 09:22:31 -04007072/*
7073 * Initialize the values to be used by the client in CREATE_SESSION
7074 * If nfs4_init_session set the fore channel request and response sizes,
7075 * use them.
7076 *
7077 * Set the back channel max_resp_sz_cached to zero to force the client to
7078 * always set csa_cachethis to FALSE because the current implementation
7079 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7080 */
7081static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7082{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007083 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007084
Andy Adamson18aad3d2013-06-26 12:21:49 -04007085 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7086 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7087
Andy Adamsonfc931582009-04-01 09:22:31 -04007088 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007089 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7090 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007091 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007092 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007093
7094 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007095 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007096 __func__,
7097 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007098 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007099
7100 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007101 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7102 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7103 args->bc_attrs.max_resp_sz_cached = 0;
7104 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7105 args->bc_attrs.max_reqs = 1;
7106
7107 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7108 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7109 __func__,
7110 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7111 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7112 args->bc_attrs.max_reqs);
7113}
7114
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007115static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04007116{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007117 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7118 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
7119
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007120 if (rcvd->max_resp_sz > sent->max_resp_sz)
7121 return -EINVAL;
7122 /*
7123 * Our requested max_ops is the minimum we need; we're not
7124 * prepared to break up compounds into smaller pieces than that.
7125 * So, no point even trying to continue if the server won't
7126 * cooperate:
7127 */
7128 if (rcvd->max_ops < sent->max_ops)
7129 return -EINVAL;
7130 if (rcvd->max_reqs == 0)
7131 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007132 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7133 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007134 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007135}
7136
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007137static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7138{
7139 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7140 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007141
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007142 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7143 return -EINVAL;
7144 if (rcvd->max_resp_sz < sent->max_resp_sz)
7145 return -EINVAL;
7146 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7147 return -EINVAL;
7148 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007149 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007150 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007151 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007152 return -EINVAL;
7153 return 0;
7154}
Andy Adamson8d353012009-04-01 09:22:32 -04007155
Andy Adamson8d353012009-04-01 09:22:32 -04007156static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7157 struct nfs4_session *session)
7158{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007159 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007160
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007161 ret = nfs4_verify_fore_channel_attrs(args, session);
7162 if (ret)
7163 return ret;
7164 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04007165}
7166
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007167static int _nfs4_proc_create_session(struct nfs_client *clp,
7168 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007169{
7170 struct nfs4_session *session = clp->cl_session;
7171 struct nfs41_create_session_args args = {
7172 .client = clp,
7173 .cb_program = NFS4_CALLBACK,
7174 };
7175 struct nfs41_create_session_res res = {
7176 .client = clp,
7177 };
7178 struct rpc_message msg = {
7179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7180 .rpc_argp = &args,
7181 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007182 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007183 };
7184 int status;
7185
7186 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007187 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007188
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007189 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007190 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007191
Trond Myklebust43095d32012-11-20 11:13:12 -05007192 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007193 /* Verify the session's negotiated channel_attrs values */
7194 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04007195 /* Increment the clientid slot sequence id */
7196 clp->cl_seqid++;
7197 }
7198
7199 return status;
7200}
7201
7202/*
7203 * Issues a CREATE_SESSION operation to the server.
7204 * It is the responsibility of the caller to verify the session is
7205 * expired before calling this routine.
7206 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007207int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007208{
7209 int status;
7210 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007211 struct nfs4_session *session = clp->cl_session;
7212
7213 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7214
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007215 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007216 if (status)
7217 goto out;
7218
Andy Adamsonaacd5532011-11-09 13:58:21 -05007219 /* Init or reset the session slot tables */
7220 status = nfs4_setup_session_slot_tables(session);
7221 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007222 if (status)
7223 goto out;
7224
7225 ptr = (unsigned *)&session->sess_id.data[0];
7226 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7227 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007228out:
7229 dprintk("<-- %s\n", __func__);
7230 return status;
7231}
7232
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007233/*
7234 * Issue the over-the-wire RPC DESTROY_SESSION.
7235 * The caller must serialize access to this routine.
7236 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007237int nfs4_proc_destroy_session(struct nfs4_session *session,
7238 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007239{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007240 struct rpc_message msg = {
7241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7242 .rpc_argp = session,
7243 .rpc_cred = cred,
7244 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007245 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007246
7247 dprintk("--> nfs4_proc_destroy_session\n");
7248
7249 /* session is still being setup */
7250 if (session->clp->cl_cons_state != NFS_CS_READY)
7251 return status;
7252
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007253 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007254 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007255
7256 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007257 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007258 "Session has been destroyed regardless...\n", status);
7259
7260 dprintk("<-- nfs4_proc_destroy_session\n");
7261 return status;
7262}
7263
Trond Myklebust7b38c362012-05-23 13:23:31 -04007264/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007265 * Renew the cl_session lease.
7266 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007267struct nfs4_sequence_data {
7268 struct nfs_client *clp;
7269 struct nfs4_sequence_args args;
7270 struct nfs4_sequence_res res;
7271};
7272
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007273static void nfs41_sequence_release(void *data)
7274{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007275 struct nfs4_sequence_data *calldata = data;
7276 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007277
Alexandros Batsakis71358402010-02-05 03:45:05 -08007278 if (atomic_read(&clp->cl_count) > 1)
7279 nfs4_schedule_state_renewal(clp);
7280 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007281 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007282}
7283
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007284static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7285{
7286 switch(task->tk_status) {
7287 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007288 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7289 return -EAGAIN;
7290 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007291 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007292 }
7293 return 0;
7294}
7295
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007296static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007297{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007298 struct nfs4_sequence_data *calldata = data;
7299 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007300
Trond Myklebust14516c32010-07-31 14:29:06 -04007301 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7302 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007303
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007304 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007305 if (task->tk_status < 0) {
7306 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007307 if (atomic_read(&clp->cl_count) == 1)
7308 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007309
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007310 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7311 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007312 return;
7313 }
7314 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007315 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007316out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007317 dprintk("<-- %s\n", __func__);
7318}
7319
7320static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7321{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007322 struct nfs4_sequence_data *calldata = data;
7323 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007324 struct nfs4_sequence_args *args;
7325 struct nfs4_sequence_res *res;
7326
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007327 args = task->tk_msg.rpc_argp;
7328 res = task->tk_msg.rpc_resp;
7329
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007330 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007331}
7332
7333static const struct rpc_call_ops nfs41_sequence_ops = {
7334 .rpc_call_done = nfs41_sequence_call_done,
7335 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007336 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007337};
7338
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007339static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7340 struct rpc_cred *cred,
7341 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007342{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007343 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007344 struct rpc_message msg = {
7345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7346 .rpc_cred = cred,
7347 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007348 struct rpc_task_setup task_setup_data = {
7349 .rpc_client = clp->cl_rpcclient,
7350 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007351 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007352 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007353 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007354
Alexandros Batsakis71358402010-02-05 03:45:05 -08007355 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007356 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007357 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007358 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007359 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007360 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007361 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007362 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007363 if (is_privileged)
7364 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007365 msg.rpc_argp = &calldata->args;
7366 msg.rpc_resp = &calldata->res;
7367 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007368 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007369
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007370 return rpc_run_task(&task_setup_data);
7371}
7372
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007373static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007374{
7375 struct rpc_task *task;
7376 int ret = 0;
7377
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007378 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007379 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007380 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007381 if (IS_ERR(task))
7382 ret = PTR_ERR(task);
7383 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007384 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007385 dprintk("<-- %s status=%d\n", __func__, ret);
7386 return ret;
7387}
7388
7389static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7390{
7391 struct rpc_task *task;
7392 int ret;
7393
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007394 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007395 if (IS_ERR(task)) {
7396 ret = PTR_ERR(task);
7397 goto out;
7398 }
7399 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007400 if (!ret) {
7401 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7402
7403 if (task->tk_status == 0)
7404 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007405 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007406 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007407 rpc_put_task(task);
7408out:
7409 dprintk("<-- %s status=%d\n", __func__, ret);
7410 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007411}
7412
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007413struct nfs4_reclaim_complete_data {
7414 struct nfs_client *clp;
7415 struct nfs41_reclaim_complete_args arg;
7416 struct nfs41_reclaim_complete_res res;
7417};
7418
7419static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7420{
7421 struct nfs4_reclaim_complete_data *calldata = data;
7422
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007423 nfs41_setup_sequence(calldata->clp->cl_session,
7424 &calldata->arg.seq_args,
7425 &calldata->res.seq_res,
7426 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007427}
7428
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007429static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7430{
7431 switch(task->tk_status) {
7432 case 0:
7433 case -NFS4ERR_COMPLETE_ALREADY:
7434 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7435 break;
7436 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007437 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007438 /* fall through */
7439 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007440 return -EAGAIN;
7441 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007442 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007443 }
7444 return 0;
7445}
7446
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007447static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7448{
7449 struct nfs4_reclaim_complete_data *calldata = data;
7450 struct nfs_client *clp = calldata->clp;
7451 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7452
7453 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007454 if (!nfs41_sequence_done(task, res))
7455 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007456
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007457 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007458 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7459 rpc_restart_call_prepare(task);
7460 return;
7461 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007462 dprintk("<-- %s\n", __func__);
7463}
7464
7465static void nfs4_free_reclaim_complete_data(void *data)
7466{
7467 struct nfs4_reclaim_complete_data *calldata = data;
7468
7469 kfree(calldata);
7470}
7471
7472static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7473 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7474 .rpc_call_done = nfs4_reclaim_complete_done,
7475 .rpc_release = nfs4_free_reclaim_complete_data,
7476};
7477
7478/*
7479 * Issue a global reclaim complete.
7480 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007481static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7482 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007483{
7484 struct nfs4_reclaim_complete_data *calldata;
7485 struct rpc_task *task;
7486 struct rpc_message msg = {
7487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007488 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007489 };
7490 struct rpc_task_setup task_setup_data = {
7491 .rpc_client = clp->cl_rpcclient,
7492 .rpc_message = &msg,
7493 .callback_ops = &nfs4_reclaim_complete_call_ops,
7494 .flags = RPC_TASK_ASYNC,
7495 };
7496 int status = -ENOMEM;
7497
7498 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007499 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007500 if (calldata == NULL)
7501 goto out;
7502 calldata->clp = clp;
7503 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007504
Chuck Levera9c92d62013-08-09 12:48:18 -04007505 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007506 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007507 msg.rpc_argp = &calldata->arg;
7508 msg.rpc_resp = &calldata->res;
7509 task_setup_data.callback_data = calldata;
7510 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007511 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007512 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007513 goto out;
7514 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007515 status = nfs4_wait_for_completion_rpc_task(task);
7516 if (status == 0)
7517 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007518 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007519 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007520out:
7521 dprintk("<-- %s status=%d\n", __func__, status);
7522 return status;
7523}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007524
7525static void
7526nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7527{
7528 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007529 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007530 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007531
7532 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007533 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7534 * right now covering the LAYOUTGET we are about to send.
7535 * However, that is not so catastrophic, and there seems
7536 * to be no way to prevent it completely.
7537 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007538 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007539 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007540 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007541 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7542 NFS_I(lgp->args.inode)->layout,
7543 lgp->args.ctx->state)) {
7544 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007545 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007546}
7547
7548static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7549{
7550 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007551 struct inode *inode = lgp->args.inode;
7552 struct nfs_server *server = NFS_SERVER(inode);
7553 struct pnfs_layout_hdr *lo;
7554 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007555 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007556
Boaz Harroshed7e5422014-01-22 20:34:54 +02007557 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007558
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007559 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007560 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007561
7562 switch (task->tk_status) {
7563 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007564 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007565 /*
7566 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7567 * (or clients) writing to the same RAID stripe
7568 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007569 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007570 /*
7571 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7572 * existing layout before getting a new one).
7573 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007574 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007575 timeo = rpc_get_timeout(task->tk_client);
7576 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007577 now = jiffies;
7578 if (time_after(giveup, now)) {
7579 unsigned long delay;
7580
7581 /* Delay for:
7582 * - Not less then NFS4_POLL_RETRY_MIN.
7583 * - One last time a jiffie before we give up
7584 * - exponential backoff (time_now minus start_attempt)
7585 */
7586 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7587 min((giveup - now - 1),
7588 now - lgp->args.timestamp));
7589
7590 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7591 __func__, delay);
7592 rpc_delay(task, delay);
7593 task->tk_status = 0;
7594 rpc_restart_call_prepare(task);
7595 goto out; /* Do not call nfs4_async_handle_error() */
7596 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007597 break;
7598 case -NFS4ERR_EXPIRED:
7599 case -NFS4ERR_BAD_STATEID:
7600 spin_lock(&inode->i_lock);
7601 lo = NFS_I(inode)->layout;
7602 if (!lo || list_empty(&lo->plh_segs)) {
7603 spin_unlock(&inode->i_lock);
7604 /* If the open stateid was bad, then recover it. */
7605 state = lgp->args.ctx->state;
7606 } else {
7607 LIST_HEAD(head);
7608
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007609 /*
7610 * Mark the bad layout state as invalid, then retry
7611 * with the current stateid.
7612 */
Trond Myklebustee314c22012-10-01 17:25:48 -07007613 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7614 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007615 pnfs_free_lseg_list(&head);
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007616
7617 task->tk_status = 0;
7618 rpc_restart_call_prepare(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007619 }
7620 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007621 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebustee314c22012-10-01 17:25:48 -07007622 rpc_restart_call_prepare(task);
7623out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007624 dprintk("<-- %s\n", __func__);
7625}
7626
Idan Kedar85541162012-08-02 11:47:10 +03007627static size_t max_response_pages(struct nfs_server *server)
7628{
7629 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7630 return nfs_page_array_len(0, max_resp_sz);
7631}
7632
7633static void nfs4_free_pages(struct page **pages, size_t size)
7634{
7635 int i;
7636
7637 if (!pages)
7638 return;
7639
7640 for (i = 0; i < size; i++) {
7641 if (!pages[i])
7642 break;
7643 __free_page(pages[i]);
7644 }
7645 kfree(pages);
7646}
7647
7648static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7649{
7650 struct page **pages;
7651 int i;
7652
7653 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7654 if (!pages) {
7655 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7656 return NULL;
7657 }
7658
7659 for (i = 0; i < size; i++) {
7660 pages[i] = alloc_page(gfp_flags);
7661 if (!pages[i]) {
7662 dprintk("%s: failed to allocate page\n", __func__);
7663 nfs4_free_pages(pages, size);
7664 return NULL;
7665 }
7666 }
7667
7668 return pages;
7669}
7670
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007671static void nfs4_layoutget_release(void *calldata)
7672{
7673 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007674 struct inode *inode = lgp->args.inode;
7675 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007676 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007677
7678 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007679 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007680 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007681 put_nfs_open_context(lgp->args.ctx);
7682 kfree(calldata);
7683 dprintk("<-- %s\n", __func__);
7684}
7685
7686static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7687 .rpc_call_prepare = nfs4_layoutget_prepare,
7688 .rpc_call_done = nfs4_layoutget_done,
7689 .rpc_release = nfs4_layoutget_release,
7690};
7691
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007692struct pnfs_layout_segment *
7693nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007694{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007695 struct inode *inode = lgp->args.inode;
7696 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007697 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007698 struct rpc_task *task;
7699 struct rpc_message msg = {
7700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7701 .rpc_argp = &lgp->args,
7702 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007703 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007704 };
7705 struct rpc_task_setup task_setup_data = {
7706 .rpc_client = server->client,
7707 .rpc_message = &msg,
7708 .callback_ops = &nfs4_layoutget_call_ops,
7709 .callback_data = lgp,
7710 .flags = RPC_TASK_ASYNC,
7711 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007712 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007713 int status = 0;
7714
7715 dprintk("--> %s\n", __func__);
7716
Peng Tao4bd5a982014-11-17 11:05:17 +08007717 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7718 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7719
Idan Kedar85541162012-08-02 11:47:10 +03007720 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7721 if (!lgp->args.layout.pages) {
7722 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007723 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007724 }
7725 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007726 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007727
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007728 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007729 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007730 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007731
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007732 task = rpc_run_task(&task_setup_data);
7733 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007734 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007735 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007736 if (status == 0)
7737 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007738 trace_nfs4_layoutget(lgp->args.ctx,
7739 &lgp->args.range,
7740 &lgp->res.range,
7741 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007742 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7743 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007744 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007745 rpc_put_task(task);
7746 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007747 if (status)
7748 return ERR_PTR(status);
7749 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007750}
7751
Benny Halevycbe82602011-05-22 19:52:37 +03007752static void
7753nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7754{
7755 struct nfs4_layoutreturn *lrp = calldata;
7756
7757 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007758 nfs41_setup_sequence(lrp->clp->cl_session,
7759 &lrp->args.seq_args,
7760 &lrp->res.seq_res,
7761 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007762}
7763
7764static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7765{
7766 struct nfs4_layoutreturn *lrp = calldata;
7767 struct nfs_server *server;
7768
7769 dprintk("--> %s\n", __func__);
7770
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007771 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007772 return;
7773
7774 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007775 switch (task->tk_status) {
7776 default:
7777 task->tk_status = 0;
7778 case 0:
7779 break;
7780 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007781 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007782 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007783 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007784 return;
7785 }
Benny Halevycbe82602011-05-22 19:52:37 +03007786 dprintk("<-- %s\n", __func__);
7787}
7788
7789static void nfs4_layoutreturn_release(void *calldata)
7790{
7791 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007792 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007793
7794 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007795 spin_lock(&lo->plh_inode->i_lock);
7796 if (lrp->res.lrs_present)
7797 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7798 lo->plh_block_lgets--;
7799 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007800 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007801 kfree(calldata);
7802 dprintk("<-- %s\n", __func__);
7803}
7804
7805static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7806 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7807 .rpc_call_done = nfs4_layoutreturn_done,
7808 .rpc_release = nfs4_layoutreturn_release,
7809};
7810
7811int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7812{
7813 struct rpc_task *task;
7814 struct rpc_message msg = {
7815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7816 .rpc_argp = &lrp->args,
7817 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007818 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007819 };
7820 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007821 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007822 .rpc_message = &msg,
7823 .callback_ops = &nfs4_layoutreturn_call_ops,
7824 .callback_data = lrp,
7825 };
7826 int status;
7827
7828 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007829 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007830 task = rpc_run_task(&task_setup_data);
7831 if (IS_ERR(task))
7832 return PTR_ERR(task);
7833 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007834 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007835 dprintk("<-- %s status=%d\n", __func__, status);
7836 rpc_put_task(task);
7837 return status;
7838}
7839
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007840static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007841_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7842 struct pnfs_device *pdev,
7843 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007844{
7845 struct nfs4_getdeviceinfo_args args = {
7846 .pdev = pdev,
7847 };
7848 struct nfs4_getdeviceinfo_res res = {
7849 .pdev = pdev,
7850 };
7851 struct rpc_message msg = {
7852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7853 .rpc_argp = &args,
7854 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007855 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007856 };
7857 int status;
7858
7859 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007860 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007861 dprintk("<-- %s status=%d\n", __func__, status);
7862
7863 return status;
7864}
7865
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007866int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7867 struct pnfs_device *pdev,
7868 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007869{
7870 struct nfs4_exception exception = { };
7871 int err;
7872
7873 do {
7874 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007875 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007876 &exception);
7877 } while (exception.retry);
7878 return err;
7879}
7880EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7881
Andy Adamson863a3c62011-03-23 13:27:54 +00007882static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7883{
7884 struct nfs4_layoutcommit_data *data = calldata;
7885 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007886 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007887
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007888 nfs41_setup_sequence(session,
7889 &data->args.seq_args,
7890 &data->res.seq_res,
7891 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007892}
7893
7894static void
7895nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7896{
7897 struct nfs4_layoutcommit_data *data = calldata;
7898 struct nfs_server *server = NFS_SERVER(data->args.inode);
7899
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007900 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007901 return;
7902
7903 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007904 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7905 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7906 case -NFS4ERR_BADLAYOUT: /* no layout */
7907 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007908 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007909 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007910 break;
7911 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10007912 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007913 rpc_restart_call_prepare(task);
7914 return;
7915 }
7916 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007917}
7918
7919static void nfs4_layoutcommit_release(void *calldata)
7920{
7921 struct nfs4_layoutcommit_data *data = calldata;
7922
Andy Adamsondb29c082011-07-30 20:52:38 -04007923 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05007924 nfs_post_op_update_inode_force_wcc(data->args.inode,
7925 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00007926 put_rpccred(data->cred);
7927 kfree(data);
7928}
7929
7930static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7931 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7932 .rpc_call_done = nfs4_layoutcommit_done,
7933 .rpc_release = nfs4_layoutcommit_release,
7934};
7935
7936int
Andy Adamsonef311532011-03-12 02:58:10 -05007937nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007938{
7939 struct rpc_message msg = {
7940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7941 .rpc_argp = &data->args,
7942 .rpc_resp = &data->res,
7943 .rpc_cred = data->cred,
7944 };
7945 struct rpc_task_setup task_setup_data = {
7946 .task = &data->task,
7947 .rpc_client = NFS_CLIENT(data->args.inode),
7948 .rpc_message = &msg,
7949 .callback_ops = &nfs4_layoutcommit_ops,
7950 .callback_data = data,
7951 .flags = RPC_TASK_ASYNC,
7952 };
7953 struct rpc_task *task;
7954 int status = 0;
7955
7956 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7957 "lbw: %llu inode %lu\n",
7958 data->task.tk_pid, sync,
7959 data->args.lastbytewritten,
7960 data->args.inode->i_ino);
7961
Chuck Levera9c92d62013-08-09 12:48:18 -04007962 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007963 task = rpc_run_task(&task_setup_data);
7964 if (IS_ERR(task))
7965 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007966 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007967 goto out;
7968 status = nfs4_wait_for_completion_rpc_task(task);
7969 if (status != 0)
7970 goto out;
7971 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007972 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007973out:
7974 dprintk("%s: status %d\n", __func__, status);
7975 rpc_put_task(task);
7976 return status;
7977}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007978
Andy Adamson97431202013-08-08 10:57:56 -04007979/**
7980 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7981 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7982 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007983static int
7984_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007985 struct nfs_fsinfo *info,
7986 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007987{
7988 struct nfs41_secinfo_no_name_args args = {
7989 .style = SECINFO_STYLE_CURRENT_FH,
7990 };
7991 struct nfs4_secinfo_res res = {
7992 .flavors = flavors,
7993 };
7994 struct rpc_message msg = {
7995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7996 .rpc_argp = &args,
7997 .rpc_resp = &res,
7998 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007999 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008000 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008001 int status;
8002
8003 if (use_integrity) {
8004 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008005 cred = nfs4_get_clid_cred(server->nfs_client);
8006 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008007 }
8008
8009 dprintk("--> %s\n", __func__);
8010 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8011 &res.seq_res, 0);
8012 dprintk("<-- %s status=%d\n", __func__, status);
8013
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008014 if (cred)
8015 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008016
8017 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008018}
8019
8020static int
8021nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8022 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8023{
8024 struct nfs4_exception exception = { };
8025 int err;
8026 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008027 /* first try using integrity protection */
8028 err = -NFS4ERR_WRONGSEC;
8029
8030 /* try to use integrity protection with machine cred */
8031 if (_nfs4_is_integrity_protected(server->nfs_client))
8032 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8033 flavors, true);
8034
8035 /*
8036 * if unable to use integrity protection, or SECINFO with
8037 * integrity protection returns NFS4ERR_WRONGSEC (which is
8038 * disallowed by spec, but exists in deployed servers) use
8039 * the current filesystem's rpc_client and the user cred.
8040 */
8041 if (err == -NFS4ERR_WRONGSEC)
8042 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8043 flavors, false);
8044
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008045 switch (err) {
8046 case 0:
8047 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008048 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008049 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008050 default:
8051 err = nfs4_handle_exception(server, err, &exception);
8052 }
8053 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008054out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008055 return err;
8056}
8057
8058static int
8059nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8060 struct nfs_fsinfo *info)
8061{
8062 int err;
8063 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008064 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008065 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008066 struct nfs4_secinfo4 *secinfo;
8067 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008068
8069 page = alloc_page(GFP_KERNEL);
8070 if (!page) {
8071 err = -ENOMEM;
8072 goto out;
8073 }
8074
8075 flavors = page_address(page);
8076 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8077
8078 /*
8079 * Fall back on "guess and check" method if
8080 * the server doesn't support SECINFO_NO_NAME
8081 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008082 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008083 err = nfs4_find_root_sec(server, fhandle, info);
8084 goto out_freepage;
8085 }
8086 if (err)
8087 goto out_freepage;
8088
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008089 for (i = 0; i < flavors->num_flavors; i++) {
8090 secinfo = &flavors->flavors[i];
8091
8092 switch (secinfo->flavor) {
8093 case RPC_AUTH_NULL:
8094 case RPC_AUTH_UNIX:
8095 case RPC_AUTH_GSS:
8096 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8097 &secinfo->flavor_info);
8098 break;
8099 default:
8100 flavor = RPC_AUTH_MAXFLAVOR;
8101 break;
8102 }
8103
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008104 if (!nfs_auth_info_match(&server->auth_info, flavor))
8105 flavor = RPC_AUTH_MAXFLAVOR;
8106
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008107 if (flavor != RPC_AUTH_MAXFLAVOR) {
8108 err = nfs4_lookup_root_sec(server, fhandle,
8109 info, flavor);
8110 if (!err)
8111 break;
8112 }
8113 }
8114
8115 if (flavor == RPC_AUTH_MAXFLAVOR)
8116 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008117
8118out_freepage:
8119 put_page(page);
8120 if (err == -EACCES)
8121 return -EPERM;
8122out:
8123 return err;
8124}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008125
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008126static int _nfs41_test_stateid(struct nfs_server *server,
8127 nfs4_stateid *stateid,
8128 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008129{
8130 int status;
8131 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008132 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008133 };
8134 struct nfs41_test_stateid_res res;
8135 struct rpc_message msg = {
8136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8137 .rpc_argp = &args,
8138 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008139 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008140 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008141 struct rpc_clnt *rpc_client = server->client;
8142
8143 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8144 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008145
Chuck Lever38527b12012-07-11 16:30:23 -04008146 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008147 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008148 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008149 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008150 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008151 if (status != NFS_OK) {
8152 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008153 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008154 }
8155 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008156 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008157}
8158
Chuck Lever38527b12012-07-11 16:30:23 -04008159/**
8160 * nfs41_test_stateid - perform a TEST_STATEID operation
8161 *
8162 * @server: server / transport on which to perform the operation
8163 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008164 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008165 *
8166 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8167 * Otherwise a negative NFS4ERR value is returned if the operation
8168 * failed or the state ID is not currently valid.
8169 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008170static int nfs41_test_stateid(struct nfs_server *server,
8171 nfs4_stateid *stateid,
8172 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008173{
8174 struct nfs4_exception exception = { };
8175 int err;
8176 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008177 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008178 if (err != -NFS4ERR_DELAY)
8179 break;
8180 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008181 } while (exception.retry);
8182 return err;
8183}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008184
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008185struct nfs_free_stateid_data {
8186 struct nfs_server *server;
8187 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008188 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008189};
8190
8191static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8192{
8193 struct nfs_free_stateid_data *data = calldata;
8194 nfs41_setup_sequence(nfs4_get_session(data->server),
8195 &data->args.seq_args,
8196 &data->res.seq_res,
8197 task);
8198}
8199
8200static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8201{
8202 struct nfs_free_stateid_data *data = calldata;
8203
8204 nfs41_sequence_done(task, &data->res.seq_res);
8205
8206 switch (task->tk_status) {
8207 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008208 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008209 rpc_restart_call_prepare(task);
8210 }
8211}
8212
8213static void nfs41_free_stateid_release(void *calldata)
8214{
8215 kfree(calldata);
8216}
8217
Trond Myklebust17f26b12013-08-21 15:48:42 -04008218static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008219 .rpc_call_prepare = nfs41_free_stateid_prepare,
8220 .rpc_call_done = nfs41_free_stateid_done,
8221 .rpc_release = nfs41_free_stateid_release,
8222};
8223
8224static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8225 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008226 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008227 bool privileged)
8228{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008229 struct rpc_message msg = {
8230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008231 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008232 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008233 struct rpc_task_setup task_setup = {
8234 .rpc_client = server->client,
8235 .rpc_message = &msg,
8236 .callback_ops = &nfs41_free_stateid_ops,
8237 .flags = RPC_TASK_ASYNC,
8238 };
8239 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008240
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008241 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8242 &task_setup.rpc_client, &msg);
8243
Chuck Lever38527b12012-07-11 16:30:23 -04008244 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008245 data = kmalloc(sizeof(*data), GFP_NOFS);
8246 if (!data)
8247 return ERR_PTR(-ENOMEM);
8248 data->server = server;
8249 nfs4_stateid_copy(&data->args.stateid, stateid);
8250
8251 task_setup.callback_data = data;
8252
8253 msg.rpc_argp = &data->args;
8254 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008255 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008256 if (privileged)
8257 nfs4_set_sequence_privileged(&data->args.seq_args);
8258
8259 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008260}
8261
Chuck Lever38527b12012-07-11 16:30:23 -04008262/**
8263 * nfs41_free_stateid - perform a FREE_STATEID operation
8264 *
8265 * @server: server / transport on which to perform the operation
8266 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008267 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008268 *
8269 * Returns NFS_OK if the server freed "stateid". Otherwise a
8270 * negative NFS4ERR value is returned.
8271 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008272static int nfs41_free_stateid(struct nfs_server *server,
8273 nfs4_stateid *stateid,
8274 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008275{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008276 struct rpc_task *task;
8277 int ret;
8278
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008279 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008280 if (IS_ERR(task))
8281 return PTR_ERR(task);
8282 ret = rpc_wait_for_completion_task(task);
8283 if (!ret)
8284 ret = task->tk_status;
8285 rpc_put_task(task);
8286 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008287}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008288
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008289static void
8290nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008291{
8292 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008293 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008294
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008295 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008296 nfs4_free_lock_state(server, lsp);
8297 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008298 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008299 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008300}
8301
Trond Myklebust36281ca2012-03-04 18:13:56 -05008302static bool nfs41_match_stateid(const nfs4_stateid *s1,
8303 const nfs4_stateid *s2)
8304{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008305 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008306 return false;
8307
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008308 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008309 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008310 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008311 return true;
8312
8313 return false;
8314}
8315
Andy Adamson557134a2009-04-01 09:21:53 -04008316#endif /* CONFIG_NFS_V4_1 */
8317
Trond Myklebust36281ca2012-03-04 18:13:56 -05008318static bool nfs4_match_stateid(const nfs4_stateid *s1,
8319 const nfs4_stateid *s2)
8320{
Trond Myklebustf597c532012-03-04 18:13:56 -05008321 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008322}
8323
8324
Trond Myklebust17280172012-03-11 13:11:00 -04008325static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008326 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008327 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 .recover_open = nfs4_open_reclaim,
8329 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008330 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008331 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332};
8333
Andy Adamson591d71c2009-04-01 09:22:47 -04008334#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008335static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008336 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8337 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8338 .recover_open = nfs4_open_reclaim,
8339 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008340 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008341 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008342 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008343};
8344#endif /* CONFIG_NFS_V4_1 */
8345
Trond Myklebust17280172012-03-11 13:11:00 -04008346static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008347 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008348 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008349 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008351 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352};
8353
Andy Adamson591d71c2009-04-01 09:22:47 -04008354#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008355static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008356 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8357 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008358 .recover_open = nfs41_open_expired,
8359 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008360 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008361};
8362#endif /* CONFIG_NFS_V4_1 */
8363
Trond Myklebust17280172012-03-11 13:11:00 -04008364static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008365 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008366 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008367 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008368};
8369
8370#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008371static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008372 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008373 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008374 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008375};
8376#endif
8377
Chuck Leverec011fe2013-10-17 14:12:39 -04008378static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008379 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008380 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008381};
8382
8383#if defined(CONFIG_NFS_V4_1)
8384static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008385 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008386 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008387};
8388#endif /* CONFIG_NFS_V4_1 */
8389
Trond Myklebust97dc1352010-06-16 09:52:26 -04008390static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8391 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008392 .init_caps = NFS_CAP_READDIRPLUS
8393 | NFS_CAP_ATOMIC_OPEN
8394 | NFS_CAP_CHANGE_ATTR
8395 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008396 .init_client = nfs40_init_client,
8397 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008398 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008399 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008400 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008401 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008402 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8403 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8404 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008405 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008406};
8407
8408#if defined(CONFIG_NFS_V4_1)
8409static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8410 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008411 .init_caps = NFS_CAP_READDIRPLUS
8412 | NFS_CAP_ATOMIC_OPEN
8413 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008414 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008415 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008416 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008417 .init_client = nfs41_init_client,
8418 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008419 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008420 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008421 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008422 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008423 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8424 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8425 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008426 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008427};
8428#endif
8429
Steve Dickson42c2c422013-05-22 12:50:38 -04008430#if defined(CONFIG_NFS_V4_2)
8431static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8432 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008433 .init_caps = NFS_CAP_READDIRPLUS
8434 | NFS_CAP_ATOMIC_OPEN
8435 | NFS_CAP_CHANGE_ATTR
8436 | NFS_CAP_POSIX_LOCK
8437 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008438 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008439 | NFS_CAP_ALLOCATE
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008440 | NFS_CAP_DEALLOCATE
Anna Schumakere9831202014-10-22 15:53:10 -04008441 | NFS_CAP_SEEK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008442 .init_client = nfs41_init_client,
8443 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008444 .match_stateid = nfs41_match_stateid,
8445 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008446 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008447 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008448 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8449 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8450 .state_renewal_ops = &nfs41_state_renewal_ops,
8451};
8452#endif
8453
Trond Myklebust97dc1352010-06-16 09:52:26 -04008454const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8455 [0] = &nfs_v4_0_minor_ops,
8456#if defined(CONFIG_NFS_V4_1)
8457 [1] = &nfs_v4_1_minor_ops,
8458#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008459#if defined(CONFIG_NFS_V4_2)
8460 [2] = &nfs_v4_2_minor_ops,
8461#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008462};
8463
Trond Myklebust17f26b12013-08-21 15:48:42 -04008464static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008465 .create = nfs_create,
8466 .lookup = nfs_lookup,
8467 .atomic_open = nfs_atomic_open,
8468 .link = nfs_link,
8469 .unlink = nfs_unlink,
8470 .symlink = nfs_symlink,
8471 .mkdir = nfs_mkdir,
8472 .rmdir = nfs_rmdir,
8473 .mknod = nfs_mknod,
8474 .rename = nfs_rename,
8475 .permission = nfs_permission,
8476 .getattr = nfs_getattr,
8477 .setattr = nfs_setattr,
8478 .getxattr = generic_getxattr,
8479 .setxattr = generic_setxattr,
8480 .listxattr = generic_listxattr,
8481 .removexattr = generic_removexattr,
8482};
8483
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008484static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008485 .permission = nfs_permission,
8486 .getattr = nfs_getattr,
8487 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008488 .getxattr = generic_getxattr,
8489 .setxattr = generic_setxattr,
8490 .listxattr = generic_listxattr,
8491 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008492};
8493
David Howells509de812006-08-22 20:06:11 -04008494const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495 .version = 4, /* protocol version */
8496 .dentry_ops = &nfs4_dentry_operations,
8497 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008498 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008499 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008500 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008501 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008502 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 .getattr = nfs4_proc_getattr,
8504 .setattr = nfs4_proc_setattr,
8505 .lookup = nfs4_proc_lookup,
8506 .access = nfs4_proc_access,
8507 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 .create = nfs4_proc_create,
8509 .remove = nfs4_proc_remove,
8510 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008511 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008513 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008514 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008515 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 .link = nfs4_proc_link,
8517 .symlink = nfs4_proc_symlink,
8518 .mkdir = nfs4_proc_mkdir,
8519 .rmdir = nfs4_proc_remove,
8520 .readdir = nfs4_proc_readdir,
8521 .mknod = nfs4_proc_mknod,
8522 .statfs = nfs4_proc_statfs,
8523 .fsinfo = nfs4_proc_fsinfo,
8524 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008525 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008527 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008529 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008531 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008533 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008534 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008536 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008537 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008538 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008539 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008540 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008541 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008542 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008543 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008544 .create_server = nfs4_create_server,
8545 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546};
8547
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008548static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8549 .prefix = XATTR_NAME_NFSV4_ACL,
8550 .list = nfs4_xattr_list_nfs4_acl,
8551 .get = nfs4_xattr_get_nfs4_acl,
8552 .set = nfs4_xattr_set_nfs4_acl,
8553};
8554
8555const struct xattr_handler *nfs4_xattr_handlers[] = {
8556 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008557#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8558 &nfs4_xattr_nfs4_label_handler,
8559#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008560 NULL
8561};
8562
Linus Torvalds1da177e2005-04-16 15:20:36 -07008563/*
8564 * Local variables:
8565 * c-basic-offset: 8
8566 * End:
8567 */