blob: 3acb1eb72930c40828bab90aeb27a3918f71138d [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>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.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"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
NeilBrown8478eaa2014-09-18 16:09:27 +100081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *, long *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040083static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
84static 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 -040085static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040087 struct nfs4_state *state, struct nfs4_label *ilabel,
88 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040090static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 struct rpc_cred *);
92static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
93 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040094#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040095
96#ifdef CONFIG_NFS_V4_SECURITY_LABEL
97static inline struct nfs4_label *
98nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
99 struct iattr *sattr, struct nfs4_label *label)
100{
101 int err;
102
103 if (label == NULL)
104 return NULL;
105
106 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
107 return NULL;
108
David Quigleyaa9c2662013-05-22 12:50:44 -0400109 err = security_dentry_init_security(dentry, sattr->ia_mode,
110 &dentry->d_name, (void **)&label->label, &label->len);
111 if (err == 0)
112 return label;
113
114 return NULL;
115}
116static inline void
117nfs4_label_release_security(struct nfs4_label *label)
118{
119 if (label)
120 security_release_secctx(label->label, label->len);
121}
122static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
123{
124 if (label)
125 return server->attr_bitmask;
126
127 return server->attr_bitmask_nl;
128}
129#else
130static inline struct nfs4_label *
131nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
132 struct iattr *sattr, struct nfs4_label *l)
133{ return NULL; }
134static inline void
135nfs4_label_release_security(struct nfs4_label *label)
136{ return; }
137static inline u32 *
138nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139{ return server->attr_bitmask; }
140#endif
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500143static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Trond Myklebust52567b02009-10-23 14:46:42 -0400145 if (err >= -1000)
146 return err;
147 switch (err) {
148 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500149 case -NFS4ERR_LAYOUTTRYLATER:
150 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400151 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000152 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400153 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800155 case -NFS4ERR_BADOWNER:
156 case -NFS4ERR_BADNAME:
157 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400158 case -NFS4ERR_SHARE_DENIED:
159 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400160 case -NFS4ERR_MINOR_VERS_MISMATCH:
161 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400162 case -NFS4ERR_FILE_OPEN:
163 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400164 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700166 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400169 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * This is our standard bitmap for GETATTR requests.
174 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400175const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 FATTR4_WORD0_TYPE
177 | FATTR4_WORD0_CHANGE
178 | FATTR4_WORD0_SIZE
179 | FATTR4_WORD0_FSID
180 | FATTR4_WORD0_FILEID,
181 FATTR4_WORD1_MODE
182 | FATTR4_WORD1_NUMLINKS
183 | FATTR4_WORD1_OWNER
184 | FATTR4_WORD1_OWNER_GROUP
185 | FATTR4_WORD1_RAWDEV
186 | FATTR4_WORD1_SPACE_USED
187 | FATTR4_WORD1_TIME_ACCESS
188 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400189 | FATTR4_WORD1_TIME_MODIFY
190 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400191#ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Trond Myklebust1549210f2012-06-05 09:16:47 -0400196static const u32 nfs4_pnfs_open_bitmap[3] = {
197 FATTR4_WORD0_TYPE
198 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_SIZE
200 | FATTR4_WORD0_FSID
201 | FATTR4_WORD0_FILEID,
202 FATTR4_WORD1_MODE
203 | FATTR4_WORD1_NUMLINKS
204 | FATTR4_WORD1_OWNER
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
212};
213
Andy Adamsone23008e2012-10-02 21:07:32 -0400214static const u32 nfs4_open_noattr_bitmap[3] = {
215 FATTR4_WORD0_TYPE
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
218};
219
David Quigleya09df2c2013-05-22 12:50:41 -0400220const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
227};
228
David Quigleya09df2c2013-05-22 12:50:41 -0400229const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 FATTR4_WORD0_MAXLINK
231 | FATTR4_WORD0_MAXNAME,
232 0
233};
234
Fred Isamandae100c2011-07-30 20:52:37 -0400235const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700239 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400245 FATTR4_WORD0_TYPE
246 | FATTR4_WORD0_CHANGE
247 | FATTR4_WORD0_SIZE
248 | FATTR4_WORD0_FSID
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
251 FATTR4_WORD1_MODE
252 | FATTR4_WORD1_NUMLINKS
253 | FATTR4_WORD1_OWNER
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400261};
262
Al Virobc4785c2006-10-19 23:28:51 -0700263static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct nfs4_readdir_arg *readdir)
265{
Al Viro0dbb4c62006-10-19 23:28:49 -0700266 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000269 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
271 return;
272 }
273
274 readdir->cookie = 0;
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
276 if (cookie == 2)
277 return;
278
279 /*
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
284 * instead of 1 or 2.
285 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800286 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (cookie == 0) {
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
294 p++;
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000298 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800314 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
NeilBrown8478eaa2014-09-18 16:09:27 +1000317static long nfs4_update_delay(long *timeout)
318{
319 long ret;
320 if (!timeout)
321 return NFS4_POLL_RETRY_MAX;
322 if (*timeout <= 0)
323 *timeout = NFS4_POLL_RETRY_MIN;
324 if (*timeout > NFS4_POLL_RETRY_MAX)
325 *timeout = NFS4_POLL_RETRY_MAX;
326 ret = *timeout;
327 *timeout <<= 1;
328 return ret;
329}
330
Trond Myklebust65de8722008-12-23 15:21:44 -0500331static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
332{
333 int res = 0;
334
335 might_sleep();
336
NeilBrown8478eaa2014-09-18 16:09:27 +1000337 freezable_schedule_timeout_killable_unsafe(
338 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 if (fatal_signal_pending(current))
340 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 return res;
342}
343
344/* This is the error handling routine for processes that are allowed
345 * to sleep.
346 */
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500347int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500348{
349 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500350 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500352 int ret = errorcode;
353
354 exception->retry = 0;
355 switch(errorcode) {
356 case 0:
357 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500358 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400359 case -NFS4ERR_DELEG_REVOKED:
360 case -NFS4ERR_ADMIN_REVOKED:
361 case -NFS4ERR_BAD_STATEID:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400362 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400363 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500364 exception->retry = 1;
365 return 0;
366 }
367 if (state == NULL)
368 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400369 ret = nfs4_schedule_stateid_recovery(server, state);
370 if (ret < 0)
371 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500372 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400373 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400374 if (state != NULL) {
375 ret = nfs4_schedule_stateid_recovery(server, state);
376 if (ret < 0)
377 break;
378 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500379 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500380 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500381 nfs4_schedule_lease_recovery(clp);
382 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400383 case -NFS4ERR_MOVED:
384 ret = nfs4_schedule_migration_recovery(server);
385 if (ret < 0)
386 break;
387 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400388 case -NFS4ERR_LEASE_MOVED:
389 nfs4_schedule_lease_moved_recovery(clp);
390 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500391#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400392 case -NFS4ERR_BADSESSION:
393 case -NFS4ERR_BADSLOT:
394 case -NFS4ERR_BAD_HIGH_SLOT:
395 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
396 case -NFS4ERR_DEADSESSION:
397 case -NFS4ERR_SEQ_FALSE_RETRY:
398 case -NFS4ERR_SEQ_MISORDERED:
399 dprintk("%s ERROR: %d Reset session\n", __func__,
400 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400401 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400402 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500403#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500405 if (exception->timeout > HZ) {
406 /* We have retried a decent amount, time to
407 * fail
408 */
409 ret = -EBUSY;
410 break;
411 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500412 case -NFS4ERR_GRACE:
413 case -NFS4ERR_DELAY:
414 ret = nfs4_delay(server->client, &exception->timeout);
415 if (ret != 0)
416 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400417 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500418 case -NFS4ERR_OLD_STATEID:
419 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800420 break;
421 case -NFS4ERR_BADOWNER:
422 /* The following works around a Linux server bug! */
423 case -NFS4ERR_BADNAME:
424 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
425 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
426 exception->retry = 1;
427 printk(KERN_WARNING "NFS: v4 server %s "
428 "does not accept raw "
429 "uid/gids. "
430 "Reenabling the idmapper.\n",
431 server->nfs_client->cl_hostname);
432 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500433 }
434 /* We failed to handle the error */
435 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500436wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500437 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400438 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
439 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500440 if (ret == 0)
441 exception->retry = 1;
442 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500443}
444
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400445/*
446 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
447 * or 'false' otherwise.
448 */
449static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
450{
451 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
452
453 if (flavor == RPC_AUTH_GSS_KRB5I ||
454 flavor == RPC_AUTH_GSS_KRB5P)
455 return true;
456
457 return false;
458}
Trond Myklebust65de8722008-12-23 15:21:44 -0500459
Trond Myklebust452e9352010-07-31 14:29:06 -0400460static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 spin_lock(&clp->cl_lock);
463 if (time_before(clp->cl_last_renewal,timestamp))
464 clp->cl_last_renewal = timestamp;
465 spin_unlock(&clp->cl_lock);
466}
467
Trond Myklebust452e9352010-07-31 14:29:06 -0400468static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
469{
Trond Myklebustbe824162015-07-05 14:50:46 -0400470 struct nfs_client *clp = server->nfs_client;
471
472 if (!nfs4_has_session(clp))
473 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400474}
475
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400476struct nfs4_call_sync_data {
477 const struct nfs_server *seq_server;
478 struct nfs4_sequence_args *seq_args;
479 struct nfs4_sequence_res *seq_res;
480};
481
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800482void nfs4_init_sequence(struct nfs4_sequence_args *args,
483 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400484{
485 args->sa_slot = NULL;
486 args->sa_cache_this = cache_reply;
487 args->sa_privileged = 0;
488
489 res->sr_slot = NULL;
490}
491
492static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
493{
494 args->sa_privileged = 1;
495}
496
Peng Taocb04ad22014-06-11 05:24:15 +0800497int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
498 struct nfs4_sequence_args *args,
499 struct nfs4_sequence_res *res,
500 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400501{
Chuck Lever3bd23842013-08-09 12:49:19 -0400502 struct nfs4_slot *slot;
503
504 /* slot already allocated? */
505 if (res->sr_slot != NULL)
506 goto out_start;
507
508 spin_lock(&tbl->slot_tbl_lock);
509 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
510 goto out_sleep;
511
512 slot = nfs4_alloc_slot(tbl);
513 if (IS_ERR(slot)) {
514 if (slot == ERR_PTR(-ENOMEM))
515 task->tk_timeout = HZ >> 2;
516 goto out_sleep;
517 }
518 spin_unlock(&tbl->slot_tbl_lock);
519
520 args->sa_slot = slot;
521 res->sr_slot = slot;
522
523out_start:
524 rpc_call_start(task);
525 return 0;
526
527out_sleep:
528 if (args->sa_privileged)
529 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
530 NULL, RPC_PRIORITY_PRIVILEGED);
531 else
532 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
533 spin_unlock(&tbl->slot_tbl_lock);
534 return -EAGAIN;
535}
Peng Taocb04ad22014-06-11 05:24:15 +0800536EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400537
538static int nfs40_sequence_done(struct rpc_task *task,
539 struct nfs4_sequence_res *res)
540{
541 struct nfs4_slot *slot = res->sr_slot;
542 struct nfs4_slot_table *tbl;
543
Trond Myklebustcab92c12014-01-29 12:12:15 -0500544 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400545 goto out;
546
547 tbl = slot->table;
548 spin_lock(&tbl->slot_tbl_lock);
549 if (!nfs41_wake_and_assign_slot(tbl, slot))
550 nfs4_free_slot(tbl, slot);
551 spin_unlock(&tbl->slot_tbl_lock);
552
553 res->sr_slot = NULL;
554out:
555 return 1;
556}
557
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400558#if defined(CONFIG_NFS_V4_1)
559
Trond Myklebustd185a332010-06-16 09:52:25 -0400560static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400561{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500562 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400563 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500564 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500565 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400566
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500567 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500568 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500569
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500570 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500571 /* Be nice to the server: try to ensure that the last transmitted
572 * value for highest_user_slotid <= target_highest_slotid
573 */
574 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
575 send_new_highest_used_slotid = true;
576
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500577 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500578 send_new_highest_used_slotid = false;
579 goto out_unlock;
580 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500581 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500582
583 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
584 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500585out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500586 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400587 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500588 if (send_new_highest_used_slotid)
589 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400590}
591
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500592int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400593{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500594 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500595 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400596 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500597 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500598 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400599
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500600 if (slot == NULL)
601 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400602 /* don't increment the sequence number if the task wasn't sent */
603 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400604 goto out;
605
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500606 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500607
Trond Myklebustac20d162012-12-15 15:36:07 -0500608 if (slot->interrupted) {
609 slot->interrupted = 0;
610 interrupted = true;
611 }
612
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400613 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500614 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400615 switch (res->sr_status) {
616 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400617 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500618 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500619 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500620 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500621 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400622 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500623 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400624 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500625 case 1:
626 /*
627 * sr_status remains 1 if an RPC level error occurred.
628 * The server may or may not have processed the sequence
629 * operation..
630 * Mark the slot as having hosted an interrupted RPC call.
631 */
632 slot->interrupted = 1;
633 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400634 case -NFS4ERR_DELAY:
635 /* The server detected a resend of the RPC call and
636 * returned NFS4ERR_DELAY as per Section 2.10.6.2
637 * of RFC5661.
638 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500639 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400640 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500641 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500642 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400643 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500644 case -NFS4ERR_BADSLOT:
645 /*
646 * The slot id we used was probably retired. Try again
647 * using a different slot id.
648 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500649 goto retry_nowait;
650 case -NFS4ERR_SEQ_MISORDERED:
651 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500652 * Was the last operation on this sequence interrupted?
653 * If so, retry after bumping the sequence number.
654 */
655 if (interrupted) {
656 ++slot->seq_nr;
657 goto retry_nowait;
658 }
659 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500660 * Could this slot have been previously retired?
661 * If so, then the server may be expecting seq_nr = 1!
662 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500663 if (slot->seq_nr != 1) {
664 slot->seq_nr = 1;
665 goto retry_nowait;
666 }
667 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500668 case -NFS4ERR_SEQ_FALSE_RETRY:
669 ++slot->seq_nr;
670 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400671 default:
672 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500673 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400674 }
675out:
676 /* The session may be reset by one of the error handlers. */
677 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400678 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500679out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500680 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500681retry_nowait:
682 if (rpc_restart_call_prepare(task)) {
683 task->tk_status = 0;
684 ret = 0;
685 }
686 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400687out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400688 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400689 goto out;
690 rpc_delay(task, NFS4_POLL_RETRY_MAX);
691 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400692}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500693EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400694
Peng Tao2c4b1312014-06-11 05:24:16 +0800695int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400696{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400698 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400699 if (!res->sr_slot->table->session)
700 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400701 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400702}
Peng Tao2c4b1312014-06-11 05:24:16 +0800703EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400704
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000705int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 struct nfs4_sequence_args *args,
707 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 struct rpc_task *task)
709{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400710 struct nfs4_slot *slot;
711 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400712
713 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400714 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400715 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400716 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400718 tbl = &session->fc_slot_table;
719
Trond Myklebust69d206b2012-11-22 13:21:02 -0500720 task->tk_timeout = 0;
721
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400722 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400723 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400724 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500725 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500726 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400727 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400728 }
729
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500730 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500731 if (IS_ERR(slot)) {
732 /* If out of memory, try again in 1/4 second */
733 if (slot == ERR_PTR(-ENOMEM))
734 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400735 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400736 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400737 }
738 spin_unlock(&tbl->slot_tbl_lock);
739
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500740 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400741
Chuck Levere8d92382013-08-09 12:47:51 -0400742 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500743 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400744
Benny Halevydfb4f3092010-09-24 09:17:01 -0400745 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500746 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400747 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400748 /*
749 * sr_status is only set in decode_sequence, and so will remain
750 * set to 1 if an rpc level failure occurs.
751 */
752 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400753 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400754out_success:
755 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400756 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400757out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400758 /* Privileged tasks are queued with top priority */
759 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400760 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
761 NULL, RPC_PRIORITY_PRIVILEGED);
762 else
763 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400764 spin_unlock(&tbl->slot_tbl_lock);
765 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400766}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000767EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400768
Chuck Lever5a580e02013-08-09 12:48:09 -0400769static int nfs4_setup_sequence(const struct nfs_server *server,
770 struct nfs4_sequence_args *args,
771 struct nfs4_sequence_res *res,
772 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400773{
Trond Myklebust035168a2010-06-16 09:52:26 -0400774 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400775 int ret = 0;
776
Chuck Lever3bd23842013-08-09 12:49:19 -0400777 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800778 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
779 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400780
Chuck Levere8d92382013-08-09 12:47:51 -0400781 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400782 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400783 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400784
Trond Myklebust9d12b212012-01-17 22:04:25 -0500785 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400786
Andy Adamsonce5039c2009-04-01 09:22:13 -0400787 dprintk("<-- %s status=%d\n", __func__, ret);
788 return ret;
789}
790
Andy Adamsonce5039c2009-04-01 09:22:13 -0400791static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
792{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400793 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400794 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400795
Trond Myklebust035168a2010-06-16 09:52:26 -0400796 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
797
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400798 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400799}
800
Andy Adamson69ab40c2009-04-01 09:22:19 -0400801static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
802{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400803 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804
Trond Myklebust14516c32010-07-31 14:29:06 -0400805 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400806}
807
Trond Myklebust17280172012-03-11 13:11:00 -0400808static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400810 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400811};
812
Chuck Lever3bd23842013-08-09 12:49:19 -0400813#else /* !CONFIG_NFS_V4_1 */
814
Chuck Lever5a580e02013-08-09 12:48:09 -0400815static int nfs4_setup_sequence(const struct nfs_server *server,
816 struct nfs4_sequence_args *args,
817 struct nfs4_sequence_res *res,
818 struct rpc_task *task)
819{
Peng Taocb04ad22014-06-11 05:24:15 +0800820 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
821 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400822}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400823
Peng Tao2c4b1312014-06-11 05:24:16 +0800824int nfs4_sequence_done(struct rpc_task *task,
825 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400826{
Chuck Lever3bd23842013-08-09 12:49:19 -0400827 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400828}
Peng Tao2c4b1312014-06-11 05:24:16 +0800829EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400830
831#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400832
Chuck Lever9915ea72013-08-09 12:48:27 -0400833static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
834{
835 struct nfs4_call_sync_data *data = calldata;
836 nfs4_setup_sequence(data->seq_server,
837 data->seq_args, data->seq_res, task);
838}
839
840static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
841{
842 struct nfs4_call_sync_data *data = calldata;
843 nfs4_sequence_done(task, data->seq_res);
844}
845
846static const struct rpc_call_ops nfs40_call_sync_ops = {
847 .rpc_call_prepare = nfs40_call_sync_prepare,
848 .rpc_call_done = nfs40_call_sync_done,
849};
850
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400851static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400852 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500853 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 struct nfs4_sequence_args *args,
855 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400856{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857 int ret;
858 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400859 struct nfs_client *clp = server->nfs_client;
860 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100861 .seq_server = server,
862 .seq_args = args,
863 .seq_res = res,
864 };
865 struct rpc_task_setup task_setup = {
866 .rpc_client = clnt,
867 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400868 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100869 .callback_data = &data
870 };
871
872 task = rpc_run_task(&task_setup);
873 if (IS_ERR(task))
874 ret = PTR_ERR(task);
875 else {
876 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400877 rpc_put_task(task);
878 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 return ret;
880}
881
Bryan Schumaker7c513052011-03-24 17:12:24 +0000882int nfs4_call_sync(struct rpc_clnt *clnt,
883 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000884 struct rpc_message *msg,
885 struct nfs4_sequence_args *args,
886 struct nfs4_sequence_res *res,
887 int cache_reply)
888{
Chuck Levera9c92d62013-08-09 12:48:18 -0400889 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400890 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000891}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
894{
895 struct nfs_inode *nfsi = NFS_I(dir);
896
897 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400898 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700899 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700901 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500902 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000903 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 spin_unlock(&dir->i_lock);
905}
906
907struct nfs4_opendata {
908 struct kref kref;
909 struct nfs_openargs o_arg;
910 struct nfs_openres o_res;
911 struct nfs_open_confirmargs c_arg;
912 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500913 struct nfs4_string owner_name;
914 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +0800915 struct nfs4_label *a_label;
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
Trond Myklebust6ae37332015-01-30 14:21:14 -0500943static u32
944nfs4_map_atomic_open_share(struct nfs_server *server,
945 fmode_t fmode, int openflags)
946{
947 u32 res = 0;
948
949 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
950 case FMODE_READ:
951 res = NFS4_SHARE_ACCESS_READ;
952 break;
953 case FMODE_WRITE:
954 res = NFS4_SHARE_ACCESS_WRITE;
955 break;
956 case FMODE_READ|FMODE_WRITE:
957 res = NFS4_SHARE_ACCESS_BOTH;
958 }
959 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
960 goto out;
961 /* Want no delegation if we're using O_DIRECT */
962 if (openflags & O_DIRECT)
963 res |= NFS4_SHARE_WANT_NO_DELEG;
964out:
965 return res;
966}
967
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400968static enum open_claim_type4
969nfs4_map_atomic_open_claim(struct nfs_server *server,
970 enum open_claim_type4 claim)
971{
972 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
973 return claim;
974 switch (claim) {
975 default:
976 return claim;
977 case NFS4_OPEN_CLAIM_FH:
978 return NFS4_OPEN_CLAIM_NULL;
979 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
980 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
981 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
982 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
983 }
984}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100987{
988 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400989 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100990 p->o_res.seqid = p->o_arg.seqid;
991 p->c_res.seqid = p->c_arg.seqid;
992 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400993 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100994 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500995 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100996}
997
Al Viro82a2c1b2011-06-22 18:30:55 -0400998static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100999 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001000 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001001 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001002 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001003 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001004{
Al Viro82a2c1b2011-06-22 18:30:55 -04001005 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001006 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001007 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001008 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001009 struct nfs4_opendata *p;
1010
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001011 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001012 if (p == NULL)
1013 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001014
1015 p->f_label = nfs4_label_alloc(server, gfp_mask);
1016 if (IS_ERR(p->f_label))
1017 goto err_free_p;
1018
Kinglong Meea49c2692015-07-27 15:31:38 +08001019 p->a_label = nfs4_label_alloc(server, gfp_mask);
1020 if (IS_ERR(p->a_label))
1021 goto err_free_f;
1022
Trond Myklebust63f5f792015-01-23 19:19:25 -05001023 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1024 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001025 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001026 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001027 nfs_sb_active(dentry->d_sb);
1028 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001029 p->dir = parent;
1030 p->owner = sp;
1031 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 p->o_arg.open_flags = flags;
1033 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001034 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1035 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001036 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1037 * will return permission denied for all bits until close */
1038 if (!(flags & O_EXCL)) {
1039 /* ask server to check for all possible rights as results
1040 * are cached */
1041 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1042 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001043 }
David Howells7539bba2006-08-22 20:06:09 -04001044 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001045 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1046 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001047 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001049 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001050 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001051 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001052 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1053 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001054 case NFS4_OPEN_CLAIM_NULL:
1055 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1056 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1057 p->o_arg.fh = NFS_FH(dir);
1058 break;
1059 case NFS4_OPEN_CLAIM_PREVIOUS:
1060 case NFS4_OPEN_CLAIM_FH:
1061 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1062 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001063 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001064 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001065 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001066 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001067
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001068 p->o_arg.u.attrs = &p->attrs;
1069 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001070
1071 verf[0] = jiffies;
1072 verf[1] = current->pid;
1073 memcpy(p->o_arg.u.verifier.data, verf,
1074 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001075 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001076 p->c_arg.fh = &p->o_res.fh;
1077 p->c_arg.stateid = &p->o_res.stateid;
1078 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001079 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001080 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001081 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001082
1083err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001084 nfs4_label_free(p->a_label);
1085err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001086 nfs4_label_free(p->f_label);
1087err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001088 kfree(p);
1089err:
1090 dput(parent);
1091 return NULL;
1092}
1093
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001094static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001095{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001096 struct nfs4_opendata *p = container_of(kref,
1097 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001098 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001099
1100 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 if (p->state != NULL)
1102 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001103 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001104
Kinglong Meea49c2692015-07-27 15:31:38 +08001105 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001106 nfs4_label_free(p->f_label);
1107
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001108 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001109 dput(p->dentry);
1110 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001111 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001112 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001113 kfree(p);
1114}
1115
1116static void nfs4_opendata_put(struct nfs4_opendata *p)
1117{
1118 if (p != NULL)
1119 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001120}
1121
Trond Myklebust06f814a2006-01-03 09:55:07 +01001122static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1123{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001124 int ret;
1125
Trond Myklebust06f814a2006-01-03 09:55:07 +01001126 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001127 return ret;
1128}
1129
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001130static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001131{
1132 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001133
Trond Myklebust536e43d2012-01-17 22:04:26 -05001134 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135 goto out;
1136 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001137 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001138 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1139 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001140 break;
1141 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001142 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1143 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001144 break;
1145 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001146 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1147 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001148 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001149out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001150 return ret;
1151}
1152
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001154{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001155 if (delegation == NULL)
1156 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001158 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001159 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1160 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001161 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001162 return 1;
1163}
1164
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001165static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001166{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001168 case FMODE_WRITE:
1169 state->n_wronly++;
1170 break;
1171 case FMODE_READ:
1172 state->n_rdonly++;
1173 break;
1174 case FMODE_READ|FMODE_WRITE:
1175 state->n_rdwr++;
1176 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001178}
1179
Trond Myklebust4f14c192014-02-12 19:15:06 -05001180static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001181{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001182 struct nfs_client *clp = state->owner->so_server->nfs_client;
1183 bool need_recover = false;
1184
1185 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1186 need_recover = true;
1187 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1188 need_recover = true;
1189 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1190 need_recover = true;
1191 if (need_recover)
1192 nfs4_state_mark_reclaim_nograce(clp, state);
1193}
1194
Trond Myklebuste999e802014-02-10 18:20:47 -05001195static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1196 nfs4_stateid *stateid)
1197{
1198 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1199 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001200 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1201 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001202 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001203 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001204 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1205 return true;
1206 return false;
1207}
1208
Trond Myklebustf95549c2015-01-23 18:06:09 -05001209static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1210{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001211 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1212 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001213 if (state->n_wronly)
1214 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1215 if (state->n_rdonly)
1216 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1217 if (state->n_rdwr)
1218 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001219 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001220}
1221
Trond Myklebust226056c2014-02-11 10:41:07 -05001222static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1223 nfs4_stateid *stateid, fmode_t fmode)
1224{
1225 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1226 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1227 case FMODE_WRITE:
1228 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1229 break;
1230 case FMODE_READ:
1231 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1232 break;
1233 case 0:
1234 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1235 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1236 clear_bit(NFS_OPEN_STATE, &state->flags);
1237 }
1238 if (stateid == NULL)
1239 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001240 /* Handle races with OPEN */
1241 if (!nfs4_stateid_match_other(stateid, &state->open_stateid) ||
1242 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1243 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001244 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001245 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001246 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001247 nfs4_stateid_copy(&state->stateid, stateid);
1248 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001249}
1250
1251static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1252{
1253 write_seqlock(&state->seqlock);
1254 nfs_clear_open_stateid_locked(state, stateid, fmode);
1255 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001256 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1257 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001258}
1259
Trond Myklebust003707c2007-07-05 18:07:55 -04001260static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1261{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001262 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001263 case FMODE_READ:
1264 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1265 break;
1266 case FMODE_WRITE:
1267 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1268 break;
1269 case FMODE_READ|FMODE_WRITE:
1270 set_bit(NFS_O_RDWR_STATE, &state->flags);
1271 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001272 if (!nfs_need_update_open_stateid(state, stateid))
1273 return;
1274 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1275 nfs4_stateid_copy(&state->stateid, stateid);
1276 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001277}
1278
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001279static 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 -07001280{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001281 /*
1282 * Protect the call to nfs4_state_set_mode_locked and
1283 * serialise the stateid update
1284 */
1285 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001286 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001287 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001288 set_bit(NFS_DELEGATED_STATE, &state->flags);
1289 }
1290 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001292 write_sequnlock(&state->seqlock);
1293 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001294 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001295 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001298static 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 -05001299{
1300 struct nfs_inode *nfsi = NFS_I(state->inode);
1301 struct nfs_delegation *deleg_cur;
1302 int ret = 0;
1303
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001304 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001305
1306 rcu_read_lock();
1307 deleg_cur = rcu_dereference(nfsi->delegation);
1308 if (deleg_cur == NULL)
1309 goto no_delegation;
1310
1311 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001312 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001313 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001314 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001315 goto no_delegation_unlock;
1316
1317 if (delegation == NULL)
1318 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001319 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001320 goto no_delegation_unlock;
1321
Trond Myklebustb7391f42008-12-23 15:21:52 -05001322 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001323 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001324 ret = 1;
1325no_delegation_unlock:
1326 spin_unlock(&deleg_cur->lock);
1327no_delegation:
1328 rcu_read_unlock();
1329
1330 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001331 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001332 ret = 1;
1333 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001334 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1335 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001336
1337 return ret;
1338}
1339
Trond Myklebust39071e62015-01-24 15:07:56 -05001340static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1341 const nfs4_stateid *stateid)
1342{
1343 struct nfs4_state *state = lsp->ls_state;
1344 bool ret = false;
1345
1346 spin_lock(&state->state_lock);
1347 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1348 goto out_noupdate;
1349 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1350 goto out_noupdate;
1351 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1352 ret = true;
1353out_noupdate:
1354 spin_unlock(&state->state_lock);
1355 return ret;
1356}
Trond Myklebust34310432008-12-23 15:21:38 -05001357
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001359{
1360 struct nfs_delegation *delegation;
1361
1362 rcu_read_lock();
1363 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001364 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001365 rcu_read_unlock();
1366 return;
1367 }
1368 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001369 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001370}
1371
Trond Myklebust6ee41262007-07-08 14:11:36 -04001372static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001373{
1374 struct nfs4_state *state = opendata->state;
1375 struct nfs_inode *nfsi = NFS_I(state->inode);
1376 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001377 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001378 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001379 nfs4_stateid stateid;
1380 int ret = -EAGAIN;
1381
Trond Myklebustaac00a82007-07-05 19:02:21 -04001382 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001383 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001384 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001385 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001386 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001387 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001388 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001389 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001390 rcu_read_lock();
1391 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001392 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001393 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001394 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001395 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001396 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001397 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001398 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001399 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001400 if (!opendata->is_recover) {
1401 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1402 if (ret != 0)
1403 goto out;
1404 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001405 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001406
1407 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001408 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001409 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001410 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001411out:
1412 return ERR_PTR(ret);
1413out_return_state:
1414 atomic_inc(&state->count);
1415 return state;
1416}
1417
Andy Adamsone23008e2012-10-02 21:07:32 -04001418static void
1419nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1420{
1421 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1422 struct nfs_delegation *delegation;
1423 int delegation_flags = 0;
1424
1425 rcu_read_lock();
1426 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1427 if (delegation)
1428 delegation_flags = delegation->flags;
1429 rcu_read_unlock();
1430 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1431 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1432 "returning a delegation for "
1433 "OPEN(CLAIM_DELEGATE_CUR)\n",
1434 clp->cl_hostname);
1435 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1436 nfs_inode_set_delegation(state->inode,
1437 data->owner->so_cred,
1438 &data->o_res);
1439 else
1440 nfs_inode_reclaim_delegation(state->inode,
1441 data->owner->so_cred,
1442 &data->o_res);
1443}
1444
1445/*
1446 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1447 * and update the nfs4_state.
1448 */
1449static struct nfs4_state *
1450_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1451{
1452 struct inode *inode = data->state->inode;
1453 struct nfs4_state *state = data->state;
1454 int ret;
1455
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001456 if (!data->rpc_done) {
1457 if (data->rpc_status) {
1458 ret = data->rpc_status;
1459 goto err;
1460 }
1461 /* cached opens have already been processed */
1462 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001463 }
1464
Andy Adamsone23008e2012-10-02 21:07:32 -04001465 ret = nfs_refresh_inode(inode, &data->f_attr);
1466 if (ret)
1467 goto err;
1468
1469 if (data->o_res.delegation_type != 0)
1470 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001471update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001472 update_open_stateid(state, &data->o_res.stateid, NULL,
1473 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001474 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001475
1476 return state;
1477err:
1478 return ERR_PTR(ret);
1479
1480}
1481
1482static struct nfs4_state *
1483_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001484{
1485 struct inode *inode;
1486 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001487 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488
Trond Myklebustaac00a82007-07-05 19:02:21 -04001489 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001490 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001491 goto out;
1492 }
1493
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001494 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001495 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001496 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001497 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001498 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001499 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001500 goto err;
1501 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001502 state = nfs4_get_open_state(inode, data->owner);
1503 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001504 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001505 if (data->o_res.delegation_type != 0)
1506 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001507 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001508 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001510out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001511 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001513err_put_inode:
1514 iput(inode);
1515err:
1516 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517}
1518
Andy Adamsone23008e2012-10-02 21:07:32 -04001519static struct nfs4_state *
1520nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1521{
1522 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1523 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1524 return _nfs4_opendata_to_nfs4_state(data);
1525}
1526
Trond Myklebust864472e2006-01-03 09:55:15 +01001527static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1528{
1529 struct nfs_inode *nfsi = NFS_I(state->inode);
1530 struct nfs_open_context *ctx;
1531
1532 spin_lock(&state->inode->i_lock);
1533 list_for_each_entry(ctx, &nfsi->open_files, list) {
1534 if (ctx->state != state)
1535 continue;
1536 get_nfs_open_context(ctx);
1537 spin_unlock(&state->inode->i_lock);
1538 return ctx;
1539 }
1540 spin_unlock(&state->inode->i_lock);
1541 return ERR_PTR(-ENOENT);
1542}
1543
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001544static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1545 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001546{
1547 struct nfs4_opendata *opendata;
1548
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001549 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001550 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001551 if (opendata == NULL)
1552 return ERR_PTR(-ENOMEM);
1553 opendata->state = state;
1554 atomic_inc(&state->count);
1555 return opendata;
1556}
1557
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001558static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001559{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001560 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001561 int ret;
1562
NeilBrown39f897f2015-06-29 14:28:54 +10001563 if ((opendata->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
1564 opendata->o_arg.claim == NFS4_OPEN_CLAIM_DELEG_CUR_FH) &&
1565 (opendata->o_arg.u.delegation_type & fmode) != fmode)
1566 /* This mode can't have been delegated, so we must have
1567 * a valid open_stateid to cover it - not need to reclaim.
1568 */
1569 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001570 opendata->o_arg.open_flags = 0;
1571 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001572 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1573 NFS_SB(opendata->dentry->d_sb),
1574 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001575 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1576 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1577 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001578 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001579 if (ret != 0)
1580 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001581 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001582 if (IS_ERR(newstate))
1583 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001584 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001585 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001586 return 0;
1587}
1588
1589static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1590{
Trond Myklebust864472e2006-01-03 09:55:15 +01001591 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001592 int ret;
1593
Trond Myklebust4f14c192014-02-12 19:15:06 -05001594 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1595 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1596 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1597 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001598 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001599 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001600 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001601 smp_rmb();
1602 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001603 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001604 if (ret != 0)
1605 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001606 if (newstate != state)
1607 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001608 }
1609 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001610 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001611 if (ret != 0)
1612 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001613 if (newstate != state)
1614 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001615 }
1616 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001617 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001618 if (ret != 0)
1619 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001620 if (newstate != state)
1621 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001622 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001623 /*
1624 * We may have performed cached opens for all three recoveries.
1625 * Check if we need to update the current stateid.
1626 */
1627 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001628 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001629 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001630 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001631 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001632 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001633 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001634 return 0;
1635}
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637/*
1638 * OPEN_RECLAIM:
1639 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001641static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001643 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001644 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001645 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 int status;
1647
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001648 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1649 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001650 if (IS_ERR(opendata))
1651 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001652 rcu_read_lock();
1653 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001654 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001655 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001656 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001657 opendata->o_arg.u.delegation_type = delegation_type;
1658 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001659 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return status;
1661}
1662
Trond Myklebust539cd032007-06-05 11:46:42 -04001663static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 struct nfs_server *server = NFS_SERVER(state->inode);
1666 struct nfs4_exception exception = { };
1667 int err;
1668 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001669 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001670 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001671 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1672 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001673 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001674 break;
1675 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 } while (exception.retry);
1677 return err;
1678}
1679
Trond Myklebust864472e2006-01-03 09:55:15 +01001680static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1681{
1682 struct nfs_open_context *ctx;
1683 int ret;
1684
1685 ctx = nfs4_state_find_open_context(state);
1686 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001687 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001688 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001689 put_nfs_open_context(ctx);
1690 return ret;
1691}
1692
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001693static 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 -07001694{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001695 switch (err) {
1696 default:
1697 printk(KERN_ERR "NFS: %s: unhandled error "
1698 "%d.\n", __func__, err);
1699 case 0:
1700 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001701 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001702 case -ESTALE:
1703 break;
1704 case -NFS4ERR_BADSESSION:
1705 case -NFS4ERR_BADSLOT:
1706 case -NFS4ERR_BAD_HIGH_SLOT:
1707 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1708 case -NFS4ERR_DEADSESSION:
1709 set_bit(NFS_DELEGATED_STATE, &state->flags);
1710 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1711 return -EAGAIN;
1712 case -NFS4ERR_STALE_CLIENTID:
1713 case -NFS4ERR_STALE_STATEID:
1714 set_bit(NFS_DELEGATED_STATE, &state->flags);
1715 case -NFS4ERR_EXPIRED:
1716 /* Don't recall a delegation if it was lost */
1717 nfs4_schedule_lease_recovery(server->nfs_client);
1718 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001719 case -NFS4ERR_MOVED:
1720 nfs4_schedule_migration_recovery(server);
1721 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001722 case -NFS4ERR_LEASE_MOVED:
1723 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1724 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001725 case -NFS4ERR_DELEG_REVOKED:
1726 case -NFS4ERR_ADMIN_REVOKED:
1727 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001728 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001729 nfs_inode_find_state_and_recover(state->inode,
1730 stateid);
1731 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001732 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001733 case -NFS4ERR_DELAY:
1734 case -NFS4ERR_GRACE:
1735 set_bit(NFS_DELEGATED_STATE, &state->flags);
1736 ssleep(1);
1737 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001738 case -ENOMEM:
1739 case -NFS4ERR_DENIED:
1740 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1741 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return err;
1744}
1745
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001746int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1747{
1748 struct nfs_server *server = NFS_SERVER(state->inode);
1749 struct nfs4_opendata *opendata;
1750 int err;
1751
1752 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1753 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1754 if (IS_ERR(opendata))
1755 return PTR_ERR(opendata);
1756 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1757 err = nfs4_open_recover(opendata, state);
1758 nfs4_opendata_put(opendata);
1759 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1760}
1761
Chuck Leverbe05c862013-08-09 12:49:47 -04001762static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1763{
1764 struct nfs4_opendata *data = calldata;
1765
Peng Taocb04ad22014-06-11 05:24:15 +08001766 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1767 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001768}
1769
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001770static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1771{
1772 struct nfs4_opendata *data = calldata;
1773
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001774 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001775
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001776 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001777 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001778 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001779 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001780 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001781 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001782 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001783}
1784
1785static void nfs4_open_confirm_release(void *calldata)
1786{
1787 struct nfs4_opendata *data = calldata;
1788 struct nfs4_state *state = NULL;
1789
1790 /* If this request hasn't been cancelled, do nothing */
1791 if (data->cancelled == 0)
1792 goto out_free;
1793 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001794 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001795 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001796 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001797 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001798 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001799out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001800 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001801}
1802
1803static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001804 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001805 .rpc_call_done = nfs4_open_confirm_done,
1806 .rpc_release = nfs4_open_confirm_release,
1807};
1808
1809/*
1810 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1811 */
1812static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1813{
David Howells2b0143b2015-03-17 22:25:59 +00001814 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001815 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001816 struct rpc_message msg = {
1817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1818 .rpc_argp = &data->c_arg,
1819 .rpc_resp = &data->c_res,
1820 .rpc_cred = data->owner->so_cred,
1821 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001822 struct rpc_task_setup task_setup_data = {
1823 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001824 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001825 .callback_ops = &nfs4_open_confirm_ops,
1826 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001827 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001828 .flags = RPC_TASK_ASYNC,
1829 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 int status;
1831
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001832 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001833 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001834 data->rpc_done = 0;
1835 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001836 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001837 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001838 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001839 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001840 status = nfs4_wait_for_completion_rpc_task(task);
1841 if (status != 0) {
1842 data->cancelled = 1;
1843 smp_wmb();
1844 } else
1845 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001846 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return status;
1848}
1849
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001850static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001852 struct nfs4_opendata *data = calldata;
1853 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001854 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001855
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001856 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001857 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001858 /*
1859 * Check if we still need to send an OPEN call, or if we can use
1860 * a delegation instead.
1861 */
1862 if (data->state != NULL) {
1863 struct nfs_delegation *delegation;
1864
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001865 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001866 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001867 rcu_read_lock();
1868 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001869 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001870 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001871 can_open_delegated(delegation, data->o_arg.fmode))
1872 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001873 rcu_read_unlock();
1874 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001875 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001876 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001877 switch (data->o_arg.claim) {
1878 case NFS4_OPEN_CLAIM_PREVIOUS:
1879 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1880 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001881 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001882 case NFS4_OPEN_CLAIM_FH:
1883 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001884 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1885 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001886 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001887 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001888 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001889 &data->o_res.seq_res,
1890 task) != 0)
1891 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001892
1893 /* Set the create mode (note dependency on the session type) */
1894 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1895 if (data->o_arg.open_flags & O_EXCL) {
1896 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1897 if (nfs4_has_persistent_session(clp))
1898 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1899 else if (clp->cl_mvops->minor_version > 0)
1900 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1901 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001902 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001903unlock_no_action:
1904 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001905out_no_action:
1906 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001907out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001908 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001909}
1910
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001911static void nfs4_open_done(struct rpc_task *task, void *calldata)
1912{
1913 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001915 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001916
Trond Myklebust14516c32010-07-31 14:29:06 -04001917 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1918 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001919
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001920 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001921 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1922 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001923 case S_IFREG:
1924 break;
1925 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001926 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001927 break;
1928 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001929 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001930 break;
1931 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001932 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001933 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001934 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001935 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001936 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1937 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001938 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001939 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001940}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001941
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001942static void nfs4_open_release(void *calldata)
1943{
1944 struct nfs4_opendata *data = calldata;
1945 struct nfs4_state *state = NULL;
1946
1947 /* If this request hasn't been cancelled, do nothing */
1948 if (data->cancelled == 0)
1949 goto out_free;
1950 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001951 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001952 goto out_free;
1953 /* In case we need an open_confirm, no cleanup! */
1954 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1955 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001956 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001957 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001958 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001959out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001960 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001961}
1962
1963static const struct rpc_call_ops nfs4_open_ops = {
1964 .rpc_call_prepare = nfs4_open_prepare,
1965 .rpc_call_done = nfs4_open_done,
1966 .rpc_release = nfs4_open_release,
1967};
1968
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001969static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001970{
David Howells2b0143b2015-03-17 22:25:59 +00001971 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001972 struct nfs_server *server = NFS_SERVER(dir);
1973 struct nfs_openargs *o_arg = &data->o_arg;
1974 struct nfs_openres *o_res = &data->o_res;
1975 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001976 struct rpc_message msg = {
1977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1978 .rpc_argp = o_arg,
1979 .rpc_resp = o_res,
1980 .rpc_cred = data->owner->so_cred,
1981 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001982 struct rpc_task_setup task_setup_data = {
1983 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001984 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001985 .callback_ops = &nfs4_open_ops,
1986 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001987 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001988 .flags = RPC_TASK_ASYNC,
1989 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001990 int status;
1991
Chuck Levera9c92d62013-08-09 12:48:18 -04001992 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001993 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001994 data->rpc_done = 0;
1995 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001996 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001997 data->is_recover = 0;
1998 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001999 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002000 data->is_recover = 1;
2001 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002002 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002003 if (IS_ERR(task))
2004 return PTR_ERR(task);
2005 status = nfs4_wait_for_completion_rpc_task(task);
2006 if (status != 0) {
2007 data->cancelled = 1;
2008 smp_wmb();
2009 } else
2010 status = data->rpc_status;
2011 rpc_put_task(task);
2012
2013 return status;
2014}
2015
2016static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2017{
David Howells2b0143b2015-03-17 22:25:59 +00002018 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002019 struct nfs_openres *o_res = &data->o_res;
2020 int status;
2021
2022 status = nfs4_run_open_task(data, 1);
2023 if (status != 0 || !data->rpc_done)
2024 return status;
2025
Trond Myklebust6926afd2012-01-07 13:22:46 -05002026 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2027
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002028 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2029 status = _nfs4_proc_open_confirm(data);
2030 if (status != 0)
2031 return status;
2032 }
2033
2034 return status;
2035}
2036
Trond Myklebustf3792d62014-07-10 08:54:32 -04002037/*
2038 * Additional permission checks in order to distinguish between an
2039 * open for read, and an open for execute. This works around the
2040 * fact that NFSv4 OPEN treats read and execute permissions as being
2041 * the same.
2042 * Note that in the non-execute case, we want to turn off permission
2043 * checking if we just created a new file (POSIX open() semantics).
2044 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002045static int nfs4_opendata_access(struct rpc_cred *cred,
2046 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002047 struct nfs4_state *state, fmode_t fmode,
2048 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002049{
2050 struct nfs_access_entry cache;
2051 u32 mask;
2052
2053 /* access call failed or for some reason the server doesn't
2054 * support any access modes -- defer access call until later */
2055 if (opendata->o_res.access_supported == 0)
2056 return 0;
2057
2058 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002059 /*
2060 * Use openflags to check for exec, because fmode won't
2061 * always have FMODE_EXEC set when file open for exec.
2062 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002063 if (openflags & __FMODE_EXEC) {
2064 /* ONLY check for exec rights */
2065 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002066 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002067 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002068
2069 cache.cred = cred;
2070 cache.jiffies = jiffies;
2071 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2072 nfs_access_add_cache(state->inode, &cache);
2073
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002074 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002075 return 0;
2076
2077 /* even though OPEN succeeded, access is denied. Close the file */
2078 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002079 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002080}
2081
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002082/*
2083 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2084 */
2085static int _nfs4_proc_open(struct nfs4_opendata *data)
2086{
David Howells2b0143b2015-03-17 22:25:59 +00002087 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002088 struct nfs_server *server = NFS_SERVER(dir);
2089 struct nfs_openargs *o_arg = &data->o_arg;
2090 struct nfs_openres *o_res = &data->o_res;
2091 int status;
2092
2093 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002094 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002095 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002096 if (status != 0) {
2097 if (status == -NFS4ERR_BADNAME &&
2098 !(o_arg->open_flags & O_CREAT))
2099 return -ENOENT;
2100 return status;
2101 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002102
Trond Myklebust6926afd2012-01-07 13:22:46 -05002103 nfs_fattr_map_and_free_names(server, &data->f_attr);
2104
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002105 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002106 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002107 if (o_arg->open_flags & O_EXCL)
2108 data->file_created = 1;
2109 else if (o_res->cinfo.before != o_res->cinfo.after)
2110 data->file_created = 1;
2111 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002112 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2113 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002115 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002120 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Andy Adamsond83217c2011-03-01 01:34:17 +00002124static int nfs4_recover_expired_lease(struct nfs_server *server)
2125{
2126 return nfs4_client_recover_expired_lease(server->nfs_client);
2127}
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129/*
2130 * OPEN_EXPIRED:
2131 * reclaim state on the server after a network partition.
2132 * Assumes caller holds the appropriate lock
2133 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002134static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002136 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002137 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002139 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002140 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002141 if (IS_ERR(opendata))
2142 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002143 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002144 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002145 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002146 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002147 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002150static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002151{
Trond Myklebust539cd032007-06-05 11:46:42 -04002152 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002153 struct nfs4_exception exception = { };
2154 int err;
2155
2156 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002157 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002158 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002159 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2160 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002161 switch (err) {
2162 default:
2163 goto out;
2164 case -NFS4ERR_GRACE:
2165 case -NFS4ERR_DELAY:
2166 nfs4_handle_exception(server, err, &exception);
2167 err = 0;
2168 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002169 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002170out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002171 return err;
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002177 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Trond Myklebust864472e2006-01-03 09:55:15 +01002179 ctx = nfs4_state_find_open_context(state);
2180 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002181 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002182 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002183 put_nfs_open_context(ctx);
2184 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002187static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2188{
2189 nfs_remove_bad_delegation(state->inode);
2190 write_seqlock(&state->seqlock);
2191 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2192 write_sequnlock(&state->seqlock);
2193 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2194}
2195
2196static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2197{
2198 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2199 nfs_finish_clear_delegation_stateid(state);
2200}
2201
2202static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2203{
2204 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2205 nfs40_clear_delegation_stateid(state);
2206 return nfs4_open_expired(sp, state);
2207}
2208
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002209#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002210static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002211{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002212 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002213 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002214 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002215 struct rpc_cred *cred;
2216 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002217
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002218 /* Get the delegation credential for use by test/free_stateid */
2219 rcu_read_lock();
2220 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002221 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002222 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002223 return;
2224 }
2225
2226 nfs4_stateid_copy(&stateid, &delegation->stateid);
2227 cred = get_rpccred(delegation->cred);
2228 rcu_read_unlock();
2229 status = nfs41_test_stateid(server, &stateid, cred);
2230 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002231
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002232 if (status != NFS_OK) {
2233 /* Free the stateid unless the server explicitly
2234 * informs us the stateid is unrecognized. */
2235 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002236 nfs41_free_stateid(server, &stateid, cred);
2237 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002238 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002239
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002240 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002241}
2242
2243/**
2244 * nfs41_check_open_stateid - possibly free an open stateid
2245 *
2246 * @state: NFSv4 state for an inode
2247 *
2248 * Returns NFS_OK if recovery for this stateid is now finished.
2249 * Otherwise a negative NFS4ERR value is returned.
2250 */
2251static int nfs41_check_open_stateid(struct nfs4_state *state)
2252{
2253 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002254 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002255 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002256 int status;
2257
2258 /* If a state reset has been done, test_stateid is unneeded */
2259 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2260 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2261 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2262 return -NFS4ERR_BAD_STATEID;
2263
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002264 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002265 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002266 if (status != NFS_OK) {
2267 /* Free the stateid unless the server explicitly
2268 * informs us the stateid is unrecognized. */
2269 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002270 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002271
2272 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2273 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2274 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002275 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002276 }
2277 return status;
2278}
2279
2280static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2281{
Chuck Levereb64cf92012-07-11 16:30:05 -04002282 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002283
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002284 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002285 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002286 if (status != NFS_OK)
2287 status = nfs4_open_expired(sp, state);
2288 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002289}
2290#endif
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002293 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2294 * fields corresponding to attributes that were used to store the verifier.
2295 * Make sure we clobber those fields in the later setattr call
2296 */
2297static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2298{
2299 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2300 !(sattr->ia_valid & ATTR_ATIME_SET))
2301 sattr->ia_valid |= ATTR_ATIME;
2302
2303 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2304 !(sattr->ia_valid & ATTR_MTIME_SET))
2305 sattr->ia_valid |= ATTR_MTIME;
2306}
2307
Trond Myklebustc21443c2013-02-07 14:26:21 -05002308static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2309 fmode_t fmode,
2310 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002311 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002312{
2313 struct nfs4_state_owner *sp = opendata->owner;
2314 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002315 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002316 struct nfs4_state *state;
2317 unsigned int seq;
2318 int ret;
2319
2320 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2321
2322 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002323 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002324 goto out;
2325
2326 state = nfs4_opendata_to_nfs4_state(opendata);
2327 ret = PTR_ERR(state);
2328 if (IS_ERR(state))
2329 goto out;
2330 if (server->caps & NFS_CAP_POSIX_LOCK)
2331 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2332
Trond Myklebust275bb302013-05-29 13:11:28 -04002333 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002334 if (d_really_is_negative(dentry)) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002335 /* FIXME: Is this d_drop() ever needed? */
2336 d_drop(dentry);
2337 dentry = d_add_unique(dentry, igrab(state->inode));
2338 if (dentry == NULL) {
2339 dentry = opendata->dentry;
2340 } else if (dentry != ctx->dentry) {
2341 dput(ctx->dentry);
2342 ctx->dentry = dget(dentry);
2343 }
2344 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002345 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002346 }
2347
Trond Myklebustc21443c2013-02-07 14:26:21 -05002348 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2349 if (ret != 0)
2350 goto out;
2351
Trond Myklebust3efb9722013-05-29 13:17:04 -04002352 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002353 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002354 nfs_inode_attach_open_context(ctx);
2355 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2356 nfs4_schedule_stateid_recovery(server, state);
2357 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002358out:
2359 return ret;
2360}
2361
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002362/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002363 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 */
Andy Adamson82be4172012-05-23 05:02:35 -04002365static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002366 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002367 int flags,
2368 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002369 struct nfs4_label *label,
2370 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 struct nfs4_state_owner *sp;
2373 struct nfs4_state *state = NULL;
2374 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002375 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002376 struct dentry *dentry = ctx->dentry;
2377 struct rpc_cred *cred = ctx->cred;
2378 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2379 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002380 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002381 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002382 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002386 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2387 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2389 goto out_err;
2390 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002391 status = nfs4_recover_expired_lease(server);
2392 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002393 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002394 if (d_really_is_positive(dentry))
2395 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002396 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002397 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002398 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002399 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002400 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002401 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002402 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
David Quigley14c43f72013-05-22 12:50:43 -04002404 if (label) {
2405 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2406 if (IS_ERR(olabel)) {
2407 status = PTR_ERR(olabel);
2408 goto err_opendata_put;
2409 }
2410 }
2411
Trond Myklebuste911b812014-03-26 13:24:37 -07002412 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2413 if (!opendata->f_attr.mdsthreshold) {
2414 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2415 if (!opendata->f_attr.mdsthreshold)
2416 goto err_free_label;
2417 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002418 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002419 }
David Howells2b0143b2015-03-17 22:25:59 +00002420 if (d_really_is_positive(dentry))
2421 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002422
Trond Myklebust3efb9722013-05-29 13:17:04 -04002423 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002424 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002425 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002426 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002427
Trond Myklebust549b19c2013-04-16 18:42:34 -04002428 if ((opendata->o_arg.open_flags & O_EXCL) &&
2429 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002430 nfs4_exclusive_attrset(opendata, sattr);
2431
2432 nfs_fattr_init(opendata->o_res.f_attr);
2433 status = nfs4_do_setattr(state->inode, cred,
2434 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002435 state, label, olabel);
2436 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05002437 nfs_setattr_update_inode(state->inode, sattr,
2438 opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002439 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002440 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002441 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002442 if (opendata->file_created)
2443 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002444
Trond Myklebuste911b812014-03-26 13:24:37 -07002445 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002446 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002447 opendata->f_attr.mdsthreshold = NULL;
2448 }
Andy Adamson82be4172012-05-23 05:02:35 -04002449
David Quigley14c43f72013-05-22 12:50:43 -04002450 nfs4_label_free(olabel);
2451
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002452 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002455err_free_label:
2456 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002457err_opendata_put:
2458 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002459err_put_state_owner:
2460 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return status;
2463}
2464
2465
Andy Adamson82be4172012-05-23 05:02:35 -04002466static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002467 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002468 int flags,
2469 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002470 struct nfs4_label *label,
2471 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002473 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 struct nfs4_exception exception = { };
2475 struct nfs4_state *res;
2476 int status;
2477
2478 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002479 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002480 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002481 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (status == 0)
2483 break;
2484 /* NOTE: BAD_SEQID means the server and client disagree about the
2485 * book-keeping w.r.t. state-changing operations
2486 * (OPEN/CLOSE/LOCK/LOCKU...)
2487 * It is actually a sign of a bug on the client or on the server.
2488 *
2489 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002490 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 * have unhashed the old state_owner for us, and that we can
2492 * therefore safely retry using a new one. We should still warn
2493 * the user though...
2494 */
2495 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002496 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002497 " returned a bad sequence-id error!\n",
2498 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 exception.retry = 1;
2500 continue;
2501 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002502 /*
2503 * BAD_STATEID on OPEN means that the server cancelled our
2504 * state before it received the OPEN_CONFIRM.
2505 * Recover by retrying the request as per the discussion
2506 * on Page 181 of RFC3530.
2507 */
2508 if (status == -NFS4ERR_BAD_STATEID) {
2509 exception.retry = 1;
2510 continue;
2511 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002512 if (status == -EAGAIN) {
2513 /* We must have found a delegation */
2514 exception.retry = 1;
2515 continue;
2516 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002517 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2518 continue;
2519 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 status, &exception));
2521 } while (exception.retry);
2522 return res;
2523}
2524
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002525static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2526 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002527 struct nfs4_state *state, struct nfs4_label *ilabel,
2528 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002530 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002532 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 .iap = sattr,
2534 .server = server,
2535 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002536 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 };
2538 struct nfs_setattrres res = {
2539 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002540 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 .server = server,
2542 };
2543 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2545 .rpc_argp = &arg,
2546 .rpc_resp = &res,
2547 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002549 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002550 fmode_t fmode;
2551 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002552 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
David Quigleyaa9c2662013-05-22 12:50:44 -04002554 arg.bitmask = nfs4_bitmask(server, ilabel);
2555 if (ilabel)
2556 arg.bitmask = nfs4_bitmask(server, olabel);
2557
Trond Myklebust0e574af2005-10-27 22:12:38 -04002558 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Trond Myklebustee3ae842013-04-29 10:35:36 -04002560 /* Servers should only apply open mode checks for file size changes */
2561 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2562 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2563
2564 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2565 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002566 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002567 struct nfs_lockowner lockowner = {
2568 .l_owner = current->files,
2569 .l_pid = current->tgid,
2570 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002571 if (!nfs4_valid_open_stateid(state))
2572 return -EBADF;
2573 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2574 &lockowner) == -EIO)
2575 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002576 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002577 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Bryan Schumaker7c513052011-03-24 17:12:24 +00002579 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002580 if (status == 0 && state != NULL)
2581 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002582 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002585static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2586 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002587 struct nfs4_state *state, struct nfs4_label *ilabel,
2588 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002590 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002591 struct nfs4_exception exception = {
2592 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002593 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002594 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 int err;
2596 do {
David Quigley1775fd32013-05-22 12:50:42 -04002597 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002598 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002599 switch (err) {
2600 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002601 if (!(sattr->ia_valid & ATTR_SIZE)) {
2602 pr_warn_once("NFSv4: server %s is incorrectly "
2603 "applying open mode checks to "
2604 "a SETATTR that is not "
2605 "changing file size.\n",
2606 server->nfs_client->cl_hostname);
2607 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002608 if (state && !(state->state & FMODE_WRITE)) {
2609 err = -EBADF;
2610 if (sattr->ia_valid & ATTR_OPEN)
2611 err = -EACCES;
2612 goto out;
2613 }
2614 }
2615 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002617out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 return err;
2619}
2620
2621struct nfs4_closedata {
2622 struct inode *inode;
2623 struct nfs4_state *state;
2624 struct nfs_closeargs arg;
2625 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002626 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002627 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002628 bool roc;
2629 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630};
2631
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002632static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002633{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002634 struct nfs4_closedata *calldata = data;
2635 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002636 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002637
Fred Isamanf7e89172011-01-06 11:36:32 +00002638 if (calldata->roc)
2639 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002640 nfs4_put_open_state(calldata->state);
2641 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002642 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002643 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002644 kfree(calldata);
2645}
2646
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002647static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002649 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002652 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Chuck Levera3ca5652012-03-01 17:00:40 -05002654 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002655 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2656 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002657 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 /* hmm. we are done with the inode, and in the process of freeing
2659 * the state_owner. we keep this around to process errors
2660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 switch (task->tk_status) {
2662 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002663 res_stateid = &calldata->res.stateid;
2664 if (calldata->arg.fmode == 0 && calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002665 pnfs_roc_set_barrier(state->inode,
2666 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002667 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002668 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002669 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002671 case -NFS4ERR_OLD_STATEID:
2672 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002674 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002675 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002676 rpc_restart_call_prepare(task);
2677 goto out_release;
2678 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002679 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002682 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002683 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002684 goto out_release;
2685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
Trond Myklebust412f6c42014-08-25 22:09:08 -04002687 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002688out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002689 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002690 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002691 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002694static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002696 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002697 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002698 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002699 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002700 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002701
Chuck Levera3ca5652012-03-01 17:00:40 -05002702 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002703 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002704 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002705
Trond Myklebust88069f72009-12-08 08:33:16 -05002706 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002707 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002708 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2709 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2710 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002711 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002712 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002713 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002714 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002715 if (state->n_rdonly == 0)
2716 call_close |= is_rdonly;
2717 else if (is_rdonly)
2718 calldata->arg.fmode |= FMODE_READ;
2719 if (state->n_wronly == 0)
2720 call_close |= is_wronly;
2721 else if (is_wronly)
2722 calldata->arg.fmode |= FMODE_WRITE;
2723 } else if (is_rdwr)
2724 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2725
2726 if (calldata->arg.fmode == 0)
2727 call_close |= is_rdwr;
2728
Trond Myklebust5d422302013-03-14 16:57:48 -04002729 if (!nfs4_valid_open_stateid(state))
2730 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002731 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002732
2733 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002734 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002735 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002736 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002737
Fred Isamanf7e89172011-01-06 11:36:32 +00002738 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002739 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002740 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002741 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2742 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002743 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002744 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002745 }
Trond Myklebust6ae37332015-01-30 14:21:14 -05002746 calldata->arg.share_access =
2747 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2748 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002749
Trond Myklebust516a6af2005-10-27 22:12:41 -04002750 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002751 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002752 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002753 &calldata->arg.seq_args,
2754 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002755 task) != 0)
2756 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002757 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002758 return;
2759out_no_action:
2760 task->tk_action = NULL;
2761out_wait:
2762 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002765static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002766 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002767 .rpc_call_done = nfs4_close_done,
2768 .rpc_release = nfs4_free_closedata,
2769};
2770
Peng Taofe08c542014-07-03 13:05:01 +08002771static bool nfs4_roc(struct inode *inode)
2772{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002773 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002774 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002775 return pnfs_roc(inode);
2776}
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778/*
2779 * It is possible for data to be read/written from a mem-mapped file
2780 * after the sys_close call (which hits the vfs layer as a flush).
2781 * This means that we can't safely call nfsv4 close on a file until
2782 * the inode is cleared. This in turn means that we are not good
2783 * NFSv4 citizens - we do not indicate to the server to update the file's
2784 * share state even when we are done with one of the three share
2785 * stateid's in the inode.
2786 *
2787 * NOTE: Caller must be holding the sp->so_owner semaphore!
2788 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002789int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002791 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002792 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002794 struct nfs4_state_owner *sp = state->owner;
2795 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002796 struct rpc_message msg = {
2797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2798 .rpc_cred = state->owner->so_cred,
2799 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002800 struct rpc_task_setup task_setup_data = {
2801 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002802 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002803 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002804 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002805 .flags = RPC_TASK_ASYNC,
2806 };
Trond Myklebust95121352005-10-18 14:20:12 -07002807 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002809 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2810 &task_setup_data.rpc_client, &msg);
2811
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002812 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002814 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002815 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002816 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002818 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002820 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2821 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002822 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002823 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002824 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002825 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002826 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002827 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002828 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002829 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002830 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002831
Trond Myklebust1174dd12010-12-21 10:52:24 -05002832 msg.rpc_argp = &calldata->arg;
2833 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002834 task_setup_data.callback_data = calldata;
2835 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002836 if (IS_ERR(task))
2837 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002838 status = 0;
2839 if (wait)
2840 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002841 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002842 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002843out_free_calldata:
2844 kfree(calldata);
2845out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002846 nfs4_put_open_state(state);
2847 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002848 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
Trond Myklebust2b484292010-09-17 10:56:51 -04002851static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002852nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2853 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002856 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2857
2858 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Trond Myklebust565277f2007-10-15 18:17:53 -04002860 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002861 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002862
2863 nfs4_label_release_security(label);
2864
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002865 if (IS_ERR(state))
2866 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002867 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
Trond Myklebust1185a552009-12-03 15:54:02 -05002870static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002871{
2872 if (ctx->state == NULL)
2873 return;
2874 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002875 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002876 else
Al Viro643168c2011-06-22 18:20:23 -04002877 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002878}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Trond Myklebustb944dba2013-11-04 15:20:20 -05002880#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2881#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002882#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2885{
Benny Halevy43652ad2009-04-01 09:21:54 -04002886 struct nfs4_server_caps_arg args = {
2887 .fhandle = fhandle,
2888 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct nfs4_server_caps_res res = {};
2890 struct rpc_message msg = {
2891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002892 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 .rpc_resp = &res,
2894 };
2895 int status;
2896
Bryan Schumaker7c513052011-03-24 17:12:24 +00002897 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002899 /* Sanity check the server answers */
2900 switch (server->nfs_client->cl_minorversion) {
2901 case 0:
2902 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2903 res.attr_bitmask[2] = 0;
2904 break;
2905 case 1:
2906 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2907 break;
2908 case 2:
2909 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002912 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2913 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2914 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2915 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002916 NFS_CAP_CTIME|NFS_CAP_MTIME|
2917 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002918 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2919 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 server->caps |= NFS_CAP_ACLS;
2921 if (res.has_links != 0)
2922 server->caps |= NFS_CAP_HARDLINKS;
2923 if (res.has_symlinks != 0)
2924 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002925 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2926 server->caps |= NFS_CAP_FILEID;
2927 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2928 server->caps |= NFS_CAP_MODE;
2929 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2930 server->caps |= NFS_CAP_NLINK;
2931 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2932 server->caps |= NFS_CAP_OWNER;
2933 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2934 server->caps |= NFS_CAP_OWNER_GROUP;
2935 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2936 server->caps |= NFS_CAP_ATIME;
2937 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2938 server->caps |= NFS_CAP_CTIME;
2939 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2940 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002941#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2942 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2943 server->caps |= NFS_CAP_SECURITY_LABEL;
2944#endif
2945 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2946 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002947 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002948
Trond Myklebusta65318b2009-03-11 14:10:28 -04002949 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2950 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2951 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002952 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002954 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return status;
2958}
2959
Trond Myklebust55a97592006-06-09 09:34:19 -04002960int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 struct nfs4_exception exception = { };
2963 int err;
2964 do {
2965 err = nfs4_handle_exception(server,
2966 _nfs4_server_capabilities(server, fhandle),
2967 &exception);
2968 } while (exception.retry);
2969 return err;
2970}
2971
2972static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2973 struct nfs_fsinfo *info)
2974{
David Quigleyaa9c2662013-05-22 12:50:44 -04002975 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002977 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 };
2979 struct nfs4_lookup_res res = {
2980 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002981 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 .fh = fhandle,
2983 };
2984 struct rpc_message msg = {
2985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2986 .rpc_argp = &args,
2987 .rpc_resp = &res,
2988 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002989
David Quigleyaa9c2662013-05-22 12:50:44 -04002990 bitmask[0] = nfs4_fattr_bitmap[0];
2991 bitmask[1] = nfs4_fattr_bitmap[1];
2992 /*
2993 * Process the label in the upcoming getfattr
2994 */
2995 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2996
Trond Myklebust0e574af2005-10-27 22:12:38 -04002997 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002998 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
3001static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3002 struct nfs_fsinfo *info)
3003{
3004 struct nfs4_exception exception = { };
3005 int err;
3006 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003007 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003008 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003009 switch (err) {
3010 case 0:
3011 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003012 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003013 default:
3014 err = nfs4_handle_exception(server, err, &exception);
3015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003017out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return err;
3019}
3020
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003021static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3022 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3023{
Trond Myklebustc2190662013-08-26 19:23:04 -04003024 struct rpc_auth_create_args auth_args = {
3025 .pseudoflavor = flavor,
3026 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003027 struct rpc_auth *auth;
3028 int ret;
3029
Trond Myklebustc2190662013-08-26 19:23:04 -04003030 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003031 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003032 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003033 goto out;
3034 }
3035 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003036out:
3037 return ret;
3038}
3039
Chuck Lever9a744ba2013-03-16 15:56:02 -04003040/*
3041 * Retry pseudoroot lookup with various security flavors. We do this when:
3042 *
3043 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3044 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3045 *
3046 * Returns zero on success, or a negative NFS4ERR value, or a
3047 * negative errno value.
3048 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003049static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003050 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003052 /* Per 3530bis 15.33.5 */
3053 static const rpc_authflavor_t flav_array[] = {
3054 RPC_AUTH_GSS_KRB5P,
3055 RPC_AUTH_GSS_KRB5I,
3056 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003057 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003058 RPC_AUTH_NULL,
3059 };
3060 int status = -EPERM;
3061 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003063 if (server->auth_info.flavor_len > 0) {
3064 /* try each flavor specified by user */
3065 for (i = 0; i < server->auth_info.flavor_len; i++) {
3066 status = nfs4_lookup_root_sec(server, fhandle, info,
3067 server->auth_info.flavors[i]);
3068 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3069 continue;
3070 break;
3071 }
3072 } else {
3073 /* no flavors specified by user, try default list */
3074 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3075 status = nfs4_lookup_root_sec(server, fhandle, info,
3076 flav_array[i]);
3077 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3078 continue;
3079 break;
3080 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003081 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003082
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003083 /*
3084 * -EACCESS could mean that the user doesn't have correct permissions
3085 * to access the mount. It could also mean that we tried to mount
3086 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3087 * existing mount programs don't handle -EACCES very well so it should
3088 * be mapped to -EPERM instead.
3089 */
3090 if (status == -EACCES)
3091 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003092 return status;
3093}
3094
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003095static int nfs4_do_find_root_sec(struct nfs_server *server,
3096 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3097{
3098 int mv = server->nfs_client->cl_minorversion;
3099 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3100}
3101
3102/**
3103 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3104 * @server: initialized nfs_server handle
3105 * @fhandle: we fill in the pseudo-fs root file handle
3106 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003107 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003108 *
3109 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003110 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003111int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003112 struct nfs_fsinfo *info,
3113 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003114{
Andre Przywarac7757072015-04-23 17:17:40 +01003115 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003116
Andre Przywarac7757072015-04-23 17:17:40 +01003117 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003118 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003119
3120 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003121 status = nfs4_do_find_root_sec(server, fhandle, info);
3122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (status == 0)
3124 status = nfs4_server_capabilities(server, fhandle);
3125 if (status == 0)
3126 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003127
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003128 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
Bryan Schumakerbae36242012-05-10 15:07:31 -04003131static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3132 struct nfs_fsinfo *info)
3133{
3134 int error;
3135 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003136 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003137
3138 error = nfs4_server_capabilities(server, mntfh);
3139 if (error < 0) {
3140 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3141 return error;
3142 }
3143
David Quigley14c43f72013-05-22 12:50:43 -04003144 label = nfs4_label_alloc(server, GFP_KERNEL);
3145 if (IS_ERR(label))
3146 return PTR_ERR(label);
3147
David Quigley1775fd32013-05-22 12:50:42 -04003148 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003149 if (error < 0) {
3150 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003151 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003152 }
3153
3154 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3155 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3156 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3157
David Quigley14c43f72013-05-22 12:50:43 -04003158err_free_label:
3159 nfs4_label_free(label);
3160
Bryan Schumakerbae36242012-05-10 15:07:31 -04003161 return error;
3162}
3163
Manoj Naik6b97fd32006-06-09 09:34:29 -04003164/*
3165 * Get locations and (maybe) other attributes of a referral.
3166 * Note that we'll actually follow the referral later when
3167 * we detect fsid mismatch in inode revalidation
3168 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003169static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3170 const struct qstr *name, struct nfs_fattr *fattr,
3171 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003172{
3173 int status = -ENOMEM;
3174 struct page *page = NULL;
3175 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003176
3177 page = alloc_page(GFP_KERNEL);
3178 if (page == NULL)
3179 goto out;
3180 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3181 if (locations == NULL)
3182 goto out;
3183
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003184 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003185 if (status != 0)
3186 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003187
3188 /*
3189 * If the fsid didn't change, this is a migration event, not a
3190 * referral. Cause us to drop into the exception handler, which
3191 * will kick off migration recovery.
3192 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003193 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003194 dprintk("%s: server did not return a different fsid for"
3195 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003196 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003197 goto out;
3198 }
Andy Adamson533eb462011-06-13 18:25:56 -04003199 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3200 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003201
Andy Adamson533eb462011-06-13 18:25:56 -04003202 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003203 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003204 memset(fhandle, 0, sizeof(struct nfs_fh));
3205out:
3206 if (page)
3207 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003208 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003209 return status;
3210}
3211
David Quigley1775fd32013-05-22 12:50:42 -04003212static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3213 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
3215 struct nfs4_getattr_arg args = {
3216 .fh = fhandle,
3217 .bitmask = server->attr_bitmask,
3218 };
3219 struct nfs4_getattr_res res = {
3220 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003221 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 .server = server,
3223 };
3224 struct rpc_message msg = {
3225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3226 .rpc_argp = &args,
3227 .rpc_resp = &res,
3228 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003229
3230 args.bitmask = nfs4_bitmask(server, label);
3231
Trond Myklebust0e574af2005-10-27 22:12:38 -04003232 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003233 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
David Quigley1775fd32013-05-22 12:50:42 -04003236static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3237 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
3239 struct nfs4_exception exception = { };
3240 int err;
3241 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003242 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3243 trace_nfs4_getattr(server, fhandle, fattr, err);
3244 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 &exception);
3246 } while (exception.retry);
3247 return err;
3248}
3249
3250/*
3251 * The file is not closed if it is opened due to the a request to change
3252 * the size of the file. The open call will not be needed once the
3253 * VFS layer lookup-intents are implemented.
3254 *
3255 * Close is called when the inode is destroyed.
3256 * If we haven't opened the file for O_WRONLY, we
3257 * need to in the size_change case to obtain a stateid.
3258 *
3259 * Got race?
3260 * Because OPEN is always done by name in nfsv4, it is
3261 * possible that we opened a different file by the same
3262 * name. We can recognize this race condition, but we
3263 * can't do anything about it besides returning an error.
3264 *
3265 * This will be fixed with VFS changes (lookup-intent).
3266 */
3267static int
3268nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3269 struct iattr *sattr)
3270{
David Howells2b0143b2015-03-17 22:25:59 +00003271 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003272 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003273 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003274 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 int status;
3276
Peng Tao88ac8152014-09-12 11:04:10 +08003277 if (pnfs_ld_layoutret_on_setattr(inode) &&
3278 sattr->ia_valid & ATTR_SIZE &&
3279 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003280 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003281
Trond Myklebust0e574af2005-10-27 22:12:38 -04003282 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Andy Adamson26699402012-05-30 16:12:24 -04003284 /* Deal with open(O_TRUNC) */
3285 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003286 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003287
3288 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003289 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003290 return 0;
3291
Trond Myklebustd5308382005-11-04 15:33:38 -05003292 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003293 if (sattr->ia_valid & ATTR_FILE) {
3294 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003295
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003296 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003297 if (ctx) {
3298 cred = ctx->cred;
3299 state = ctx->state;
3300 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003301 }
3302
David Quigley14c43f72013-05-22 12:50:43 -04003303 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3304 if (IS_ERR(label))
3305 return PTR_ERR(label);
3306
3307 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003308 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003309 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003310 nfs_setsecurity(inode, fattr, label);
3311 }
David Quigley14c43f72013-05-22 12:50:43 -04003312 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return status;
3314}
3315
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003316static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3317 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003318 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003319{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003320 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003321 int status;
3322 struct nfs4_lookup_arg args = {
3323 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003324 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003325 .name = name,
3326 };
3327 struct nfs4_lookup_res res = {
3328 .server = server,
3329 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003330 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003331 .fh = fhandle,
3332 };
3333 struct rpc_message msg = {
3334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3335 .rpc_argp = &args,
3336 .rpc_resp = &res,
3337 };
3338
David Quigleyaa9c2662013-05-22 12:50:44 -04003339 args.bitmask = nfs4_bitmask(server, label);
3340
David Howells2b3de442006-08-22 20:06:09 -04003341 nfs_fattr_init(fattr);
3342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003344 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 dprintk("NFS reply lookup: %d\n", status);
3346 return status;
3347}
3348
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003349static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003350{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003351 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003352 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003353 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3354 fattr->nlink = 2;
3355}
3356
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003357static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3358 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003359 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003360{
3361 struct nfs4_exception exception = { };
3362 struct rpc_clnt *client = *clnt;
3363 int err;
3364 do {
David Quigley1775fd32013-05-22 12:50:42 -04003365 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003366 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003367 switch (err) {
3368 case -NFS4ERR_BADNAME:
3369 err = -ENOENT;
3370 goto out;
3371 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003372 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003373 if (err == -NFS4ERR_MOVED)
3374 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003375 goto out;
3376 case -NFS4ERR_WRONGSEC:
3377 err = -EPERM;
3378 if (client != *clnt)
3379 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003380 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003381 if (IS_ERR(client))
3382 return PTR_ERR(client);
3383
3384 exception.retry = 1;
3385 break;
3386 default:
3387 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3388 }
3389 } while (exception.retry);
3390
3391out:
3392 if (err == 0)
3393 *clnt = client;
3394 else if (client != *clnt)
3395 rpc_shutdown_client(client);
3396
3397 return err;
3398}
3399
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003400static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003401 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3402 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003404 int status;
3405 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003406
David Quigley1775fd32013-05-22 12:50:42 -04003407 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003408 if (client != NFS_CLIENT(dir)) {
3409 rpc_shutdown_client(client);
3410 nfs_fixup_secinfo_attributes(fattr);
3411 }
3412 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003415struct rpc_clnt *
3416nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3417 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3418{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003419 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003420 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003421
David Quigley1775fd32013-05-22 12:50:42 -04003422 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003423 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003424 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003425 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003426}
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3429{
Trond Myklebust76b32992007-08-10 17:45:11 -04003430 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 struct nfs4_accessargs args = {
3432 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003433 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003435 struct nfs4_accessres res = {
3436 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003437 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 struct rpc_message msg = {
3439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3440 .rpc_argp = &args,
3441 .rpc_resp = &res,
3442 .rpc_cred = entry->cred,
3443 };
3444 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003445 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
3447 /*
3448 * Determine which access bits we want to ask for...
3449 */
3450 if (mode & MAY_READ)
3451 args.access |= NFS4_ACCESS_READ;
3452 if (S_ISDIR(inode->i_mode)) {
3453 if (mode & MAY_WRITE)
3454 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3455 if (mode & MAY_EXEC)
3456 args.access |= NFS4_ACCESS_LOOKUP;
3457 } else {
3458 if (mode & MAY_WRITE)
3459 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3460 if (mode & MAY_EXEC)
3461 args.access |= NFS4_ACCESS_EXECUTE;
3462 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003463
3464 res.fattr = nfs_alloc_fattr();
3465 if (res.fattr == NULL)
3466 return -ENOMEM;
3467
Bryan Schumaker7c513052011-03-24 17:12:24 +00003468 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003470 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003471 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003473 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 return status;
3475}
3476
3477static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3478{
3479 struct nfs4_exception exception = { };
3480 int err;
3481 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003482 err = _nfs4_proc_access(inode, entry);
3483 trace_nfs4_access(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
3490/*
3491 * TODO: For the time being, we don't try to get any attributes
3492 * along with any of the zero-copy operations READ, READDIR,
3493 * READLINK, WRITE.
3494 *
3495 * In the case of the first three, we want to put the GETATTR
3496 * after the read-type operation -- this is because it is hard
3497 * to predict the length of a GETATTR response in v4, and thus
3498 * align the READ data correctly. This means that the GETATTR
3499 * may end up partially falling into the page cache, and we should
3500 * shift it into the 'tail' of the xdr_buf before processing.
3501 * To do this efficiently, we need to know the total length
3502 * of data received, which doesn't seem to be available outside
3503 * of the RPC layer.
3504 *
3505 * In the case of WRITE, we also want to put the GETATTR after
3506 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003507 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 *
3509 * Both of these changes to the XDR layer would in fact be quite
3510 * minor, but I decided to leave them for a subsequent patch.
3511 */
3512static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3513 unsigned int pgbase, unsigned int pglen)
3514{
3515 struct nfs4_readlink args = {
3516 .fh = NFS_FH(inode),
3517 .pgbase = pgbase,
3518 .pglen = pglen,
3519 .pages = &page,
3520 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003521 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 struct rpc_message msg = {
3523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3524 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003525 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 };
3527
Bryan Schumaker7c513052011-03-24 17:12:24 +00003528 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 -07003529}
3530
3531static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3532 unsigned int pgbase, unsigned int pglen)
3533{
3534 struct nfs4_exception exception = { };
3535 int err;
3536 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003537 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3538 trace_nfs4_readlink(inode, err);
3539 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 &exception);
3541 } while (exception.retry);
3542 return err;
3543}
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003546 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548static int
3549nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003550 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
David Quigleyaa9c2662013-05-22 12:50:44 -04003552 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003553 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003555 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 int status = 0;
3557
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003558 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3559 if (IS_ERR(ctx))
3560 return PTR_ERR(ctx);
3561
David Quigleyaa9c2662013-05-22 12:50:44 -04003562 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3563
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003564 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003565 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 if (IS_ERR(state)) {
3567 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003568 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003571 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003572 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 return status;
3574}
3575
3576static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3577{
Trond Myklebust16e42952005-10-27 22:12:44 -04003578 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003579 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003581 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003583 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003584 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003585 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3588 .rpc_argp = &args,
3589 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003591 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003592
Bryan Schumaker7c513052011-03-24 17:12:24 +00003593 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003594 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003595 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 return status;
3597}
3598
3599static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3600{
3601 struct nfs4_exception exception = { };
3602 int err;
3603 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003604 err = _nfs4_proc_remove(dir, name);
3605 trace_nfs4_remove(dir, name, err);
3606 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 &exception);
3608 } while (exception.retry);
3609 return err;
3610}
3611
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003612static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003614 struct nfs_server *server = NFS_SERVER(dir);
3615 struct nfs_removeargs *args = msg->rpc_argp;
3616 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003618 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003620 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003621
3622 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623}
3624
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003625static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3626{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003627 nfs4_setup_sequence(NFS_SERVER(data->dir),
3628 &data->args.seq_args,
3629 &data->res.seq_res,
3630 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003633static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003635 struct nfs_unlinkdata *data = task->tk_calldata;
3636 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003637
Trond Myklebust14516c32010-07-31 14:29:06 -04003638 if (!nfs4_sequence_done(task, &res->seq_res))
3639 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003640 if (nfs4_async_handle_error(task, res->server, NULL,
3641 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003642 return 0;
3643 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003644 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645}
3646
Jeff Laytond3d41522010-09-17 17:31:57 -04003647static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3648{
3649 struct nfs_server *server = NFS_SERVER(dir);
3650 struct nfs_renameargs *arg = msg->rpc_argp;
3651 struct nfs_renameres *res = msg->rpc_resp;
3652
3653 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003654 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003655 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003656}
3657
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003658static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3659{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003660 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3661 &data->args.seq_args,
3662 &data->res.seq_res,
3663 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003664}
3665
3666static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3667 struct inode *new_dir)
3668{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003669 struct nfs_renamedata *data = task->tk_calldata;
3670 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003671
3672 if (!nfs4_sequence_done(task, &res->seq_res))
3673 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003674 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003675 return 0;
3676
3677 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003678 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003679 return 1;
3680}
3681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3683{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003684 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 struct nfs4_link_arg arg = {
3686 .fh = NFS_FH(inode),
3687 .dir_fh = NFS_FH(dir),
3688 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003689 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003691 struct nfs4_link_res res = {
3692 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003693 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003694 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 struct rpc_message msg = {
3696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3697 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003698 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003700 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Trond Myklebust136f2622010-04-16 16:22:49 -04003702 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003703 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003704 goto out;
3705
David Quigley14c43f72013-05-22 12:50:43 -04003706 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3707 if (IS_ERR(res.label)) {
3708 status = PTR_ERR(res.label);
3709 goto out;
3710 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003711 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003712
Bryan Schumaker7c513052011-03-24 17:12:24 +00003713 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003714 if (!status) {
3715 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003716 status = nfs_post_op_update_inode(inode, res.fattr);
3717 if (!status)
3718 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003719 }
David Quigley14c43f72013-05-22 12:50:43 -04003720
3721
3722 nfs4_label_free(res.label);
3723
Trond Myklebust136f2622010-04-16 16:22:49 -04003724out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003725 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return status;
3727}
3728
3729static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3730{
3731 struct nfs4_exception exception = { };
3732 int err;
3733 do {
3734 err = nfs4_handle_exception(NFS_SERVER(inode),
3735 _nfs4_proc_link(inode, dir, name),
3736 &exception);
3737 } while (exception.retry);
3738 return err;
3739}
3740
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003741struct nfs4_createdata {
3742 struct rpc_message msg;
3743 struct nfs4_create_arg arg;
3744 struct nfs4_create_res res;
3745 struct nfs_fh fh;
3746 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003747 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003748};
3749
3750static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3751 struct qstr *name, struct iattr *sattr, u32 ftype)
3752{
3753 struct nfs4_createdata *data;
3754
3755 data = kzalloc(sizeof(*data), GFP_KERNEL);
3756 if (data != NULL) {
3757 struct nfs_server *server = NFS_SERVER(dir);
3758
David Quigley14c43f72013-05-22 12:50:43 -04003759 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3760 if (IS_ERR(data->label))
3761 goto out_free;
3762
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003763 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3764 data->msg.rpc_argp = &data->arg;
3765 data->msg.rpc_resp = &data->res;
3766 data->arg.dir_fh = NFS_FH(dir);
3767 data->arg.server = server;
3768 data->arg.name = name;
3769 data->arg.attrs = sattr;
3770 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003771 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003772 data->res.server = server;
3773 data->res.fh = &data->fh;
3774 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003775 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003776 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003777 }
3778 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003779out_free:
3780 kfree(data);
3781 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003782}
3783
3784static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3785{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003786 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003787 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003788 if (status == 0) {
3789 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003790 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003791 }
3792 return status;
3793}
3794
3795static void nfs4_free_createdata(struct nfs4_createdata *data)
3796{
David Quigley14c43f72013-05-22 12:50:43 -04003797 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003798 kfree(data);
3799}
3800
Chuck Lever4f390c12006-08-22 20:06:22 -04003801static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003802 struct page *page, unsigned int len, struct iattr *sattr,
3803 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003805 struct nfs4_createdata *data;
3806 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Chuck Lever94a6d752006-08-22 20:06:23 -04003808 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003809 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003810
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003811 status = -ENOMEM;
3812 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3813 if (data == NULL)
3814 goto out;
3815
3816 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3817 data->arg.u.symlink.pages = &page;
3818 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003819 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003821 status = nfs4_do_create(dir, dentry, data);
3822
3823 nfs4_free_createdata(data);
3824out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 return status;
3826}
3827
Chuck Lever4f390c12006-08-22 20:06:22 -04003828static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003829 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
3831 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003832 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003834
3835 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3836
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003838 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3839 trace_nfs4_symlink(dir, &dentry->d_name, err);
3840 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 &exception);
3842 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003843
3844 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 return err;
3846}
3847
3848static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003849 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003851 struct nfs4_createdata *data;
3852 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003854 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3855 if (data == NULL)
3856 goto out;
3857
David Quigley1775fd32013-05-22 12:50:42 -04003858 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003859 status = nfs4_do_create(dir, dentry, data);
3860
3861 nfs4_free_createdata(data);
3862out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 return status;
3864}
3865
3866static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3867 struct iattr *sattr)
3868{
3869 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003870 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003872
David Quigleyaa9c2662013-05-22 12:50:44 -04003873 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3874
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003875 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003877 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3878 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3879 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 &exception);
3881 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003882 nfs4_label_release_security(label);
3883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 return err;
3885}
3886
3887static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003888 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
David Howells2b0143b2015-03-17 22:25:59 +00003890 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 struct nfs4_readdir_arg args = {
3892 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003893 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 .pgbase = 0,
3895 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00003896 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003897 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 };
3899 struct nfs4_readdir_res res;
3900 struct rpc_message msg = {
3901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3902 .rpc_argp = &args,
3903 .rpc_resp = &res,
3904 .rpc_cred = cred,
3905 };
3906 int status;
3907
Al Viro6de14722013-09-16 10:53:17 -04003908 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3909 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003910 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003911 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003913 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 -05003914 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003915 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003916 status += args.pgbase;
3917 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003918
3919 nfs_invalidate_atime(dir);
3920
Harvey Harrison3110ff82008-05-02 13:42:44 -07003921 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 return status;
3923}
3924
3925static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003926 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
3928 struct nfs4_exception exception = { };
3929 int err;
3930 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003931 err = _nfs4_proc_readdir(dentry, cred, cookie,
3932 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00003933 trace_nfs4_readdir(d_inode(dentry), err);
3934 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 &exception);
3936 } while (exception.retry);
3937 return err;
3938}
3939
3940static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003941 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003943 struct nfs4_createdata *data;
3944 int mode = sattr->ia_mode;
3945 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003947 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3948 if (data == NULL)
3949 goto out;
3950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003952 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003954 data->arg.ftype = NF4BLK;
3955 data->arg.u.device.specdata1 = MAJOR(rdev);
3956 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 }
3958 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003959 data->arg.ftype = NF4CHR;
3960 data->arg.u.device.specdata1 = MAJOR(rdev);
3961 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003962 } else if (!S_ISSOCK(mode)) {
3963 status = -EINVAL;
3964 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
David Quigley1775fd32013-05-22 12:50:42 -04003966
David Quigleyaa9c2662013-05-22 12:50:44 -04003967 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003968 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003969out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003970 nfs4_free_createdata(data);
3971out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 return status;
3973}
3974
3975static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3976 struct iattr *sattr, dev_t rdev)
3977{
3978 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003979 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003981
David Quigleyaa9c2662013-05-22 12:50:44 -04003982 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3983
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003984 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003986 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3987 trace_nfs4_mknod(dir, &dentry->d_name, err);
3988 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 &exception);
3990 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003991
3992 nfs4_label_release_security(label);
3993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 return err;
3995}
3996
3997static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3998 struct nfs_fsstat *fsstat)
3999{
4000 struct nfs4_statfs_arg args = {
4001 .fh = fhandle,
4002 .bitmask = server->attr_bitmask,
4003 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004004 struct nfs4_statfs_res res = {
4005 .fsstat = fsstat,
4006 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 struct rpc_message msg = {
4008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4009 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004010 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 };
4012
Trond Myklebust0e574af2005-10-27 22:12:38 -04004013 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004014 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015}
4016
4017static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4018{
4019 struct nfs4_exception exception = { };
4020 int err;
4021 do {
4022 err = nfs4_handle_exception(server,
4023 _nfs4_proc_statfs(server, fhandle, fsstat),
4024 &exception);
4025 } while (exception.retry);
4026 return err;
4027}
4028
4029static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4030 struct nfs_fsinfo *fsinfo)
4031{
4032 struct nfs4_fsinfo_arg args = {
4033 .fh = fhandle,
4034 .bitmask = server->attr_bitmask,
4035 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004036 struct nfs4_fsinfo_res res = {
4037 .fsinfo = fsinfo,
4038 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 struct rpc_message msg = {
4040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4041 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004042 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 };
4044
Bryan Schumaker7c513052011-03-24 17:12:24 +00004045 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046}
4047
4048static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4049{
4050 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004051 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 int err;
4053
4054 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004055 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004056 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004057 if (err == 0) {
4058 struct nfs_client *clp = server->nfs_client;
4059
4060 spin_lock(&clp->cl_lock);
4061 clp->cl_lease_time = fsinfo->lease_time * HZ;
4062 clp->cl_last_renewal = now;
4063 spin_unlock(&clp->cl_lock);
4064 break;
4065 }
4066 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 } while (exception.retry);
4068 return err;
4069}
4070
4071static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4072{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004073 int error;
4074
Trond Myklebust0e574af2005-10-27 22:12:38 -04004075 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004076 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004077 if (error == 0) {
4078 /* block layout checks this! */
4079 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004080 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004081 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004082
4083 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084}
4085
4086static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4087 struct nfs_pathconf *pathconf)
4088{
4089 struct nfs4_pathconf_arg args = {
4090 .fh = fhandle,
4091 .bitmask = server->attr_bitmask,
4092 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004093 struct nfs4_pathconf_res res = {
4094 .pathconf = pathconf,
4095 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 struct rpc_message msg = {
4097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4098 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004099 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 };
4101
4102 /* None of the pathconf attributes are mandatory to implement */
4103 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4104 memset(pathconf, 0, sizeof(*pathconf));
4105 return 0;
4106 }
4107
Trond Myklebust0e574af2005-10-27 22:12:38 -04004108 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004109 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110}
4111
4112static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4113 struct nfs_pathconf *pathconf)
4114{
4115 struct nfs4_exception exception = { };
4116 int err;
4117
4118 do {
4119 err = nfs4_handle_exception(server,
4120 _nfs4_proc_pathconf(server, fhandle, pathconf),
4121 &exception);
4122 } while (exception.retry);
4123 return err;
4124}
4125
Trond Myklebust5521abf2013-03-16 20:54:34 -04004126int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004127 const struct nfs_open_context *ctx,
4128 const struct nfs_lock_context *l_ctx,
4129 fmode_t fmode)
4130{
4131 const struct nfs_lockowner *lockowner = NULL;
4132
4133 if (l_ctx != NULL)
4134 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004135 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004136}
4137EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4138
Trond Myklebust5521abf2013-03-16 20:54:34 -04004139static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4140 const struct nfs_open_context *ctx,
4141 const struct nfs_lock_context *l_ctx,
4142 fmode_t fmode)
4143{
4144 nfs4_stateid current_stateid;
4145
Trond Myklebuste1253be2014-03-05 08:44:23 -05004146 /* If the current stateid represents a lost lock, then exit */
4147 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4148 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004149 return nfs4_stateid_match(stateid, &current_stateid);
4150}
4151
4152static bool nfs4_error_stateid_expired(int err)
4153{
4154 switch (err) {
4155 case -NFS4ERR_DELEG_REVOKED:
4156 case -NFS4ERR_ADMIN_REVOKED:
4157 case -NFS4ERR_BAD_STATEID:
4158 case -NFS4ERR_STALE_STATEID:
4159 case -NFS4ERR_OLD_STATEID:
4160 case -NFS4ERR_OPENMODE:
4161 case -NFS4ERR_EXPIRED:
4162 return true;
4163 }
4164 return false;
4165}
4166
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004167void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004168{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004169 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004170}
4171
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004172static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004174 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004176 trace_nfs4_read(hdr, task->tk_status);
4177 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004178 hdr->args.context->state,
4179 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004180 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004181 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004183
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004184 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004186 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
Trond Myklebust5521abf2013-03-16 20:54:34 -04004190static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004191 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004192{
4193
4194 if (!nfs4_error_stateid_expired(task->tk_status) ||
4195 nfs4_stateid_is_current(&args->stateid,
4196 args->context,
4197 args->lock_context,
4198 FMODE_READ))
4199 return false;
4200 rpc_restart_call_prepare(task);
4201 return true;
4202}
4203
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004204static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004205{
4206
4207 dprintk("--> %s\n", __func__);
4208
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004209 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004210 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004211 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004212 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004213 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4214 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004215}
4216
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004217static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4218 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004220 hdr->timestamp = jiffies;
4221 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004222 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004223 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224}
4225
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004226static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4227 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004228{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004229 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4230 &hdr->args.seq_args,
4231 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004232 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004233 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004234 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4235 hdr->args.lock_context,
4236 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004237 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004238 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004239 return -EIO;
4240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241}
4242
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004243static int nfs4_write_done_cb(struct rpc_task *task,
4244 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004246 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004247
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004248 trace_nfs4_write(hdr, task->tk_status);
4249 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004250 hdr->args.context->state,
4251 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004252 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004253 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004255 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004256 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004257 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004258 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260}
4261
Trond Myklebust5521abf2013-03-16 20:54:34 -04004262static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004263 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004264{
4265
4266 if (!nfs4_error_stateid_expired(task->tk_status) ||
4267 nfs4_stateid_is_current(&args->stateid,
4268 args->context,
4269 args->lock_context,
4270 FMODE_WRITE))
4271 return false;
4272 rpc_restart_call_prepare(task);
4273 return true;
4274}
4275
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004276static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004277{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004278 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004279 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004280 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004281 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004282 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4283 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004284}
4285
Trond Myklebust5a37f852012-04-28 14:55:16 -04004286static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004287bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004288{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004289 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004290 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004291 return false;
4292 /* Otherwise, request attributes if and only if we don't hold
4293 * a delegation
4294 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004295 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004296}
Fred Isamana69aef12011-03-03 15:13:47 +00004297
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004298static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4299 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004301 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004302
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004303 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4304 hdr->args.bitmask = NULL;
4305 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004306 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004307 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004308
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004309 if (!hdr->pgio_done_cb)
4310 hdr->pgio_done_cb = nfs4_write_done_cb;
4311 hdr->res.server = server;
4312 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004314 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004315 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
Fred Isaman0b7c0152012-04-20 14:47:39 -04004318static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4319{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004320 nfs4_setup_sequence(NFS_SERVER(data->inode),
4321 &data->args.seq_args,
4322 &data->res.seq_res,
4323 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324}
4325
Fred Isaman0b7c0152012-04-20 14:47:39 -04004326static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004329
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004330 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004331 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4332 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004333 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004334 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337}
4338
Fred Isaman0b7c0152012-04-20 14:47:39 -04004339static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004340{
4341 if (!nfs4_sequence_done(task, &data->res.seq_res))
4342 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004343 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004344}
4345
Fred Isaman0b7c0152012-04-20 14:47:39 -04004346static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004348 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004349
Fred Isaman0b7c0152012-04-20 14:47:39 -04004350 if (data->commit_done_cb == NULL)
4351 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004352 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004353 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004354 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355}
4356
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004357struct nfs4_renewdata {
4358 struct nfs_client *client;
4359 unsigned long timestamp;
4360};
4361
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362/*
4363 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4364 * standalone procedure for queueing an asynchronous RENEW.
4365 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004366static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004367{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004368 struct nfs4_renewdata *data = calldata;
4369 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004370
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004371 if (atomic_read(&clp->cl_count) > 1)
4372 nfs4_schedule_state_renewal(clp);
4373 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004374 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004375}
4376
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004377static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004379 struct nfs4_renewdata *data = calldata;
4380 struct nfs_client *clp = data->client;
4381 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004383 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004384 switch (task->tk_status) {
4385 case 0:
4386 break;
4387 case -NFS4ERR_LEASE_MOVED:
4388 nfs4_schedule_lease_moved_recovery(clp);
4389 break;
4390 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004391 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004392 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4393 return;
4394 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004395 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004396 return;
4397 }
4398 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004400 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401}
4402
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004403static const struct rpc_call_ops nfs4_renew_ops = {
4404 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004405 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004406};
4407
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004408static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
4410 struct rpc_message msg = {
4411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4412 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004413 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004415 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004417 if (renew_flags == 0)
4418 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004419 if (!atomic_inc_not_zero(&clp->cl_count))
4420 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004421 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004422 if (data == NULL)
4423 return -ENOMEM;
4424 data->client = clp;
4425 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004426 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004427 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004430static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
4432 struct rpc_message msg = {
4433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4434 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004435 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 };
4437 unsigned long now = jiffies;
4438 int status;
4439
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004440 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 if (status < 0)
4442 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004443 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 return 0;
4445}
4446
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004447static inline int nfs4_server_supports_acls(struct nfs_server *server)
4448{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004449 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004450}
4451
Trond Myklebust21f498c2012-08-24 10:59:25 -04004452/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4453 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004454 * the stack.
4455 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004456#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004457
Neil Hormane9e3d722011-03-04 19:26:03 -05004458static int buf_to_pages_noslab(const void *buf, size_t buflen,
4459 struct page **pages, unsigned int *pgbase)
4460{
4461 struct page *newpage, **spages;
4462 int rc = 0;
4463 size_t len;
4464 spages = pages;
4465
4466 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004467 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004468 newpage = alloc_page(GFP_KERNEL);
4469
4470 if (newpage == NULL)
4471 goto unwind;
4472 memcpy(page_address(newpage), buf, len);
4473 buf += len;
4474 buflen -= len;
4475 *pages++ = newpage;
4476 rc++;
4477 } while (buflen != 0);
4478
4479 return rc;
4480
4481unwind:
4482 for(; rc > 0; rc--)
4483 __free_page(spages[rc-1]);
4484 return -ENOMEM;
4485}
4486
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004487struct nfs4_cached_acl {
4488 int cached;
4489 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004490 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004491};
4492
4493static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004494{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004495 struct nfs_inode *nfsi = NFS_I(inode);
4496
4497 spin_lock(&inode->i_lock);
4498 kfree(nfsi->nfs4_acl);
4499 nfsi->nfs4_acl = acl;
4500 spin_unlock(&inode->i_lock);
4501}
4502
4503static void nfs4_zap_acl_attr(struct inode *inode)
4504{
4505 nfs4_set_cached_acl(inode, NULL);
4506}
4507
4508static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4509{
4510 struct nfs_inode *nfsi = NFS_I(inode);
4511 struct nfs4_cached_acl *acl;
4512 int ret = -ENOENT;
4513
4514 spin_lock(&inode->i_lock);
4515 acl = nfsi->nfs4_acl;
4516 if (acl == NULL)
4517 goto out;
4518 if (buf == NULL) /* user is just asking for length */
4519 goto out_len;
4520 if (acl->cached == 0)
4521 goto out;
4522 ret = -ERANGE; /* see getxattr(2) man page */
4523 if (acl->len > buflen)
4524 goto out;
4525 memcpy(buf, acl->data, acl->len);
4526out_len:
4527 ret = acl->len;
4528out:
4529 spin_unlock(&inode->i_lock);
4530 return ret;
4531}
4532
Sachin Prabhu5794d212012-04-17 14:36:40 +01004533static 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 +00004534{
4535 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004536 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004537
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004538 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004539 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004540 if (acl == NULL)
4541 goto out;
4542 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004543 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004544 } else {
4545 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4546 if (acl == NULL)
4547 goto out;
4548 acl->cached = 0;
4549 }
4550 acl->len = acl_len;
4551out:
4552 nfs4_set_cached_acl(inode, acl);
4553}
4554
Andy Adamsonbf118a32011-12-07 11:55:27 -05004555/*
4556 * The getxattr API returns the required buffer length when called with a
4557 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4558 * the required buf. On a NULL buf, we send a page of data to the server
4559 * guessing that the ACL request can be serviced by a page. If so, we cache
4560 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4561 * the cache. If not so, we throw away the page, and cache the required
4562 * length. The next getxattr call will then produce another round trip to
4563 * the server, this time with the input buf of the required size.
4564 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004565static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004566{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004567 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004568 struct nfs_getaclargs args = {
4569 .fh = NFS_FH(inode),
4570 .acl_pages = pages,
4571 .acl_len = buflen,
4572 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004573 struct nfs_getaclres res = {
4574 .acl_len = buflen,
4575 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004576 struct rpc_message msg = {
4577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4578 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004579 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004580 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004581 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4582 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004583
Andy Adamsonbf118a32011-12-07 11:55:27 -05004584 /* As long as we're doing a round trip to the server anyway,
4585 * let's be prepared for a page of acl data. */
4586 if (npages == 0)
4587 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004588 if (npages > ARRAY_SIZE(pages))
4589 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004590
Andy Adamsonbf118a32011-12-07 11:55:27 -05004591 for (i = 0; i < npages; i++) {
4592 pages[i] = alloc_page(GFP_KERNEL);
4593 if (!pages[i])
4594 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004595 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004596
4597 /* for decoding across pages */
4598 res.acl_scratch = alloc_page(GFP_KERNEL);
4599 if (!res.acl_scratch)
4600 goto out_free;
4601
Andy Adamsonbf118a32011-12-07 11:55:27 -05004602 args.acl_len = npages * PAGE_SIZE;
4603 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004604
Peng Taode040be2012-01-10 22:42:47 +08004605 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004606 __func__, buf, buflen, npages, args.acl_len);
4607 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4608 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004609 if (ret)
4610 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004611
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004612 /* Handle the case where the passed-in buffer is too short */
4613 if (res.acl_flags & NFS4_ACL_TRUNC) {
4614 /* Did the user only issue a request for the acl length? */
4615 if (buf == NULL)
4616 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004617 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004618 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004619 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004620 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004621 if (buf) {
4622 if (res.acl_len > buflen) {
4623 ret = -ERANGE;
4624 goto out_free;
4625 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004626 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004627 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004628out_ok:
4629 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004630out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004631 for (i = 0; i < npages; i++)
4632 if (pages[i])
4633 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004634 if (res.acl_scratch)
4635 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004636 return ret;
4637}
4638
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004639static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4640{
4641 struct nfs4_exception exception = { };
4642 ssize_t ret;
4643 do {
4644 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004645 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004646 if (ret >= 0)
4647 break;
4648 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4649 } while (exception.retry);
4650 return ret;
4651}
4652
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004653static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4654{
4655 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004656 int ret;
4657
4658 if (!nfs4_server_supports_acls(server))
4659 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004660 ret = nfs_revalidate_inode(server, inode);
4661 if (ret < 0)
4662 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004663 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4664 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004665 ret = nfs4_read_cached_acl(inode, buf, buflen);
4666 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004667 /* -ENOENT is returned if there is no ACL or if there is an ACL
4668 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004669 return ret;
4670 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004671}
4672
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004673static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004674{
4675 struct nfs_server *server = NFS_SERVER(inode);
4676 struct page *pages[NFS4ACL_MAXPAGES];
4677 struct nfs_setaclargs arg = {
4678 .fh = NFS_FH(inode),
4679 .acl_pages = pages,
4680 .acl_len = buflen,
4681 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004682 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004683 struct rpc_message msg = {
4684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4685 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004686 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004687 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004688 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004689 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004690
4691 if (!nfs4_server_supports_acls(server))
4692 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004693 if (npages > ARRAY_SIZE(pages))
4694 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004695 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4696 if (i < 0)
4697 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004698 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004699 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004700
4701 /*
4702 * Free each page after tx, so the only ref left is
4703 * held by the network stack
4704 */
4705 for (; i > 0; i--)
4706 put_page(pages[i-1]);
4707
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004708 /*
4709 * Acl update can result in inode attribute update.
4710 * so mark the attribute cache invalid.
4711 */
4712 spin_lock(&inode->i_lock);
4713 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4714 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004715 nfs_access_zap_cache(inode);
4716 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004717 return ret;
4718}
4719
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004720static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4721{
4722 struct nfs4_exception exception = { };
4723 int err;
4724 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004725 err = __nfs4_proc_set_acl(inode, buf, buflen);
4726 trace_nfs4_set_acl(inode, err);
4727 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004728 &exception);
4729 } while (exception.retry);
4730 return err;
4731}
4732
David Quigleyaa9c2662013-05-22 12:50:44 -04004733#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4734static int _nfs4_get_security_label(struct inode *inode, void *buf,
4735 size_t buflen)
4736{
4737 struct nfs_server *server = NFS_SERVER(inode);
4738 struct nfs_fattr fattr;
4739 struct nfs4_label label = {0, 0, buflen, buf};
4740
4741 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004742 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004743 .fh = NFS_FH(inode),
4744 .bitmask = bitmask,
4745 };
4746 struct nfs4_getattr_res res = {
4747 .fattr = &fattr,
4748 .label = &label,
4749 .server = server,
4750 };
4751 struct rpc_message msg = {
4752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004753 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004754 .rpc_resp = &res,
4755 };
4756 int ret;
4757
4758 nfs_fattr_init(&fattr);
4759
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004760 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004761 if (ret)
4762 return ret;
4763 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4764 return -ENOENT;
4765 if (buflen < label.len)
4766 return -ERANGE;
4767 return 0;
4768}
4769
4770static int nfs4_get_security_label(struct inode *inode, void *buf,
4771 size_t buflen)
4772{
4773 struct nfs4_exception exception = { };
4774 int err;
4775
4776 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4777 return -EOPNOTSUPP;
4778
4779 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004780 err = _nfs4_get_security_label(inode, buf, buflen);
4781 trace_nfs4_get_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 _nfs4_do_set_security_label(struct inode *inode,
4789 struct nfs4_label *ilabel,
4790 struct nfs_fattr *fattr,
4791 struct nfs4_label *olabel)
4792{
4793
4794 struct iattr sattr = {0};
4795 struct nfs_server *server = NFS_SERVER(inode);
4796 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004797 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004798 .fh = NFS_FH(inode),
4799 .iap = &sattr,
4800 .server = server,
4801 .bitmask = bitmask,
4802 .label = ilabel,
4803 };
4804 struct nfs_setattrres res = {
4805 .fattr = fattr,
4806 .label = olabel,
4807 .server = server,
4808 };
4809 struct rpc_message msg = {
4810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004811 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004812 .rpc_resp = &res,
4813 };
4814 int status;
4815
Jeff Layton12207f62013-11-01 10:49:32 -04004816 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004817
Jeff Layton12207f62013-11-01 10:49:32 -04004818 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004819 if (status)
4820 dprintk("%s failed: %d\n", __func__, status);
4821
4822 return status;
4823}
4824
4825static int nfs4_do_set_security_label(struct inode *inode,
4826 struct nfs4_label *ilabel,
4827 struct nfs_fattr *fattr,
4828 struct nfs4_label *olabel)
4829{
4830 struct nfs4_exception exception = { };
4831 int err;
4832
4833 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004834 err = _nfs4_do_set_security_label(inode, ilabel,
4835 fattr, olabel);
4836 trace_nfs4_set_security_label(inode, err);
4837 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004838 &exception);
4839 } while (exception.retry);
4840 return err;
4841}
4842
4843static int
4844nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4845{
4846 struct nfs4_label ilabel, *olabel = NULL;
4847 struct nfs_fattr fattr;
4848 struct rpc_cred *cred;
David Howells2b0143b2015-03-17 22:25:59 +00004849 struct inode *inode = d_inode(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004850 int status;
4851
4852 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4853 return -EOPNOTSUPP;
4854
4855 nfs_fattr_init(&fattr);
4856
4857 ilabel.pi = 0;
4858 ilabel.lfs = 0;
4859 ilabel.label = (char *)buf;
4860 ilabel.len = buflen;
4861
4862 cred = rpc_lookup_cred();
4863 if (IS_ERR(cred))
4864 return PTR_ERR(cred);
4865
4866 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4867 if (IS_ERR(olabel)) {
4868 status = -PTR_ERR(olabel);
4869 goto out;
4870 }
4871
4872 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4873 if (status == 0)
4874 nfs_setsecurity(inode, &fattr, olabel);
4875
4876 nfs4_label_free(olabel);
4877out:
4878 put_rpccred(cred);
4879 return status;
4880}
4881#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4882
4883
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004885nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4886 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004888 struct nfs_client *clp = server->nfs_client;
4889
4890 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 return 0;
4892 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004893 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004894 case -NFS4ERR_ADMIN_REVOKED:
4895 case -NFS4ERR_BAD_STATEID:
4896 case -NFS4ERR_OPENMODE:
4897 if (state == NULL)
4898 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004899 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004900 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004901 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004902 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004903 if (state != NULL) {
4904 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004905 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004908 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004909 nfs4_schedule_lease_recovery(clp);
4910 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004911 case -NFS4ERR_MOVED:
4912 if (nfs4_schedule_migration_recovery(server) < 0)
4913 goto recovery_failed;
4914 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004915 case -NFS4ERR_LEASE_MOVED:
4916 nfs4_schedule_lease_moved_recovery(clp);
4917 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004918#if defined(CONFIG_NFS_V4_1)
4919 case -NFS4ERR_BADSESSION:
4920 case -NFS4ERR_BADSLOT:
4921 case -NFS4ERR_BAD_HIGH_SLOT:
4922 case -NFS4ERR_DEADSESSION:
4923 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4924 case -NFS4ERR_SEQ_FALSE_RETRY:
4925 case -NFS4ERR_SEQ_MISORDERED:
4926 dprintk("%s ERROR %d, Reset session\n", __func__,
4927 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004928 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004929 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004930#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004932 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10004933 rpc_delay(task, nfs4_update_delay(timeout));
4934 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05004935 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004937 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004939 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 }
4941 task->tk_status = nfs4_map_errors(task->tk_status);
4942 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004943recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004944 task->tk_status = -EIO;
4945 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004946wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004947 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004948 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4949 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004950 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4951 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004952restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004953 task->tk_status = 0;
4954 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955}
4956
Chuck Leverf0920752012-05-21 22:45:41 -04004957static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4958 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004959{
4960 __be32 verf[2];
4961
Chuck Lever2c820d92012-05-21 22:45:33 -04004962 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4963 /* An impossible timestamp guarantees this value
4964 * will never match a generated boot time. */
4965 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004966 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004967 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004968 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004969 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4970 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004971 }
Chuck Levercd937102012-03-02 17:14:31 -05004972 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4973}
4974
Jeff Laytona3192682015-06-09 19:43:59 -04004975static int
4976nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04004977{
Jeff Laytona3192682015-06-09 19:43:59 -04004978 int result;
4979 size_t len;
4980 char *str;
4981 bool retried = false;
Chuck Levere984a552012-09-14 17:24:21 -04004982
Trond Myklebustceb3a162015-01-03 15:16:04 -05004983 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04004984 return 0;
4985retry:
4986 rcu_read_lock();
4987 len = 10 + strlen(clp->cl_ipaddr) + 1 +
4988 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
4989 1 +
4990 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
4991 1;
4992 rcu_read_unlock();
4993
4994 if (len > NFS4_OPAQUE_LIMIT + 1)
4995 return -EINVAL;
4996
4997 /*
4998 * Since this string is allocated at mount time, and held until the
4999 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5000 * about a memory-reclaim deadlock.
5001 */
5002 str = kmalloc(len, GFP_KERNEL);
5003 if (!str)
5004 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005005
Chuck Levere984a552012-09-14 17:24:21 -04005006 rcu_read_lock();
Jeff Laytona3192682015-06-09 19:43:59 -04005007 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5008 clp->cl_ipaddr,
5009 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5010 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005011 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005012
5013 /* Did something change? */
5014 if (result >= len) {
5015 kfree(str);
5016 if (retried)
5017 return -EINVAL;
5018 retried = true;
5019 goto retry;
5020 }
5021 clp->cl_owner_id = str;
5022 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005023}
5024
Jeff Layton873e3852015-06-09 19:44:00 -04005025static int
5026nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005027{
Jeff Layton873e3852015-06-09 19:44:00 -04005028 int result;
5029 size_t len;
5030 char *str;
5031
5032 len = 10 + 10 + 1 + 10 + 1 +
5033 strlen(nfs4_client_id_uniquifier) + 1 +
5034 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5035
5036 if (len > NFS4_OPAQUE_LIMIT + 1)
5037 return -EINVAL;
5038
5039 /*
5040 * Since this string is allocated at mount time, and held until the
5041 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5042 * about a memory-reclaim deadlock.
5043 */
5044 str = kmalloc(len, GFP_KERNEL);
5045 if (!str)
5046 return -ENOMEM;
5047
5048 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5049 clp->rpc_ops->version, clp->cl_minorversion,
5050 nfs4_client_id_uniquifier,
5051 clp->cl_rpcclient->cl_nodename);
5052 if (result >= len) {
5053 kfree(str);
5054 return -EINVAL;
5055 }
5056 clp->cl_owner_id = str;
5057 return 0;
5058}
5059
5060static int
5061nfs4_init_uniform_client_string(struct nfs_client *clp)
5062{
5063 int result;
5064 size_t len;
5065 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005066
5067 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005068 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005069
5070 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005071 return nfs4_init_uniquifier_client_string(clp);
5072
5073 len = 10 + 10 + 1 + 10 + 1 +
5074 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5075
5076 if (len > NFS4_OPAQUE_LIMIT + 1)
5077 return -EINVAL;
5078
5079 /*
5080 * Since this string is allocated at mount time, and held until the
5081 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5082 * about a memory-reclaim deadlock.
5083 */
5084 str = kmalloc(len, GFP_KERNEL);
5085 if (!str)
5086 return -ENOMEM;
5087
5088 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5089 clp->rpc_ops->version, clp->cl_minorversion,
5090 clp->cl_rpcclient->cl_nodename);
5091 if (result >= len) {
5092 kfree(str);
5093 return -EINVAL;
5094 }
5095 clp->cl_owner_id = str;
5096 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005097}
5098
Chuck Lever706cb8d2014-03-12 12:51:47 -04005099/*
5100 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5101 * services. Advertise one based on the address family of the
5102 * clientaddr.
5103 */
5104static unsigned int
5105nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5106{
5107 if (strchr(clp->cl_ipaddr, ':') != NULL)
5108 return scnprintf(buf, len, "tcp6");
5109 else
5110 return scnprintf(buf, len, "tcp");
5111}
5112
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005113static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5114{
5115 struct nfs4_setclientid *sc = calldata;
5116
5117 if (task->tk_status == 0)
5118 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5119}
5120
5121static const struct rpc_call_ops nfs4_setclientid_ops = {
5122 .rpc_call_done = nfs4_setclientid_done,
5123};
5124
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005125/**
5126 * nfs4_proc_setclientid - Negotiate client ID
5127 * @clp: state data structure
5128 * @program: RPC program for NFSv4 callback service
5129 * @port: IP port number for NFS4 callback service
5130 * @cred: RPC credential to use for this call
5131 * @res: where to place the result
5132 *
5133 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5134 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005135int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5136 unsigned short port, struct rpc_cred *cred,
5137 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138{
5139 nfs4_verifier sc_verifier;
5140 struct nfs4_setclientid setclientid = {
5141 .sc_verifier = &sc_verifier,
5142 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005143 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 };
5145 struct rpc_message msg = {
5146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5147 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005148 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005149 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005151 struct rpc_task *task;
5152 struct rpc_task_setup task_setup_data = {
5153 .rpc_client = clp->cl_rpcclient,
5154 .rpc_message = &msg,
5155 .callback_ops = &nfs4_setclientid_ops,
5156 .callback_data = &setclientid,
5157 .flags = RPC_TASK_TIMEOUT,
5158 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005159 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
Chuck Leverde734832012-07-11 16:30:50 -04005161 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005162 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005163
5164 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5165 status = nfs4_init_uniform_client_string(clp);
5166 else
Jeff Laytona3192682015-06-09 19:43:59 -04005167 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005168
5169 if (status)
5170 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005171
Chuck Leverde734832012-07-11 16:30:50 -04005172 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005173 setclientid.sc_netid_len =
5174 nfs4_init_callback_netid(clp,
5175 setclientid.sc_netid,
5176 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005177 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005178 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 clp->cl_ipaddr, port >> 8, port & 255);
5180
Jeff Layton3a6bb732015-06-09 19:43:57 -04005181 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005182 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005183 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005184 task = rpc_run_task(&task_setup_data);
5185 if (IS_ERR(task)) {
5186 status = PTR_ERR(task);
5187 goto out;
5188 }
5189 status = task->tk_status;
5190 if (setclientid.sc_cred) {
5191 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5192 put_rpccred(setclientid.sc_cred);
5193 }
5194 rpc_put_task(task);
5195out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005196 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005197 dprintk("NFS reply setclientid: %d\n", status);
5198 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199}
5200
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005201/**
5202 * nfs4_proc_setclientid_confirm - Confirm client ID
5203 * @clp: state data structure
5204 * @res: result of a previous SETCLIENTID
5205 * @cred: RPC credential to use for this call
5206 *
5207 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5208 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005209int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005210 struct nfs4_setclientid_res *arg,
5211 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 struct rpc_message msg = {
5214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005215 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005216 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 int status;
5219
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005220 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5221 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5222 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005223 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005224 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005225 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 return status;
5227}
5228
Trond Myklebustfe650402006-01-03 09:55:18 +01005229struct nfs4_delegreturndata {
5230 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005231 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005232 struct nfs_fh fh;
5233 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005234 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005235 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005236 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005237 struct inode *inode;
5238 bool roc;
5239 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005240};
5241
Trond Myklebustfe650402006-01-03 09:55:18 +01005242static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5243{
5244 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005245
Trond Myklebust14516c32010-07-31 14:29:06 -04005246 if (!nfs4_sequence_done(task, &data->res.seq_res))
5247 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005248
Trond Myklebustca8acf82013-08-13 10:36:56 -04005249 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005250 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005251 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005252 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005253 case -NFS4ERR_ADMIN_REVOKED:
5254 case -NFS4ERR_DELEG_REVOKED:
5255 case -NFS4ERR_BAD_STATEID:
5256 case -NFS4ERR_OLD_STATEID:
5257 case -NFS4ERR_STALE_STATEID:
5258 case -NFS4ERR_EXPIRED:
5259 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005260 if (data->roc)
5261 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005262 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005263 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005264 if (nfs4_async_handle_error(task, data->res.server,
5265 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005266 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005267 return;
5268 }
5269 }
5270 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005271}
5272
5273static void nfs4_delegreturn_release(void *calldata)
5274{
Peng Tao039b7562014-07-03 13:05:02 +08005275 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005276 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005277
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005278 if (inode) {
5279 if (data->roc)
5280 pnfs_roc_release(inode);
5281 nfs_iput_and_deactive(inode);
5282 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005283 kfree(calldata);
5284}
5285
Andy Adamson938e1012009-04-01 09:22:28 -04005286static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5287{
5288 struct nfs4_delegreturndata *d_data;
5289
5290 d_data = (struct nfs4_delegreturndata *)data;
5291
Peng Tao039b7562014-07-03 13:05:02 +08005292 if (d_data->roc &&
5293 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5294 return;
5295
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005296 nfs4_setup_sequence(d_data->res.server,
5297 &d_data->args.seq_args,
5298 &d_data->res.seq_res,
5299 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005300}
Andy Adamson938e1012009-04-01 09:22:28 -04005301
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005302static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005303 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005304 .rpc_call_done = nfs4_delegreturn_done,
5305 .rpc_release = nfs4_delegreturn_release,
5306};
5307
Trond Myklebuste6f81072008-01-24 18:14:34 -05005308static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005309{
5310 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005311 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005312 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005313 struct rpc_message msg = {
5314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5315 .rpc_cred = cred,
5316 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005317 struct rpc_task_setup task_setup_data = {
5318 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005319 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005320 .callback_ops = &nfs4_delegreturn_ops,
5321 .flags = RPC_TASK_ASYNC,
5322 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005323 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005324
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005325 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005326 if (data == NULL)
5327 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005328 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005329 data->args.fhandle = &data->fh;
5330 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005331 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005332 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005333 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005334 data->res.fattr = &data->fattr;
5335 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005336 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005337 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005338 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005339 data->inode = nfs_igrab_and_active(inode);
5340 if (data->inode)
5341 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005342
Trond Myklebustc970aa82007-07-14 15:39:59 -04005343 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005344 msg.rpc_argp = &data->args;
5345 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005346 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005347 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005348 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005349 if (!issync)
5350 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005351 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005352 if (status != 0)
5353 goto out;
5354 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005355 if (status == 0)
5356 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5357 else
5358 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005359out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005360 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005361 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362}
5363
Trond Myklebuste6f81072008-01-24 18:14:34 -05005364int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365{
5366 struct nfs_server *server = NFS_SERVER(inode);
5367 struct nfs4_exception exception = { };
5368 int err;
5369 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005370 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005371 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 switch (err) {
5373 case -NFS4ERR_STALE_STATEID:
5374 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 case 0:
5376 return 0;
5377 }
5378 err = nfs4_handle_exception(server, err, &exception);
5379 } while (exception.retry);
5380 return err;
5381}
5382
5383#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5384#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5385
5386/*
5387 * sleep, with exponential backoff, and retry the LOCK operation.
5388 */
5389static unsigned long
5390nfs4_set_lock_task_retry(unsigned long timeout)
5391{
Colin Cross416ad3c2013-05-06 23:50:06 +00005392 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 timeout <<= 1;
5394 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5395 return NFS4_LOCK_MAXTIMEOUT;
5396 return timeout;
5397}
5398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5400{
5401 struct inode *inode = state->inode;
5402 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005403 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005404 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005406 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005408 struct nfs_lockt_res res = {
5409 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 };
5411 struct rpc_message msg = {
5412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5413 .rpc_argp = &arg,
5414 .rpc_resp = &res,
5415 .rpc_cred = state->owner->so_cred,
5416 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 struct nfs4_lock_state *lsp;
5418 int status;
5419
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005420 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005421 status = nfs4_set_lock_state(state, request);
5422 if (status != 0)
5423 goto out;
5424 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005425 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005426 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005427 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005428 switch (status) {
5429 case 0:
5430 request->fl_type = F_UNLCK;
5431 break;
5432 case -NFS4ERR_DENIED:
5433 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005435 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005436 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005437out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 return status;
5439}
5440
5441static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5442{
5443 struct nfs4_exception exception = { };
5444 int err;
5445
5446 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005447 err = _nfs4_proc_getlk(state, cmd, request);
5448 trace_nfs4_get_lock(request, state, cmd, err);
5449 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 &exception);
5451 } while (exception.retry);
5452 return err;
5453}
5454
Jeff Layton83bfff22015-07-11 06:43:03 -04005455static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456{
5457 int res = 0;
5458 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5459 case FL_POSIX:
Jeff Layton83bfff22015-07-11 06:43:03 -04005460 res = posix_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 break;
5462 case FL_FLOCK:
Jeff Layton83bfff22015-07-11 06:43:03 -04005463 res = flock_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 break;
5465 default:
5466 BUG();
5467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 return res;
5469}
5470
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005471struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005472 struct nfs_locku_args arg;
5473 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005474 struct nfs4_lock_state *lsp;
5475 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005476 struct file_lock fl;
5477 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005478 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005479};
5480
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005481static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5482 struct nfs_open_context *ctx,
5483 struct nfs4_lock_state *lsp,
5484 struct nfs_seqid *seqid)
5485{
5486 struct nfs4_unlockdata *p;
5487 struct inode *inode = lsp->ls_state->inode;
5488
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005489 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005490 if (p == NULL)
5491 return NULL;
5492 p->arg.fh = NFS_FH(inode);
5493 p->arg.fl = &p->fl;
5494 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005495 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005496 p->lsp = lsp;
5497 atomic_inc(&lsp->ls_count);
5498 /* Ensure we don't close file until we're done freeing locks! */
5499 p->ctx = get_nfs_open_context(ctx);
5500 memcpy(&p->fl, fl, sizeof(p->fl));
5501 p->server = NFS_SERVER(inode);
5502 return p;
5503}
5504
Trond Myklebust06f814a2006-01-03 09:55:07 +01005505static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005506{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005507 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005508 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005509 nfs4_put_lock_state(calldata->lsp);
5510 put_nfs_open_context(calldata->ctx);
5511 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005512}
5513
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005514static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005515{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005516 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005517
Trond Myklebust14516c32010-07-31 14:29:06 -04005518 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5519 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005520 switch (task->tk_status) {
5521 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005522 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005523 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005524 if (nfs4_update_lock_stateid(calldata->lsp,
5525 &calldata->res.stateid))
5526 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005527 case -NFS4ERR_BAD_STATEID:
5528 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005529 case -NFS4ERR_STALE_STATEID:
5530 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005531 if (!nfs4_stateid_match(&calldata->arg.stateid,
5532 &calldata->lsp->ls_stateid))
5533 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005534 break;
5535 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005536 if (nfs4_async_handle_error(task, calldata->server,
5537 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005538 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005539 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005540 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005541}
5542
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005543static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005544{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005545 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005547 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005548 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005549 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005550 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005551 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005552 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005553 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005554 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005555 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005556 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005557 &calldata->res.seq_res,
5558 task) != 0)
5559 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005560 return;
5561out_no_action:
5562 task->tk_action = NULL;
5563out_wait:
5564 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565}
5566
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005567static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005568 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005569 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005570 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005571};
5572
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005573static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5574 struct nfs_open_context *ctx,
5575 struct nfs4_lock_state *lsp,
5576 struct nfs_seqid *seqid)
5577{
5578 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005579 struct rpc_message msg = {
5580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5581 .rpc_cred = ctx->cred,
5582 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005583 struct rpc_task_setup task_setup_data = {
5584 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005585 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005586 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005587 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005588 .flags = RPC_TASK_ASYNC,
5589 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005590
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005591 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5592 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5593
Frank Filz137d6ac2007-07-09 15:32:29 -07005594 /* Ensure this is an unlock - when canceling a lock, the
5595 * canceled lock is passed in, and it won't be an unlock.
5596 */
5597 fl->fl_type = F_UNLCK;
5598
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005599 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5600 if (data == NULL) {
5601 nfs_free_seqid(seqid);
5602 return ERR_PTR(-ENOMEM);
5603 }
5604
Chuck Levera9c92d62013-08-09 12:48:18 -04005605 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005606 msg.rpc_argp = &data->arg;
5607 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005608 task_setup_data.callback_data = data;
5609 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005610}
5611
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5613{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005614 struct inode *inode = state->inode;
5615 struct nfs4_state_owner *sp = state->owner;
5616 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005617 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005618 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005619 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005620 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005621 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005622 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Trond Myklebust9b073572006-06-29 16:38:34 -04005624 status = nfs4_set_lock_state(state, request);
5625 /* Unlock _before_ we do the RPC call */
5626 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005627 /* Exclude nfs_delegation_claim_locks() */
5628 mutex_lock(&sp->so_delegreturn_mutex);
5629 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005630 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005631 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005632 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005633 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005634 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005635 }
5636 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005637 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005638 if (status != 0)
5639 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005640 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005641 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005642 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5643 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005644 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5645 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005646 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005647 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005648 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005649 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005650 status = PTR_ERR(task);
5651 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005652 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005653 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005654 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005655out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005656 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005657 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005658 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659}
5660
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005661struct nfs4_lockdata {
5662 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005663 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005664 struct nfs4_lock_state *lsp;
5665 struct nfs_open_context *ctx;
5666 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005667 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005668 int rpc_status;
5669 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005670 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005671};
5672
5673static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005674 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5675 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005676{
5677 struct nfs4_lockdata *p;
5678 struct inode *inode = lsp->ls_state->inode;
5679 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005680 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005681
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005682 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005683 if (p == NULL)
5684 return NULL;
5685
5686 p->arg.fh = NFS_FH(inode);
5687 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005688 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005689 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005690 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005691 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5692 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005693 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005694 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005695 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005696 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005697 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005698 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005699 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005700 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005701 atomic_inc(&lsp->ls_count);
5702 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005703 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005704 memcpy(&p->fl, fl, sizeof(p->fl));
5705 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005706out_free_seqid:
5707 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005708out_free:
5709 kfree(p);
5710 return NULL;
5711}
5712
5713static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5714{
5715 struct nfs4_lockdata *data = calldata;
5716 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
Harvey Harrison3110ff82008-05-02 13:42:44 -07005718 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005719 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005720 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005721 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005722 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005723 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005724 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005725 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005726 nfs4_stateid_copy(&data->arg.open_stateid,
5727 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005728 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005729 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005730 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005731 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005732 nfs4_stateid_copy(&data->arg.lock_stateid,
5733 &data->lsp->ls_stateid);
5734 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005735 if (!nfs4_valid_open_stateid(state)) {
5736 data->rpc_status = -EBADF;
5737 task->tk_action = NULL;
5738 goto out_release_open_seqid;
5739 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005740 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005741 if (nfs4_setup_sequence(data->server,
5742 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005743 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005744 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005745 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005746out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005747 nfs_release_seqid(data->arg.open_seqid);
5748out_release_lock_seqid:
5749 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005750out_wait:
5751 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005752 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005753}
5754
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005755static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5756{
5757 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005758 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005759
Harvey Harrison3110ff82008-05-02 13:42:44 -07005760 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005761
Trond Myklebust14516c32010-07-31 14:29:06 -04005762 if (!nfs4_sequence_done(task, &data->res.seq_res))
5763 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005764
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005765 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005766 switch (task->tk_status) {
5767 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005768 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005769 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005770 if (data->arg.new_lock) {
5771 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005772 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005773 rpc_restart_call_prepare(task);
5774 break;
5775 }
5776 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005777 if (data->arg.new_lock_owner != 0) {
5778 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5779 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5780 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5781 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5782 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005783 break;
5784 case -NFS4ERR_BAD_STATEID:
5785 case -NFS4ERR_OLD_STATEID:
5786 case -NFS4ERR_STALE_STATEID:
5787 case -NFS4ERR_EXPIRED:
5788 if (data->arg.new_lock_owner != 0) {
5789 if (!nfs4_stateid_match(&data->arg.open_stateid,
5790 &lsp->ls_state->open_stateid))
5791 rpc_restart_call_prepare(task);
5792 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5793 &lsp->ls_stateid))
5794 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005795 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005796 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005797}
5798
5799static void nfs4_lock_release(void *calldata)
5800{
5801 struct nfs4_lockdata *data = calldata;
5802
Harvey Harrison3110ff82008-05-02 13:42:44 -07005803 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005804 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005805 if (data->cancelled != 0) {
5806 struct rpc_task *task;
5807 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5808 data->arg.lock_seqid);
5809 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005810 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005811 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005812 } else
5813 nfs_free_seqid(data->arg.lock_seqid);
5814 nfs4_put_lock_state(data->lsp);
5815 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005816 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005817 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005818 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005819}
5820
5821static const struct rpc_call_ops nfs4_lock_ops = {
5822 .rpc_call_prepare = nfs4_lock_prepare,
5823 .rpc_call_done = nfs4_lock_done,
5824 .rpc_release = nfs4_lock_release,
5825};
5826
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005827static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5828{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005829 switch (error) {
5830 case -NFS4ERR_ADMIN_REVOKED:
5831 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005832 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005833 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005834 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005835 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005836 break;
5837 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005838 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005839 case -NFS4ERR_EXPIRED:
5840 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005841 };
5842}
5843
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005844static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005845{
5846 struct nfs4_lockdata *data;
5847 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005848 struct rpc_message msg = {
5849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5850 .rpc_cred = state->owner->so_cred,
5851 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005852 struct rpc_task_setup task_setup_data = {
5853 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005854 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005855 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005856 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005857 .flags = RPC_TASK_ASYNC,
5858 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005859 int ret;
5860
Harvey Harrison3110ff82008-05-02 13:42:44 -07005861 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005862 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005863 fl->fl_u.nfs4_fl.owner,
5864 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005865 if (data == NULL)
5866 return -ENOMEM;
5867 if (IS_SETLKW(cmd))
5868 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005869 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005870 msg.rpc_argp = &data->arg;
5871 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005872 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005873 if (recovery_type > NFS_LOCK_NEW) {
5874 if (recovery_type == NFS_LOCK_RECLAIM)
5875 data->arg.reclaim = NFS_LOCK_RECLAIM;
5876 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005877 } else
5878 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005879 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005880 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005881 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005882 ret = nfs4_wait_for_completion_rpc_task(task);
5883 if (ret == 0) {
5884 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005885 if (ret)
5886 nfs4_handle_setlk_error(data->server, data->lsp,
5887 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005888 } else
5889 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005890 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005891 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893}
5894
5895static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5896{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005897 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005898 struct nfs4_exception exception = {
5899 .inode = state->inode,
5900 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005901 int err;
5902
5903 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005904 /* Cache the lock if possible... */
5905 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5906 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005907 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005908 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005909 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005910 break;
5911 nfs4_handle_exception(server, err, &exception);
5912 } while (exception.retry);
5913 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914}
5915
5916static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5917{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005918 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005919 struct nfs4_exception exception = {
5920 .inode = state->inode,
5921 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005922 int err;
5923
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005924 err = nfs4_set_lock_state(state, request);
5925 if (err != 0)
5926 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005927 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005928 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5929 return 0;
5930 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005931 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005932 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5933 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005934 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005935 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005936 switch (err) {
5937 default:
5938 goto out;
5939 case -NFS4ERR_GRACE:
5940 case -NFS4ERR_DELAY:
5941 nfs4_handle_exception(server, err, &exception);
5942 err = 0;
5943 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005944 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005945out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947}
5948
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005949#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005950/**
5951 * nfs41_check_expired_locks - possibly free a lock stateid
5952 *
5953 * @state: NFSv4 state for an inode
5954 *
5955 * Returns NFS_OK if recovery for this stateid is now finished.
5956 * Otherwise a negative NFS4ERR value is returned.
5957 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005958static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005959{
Chuck Levereb64cf92012-07-11 16:30:05 -04005960 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005961 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005962 struct nfs_server *server = NFS_SERVER(state->inode);
5963
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005964 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005965 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005966 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5967
5968 status = nfs41_test_stateid(server,
5969 &lsp->ls_stateid,
5970 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005971 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005972 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005973 /* Free the stateid unless the server
5974 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005975 if (status != -NFS4ERR_BAD_STATEID)
5976 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005977 &lsp->ls_stateid,
5978 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005979 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005980 ret = status;
5981 }
5982 }
5983 };
5984
5985 return ret;
5986}
5987
5988static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5989{
5990 int status = NFS_OK;
5991
5992 if (test_bit(LK_STATE_IN_USE, &state->flags))
5993 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005994 if (status != NFS_OK)
5995 status = nfs4_lock_expired(state, request);
5996 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005997}
5998#endif
5999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6001{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006002 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006003 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006004 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006006 if ((fl_flags & FL_POSIX) &&
6007 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6008 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006009 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006010 status = nfs4_set_lock_state(state, request);
6011 if (status != 0)
6012 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006013 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006014 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006015 if (status < 0)
6016 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006017 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006018 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006019 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006020 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006021 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006022 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006023 up_read(&nfsi->rwsem);
6024 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006025 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006026 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006027 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006028out:
6029 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 return status;
6031}
6032
6033static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6034{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006035 struct nfs4_exception exception = {
6036 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006037 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006038 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 int err;
6040
6041 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006042 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006043 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07006044 if (err == -NFS4ERR_DENIED)
6045 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006047 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 } while (exception.retry);
6049 return err;
6050}
6051
6052static int
6053nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6054{
6055 struct nfs_open_context *ctx;
6056 struct nfs4_state *state;
6057 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6058 int status;
6059
6060 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006061 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 state = ctx->state;
6063
6064 if (request->fl_start < 0 || request->fl_end < 0)
6065 return -EINVAL;
6066
Trond Myklebustd9531262009-07-21 19:22:38 -04006067 if (IS_GETLK(cmd)) {
6068 if (state != NULL)
6069 return nfs4_proc_getlk(state, F_GETLK, request);
6070 return 0;
6071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
6073 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6074 return -EINVAL;
6075
Trond Myklebustd9531262009-07-21 19:22:38 -04006076 if (request->fl_type == F_UNLCK) {
6077 if (state != NULL)
6078 return nfs4_proc_unlck(state, cmd, request);
6079 return 0;
6080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Trond Myklebustd9531262009-07-21 19:22:38 -04006082 if (state == NULL)
6083 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006084 /*
6085 * Don't rely on the VFS having checked the file open mode,
6086 * since it won't do this for flock() locks.
6087 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006088 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006089 case F_RDLCK:
6090 if (!(filp->f_mode & FMODE_READ))
6091 return -EBADF;
6092 break;
6093 case F_WRLCK:
6094 if (!(filp->f_mode & FMODE_WRITE))
6095 return -EBADF;
6096 }
6097
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 do {
6099 status = nfs4_proc_setlk(state, cmd, request);
6100 if ((status != -EAGAIN) || IS_SETLK(cmd))
6101 break;
6102 timeout = nfs4_set_lock_task_retry(timeout);
6103 status = -ERESTARTSYS;
6104 if (signalled())
6105 break;
6106 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 return status;
6108}
6109
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006110int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006111{
6112 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006113 int err;
6114
6115 err = nfs4_set_lock_state(state, fl);
6116 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006117 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006118 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006119 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006120}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006121
Trond Myklebustcf470c32012-03-07 13:49:12 -05006122struct nfs_release_lockowner_data {
6123 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006124 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006125 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006126 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006127 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006128};
6129
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006130static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6131{
6132 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006133 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006134 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6135 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006136 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006137 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006138}
6139
6140static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6141{
6142 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006143 struct nfs_server *server = data->server;
6144
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006145 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006146
6147 switch (task->tk_status) {
6148 case 0:
6149 renew_lease(server, data->timestamp);
6150 break;
6151 case -NFS4ERR_STALE_CLIENTID:
6152 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006153 nfs4_schedule_lease_recovery(server->nfs_client);
6154 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006155 case -NFS4ERR_LEASE_MOVED:
6156 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006157 if (nfs4_async_handle_error(task, server,
6158 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006159 rpc_restart_call_prepare(task);
6160 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006161}
6162
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006163static void nfs4_release_lockowner_release(void *calldata)
6164{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006165 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006166 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006167 kfree(calldata);
6168}
6169
Trond Myklebust17280172012-03-11 13:11:00 -04006170static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006171 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6172 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006173 .rpc_release = nfs4_release_lockowner_release,
6174};
6175
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006176static void
6177nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006178{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006179 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006180 struct rpc_message msg = {
6181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6182 };
6183
6184 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006185 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006186
Trond Myklebustcf470c32012-03-07 13:49:12 -05006187 data = kmalloc(sizeof(*data), GFP_NOFS);
6188 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006189 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006190 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006191 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006192 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6193 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6194 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006195
Trond Myklebustcf470c32012-03-07 13:49:12 -05006196 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006197 msg.rpc_resp = &data->res;
6198 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006199 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006200}
6201
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006202#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6203
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006204static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6205 const void *buf, size_t buflen,
6206 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006207{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006208 if (strcmp(key, "") != 0)
6209 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006210
David Howells2b0143b2015-03-17 22:25:59 +00006211 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006212}
6213
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006214static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6215 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006216{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006217 if (strcmp(key, "") != 0)
6218 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006219
David Howells2b0143b2015-03-17 22:25:59 +00006220 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006221}
6222
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006223static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6224 size_t list_len, const char *name,
6225 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006226{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006227 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006228
David Howells2b0143b2015-03-17 22:25:59 +00006229 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
J. Bruce Fields096455a2006-03-20 23:23:42 -05006230 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006231
6232 if (list && len <= list_len)
6233 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006234 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006235}
6236
David Quigleyc9bccef2013-05-22 12:50:45 -04006237#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6238static inline int nfs4_server_supports_labels(struct nfs_server *server)
6239{
6240 return server->caps & NFS_CAP_SECURITY_LABEL;
6241}
6242
6243static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6244 const void *buf, size_t buflen,
6245 int flags, int type)
6246{
6247 if (security_ismaclabel(key))
6248 return nfs4_set_security_label(dentry, buf, buflen);
6249
6250 return -EOPNOTSUPP;
6251}
6252
6253static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6254 void *buf, size_t buflen, int type)
6255{
6256 if (security_ismaclabel(key))
David Howells2b0143b2015-03-17 22:25:59 +00006257 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006258 return -EOPNOTSUPP;
6259}
6260
6261static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6262 size_t list_len, const char *name,
6263 size_t name_len, int type)
6264{
6265 size_t len = 0;
6266
David Howells2b0143b2015-03-17 22:25:59 +00006267 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6268 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
David Quigleyc9bccef2013-05-22 12:50:45 -04006269 if (list && len <= list_len)
David Howells2b0143b2015-03-17 22:25:59 +00006270 security_inode_listsecurity(d_inode(dentry), list, len);
David Quigleyc9bccef2013-05-22 12:50:45 -04006271 }
6272 return len;
6273}
6274
6275static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6276 .prefix = XATTR_SECURITY_PREFIX,
6277 .list = nfs4_xattr_list_nfs4_label,
6278 .get = nfs4_xattr_get_nfs4_label,
6279 .set = nfs4_xattr_set_nfs4_label,
6280};
6281#endif
6282
6283
Andy Adamson533eb462011-06-13 18:25:56 -04006284/*
6285 * nfs_fhget will use either the mounted_on_fileid or the fileid
6286 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006287static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6288{
Andy Adamson533eb462011-06-13 18:25:56 -04006289 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6290 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6291 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006292 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006293 return;
6294
6295 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006296 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006297 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6298 fattr->nlink = 2;
6299}
6300
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006301static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6302 const struct qstr *name,
6303 struct nfs4_fs_locations *fs_locations,
6304 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006305{
6306 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006307 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006308 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006309 };
6310 struct nfs4_fs_locations_arg args = {
6311 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006312 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006313 .page = page,
6314 .bitmask = bitmask,
6315 };
Benny Halevy22958462009-04-01 09:22:02 -04006316 struct nfs4_fs_locations_res res = {
6317 .fs_locations = fs_locations,
6318 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006319 struct rpc_message msg = {
6320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6321 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006322 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006323 };
6324 int status;
6325
Harvey Harrison3110ff82008-05-02 13:42:44 -07006326 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006327
6328 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6329 * is not supported */
6330 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6331 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6332 else
6333 bitmask[0] |= FATTR4_WORD0_FILEID;
6334
Trond Myklebustc228fd32007-01-13 02:28:11 -05006335 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006336 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006337 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006338 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006339 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006340 return status;
6341}
6342
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006343int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6344 const struct qstr *name,
6345 struct nfs4_fs_locations *fs_locations,
6346 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006347{
6348 struct nfs4_exception exception = { };
6349 int err;
6350 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006351 err = _nfs4_proc_fs_locations(client, dir, name,
6352 fs_locations, page);
6353 trace_nfs4_get_fs_locations(dir, name, err);
6354 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006355 &exception);
6356 } while (exception.retry);
6357 return err;
6358}
6359
Chuck Leverb03d7352013-10-17 14:12:50 -04006360/*
6361 * This operation also signals the server that this client is
6362 * performing migration recovery. The server can stop returning
6363 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6364 * appended to this compound to identify the client ID which is
6365 * performing recovery.
6366 */
6367static int _nfs40_proc_get_locations(struct inode *inode,
6368 struct nfs4_fs_locations *locations,
6369 struct page *page, struct rpc_cred *cred)
6370{
6371 struct nfs_server *server = NFS_SERVER(inode);
6372 struct rpc_clnt *clnt = server->client;
6373 u32 bitmask[2] = {
6374 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6375 };
6376 struct nfs4_fs_locations_arg args = {
6377 .clientid = server->nfs_client->cl_clientid,
6378 .fh = NFS_FH(inode),
6379 .page = page,
6380 .bitmask = bitmask,
6381 .migration = 1, /* skip LOOKUP */
6382 .renew = 1, /* append RENEW */
6383 };
6384 struct nfs4_fs_locations_res res = {
6385 .fs_locations = locations,
6386 .migration = 1,
6387 .renew = 1,
6388 };
6389 struct rpc_message msg = {
6390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6391 .rpc_argp = &args,
6392 .rpc_resp = &res,
6393 .rpc_cred = cred,
6394 };
6395 unsigned long now = jiffies;
6396 int status;
6397
6398 nfs_fattr_init(&locations->fattr);
6399 locations->server = server;
6400 locations->nlocations = 0;
6401
6402 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6403 nfs4_set_sequence_privileged(&args.seq_args);
6404 status = nfs4_call_sync_sequence(clnt, server, &msg,
6405 &args.seq_args, &res.seq_res);
6406 if (status)
6407 return status;
6408
6409 renew_lease(server, now);
6410 return 0;
6411}
6412
6413#ifdef CONFIG_NFS_V4_1
6414
6415/*
6416 * This operation also signals the server that this client is
6417 * performing migration recovery. The server can stop asserting
6418 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6419 * performing this operation is identified in the SEQUENCE
6420 * operation in this compound.
6421 *
6422 * When the client supports GETATTR(fs_locations_info), it can
6423 * be plumbed in here.
6424 */
6425static int _nfs41_proc_get_locations(struct inode *inode,
6426 struct nfs4_fs_locations *locations,
6427 struct page *page, struct rpc_cred *cred)
6428{
6429 struct nfs_server *server = NFS_SERVER(inode);
6430 struct rpc_clnt *clnt = server->client;
6431 u32 bitmask[2] = {
6432 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6433 };
6434 struct nfs4_fs_locations_arg args = {
6435 .fh = NFS_FH(inode),
6436 .page = page,
6437 .bitmask = bitmask,
6438 .migration = 1, /* skip LOOKUP */
6439 };
6440 struct nfs4_fs_locations_res res = {
6441 .fs_locations = locations,
6442 .migration = 1,
6443 };
6444 struct rpc_message msg = {
6445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6446 .rpc_argp = &args,
6447 .rpc_resp = &res,
6448 .rpc_cred = cred,
6449 };
6450 int status;
6451
6452 nfs_fattr_init(&locations->fattr);
6453 locations->server = server;
6454 locations->nlocations = 0;
6455
6456 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6457 nfs4_set_sequence_privileged(&args.seq_args);
6458 status = nfs4_call_sync_sequence(clnt, server, &msg,
6459 &args.seq_args, &res.seq_res);
6460 if (status == NFS4_OK &&
6461 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6462 status = -NFS4ERR_LEASE_MOVED;
6463 return status;
6464}
6465
6466#endif /* CONFIG_NFS_V4_1 */
6467
6468/**
6469 * nfs4_proc_get_locations - discover locations for a migrated FSID
6470 * @inode: inode on FSID that is migrating
6471 * @locations: result of query
6472 * @page: buffer
6473 * @cred: credential to use for this operation
6474 *
6475 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6476 * operation failed, or a negative errno if a local error occurred.
6477 *
6478 * On success, "locations" is filled in, but if the server has
6479 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6480 * asserted.
6481 *
6482 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6483 * from this client that require migration recovery.
6484 */
6485int nfs4_proc_get_locations(struct inode *inode,
6486 struct nfs4_fs_locations *locations,
6487 struct page *page, struct rpc_cred *cred)
6488{
6489 struct nfs_server *server = NFS_SERVER(inode);
6490 struct nfs_client *clp = server->nfs_client;
6491 const struct nfs4_mig_recovery_ops *ops =
6492 clp->cl_mvops->mig_recovery_ops;
6493 struct nfs4_exception exception = { };
6494 int status;
6495
6496 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6497 (unsigned long long)server->fsid.major,
6498 (unsigned long long)server->fsid.minor,
6499 clp->cl_hostname);
6500 nfs_display_fhandle(NFS_FH(inode), __func__);
6501
6502 do {
6503 status = ops->get_locations(inode, locations, page, cred);
6504 if (status != -NFS4ERR_DELAY)
6505 break;
6506 nfs4_handle_exception(server, status, &exception);
6507 } while (exception.retry);
6508 return status;
6509}
6510
Chuck Lever44c99932013-10-17 14:13:30 -04006511/*
6512 * This operation also signals the server that this client is
6513 * performing "lease moved" recovery. The server can stop
6514 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6515 * is appended to this compound to identify the client ID which is
6516 * performing recovery.
6517 */
6518static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6519{
6520 struct nfs_server *server = NFS_SERVER(inode);
6521 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6522 struct rpc_clnt *clnt = server->client;
6523 struct nfs4_fsid_present_arg args = {
6524 .fh = NFS_FH(inode),
6525 .clientid = clp->cl_clientid,
6526 .renew = 1, /* append RENEW */
6527 };
6528 struct nfs4_fsid_present_res res = {
6529 .renew = 1,
6530 };
6531 struct rpc_message msg = {
6532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6533 .rpc_argp = &args,
6534 .rpc_resp = &res,
6535 .rpc_cred = cred,
6536 };
6537 unsigned long now = jiffies;
6538 int status;
6539
6540 res.fh = nfs_alloc_fhandle();
6541 if (res.fh == NULL)
6542 return -ENOMEM;
6543
6544 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6545 nfs4_set_sequence_privileged(&args.seq_args);
6546 status = nfs4_call_sync_sequence(clnt, server, &msg,
6547 &args.seq_args, &res.seq_res);
6548 nfs_free_fhandle(res.fh);
6549 if (status)
6550 return status;
6551
6552 do_renew_lease(clp, now);
6553 return 0;
6554}
6555
6556#ifdef CONFIG_NFS_V4_1
6557
6558/*
6559 * This operation also signals the server that this client is
6560 * performing "lease moved" recovery. The server can stop asserting
6561 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6562 * this operation is identified in the SEQUENCE operation in this
6563 * compound.
6564 */
6565static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6566{
6567 struct nfs_server *server = NFS_SERVER(inode);
6568 struct rpc_clnt *clnt = server->client;
6569 struct nfs4_fsid_present_arg args = {
6570 .fh = NFS_FH(inode),
6571 };
6572 struct nfs4_fsid_present_res res = {
6573 };
6574 struct rpc_message msg = {
6575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6576 .rpc_argp = &args,
6577 .rpc_resp = &res,
6578 .rpc_cred = cred,
6579 };
6580 int status;
6581
6582 res.fh = nfs_alloc_fhandle();
6583 if (res.fh == NULL)
6584 return -ENOMEM;
6585
6586 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6587 nfs4_set_sequence_privileged(&args.seq_args);
6588 status = nfs4_call_sync_sequence(clnt, server, &msg,
6589 &args.seq_args, &res.seq_res);
6590 nfs_free_fhandle(res.fh);
6591 if (status == NFS4_OK &&
6592 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6593 status = -NFS4ERR_LEASE_MOVED;
6594 return status;
6595}
6596
6597#endif /* CONFIG_NFS_V4_1 */
6598
6599/**
6600 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6601 * @inode: inode on FSID to check
6602 * @cred: credential to use for this operation
6603 *
6604 * Server indicates whether the FSID is present, moved, or not
6605 * recognized. This operation is necessary to clear a LEASE_MOVED
6606 * condition for this client ID.
6607 *
6608 * Returns NFS4_OK if the FSID is present on this server,
6609 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6610 * NFS4ERR code if some error occurred on the server, or a
6611 * negative errno if a local failure occurred.
6612 */
6613int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6614{
6615 struct nfs_server *server = NFS_SERVER(inode);
6616 struct nfs_client *clp = server->nfs_client;
6617 const struct nfs4_mig_recovery_ops *ops =
6618 clp->cl_mvops->mig_recovery_ops;
6619 struct nfs4_exception exception = { };
6620 int status;
6621
6622 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6623 (unsigned long long)server->fsid.major,
6624 (unsigned long long)server->fsid.minor,
6625 clp->cl_hostname);
6626 nfs_display_fhandle(NFS_FH(inode), __func__);
6627
6628 do {
6629 status = ops->fsid_present(inode, cred);
6630 if (status != -NFS4ERR_DELAY)
6631 break;
6632 nfs4_handle_exception(server, status, &exception);
6633 } while (exception.retry);
6634 return status;
6635}
6636
Andy Adamson5ec16a82013-08-08 10:57:55 -04006637/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006638 * If 'use_integrity' is true and the state managment nfs_client
6639 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6640 * and the machine credential as per RFC3530bis and RFC5661 Security
6641 * Considerations sections. Otherwise, just use the user cred with the
6642 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006643 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006644static 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 +00006645{
6646 int status;
6647 struct nfs4_secinfo_arg args = {
6648 .dir_fh = NFS_FH(dir),
6649 .name = name,
6650 };
6651 struct nfs4_secinfo_res res = {
6652 .flavors = flavors,
6653 };
6654 struct rpc_message msg = {
6655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6656 .rpc_argp = &args,
6657 .rpc_resp = &res,
6658 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006659 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006660 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006661
6662 if (use_integrity) {
6663 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006664 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6665 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006666 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006667
6668 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006669
6670 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6671 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6672
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006673 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6674 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006675 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006676
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006677 if (cred)
6678 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006679
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006680 return status;
6681}
6682
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006683int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6684 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006685{
6686 struct nfs4_exception exception = { };
6687 int err;
6688 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006689 err = -NFS4ERR_WRONGSEC;
6690
6691 /* try to use integrity protection with machine cred */
6692 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6693 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6694
6695 /*
6696 * if unable to use integrity protection, or SECINFO with
6697 * integrity protection returns NFS4ERR_WRONGSEC (which is
6698 * disallowed by spec, but exists in deployed servers) use
6699 * the current filesystem's rpc_client and the user cred.
6700 */
6701 if (err == -NFS4ERR_WRONGSEC)
6702 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6703
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006704 trace_nfs4_secinfo(dir, name, err);
6705 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006706 &exception);
6707 } while (exception.retry);
6708 return err;
6709}
6710
Andy Adamson557134a2009-04-01 09:21:53 -04006711#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006712/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006713 * Check the exchange flags returned by the server for invalid flags, having
6714 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6715 * DS flags set.
6716 */
6717static int nfs4_check_cl_exchange_flags(u32 flags)
6718{
6719 if (flags & ~EXCHGID4_FLAG_MASK_R)
6720 goto out_inval;
6721 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6722 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6723 goto out_inval;
6724 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6725 goto out_inval;
6726 return NFS_OK;
6727out_inval:
6728 return -NFS4ERR_INVAL;
6729}
6730
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006731static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006732nfs41_same_server_scope(struct nfs41_server_scope *a,
6733 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006734{
6735 if (a->server_scope_sz == b->server_scope_sz &&
6736 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6737 return true;
6738
6739 return false;
6740}
6741
Andy Adamson357f54d2010-12-14 10:11:57 -05006742/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006743 * nfs4_proc_bind_conn_to_session()
6744 *
6745 * The 4.1 client currently uses the same TCP connection for the
6746 * fore and backchannel.
6747 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006748int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006749{
6750 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006751 struct nfs41_bind_conn_to_session_args args = {
6752 .client = clp,
6753 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6754 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006755 struct nfs41_bind_conn_to_session_res res;
6756 struct rpc_message msg = {
6757 .rpc_proc =
6758 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006759 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006760 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006761 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006762 };
6763
6764 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006765
Trond Myklebust71a097c2015-02-18 09:27:18 -08006766 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6767 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6768 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006769
6770 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006771 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006772 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006773 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006774 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6775 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6776 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006777 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006778 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006779 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006780 dprintk("NFS: %s: Unexpected direction from server\n",
6781 __func__);
6782 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006783 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006784 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006785 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006786 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6787 __func__);
6788 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006789 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006790 }
6791 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006792out:
6793 dprintk("<-- %s status= %d\n", __func__, status);
6794 return status;
6795}
6796
6797/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006798 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6799 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006800 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006801static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6802 .how = SP4_MACH_CRED,
6803 .enforce.u.words = {
6804 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6805 1 << (OP_EXCHANGE_ID - 32) |
6806 1 << (OP_CREATE_SESSION - 32) |
6807 1 << (OP_DESTROY_SESSION - 32) |
6808 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006809 },
6810 .allow.u.words = {
6811 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006812 1 << (OP_LOCKU) |
6813 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006814 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006815 1 << (OP_SECINFO_NO_NAME - 32) |
6816 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006817 1 << (OP_FREE_STATEID - 32) |
6818 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006819 }
6820};
6821
6822/*
6823 * Select the state protection mode for client `clp' given the server results
6824 * from exchange_id in `sp'.
6825 *
6826 * Returns 0 on success, negative errno otherwise.
6827 */
6828static int nfs4_sp4_select_mode(struct nfs_client *clp,
6829 struct nfs41_state_protection *sp)
6830{
6831 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6832 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6833 1 << (OP_EXCHANGE_ID - 32) |
6834 1 << (OP_CREATE_SESSION - 32) |
6835 1 << (OP_DESTROY_SESSION - 32) |
6836 1 << (OP_DESTROY_CLIENTID - 32)
6837 };
6838 unsigned int i;
6839
6840 if (sp->how == SP4_MACH_CRED) {
6841 /* Print state protect result */
6842 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6843 for (i = 0; i <= LAST_NFS4_OP; i++) {
6844 if (test_bit(i, sp->enforce.u.longs))
6845 dfprintk(MOUNT, " enforce op %d\n", i);
6846 if (test_bit(i, sp->allow.u.longs))
6847 dfprintk(MOUNT, " allow op %d\n", i);
6848 }
6849
6850 /* make sure nothing is on enforce list that isn't supported */
6851 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6852 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6853 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6854 return -EINVAL;
6855 }
6856 }
6857
6858 /*
6859 * Minimal mode - state operations are allowed to use machine
6860 * credential. Note this already happens by default, so the
6861 * client doesn't have to do anything more than the negotiation.
6862 *
6863 * NOTE: we don't care if EXCHANGE_ID is in the list -
6864 * we're already using the machine cred for exchange_id
6865 * and will never use a different cred.
6866 */
6867 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6868 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6869 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6870 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6871 dfprintk(MOUNT, "sp4_mach_cred:\n");
6872 dfprintk(MOUNT, " minimal mode enabled\n");
6873 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6874 } else {
6875 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6876 return -EINVAL;
6877 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006878
6879 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6880 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6881 dfprintk(MOUNT, " cleanup mode enabled\n");
6882 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6883 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006884
6885 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6886 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6887 dfprintk(MOUNT, " secinfo mode enabled\n");
6888 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6889 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006890
6891 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6892 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6893 dfprintk(MOUNT, " stateid mode enabled\n");
6894 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6895 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006896
6897 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6898 dfprintk(MOUNT, " write mode enabled\n");
6899 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6900 }
6901
6902 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6903 dfprintk(MOUNT, " commit mode enabled\n");
6904 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6905 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006906 }
6907
6908 return 0;
6909}
6910
6911/*
6912 * _nfs4_proc_exchange_id()
6913 *
6914 * Wrapper for EXCHANGE_ID operation.
6915 */
6916static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6917 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006918{
6919 nfs4_verifier verifier;
6920 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006921 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006922 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006923#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006924 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006925 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6926 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6927#else
6928 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6929 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6930#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006931 };
6932 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006933 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006934 };
6935 int status;
6936 struct rpc_message msg = {
6937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6938 .rpc_argp = &args,
6939 .rpc_resp = &res,
6940 .rpc_cred = cred,
6941 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006942
Chuck Leverf0920752012-05-21 22:45:41 -04006943 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006944
6945 status = nfs4_init_uniform_client_string(clp);
6946 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04006947 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006948
6949 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006950 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006951 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006952
Chuck Leveracdeb692012-05-21 22:46:16 -04006953 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006954 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006955 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006956 status = -ENOMEM;
6957 goto out;
6958 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006959
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006960 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006961 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006962 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006963 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006964 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006965 }
6966
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006967 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006968 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006969 status = -ENOMEM;
6970 goto out_server_scope;
6971 }
6972
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006973 switch (sp4_how) {
6974 case SP4_NONE:
6975 args.state_protect.how = SP4_NONE;
6976 break;
6977
6978 case SP4_MACH_CRED:
6979 args.state_protect = nfs4_sp4_mach_cred_request;
6980 break;
6981
6982 default:
6983 /* unsupported! */
6984 WARN_ON_ONCE(1);
6985 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08006986 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006987 }
6988
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006989 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006990 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006991 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006992 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006993
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006994 if (status == 0)
6995 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6996
Chuck Lever177313f2012-05-21 22:44:58 -04006997 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006998 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05006999 clp->cl_exchange_flags = res.flags;
7000 /* Client ID is not confirmed */
7001 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7002 clear_bit(NFS4_SESSION_ESTABLISHED,
7003 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007004 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007005 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007006
Chuck Leveracdeb692012-05-21 22:46:16 -04007007 kfree(clp->cl_serverowner);
7008 clp->cl_serverowner = res.server_owner;
7009 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007010
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007011 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007012 kfree(clp->cl_implid);
7013 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007014 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007015
Chuck Lever177313f2012-05-21 22:44:58 -04007016 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007017 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007018 res.server_scope)) {
7019 dprintk("%s: server_scope mismatch detected\n",
7020 __func__);
7021 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007022 kfree(clp->cl_serverscope);
7023 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007024 }
7025
Chuck Lever177313f2012-05-21 22:44:58 -04007026 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007027 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007028 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007029 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007030 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007031
Kinglong Mee6b559702015-07-01 11:54:53 +08007032out_impl_id:
7033 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007034out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007035 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007036out_server_owner:
7037 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007038out:
Chuck Lever177313f2012-05-21 22:44:58 -04007039 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007040 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007041 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007042 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007043 clp->cl_implid->date.seconds,
7044 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007045 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007046 return status;
7047}
7048
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007049/*
7050 * nfs4_proc_exchange_id()
7051 *
7052 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7053 *
7054 * Since the clientid has expired, all compounds using sessions
7055 * associated with the stale clientid will be returning
7056 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7057 * be in some phase of session reset.
7058 *
7059 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7060 */
7061int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7062{
7063 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7064 int status;
7065
7066 /* try SP4_MACH_CRED if krb5i/p */
7067 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7068 authflavor == RPC_AUTH_GSS_KRB5P) {
7069 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7070 if (!status)
7071 return 0;
7072 }
7073
7074 /* try SP4_NONE */
7075 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7076}
7077
Trond Myklebust66245532012-05-25 17:18:09 -04007078static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7079 struct rpc_cred *cred)
7080{
7081 struct rpc_message msg = {
7082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7083 .rpc_argp = clp,
7084 .rpc_cred = cred,
7085 };
7086 int status;
7087
7088 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007089 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007090 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007091 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007092 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7093 return status;
7094}
7095
7096static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7097 struct rpc_cred *cred)
7098{
7099 unsigned int loop;
7100 int ret;
7101
7102 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7103 ret = _nfs4_proc_destroy_clientid(clp, cred);
7104 switch (ret) {
7105 case -NFS4ERR_DELAY:
7106 case -NFS4ERR_CLIENTID_BUSY:
7107 ssleep(1);
7108 break;
7109 default:
7110 return ret;
7111 }
7112 }
7113 return 0;
7114}
7115
7116int nfs4_destroy_clientid(struct nfs_client *clp)
7117{
7118 struct rpc_cred *cred;
7119 int ret = 0;
7120
7121 if (clp->cl_mvops->minor_version < 1)
7122 goto out;
7123 if (clp->cl_exchange_flags == 0)
7124 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007125 if (clp->cl_preserve_clid)
7126 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007127 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007128 ret = nfs4_proc_destroy_clientid(clp, cred);
7129 if (cred)
7130 put_rpccred(cred);
7131 switch (ret) {
7132 case 0:
7133 case -NFS4ERR_STALE_CLIENTID:
7134 clp->cl_exchange_flags = 0;
7135 }
7136out:
7137 return ret;
7138}
7139
Andy Adamson2050f0c2009-04-01 09:22:30 -04007140struct nfs4_get_lease_time_data {
7141 struct nfs4_get_lease_time_args *args;
7142 struct nfs4_get_lease_time_res *res;
7143 struct nfs_client *clp;
7144};
7145
7146static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7147 void *calldata)
7148{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007149 struct nfs4_get_lease_time_data *data =
7150 (struct nfs4_get_lease_time_data *)calldata;
7151
7152 dprintk("--> %s\n", __func__);
7153 /* just setup sequence, do not trigger session recovery
7154 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007155 nfs41_setup_sequence(data->clp->cl_session,
7156 &data->args->la_seq_args,
7157 &data->res->lr_seq_res,
7158 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007159 dprintk("<-- %s\n", __func__);
7160}
7161
7162/*
7163 * Called from nfs4_state_manager thread for session setup, so don't recover
7164 * from sequence operation or clientid errors.
7165 */
7166static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7167{
7168 struct nfs4_get_lease_time_data *data =
7169 (struct nfs4_get_lease_time_data *)calldata;
7170
7171 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007172 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7173 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007174 switch (task->tk_status) {
7175 case -NFS4ERR_DELAY:
7176 case -NFS4ERR_GRACE:
7177 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7178 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7179 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007180 /* fall through */
7181 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007182 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007183 return;
7184 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007185 dprintk("<-- %s\n", __func__);
7186}
7187
Trond Myklebust17280172012-03-11 13:11:00 -04007188static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007189 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7190 .rpc_call_done = nfs4_get_lease_time_done,
7191};
7192
7193int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7194{
7195 struct rpc_task *task;
7196 struct nfs4_get_lease_time_args args;
7197 struct nfs4_get_lease_time_res res = {
7198 .lr_fsinfo = fsinfo,
7199 };
7200 struct nfs4_get_lease_time_data data = {
7201 .args = &args,
7202 .res = &res,
7203 .clp = clp,
7204 };
7205 struct rpc_message msg = {
7206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7207 .rpc_argp = &args,
7208 .rpc_resp = &res,
7209 };
7210 struct rpc_task_setup task_setup = {
7211 .rpc_client = clp->cl_rpcclient,
7212 .rpc_message = &msg,
7213 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007214 .callback_data = &data,
7215 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007216 };
7217 int status;
7218
Chuck Levera9c92d62013-08-09 12:48:18 -04007219 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007220 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007221 dprintk("--> %s\n", __func__);
7222 task = rpc_run_task(&task_setup);
7223
7224 if (IS_ERR(task))
7225 status = PTR_ERR(task);
7226 else {
7227 status = task->tk_status;
7228 rpc_put_task(task);
7229 }
7230 dprintk("<-- %s return %d\n", __func__, status);
7231
7232 return status;
7233}
7234
Andy Adamsonfc931582009-04-01 09:22:31 -04007235/*
7236 * Initialize the values to be used by the client in CREATE_SESSION
7237 * If nfs4_init_session set the fore channel request and response sizes,
7238 * use them.
7239 *
7240 * Set the back channel max_resp_sz_cached to zero to force the client to
7241 * always set csa_cachethis to FALSE because the current implementation
7242 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7243 */
7244static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7245{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007246 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007247
Andy Adamson18aad3d2013-06-26 12:21:49 -04007248 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7249 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7250
Andy Adamsonfc931582009-04-01 09:22:31 -04007251 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007252 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7253 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007254 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007255 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007256
7257 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007258 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007259 __func__,
7260 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007261 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007262
7263 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007264 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7265 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7266 args->bc_attrs.max_resp_sz_cached = 0;
7267 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7268 args->bc_attrs.max_reqs = 1;
7269
7270 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7271 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7272 __func__,
7273 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7274 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7275 args->bc_attrs.max_reqs);
7276}
7277
Trond Myklebust79969dd2015-02-18 11:30:18 -08007278static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7279 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007280{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007281 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007282 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007283
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007284 if (rcvd->max_resp_sz > sent->max_resp_sz)
7285 return -EINVAL;
7286 /*
7287 * Our requested max_ops is the minimum we need; we're not
7288 * prepared to break up compounds into smaller pieces than that.
7289 * So, no point even trying to continue if the server won't
7290 * cooperate:
7291 */
7292 if (rcvd->max_ops < sent->max_ops)
7293 return -EINVAL;
7294 if (rcvd->max_reqs == 0)
7295 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007296 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7297 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007298 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007299}
7300
Trond Myklebust79969dd2015-02-18 11:30:18 -08007301static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7302 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007303{
7304 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007305 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007306
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007307 if (!(res->flags & SESSION4_BACK_CHAN))
7308 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007309 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7310 return -EINVAL;
7311 if (rcvd->max_resp_sz < sent->max_resp_sz)
7312 return -EINVAL;
7313 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7314 return -EINVAL;
7315 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007316 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007317 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007318 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007319 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007320out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007321 return 0;
7322}
Andy Adamson8d353012009-04-01 09:22:32 -04007323
Andy Adamson8d353012009-04-01 09:22:32 -04007324static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007325 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007326{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007327 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007328
Trond Myklebust79969dd2015-02-18 11:30:18 -08007329 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007330 if (ret)
7331 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007332 return nfs4_verify_back_channel_attrs(args, res);
7333}
7334
7335static void nfs4_update_session(struct nfs4_session *session,
7336 struct nfs41_create_session_res *res)
7337{
7338 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007339 /* Mark client id and session as being confirmed */
7340 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7341 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007342 session->flags = res->flags;
7343 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007344 if (res->flags & SESSION4_BACK_CHAN)
7345 memcpy(&session->bc_attrs, &res->bc_attrs,
7346 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007347}
7348
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007349static int _nfs4_proc_create_session(struct nfs_client *clp,
7350 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007351{
7352 struct nfs4_session *session = clp->cl_session;
7353 struct nfs41_create_session_args args = {
7354 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007355 .clientid = clp->cl_clientid,
7356 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007357 .cb_program = NFS4_CALLBACK,
7358 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007359 struct nfs41_create_session_res res;
7360
Andy Adamsonfc931582009-04-01 09:22:31 -04007361 struct rpc_message msg = {
7362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7363 .rpc_argp = &args,
7364 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007365 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007366 };
7367 int status;
7368
7369 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007370 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007371
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007372 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007373 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007374
Trond Myklebust43095d32012-11-20 11:13:12 -05007375 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007376 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007377 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007378 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007379 if (clp->cl_seqid == res.seqid)
7380 clp->cl_seqid++;
7381 if (status)
7382 goto out;
7383 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007384 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007385out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007386 return status;
7387}
7388
7389/*
7390 * Issues a CREATE_SESSION operation to the server.
7391 * It is the responsibility of the caller to verify the session is
7392 * expired before calling this routine.
7393 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007394int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007395{
7396 int status;
7397 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007398 struct nfs4_session *session = clp->cl_session;
7399
7400 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7401
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007402 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007403 if (status)
7404 goto out;
7405
Andy Adamsonaacd5532011-11-09 13:58:21 -05007406 /* Init or reset the session slot tables */
7407 status = nfs4_setup_session_slot_tables(session);
7408 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007409 if (status)
7410 goto out;
7411
7412 ptr = (unsigned *)&session->sess_id.data[0];
7413 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7414 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007415out:
7416 dprintk("<-- %s\n", __func__);
7417 return status;
7418}
7419
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007420/*
7421 * Issue the over-the-wire RPC DESTROY_SESSION.
7422 * The caller must serialize access to this routine.
7423 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007424int nfs4_proc_destroy_session(struct nfs4_session *session,
7425 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007426{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007427 struct rpc_message msg = {
7428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7429 .rpc_argp = session,
7430 .rpc_cred = cred,
7431 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007432 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007433
7434 dprintk("--> nfs4_proc_destroy_session\n");
7435
7436 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007437 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7438 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007439
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007440 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007441 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007442
7443 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007444 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007445 "Session has been destroyed regardless...\n", status);
7446
7447 dprintk("<-- nfs4_proc_destroy_session\n");
7448 return status;
7449}
7450
Trond Myklebust7b38c362012-05-23 13:23:31 -04007451/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007452 * Renew the cl_session lease.
7453 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007454struct nfs4_sequence_data {
7455 struct nfs_client *clp;
7456 struct nfs4_sequence_args args;
7457 struct nfs4_sequence_res res;
7458};
7459
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007460static void nfs41_sequence_release(void *data)
7461{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007462 struct nfs4_sequence_data *calldata = data;
7463 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007464
Alexandros Batsakis71358402010-02-05 03:45:05 -08007465 if (atomic_read(&clp->cl_count) > 1)
7466 nfs4_schedule_state_renewal(clp);
7467 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007468 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007469}
7470
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007471static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7472{
7473 switch(task->tk_status) {
7474 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007475 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7476 return -EAGAIN;
7477 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007478 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007479 }
7480 return 0;
7481}
7482
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007483static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007484{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007485 struct nfs4_sequence_data *calldata = data;
7486 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007487
Trond Myklebust14516c32010-07-31 14:29:06 -04007488 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7489 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007490
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007491 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007492 if (task->tk_status < 0) {
7493 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007494 if (atomic_read(&clp->cl_count) == 1)
7495 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007496
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007497 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7498 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007499 return;
7500 }
7501 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007502 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007503out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007504 dprintk("<-- %s\n", __func__);
7505}
7506
7507static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7508{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007509 struct nfs4_sequence_data *calldata = data;
7510 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007511 struct nfs4_sequence_args *args;
7512 struct nfs4_sequence_res *res;
7513
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007514 args = task->tk_msg.rpc_argp;
7515 res = task->tk_msg.rpc_resp;
7516
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007517 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007518}
7519
7520static const struct rpc_call_ops nfs41_sequence_ops = {
7521 .rpc_call_done = nfs41_sequence_call_done,
7522 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007523 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007524};
7525
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007526static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7527 struct rpc_cred *cred,
7528 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007529{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007530 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007531 struct rpc_message msg = {
7532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7533 .rpc_cred = cred,
7534 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007535 struct rpc_task_setup task_setup_data = {
7536 .rpc_client = clp->cl_rpcclient,
7537 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007538 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007539 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007540 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007541
Alexandros Batsakis71358402010-02-05 03:45:05 -08007542 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007543 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007544 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007545 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007546 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007547 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007548 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007549 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007550 if (is_privileged)
7551 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007552 msg.rpc_argp = &calldata->args;
7553 msg.rpc_resp = &calldata->res;
7554 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007555 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007556
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007557 return rpc_run_task(&task_setup_data);
7558}
7559
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007560static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007561{
7562 struct rpc_task *task;
7563 int ret = 0;
7564
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007565 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007566 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007567 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007568 if (IS_ERR(task))
7569 ret = PTR_ERR(task);
7570 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007571 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007572 dprintk("<-- %s status=%d\n", __func__, ret);
7573 return ret;
7574}
7575
7576static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7577{
7578 struct rpc_task *task;
7579 int ret;
7580
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007581 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007582 if (IS_ERR(task)) {
7583 ret = PTR_ERR(task);
7584 goto out;
7585 }
7586 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007587 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007588 ret = task->tk_status;
7589 rpc_put_task(task);
7590out:
7591 dprintk("<-- %s status=%d\n", __func__, ret);
7592 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007593}
7594
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007595struct nfs4_reclaim_complete_data {
7596 struct nfs_client *clp;
7597 struct nfs41_reclaim_complete_args arg;
7598 struct nfs41_reclaim_complete_res res;
7599};
7600
7601static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7602{
7603 struct nfs4_reclaim_complete_data *calldata = data;
7604
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007605 nfs41_setup_sequence(calldata->clp->cl_session,
7606 &calldata->arg.seq_args,
7607 &calldata->res.seq_res,
7608 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007609}
7610
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007611static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7612{
7613 switch(task->tk_status) {
7614 case 0:
7615 case -NFS4ERR_COMPLETE_ALREADY:
7616 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7617 break;
7618 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007619 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007620 /* fall through */
7621 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007622 return -EAGAIN;
7623 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007624 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007625 }
7626 return 0;
7627}
7628
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007629static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7630{
7631 struct nfs4_reclaim_complete_data *calldata = data;
7632 struct nfs_client *clp = calldata->clp;
7633 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7634
7635 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007636 if (!nfs41_sequence_done(task, res))
7637 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007638
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007639 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007640 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7641 rpc_restart_call_prepare(task);
7642 return;
7643 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007644 dprintk("<-- %s\n", __func__);
7645}
7646
7647static void nfs4_free_reclaim_complete_data(void *data)
7648{
7649 struct nfs4_reclaim_complete_data *calldata = data;
7650
7651 kfree(calldata);
7652}
7653
7654static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7655 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7656 .rpc_call_done = nfs4_reclaim_complete_done,
7657 .rpc_release = nfs4_free_reclaim_complete_data,
7658};
7659
7660/*
7661 * Issue a global reclaim complete.
7662 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007663static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7664 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007665{
7666 struct nfs4_reclaim_complete_data *calldata;
7667 struct rpc_task *task;
7668 struct rpc_message msg = {
7669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007670 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007671 };
7672 struct rpc_task_setup task_setup_data = {
7673 .rpc_client = clp->cl_rpcclient,
7674 .rpc_message = &msg,
7675 .callback_ops = &nfs4_reclaim_complete_call_ops,
7676 .flags = RPC_TASK_ASYNC,
7677 };
7678 int status = -ENOMEM;
7679
7680 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007681 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007682 if (calldata == NULL)
7683 goto out;
7684 calldata->clp = clp;
7685 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007686
Chuck Levera9c92d62013-08-09 12:48:18 -04007687 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007688 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007689 msg.rpc_argp = &calldata->arg;
7690 msg.rpc_resp = &calldata->res;
7691 task_setup_data.callback_data = calldata;
7692 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007693 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007694 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007695 goto out;
7696 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007697 status = nfs4_wait_for_completion_rpc_task(task);
7698 if (status == 0)
7699 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007700 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007701 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007702out:
7703 dprintk("<-- %s status=%d\n", __func__, status);
7704 return status;
7705}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007706
7707static void
7708nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7709{
7710 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007711 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007712 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007713
7714 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007715 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7716 * right now covering the LAYOUTGET we are about to send.
7717 * However, that is not so catastrophic, and there seems
7718 * to be no way to prevent it completely.
7719 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007720 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007721 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007722 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007723 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7724 NFS_I(lgp->args.inode)->layout,
Peng Taoce6ab4f2014-09-06 00:53:24 +08007725 &lgp->args.range,
Fred Isamancf7d63f2011-01-06 11:36:25 +00007726 lgp->args.ctx->state)) {
7727 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007728 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007729}
7730
7731static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7732{
7733 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007734 struct inode *inode = lgp->args.inode;
7735 struct nfs_server *server = NFS_SERVER(inode);
7736 struct pnfs_layout_hdr *lo;
7737 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007738 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007739
Boaz Harroshed7e5422014-01-22 20:34:54 +02007740 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007741
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007742 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007743 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007744
7745 switch (task->tk_status) {
7746 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007747 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007748 /*
7749 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7750 * (or clients) writing to the same RAID stripe
7751 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007752 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007753 /*
7754 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7755 * existing layout before getting a new one).
7756 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007757 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007758 timeo = rpc_get_timeout(task->tk_client);
7759 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007760 now = jiffies;
7761 if (time_after(giveup, now)) {
7762 unsigned long delay;
7763
7764 /* Delay for:
7765 * - Not less then NFS4_POLL_RETRY_MIN.
7766 * - One last time a jiffie before we give up
7767 * - exponential backoff (time_now minus start_attempt)
7768 */
7769 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7770 min((giveup - now - 1),
7771 now - lgp->args.timestamp));
7772
7773 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7774 __func__, delay);
7775 rpc_delay(task, delay);
7776 task->tk_status = 0;
7777 rpc_restart_call_prepare(task);
7778 goto out; /* Do not call nfs4_async_handle_error() */
7779 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007780 break;
7781 case -NFS4ERR_EXPIRED:
7782 case -NFS4ERR_BAD_STATEID:
7783 spin_lock(&inode->i_lock);
7784 lo = NFS_I(inode)->layout;
7785 if (!lo || list_empty(&lo->plh_segs)) {
7786 spin_unlock(&inode->i_lock);
7787 /* If the open stateid was bad, then recover it. */
7788 state = lgp->args.ctx->state;
7789 } else {
7790 LIST_HEAD(head);
7791
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007792 /*
7793 * Mark the bad layout state as invalid, then retry
7794 * with the current stateid.
7795 */
Trond Myklebustee314c22012-10-01 17:25:48 -07007796 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7797 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007798 pnfs_free_lseg_list(&head);
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007799
7800 task->tk_status = 0;
7801 rpc_restart_call_prepare(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007802 }
7803 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007804 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebustee314c22012-10-01 17:25:48 -07007805 rpc_restart_call_prepare(task);
7806out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007807 dprintk("<-- %s\n", __func__);
7808}
7809
Idan Kedar85541162012-08-02 11:47:10 +03007810static size_t max_response_pages(struct nfs_server *server)
7811{
7812 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7813 return nfs_page_array_len(0, max_resp_sz);
7814}
7815
7816static void nfs4_free_pages(struct page **pages, size_t size)
7817{
7818 int i;
7819
7820 if (!pages)
7821 return;
7822
7823 for (i = 0; i < size; i++) {
7824 if (!pages[i])
7825 break;
7826 __free_page(pages[i]);
7827 }
7828 kfree(pages);
7829}
7830
7831static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7832{
7833 struct page **pages;
7834 int i;
7835
7836 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7837 if (!pages) {
7838 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7839 return NULL;
7840 }
7841
7842 for (i = 0; i < size; i++) {
7843 pages[i] = alloc_page(gfp_flags);
7844 if (!pages[i]) {
7845 dprintk("%s: failed to allocate page\n", __func__);
7846 nfs4_free_pages(pages, size);
7847 return NULL;
7848 }
7849 }
7850
7851 return pages;
7852}
7853
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007854static void nfs4_layoutget_release(void *calldata)
7855{
7856 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007857 struct inode *inode = lgp->args.inode;
7858 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007859 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007860
7861 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007862 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007863 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007864 put_nfs_open_context(lgp->args.ctx);
7865 kfree(calldata);
7866 dprintk("<-- %s\n", __func__);
7867}
7868
7869static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7870 .rpc_call_prepare = nfs4_layoutget_prepare,
7871 .rpc_call_done = nfs4_layoutget_done,
7872 .rpc_release = nfs4_layoutget_release,
7873};
7874
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007875struct pnfs_layout_segment *
7876nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007877{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007878 struct inode *inode = lgp->args.inode;
7879 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007880 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007881 struct rpc_task *task;
7882 struct rpc_message msg = {
7883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7884 .rpc_argp = &lgp->args,
7885 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007886 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007887 };
7888 struct rpc_task_setup task_setup_data = {
7889 .rpc_client = server->client,
7890 .rpc_message = &msg,
7891 .callback_ops = &nfs4_layoutget_call_ops,
7892 .callback_data = lgp,
7893 .flags = RPC_TASK_ASYNC,
7894 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007895 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007896 int status = 0;
7897
7898 dprintk("--> %s\n", __func__);
7899
Peng Tao4bd5a982014-11-17 11:05:17 +08007900 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7901 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7902
Idan Kedar85541162012-08-02 11:47:10 +03007903 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7904 if (!lgp->args.layout.pages) {
7905 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007906 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007907 }
7908 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007909 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007910
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007911 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007912 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007913 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007914
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007915 task = rpc_run_task(&task_setup_data);
7916 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007917 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007918 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007919 if (status == 0)
7920 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007921 trace_nfs4_layoutget(lgp->args.ctx,
7922 &lgp->args.range,
7923 &lgp->res.range,
7924 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007925 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7926 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007927 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007928 rpc_put_task(task);
7929 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007930 if (status)
7931 return ERR_PTR(status);
7932 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007933}
7934
Benny Halevycbe82602011-05-22 19:52:37 +03007935static void
7936nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7937{
7938 struct nfs4_layoutreturn *lrp = calldata;
7939
7940 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007941 nfs41_setup_sequence(lrp->clp->cl_session,
7942 &lrp->args.seq_args,
7943 &lrp->res.seq_res,
7944 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007945}
7946
7947static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7948{
7949 struct nfs4_layoutreturn *lrp = calldata;
7950 struct nfs_server *server;
7951
7952 dprintk("--> %s\n", __func__);
7953
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007954 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007955 return;
7956
7957 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007958 switch (task->tk_status) {
7959 default:
7960 task->tk_status = 0;
7961 case 0:
7962 break;
7963 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007964 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007965 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007966 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007967 return;
7968 }
Benny Halevycbe82602011-05-22 19:52:37 +03007969 dprintk("<-- %s\n", __func__);
7970}
7971
7972static void nfs4_layoutreturn_release(void *calldata)
7973{
7974 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007975 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02007976 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03007977
7978 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007979 spin_lock(&lo->plh_inode->i_lock);
7980 if (lrp->res.lrs_present)
7981 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebustc5d73712015-07-09 17:58:39 +02007982 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
Tom Haynesd67ae822014-12-11 17:02:04 -05007983 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04007984 lo->plh_block_lgets--;
7985 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02007986 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007987 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05007988 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03007989 kfree(calldata);
7990 dprintk("<-- %s\n", __func__);
7991}
7992
7993static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7994 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7995 .rpc_call_done = nfs4_layoutreturn_done,
7996 .rpc_release = nfs4_layoutreturn_release,
7997};
7998
Peng Tao6c166052014-11-17 09:30:40 +08007999int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008000{
8001 struct rpc_task *task;
8002 struct rpc_message msg = {
8003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8004 .rpc_argp = &lrp->args,
8005 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008006 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008007 };
8008 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008009 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008010 .rpc_message = &msg,
8011 .callback_ops = &nfs4_layoutreturn_call_ops,
8012 .callback_data = lrp,
8013 };
Peng Tao6c166052014-11-17 09:30:40 +08008014 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008015
8016 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008017 if (!sync) {
8018 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8019 if (!lrp->inode) {
8020 nfs4_layoutreturn_release(lrp);
8021 return -EAGAIN;
8022 }
8023 task_setup_data.flags |= RPC_TASK_ASYNC;
8024 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008025 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008026 task = rpc_run_task(&task_setup_data);
8027 if (IS_ERR(task))
8028 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008029 if (sync)
8030 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008031 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008032 dprintk("<-- %s status=%d\n", __func__, status);
8033 rpc_put_task(task);
8034 return status;
8035}
8036
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008037static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008038_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8039 struct pnfs_device *pdev,
8040 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008041{
8042 struct nfs4_getdeviceinfo_args args = {
8043 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008044 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8045 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008046 };
8047 struct nfs4_getdeviceinfo_res res = {
8048 .pdev = pdev,
8049 };
8050 struct rpc_message msg = {
8051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8052 .rpc_argp = &args,
8053 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008054 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008055 };
8056 int status;
8057
8058 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008059 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008060 if (res.notification & ~args.notify_types)
8061 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008062 if (res.notification != args.notify_types)
8063 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008064
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008065 dprintk("<-- %s status=%d\n", __func__, status);
8066
8067 return status;
8068}
8069
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008070int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8071 struct pnfs_device *pdev,
8072 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008073{
8074 struct nfs4_exception exception = { };
8075 int err;
8076
8077 do {
8078 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008079 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008080 &exception);
8081 } while (exception.retry);
8082 return err;
8083}
8084EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8085
Andy Adamson863a3c62011-03-23 13:27:54 +00008086static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8087{
8088 struct nfs4_layoutcommit_data *data = calldata;
8089 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008090 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008091
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008092 nfs41_setup_sequence(session,
8093 &data->args.seq_args,
8094 &data->res.seq_res,
8095 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008096}
8097
8098static void
8099nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8100{
8101 struct nfs4_layoutcommit_data *data = calldata;
8102 struct nfs_server *server = NFS_SERVER(data->args.inode);
8103
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008104 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008105 return;
8106
8107 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008108 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8109 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8110 case -NFS4ERR_BADLAYOUT: /* no layout */
8111 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008112 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008113 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008114 break;
8115 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008116 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008117 rpc_restart_call_prepare(task);
8118 return;
8119 }
8120 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008121}
8122
8123static void nfs4_layoutcommit_release(void *calldata)
8124{
8125 struct nfs4_layoutcommit_data *data = calldata;
8126
Andy Adamsondb29c082011-07-30 20:52:38 -04008127 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008128 nfs_post_op_update_inode_force_wcc(data->args.inode,
8129 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008130 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008131 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008132 kfree(data);
8133}
8134
8135static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8136 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8137 .rpc_call_done = nfs4_layoutcommit_done,
8138 .rpc_release = nfs4_layoutcommit_release,
8139};
8140
8141int
Andy Adamsonef311532011-03-12 02:58:10 -05008142nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008143{
8144 struct rpc_message msg = {
8145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8146 .rpc_argp = &data->args,
8147 .rpc_resp = &data->res,
8148 .rpc_cred = data->cred,
8149 };
8150 struct rpc_task_setup task_setup_data = {
8151 .task = &data->task,
8152 .rpc_client = NFS_CLIENT(data->args.inode),
8153 .rpc_message = &msg,
8154 .callback_ops = &nfs4_layoutcommit_ops,
8155 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008156 };
8157 struct rpc_task *task;
8158 int status = 0;
8159
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008160 dprintk("NFS: initiating layoutcommit call. sync %d "
8161 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008162 data->args.lastbytewritten,
8163 data->args.inode->i_ino);
8164
Trond Myklebust472e2592015-02-05 16:50:30 -05008165 if (!sync) {
8166 data->inode = nfs_igrab_and_active(data->args.inode);
8167 if (data->inode == NULL) {
8168 nfs4_layoutcommit_release(data);
8169 return -EAGAIN;
8170 }
8171 task_setup_data.flags = RPC_TASK_ASYNC;
8172 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008173 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008174 task = rpc_run_task(&task_setup_data);
8175 if (IS_ERR(task))
8176 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008177 if (sync)
8178 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008179 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008180 dprintk("%s: status %d\n", __func__, status);
8181 rpc_put_task(task);
8182 return status;
8183}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008184
Andy Adamson97431202013-08-08 10:57:56 -04008185/**
8186 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8187 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8188 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008189static int
8190_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008191 struct nfs_fsinfo *info,
8192 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008193{
8194 struct nfs41_secinfo_no_name_args args = {
8195 .style = SECINFO_STYLE_CURRENT_FH,
8196 };
8197 struct nfs4_secinfo_res res = {
8198 .flavors = flavors,
8199 };
8200 struct rpc_message msg = {
8201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8202 .rpc_argp = &args,
8203 .rpc_resp = &res,
8204 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008205 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008206 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008207 int status;
8208
8209 if (use_integrity) {
8210 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008211 cred = nfs4_get_clid_cred(server->nfs_client);
8212 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008213 }
8214
8215 dprintk("--> %s\n", __func__);
8216 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8217 &res.seq_res, 0);
8218 dprintk("<-- %s status=%d\n", __func__, status);
8219
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008220 if (cred)
8221 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008222
8223 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008224}
8225
8226static int
8227nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8228 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8229{
8230 struct nfs4_exception exception = { };
8231 int err;
8232 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008233 /* first try using integrity protection */
8234 err = -NFS4ERR_WRONGSEC;
8235
8236 /* try to use integrity protection with machine cred */
8237 if (_nfs4_is_integrity_protected(server->nfs_client))
8238 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8239 flavors, true);
8240
8241 /*
8242 * if unable to use integrity protection, or SECINFO with
8243 * integrity protection returns NFS4ERR_WRONGSEC (which is
8244 * disallowed by spec, but exists in deployed servers) use
8245 * the current filesystem's rpc_client and the user cred.
8246 */
8247 if (err == -NFS4ERR_WRONGSEC)
8248 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8249 flavors, false);
8250
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008251 switch (err) {
8252 case 0:
8253 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008254 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008255 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008256 default:
8257 err = nfs4_handle_exception(server, err, &exception);
8258 }
8259 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008260out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008261 return err;
8262}
8263
8264static int
8265nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8266 struct nfs_fsinfo *info)
8267{
8268 int err;
8269 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008270 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008271 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008272 struct nfs4_secinfo4 *secinfo;
8273 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008274
8275 page = alloc_page(GFP_KERNEL);
8276 if (!page) {
8277 err = -ENOMEM;
8278 goto out;
8279 }
8280
8281 flavors = page_address(page);
8282 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8283
8284 /*
8285 * Fall back on "guess and check" method if
8286 * the server doesn't support SECINFO_NO_NAME
8287 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008288 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008289 err = nfs4_find_root_sec(server, fhandle, info);
8290 goto out_freepage;
8291 }
8292 if (err)
8293 goto out_freepage;
8294
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008295 for (i = 0; i < flavors->num_flavors; i++) {
8296 secinfo = &flavors->flavors[i];
8297
8298 switch (secinfo->flavor) {
8299 case RPC_AUTH_NULL:
8300 case RPC_AUTH_UNIX:
8301 case RPC_AUTH_GSS:
8302 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8303 &secinfo->flavor_info);
8304 break;
8305 default:
8306 flavor = RPC_AUTH_MAXFLAVOR;
8307 break;
8308 }
8309
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008310 if (!nfs_auth_info_match(&server->auth_info, flavor))
8311 flavor = RPC_AUTH_MAXFLAVOR;
8312
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008313 if (flavor != RPC_AUTH_MAXFLAVOR) {
8314 err = nfs4_lookup_root_sec(server, fhandle,
8315 info, flavor);
8316 if (!err)
8317 break;
8318 }
8319 }
8320
8321 if (flavor == RPC_AUTH_MAXFLAVOR)
8322 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008323
8324out_freepage:
8325 put_page(page);
8326 if (err == -EACCES)
8327 return -EPERM;
8328out:
8329 return err;
8330}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008331
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008332static int _nfs41_test_stateid(struct nfs_server *server,
8333 nfs4_stateid *stateid,
8334 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008335{
8336 int status;
8337 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008338 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008339 };
8340 struct nfs41_test_stateid_res res;
8341 struct rpc_message msg = {
8342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8343 .rpc_argp = &args,
8344 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008345 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008346 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008347 struct rpc_clnt *rpc_client = server->client;
8348
8349 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8350 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008351
Chuck Lever38527b12012-07-11 16:30:23 -04008352 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008353 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008354 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008355 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008356 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008357 if (status != NFS_OK) {
8358 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008359 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008360 }
8361 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008362 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008363}
8364
Chuck Lever38527b12012-07-11 16:30:23 -04008365/**
8366 * nfs41_test_stateid - perform a TEST_STATEID operation
8367 *
8368 * @server: server / transport on which to perform the operation
8369 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008370 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008371 *
8372 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8373 * Otherwise a negative NFS4ERR value is returned if the operation
8374 * failed or the state ID is not currently valid.
8375 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008376static int nfs41_test_stateid(struct nfs_server *server,
8377 nfs4_stateid *stateid,
8378 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008379{
8380 struct nfs4_exception exception = { };
8381 int err;
8382 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008383 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008384 if (err != -NFS4ERR_DELAY)
8385 break;
8386 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008387 } while (exception.retry);
8388 return err;
8389}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008390
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008391struct nfs_free_stateid_data {
8392 struct nfs_server *server;
8393 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008394 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008395};
8396
8397static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8398{
8399 struct nfs_free_stateid_data *data = calldata;
8400 nfs41_setup_sequence(nfs4_get_session(data->server),
8401 &data->args.seq_args,
8402 &data->res.seq_res,
8403 task);
8404}
8405
8406static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8407{
8408 struct nfs_free_stateid_data *data = calldata;
8409
8410 nfs41_sequence_done(task, &data->res.seq_res);
8411
8412 switch (task->tk_status) {
8413 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008414 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008415 rpc_restart_call_prepare(task);
8416 }
8417}
8418
8419static void nfs41_free_stateid_release(void *calldata)
8420{
8421 kfree(calldata);
8422}
8423
Trond Myklebust17f26b12013-08-21 15:48:42 -04008424static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008425 .rpc_call_prepare = nfs41_free_stateid_prepare,
8426 .rpc_call_done = nfs41_free_stateid_done,
8427 .rpc_release = nfs41_free_stateid_release,
8428};
8429
8430static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8431 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008432 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008433 bool privileged)
8434{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008435 struct rpc_message msg = {
8436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008437 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008438 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008439 struct rpc_task_setup task_setup = {
8440 .rpc_client = server->client,
8441 .rpc_message = &msg,
8442 .callback_ops = &nfs41_free_stateid_ops,
8443 .flags = RPC_TASK_ASYNC,
8444 };
8445 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008446
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008447 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8448 &task_setup.rpc_client, &msg);
8449
Chuck Lever38527b12012-07-11 16:30:23 -04008450 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008451 data = kmalloc(sizeof(*data), GFP_NOFS);
8452 if (!data)
8453 return ERR_PTR(-ENOMEM);
8454 data->server = server;
8455 nfs4_stateid_copy(&data->args.stateid, stateid);
8456
8457 task_setup.callback_data = data;
8458
8459 msg.rpc_argp = &data->args;
8460 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008461 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008462 if (privileged)
8463 nfs4_set_sequence_privileged(&data->args.seq_args);
8464
8465 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008466}
8467
Chuck Lever38527b12012-07-11 16:30:23 -04008468/**
8469 * nfs41_free_stateid - perform a FREE_STATEID operation
8470 *
8471 * @server: server / transport on which to perform the operation
8472 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008473 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008474 *
8475 * Returns NFS_OK if the server freed "stateid". Otherwise a
8476 * negative NFS4ERR value is returned.
8477 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008478static int nfs41_free_stateid(struct nfs_server *server,
8479 nfs4_stateid *stateid,
8480 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008481{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008482 struct rpc_task *task;
8483 int ret;
8484
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008485 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008486 if (IS_ERR(task))
8487 return PTR_ERR(task);
8488 ret = rpc_wait_for_completion_task(task);
8489 if (!ret)
8490 ret = task->tk_status;
8491 rpc_put_task(task);
8492 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008493}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008494
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008495static void
8496nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008497{
8498 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008499 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008500
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008501 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008502 nfs4_free_lock_state(server, lsp);
8503 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008504 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008505 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008506}
8507
Trond Myklebust36281ca2012-03-04 18:13:56 -05008508static bool nfs41_match_stateid(const nfs4_stateid *s1,
8509 const nfs4_stateid *s2)
8510{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008511 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008512 return false;
8513
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008514 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008515 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008516 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008517 return true;
8518
8519 return false;
8520}
8521
Andy Adamson557134a2009-04-01 09:21:53 -04008522#endif /* CONFIG_NFS_V4_1 */
8523
Trond Myklebust36281ca2012-03-04 18:13:56 -05008524static bool nfs4_match_stateid(const nfs4_stateid *s1,
8525 const nfs4_stateid *s2)
8526{
Trond Myklebustf597c532012-03-04 18:13:56 -05008527 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008528}
8529
8530
Trond Myklebust17280172012-03-11 13:11:00 -04008531static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008532 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008533 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534 .recover_open = nfs4_open_reclaim,
8535 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008536 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008537 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538};
8539
Andy Adamson591d71c2009-04-01 09:22:47 -04008540#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008541static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008542 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8543 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8544 .recover_open = nfs4_open_reclaim,
8545 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008546 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008547 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008548 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008549};
8550#endif /* CONFIG_NFS_V4_1 */
8551
Trond Myklebust17280172012-03-11 13:11:00 -04008552static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008553 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008554 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008555 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008557 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558};
8559
Andy Adamson591d71c2009-04-01 09:22:47 -04008560#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008561static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008562 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8563 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008564 .recover_open = nfs41_open_expired,
8565 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008566 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008567};
8568#endif /* CONFIG_NFS_V4_1 */
8569
Trond Myklebust17280172012-03-11 13:11:00 -04008570static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008571 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008572 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008573 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008574};
8575
8576#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008577static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008578 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008579 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008580 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008581};
8582#endif
8583
Chuck Leverec011fe2013-10-17 14:12:39 -04008584static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008585 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008586 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008587};
8588
8589#if defined(CONFIG_NFS_V4_1)
8590static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008591 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008592 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008593};
8594#endif /* CONFIG_NFS_V4_1 */
8595
Trond Myklebust97dc1352010-06-16 09:52:26 -04008596static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8597 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008598 .init_caps = NFS_CAP_READDIRPLUS
8599 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008600 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008601 .init_client = nfs40_init_client,
8602 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008603 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008604 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008605 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008606 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008607 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008608 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8609 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8610 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008611 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008612};
8613
8614#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008615static struct nfs_seqid *
8616nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8617{
8618 return NULL;
8619}
8620
Trond Myklebust97dc1352010-06-16 09:52:26 -04008621static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8622 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008623 .init_caps = NFS_CAP_READDIRPLUS
8624 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008625 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008626 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008627 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008628 .init_client = nfs41_init_client,
8629 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008630 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008631 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008632 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008633 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008634 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008635 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8636 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8637 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008638 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008639};
8640#endif
8641
Steve Dickson42c2c422013-05-22 12:50:38 -04008642#if defined(CONFIG_NFS_V4_2)
8643static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8644 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008645 .init_caps = NFS_CAP_READDIRPLUS
8646 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008647 | NFS_CAP_POSIX_LOCK
8648 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008649 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008650 | NFS_CAP_ALLOCATE
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008651 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008652 | NFS_CAP_SEEK
8653 | NFS_CAP_LAYOUTSTATS,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008654 .init_client = nfs41_init_client,
8655 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008656 .match_stateid = nfs41_match_stateid,
8657 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008658 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008659 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008660 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008661 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8662 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8663 .state_renewal_ops = &nfs41_state_renewal_ops,
8664};
8665#endif
8666
Trond Myklebust97dc1352010-06-16 09:52:26 -04008667const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8668 [0] = &nfs_v4_0_minor_ops,
8669#if defined(CONFIG_NFS_V4_1)
8670 [1] = &nfs_v4_1_minor_ops,
8671#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008672#if defined(CONFIG_NFS_V4_2)
8673 [2] = &nfs_v4_2_minor_ops,
8674#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008675};
8676
Trond Myklebust17f26b12013-08-21 15:48:42 -04008677static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008678 .create = nfs_create,
8679 .lookup = nfs_lookup,
8680 .atomic_open = nfs_atomic_open,
8681 .link = nfs_link,
8682 .unlink = nfs_unlink,
8683 .symlink = nfs_symlink,
8684 .mkdir = nfs_mkdir,
8685 .rmdir = nfs_rmdir,
8686 .mknod = nfs_mknod,
8687 .rename = nfs_rename,
8688 .permission = nfs_permission,
8689 .getattr = nfs_getattr,
8690 .setattr = nfs_setattr,
8691 .getxattr = generic_getxattr,
8692 .setxattr = generic_setxattr,
8693 .listxattr = generic_listxattr,
8694 .removexattr = generic_removexattr,
8695};
8696
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008697static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008698 .permission = nfs_permission,
8699 .getattr = nfs_getattr,
8700 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008701 .getxattr = generic_getxattr,
8702 .setxattr = generic_setxattr,
8703 .listxattr = generic_listxattr,
8704 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008705};
8706
David Howells509de812006-08-22 20:06:11 -04008707const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008708 .version = 4, /* protocol version */
8709 .dentry_ops = &nfs4_dentry_operations,
8710 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008711 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008712 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008713 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008714 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008715 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716 .getattr = nfs4_proc_getattr,
8717 .setattr = nfs4_proc_setattr,
8718 .lookup = nfs4_proc_lookup,
8719 .access = nfs4_proc_access,
8720 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 .create = nfs4_proc_create,
8722 .remove = nfs4_proc_remove,
8723 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008724 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008726 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008727 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008728 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008729 .link = nfs4_proc_link,
8730 .symlink = nfs4_proc_symlink,
8731 .mkdir = nfs4_proc_mkdir,
8732 .rmdir = nfs4_proc_remove,
8733 .readdir = nfs4_proc_readdir,
8734 .mknod = nfs4_proc_mknod,
8735 .statfs = nfs4_proc_statfs,
8736 .fsinfo = nfs4_proc_fsinfo,
8737 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008738 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008740 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008742 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008744 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008746 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008747 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008749 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008750 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008751 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008752 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008753 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008754 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008755 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008756 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008757 .create_server = nfs4_create_server,
8758 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759};
8760
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008761static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8762 .prefix = XATTR_NAME_NFSV4_ACL,
8763 .list = nfs4_xattr_list_nfs4_acl,
8764 .get = nfs4_xattr_get_nfs4_acl,
8765 .set = nfs4_xattr_set_nfs4_acl,
8766};
8767
8768const struct xattr_handler *nfs4_xattr_handlers[] = {
8769 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008770#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8771 &nfs4_xattr_nfs4_label_handler,
8772#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008773 NULL
8774};
8775
Linus Torvalds1da177e2005-04-16 15:20:36 -07008776/*
8777 * Local variables:
8778 * c-basic-offset: 8
8779 * End:
8780 */