blob: 5b2b07bca490d51571da00139f0c8a18f1fd473e [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
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static 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 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400102static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Trond Myklebust26d36302016-09-22 13:39:05 -0400331static void nfs4_test_and_free_stateid(struct nfs_server *server,
332 nfs4_stateid *stateid,
333 struct rpc_cred *cred)
334{
335 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
336
337 ops->test_and_free_expired(server, stateid, cred);
338}
339
340static void __nfs4_free_revoked_stateid(struct nfs_server *server,
341 nfs4_stateid *stateid,
342 struct rpc_cred *cred)
343{
344 stateid->type = NFS4_REVOKED_STATEID_TYPE;
345 nfs4_test_and_free_stateid(server, stateid, cred);
346}
347
348static void nfs4_free_revoked_stateid(struct nfs_server *server,
349 const nfs4_stateid *stateid,
350 struct rpc_cred *cred)
351{
352 nfs4_stateid tmp;
353
354 nfs4_stateid_copy(&tmp, stateid);
355 __nfs4_free_revoked_stateid(server, &tmp, cred);
356}
357
NeilBrown8478eaa2014-09-18 16:09:27 +1000358static long nfs4_update_delay(long *timeout)
359{
360 long ret;
361 if (!timeout)
362 return NFS4_POLL_RETRY_MAX;
363 if (*timeout <= 0)
364 *timeout = NFS4_POLL_RETRY_MIN;
365 if (*timeout > NFS4_POLL_RETRY_MAX)
366 *timeout = NFS4_POLL_RETRY_MAX;
367 ret = *timeout;
368 *timeout <<= 1;
369 return ret;
370}
371
Trond Myklebust65de8722008-12-23 15:21:44 -0500372static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
373{
374 int res = 0;
375
376 might_sleep();
377
NeilBrown8478eaa2014-09-18 16:09:27 +1000378 freezable_schedule_timeout_killable_unsafe(
379 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 if (fatal_signal_pending(current))
381 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 return res;
383}
384
385/* This is the error handling routine for processes that are allowed
386 * to sleep.
387 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400388static int nfs4_do_handle_exception(struct nfs_server *server,
389 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500390{
391 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500392 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400393 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500394 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 int ret = errorcode;
396
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400397 exception->delay = 0;
398 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400400
401 if (stateid == NULL && state != NULL)
402 stateid = &state->stateid;
403
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 switch(errorcode) {
405 case 0:
406 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400407 case -NFS4ERR_DELEG_REVOKED:
408 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400410 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400411 if (inode != NULL && stateid != NULL) {
412 nfs_inode_find_state_and_recover(inode,
413 stateid);
414 goto wait_on_recovery;
415 }
416 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400417 if (inode) {
418 int err;
419
420 err = nfs_async_inode_return_delegation(inode,
421 stateid);
422 if (err == 0)
423 goto wait_on_recovery;
424 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
425 exception->retry = 1;
426 break;
427 }
428 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500429 if (state == NULL)
430 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400431 ret = nfs4_schedule_stateid_recovery(server, state);
432 if (ret < 0)
433 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500434 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500436 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500437 nfs4_schedule_lease_recovery(clp);
438 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400439 case -NFS4ERR_MOVED:
440 ret = nfs4_schedule_migration_recovery(server);
441 if (ret < 0)
442 break;
443 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400444 case -NFS4ERR_LEASE_MOVED:
445 nfs4_schedule_lease_moved_recovery(clp);
446 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500447#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400448 case -NFS4ERR_BADSESSION:
449 case -NFS4ERR_BADSLOT:
450 case -NFS4ERR_BAD_HIGH_SLOT:
451 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
452 case -NFS4ERR_DEADSESSION:
453 case -NFS4ERR_SEQ_FALSE_RETRY:
454 case -NFS4ERR_SEQ_MISORDERED:
455 dprintk("%s ERROR: %d Reset session\n", __func__,
456 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400457 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400458 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500459#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500460 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500461 if (exception->timeout > HZ) {
462 /* We have retried a decent amount, time to
463 * fail
464 */
465 ret = -EBUSY;
466 break;
467 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400469 nfs_inc_server_stats(server, NFSIOS_DELAY);
470 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400471 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400472 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400473 exception->delay = 1;
474 return 0;
475
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400476 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500477 case -NFS4ERR_OLD_STATEID:
478 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800479 break;
480 case -NFS4ERR_BADOWNER:
481 /* The following works around a Linux server bug! */
482 case -NFS4ERR_BADNAME:
483 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
484 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
485 exception->retry = 1;
486 printk(KERN_WARNING "NFS: v4 server %s "
487 "does not accept raw "
488 "uid/gids. "
489 "Reenabling the idmapper.\n",
490 server->nfs_client->cl_hostname);
491 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500492 }
493 /* We failed to handle the error */
494 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500495wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400496 exception->recovering = 1;
497 return 0;
498}
499
500/* This is the error handling routine for processes that are allowed
501 * to sleep.
502 */
503int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
504{
505 struct nfs_client *clp = server->nfs_client;
506 int ret;
507
508 ret = nfs4_do_handle_exception(server, errorcode, exception);
509 if (exception->delay) {
510 ret = nfs4_delay(server->client, &exception->timeout);
511 goto out_retry;
512 }
513 if (exception->recovering) {
514 ret = nfs4_wait_clnt_recover(clp);
515 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
516 return -EIO;
517 goto out_retry;
518 }
519 return ret;
520out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500521 if (ret == 0)
522 exception->retry = 1;
523 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500524}
525
Trond Myklebust037fc982015-09-20 15:51:00 -0400526static int
527nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
528 int errorcode, struct nfs4_exception *exception)
529{
530 struct nfs_client *clp = server->nfs_client;
531 int ret;
532
533 ret = nfs4_do_handle_exception(server, errorcode, exception);
534 if (exception->delay) {
535 rpc_delay(task, nfs4_update_delay(&exception->timeout));
536 goto out_retry;
537 }
538 if (exception->recovering) {
539 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
540 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
541 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
542 goto out_retry;
543 }
544 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
545 ret = -EIO;
546 return ret;
547out_retry:
548 if (ret == 0)
549 exception->retry = 1;
550 return ret;
551}
552
553static int
554nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
555 struct nfs4_state *state, long *timeout)
556{
557 struct nfs4_exception exception = {
558 .state = state,
559 };
560
561 if (task->tk_status >= 0)
562 return 0;
563 if (timeout)
564 exception.timeout = *timeout;
565 task->tk_status = nfs4_async_handle_exception(task, server,
566 task->tk_status,
567 &exception);
568 if (exception.delay && timeout)
569 *timeout = exception.timeout;
570 if (exception.retry)
571 return -EAGAIN;
572 return 0;
573}
574
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400575/*
576 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
577 * or 'false' otherwise.
578 */
579static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
580{
581 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
582
583 if (flavor == RPC_AUTH_GSS_KRB5I ||
584 flavor == RPC_AUTH_GSS_KRB5P)
585 return true;
586
587 return false;
588}
Trond Myklebust65de8722008-12-23 15:21:44 -0500589
Trond Myklebust452e9352010-07-31 14:29:06 -0400590static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock(&clp->cl_lock);
593 if (time_before(clp->cl_last_renewal,timestamp))
594 clp->cl_last_renewal = timestamp;
595 spin_unlock(&clp->cl_lock);
596}
597
Trond Myklebust452e9352010-07-31 14:29:06 -0400598static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
599{
Trond Myklebustbe824162015-07-05 14:50:46 -0400600 struct nfs_client *clp = server->nfs_client;
601
602 if (!nfs4_has_session(clp))
603 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400604}
605
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400606struct nfs4_call_sync_data {
607 const struct nfs_server *seq_server;
608 struct nfs4_sequence_args *seq_args;
609 struct nfs4_sequence_res *seq_res;
610};
611
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800612void nfs4_init_sequence(struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400614{
615 args->sa_slot = NULL;
616 args->sa_cache_this = cache_reply;
617 args->sa_privileged = 0;
618
619 res->sr_slot = NULL;
620}
621
622static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
623{
624 args->sa_privileged = 1;
625}
626
Peng Taocb04ad22014-06-11 05:24:15 +0800627int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
628 struct nfs4_sequence_args *args,
629 struct nfs4_sequence_res *res,
630 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400631{
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 struct nfs4_slot *slot;
633
634 /* slot already allocated? */
635 if (res->sr_slot != NULL)
636 goto out_start;
637
638 spin_lock(&tbl->slot_tbl_lock);
639 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
640 goto out_sleep;
641
642 slot = nfs4_alloc_slot(tbl);
643 if (IS_ERR(slot)) {
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
646 goto out_sleep;
647 }
648 spin_unlock(&tbl->slot_tbl_lock);
649
Trond Myklebust0a014a42016-09-22 13:38:51 -0400650 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400651 args->sa_slot = slot;
652 res->sr_slot = slot;
653
654out_start:
655 rpc_call_start(task);
656 return 0;
657
658out_sleep:
659 if (args->sa_privileged)
660 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
661 NULL, RPC_PRIORITY_PRIVILEGED);
662 else
663 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
664 spin_unlock(&tbl->slot_tbl_lock);
665 return -EAGAIN;
666}
Peng Taocb04ad22014-06-11 05:24:15 +0800667EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400668
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400669static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400670{
671 struct nfs4_slot *slot = res->sr_slot;
672 struct nfs4_slot_table *tbl;
673
Chuck Lever3bd23842013-08-09 12:49:19 -0400674 tbl = slot->table;
675 spin_lock(&tbl->slot_tbl_lock);
676 if (!nfs41_wake_and_assign_slot(tbl, slot))
677 nfs4_free_slot(tbl, slot);
678 spin_unlock(&tbl->slot_tbl_lock);
679
680 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400681}
682
683static int nfs40_sequence_done(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
685{
686 if (res->sr_slot != NULL)
687 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400688 return 1;
689}
690
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400691#if defined(CONFIG_NFS_V4_1)
692
Trond Myklebustd185a332010-06-16 09:52:25 -0400693static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400694{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500695 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400696 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500697 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500698 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400699
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500701 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500702
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400703 /* Bump the slot sequence number */
704 if (slot->seq_done)
705 slot->seq_nr++;
706 slot->seq_done = 0;
707
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500708 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500709 /* Be nice to the server: try to ensure that the last transmitted
710 * value for highest_user_slotid <= target_highest_slotid
711 */
712 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
713 send_new_highest_used_slotid = true;
714
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500716 send_new_highest_used_slotid = false;
717 goto out_unlock;
718 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500719 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500720
721 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
722 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500723out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500724 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400725 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500726 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400727 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400728 if (waitqueue_active(&tbl->slot_waitq))
729 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400730}
731
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400732static int nfs41_sequence_process(struct rpc_task *task,
733 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400734{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500735 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500736 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400737 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500738 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500739 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400740
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500741 if (slot == NULL)
742 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400743 /* don't increment the sequence number if the task wasn't sent */
744 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400745 goto out;
746
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500747 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500748
Trond Myklebustac20d162012-12-15 15:36:07 -0500749 if (slot->interrupted) {
750 slot->interrupted = 0;
751 interrupted = true;
752 }
753
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400754 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500755 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400756 switch (res->sr_status) {
757 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400758 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400759 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500760 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500761 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500762 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400763 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
764 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500765 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400766 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500767 case 1:
768 /*
769 * sr_status remains 1 if an RPC level error occurred.
770 * The server may or may not have processed the sequence
771 * operation..
772 * Mark the slot as having hosted an interrupted RPC call.
773 */
774 slot->interrupted = 1;
775 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 case -NFS4ERR_DELAY:
777 /* The server detected a resend of the RPC call and
778 * returned NFS4ERR_DELAY as per Section 2.10.6.2
779 * of RFC5661.
780 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500781 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400782 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500783 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500784 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400785 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500786 case -NFS4ERR_BADSLOT:
787 /*
788 * The slot id we used was probably retired. Try again
789 * using a different slot id.
790 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500791 goto retry_nowait;
792 case -NFS4ERR_SEQ_MISORDERED:
793 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500794 * Was the last operation on this sequence interrupted?
795 * If so, retry after bumping the sequence number.
796 */
797 if (interrupted) {
798 ++slot->seq_nr;
799 goto retry_nowait;
800 }
801 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500802 * Could this slot have been previously retired?
803 * If so, then the server may be expecting seq_nr = 1!
804 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500805 if (slot->seq_nr != 1) {
806 slot->seq_nr = 1;
807 goto retry_nowait;
808 }
809 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500810 case -NFS4ERR_SEQ_FALSE_RETRY:
811 ++slot->seq_nr;
812 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400813 default:
814 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400815 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400816 }
817out:
818 /* The session may be reset by one of the error handlers. */
819 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500820out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500821 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500822retry_nowait:
823 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400824 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500825 task->tk_status = 0;
826 ret = 0;
827 }
828 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400829out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400830 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 goto out;
832 rpc_delay(task, NFS4_POLL_RETRY_MAX);
833 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400834}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400835
836int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
837{
838 if (!nfs41_sequence_process(task, res))
839 return 0;
840 if (res->sr_slot != NULL)
841 nfs41_sequence_free_slot(res);
842 return 1;
843
844}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500845EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400846
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400847static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
848{
849 if (res->sr_slot == NULL)
850 return 1;
851 if (res->sr_slot->table->session != NULL)
852 return nfs41_sequence_process(task, res);
853 return nfs40_sequence_done(task, res);
854}
855
856static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
857{
858 if (res->sr_slot != NULL) {
859 if (res->sr_slot->table->session != NULL)
860 nfs41_sequence_free_slot(res);
861 else
862 nfs40_sequence_free_slot(res);
863 }
864}
865
Peng Tao2c4b1312014-06-11 05:24:16 +0800866int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400867{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500868 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400869 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400870 if (!res->sr_slot->table->session)
871 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400872 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400873}
Peng Tao2c4b1312014-06-11 05:24:16 +0800874EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400875
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000876int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400877 struct nfs4_sequence_args *args,
878 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400879 struct rpc_task *task)
880{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400881 struct nfs4_slot *slot;
882 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400883
884 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400885 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400886 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400887 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400888
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400889 tbl = &session->fc_slot_table;
890
Trond Myklebust69d206b2012-11-22 13:21:02 -0500891 task->tk_timeout = 0;
892
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400894 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400895 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500896 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500897 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400898 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400899 }
900
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500901 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500902 if (IS_ERR(slot)) {
903 /* If out of memory, try again in 1/4 second */
904 if (slot == ERR_PTR(-ENOMEM))
905 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400906 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400907 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400908 }
909 spin_unlock(&tbl->slot_tbl_lock);
910
Trond Myklebust0a014a42016-09-22 13:38:51 -0400911 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500912 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400913
Chuck Levere8d92382013-08-09 12:47:51 -0400914 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500915 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916
Benny Halevydfb4f3092010-09-24 09:17:01 -0400917 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500918 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400919 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400920 /*
921 * sr_status is only set in decode_sequence, and so will remain
922 * set to 1 if an rpc level failure occurs.
923 */
924 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400925 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400926out_success:
927 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400928 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400929out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400930 /* Privileged tasks are queued with top priority */
931 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400932 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
933 NULL, RPC_PRIORITY_PRIVILEGED);
934 else
935 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400936 spin_unlock(&tbl->slot_tbl_lock);
937 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000939EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400940
Chuck Lever5a580e02013-08-09 12:48:09 -0400941static int nfs4_setup_sequence(const struct nfs_server *server,
942 struct nfs4_sequence_args *args,
943 struct nfs4_sequence_res *res,
944 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400945{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400946 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947 int ret = 0;
948
Chuck Lever3bd23842013-08-09 12:49:19 -0400949 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800950 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
951 args, res, task);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400952
Chuck Levere8d92382013-08-09 12:47:51 -0400953 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400954 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400955 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400956
Trond Myklebust9d12b212012-01-17 22:04:25 -0500957 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400958
Andy Adamsonce5039c2009-04-01 09:22:13 -0400959 dprintk("<-- %s status=%d\n", __func__, ret);
960 return ret;
961}
962
Andy Adamsonce5039c2009-04-01 09:22:13 -0400963static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
964{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400965 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400966 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400967
Trond Myklebust035168ab2010-06-16 09:52:26 -0400968 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
969
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400970 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400971}
972
Andy Adamson69ab40c2009-04-01 09:22:19 -0400973static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400976
Trond Myklebust14516c32010-07-31 14:29:06 -0400977 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400978}
979
Trond Myklebust17280172012-03-11 13:11:00 -0400980static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400982 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400983};
984
Chuck Lever3bd23842013-08-09 12:49:19 -0400985#else /* !CONFIG_NFS_V4_1 */
986
Chuck Lever5a580e02013-08-09 12:48:09 -0400987static int nfs4_setup_sequence(const struct nfs_server *server,
988 struct nfs4_sequence_args *args,
989 struct nfs4_sequence_res *res,
990 struct rpc_task *task)
991{
Peng Taocb04ad22014-06-11 05:24:15 +0800992 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
993 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400994}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400995
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400996static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
997{
998 return nfs40_sequence_done(task, res);
999}
1000
1001static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1002{
1003 if (res->sr_slot != NULL)
1004 nfs40_sequence_free_slot(res);
1005}
1006
Peng Tao2c4b1312014-06-11 05:24:16 +08001007int nfs4_sequence_done(struct rpc_task *task,
1008 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001009{
Chuck Lever3bd23842013-08-09 12:49:19 -04001010 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001011}
Peng Tao2c4b1312014-06-11 05:24:16 +08001012EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001013
1014#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001015
Chuck Lever9915ea72013-08-09 12:48:27 -04001016static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1017{
1018 struct nfs4_call_sync_data *data = calldata;
1019 nfs4_setup_sequence(data->seq_server,
1020 data->seq_args, data->seq_res, task);
1021}
1022
1023static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1024{
1025 struct nfs4_call_sync_data *data = calldata;
1026 nfs4_sequence_done(task, data->seq_res);
1027}
1028
1029static const struct rpc_call_ops nfs40_call_sync_ops = {
1030 .rpc_call_prepare = nfs40_call_sync_prepare,
1031 .rpc_call_done = nfs40_call_sync_done,
1032};
1033
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001034static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001035 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001039{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001040 int ret;
1041 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001042 struct nfs_client *clp = server->nfs_client;
1043 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001044 .seq_server = server,
1045 .seq_args = args,
1046 .seq_res = res,
1047 };
1048 struct rpc_task_setup task_setup = {
1049 .rpc_client = clnt,
1050 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001051 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 .callback_data = &data
1053 };
1054
1055 task = rpc_run_task(&task_setup);
1056 if (IS_ERR(task))
1057 ret = PTR_ERR(task);
1058 else {
1059 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001060 rpc_put_task(task);
1061 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 return ret;
1063}
1064
Bryan Schumaker7c513052011-03-24 17:12:24 +00001065int nfs4_call_sync(struct rpc_clnt *clnt,
1066 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001067 struct rpc_message *msg,
1068 struct nfs4_sequence_args *args,
1069 struct nfs4_sequence_res *res,
1070 int cache_reply)
1071{
Chuck Levera9c92d62013-08-09 12:48:18 -04001072 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001073 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1077{
1078 struct nfs_inode *nfsi = NFS_I(dir);
1079
1080 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001081 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001082 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001084 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001085 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001086 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 spin_unlock(&dir->i_lock);
1088}
1089
1090struct nfs4_opendata {
1091 struct kref kref;
1092 struct nfs_openargs o_arg;
1093 struct nfs_openres o_res;
1094 struct nfs_open_confirmargs c_arg;
1095 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001096 struct nfs4_string owner_name;
1097 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001098 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001100 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001102 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct nfs4_state_owner *owner;
1104 struct nfs4_state *state;
1105 struct iattr attrs;
1106 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001107 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001108 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001109 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001110 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 int cancelled;
1112};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001113
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001114static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1115 int err, struct nfs4_exception *exception)
1116{
1117 if (err != -EINVAL)
1118 return false;
1119 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1120 return false;
1121 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1122 exception->retry = 1;
1123 return true;
1124}
1125
Trond Myklebust6ae37332015-01-30 14:21:14 -05001126static u32
1127nfs4_map_atomic_open_share(struct nfs_server *server,
1128 fmode_t fmode, int openflags)
1129{
1130 u32 res = 0;
1131
1132 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1133 case FMODE_READ:
1134 res = NFS4_SHARE_ACCESS_READ;
1135 break;
1136 case FMODE_WRITE:
1137 res = NFS4_SHARE_ACCESS_WRITE;
1138 break;
1139 case FMODE_READ|FMODE_WRITE:
1140 res = NFS4_SHARE_ACCESS_BOTH;
1141 }
1142 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1143 goto out;
1144 /* Want no delegation if we're using O_DIRECT */
1145 if (openflags & O_DIRECT)
1146 res |= NFS4_SHARE_WANT_NO_DELEG;
1147out:
1148 return res;
1149}
1150
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001151static enum open_claim_type4
1152nfs4_map_atomic_open_claim(struct nfs_server *server,
1153 enum open_claim_type4 claim)
1154{
1155 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1156 return claim;
1157 switch (claim) {
1158 default:
1159 return claim;
1160 case NFS4_OPEN_CLAIM_FH:
1161 return NFS4_OPEN_CLAIM_NULL;
1162 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1163 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1164 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1165 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1166 }
1167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170{
1171 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001172 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173 p->o_res.seqid = p->o_arg.seqid;
1174 p->c_res.seqid = p->c_arg.seqid;
1175 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001176 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001177 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001178 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001179}
1180
Al Viro82a2c1b2011-06-22 18:30:55 -04001181static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001182 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001183 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001184 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001185 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001186 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187{
Al Viro82a2c1b2011-06-22 18:30:55 -04001188 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001189 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001190 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001191 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 struct nfs4_opendata *p;
1193
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001194 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 if (p == NULL)
1196 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001197
1198 p->f_label = nfs4_label_alloc(server, gfp_mask);
1199 if (IS_ERR(p->f_label))
1200 goto err_free_p;
1201
Kinglong Meea49c2692015-07-27 15:31:38 +08001202 p->a_label = nfs4_label_alloc(server, gfp_mask);
1203 if (IS_ERR(p->a_label))
1204 goto err_free_f;
1205
Trond Myklebust63f5f792015-01-23 19:19:25 -05001206 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1207 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001208 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001209 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001210 nfs_sb_active(dentry->d_sb);
1211 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001212 p->dir = parent;
1213 p->owner = sp;
1214 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 p->o_arg.open_flags = flags;
1216 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001217 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1218 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001219 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1220 * will return permission denied for all bits until close */
1221 if (!(flags & O_EXCL)) {
1222 /* ask server to check for all possible rights as results
1223 * are cached */
1224 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1225 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001226 }
David Howells7539bba2006-08-22 20:06:09 -04001227 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001228 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1229 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001230 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001231 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001232 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001233 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001234 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001235 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1236 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001237 case NFS4_OPEN_CLAIM_NULL:
1238 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1239 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1240 p->o_arg.fh = NFS_FH(dir);
1241 break;
1242 case NFS4_OPEN_CLAIM_PREVIOUS:
1243 case NFS4_OPEN_CLAIM_FH:
1244 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1245 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001246 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001247 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001248 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001249 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001250
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001251 p->o_arg.u.attrs = &p->attrs;
1252 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001253
1254 verf[0] = jiffies;
1255 verf[1] = current->pid;
1256 memcpy(p->o_arg.u.verifier.data, verf,
1257 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001258 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001259 p->c_arg.fh = &p->o_res.fh;
1260 p->c_arg.stateid = &p->o_res.stateid;
1261 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001262 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001264 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001265
1266err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001267 nfs4_label_free(p->a_label);
1268err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001269 nfs4_label_free(p->f_label);
1270err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 kfree(p);
1272err:
1273 dput(parent);
1274 return NULL;
1275}
1276
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001279 struct nfs4_opendata *p = container_of(kref,
1280 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001281 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001282
1283 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001284 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001285 if (p->state != NULL)
1286 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001287 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001288
Kinglong Meea49c2692015-07-27 15:31:38 +08001289 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001290 nfs4_label_free(p->f_label);
1291
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001293 dput(p->dentry);
1294 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001295 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001296 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001297 kfree(p);
1298}
1299
1300static void nfs4_opendata_put(struct nfs4_opendata *p)
1301{
1302 if (p != NULL)
1303 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304}
1305
Trond Myklebust06f814a2006-01-03 09:55:07 +01001306static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1307{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001308 int ret;
1309
Trond Myklebust06f814a2006-01-03 09:55:07 +01001310 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001311 return ret;
1312}
1313
Trond Myklebust24311f82015-09-20 10:50:17 -04001314static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1315 fmode_t fmode)
1316{
1317 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1318 case FMODE_READ|FMODE_WRITE:
1319 return state->n_rdwr != 0;
1320 case FMODE_WRITE:
1321 return state->n_wronly != 0;
1322 case FMODE_READ:
1323 return state->n_rdonly != 0;
1324 }
1325 WARN_ON_ONCE(1);
1326 return false;
1327}
1328
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001329static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001330{
1331 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001332
Trond Myklebust536e43d2012-01-17 22:04:26 -05001333 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334 goto out;
1335 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001336 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001337 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1338 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001339 break;
1340 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001341 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1342 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001343 break;
1344 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001345 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1346 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001347 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001348out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001349 return ret;
1350}
1351
Trond Myklebust2a606182015-08-19 22:30:00 -05001352static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1353 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001354{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001355 if (delegation == NULL)
1356 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001358 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001359 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1360 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001361 switch (claim) {
1362 case NFS4_OPEN_CLAIM_NULL:
1363 case NFS4_OPEN_CLAIM_FH:
1364 break;
1365 case NFS4_OPEN_CLAIM_PREVIOUS:
1366 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1367 break;
1368 default:
1369 return 0;
1370 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001371 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001372 return 1;
1373}
1374
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001375static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001376{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001377 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001378 case FMODE_WRITE:
1379 state->n_wronly++;
1380 break;
1381 case FMODE_READ:
1382 state->n_rdonly++;
1383 break;
1384 case FMODE_READ|FMODE_WRITE:
1385 state->n_rdwr++;
1386 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001388}
1389
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001390#ifdef CONFIG_NFS_V4_1
1391static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1392{
1393 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1394 return true;
1395 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1396 return true;
1397 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1398 return true;
1399 return false;
1400}
1401#endif /* CONFIG_NFS_V4_1 */
1402
Trond Myklebust4f14c192014-02-12 19:15:06 -05001403static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001404{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001405 struct nfs_client *clp = state->owner->so_server->nfs_client;
1406 bool need_recover = false;
1407
1408 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1409 need_recover = true;
1410 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1411 need_recover = true;
1412 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1413 need_recover = true;
1414 if (need_recover)
1415 nfs4_state_mark_reclaim_nograce(clp, state);
1416}
1417
Trond Myklebuste999e802014-02-10 18:20:47 -05001418static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001419 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001420{
1421 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1422 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001423 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001424 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001425 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001426 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001427 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001428 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1429 return true;
1430 return false;
1431}
1432
Trond Myklebustf95549c2015-01-23 18:06:09 -05001433static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1434{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001435 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1436 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001437 if (state->n_wronly)
1438 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1439 if (state->n_rdonly)
1440 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1441 if (state->n_rdwr)
1442 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001443 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001444}
1445
Trond Myklebust226056c2014-02-11 10:41:07 -05001446static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001447 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001448 nfs4_stateid *stateid, fmode_t fmode)
1449{
1450 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1451 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1452 case FMODE_WRITE:
1453 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1454 break;
1455 case FMODE_READ:
1456 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1457 break;
1458 case 0:
1459 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1460 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1461 clear_bit(NFS_OPEN_STATE, &state->flags);
1462 }
1463 if (stateid == NULL)
1464 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001465 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001466 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1467 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1468 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001469 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001470 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001471 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001472 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001473 nfs4_stateid_copy(&state->stateid, stateid);
1474 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001475}
1476
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001477static void nfs_clear_open_stateid(struct nfs4_state *state,
1478 nfs4_stateid *arg_stateid,
1479 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001480{
1481 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001482 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001483 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001484 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1485 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001486}
1487
Trond Myklebust1393d962016-09-22 13:39:13 -04001488static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1489 const nfs4_stateid *stateid, fmode_t fmode,
1490 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001491{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001492 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001493 case FMODE_READ:
1494 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1495 break;
1496 case FMODE_WRITE:
1497 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1498 break;
1499 case FMODE_READ|FMODE_WRITE:
1500 set_bit(NFS_O_RDWR_STATE, &state->flags);
1501 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001502 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001503 return;
1504 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1505 nfs4_stateid_copy(&state->stateid, stateid);
1506 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001507}
1508
Trond Myklebust1393d962016-09-22 13:39:13 -04001509static void __update_open_stateid(struct nfs4_state *state,
1510 const nfs4_stateid *open_stateid,
1511 const nfs4_stateid *deleg_stateid,
1512 fmode_t fmode,
1513 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001515 /*
1516 * Protect the call to nfs4_state_set_mode_locked and
1517 * serialise the stateid update
1518 */
Andrew Elble361cad32015-12-02 09:20:57 -05001519 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001520 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001521 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001522 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001523 set_bit(NFS_DELEGATED_STATE, &state->flags);
1524 }
1525 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001526 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001527 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001528 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001529 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Trond Myklebust1393d962016-09-22 13:39:13 -04001532static int update_open_stateid(struct nfs4_state *state,
1533 const nfs4_stateid *open_stateid,
1534 const nfs4_stateid *delegation,
1535 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001536{
Trond Myklebust1393d962016-09-22 13:39:13 -04001537 struct nfs_server *server = NFS_SERVER(state->inode);
1538 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001539 struct nfs_inode *nfsi = NFS_I(state->inode);
1540 struct nfs_delegation *deleg_cur;
Trond Myklebust1393d962016-09-22 13:39:13 -04001541 nfs4_stateid freeme = {0};
Trond Myklebust34310432008-12-23 15:21:38 -05001542 int ret = 0;
1543
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001544 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001545
1546 rcu_read_lock();
1547 deleg_cur = rcu_dereference(nfsi->delegation);
1548 if (deleg_cur == NULL)
1549 goto no_delegation;
1550
1551 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001552 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001553 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001554 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001555 goto no_delegation_unlock;
1556
1557 if (delegation == NULL)
1558 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001559 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001560 goto no_delegation_unlock;
1561
Trond Myklebustb7391f42008-12-23 15:21:52 -05001562 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001563 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1564 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001565 ret = 1;
1566no_delegation_unlock:
1567 spin_unlock(&deleg_cur->lock);
1568no_delegation:
1569 rcu_read_unlock();
1570
1571 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001572 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001573 ret = 1;
1574 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001575 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001576 nfs4_schedule_state_manager(clp);
1577 if (freeme.type != 0)
1578 nfs4_test_and_free_stateid(server, &freeme,
1579 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001580
1581 return ret;
1582}
1583
Trond Myklebust39071e62015-01-24 15:07:56 -05001584static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1585 const nfs4_stateid *stateid)
1586{
1587 struct nfs4_state *state = lsp->ls_state;
1588 bool ret = false;
1589
1590 spin_lock(&state->state_lock);
1591 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1592 goto out_noupdate;
1593 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1594 goto out_noupdate;
1595 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1596 ret = true;
1597out_noupdate:
1598 spin_unlock(&state->state_lock);
1599 return ret;
1600}
Trond Myklebust34310432008-12-23 15:21:38 -05001601
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001602static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001603{
1604 struct nfs_delegation *delegation;
1605
1606 rcu_read_lock();
1607 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001608 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001609 rcu_read_unlock();
1610 return;
1611 }
1612 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001613 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001614}
1615
Trond Myklebust6ee41262007-07-08 14:11:36 -04001616static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001617{
1618 struct nfs4_state *state = opendata->state;
1619 struct nfs_inode *nfsi = NFS_I(state->inode);
1620 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001621 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001622 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001623 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001624 nfs4_stateid stateid;
1625 int ret = -EAGAIN;
1626
Trond Myklebustaac00a82007-07-05 19:02:21 -04001627 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001628 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001629 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001630 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001631 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001632 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001633 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001634 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001635 rcu_read_lock();
1636 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001637 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001638 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001639 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001640 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001641 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001642 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001643 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001644 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001645 if (!opendata->is_recover) {
1646 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1647 if (ret != 0)
1648 goto out;
1649 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001650 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001651
1652 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001653 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001654 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001655 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001656out:
1657 return ERR_PTR(ret);
1658out_return_state:
1659 atomic_inc(&state->count);
1660 return state;
1661}
1662
Andy Adamsone23008e2012-10-02 21:07:32 -04001663static void
1664nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1665{
1666 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1667 struct nfs_delegation *delegation;
1668 int delegation_flags = 0;
1669
1670 rcu_read_lock();
1671 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1672 if (delegation)
1673 delegation_flags = delegation->flags;
1674 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001675 switch (data->o_arg.claim) {
1676 default:
1677 break;
1678 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1679 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001680 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1681 "returning a delegation for "
1682 "OPEN(CLAIM_DELEGATE_CUR)\n",
1683 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001684 return;
1685 }
1686 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001687 nfs_inode_set_delegation(state->inode,
1688 data->owner->so_cred,
1689 &data->o_res);
1690 else
1691 nfs_inode_reclaim_delegation(state->inode,
1692 data->owner->so_cred,
1693 &data->o_res);
1694}
1695
1696/*
1697 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1698 * and update the nfs4_state.
1699 */
1700static struct nfs4_state *
1701_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1702{
1703 struct inode *inode = data->state->inode;
1704 struct nfs4_state *state = data->state;
1705 int ret;
1706
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001707 if (!data->rpc_done) {
1708 if (data->rpc_status) {
1709 ret = data->rpc_status;
1710 goto err;
1711 }
1712 /* cached opens have already been processed */
1713 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001714 }
1715
Andy Adamsone23008e2012-10-02 21:07:32 -04001716 ret = nfs_refresh_inode(inode, &data->f_attr);
1717 if (ret)
1718 goto err;
1719
1720 if (data->o_res.delegation_type != 0)
1721 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001722update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001723 update_open_stateid(state, &data->o_res.stateid, NULL,
1724 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001725 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001726
1727 return state;
1728err:
1729 return ERR_PTR(ret);
1730
1731}
1732
1733static struct nfs4_state *
1734_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001735{
1736 struct inode *inode;
1737 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001738 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001739
Trond Myklebustaac00a82007-07-05 19:02:21 -04001740 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001741 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001742 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001743 goto out;
1744 }
1745
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001746 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001747 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001748 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001749 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001750 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001751 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001752 goto err;
1753 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754 state = nfs4_get_open_state(inode, data->owner);
1755 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001756 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001757 if (data->o_res.delegation_type != 0)
1758 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001759 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001760 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001762out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001763 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001764 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001765err_put_inode:
1766 iput(inode);
1767err:
1768 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001769}
1770
Andy Adamsone23008e2012-10-02 21:07:32 -04001771static struct nfs4_state *
1772nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1773{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001774 struct nfs4_state *ret;
1775
Andy Adamsone23008e2012-10-02 21:07:32 -04001776 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001777 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1778 else
1779 ret = _nfs4_opendata_to_nfs4_state(data);
1780 nfs4_sequence_free_slot(&data->o_res.seq_res);
1781 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001782}
1783
Trond Myklebust864472e2006-01-03 09:55:15 +01001784static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1785{
1786 struct nfs_inode *nfsi = NFS_I(state->inode);
1787 struct nfs_open_context *ctx;
1788
1789 spin_lock(&state->inode->i_lock);
1790 list_for_each_entry(ctx, &nfsi->open_files, list) {
1791 if (ctx->state != state)
1792 continue;
1793 get_nfs_open_context(ctx);
1794 spin_unlock(&state->inode->i_lock);
1795 return ctx;
1796 }
1797 spin_unlock(&state->inode->i_lock);
1798 return ERR_PTR(-ENOENT);
1799}
1800
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001801static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1802 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001803{
1804 struct nfs4_opendata *opendata;
1805
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001806 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001807 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001808 if (opendata == NULL)
1809 return ERR_PTR(-ENOMEM);
1810 opendata->state = state;
1811 atomic_inc(&state->count);
1812 return opendata;
1813}
1814
Trond Myklebust24311f82015-09-20 10:50:17 -04001815static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1816 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001817{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001818 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001819 int ret;
1820
Trond Myklebust24311f82015-09-20 10:50:17 -04001821 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001822 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001823 opendata->o_arg.open_flags = 0;
1824 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001825 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1826 NFS_SB(opendata->dentry->d_sb),
1827 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001828 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1829 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1830 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001831 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001832 if (ret != 0)
1833 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001834 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001835 if (IS_ERR(newstate))
1836 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001837 if (newstate != opendata->state)
1838 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001839 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001840 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001841}
1842
1843static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1844{
Trond Myklebust864472e2006-01-03 09:55:15 +01001845 int ret;
1846
Trond Myklebust4f14c192014-02-12 19:15:06 -05001847 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1848 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1849 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1850 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001851 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001852 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001853 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001854 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001855 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1856 if (ret != 0)
1857 return ret;
1858 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1859 if (ret != 0)
1860 return ret;
1861 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1862 if (ret != 0)
1863 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001864 /*
1865 * We may have performed cached opens for all three recoveries.
1866 * Check if we need to update the current stateid.
1867 */
1868 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001869 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001870 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001871 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001872 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001873 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001874 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001875 return 0;
1876}
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878/*
1879 * OPEN_RECLAIM:
1880 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001882static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001884 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001885 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001886 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 int status;
1888
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001889 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1890 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001891 if (IS_ERR(opendata))
1892 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001893 rcu_read_lock();
1894 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001895 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001896 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001897 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001898 opendata->o_arg.u.delegation_type = delegation_type;
1899 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001900 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return status;
1902}
1903
Trond Myklebust539cd032007-06-05 11:46:42 -04001904static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 struct nfs_server *server = NFS_SERVER(state->inode);
1907 struct nfs4_exception exception = { };
1908 int err;
1909 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001910 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001911 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001912 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1913 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001914 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001915 break;
1916 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 } while (exception.retry);
1918 return err;
1919}
1920
Trond Myklebust864472e2006-01-03 09:55:15 +01001921static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1922{
1923 struct nfs_open_context *ctx;
1924 int ret;
1925
1926 ctx = nfs4_state_find_open_context(state);
1927 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001928 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001929 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001930 put_nfs_open_context(ctx);
1931 return ret;
1932}
1933
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001934static 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 -07001935{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001936 switch (err) {
1937 default:
1938 printk(KERN_ERR "NFS: %s: unhandled error "
1939 "%d.\n", __func__, err);
1940 case 0:
1941 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001942 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001943 case -ESTALE:
1944 break;
1945 case -NFS4ERR_BADSESSION:
1946 case -NFS4ERR_BADSLOT:
1947 case -NFS4ERR_BAD_HIGH_SLOT:
1948 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1949 case -NFS4ERR_DEADSESSION:
1950 set_bit(NFS_DELEGATED_STATE, &state->flags);
1951 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1952 return -EAGAIN;
1953 case -NFS4ERR_STALE_CLIENTID:
1954 case -NFS4ERR_STALE_STATEID:
1955 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001956 /* Don't recall a delegation if it was lost */
1957 nfs4_schedule_lease_recovery(server->nfs_client);
1958 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001959 case -NFS4ERR_MOVED:
1960 nfs4_schedule_migration_recovery(server);
1961 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001962 case -NFS4ERR_LEASE_MOVED:
1963 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1964 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001965 case -NFS4ERR_DELEG_REVOKED:
1966 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001967 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001968 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001969 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001970 nfs_inode_find_state_and_recover(state->inode,
1971 stateid);
1972 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001973 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001974 case -NFS4ERR_DELAY:
1975 case -NFS4ERR_GRACE:
1976 set_bit(NFS_DELEGATED_STATE, &state->flags);
1977 ssleep(1);
1978 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001979 case -ENOMEM:
1980 case -NFS4ERR_DENIED:
1981 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1982 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return err;
1985}
1986
Trond Myklebust24311f82015-09-20 10:50:17 -04001987int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1988 struct nfs4_state *state, const nfs4_stateid *stateid,
1989 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001990{
1991 struct nfs_server *server = NFS_SERVER(state->inode);
1992 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001993 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001994
1995 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1996 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1997 if (IS_ERR(opendata))
1998 return PTR_ERR(opendata);
1999 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002000 write_seqlock(&state->seqlock);
2001 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2002 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002003 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2004 switch (type & (FMODE_READ|FMODE_WRITE)) {
2005 case FMODE_READ|FMODE_WRITE:
2006 case FMODE_WRITE:
2007 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2008 if (err)
2009 break;
2010 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2011 if (err)
2012 break;
2013 case FMODE_READ:
2014 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2015 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002016 nfs4_opendata_put(opendata);
2017 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2018}
2019
Chuck Leverbe05c862013-08-09 12:49:47 -04002020static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2021{
2022 struct nfs4_opendata *data = calldata;
2023
Peng Taocb04ad22014-06-11 05:24:15 +08002024 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2025 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002026}
2027
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002028static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2029{
2030 struct nfs4_opendata *data = calldata;
2031
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002032 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002033
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002034 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002035 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002036 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002037 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002038 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002039 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002040 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002041}
2042
2043static void nfs4_open_confirm_release(void *calldata)
2044{
2045 struct nfs4_opendata *data = calldata;
2046 struct nfs4_state *state = NULL;
2047
2048 /* If this request hasn't been cancelled, do nothing */
2049 if (data->cancelled == 0)
2050 goto out_free;
2051 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002052 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002053 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002054 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002055 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002056 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002057out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002058 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002059}
2060
2061static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002062 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002063 .rpc_call_done = nfs4_open_confirm_done,
2064 .rpc_release = nfs4_open_confirm_release,
2065};
2066
2067/*
2068 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2069 */
2070static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2071{
David Howells2b0143b2015-03-17 22:25:59 +00002072 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002073 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002074 struct rpc_message msg = {
2075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2076 .rpc_argp = &data->c_arg,
2077 .rpc_resp = &data->c_res,
2078 .rpc_cred = data->owner->so_cred,
2079 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002080 struct rpc_task_setup task_setup_data = {
2081 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002082 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002083 .callback_ops = &nfs4_open_confirm_ops,
2084 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002085 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002086 .flags = RPC_TASK_ASYNC,
2087 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 int status;
2089
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002090 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002091 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002092 data->rpc_done = 0;
2093 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002094 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002095 if (data->is_recover)
2096 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002097 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002098 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002099 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002100 status = nfs4_wait_for_completion_rpc_task(task);
2101 if (status != 0) {
2102 data->cancelled = 1;
2103 smp_wmb();
2104 } else
2105 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002106 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return status;
2108}
2109
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002110static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002112 struct nfs4_opendata *data = calldata;
2113 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002114 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002115 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002116
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002118 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002119 /*
2120 * Check if we still need to send an OPEN call, or if we can use
2121 * a delegation instead.
2122 */
2123 if (data->state != NULL) {
2124 struct nfs_delegation *delegation;
2125
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002126 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002127 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002128 rcu_read_lock();
2129 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002130 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002131 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002132 rcu_read_unlock();
2133 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002134 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002135 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002136 switch (claim) {
2137 default:
2138 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002139 case NFS4_OPEN_CLAIM_PREVIOUS:
2140 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2141 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002142 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002143 case NFS4_OPEN_CLAIM_FH:
2144 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002145 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2146 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002147 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002148 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002149 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002150 &data->o_res.seq_res,
2151 task) != 0)
2152 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002153
2154 /* Set the create mode (note dependency on the session type) */
2155 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2156 if (data->o_arg.open_flags & O_EXCL) {
2157 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2158 if (nfs4_has_persistent_session(clp))
2159 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2160 else if (clp->cl_mvops->minor_version > 0)
2161 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2162 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002163 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002164unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002165 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002166 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002167out_no_action:
2168 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002169out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002170 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002171}
2172
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002173static void nfs4_open_done(struct rpc_task *task, void *calldata)
2174{
2175 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002177 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002178
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002179 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002180 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002181
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002182 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002183 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2184 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002185 case S_IFREG:
2186 break;
2187 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002188 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002189 break;
2190 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002192 break;
2193 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002194 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002195 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002196 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002197 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002198 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2199 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002200 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002201 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002202}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002203
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002204static void nfs4_open_release(void *calldata)
2205{
2206 struct nfs4_opendata *data = calldata;
2207 struct nfs4_state *state = NULL;
2208
2209 /* If this request hasn't been cancelled, do nothing */
2210 if (data->cancelled == 0)
2211 goto out_free;
2212 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002213 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002214 goto out_free;
2215 /* In case we need an open_confirm, no cleanup! */
2216 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2217 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002218 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002219 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002220 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002221out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002222 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002223}
2224
2225static const struct rpc_call_ops nfs4_open_ops = {
2226 .rpc_call_prepare = nfs4_open_prepare,
2227 .rpc_call_done = nfs4_open_done,
2228 .rpc_release = nfs4_open_release,
2229};
2230
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002231static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002232{
David Howells2b0143b2015-03-17 22:25:59 +00002233 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002234 struct nfs_server *server = NFS_SERVER(dir);
2235 struct nfs_openargs *o_arg = &data->o_arg;
2236 struct nfs_openres *o_res = &data->o_res;
2237 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002238 struct rpc_message msg = {
2239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2240 .rpc_argp = o_arg,
2241 .rpc_resp = o_res,
2242 .rpc_cred = data->owner->so_cred,
2243 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002244 struct rpc_task_setup task_setup_data = {
2245 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002246 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002247 .callback_ops = &nfs4_open_ops,
2248 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002249 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002250 .flags = RPC_TASK_ASYNC,
2251 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002252 int status;
2253
Chuck Levera9c92d62013-08-09 12:48:18 -04002254 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002255 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002256 data->rpc_done = 0;
2257 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002258 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002259 data->is_recover = 0;
2260 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002261 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002262 data->is_recover = 1;
2263 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002264 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002265 if (IS_ERR(task))
2266 return PTR_ERR(task);
2267 status = nfs4_wait_for_completion_rpc_task(task);
2268 if (status != 0) {
2269 data->cancelled = 1;
2270 smp_wmb();
2271 } else
2272 status = data->rpc_status;
2273 rpc_put_task(task);
2274
2275 return status;
2276}
2277
2278static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2279{
David Howells2b0143b2015-03-17 22:25:59 +00002280 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002281 struct nfs_openres *o_res = &data->o_res;
2282 int status;
2283
2284 status = nfs4_run_open_task(data, 1);
2285 if (status != 0 || !data->rpc_done)
2286 return status;
2287
Trond Myklebust6926afd2012-01-07 13:22:46 -05002288 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2289
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002290 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2291 status = _nfs4_proc_open_confirm(data);
2292 if (status != 0)
2293 return status;
2294 }
2295
2296 return status;
2297}
2298
Trond Myklebustf3792d62014-07-10 08:54:32 -04002299/*
2300 * Additional permission checks in order to distinguish between an
2301 * open for read, and an open for execute. This works around the
2302 * fact that NFSv4 OPEN treats read and execute permissions as being
2303 * the same.
2304 * Note that in the non-execute case, we want to turn off permission
2305 * checking if we just created a new file (POSIX open() semantics).
2306 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002307static int nfs4_opendata_access(struct rpc_cred *cred,
2308 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002309 struct nfs4_state *state, fmode_t fmode,
2310 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002311{
2312 struct nfs_access_entry cache;
2313 u32 mask;
2314
2315 /* access call failed or for some reason the server doesn't
2316 * support any access modes -- defer access call until later */
2317 if (opendata->o_res.access_supported == 0)
2318 return 0;
2319
2320 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002321 /*
2322 * Use openflags to check for exec, because fmode won't
2323 * always have FMODE_EXEC set when file open for exec.
2324 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002325 if (openflags & __FMODE_EXEC) {
2326 /* ONLY check for exec rights */
2327 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002328 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002329 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002330
2331 cache.cred = cred;
2332 cache.jiffies = jiffies;
2333 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2334 nfs_access_add_cache(state->inode, &cache);
2335
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002336 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002337 return 0;
2338
2339 /* even though OPEN succeeded, access is denied. Close the file */
2340 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002341 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002342}
2343
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002344/*
2345 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2346 */
2347static int _nfs4_proc_open(struct nfs4_opendata *data)
2348{
David Howells2b0143b2015-03-17 22:25:59 +00002349 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002350 struct nfs_server *server = NFS_SERVER(dir);
2351 struct nfs_openargs *o_arg = &data->o_arg;
2352 struct nfs_openres *o_res = &data->o_res;
2353 int status;
2354
2355 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002356 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002357 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002358 if (status != 0) {
2359 if (status == -NFS4ERR_BADNAME &&
2360 !(o_arg->open_flags & O_CREAT))
2361 return -ENOENT;
2362 return status;
2363 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002364
Trond Myklebust6926afd2012-01-07 13:22:46 -05002365 nfs_fattr_map_and_free_names(server, &data->f_attr);
2366
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002367 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002368 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002369 if (o_arg->open_flags & O_EXCL)
2370 data->file_created = 1;
2371 else if (o_res->cinfo.before != o_res->cinfo.after)
2372 data->file_created = 1;
2373 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002374 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2375 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002377 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002379 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
2381 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002382 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Andy Adamsond83217c2011-03-01 01:34:17 +00002386static int nfs4_recover_expired_lease(struct nfs_server *server)
2387{
2388 return nfs4_client_recover_expired_lease(server->nfs_client);
2389}
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391/*
2392 * OPEN_EXPIRED:
2393 * reclaim state on the server after a network partition.
2394 * Assumes caller holds the appropriate lock
2395 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002396static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002398 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002399 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002401 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002402 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002403 if (IS_ERR(opendata))
2404 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002405 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002406 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002407 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002408 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002409 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002412static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002413{
Trond Myklebust539cd032007-06-05 11:46:42 -04002414 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002415 struct nfs4_exception exception = { };
2416 int err;
2417
2418 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002419 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002420 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002421 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2422 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002423 switch (err) {
2424 default:
2425 goto out;
2426 case -NFS4ERR_GRACE:
2427 case -NFS4ERR_DELAY:
2428 nfs4_handle_exception(server, err, &exception);
2429 err = 0;
2430 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002431 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002432out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002433 return err;
2434}
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2437{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002439 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Trond Myklebust864472e2006-01-03 09:55:15 +01002441 ctx = nfs4_state_find_open_context(state);
2442 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002443 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002444 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002445 put_nfs_open_context(ctx);
2446 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
Trond Myklebust41020b62016-09-22 13:38:58 -04002449static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2450 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002451{
Trond Myklebust41020b62016-09-22 13:38:58 -04002452 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002453 write_seqlock(&state->seqlock);
2454 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2455 write_sequnlock(&state->seqlock);
2456 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2457}
2458
2459static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2460{
2461 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002462 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002463}
2464
2465static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2466{
2467 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2468 nfs40_clear_delegation_stateid(state);
2469 return nfs4_open_expired(sp, state);
2470}
2471
Trond Myklebust45870d62016-09-22 13:38:59 -04002472static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2473 nfs4_stateid *stateid,
2474 struct rpc_cred *cred)
2475{
2476 return -NFS4ERR_BAD_STATEID;
2477}
2478
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002479#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002480static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2481 nfs4_stateid *stateid,
2482 struct rpc_cred *cred)
2483{
2484 int status;
2485
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002486 switch (stateid->type) {
2487 default:
2488 break;
2489 case NFS4_INVALID_STATEID_TYPE:
2490 case NFS4_SPECIAL_STATEID_TYPE:
2491 return -NFS4ERR_BAD_STATEID;
2492 case NFS4_REVOKED_STATEID_TYPE:
2493 goto out_free;
2494 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002495
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002496 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002497 switch (status) {
2498 case -NFS4ERR_EXPIRED:
2499 case -NFS4ERR_ADMIN_REVOKED:
2500 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002501 break;
2502 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002503 return status;
2504 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002505out_free:
2506 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002507 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002508 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002509}
2510
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002511static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002512{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002513 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002514 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002515 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002516 struct rpc_cred *cred;
2517 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002518
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002519 /* Get the delegation credential for use by test/free_stateid */
2520 rcu_read_lock();
2521 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002522 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002523 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002524 return;
2525 }
Trond Myklebust41020b62016-09-22 13:38:58 -04002526
2527 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002528 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2529 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002530 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002531 return;
2532 }
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002533
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002534 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2535 rcu_read_unlock();
2536 return;
2537 }
2538
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002539 cred = get_rpccred(delegation->cred);
2540 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002541 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002542 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002543 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002544 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002545
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002546 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002547}
2548
2549/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002550 * nfs41_check_expired_locks - possibly free a lock stateid
2551 *
2552 * @state: NFSv4 state for an inode
2553 *
2554 * Returns NFS_OK if recovery for this stateid is now finished.
2555 * Otherwise a negative NFS4ERR value is returned.
2556 */
2557static int nfs41_check_expired_locks(struct nfs4_state *state)
2558{
2559 int status, ret = NFS_OK;
2560 struct nfs4_lock_state *lsp;
2561 struct nfs_server *server = NFS_SERVER(state->inode);
2562
2563 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2564 goto out;
2565 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2566 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2567 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2568
2569 status = nfs41_test_and_free_expired_stateid(server,
2570 &lsp->ls_stateid,
2571 cred);
2572 trace_nfs4_test_lock_stateid(state, lsp, status);
2573 if (status == -NFS4ERR_EXPIRED ||
2574 status == -NFS4ERR_BAD_STATEID) {
2575 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002576 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002577 if (!recover_lost_locks)
2578 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2579 } else if (status != NFS_OK) {
2580 ret = status;
2581 break;
2582 }
2583 }
2584 };
2585out:
2586 return ret;
2587}
2588
2589/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002590 * nfs41_check_open_stateid - possibly free an open stateid
2591 *
2592 * @state: NFSv4 state for an inode
2593 *
2594 * Returns NFS_OK if recovery for this stateid is now finished.
2595 * Otherwise a negative NFS4ERR value is returned.
2596 */
2597static int nfs41_check_open_stateid(struct nfs4_state *state)
2598{
2599 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002600 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002601 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002602 int status;
2603
Trond Myklebustb134fc42016-09-22 13:39:16 -04002604 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002605 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2606 if (nfs4_have_delegation(state->inode, state->state))
2607 return NFS_OK;
2608 return -NFS4ERR_OPENMODE;
2609 }
Trond Myklebustb134fc42016-09-22 13:39:16 -04002610 return -NFS4ERR_BAD_STATEID;
2611 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002612 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002613 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002614 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002615 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2616 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2617 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002618 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002619 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002620 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002621 if (status != NFS_OK)
2622 return status;
2623 if (nfs_open_stateid_recover_openmode(state))
2624 return -NFS4ERR_OPENMODE;
2625 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002626}
2627
2628static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2629{
Chuck Levereb64cf92012-07-11 16:30:05 -04002630 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002631
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002632 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002633 status = nfs41_check_expired_locks(state);
2634 if (status != NFS_OK)
2635 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002636 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002637 if (status != NFS_OK)
2638 status = nfs4_open_expired(sp, state);
2639 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002640}
2641#endif
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002644 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2645 * fields corresponding to attributes that were used to store the verifier.
2646 * Make sure we clobber those fields in the later setattr call
2647 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002648static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2649 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002650{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002651 const u32 *attrset = opendata->o_res.attrset;
2652
2653 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002654 !(sattr->ia_valid & ATTR_ATIME_SET))
2655 sattr->ia_valid |= ATTR_ATIME;
2656
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002657 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002658 !(sattr->ia_valid & ATTR_MTIME_SET))
2659 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002660
2661 /* Except MODE, it seems harmless of setting twice. */
2662 if ((attrset[1] & FATTR4_WORD1_MODE))
2663 sattr->ia_valid &= ~ATTR_MODE;
2664
2665 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2666 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002667}
2668
Trond Myklebustc21443c2013-02-07 14:26:21 -05002669static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2670 fmode_t fmode,
2671 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002672 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002673{
2674 struct nfs4_state_owner *sp = opendata->owner;
2675 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002676 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002677 struct nfs4_state *state;
2678 unsigned int seq;
2679 int ret;
2680
2681 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2682
2683 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002684 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002685 goto out;
2686
2687 state = nfs4_opendata_to_nfs4_state(opendata);
2688 ret = PTR_ERR(state);
2689 if (IS_ERR(state))
2690 goto out;
2691 if (server->caps & NFS_CAP_POSIX_LOCK)
2692 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002693 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2694 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002695
Trond Myklebust275bb302013-05-29 13:11:28 -04002696 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002697 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002698 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002699 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002700 alias = d_exact_alias(dentry, state->inode);
2701 if (!alias)
2702 alias = d_splice_alias(igrab(state->inode), dentry);
2703 /* d_splice_alias() can't fail here - it's a non-directory */
2704 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002705 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002706 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002707 }
2708 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002709 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002710 }
2711
Trond Myklebustc21443c2013-02-07 14:26:21 -05002712 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2713 if (ret != 0)
2714 goto out;
2715
Trond Myklebust3efb9722013-05-29 13:17:04 -04002716 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002717 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002718 nfs_inode_attach_open_context(ctx);
2719 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2720 nfs4_schedule_stateid_recovery(server, state);
2721 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002722out:
2723 return ret;
2724}
2725
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002726/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002727 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 */
Andy Adamson82be4172012-05-23 05:02:35 -04002729static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002730 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002731 int flags,
2732 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002733 struct nfs4_label *label,
2734 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
2736 struct nfs4_state_owner *sp;
2737 struct nfs4_state *state = NULL;
2738 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002739 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002740 struct dentry *dentry = ctx->dentry;
2741 struct rpc_cred *cred = ctx->cred;
2742 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2743 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002744 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002745 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002746 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002750 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2751 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2753 goto out_err;
2754 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002755 status = nfs4_recover_expired_lease(server);
2756 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002757 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002758 if (d_really_is_positive(dentry))
2759 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002760 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002761 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002762 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002763 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002764 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002765 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002766 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
David Quigley14c43f72013-05-22 12:50:43 -04002768 if (label) {
2769 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2770 if (IS_ERR(olabel)) {
2771 status = PTR_ERR(olabel);
2772 goto err_opendata_put;
2773 }
2774 }
2775
Trond Myklebuste911b812014-03-26 13:24:37 -07002776 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2777 if (!opendata->f_attr.mdsthreshold) {
2778 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2779 if (!opendata->f_attr.mdsthreshold)
2780 goto err_free_label;
2781 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002782 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002783 }
David Howells2b0143b2015-03-17 22:25:59 +00002784 if (d_really_is_positive(dentry))
2785 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002786
Trond Myklebust3efb9722013-05-29 13:17:04 -04002787 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002788 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002789 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002790 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002791
NeilBrownefcbc042015-07-30 13:00:56 +10002792 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002793 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002794 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002795 /*
2796 * send create attributes which was not set by open
2797 * with an extra setattr.
2798 */
2799 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2800 nfs_fattr_init(opendata->o_res.f_attr);
2801 status = nfs4_do_setattr(state->inode, cred,
2802 opendata->o_res.f_attr, sattr,
2803 state, label, olabel);
2804 if (status == 0) {
2805 nfs_setattr_update_inode(state->inode, sattr,
2806 opendata->o_res.f_attr);
2807 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2808 }
David Quigley1775fd32013-05-22 12:50:42 -04002809 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002810 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002811 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002812 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002813
Trond Myklebuste911b812014-03-26 13:24:37 -07002814 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002815 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002816 opendata->f_attr.mdsthreshold = NULL;
2817 }
Andy Adamson82be4172012-05-23 05:02:35 -04002818
David Quigley14c43f72013-05-22 12:50:43 -04002819 nfs4_label_free(olabel);
2820
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002821 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002824err_free_label:
2825 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002826err_opendata_put:
2827 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002828err_put_state_owner:
2829 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return status;
2832}
2833
2834
Andy Adamson82be4172012-05-23 05:02:35 -04002835static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002836 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002837 int flags,
2838 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002839 struct nfs4_label *label,
2840 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002842 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 struct nfs4_exception exception = { };
2844 struct nfs4_state *res;
2845 int status;
2846
2847 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002848 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002849 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002850 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (status == 0)
2852 break;
2853 /* NOTE: BAD_SEQID means the server and client disagree about the
2854 * book-keeping w.r.t. state-changing operations
2855 * (OPEN/CLOSE/LOCK/LOCKU...)
2856 * It is actually a sign of a bug on the client or on the server.
2857 *
2858 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002859 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 * have unhashed the old state_owner for us, and that we can
2861 * therefore safely retry using a new one. We should still warn
2862 * the user though...
2863 */
2864 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002865 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002866 " returned a bad sequence-id error!\n",
2867 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 exception.retry = 1;
2869 continue;
2870 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002871 /*
2872 * BAD_STATEID on OPEN means that the server cancelled our
2873 * state before it received the OPEN_CONFIRM.
2874 * Recover by retrying the request as per the discussion
2875 * on Page 181 of RFC3530.
2876 */
2877 if (status == -NFS4ERR_BAD_STATEID) {
2878 exception.retry = 1;
2879 continue;
2880 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002881 if (status == -EAGAIN) {
2882 /* We must have found a delegation */
2883 exception.retry = 1;
2884 continue;
2885 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002886 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2887 continue;
2888 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 status, &exception));
2890 } while (exception.retry);
2891 return res;
2892}
2893
Trond Myklebust8487c472016-06-26 08:44:35 -04002894static int _nfs4_do_setattr(struct inode *inode,
2895 struct nfs_setattrargs *arg,
2896 struct nfs_setattrres *res,
2897 struct rpc_cred *cred,
2898 struct nfs4_state *state)
2899{
2900 struct nfs_server *server = NFS_SERVER(inode);
2901 struct rpc_message msg = {
2902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2903 .rpc_argp = arg,
2904 .rpc_resp = res,
2905 .rpc_cred = cred,
2906 };
2907 struct rpc_cred *delegation_cred = NULL;
2908 unsigned long timestamp = jiffies;
2909 fmode_t fmode;
2910 bool truncate;
2911 int status;
2912
2913 nfs_fattr_init(res->fattr);
2914
2915 /* Servers should only apply open mode checks for file size changes */
2916 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2917 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2918
2919 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2920 /* Use that stateid */
2921 } else if (truncate && state != NULL) {
2922 struct nfs_lockowner lockowner = {
2923 .l_owner = current->files,
2924 .l_pid = current->tgid,
2925 };
2926 if (!nfs4_valid_open_stateid(state))
2927 return -EBADF;
2928 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2929 &arg->stateid, &delegation_cred) == -EIO)
2930 return -EBADF;
2931 } else
2932 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2933 if (delegation_cred)
2934 msg.rpc_cred = delegation_cred;
2935
2936 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2937
2938 put_rpccred(delegation_cred);
2939 if (status == 0 && state != NULL)
2940 renew_lease(server, timestamp);
2941 trace_nfs4_setattr(inode, &arg->stateid, status);
2942 return status;
2943}
2944
2945static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2946 struct nfs_fattr *fattr, struct iattr *sattr,
2947 struct nfs4_state *state, struct nfs4_label *ilabel,
2948 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002950 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002952 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 .iap = sattr,
2954 .server = server,
2955 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002956 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 };
2958 struct nfs_setattrres res = {
2959 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002960 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 .server = server,
2962 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002963 struct nfs4_exception exception = {
2964 .state = state,
2965 .inode = inode,
2966 .stateid = &arg.stateid,
2967 };
2968 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
David Quigleyaa9c2662013-05-22 12:50:44 -04002970 arg.bitmask = nfs4_bitmask(server, ilabel);
2971 if (ilabel)
2972 arg.bitmask = nfs4_bitmask(server, olabel);
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002975 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002976 switch (err) {
2977 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002978 if (!(sattr->ia_valid & ATTR_SIZE)) {
2979 pr_warn_once("NFSv4: server %s is incorrectly "
2980 "applying open mode checks to "
2981 "a SETATTR that is not "
2982 "changing file size.\n",
2983 server->nfs_client->cl_hostname);
2984 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002985 if (state && !(state->state & FMODE_WRITE)) {
2986 err = -EBADF;
2987 if (sattr->ia_valid & ATTR_OPEN)
2988 err = -EACCES;
2989 goto out;
2990 }
2991 }
2992 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002994out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return err;
2996}
2997
Peng Tao500d7012015-09-22 11:35:22 +08002998static bool
2999nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3000{
3001 if (inode == NULL || !nfs_have_layout(inode))
3002 return false;
3003
3004 return pnfs_wait_on_layoutreturn(inode, task);
3005}
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007struct nfs4_closedata {
3008 struct inode *inode;
3009 struct nfs4_state *state;
3010 struct nfs_closeargs arg;
3011 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003012 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003013 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003014 bool roc;
3015 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016};
3017
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003018static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003019{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003020 struct nfs4_closedata *calldata = data;
3021 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003022 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003023
Fred Isamanf7e89172011-01-06 11:36:32 +00003024 if (calldata->roc)
3025 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003026 nfs4_put_open_state(calldata->state);
3027 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003028 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003029 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003030 kfree(calldata);
3031}
3032
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003033static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003035 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003038 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Chuck Levera3ca5652012-03-01 17:00:40 -05003040 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003041 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3042 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003043 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 /* hmm. we are done with the inode, and in the process of freeing
3045 * the state_owner. we keep this around to process errors
3046 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 switch (task->tk_status) {
3048 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003049 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003050 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003051 pnfs_roc_set_barrier(state->inode,
3052 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003053 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003054 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003055 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003057 case -NFS4ERR_EXPIRED:
3058 nfs4_free_revoked_stateid(server,
3059 &calldata->arg.stateid,
3060 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003061 case -NFS4ERR_OLD_STATEID:
3062 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003063 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003064 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003065 rpc_restart_call_prepare(task);
3066 goto out_release;
3067 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003068 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003071 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003072 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003073 goto out_release;
3074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003076 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3077 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003078out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003079 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003080 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003081 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003084static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003086 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003087 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003088 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003089 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003090 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003091
Chuck Levera3ca5652012-03-01 17:00:40 -05003092 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003093 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003094 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003095
Trond Myklebust88069f72009-12-08 08:33:16 -05003096 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003097 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003098 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3099 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3100 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003101 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003102 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003103 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003104 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003105 if (state->n_rdonly == 0)
3106 call_close |= is_rdonly;
3107 else if (is_rdonly)
3108 calldata->arg.fmode |= FMODE_READ;
3109 if (state->n_wronly == 0)
3110 call_close |= is_wronly;
3111 else if (is_wronly)
3112 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003113 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3114 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003115 } else if (is_rdwr)
3116 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3117
Trond Myklebust5d422302013-03-14 16:57:48 -04003118 if (!nfs4_valid_open_stateid(state))
3119 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003120 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003121
3122 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003123 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003124 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003125 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003126
Peng Tao500d7012015-09-22 11:35:22 +08003127 if (nfs4_wait_on_layoutreturn(inode, task)) {
3128 nfs_release_seqid(calldata->arg.seqid);
3129 goto out_wait;
3130 }
3131
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003132 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003133 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003134 if (calldata->roc)
3135 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3136
Trond Myklebust6ae37332015-01-30 14:21:14 -05003137 calldata->arg.share_access =
3138 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3139 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003140
Trond Myklebust516a6af2005-10-27 22:12:41 -04003141 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003142 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003143 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003144 &calldata->arg.seq_args,
3145 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003146 task) != 0)
3147 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003148 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003149 return;
3150out_no_action:
3151 task->tk_action = NULL;
3152out_wait:
3153 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003156static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003157 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003158 .rpc_call_done = nfs4_close_done,
3159 .rpc_release = nfs4_free_closedata,
3160};
3161
Peng Taofe08c542014-07-03 13:05:01 +08003162static bool nfs4_roc(struct inode *inode)
3163{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003164 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003165 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003166 return pnfs_roc(inode);
3167}
3168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169/*
3170 * It is possible for data to be read/written from a mem-mapped file
3171 * after the sys_close call (which hits the vfs layer as a flush).
3172 * This means that we can't safely call nfsv4 close on a file until
3173 * the inode is cleared. This in turn means that we are not good
3174 * NFSv4 citizens - we do not indicate to the server to update the file's
3175 * share state even when we are done with one of the three share
3176 * stateid's in the inode.
3177 *
3178 * NOTE: Caller must be holding the sp->so_owner semaphore!
3179 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003180int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003182 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003183 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003185 struct nfs4_state_owner *sp = state->owner;
3186 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003187 struct rpc_message msg = {
3188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3189 .rpc_cred = state->owner->so_cred,
3190 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003191 struct rpc_task_setup task_setup_data = {
3192 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003193 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003194 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003195 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003196 .flags = RPC_TASK_ASYNC,
3197 };
Trond Myklebust95121352005-10-18 14:20:12 -07003198 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003200 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3201 &task_setup_data.rpc_client, &msg);
3202
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003203 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003205 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003206 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003207 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003209 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003211 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3212 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003213 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003214 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003215 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003216 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003217 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003218 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003219 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003220 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003221 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003222
Trond Myklebust1174dd12010-12-21 10:52:24 -05003223 msg.rpc_argp = &calldata->arg;
3224 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003225 task_setup_data.callback_data = calldata;
3226 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003227 if (IS_ERR(task))
3228 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003229 status = 0;
3230 if (wait)
3231 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003232 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003233 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003234out_free_calldata:
3235 kfree(calldata);
3236out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003237 nfs4_put_open_state(state);
3238 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003239 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Trond Myklebust2b484292010-09-17 10:56:51 -04003242static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003243nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3244 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003247 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3248
3249 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Trond Myklebust565277f2007-10-15 18:17:53 -04003251 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003252 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003253
3254 nfs4_label_release_security(label);
3255
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003256 if (IS_ERR(state))
3257 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003258 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
Trond Myklebust1185a552009-12-03 15:54:02 -05003261static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003262{
3263 if (ctx->state == NULL)
3264 return;
3265 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003266 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003267 else
Al Viro643168c2011-06-22 18:20:23 -04003268 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Trond Myklebustb944dba2013-11-04 15:20:20 -05003271#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3272#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003273#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3276{
Kinglong Mee8c612822015-08-26 21:12:58 +08003277 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003278 struct nfs4_server_caps_arg args = {
3279 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003280 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003281 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 struct nfs4_server_caps_res res = {};
3283 struct rpc_message msg = {
3284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003285 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 .rpc_resp = &res,
3287 };
3288 int status;
3289
Kinglong Mee8c612822015-08-26 21:12:58 +08003290 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3291 FATTR4_WORD0_FH_EXPIRE_TYPE |
3292 FATTR4_WORD0_LINK_SUPPORT |
3293 FATTR4_WORD0_SYMLINK_SUPPORT |
3294 FATTR4_WORD0_ACLSUPPORT;
3295 if (minorversion)
3296 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3297
Bryan Schumaker7c513052011-03-24 17:12:24 +00003298 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003300 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003301 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003302 case 0:
3303 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3304 res.attr_bitmask[2] = 0;
3305 break;
3306 case 1:
3307 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3308 break;
3309 case 2:
3310 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003313 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3314 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3315 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3316 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003317 NFS_CAP_CTIME|NFS_CAP_MTIME|
3318 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003319 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3320 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 server->caps |= NFS_CAP_ACLS;
3322 if (res.has_links != 0)
3323 server->caps |= NFS_CAP_HARDLINKS;
3324 if (res.has_symlinks != 0)
3325 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003326 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3327 server->caps |= NFS_CAP_FILEID;
3328 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3329 server->caps |= NFS_CAP_MODE;
3330 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3331 server->caps |= NFS_CAP_NLINK;
3332 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3333 server->caps |= NFS_CAP_OWNER;
3334 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3335 server->caps |= NFS_CAP_OWNER_GROUP;
3336 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3337 server->caps |= NFS_CAP_ATIME;
3338 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3339 server->caps |= NFS_CAP_CTIME;
3340 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3341 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003342#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3343 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3344 server->caps |= NFS_CAP_SECURITY_LABEL;
3345#endif
3346 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3347 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003348 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003349
Trond Myklebusta65318b2009-03-11 14:10:28 -04003350 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3351 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3352 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003353 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003354 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3355 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003357 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return status;
3361}
3362
Trond Myklebust55a97592006-06-09 09:34:19 -04003363int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
3365 struct nfs4_exception exception = { };
3366 int err;
3367 do {
3368 err = nfs4_handle_exception(server,
3369 _nfs4_server_capabilities(server, fhandle),
3370 &exception);
3371 } while (exception.retry);
3372 return err;
3373}
3374
3375static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3376 struct nfs_fsinfo *info)
3377{
David Quigleyaa9c2662013-05-22 12:50:44 -04003378 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003380 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 };
3382 struct nfs4_lookup_res res = {
3383 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003384 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 .fh = fhandle,
3386 };
3387 struct rpc_message msg = {
3388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3389 .rpc_argp = &args,
3390 .rpc_resp = &res,
3391 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003392
David Quigleyaa9c2662013-05-22 12:50:44 -04003393 bitmask[0] = nfs4_fattr_bitmap[0];
3394 bitmask[1] = nfs4_fattr_bitmap[1];
3395 /*
3396 * Process the label in the upcoming getfattr
3397 */
3398 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3399
Trond Myklebust0e574af2005-10-27 22:12:38 -04003400 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003401 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
3404static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3405 struct nfs_fsinfo *info)
3406{
3407 struct nfs4_exception exception = { };
3408 int err;
3409 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003410 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003411 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003412 switch (err) {
3413 case 0:
3414 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003415 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003416 default:
3417 err = nfs4_handle_exception(server, err, &exception);
3418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003420out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return err;
3422}
3423
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003424static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3425 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3426{
Trond Myklebustc2190662013-08-26 19:23:04 -04003427 struct rpc_auth_create_args auth_args = {
3428 .pseudoflavor = flavor,
3429 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003430 struct rpc_auth *auth;
3431 int ret;
3432
Trond Myklebustc2190662013-08-26 19:23:04 -04003433 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003434 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003435 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003436 goto out;
3437 }
3438 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003439out:
3440 return ret;
3441}
3442
Chuck Lever9a744ba2013-03-16 15:56:02 -04003443/*
3444 * Retry pseudoroot lookup with various security flavors. We do this when:
3445 *
3446 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3447 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3448 *
3449 * Returns zero on success, or a negative NFS4ERR value, or a
3450 * negative errno value.
3451 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003452static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003453 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003455 /* Per 3530bis 15.33.5 */
3456 static const rpc_authflavor_t flav_array[] = {
3457 RPC_AUTH_GSS_KRB5P,
3458 RPC_AUTH_GSS_KRB5I,
3459 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003460 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003461 RPC_AUTH_NULL,
3462 };
3463 int status = -EPERM;
3464 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003466 if (server->auth_info.flavor_len > 0) {
3467 /* try each flavor specified by user */
3468 for (i = 0; i < server->auth_info.flavor_len; i++) {
3469 status = nfs4_lookup_root_sec(server, fhandle, info,
3470 server->auth_info.flavors[i]);
3471 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3472 continue;
3473 break;
3474 }
3475 } else {
3476 /* no flavors specified by user, try default list */
3477 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3478 status = nfs4_lookup_root_sec(server, fhandle, info,
3479 flav_array[i]);
3480 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3481 continue;
3482 break;
3483 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003484 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003485
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003486 /*
3487 * -EACCESS could mean that the user doesn't have correct permissions
3488 * to access the mount. It could also mean that we tried to mount
3489 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3490 * existing mount programs don't handle -EACCES very well so it should
3491 * be mapped to -EPERM instead.
3492 */
3493 if (status == -EACCES)
3494 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003495 return status;
3496}
3497
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003498/**
3499 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3500 * @server: initialized nfs_server handle
3501 * @fhandle: we fill in the pseudo-fs root file handle
3502 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003503 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003504 *
3505 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003506 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003507int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003508 struct nfs_fsinfo *info,
3509 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003510{
Andre Przywarac7757072015-04-23 17:17:40 +01003511 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003512
Andre Przywarac7757072015-04-23 17:17:40 +01003513 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003514 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003515
3516 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003517 status = server->nfs_client->cl_mvops->find_root_sec(server,
3518 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 if (status == 0)
3521 status = nfs4_server_capabilities(server, fhandle);
3522 if (status == 0)
3523 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003524
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003525 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
Bryan Schumakerbae36242012-05-10 15:07:31 -04003528static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3529 struct nfs_fsinfo *info)
3530{
3531 int error;
3532 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003533 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003534
3535 error = nfs4_server_capabilities(server, mntfh);
3536 if (error < 0) {
3537 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3538 return error;
3539 }
3540
David Quigley14c43f72013-05-22 12:50:43 -04003541 label = nfs4_label_alloc(server, GFP_KERNEL);
3542 if (IS_ERR(label))
3543 return PTR_ERR(label);
3544
David Quigley1775fd32013-05-22 12:50:42 -04003545 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003546 if (error < 0) {
3547 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003548 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003549 }
3550
3551 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3552 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3553 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3554
David Quigley14c43f72013-05-22 12:50:43 -04003555err_free_label:
3556 nfs4_label_free(label);
3557
Bryan Schumakerbae36242012-05-10 15:07:31 -04003558 return error;
3559}
3560
Manoj Naik6b97fd32006-06-09 09:34:29 -04003561/*
3562 * Get locations and (maybe) other attributes of a referral.
3563 * Note that we'll actually follow the referral later when
3564 * we detect fsid mismatch in inode revalidation
3565 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003566static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3567 const struct qstr *name, struct nfs_fattr *fattr,
3568 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003569{
3570 int status = -ENOMEM;
3571 struct page *page = NULL;
3572 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003573
3574 page = alloc_page(GFP_KERNEL);
3575 if (page == NULL)
3576 goto out;
3577 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3578 if (locations == NULL)
3579 goto out;
3580
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003581 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003582 if (status != 0)
3583 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003584
3585 /*
3586 * If the fsid didn't change, this is a migration event, not a
3587 * referral. Cause us to drop into the exception handler, which
3588 * will kick off migration recovery.
3589 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003590 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003591 dprintk("%s: server did not return a different fsid for"
3592 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003593 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003594 goto out;
3595 }
Andy Adamson533eb462011-06-13 18:25:56 -04003596 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3597 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003598
Andy Adamson533eb462011-06-13 18:25:56 -04003599 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003600 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003601 memset(fhandle, 0, sizeof(struct nfs_fh));
3602out:
3603 if (page)
3604 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003605 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003606 return status;
3607}
3608
David Quigley1775fd32013-05-22 12:50:42 -04003609static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3610 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611{
3612 struct nfs4_getattr_arg args = {
3613 .fh = fhandle,
3614 .bitmask = server->attr_bitmask,
3615 };
3616 struct nfs4_getattr_res res = {
3617 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003618 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 .server = server,
3620 };
3621 struct rpc_message msg = {
3622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3623 .rpc_argp = &args,
3624 .rpc_resp = &res,
3625 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003626
3627 args.bitmask = nfs4_bitmask(server, label);
3628
Trond Myklebust0e574af2005-10-27 22:12:38 -04003629 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003630 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
David Quigley1775fd32013-05-22 12:50:42 -04003633static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3634 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
3636 struct nfs4_exception exception = { };
3637 int err;
3638 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003639 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3640 trace_nfs4_getattr(server, fhandle, fattr, err);
3641 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 &exception);
3643 } while (exception.retry);
3644 return err;
3645}
3646
3647/*
3648 * The file is not closed if it is opened due to the a request to change
3649 * the size of the file. The open call will not be needed once the
3650 * VFS layer lookup-intents are implemented.
3651 *
3652 * Close is called when the inode is destroyed.
3653 * If we haven't opened the file for O_WRONLY, we
3654 * need to in the size_change case to obtain a stateid.
3655 *
3656 * Got race?
3657 * Because OPEN is always done by name in nfsv4, it is
3658 * possible that we opened a different file by the same
3659 * name. We can recognize this race condition, but we
3660 * can't do anything about it besides returning an error.
3661 *
3662 * This will be fixed with VFS changes (lookup-intent).
3663 */
3664static int
3665nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3666 struct iattr *sattr)
3667{
David Howells2b0143b2015-03-17 22:25:59 +00003668 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003669 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003670 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003671 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 int status;
3673
Peng Tao88ac8152014-09-12 11:04:10 +08003674 if (pnfs_ld_layoutret_on_setattr(inode) &&
3675 sattr->ia_valid & ATTR_SIZE &&
3676 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003677 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003678
Trond Myklebust0e574af2005-10-27 22:12:38 -04003679 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Andy Adamson26699402012-05-30 16:12:24 -04003681 /* Deal with open(O_TRUNC) */
3682 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003683 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003684
3685 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003686 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003687 return 0;
3688
Trond Myklebustd5308382005-11-04 15:33:38 -05003689 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003690 if (sattr->ia_valid & ATTR_FILE) {
3691 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003692
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003693 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003694 if (ctx) {
3695 cred = ctx->cred;
3696 state = ctx->state;
3697 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003698 }
3699
David Quigley14c43f72013-05-22 12:50:43 -04003700 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3701 if (IS_ERR(label))
3702 return PTR_ERR(label);
3703
3704 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003705 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003706 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003707 nfs_setsecurity(inode, fattr, label);
3708 }
David Quigley14c43f72013-05-22 12:50:43 -04003709 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return status;
3711}
3712
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003713static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3714 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003715 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003716{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003717 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003718 int status;
3719 struct nfs4_lookup_arg args = {
3720 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003721 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003722 .name = name,
3723 };
3724 struct nfs4_lookup_res res = {
3725 .server = server,
3726 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003727 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003728 .fh = fhandle,
3729 };
3730 struct rpc_message msg = {
3731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3732 .rpc_argp = &args,
3733 .rpc_resp = &res,
3734 };
3735
David Quigleyaa9c2662013-05-22 12:50:44 -04003736 args.bitmask = nfs4_bitmask(server, label);
3737
David Howells2b3de442006-08-22 20:06:09 -04003738 nfs_fattr_init(fattr);
3739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003741 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 dprintk("NFS reply lookup: %d\n", status);
3743 return status;
3744}
3745
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003746static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003747{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003748 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003749 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003750 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3751 fattr->nlink = 2;
3752}
3753
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003754static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003755 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003756 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003757{
3758 struct nfs4_exception exception = { };
3759 struct rpc_clnt *client = *clnt;
3760 int err;
3761 do {
David Quigley1775fd32013-05-22 12:50:42 -04003762 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003763 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003764 switch (err) {
3765 case -NFS4ERR_BADNAME:
3766 err = -ENOENT;
3767 goto out;
3768 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003769 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003770 if (err == -NFS4ERR_MOVED)
3771 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003772 goto out;
3773 case -NFS4ERR_WRONGSEC:
3774 err = -EPERM;
3775 if (client != *clnt)
3776 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003777 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003778 if (IS_ERR(client))
3779 return PTR_ERR(client);
3780
3781 exception.retry = 1;
3782 break;
3783 default:
3784 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3785 }
3786 } while (exception.retry);
3787
3788out:
3789 if (err == 0)
3790 *clnt = client;
3791 else if (client != *clnt)
3792 rpc_shutdown_client(client);
3793
3794 return err;
3795}
3796
Al Virobeffb8f2016-07-20 16:34:42 -04003797static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003798 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3799 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003801 int status;
3802 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003803
David Quigley1775fd32013-05-22 12:50:42 -04003804 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003805 if (client != NFS_CLIENT(dir)) {
3806 rpc_shutdown_client(client);
3807 nfs_fixup_secinfo_attributes(fattr);
3808 }
3809 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
3811
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003812struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003813nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003814 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3815{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003816 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003817 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003818
David Quigley1775fd32013-05-22 12:50:42 -04003819 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003820 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003821 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003822 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003823}
3824
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3826{
Trond Myklebust76b32992007-08-10 17:45:11 -04003827 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 struct nfs4_accessargs args = {
3829 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003830 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003832 struct nfs4_accessres res = {
3833 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003834 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 struct rpc_message msg = {
3836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3837 .rpc_argp = &args,
3838 .rpc_resp = &res,
3839 .rpc_cred = entry->cred,
3840 };
3841 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003842 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
3844 /*
3845 * Determine which access bits we want to ask for...
3846 */
3847 if (mode & MAY_READ)
3848 args.access |= NFS4_ACCESS_READ;
3849 if (S_ISDIR(inode->i_mode)) {
3850 if (mode & MAY_WRITE)
3851 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3852 if (mode & MAY_EXEC)
3853 args.access |= NFS4_ACCESS_LOOKUP;
3854 } else {
3855 if (mode & MAY_WRITE)
3856 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3857 if (mode & MAY_EXEC)
3858 args.access |= NFS4_ACCESS_EXECUTE;
3859 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003860
3861 res.fattr = nfs_alloc_fattr();
3862 if (res.fattr == NULL)
3863 return -ENOMEM;
3864
Bryan Schumaker7c513052011-03-24 17:12:24 +00003865 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003867 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003868 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003870 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 return status;
3872}
3873
3874static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3875{
3876 struct nfs4_exception exception = { };
3877 int err;
3878 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003879 err = _nfs4_proc_access(inode, entry);
3880 trace_nfs4_access(inode, err);
3881 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 &exception);
3883 } while (exception.retry);
3884 return err;
3885}
3886
3887/*
3888 * TODO: For the time being, we don't try to get any attributes
3889 * along with any of the zero-copy operations READ, READDIR,
3890 * READLINK, WRITE.
3891 *
3892 * In the case of the first three, we want to put the GETATTR
3893 * after the read-type operation -- this is because it is hard
3894 * to predict the length of a GETATTR response in v4, and thus
3895 * align the READ data correctly. This means that the GETATTR
3896 * may end up partially falling into the page cache, and we should
3897 * shift it into the 'tail' of the xdr_buf before processing.
3898 * To do this efficiently, we need to know the total length
3899 * of data received, which doesn't seem to be available outside
3900 * of the RPC layer.
3901 *
3902 * In the case of WRITE, we also want to put the GETATTR after
3903 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003904 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 *
3906 * Both of these changes to the XDR layer would in fact be quite
3907 * minor, but I decided to leave them for a subsequent patch.
3908 */
3909static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3910 unsigned int pgbase, unsigned int pglen)
3911{
3912 struct nfs4_readlink args = {
3913 .fh = NFS_FH(inode),
3914 .pgbase = pgbase,
3915 .pglen = pglen,
3916 .pages = &page,
3917 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003918 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 struct rpc_message msg = {
3920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3921 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003922 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 };
3924
Bryan Schumaker7c513052011-03-24 17:12:24 +00003925 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 -07003926}
3927
3928static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3929 unsigned int pgbase, unsigned int pglen)
3930{
3931 struct nfs4_exception exception = { };
3932 int err;
3933 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003934 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3935 trace_nfs4_readlink(inode, err);
3936 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 &exception);
3938 } while (exception.retry);
3939 return err;
3940}
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003943 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945static int
3946nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003947 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
David Quigleyaa9c2662013-05-22 12:50:44 -04003949 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003950 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 int status = 0;
3953
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003954 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3955 if (IS_ERR(ctx))
3956 return PTR_ERR(ctx);
3957
David Quigleyaa9c2662013-05-22 12:50:44 -04003958 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3959
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003960 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003961 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 if (IS_ERR(state)) {
3963 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003967 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003968 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return status;
3970}
3971
Al Virobeffb8f2016-07-20 16:34:42 -04003972static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Trond Myklebust16e42952005-10-27 22:12:44 -04003974 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003975 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003977 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003979 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003980 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003981 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3984 .rpc_argp = &args,
3985 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003987 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003988
Bryan Schumaker7c513052011-03-24 17:12:24 +00003989 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003990 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003991 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 return status;
3993}
3994
Al Virobeffb8f2016-07-20 16:34:42 -04003995static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
3997 struct nfs4_exception exception = { };
3998 int err;
3999 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004000 err = _nfs4_proc_remove(dir, name);
4001 trace_nfs4_remove(dir, name, err);
4002 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 &exception);
4004 } while (exception.retry);
4005 return err;
4006}
4007
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004008static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004010 struct nfs_server *server = NFS_SERVER(dir);
4011 struct nfs_removeargs *args = msg->rpc_argp;
4012 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004014 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004016 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004017
4018 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019}
4020
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004021static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4022{
Al Viro884be172016-04-28 23:56:31 -04004023 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004024 &data->args.seq_args,
4025 &data->res.seq_res,
4026 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004029static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004031 struct nfs_unlinkdata *data = task->tk_calldata;
4032 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004033
Trond Myklebust14516c32010-07-31 14:29:06 -04004034 if (!nfs4_sequence_done(task, &res->seq_res))
4035 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004036 if (nfs4_async_handle_error(task, res->server, NULL,
4037 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004038 return 0;
4039 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004040 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Jeff Laytond3d41522010-09-17 17:31:57 -04004043static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4044{
4045 struct nfs_server *server = NFS_SERVER(dir);
4046 struct nfs_renameargs *arg = msg->rpc_argp;
4047 struct nfs_renameres *res = msg->rpc_resp;
4048
4049 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004050 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004051 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004052}
4053
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004054static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4055{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004056 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4057 &data->args.seq_args,
4058 &data->res.seq_res,
4059 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004060}
4061
4062static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4063 struct inode *new_dir)
4064{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004065 struct nfs_renamedata *data = task->tk_calldata;
4066 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004067
4068 if (!nfs4_sequence_done(task, &res->seq_res))
4069 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004070 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004071 return 0;
4072
4073 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004074 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004075 return 1;
4076}
4077
Al Virobeffb8f2016-07-20 16:34:42 -04004078static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004080 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 struct nfs4_link_arg arg = {
4082 .fh = NFS_FH(inode),
4083 .dir_fh = NFS_FH(dir),
4084 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004085 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004087 struct nfs4_link_res res = {
4088 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004089 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 struct rpc_message msg = {
4092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4093 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004094 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004096 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Trond Myklebust136f2622010-04-16 16:22:49 -04004098 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004099 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004100 goto out;
4101
David Quigley14c43f72013-05-22 12:50:43 -04004102 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4103 if (IS_ERR(res.label)) {
4104 status = PTR_ERR(res.label);
4105 goto out;
4106 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004107 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004108
Bryan Schumaker7c513052011-03-24 17:12:24 +00004109 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004110 if (!status) {
4111 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004112 status = nfs_post_op_update_inode(inode, res.fattr);
4113 if (!status)
4114 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004115 }
David Quigley14c43f72013-05-22 12:50:43 -04004116
4117
4118 nfs4_label_free(res.label);
4119
Trond Myklebust136f2622010-04-16 16:22:49 -04004120out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004121 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 return status;
4123}
4124
Al Virobeffb8f2016-07-20 16:34:42 -04004125static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
4127 struct nfs4_exception exception = { };
4128 int err;
4129 do {
4130 err = nfs4_handle_exception(NFS_SERVER(inode),
4131 _nfs4_proc_link(inode, dir, name),
4132 &exception);
4133 } while (exception.retry);
4134 return err;
4135}
4136
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004137struct nfs4_createdata {
4138 struct rpc_message msg;
4139 struct nfs4_create_arg arg;
4140 struct nfs4_create_res res;
4141 struct nfs_fh fh;
4142 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004143 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004144};
4145
4146static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004147 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004148{
4149 struct nfs4_createdata *data;
4150
4151 data = kzalloc(sizeof(*data), GFP_KERNEL);
4152 if (data != NULL) {
4153 struct nfs_server *server = NFS_SERVER(dir);
4154
David Quigley14c43f72013-05-22 12:50:43 -04004155 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4156 if (IS_ERR(data->label))
4157 goto out_free;
4158
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004159 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4160 data->msg.rpc_argp = &data->arg;
4161 data->msg.rpc_resp = &data->res;
4162 data->arg.dir_fh = NFS_FH(dir);
4163 data->arg.server = server;
4164 data->arg.name = name;
4165 data->arg.attrs = sattr;
4166 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004167 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004168 data->res.server = server;
4169 data->res.fh = &data->fh;
4170 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004171 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004172 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004173 }
4174 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004175out_free:
4176 kfree(data);
4177 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004178}
4179
4180static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4181{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004182 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004183 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004184 if (status == 0) {
4185 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004186 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187 }
4188 return status;
4189}
4190
4191static void nfs4_free_createdata(struct nfs4_createdata *data)
4192{
David Quigley14c43f72013-05-22 12:50:43 -04004193 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004194 kfree(data);
4195}
4196
Chuck Lever4f390c12006-08-22 20:06:22 -04004197static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004198 struct page *page, unsigned int len, struct iattr *sattr,
4199 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004201 struct nfs4_createdata *data;
4202 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Chuck Lever94a6d752006-08-22 20:06:23 -04004204 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004205 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004206
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004207 status = -ENOMEM;
4208 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4209 if (data == NULL)
4210 goto out;
4211
4212 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4213 data->arg.u.symlink.pages = &page;
4214 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004215 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004217 status = nfs4_do_create(dir, dentry, data);
4218
4219 nfs4_free_createdata(data);
4220out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 return status;
4222}
4223
Chuck Lever4f390c12006-08-22 20:06:22 -04004224static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004225 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
4227 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004228 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004230
4231 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004234 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4235 trace_nfs4_symlink(dir, &dentry->d_name, err);
4236 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 &exception);
4238 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004239
4240 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return err;
4242}
4243
4244static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004245 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004247 struct nfs4_createdata *data;
4248 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004250 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4251 if (data == NULL)
4252 goto out;
4253
David Quigley1775fd32013-05-22 12:50:42 -04004254 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004255 status = nfs4_do_create(dir, dentry, data);
4256
4257 nfs4_free_createdata(data);
4258out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 return status;
4260}
4261
4262static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4263 struct iattr *sattr)
4264{
4265 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004266 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004268
David Quigleyaa9c2662013-05-22 12:50:44 -04004269 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4270
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004271 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004273 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4274 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4275 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 &exception);
4277 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004278 nfs4_label_release_security(label);
4279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 return err;
4281}
4282
4283static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004284 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285{
David Howells2b0143b2015-03-17 22:25:59 +00004286 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 struct nfs4_readdir_arg args = {
4288 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004289 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 .pgbase = 0,
4291 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004292 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004293 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 };
4295 struct nfs4_readdir_res res;
4296 struct rpc_message msg = {
4297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4298 .rpc_argp = &args,
4299 .rpc_resp = &res,
4300 .rpc_cred = cred,
4301 };
4302 int status;
4303
Al Viro6de14722013-09-16 10:53:17 -04004304 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4305 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004306 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004307 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004309 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 -05004310 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004311 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004312 status += args.pgbase;
4313 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004314
4315 nfs_invalidate_atime(dir);
4316
Harvey Harrison3110ff82008-05-02 13:42:44 -07004317 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 return status;
4319}
4320
4321static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004322 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
4324 struct nfs4_exception exception = { };
4325 int err;
4326 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004327 err = _nfs4_proc_readdir(dentry, cred, cookie,
4328 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004329 trace_nfs4_readdir(d_inode(dentry), err);
4330 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 &exception);
4332 } while (exception.retry);
4333 return err;
4334}
4335
4336static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004337 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004339 struct nfs4_createdata *data;
4340 int mode = sattr->ia_mode;
4341 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004343 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4344 if (data == NULL)
4345 goto out;
4346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004350 data->arg.ftype = NF4BLK;
4351 data->arg.u.device.specdata1 = MAJOR(rdev);
4352 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 }
4354 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004355 data->arg.ftype = NF4CHR;
4356 data->arg.u.device.specdata1 = MAJOR(rdev);
4357 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004358 } else if (!S_ISSOCK(mode)) {
4359 status = -EINVAL;
4360 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
David Quigley1775fd32013-05-22 12:50:42 -04004362
David Quigleyaa9c2662013-05-22 12:50:44 -04004363 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004364 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004365out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004366 nfs4_free_createdata(data);
4367out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 return status;
4369}
4370
4371static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4372 struct iattr *sattr, dev_t rdev)
4373{
4374 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004375 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004377
David Quigleyaa9c2662013-05-22 12:50:44 -04004378 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4379
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004380 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004382 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4383 trace_nfs4_mknod(dir, &dentry->d_name, err);
4384 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 &exception);
4386 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004387
4388 nfs4_label_release_security(label);
4389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return err;
4391}
4392
4393static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4394 struct nfs_fsstat *fsstat)
4395{
4396 struct nfs4_statfs_arg args = {
4397 .fh = fhandle,
4398 .bitmask = server->attr_bitmask,
4399 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004400 struct nfs4_statfs_res res = {
4401 .fsstat = fsstat,
4402 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 struct rpc_message msg = {
4404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4405 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004406 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 };
4408
Trond Myklebust0e574af2005-10-27 22:12:38 -04004409 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004410 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411}
4412
4413static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4414{
4415 struct nfs4_exception exception = { };
4416 int err;
4417 do {
4418 err = nfs4_handle_exception(server,
4419 _nfs4_proc_statfs(server, fhandle, fsstat),
4420 &exception);
4421 } while (exception.retry);
4422 return err;
4423}
4424
4425static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4426 struct nfs_fsinfo *fsinfo)
4427{
4428 struct nfs4_fsinfo_arg args = {
4429 .fh = fhandle,
4430 .bitmask = server->attr_bitmask,
4431 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004432 struct nfs4_fsinfo_res res = {
4433 .fsinfo = fsinfo,
4434 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 struct rpc_message msg = {
4436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4437 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004438 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 };
4440
Bryan Schumaker7c513052011-03-24 17:12:24 +00004441 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
4444static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4445{
4446 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004447 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 int err;
4449
4450 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004451 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004452 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004453 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004454 nfs4_set_lease_period(server->nfs_client,
4455 fsinfo->lease_time * HZ,
4456 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004457 break;
4458 }
4459 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 } while (exception.retry);
4461 return err;
4462}
4463
4464static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4465{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004466 int error;
4467
Trond Myklebust0e574af2005-10-27 22:12:38 -04004468 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004469 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004470 if (error == 0) {
4471 /* block layout checks this! */
4472 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004473 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004474 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004475
4476 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
4479static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4480 struct nfs_pathconf *pathconf)
4481{
4482 struct nfs4_pathconf_arg args = {
4483 .fh = fhandle,
4484 .bitmask = server->attr_bitmask,
4485 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004486 struct nfs4_pathconf_res res = {
4487 .pathconf = pathconf,
4488 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 struct rpc_message msg = {
4490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4491 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004492 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 };
4494
4495 /* None of the pathconf attributes are mandatory to implement */
4496 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4497 memset(pathconf, 0, sizeof(*pathconf));
4498 return 0;
4499 }
4500
Trond Myklebust0e574af2005-10-27 22:12:38 -04004501 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004502 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
4505static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4506 struct nfs_pathconf *pathconf)
4507{
4508 struct nfs4_exception exception = { };
4509 int err;
4510
4511 do {
4512 err = nfs4_handle_exception(server,
4513 _nfs4_proc_pathconf(server, fhandle, pathconf),
4514 &exception);
4515 } while (exception.retry);
4516 return err;
4517}
4518
Trond Myklebust5521abf2013-03-16 20:54:34 -04004519int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004520 const struct nfs_open_context *ctx,
4521 const struct nfs_lock_context *l_ctx,
4522 fmode_t fmode)
4523{
4524 const struct nfs_lockowner *lockowner = NULL;
4525
4526 if (l_ctx != NULL)
4527 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004528 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004529}
4530EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4531
Trond Myklebust5521abf2013-03-16 20:54:34 -04004532static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4533 const struct nfs_open_context *ctx,
4534 const struct nfs_lock_context *l_ctx,
4535 fmode_t fmode)
4536{
4537 nfs4_stateid current_stateid;
4538
Trond Myklebuste1253be2014-03-05 08:44:23 -05004539 /* If the current stateid represents a lost lock, then exit */
4540 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4541 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004542 return nfs4_stateid_match(stateid, &current_stateid);
4543}
4544
4545static bool nfs4_error_stateid_expired(int err)
4546{
4547 switch (err) {
4548 case -NFS4ERR_DELEG_REVOKED:
4549 case -NFS4ERR_ADMIN_REVOKED:
4550 case -NFS4ERR_BAD_STATEID:
4551 case -NFS4ERR_STALE_STATEID:
4552 case -NFS4ERR_OLD_STATEID:
4553 case -NFS4ERR_OPENMODE:
4554 case -NFS4ERR_EXPIRED:
4555 return true;
4556 }
4557 return false;
4558}
4559
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004560static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004562 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004564 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004565 if (task->tk_status < 0) {
4566 struct nfs4_exception exception = {
4567 .inode = hdr->inode,
4568 .state = hdr->args.context->state,
4569 .stateid = &hdr->args.stateid,
4570 };
4571 task->tk_status = nfs4_async_handle_exception(task,
4572 server, task->tk_status, &exception);
4573 if (exception.retry) {
4574 rpc_restart_call_prepare(task);
4575 return -EAGAIN;
4576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004580 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582}
4583
Trond Myklebust5521abf2013-03-16 20:54:34 -04004584static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004585 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004586{
4587
4588 if (!nfs4_error_stateid_expired(task->tk_status) ||
4589 nfs4_stateid_is_current(&args->stateid,
4590 args->context,
4591 args->lock_context,
4592 FMODE_READ))
4593 return false;
4594 rpc_restart_call_prepare(task);
4595 return true;
4596}
4597
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004598static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004599{
4600
4601 dprintk("--> %s\n", __func__);
4602
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004603 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004604 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004606 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004607 if (task->tk_status > 0)
4608 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004609 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4610 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004611}
4612
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004613static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4614 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004616 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004617 if (!hdr->pgio_done_cb)
4618 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004619 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004620 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621}
4622
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4624 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004625{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004626 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4627 &hdr->args.seq_args,
4628 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004629 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004630 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004631 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4632 hdr->args.lock_context,
4633 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004634 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004635 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004636 return -EIO;
4637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638}
4639
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004640static int nfs4_write_done_cb(struct rpc_task *task,
4641 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004643 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004644
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004645 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004646 if (task->tk_status < 0) {
4647 struct nfs4_exception exception = {
4648 .inode = hdr->inode,
4649 .state = hdr->args.context->state,
4650 .stateid = &hdr->args.stateid,
4651 };
4652 task->tk_status = nfs4_async_handle_exception(task,
4653 NFS_SERVER(inode), task->tk_status,
4654 &exception);
4655 if (exception.retry) {
4656 rpc_restart_call_prepare(task);
4657 return -EAGAIN;
4658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004660 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004661 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004662 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004663 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
Trond Myklebust5521abf2013-03-16 20:54:34 -04004667static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004668 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004669{
4670
4671 if (!nfs4_error_stateid_expired(task->tk_status) ||
4672 nfs4_stateid_is_current(&args->stateid,
4673 args->context,
4674 args->lock_context,
4675 FMODE_WRITE))
4676 return false;
4677 rpc_restart_call_prepare(task);
4678 return true;
4679}
4680
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004681static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004682{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004683 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004684 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004685 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004686 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004687 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4688 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004689}
4690
Trond Myklebust5a37f852012-04-28 14:55:16 -04004691static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004692bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004693{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004694 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004695 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004696 return false;
4697 /* Otherwise, request attributes if and only if we don't hold
4698 * a delegation
4699 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004700 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004701}
Fred Isamana69aef12011-03-03 15:13:47 +00004702
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4704 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004706 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004707
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004708 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4709 hdr->args.bitmask = NULL;
4710 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004711 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004712 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004713
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004714 if (!hdr->pgio_done_cb)
4715 hdr->pgio_done_cb = nfs4_write_done_cb;
4716 hdr->res.server = server;
4717 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004719 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004720 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
Fred Isaman0b7c0152012-04-20 14:47:39 -04004723static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4724{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004725 nfs4_setup_sequence(NFS_SERVER(data->inode),
4726 &data->args.seq_args,
4727 &data->res.seq_res,
4728 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729}
4730
Fred Isaman0b7c0152012-04-20 14:47:39 -04004731static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004734
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004735 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004736 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4737 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004738 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004739 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742}
4743
Fred Isaman0b7c0152012-04-20 14:47:39 -04004744static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004745{
4746 if (!nfs4_sequence_done(task, &data->res.seq_res))
4747 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004748 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004749}
4750
Fred Isaman0b7c0152012-04-20 14:47:39 -04004751static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004753 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004754
Fred Isaman0b7c0152012-04-20 14:47:39 -04004755 if (data->commit_done_cb == NULL)
4756 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004757 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004758 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004759 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760}
4761
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004762struct nfs4_renewdata {
4763 struct nfs_client *client;
4764 unsigned long timestamp;
4765};
4766
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767/*
4768 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4769 * standalone procedure for queueing an asynchronous RENEW.
4770 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004771static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004772{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004773 struct nfs4_renewdata *data = calldata;
4774 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004775
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004776 if (atomic_read(&clp->cl_count) > 1)
4777 nfs4_schedule_state_renewal(clp);
4778 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004779 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004780}
4781
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004782static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004784 struct nfs4_renewdata *data = calldata;
4785 struct nfs_client *clp = data->client;
4786 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004788 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004789 switch (task->tk_status) {
4790 case 0:
4791 break;
4792 case -NFS4ERR_LEASE_MOVED:
4793 nfs4_schedule_lease_moved_recovery(clp);
4794 break;
4795 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004796 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004797 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4798 return;
4799 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004800 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004801 return;
4802 }
4803 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004805 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806}
4807
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004808static const struct rpc_call_ops nfs4_renew_ops = {
4809 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004810 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004811};
4812
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004813static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814{
4815 struct rpc_message msg = {
4816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4817 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004818 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004820 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004822 if (renew_flags == 0)
4823 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004824 if (!atomic_inc_not_zero(&clp->cl_count))
4825 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004826 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004827 if (data == NULL)
4828 return -ENOMEM;
4829 data->client = clp;
4830 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004831 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004832 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833}
4834
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004835static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
4837 struct rpc_message msg = {
4838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4839 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004840 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 };
4842 unsigned long now = jiffies;
4843 int status;
4844
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004845 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 if (status < 0)
4847 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004848 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 return 0;
4850}
4851
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004852static inline int nfs4_server_supports_acls(struct nfs_server *server)
4853{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004854 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004855}
4856
Trond Myklebust21f498c2012-08-24 10:59:25 -04004857/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4858 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004859 * the stack.
4860 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004861#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004862
Neil Hormane9e3d722011-03-04 19:26:03 -05004863static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004864 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004865{
4866 struct page *newpage, **spages;
4867 int rc = 0;
4868 size_t len;
4869 spages = pages;
4870
4871 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004872 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004873 newpage = alloc_page(GFP_KERNEL);
4874
4875 if (newpage == NULL)
4876 goto unwind;
4877 memcpy(page_address(newpage), buf, len);
4878 buf += len;
4879 buflen -= len;
4880 *pages++ = newpage;
4881 rc++;
4882 } while (buflen != 0);
4883
4884 return rc;
4885
4886unwind:
4887 for(; rc > 0; rc--)
4888 __free_page(spages[rc-1]);
4889 return -ENOMEM;
4890}
4891
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004892struct nfs4_cached_acl {
4893 int cached;
4894 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004895 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004896};
4897
4898static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004899{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004900 struct nfs_inode *nfsi = NFS_I(inode);
4901
4902 spin_lock(&inode->i_lock);
4903 kfree(nfsi->nfs4_acl);
4904 nfsi->nfs4_acl = acl;
4905 spin_unlock(&inode->i_lock);
4906}
4907
4908static void nfs4_zap_acl_attr(struct inode *inode)
4909{
4910 nfs4_set_cached_acl(inode, NULL);
4911}
4912
4913static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4914{
4915 struct nfs_inode *nfsi = NFS_I(inode);
4916 struct nfs4_cached_acl *acl;
4917 int ret = -ENOENT;
4918
4919 spin_lock(&inode->i_lock);
4920 acl = nfsi->nfs4_acl;
4921 if (acl == NULL)
4922 goto out;
4923 if (buf == NULL) /* user is just asking for length */
4924 goto out_len;
4925 if (acl->cached == 0)
4926 goto out;
4927 ret = -ERANGE; /* see getxattr(2) man page */
4928 if (acl->len > buflen)
4929 goto out;
4930 memcpy(buf, acl->data, acl->len);
4931out_len:
4932 ret = acl->len;
4933out:
4934 spin_unlock(&inode->i_lock);
4935 return ret;
4936}
4937
Sachin Prabhu5794d212012-04-17 14:36:40 +01004938static 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 +00004939{
4940 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004941 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004942
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004943 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004944 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004945 if (acl == NULL)
4946 goto out;
4947 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004948 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004949 } else {
4950 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4951 if (acl == NULL)
4952 goto out;
4953 acl->cached = 0;
4954 }
4955 acl->len = acl_len;
4956out:
4957 nfs4_set_cached_acl(inode, acl);
4958}
4959
Andy Adamsonbf118a32011-12-07 11:55:27 -05004960/*
4961 * The getxattr API returns the required buffer length when called with a
4962 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4963 * the required buf. On a NULL buf, we send a page of data to the server
4964 * guessing that the ACL request can be serviced by a page. If so, we cache
4965 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4966 * the cache. If not so, we throw away the page, and cache the required
4967 * length. The next getxattr call will then produce another round trip to
4968 * the server, this time with the input buf of the required size.
4969 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004970static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004971{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004972 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004973 struct nfs_getaclargs args = {
4974 .fh = NFS_FH(inode),
4975 .acl_pages = pages,
4976 .acl_len = buflen,
4977 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004978 struct nfs_getaclres res = {
4979 .acl_len = buflen,
4980 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004981 struct rpc_message msg = {
4982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4983 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004984 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004985 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004986 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4987 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004988
Andy Adamsonbf118a32011-12-07 11:55:27 -05004989 /* As long as we're doing a round trip to the server anyway,
4990 * let's be prepared for a page of acl data. */
4991 if (npages == 0)
4992 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004993 if (npages > ARRAY_SIZE(pages))
4994 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004995
Andy Adamsonbf118a32011-12-07 11:55:27 -05004996 for (i = 0; i < npages; i++) {
4997 pages[i] = alloc_page(GFP_KERNEL);
4998 if (!pages[i])
4999 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005000 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005001
5002 /* for decoding across pages */
5003 res.acl_scratch = alloc_page(GFP_KERNEL);
5004 if (!res.acl_scratch)
5005 goto out_free;
5006
Andy Adamsonbf118a32011-12-07 11:55:27 -05005007 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005008
Peng Taode040be2012-01-10 22:42:47 +08005009 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005010 __func__, buf, buflen, npages, args.acl_len);
5011 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5012 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005013 if (ret)
5014 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005015
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005016 /* Handle the case where the passed-in buffer is too short */
5017 if (res.acl_flags & NFS4_ACL_TRUNC) {
5018 /* Did the user only issue a request for the acl length? */
5019 if (buf == NULL)
5020 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005021 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005022 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005023 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005024 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005025 if (buf) {
5026 if (res.acl_len > buflen) {
5027 ret = -ERANGE;
5028 goto out_free;
5029 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005030 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005031 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005032out_ok:
5033 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005034out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005035 for (i = 0; i < npages; i++)
5036 if (pages[i])
5037 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005038 if (res.acl_scratch)
5039 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005040 return ret;
5041}
5042
Trond Myklebust16b42892006-08-24 12:27:15 -04005043static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5044{
5045 struct nfs4_exception exception = { };
5046 ssize_t ret;
5047 do {
5048 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005049 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005050 if (ret >= 0)
5051 break;
5052 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5053 } while (exception.retry);
5054 return ret;
5055}
5056
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005057static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5058{
5059 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005060 int ret;
5061
5062 if (!nfs4_server_supports_acls(server))
5063 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005064 ret = nfs_revalidate_inode(server, inode);
5065 if (ret < 0)
5066 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005067 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5068 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005069 ret = nfs4_read_cached_acl(inode, buf, buflen);
5070 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005071 /* -ENOENT is returned if there is no ACL or if there is an ACL
5072 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005073 return ret;
5074 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005075}
5076
Trond Myklebust16b42892006-08-24 12:27:15 -04005077static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005078{
5079 struct nfs_server *server = NFS_SERVER(inode);
5080 struct page *pages[NFS4ACL_MAXPAGES];
5081 struct nfs_setaclargs arg = {
5082 .fh = NFS_FH(inode),
5083 .acl_pages = pages,
5084 .acl_len = buflen,
5085 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005086 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005087 struct rpc_message msg = {
5088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5089 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005090 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005091 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005092 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005093 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005094
5095 if (!nfs4_server_supports_acls(server))
5096 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005097 if (npages > ARRAY_SIZE(pages))
5098 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005099 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005100 if (i < 0)
5101 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005102 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005103 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005104
5105 /*
5106 * Free each page after tx, so the only ref left is
5107 * held by the network stack
5108 */
5109 for (; i > 0; i--)
5110 put_page(pages[i-1]);
5111
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005112 /*
5113 * Acl update can result in inode attribute update.
5114 * so mark the attribute cache invalid.
5115 */
5116 spin_lock(&inode->i_lock);
5117 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5118 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005119 nfs_access_zap_cache(inode);
5120 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005121 return ret;
5122}
5123
Trond Myklebust16b42892006-08-24 12:27:15 -04005124static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5125{
5126 struct nfs4_exception exception = { };
5127 int err;
5128 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005129 err = __nfs4_proc_set_acl(inode, buf, buflen);
5130 trace_nfs4_set_acl(inode, err);
5131 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005132 &exception);
5133 } while (exception.retry);
5134 return err;
5135}
5136
David Quigleyaa9c2662013-05-22 12:50:44 -04005137#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5138static int _nfs4_get_security_label(struct inode *inode, void *buf,
5139 size_t buflen)
5140{
5141 struct nfs_server *server = NFS_SERVER(inode);
5142 struct nfs_fattr fattr;
5143 struct nfs4_label label = {0, 0, buflen, buf};
5144
5145 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005146 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005147 .fh = NFS_FH(inode),
5148 .bitmask = bitmask,
5149 };
5150 struct nfs4_getattr_res res = {
5151 .fattr = &fattr,
5152 .label = &label,
5153 .server = server,
5154 };
5155 struct rpc_message msg = {
5156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005157 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005158 .rpc_resp = &res,
5159 };
5160 int ret;
5161
5162 nfs_fattr_init(&fattr);
5163
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005164 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005165 if (ret)
5166 return ret;
5167 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5168 return -ENOENT;
5169 if (buflen < label.len)
5170 return -ERANGE;
5171 return 0;
5172}
5173
5174static int nfs4_get_security_label(struct inode *inode, void *buf,
5175 size_t buflen)
5176{
5177 struct nfs4_exception exception = { };
5178 int err;
5179
5180 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5181 return -EOPNOTSUPP;
5182
5183 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005184 err = _nfs4_get_security_label(inode, buf, buflen);
5185 trace_nfs4_get_security_label(inode, err);
5186 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005187 &exception);
5188 } while (exception.retry);
5189 return err;
5190}
5191
5192static int _nfs4_do_set_security_label(struct inode *inode,
5193 struct nfs4_label *ilabel,
5194 struct nfs_fattr *fattr,
5195 struct nfs4_label *olabel)
5196{
5197
5198 struct iattr sattr = {0};
5199 struct nfs_server *server = NFS_SERVER(inode);
5200 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005201 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005202 .fh = NFS_FH(inode),
5203 .iap = &sattr,
5204 .server = server,
5205 .bitmask = bitmask,
5206 .label = ilabel,
5207 };
5208 struct nfs_setattrres res = {
5209 .fattr = fattr,
5210 .label = olabel,
5211 .server = server,
5212 };
5213 struct rpc_message msg = {
5214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005215 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005216 .rpc_resp = &res,
5217 };
5218 int status;
5219
Jeff Layton12207f62013-11-01 10:49:32 -04005220 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005221
Jeff Layton12207f62013-11-01 10:49:32 -04005222 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005223 if (status)
5224 dprintk("%s failed: %d\n", __func__, status);
5225
5226 return status;
5227}
5228
5229static int nfs4_do_set_security_label(struct inode *inode,
5230 struct nfs4_label *ilabel,
5231 struct nfs_fattr *fattr,
5232 struct nfs4_label *olabel)
5233{
5234 struct nfs4_exception exception = { };
5235 int err;
5236
5237 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005238 err = _nfs4_do_set_security_label(inode, ilabel,
5239 fattr, olabel);
5240 trace_nfs4_set_security_label(inode, err);
5241 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005242 &exception);
5243 } while (exception.retry);
5244 return err;
5245}
5246
5247static int
Al Viro59301222016-05-27 10:19:30 -04005248nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005249{
5250 struct nfs4_label ilabel, *olabel = NULL;
5251 struct nfs_fattr fattr;
5252 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005253 int status;
5254
5255 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5256 return -EOPNOTSUPP;
5257
5258 nfs_fattr_init(&fattr);
5259
5260 ilabel.pi = 0;
5261 ilabel.lfs = 0;
5262 ilabel.label = (char *)buf;
5263 ilabel.len = buflen;
5264
5265 cred = rpc_lookup_cred();
5266 if (IS_ERR(cred))
5267 return PTR_ERR(cred);
5268
5269 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5270 if (IS_ERR(olabel)) {
5271 status = -PTR_ERR(olabel);
5272 goto out;
5273 }
5274
5275 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5276 if (status == 0)
5277 nfs_setsecurity(inode, &fattr, olabel);
5278
5279 nfs4_label_free(olabel);
5280out:
5281 put_rpccred(cred);
5282 return status;
5283}
5284#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5285
5286
Chuck Leverf0920752012-05-21 22:45:41 -04005287static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5288 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005289{
5290 __be32 verf[2];
5291
Chuck Lever2c820d92012-05-21 22:45:33 -04005292 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5293 /* An impossible timestamp guarantees this value
5294 * will never match a generated boot time. */
5295 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005296 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005297 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005298 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005299 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5300 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005301 }
Chuck Levercd937102012-03-02 17:14:31 -05005302 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5303}
5304
Jeff Laytona3192682015-06-09 19:43:59 -04005305static int
5306nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005307{
Jeff Laytona3192682015-06-09 19:43:59 -04005308 size_t len;
5309 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005310
Trond Myklebustceb3a162015-01-03 15:16:04 -05005311 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005312 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005313
Jeff Laytona3192682015-06-09 19:43:59 -04005314 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005315 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005316 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5317 1 +
5318 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5319 1;
5320 rcu_read_unlock();
5321
5322 if (len > NFS4_OPAQUE_LIMIT + 1)
5323 return -EINVAL;
5324
5325 /*
5326 * Since this string is allocated at mount time, and held until the
5327 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5328 * about a memory-reclaim deadlock.
5329 */
5330 str = kmalloc(len, GFP_KERNEL);
5331 if (!str)
5332 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005333
Chuck Levere984a552012-09-14 17:24:21 -04005334 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005335 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005336 clp->cl_ipaddr,
5337 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5338 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005339 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005340
Jeff Laytona3192682015-06-09 19:43:59 -04005341 clp->cl_owner_id = str;
5342 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005343}
5344
Jeff Layton873e3852015-06-09 19:44:00 -04005345static int
5346nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005347{
Jeff Layton873e3852015-06-09 19:44:00 -04005348 size_t len;
5349 char *str;
5350
5351 len = 10 + 10 + 1 + 10 + 1 +
5352 strlen(nfs4_client_id_uniquifier) + 1 +
5353 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5354
5355 if (len > NFS4_OPAQUE_LIMIT + 1)
5356 return -EINVAL;
5357
5358 /*
5359 * Since this string is allocated at mount time, and held until the
5360 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5361 * about a memory-reclaim deadlock.
5362 */
5363 str = kmalloc(len, GFP_KERNEL);
5364 if (!str)
5365 return -ENOMEM;
5366
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005367 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005368 clp->rpc_ops->version, clp->cl_minorversion,
5369 nfs4_client_id_uniquifier,
5370 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005371 clp->cl_owner_id = str;
5372 return 0;
5373}
5374
5375static int
5376nfs4_init_uniform_client_string(struct nfs_client *clp)
5377{
Jeff Layton873e3852015-06-09 19:44:00 -04005378 size_t len;
5379 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005380
5381 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005382 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005383
5384 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005385 return nfs4_init_uniquifier_client_string(clp);
5386
5387 len = 10 + 10 + 1 + 10 + 1 +
5388 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5389
5390 if (len > NFS4_OPAQUE_LIMIT + 1)
5391 return -EINVAL;
5392
5393 /*
5394 * Since this string is allocated at mount time, and held until the
5395 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5396 * about a memory-reclaim deadlock.
5397 */
5398 str = kmalloc(len, GFP_KERNEL);
5399 if (!str)
5400 return -ENOMEM;
5401
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005402 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005403 clp->rpc_ops->version, clp->cl_minorversion,
5404 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005405 clp->cl_owner_id = str;
5406 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005407}
5408
Chuck Lever706cb8d2014-03-12 12:51:47 -04005409/*
5410 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5411 * services. Advertise one based on the address family of the
5412 * clientaddr.
5413 */
5414static unsigned int
5415nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5416{
5417 if (strchr(clp->cl_ipaddr, ':') != NULL)
5418 return scnprintf(buf, len, "tcp6");
5419 else
5420 return scnprintf(buf, len, "tcp");
5421}
5422
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005423static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5424{
5425 struct nfs4_setclientid *sc = calldata;
5426
5427 if (task->tk_status == 0)
5428 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5429}
5430
5431static const struct rpc_call_ops nfs4_setclientid_ops = {
5432 .rpc_call_done = nfs4_setclientid_done,
5433};
5434
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005435/**
5436 * nfs4_proc_setclientid - Negotiate client ID
5437 * @clp: state data structure
5438 * @program: RPC program for NFSv4 callback service
5439 * @port: IP port number for NFS4 callback service
5440 * @cred: RPC credential to use for this call
5441 * @res: where to place the result
5442 *
5443 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5444 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005445int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5446 unsigned short port, struct rpc_cred *cred,
5447 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448{
5449 nfs4_verifier sc_verifier;
5450 struct nfs4_setclientid setclientid = {
5451 .sc_verifier = &sc_verifier,
5452 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005453 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 };
5455 struct rpc_message msg = {
5456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5457 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005458 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005459 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005461 struct rpc_task *task;
5462 struct rpc_task_setup task_setup_data = {
5463 .rpc_client = clp->cl_rpcclient,
5464 .rpc_message = &msg,
5465 .callback_ops = &nfs4_setclientid_ops,
5466 .callback_data = &setclientid,
5467 .flags = RPC_TASK_TIMEOUT,
5468 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005469 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
Chuck Leverde734832012-07-11 16:30:50 -04005471 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005472 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005473
5474 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5475 status = nfs4_init_uniform_client_string(clp);
5476 else
Jeff Laytona3192682015-06-09 19:43:59 -04005477 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005478
5479 if (status)
5480 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005481
Chuck Leverde734832012-07-11 16:30:50 -04005482 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005483 setclientid.sc_netid_len =
5484 nfs4_init_callback_netid(clp,
5485 setclientid.sc_netid,
5486 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005487 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005488 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 clp->cl_ipaddr, port >> 8, port & 255);
5490
Jeff Layton3a6bb732015-06-09 19:43:57 -04005491 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005492 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005493 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005494 task = rpc_run_task(&task_setup_data);
5495 if (IS_ERR(task)) {
5496 status = PTR_ERR(task);
5497 goto out;
5498 }
5499 status = task->tk_status;
5500 if (setclientid.sc_cred) {
5501 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5502 put_rpccred(setclientid.sc_cred);
5503 }
5504 rpc_put_task(task);
5505out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005506 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005507 dprintk("NFS reply setclientid: %d\n", status);
5508 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509}
5510
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005511/**
5512 * nfs4_proc_setclientid_confirm - Confirm client ID
5513 * @clp: state data structure
5514 * @res: result of a previous SETCLIENTID
5515 * @cred: RPC credential to use for this call
5516 *
5517 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5518 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005519int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005520 struct nfs4_setclientid_res *arg,
5521 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 struct rpc_message msg = {
5524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005525 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005526 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 int status;
5529
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005530 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5531 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5532 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005533 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005534 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005535 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 return status;
5537}
5538
Trond Myklebustfe650402006-01-03 09:55:18 +01005539struct nfs4_delegreturndata {
5540 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005541 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005542 struct nfs_fh fh;
5543 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005544 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005545 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005546 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005547 struct inode *inode;
5548 bool roc;
5549 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005550};
5551
Trond Myklebustfe650402006-01-03 09:55:18 +01005552static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5553{
5554 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005555
Trond Myklebust14516c32010-07-31 14:29:06 -04005556 if (!nfs4_sequence_done(task, &data->res.seq_res))
5557 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005558
Trond Myklebustca8acf82013-08-13 10:36:56 -04005559 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005560 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005561 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005562 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005563 case -NFS4ERR_ADMIN_REVOKED:
5564 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005565 case -NFS4ERR_EXPIRED:
5566 nfs4_free_revoked_stateid(data->res.server,
5567 data->args.stateid,
5568 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005569 case -NFS4ERR_BAD_STATEID:
5570 case -NFS4ERR_OLD_STATEID:
5571 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005572 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005573 if (data->roc)
5574 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005575 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005576 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005577 if (nfs4_async_handle_error(task, data->res.server,
5578 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005579 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005580 return;
5581 }
5582 }
5583 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005584}
5585
5586static void nfs4_delegreturn_release(void *calldata)
5587{
Peng Tao039b7562014-07-03 13:05:02 +08005588 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005589 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005590
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005591 if (inode) {
5592 if (data->roc)
5593 pnfs_roc_release(inode);
5594 nfs_iput_and_deactive(inode);
5595 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005596 kfree(calldata);
5597}
5598
Andy Adamson938e1012009-04-01 09:22:28 -04005599static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5600{
5601 struct nfs4_delegreturndata *d_data;
5602
5603 d_data = (struct nfs4_delegreturndata *)data;
5604
Peng Tao500d7012015-09-22 11:35:22 +08005605 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5606 return;
5607
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005608 if (d_data->roc)
5609 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005610
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005611 nfs4_setup_sequence(d_data->res.server,
5612 &d_data->args.seq_args,
5613 &d_data->res.seq_res,
5614 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005615}
Andy Adamson938e1012009-04-01 09:22:28 -04005616
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005617static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005618 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005619 .rpc_call_done = nfs4_delegreturn_done,
5620 .rpc_release = nfs4_delegreturn_release,
5621};
5622
Trond Myklebuste6f81072008-01-24 18:14:34 -05005623static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005624{
5625 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005626 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005627 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005628 struct rpc_message msg = {
5629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5630 .rpc_cred = cred,
5631 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005632 struct rpc_task_setup task_setup_data = {
5633 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005634 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005635 .callback_ops = &nfs4_delegreturn_ops,
5636 .flags = RPC_TASK_ASYNC,
5637 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005638 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005639
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005640 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005641 if (data == NULL)
5642 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005643 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005644
5645 nfs4_state_protect(server->nfs_client,
5646 NFS_SP4_MACH_CRED_CLEANUP,
5647 &task_setup_data.rpc_client, &msg);
5648
Trond Myklebustfe650402006-01-03 09:55:18 +01005649 data->args.fhandle = &data->fh;
5650 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005651 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005652 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005653 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005654 data->res.fattr = &data->fattr;
5655 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005656 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005657 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005658 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005659 data->inode = nfs_igrab_and_active(inode);
5660 if (data->inode)
5661 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005662
Trond Myklebustc970aa82007-07-14 15:39:59 -04005663 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005664 msg.rpc_argp = &data->args;
5665 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005666 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005667 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005668 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005669 if (!issync)
5670 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005671 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005672 if (status != 0)
5673 goto out;
5674 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005675 if (status == 0)
5676 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5677 else
5678 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005679out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005680 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005681 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682}
5683
Trond Myklebuste6f81072008-01-24 18:14:34 -05005684int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685{
5686 struct nfs_server *server = NFS_SERVER(inode);
5687 struct nfs4_exception exception = { };
5688 int err;
5689 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005690 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005691 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 switch (err) {
5693 case -NFS4ERR_STALE_STATEID:
5694 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 case 0:
5696 return 0;
5697 }
5698 err = nfs4_handle_exception(server, err, &exception);
5699 } while (exception.retry);
5700 return err;
5701}
5702
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5704{
5705 struct inode *inode = state->inode;
5706 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005707 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005708 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005710 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005712 struct nfs_lockt_res res = {
5713 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 };
5715 struct rpc_message msg = {
5716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5717 .rpc_argp = &arg,
5718 .rpc_resp = &res,
5719 .rpc_cred = state->owner->so_cred,
5720 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 struct nfs4_lock_state *lsp;
5722 int status;
5723
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005724 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005725 status = nfs4_set_lock_state(state, request);
5726 if (status != 0)
5727 goto out;
5728 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005729 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005730 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005731 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005732 switch (status) {
5733 case 0:
5734 request->fl_type = F_UNLCK;
5735 break;
5736 case -NFS4ERR_DENIED:
5737 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005739 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005740 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005741out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 return status;
5743}
5744
5745static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5746{
5747 struct nfs4_exception exception = { };
5748 int err;
5749
5750 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005751 err = _nfs4_proc_getlk(state, cmd, request);
5752 trace_nfs4_get_lock(request, state, cmd, err);
5753 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 &exception);
5755 } while (exception.retry);
5756 return err;
5757}
5758
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005759struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005760 struct nfs_locku_args arg;
5761 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005762 struct nfs4_lock_state *lsp;
5763 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005764 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005765 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005766 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005767};
5768
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005769static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5770 struct nfs_open_context *ctx,
5771 struct nfs4_lock_state *lsp,
5772 struct nfs_seqid *seqid)
5773{
5774 struct nfs4_unlockdata *p;
5775 struct inode *inode = lsp->ls_state->inode;
5776
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005777 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005778 if (p == NULL)
5779 return NULL;
5780 p->arg.fh = NFS_FH(inode);
5781 p->arg.fl = &p->fl;
5782 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005783 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005784 p->lsp = lsp;
5785 atomic_inc(&lsp->ls_count);
5786 /* Ensure we don't close file until we're done freeing locks! */
5787 p->ctx = get_nfs_open_context(ctx);
5788 memcpy(&p->fl, fl, sizeof(p->fl));
5789 p->server = NFS_SERVER(inode);
5790 return p;
5791}
5792
Trond Myklebust06f814a2006-01-03 09:55:07 +01005793static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005794{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005795 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005796 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005797 nfs4_put_lock_state(calldata->lsp);
5798 put_nfs_open_context(calldata->ctx);
5799 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005800}
5801
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005802static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005803{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005804 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005805
Trond Myklebust14516c32010-07-31 14:29:06 -04005806 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5807 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005808 switch (task->tk_status) {
5809 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005810 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005811 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005812 if (nfs4_update_lock_stateid(calldata->lsp,
5813 &calldata->res.stateid))
5814 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005815 case -NFS4ERR_ADMIN_REVOKED:
5816 case -NFS4ERR_EXPIRED:
5817 nfs4_free_revoked_stateid(calldata->server,
5818 &calldata->arg.stateid,
5819 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005820 case -NFS4ERR_BAD_STATEID:
5821 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005822 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005823 if (!nfs4_stateid_match(&calldata->arg.stateid,
5824 &calldata->lsp->ls_stateid))
5825 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005826 break;
5827 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005828 if (nfs4_async_handle_error(task, calldata->server,
5829 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005830 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005831 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005832 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005833}
5834
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005835static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005836{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005837 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005839 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005840 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005841 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005842 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005843 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005844 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005845 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005846 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005847 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005848 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005849 &calldata->res.seq_res,
5850 task) != 0)
5851 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005852 return;
5853out_no_action:
5854 task->tk_action = NULL;
5855out_wait:
5856 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857}
5858
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005859static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005860 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005861 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005862 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005863};
5864
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005865static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5866 struct nfs_open_context *ctx,
5867 struct nfs4_lock_state *lsp,
5868 struct nfs_seqid *seqid)
5869{
5870 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005871 struct rpc_message msg = {
5872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5873 .rpc_cred = ctx->cred,
5874 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005875 struct rpc_task_setup task_setup_data = {
5876 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005877 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005878 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005879 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005880 .flags = RPC_TASK_ASYNC,
5881 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005882
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005883 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5884 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5885
Frank Filz137d6ac2007-07-09 15:32:29 -07005886 /* Ensure this is an unlock - when canceling a lock, the
5887 * canceled lock is passed in, and it won't be an unlock.
5888 */
5889 fl->fl_type = F_UNLCK;
5890
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005891 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5892 if (data == NULL) {
5893 nfs_free_seqid(seqid);
5894 return ERR_PTR(-ENOMEM);
5895 }
5896
Chuck Levera9c92d62013-08-09 12:48:18 -04005897 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005898 msg.rpc_argp = &data->arg;
5899 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005900 task_setup_data.callback_data = data;
5901 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005902}
5903
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5905{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005906 struct inode *inode = state->inode;
5907 struct nfs4_state_owner *sp = state->owner;
5908 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005909 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005910 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005911 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005912 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005913 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005914 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
Trond Myklebust9b073572006-06-29 16:38:34 -04005916 status = nfs4_set_lock_state(state, request);
5917 /* Unlock _before_ we do the RPC call */
5918 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005919 /* Exclude nfs_delegation_claim_locks() */
5920 mutex_lock(&sp->so_delegreturn_mutex);
5921 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005922 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005923 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005924 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005925 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005926 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005927 }
5928 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005929 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005930 if (status != 0)
5931 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005932 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005933 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005934 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5935 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005936 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5937 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005938 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005939 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005940 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005941 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005942 status = PTR_ERR(task);
5943 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005944 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005945 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005946 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005947out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005948 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005949 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005950 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951}
5952
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005953struct nfs4_lockdata {
5954 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005955 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005956 struct nfs4_lock_state *lsp;
5957 struct nfs_open_context *ctx;
5958 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005959 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005960 int rpc_status;
5961 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005962 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005963};
5964
5965static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005966 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5967 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005968{
5969 struct nfs4_lockdata *p;
5970 struct inode *inode = lsp->ls_state->inode;
5971 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005972 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005973
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005974 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005975 if (p == NULL)
5976 return NULL;
5977
5978 p->arg.fh = NFS_FH(inode);
5979 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005980 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005981 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005982 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005983 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5984 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005985 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005986 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005987 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005988 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005989 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005990 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005991 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005992 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005993 atomic_inc(&lsp->ls_count);
5994 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005995 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005996 memcpy(&p->fl, fl, sizeof(p->fl));
5997 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005998out_free_seqid:
5999 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006000out_free:
6001 kfree(p);
6002 return NULL;
6003}
6004
6005static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6006{
6007 struct nfs4_lockdata *data = calldata;
6008 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
Harvey Harrison3110ff82008-05-02 13:42:44 -07006010 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006011 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006012 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006013 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006014 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006015 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006016 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006017 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006018 nfs4_stateid_copy(&data->arg.open_stateid,
6019 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006020 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006021 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006022 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006023 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006024 nfs4_stateid_copy(&data->arg.lock_stateid,
6025 &data->lsp->ls_stateid);
6026 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006027 if (!nfs4_valid_open_stateid(state)) {
6028 data->rpc_status = -EBADF;
6029 task->tk_action = NULL;
6030 goto out_release_open_seqid;
6031 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006032 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04006033 if (nfs4_setup_sequence(data->server,
6034 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006035 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006036 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006037 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006038out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006039 nfs_release_seqid(data->arg.open_seqid);
6040out_release_lock_seqid:
6041 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006042out_wait:
6043 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006044 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006045}
6046
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006047static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6048{
6049 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006050 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006051
Harvey Harrison3110ff82008-05-02 13:42:44 -07006052 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006053
Trond Myklebust14516c32010-07-31 14:29:06 -04006054 if (!nfs4_sequence_done(task, &data->res.seq_res))
6055 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006056
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006057 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006058 switch (task->tk_status) {
6059 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006060 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006061 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006062 if (data->arg.new_lock) {
6063 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006064 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006065 rpc_restart_call_prepare(task);
6066 break;
6067 }
6068 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006069 if (data->arg.new_lock_owner != 0) {
6070 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6071 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6072 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6073 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6074 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006075 break;
6076 case -NFS4ERR_BAD_STATEID:
6077 case -NFS4ERR_OLD_STATEID:
6078 case -NFS4ERR_STALE_STATEID:
6079 case -NFS4ERR_EXPIRED:
6080 if (data->arg.new_lock_owner != 0) {
6081 if (!nfs4_stateid_match(&data->arg.open_stateid,
6082 &lsp->ls_state->open_stateid))
6083 rpc_restart_call_prepare(task);
6084 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6085 &lsp->ls_stateid))
6086 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006088 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006089}
6090
6091static void nfs4_lock_release(void *calldata)
6092{
6093 struct nfs4_lockdata *data = calldata;
6094
Harvey Harrison3110ff82008-05-02 13:42:44 -07006095 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006096 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006097 if (data->cancelled != 0) {
6098 struct rpc_task *task;
6099 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6100 data->arg.lock_seqid);
6101 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006102 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006103 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006104 } else
6105 nfs_free_seqid(data->arg.lock_seqid);
6106 nfs4_put_lock_state(data->lsp);
6107 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006108 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006109 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006110 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006111}
6112
6113static const struct rpc_call_ops nfs4_lock_ops = {
6114 .rpc_call_prepare = nfs4_lock_prepare,
6115 .rpc_call_done = nfs4_lock_done,
6116 .rpc_release = nfs4_lock_release,
6117};
6118
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006119static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6120{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006121 switch (error) {
6122 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006123 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006124 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006125 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006126 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006127 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006128 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006129 break;
6130 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006131 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006132 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006133 };
6134}
6135
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006136static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006137{
6138 struct nfs4_lockdata *data;
6139 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006140 struct rpc_message msg = {
6141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6142 .rpc_cred = state->owner->so_cred,
6143 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006144 struct rpc_task_setup task_setup_data = {
6145 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006146 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006147 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006148 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006149 .flags = RPC_TASK_ASYNC,
6150 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006151 int ret;
6152
Harvey Harrison3110ff82008-05-02 13:42:44 -07006153 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006154 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006155 fl->fl_u.nfs4_fl.owner,
6156 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006157 if (data == NULL)
6158 return -ENOMEM;
6159 if (IS_SETLKW(cmd))
6160 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006161 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006162 msg.rpc_argp = &data->arg;
6163 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006164 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006165 if (recovery_type > NFS_LOCK_NEW) {
6166 if (recovery_type == NFS_LOCK_RECLAIM)
6167 data->arg.reclaim = NFS_LOCK_RECLAIM;
6168 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006169 } else
6170 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006171 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006172 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006173 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006174 ret = nfs4_wait_for_completion_rpc_task(task);
6175 if (ret == 0) {
6176 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006177 if (ret)
6178 nfs4_handle_setlk_error(data->server, data->lsp,
6179 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006180 } else
6181 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006182 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006183 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006184 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006185 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186}
6187
6188static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6189{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006190 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006191 struct nfs4_exception exception = {
6192 .inode = state->inode,
6193 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006194 int err;
6195
6196 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006197 /* Cache the lock if possible... */
6198 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6199 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006200 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006201 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006202 break;
6203 nfs4_handle_exception(server, err, &exception);
6204 } while (exception.retry);
6205 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206}
6207
6208static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6209{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006210 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006211 struct nfs4_exception exception = {
6212 .inode = state->inode,
6213 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006214 int err;
6215
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006216 err = nfs4_set_lock_state(state, request);
6217 if (err != 0)
6218 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006219 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006220 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6221 return 0;
6222 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006223 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006224 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6225 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006226 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006227 switch (err) {
6228 default:
6229 goto out;
6230 case -NFS4ERR_GRACE:
6231 case -NFS4ERR_DELAY:
6232 nfs4_handle_exception(server, err, &exception);
6233 err = 0;
6234 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006235 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006236out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006237 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238}
6239
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006240#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006241static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6242{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006243 struct nfs4_lock_state *lsp;
6244 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006245
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006246 status = nfs4_set_lock_state(state, request);
6247 if (status != 0)
6248 return status;
6249 lsp = request->fl_u.nfs4_fl.owner;
6250 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6251 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6252 return 0;
6253 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006254 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006255}
6256#endif
6257
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6259{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006260 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006261 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006262 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006263 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264
Trond Myklebust01c3b862006-06-29 16:38:39 -04006265 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006266 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006267 if (status < 0)
6268 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006269 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006270 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006271 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006272 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006273 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006274 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006275 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006276 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006277 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006278 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006279 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006280 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006281 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006282 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006283out:
6284 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 return status;
6286}
6287
6288static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6289{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006290 struct nfs4_exception exception = {
6291 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006292 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006293 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 int err;
6295
6296 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006297 err = _nfs4_proc_setlk(state, cmd, request);
6298 if (err == -NFS4ERR_DENIED)
6299 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006301 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 } while (exception.retry);
6303 return err;
6304}
6305
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006306#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6307#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6308
6309static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006310nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6311 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006312{
6313 int status = -ERESTARTSYS;
6314 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6315
6316 while(!signalled()) {
6317 status = nfs4_proc_setlk(state, cmd, request);
6318 if ((status != -EAGAIN) || IS_SETLK(cmd))
6319 break;
6320 freezable_schedule_timeout_interruptible(timeout);
6321 timeout *= 2;
6322 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6323 status = -ERESTARTSYS;
6324 }
6325 return status;
6326}
6327
Jeff Laytona1d617d82016-09-17 18:17:39 -04006328#ifdef CONFIG_NFS_V4_1
6329struct nfs4_lock_waiter {
6330 struct task_struct *task;
6331 struct inode *inode;
6332 struct nfs_lowner *owner;
6333 bool notified;
6334};
6335
6336static int
6337nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6338{
6339 int ret;
6340 struct cb_notify_lock_args *cbnl = key;
6341 struct nfs4_lock_waiter *waiter = wait->private;
6342 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6343 *wowner = waiter->owner;
6344
6345 /* Only wake if the callback was for the same owner */
6346 if (lowner->clientid != wowner->clientid ||
6347 lowner->id != wowner->id ||
6348 lowner->s_dev != wowner->s_dev)
6349 return 0;
6350
6351 /* Make sure it's for the right inode */
6352 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6353 return 0;
6354
6355 waiter->notified = true;
6356
6357 /* override "private" so we can use default_wake_function */
6358 wait->private = waiter->task;
6359 ret = autoremove_wake_function(wait, mode, flags, key);
6360 wait->private = waiter;
6361 return ret;
6362}
6363
6364static int
6365nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6366{
6367 int status = -ERESTARTSYS;
6368 unsigned long flags;
6369 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6370 struct nfs_server *server = NFS_SERVER(state->inode);
6371 struct nfs_client *clp = server->nfs_client;
6372 wait_queue_head_t *q = &clp->cl_lock_waitq;
6373 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6374 .id = lsp->ls_seqid.owner_id,
6375 .s_dev = server->s_dev };
6376 struct nfs4_lock_waiter waiter = { .task = current,
6377 .inode = state->inode,
6378 .owner = &owner,
6379 .notified = false };
6380 wait_queue_t wait;
6381
6382 /* Don't bother with waitqueue if we don't expect a callback */
6383 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6384 return nfs4_retry_setlk_simple(state, cmd, request);
6385
6386 init_wait(&wait);
6387 wait.private = &waiter;
6388 wait.func = nfs4_wake_lock_waiter;
6389 add_wait_queue(q, &wait);
6390
6391 while(!signalled()) {
6392 status = nfs4_proc_setlk(state, cmd, request);
6393 if ((status != -EAGAIN) || IS_SETLK(cmd))
6394 break;
6395
6396 status = -ERESTARTSYS;
6397 spin_lock_irqsave(&q->lock, flags);
6398 if (waiter.notified) {
6399 spin_unlock_irqrestore(&q->lock, flags);
6400 continue;
6401 }
6402 set_current_state(TASK_INTERRUPTIBLE);
6403 spin_unlock_irqrestore(&q->lock, flags);
6404
6405 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6406 }
6407
6408 finish_wait(q, &wait);
6409 return status;
6410}
6411#else /* !CONFIG_NFS_V4_1 */
6412static inline int
6413nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6414{
6415 return nfs4_retry_setlk_simple(state, cmd, request);
6416}
6417#endif
6418
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419static int
6420nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6421{
6422 struct nfs_open_context *ctx;
6423 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 int status;
6425
6426 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006427 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 state = ctx->state;
6429
6430 if (request->fl_start < 0 || request->fl_end < 0)
6431 return -EINVAL;
6432
Trond Myklebustd9531262009-07-21 19:22:38 -04006433 if (IS_GETLK(cmd)) {
6434 if (state != NULL)
6435 return nfs4_proc_getlk(state, F_GETLK, request);
6436 return 0;
6437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438
6439 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6440 return -EINVAL;
6441
Trond Myklebustd9531262009-07-21 19:22:38 -04006442 if (request->fl_type == F_UNLCK) {
6443 if (state != NULL)
6444 return nfs4_proc_unlck(state, cmd, request);
6445 return 0;
6446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
Trond Myklebustd9531262009-07-21 19:22:38 -04006448 if (state == NULL)
6449 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006450
6451 if ((request->fl_flags & FL_POSIX) &&
6452 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6453 return -ENOLCK;
6454
Trond Myklebust55725512012-04-18 12:48:35 -04006455 /*
6456 * Don't rely on the VFS having checked the file open mode,
6457 * since it won't do this for flock() locks.
6458 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006459 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006460 case F_RDLCK:
6461 if (!(filp->f_mode & FMODE_READ))
6462 return -EBADF;
6463 break;
6464 case F_WRLCK:
6465 if (!(filp->f_mode & FMODE_WRITE))
6466 return -EBADF;
6467 }
6468
Jeff Layton1ea67db2016-09-17 18:17:37 -04006469 status = nfs4_set_lock_state(state, request);
6470 if (status != 0)
6471 return status;
6472
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006473 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474}
6475
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006476int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006477{
6478 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006479 int err;
6480
6481 err = nfs4_set_lock_state(state, fl);
6482 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006483 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006484 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006485 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006486}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006487
Trond Myklebustcf470c32012-03-07 13:49:12 -05006488struct nfs_release_lockowner_data {
6489 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006490 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006491 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006492 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006493 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006494};
6495
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006496static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6497{
6498 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006499 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006500 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6501 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006502 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006503 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006504}
6505
6506static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6507{
6508 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006509 struct nfs_server *server = data->server;
6510
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006511 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006512
6513 switch (task->tk_status) {
6514 case 0:
6515 renew_lease(server, data->timestamp);
6516 break;
6517 case -NFS4ERR_STALE_CLIENTID:
6518 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006519 nfs4_schedule_lease_recovery(server->nfs_client);
6520 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006521 case -NFS4ERR_LEASE_MOVED:
6522 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006523 if (nfs4_async_handle_error(task, server,
6524 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006525 rpc_restart_call_prepare(task);
6526 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006527}
6528
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006529static void nfs4_release_lockowner_release(void *calldata)
6530{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006531 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006532 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006533 kfree(calldata);
6534}
6535
Trond Myklebust17280172012-03-11 13:11:00 -04006536static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006537 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6538 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006539 .rpc_release = nfs4_release_lockowner_release,
6540};
6541
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006542static void
6543nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006544{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006545 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006546 struct rpc_message msg = {
6547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6548 };
6549
6550 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006551 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006552
Trond Myklebustcf470c32012-03-07 13:49:12 -05006553 data = kmalloc(sizeof(*data), GFP_NOFS);
6554 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006555 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006556 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006557 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006558 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6559 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6560 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006561
Trond Myklebustcf470c32012-03-07 13:49:12 -05006562 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006563 msg.rpc_resp = &data->res;
6564 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006565 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006566}
6567
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006568#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6569
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006570static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006571 struct dentry *unused, struct inode *inode,
6572 const char *key, const void *buf,
6573 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006574{
Al Viro59301222016-05-27 10:19:30 -04006575 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006576}
6577
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006578static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006579 struct dentry *unused, struct inode *inode,
6580 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006581{
Al Virob2968212016-04-10 20:48:24 -04006582 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006583}
6584
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006585static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006586{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006587 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006588}
6589
David Quigleyc9bccef2013-05-22 12:50:45 -04006590#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006591
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006592static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006593 struct dentry *unused, struct inode *inode,
6594 const char *key, const void *buf,
6595 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006596{
6597 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006598 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006599
6600 return -EOPNOTSUPP;
6601}
6602
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006603static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006604 struct dentry *unused, struct inode *inode,
6605 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006606{
6607 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006608 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006609 return -EOPNOTSUPP;
6610}
6611
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006612static ssize_t
6613nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006614{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006615 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006616
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006617 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6618 len = security_inode_listsecurity(inode, list, list_len);
6619 if (list_len && len > list_len)
6620 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006621 }
6622 return len;
6623}
6624
6625static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6626 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006627 .get = nfs4_xattr_get_nfs4_label,
6628 .set = nfs4_xattr_set_nfs4_label,
6629};
David Quigleyc9bccef2013-05-22 12:50:45 -04006630
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006631#else
6632
6633static ssize_t
6634nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6635{
6636 return 0;
6637}
6638
6639#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006640
Andy Adamson533eb462011-06-13 18:25:56 -04006641/*
6642 * nfs_fhget will use either the mounted_on_fileid or the fileid
6643 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006644static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6645{
Andy Adamson533eb462011-06-13 18:25:56 -04006646 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6647 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6648 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006649 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006650 return;
6651
6652 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006653 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006654 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6655 fattr->nlink = 2;
6656}
6657
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006658static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6659 const struct qstr *name,
6660 struct nfs4_fs_locations *fs_locations,
6661 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006662{
6663 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006664 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006665 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006666 };
6667 struct nfs4_fs_locations_arg args = {
6668 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006669 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006670 .page = page,
6671 .bitmask = bitmask,
6672 };
Benny Halevy22958462009-04-01 09:22:02 -04006673 struct nfs4_fs_locations_res res = {
6674 .fs_locations = fs_locations,
6675 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006676 struct rpc_message msg = {
6677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6678 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006679 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006680 };
6681 int status;
6682
Harvey Harrison3110ff82008-05-02 13:42:44 -07006683 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006684
6685 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6686 * is not supported */
6687 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6688 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6689 else
6690 bitmask[0] |= FATTR4_WORD0_FILEID;
6691
Trond Myklebustc228fd32007-01-13 02:28:11 -05006692 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006693 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006694 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006695 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006696 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006697 return status;
6698}
6699
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006700int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6701 const struct qstr *name,
6702 struct nfs4_fs_locations *fs_locations,
6703 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006704{
6705 struct nfs4_exception exception = { };
6706 int err;
6707 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006708 err = _nfs4_proc_fs_locations(client, dir, name,
6709 fs_locations, page);
6710 trace_nfs4_get_fs_locations(dir, name, err);
6711 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006712 &exception);
6713 } while (exception.retry);
6714 return err;
6715}
6716
Chuck Leverb03d7352013-10-17 14:12:50 -04006717/*
6718 * This operation also signals the server that this client is
6719 * performing migration recovery. The server can stop returning
6720 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6721 * appended to this compound to identify the client ID which is
6722 * performing recovery.
6723 */
6724static int _nfs40_proc_get_locations(struct inode *inode,
6725 struct nfs4_fs_locations *locations,
6726 struct page *page, struct rpc_cred *cred)
6727{
6728 struct nfs_server *server = NFS_SERVER(inode);
6729 struct rpc_clnt *clnt = server->client;
6730 u32 bitmask[2] = {
6731 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6732 };
6733 struct nfs4_fs_locations_arg args = {
6734 .clientid = server->nfs_client->cl_clientid,
6735 .fh = NFS_FH(inode),
6736 .page = page,
6737 .bitmask = bitmask,
6738 .migration = 1, /* skip LOOKUP */
6739 .renew = 1, /* append RENEW */
6740 };
6741 struct nfs4_fs_locations_res res = {
6742 .fs_locations = locations,
6743 .migration = 1,
6744 .renew = 1,
6745 };
6746 struct rpc_message msg = {
6747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6748 .rpc_argp = &args,
6749 .rpc_resp = &res,
6750 .rpc_cred = cred,
6751 };
6752 unsigned long now = jiffies;
6753 int status;
6754
6755 nfs_fattr_init(&locations->fattr);
6756 locations->server = server;
6757 locations->nlocations = 0;
6758
6759 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6760 nfs4_set_sequence_privileged(&args.seq_args);
6761 status = nfs4_call_sync_sequence(clnt, server, &msg,
6762 &args.seq_args, &res.seq_res);
6763 if (status)
6764 return status;
6765
6766 renew_lease(server, now);
6767 return 0;
6768}
6769
6770#ifdef CONFIG_NFS_V4_1
6771
6772/*
6773 * This operation also signals the server that this client is
6774 * performing migration recovery. The server can stop asserting
6775 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6776 * performing this operation is identified in the SEQUENCE
6777 * operation in this compound.
6778 *
6779 * When the client supports GETATTR(fs_locations_info), it can
6780 * be plumbed in here.
6781 */
6782static int _nfs41_proc_get_locations(struct inode *inode,
6783 struct nfs4_fs_locations *locations,
6784 struct page *page, struct rpc_cred *cred)
6785{
6786 struct nfs_server *server = NFS_SERVER(inode);
6787 struct rpc_clnt *clnt = server->client;
6788 u32 bitmask[2] = {
6789 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6790 };
6791 struct nfs4_fs_locations_arg args = {
6792 .fh = NFS_FH(inode),
6793 .page = page,
6794 .bitmask = bitmask,
6795 .migration = 1, /* skip LOOKUP */
6796 };
6797 struct nfs4_fs_locations_res res = {
6798 .fs_locations = locations,
6799 .migration = 1,
6800 };
6801 struct rpc_message msg = {
6802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6803 .rpc_argp = &args,
6804 .rpc_resp = &res,
6805 .rpc_cred = cred,
6806 };
6807 int status;
6808
6809 nfs_fattr_init(&locations->fattr);
6810 locations->server = server;
6811 locations->nlocations = 0;
6812
6813 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6814 nfs4_set_sequence_privileged(&args.seq_args);
6815 status = nfs4_call_sync_sequence(clnt, server, &msg,
6816 &args.seq_args, &res.seq_res);
6817 if (status == NFS4_OK &&
6818 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6819 status = -NFS4ERR_LEASE_MOVED;
6820 return status;
6821}
6822
6823#endif /* CONFIG_NFS_V4_1 */
6824
6825/**
6826 * nfs4_proc_get_locations - discover locations for a migrated FSID
6827 * @inode: inode on FSID that is migrating
6828 * @locations: result of query
6829 * @page: buffer
6830 * @cred: credential to use for this operation
6831 *
6832 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6833 * operation failed, or a negative errno if a local error occurred.
6834 *
6835 * On success, "locations" is filled in, but if the server has
6836 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6837 * asserted.
6838 *
6839 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6840 * from this client that require migration recovery.
6841 */
6842int nfs4_proc_get_locations(struct inode *inode,
6843 struct nfs4_fs_locations *locations,
6844 struct page *page, struct rpc_cred *cred)
6845{
6846 struct nfs_server *server = NFS_SERVER(inode);
6847 struct nfs_client *clp = server->nfs_client;
6848 const struct nfs4_mig_recovery_ops *ops =
6849 clp->cl_mvops->mig_recovery_ops;
6850 struct nfs4_exception exception = { };
6851 int status;
6852
6853 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6854 (unsigned long long)server->fsid.major,
6855 (unsigned long long)server->fsid.minor,
6856 clp->cl_hostname);
6857 nfs_display_fhandle(NFS_FH(inode), __func__);
6858
6859 do {
6860 status = ops->get_locations(inode, locations, page, cred);
6861 if (status != -NFS4ERR_DELAY)
6862 break;
6863 nfs4_handle_exception(server, status, &exception);
6864 } while (exception.retry);
6865 return status;
6866}
6867
Chuck Lever44c99932013-10-17 14:13:30 -04006868/*
6869 * This operation also signals the server that this client is
6870 * performing "lease moved" recovery. The server can stop
6871 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6872 * is appended to this compound to identify the client ID which is
6873 * performing recovery.
6874 */
6875static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6876{
6877 struct nfs_server *server = NFS_SERVER(inode);
6878 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6879 struct rpc_clnt *clnt = server->client;
6880 struct nfs4_fsid_present_arg args = {
6881 .fh = NFS_FH(inode),
6882 .clientid = clp->cl_clientid,
6883 .renew = 1, /* append RENEW */
6884 };
6885 struct nfs4_fsid_present_res res = {
6886 .renew = 1,
6887 };
6888 struct rpc_message msg = {
6889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6890 .rpc_argp = &args,
6891 .rpc_resp = &res,
6892 .rpc_cred = cred,
6893 };
6894 unsigned long now = jiffies;
6895 int status;
6896
6897 res.fh = nfs_alloc_fhandle();
6898 if (res.fh == NULL)
6899 return -ENOMEM;
6900
6901 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6902 nfs4_set_sequence_privileged(&args.seq_args);
6903 status = nfs4_call_sync_sequence(clnt, server, &msg,
6904 &args.seq_args, &res.seq_res);
6905 nfs_free_fhandle(res.fh);
6906 if (status)
6907 return status;
6908
6909 do_renew_lease(clp, now);
6910 return 0;
6911}
6912
6913#ifdef CONFIG_NFS_V4_1
6914
6915/*
6916 * This operation also signals the server that this client is
6917 * performing "lease moved" recovery. The server can stop asserting
6918 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6919 * this operation is identified in the SEQUENCE operation in this
6920 * compound.
6921 */
6922static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6923{
6924 struct nfs_server *server = NFS_SERVER(inode);
6925 struct rpc_clnt *clnt = server->client;
6926 struct nfs4_fsid_present_arg args = {
6927 .fh = NFS_FH(inode),
6928 };
6929 struct nfs4_fsid_present_res res = {
6930 };
6931 struct rpc_message msg = {
6932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6933 .rpc_argp = &args,
6934 .rpc_resp = &res,
6935 .rpc_cred = cred,
6936 };
6937 int status;
6938
6939 res.fh = nfs_alloc_fhandle();
6940 if (res.fh == NULL)
6941 return -ENOMEM;
6942
6943 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6944 nfs4_set_sequence_privileged(&args.seq_args);
6945 status = nfs4_call_sync_sequence(clnt, server, &msg,
6946 &args.seq_args, &res.seq_res);
6947 nfs_free_fhandle(res.fh);
6948 if (status == NFS4_OK &&
6949 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6950 status = -NFS4ERR_LEASE_MOVED;
6951 return status;
6952}
6953
6954#endif /* CONFIG_NFS_V4_1 */
6955
6956/**
6957 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6958 * @inode: inode on FSID to check
6959 * @cred: credential to use for this operation
6960 *
6961 * Server indicates whether the FSID is present, moved, or not
6962 * recognized. This operation is necessary to clear a LEASE_MOVED
6963 * condition for this client ID.
6964 *
6965 * Returns NFS4_OK if the FSID is present on this server,
6966 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6967 * NFS4ERR code if some error occurred on the server, or a
6968 * negative errno if a local failure occurred.
6969 */
6970int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6971{
6972 struct nfs_server *server = NFS_SERVER(inode);
6973 struct nfs_client *clp = server->nfs_client;
6974 const struct nfs4_mig_recovery_ops *ops =
6975 clp->cl_mvops->mig_recovery_ops;
6976 struct nfs4_exception exception = { };
6977 int status;
6978
6979 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6980 (unsigned long long)server->fsid.major,
6981 (unsigned long long)server->fsid.minor,
6982 clp->cl_hostname);
6983 nfs_display_fhandle(NFS_FH(inode), __func__);
6984
6985 do {
6986 status = ops->fsid_present(inode, cred);
6987 if (status != -NFS4ERR_DELAY)
6988 break;
6989 nfs4_handle_exception(server, status, &exception);
6990 } while (exception.retry);
6991 return status;
6992}
6993
Andy Adamson5ec16a82013-08-08 10:57:55 -04006994/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006995 * If 'use_integrity' is true and the state managment nfs_client
6996 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6997 * and the machine credential as per RFC3530bis and RFC5661 Security
6998 * Considerations sections. Otherwise, just use the user cred with the
6999 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007000 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007001static 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 +00007002{
7003 int status;
7004 struct nfs4_secinfo_arg args = {
7005 .dir_fh = NFS_FH(dir),
7006 .name = name,
7007 };
7008 struct nfs4_secinfo_res res = {
7009 .flavors = flavors,
7010 };
7011 struct rpc_message msg = {
7012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7013 .rpc_argp = &args,
7014 .rpc_resp = &res,
7015 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007016 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007017 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007018
7019 if (use_integrity) {
7020 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007021 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7022 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007023 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007024
7025 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007026
7027 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7028 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7029
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007030 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7031 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007032 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007033
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007034 if (cred)
7035 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007036
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007037 return status;
7038}
7039
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007040int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7041 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007042{
7043 struct nfs4_exception exception = { };
7044 int err;
7045 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007046 err = -NFS4ERR_WRONGSEC;
7047
7048 /* try to use integrity protection with machine cred */
7049 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7050 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7051
7052 /*
7053 * if unable to use integrity protection, or SECINFO with
7054 * integrity protection returns NFS4ERR_WRONGSEC (which is
7055 * disallowed by spec, but exists in deployed servers) use
7056 * the current filesystem's rpc_client and the user cred.
7057 */
7058 if (err == -NFS4ERR_WRONGSEC)
7059 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7060
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007061 trace_nfs4_secinfo(dir, name, err);
7062 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007063 &exception);
7064 } while (exception.retry);
7065 return err;
7066}
7067
Andy Adamson557134a2009-04-01 09:21:53 -04007068#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007069/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007070 * Check the exchange flags returned by the server for invalid flags, having
7071 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7072 * DS flags set.
7073 */
7074static int nfs4_check_cl_exchange_flags(u32 flags)
7075{
7076 if (flags & ~EXCHGID4_FLAG_MASK_R)
7077 goto out_inval;
7078 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7079 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7080 goto out_inval;
7081 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7082 goto out_inval;
7083 return NFS_OK;
7084out_inval:
7085 return -NFS4ERR_INVAL;
7086}
7087
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007088static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007089nfs41_same_server_scope(struct nfs41_server_scope *a,
7090 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007091{
7092 if (a->server_scope_sz == b->server_scope_sz &&
7093 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7094 return true;
7095
7096 return false;
7097}
7098
Andy Adamson02a95de2016-02-05 16:08:37 -05007099static void
7100nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7101{
7102}
7103
7104static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7105 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7106};
7107
Andy Adamson357f54d2010-12-14 10:11:57 -05007108/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007109 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007110 *
7111 * The 4.1 client currently uses the same TCP connection for the
7112 * fore and backchannel.
7113 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007114static
7115int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7116 struct rpc_xprt *xprt,
7117 struct nfs_client *clp,
7118 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007119{
7120 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007121 struct nfs41_bind_conn_to_session_args args = {
7122 .client = clp,
7123 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7124 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007125 struct nfs41_bind_conn_to_session_res res;
7126 struct rpc_message msg = {
7127 .rpc_proc =
7128 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007129 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007130 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007131 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007132 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007133 struct rpc_task_setup task_setup_data = {
7134 .rpc_client = clnt,
7135 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007136 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007137 .rpc_message = &msg,
7138 .flags = RPC_TASK_TIMEOUT,
7139 };
7140 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007141
7142 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007143
Trond Myklebust71a097c2015-02-18 09:27:18 -08007144 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7145 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7146 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007147
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007148 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7149 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7150 args.dir = NFS4_CDFC4_FORE;
7151
7152 task = rpc_run_task(&task_setup_data);
7153 if (!IS_ERR(task)) {
7154 status = task->tk_status;
7155 rpc_put_task(task);
7156 } else
7157 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007158 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007159 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007160 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007161 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7162 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7163 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007164 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007165 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007166 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007167 dprintk("NFS: %s: Unexpected direction from server\n",
7168 __func__);
7169 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007170 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007171 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007172 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007173 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7174 __func__);
7175 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007176 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007177 }
7178 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007179out:
7180 dprintk("<-- %s status= %d\n", __func__, status);
7181 return status;
7182}
7183
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007184struct rpc_bind_conn_calldata {
7185 struct nfs_client *clp;
7186 struct rpc_cred *cred;
7187};
7188
7189static int
7190nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7191 struct rpc_xprt *xprt,
7192 void *calldata)
7193{
7194 struct rpc_bind_conn_calldata *p = calldata;
7195
7196 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7197}
7198
7199int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7200{
7201 struct rpc_bind_conn_calldata data = {
7202 .clp = clp,
7203 .cred = cred,
7204 };
7205 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7206 nfs4_proc_bind_conn_to_session_callback, &data);
7207}
7208
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007209/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007210 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7211 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007212 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007213static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7214 .how = SP4_MACH_CRED,
7215 .enforce.u.words = {
7216 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7217 1 << (OP_EXCHANGE_ID - 32) |
7218 1 << (OP_CREATE_SESSION - 32) |
7219 1 << (OP_DESTROY_SESSION - 32) |
7220 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007221 },
7222 .allow.u.words = {
7223 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007224 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007225 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007226 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007227 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007228 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007229 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007230 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007231 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007232 1 << (OP_FREE_STATEID - 32) |
7233 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007234 }
7235};
7236
7237/*
7238 * Select the state protection mode for client `clp' given the server results
7239 * from exchange_id in `sp'.
7240 *
7241 * Returns 0 on success, negative errno otherwise.
7242 */
7243static int nfs4_sp4_select_mode(struct nfs_client *clp,
7244 struct nfs41_state_protection *sp)
7245{
7246 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7247 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7248 1 << (OP_EXCHANGE_ID - 32) |
7249 1 << (OP_CREATE_SESSION - 32) |
7250 1 << (OP_DESTROY_SESSION - 32) |
7251 1 << (OP_DESTROY_CLIENTID - 32)
7252 };
7253 unsigned int i;
7254
7255 if (sp->how == SP4_MACH_CRED) {
7256 /* Print state protect result */
7257 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7258 for (i = 0; i <= LAST_NFS4_OP; i++) {
7259 if (test_bit(i, sp->enforce.u.longs))
7260 dfprintk(MOUNT, " enforce op %d\n", i);
7261 if (test_bit(i, sp->allow.u.longs))
7262 dfprintk(MOUNT, " allow op %d\n", i);
7263 }
7264
7265 /* make sure nothing is on enforce list that isn't supported */
7266 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7267 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7268 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7269 return -EINVAL;
7270 }
7271 }
7272
7273 /*
7274 * Minimal mode - state operations are allowed to use machine
7275 * credential. Note this already happens by default, so the
7276 * client doesn't have to do anything more than the negotiation.
7277 *
7278 * NOTE: we don't care if EXCHANGE_ID is in the list -
7279 * we're already using the machine cred for exchange_id
7280 * and will never use a different cred.
7281 */
7282 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7283 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7284 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7285 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7286 dfprintk(MOUNT, "sp4_mach_cred:\n");
7287 dfprintk(MOUNT, " minimal mode enabled\n");
7288 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7289 } else {
7290 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7291 return -EINVAL;
7292 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007293
7294 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007295 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7296 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007297 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7298 dfprintk(MOUNT, " cleanup mode enabled\n");
7299 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7300 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007301
Andrew Elble99ade3c2015-12-02 09:39:51 -05007302 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7303 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7304 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7305 &clp->cl_sp4_flags);
7306 }
7307
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007308 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7309 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7310 dfprintk(MOUNT, " secinfo mode enabled\n");
7311 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7312 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007313
7314 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7315 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7316 dfprintk(MOUNT, " stateid mode enabled\n");
7317 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7318 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007319
7320 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7321 dfprintk(MOUNT, " write mode enabled\n");
7322 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7323 }
7324
7325 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7326 dfprintk(MOUNT, " commit mode enabled\n");
7327 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7328 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007329 }
7330
7331 return 0;
7332}
7333
Andy Adamson8d89bd72016-09-09 09:22:18 -04007334struct nfs41_exchange_id_data {
7335 struct nfs41_exchange_id_res res;
7336 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007337 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007338 int rpc_status;
7339};
7340
7341static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7342{
7343 struct nfs41_exchange_id_data *cdata =
7344 (struct nfs41_exchange_id_data *)data;
7345 struct nfs_client *clp = cdata->args.client;
7346 int status = task->tk_status;
7347
7348 trace_nfs4_exchange_id(clp, status);
7349
7350 if (status == 0)
7351 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007352
7353 if (cdata->xprt && status == 0) {
7354 status = nfs4_detect_session_trunking(clp, &cdata->res,
7355 cdata->xprt);
7356 goto out;
7357 }
7358
Andy Adamson8d89bd72016-09-09 09:22:18 -04007359 if (status == 0)
7360 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7361
7362 if (status == 0) {
7363 clp->cl_clientid = cdata->res.clientid;
7364 clp->cl_exchange_flags = cdata->res.flags;
7365 /* Client ID is not confirmed */
7366 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7367 clear_bit(NFS4_SESSION_ESTABLISHED,
7368 &clp->cl_session->session_state);
7369 clp->cl_seqid = cdata->res.seqid;
7370 }
7371
7372 kfree(clp->cl_serverowner);
7373 clp->cl_serverowner = cdata->res.server_owner;
7374 cdata->res.server_owner = NULL;
7375
7376 /* use the most recent implementation id */
7377 kfree(clp->cl_implid);
7378 clp->cl_implid = cdata->res.impl_id;
7379 cdata->res.impl_id = NULL;
7380
7381 if (clp->cl_serverscope != NULL &&
7382 !nfs41_same_server_scope(clp->cl_serverscope,
7383 cdata->res.server_scope)) {
7384 dprintk("%s: server_scope mismatch detected\n",
7385 __func__);
7386 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7387 kfree(clp->cl_serverscope);
7388 clp->cl_serverscope = NULL;
7389 }
7390
7391 if (clp->cl_serverscope == NULL) {
7392 clp->cl_serverscope = cdata->res.server_scope;
7393 cdata->res.server_scope = NULL;
7394 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007395 /* Save the EXCHANGE_ID verifier session trunk tests */
7396 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7397 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007398 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007399out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007400 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007401 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007402}
7403
7404static void nfs4_exchange_id_release(void *data)
7405{
7406 struct nfs41_exchange_id_data *cdata =
7407 (struct nfs41_exchange_id_data *)data;
7408
7409 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007410 if (cdata->xprt) {
7411 xprt_put(cdata->xprt);
7412 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7413 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007414 kfree(cdata->res.impl_id);
7415 kfree(cdata->res.server_scope);
7416 kfree(cdata->res.server_owner);
7417 kfree(cdata);
7418}
7419
7420static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7421 .rpc_call_done = nfs4_exchange_id_done,
7422 .rpc_release = nfs4_exchange_id_release,
7423};
7424
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007425/*
7426 * _nfs4_proc_exchange_id()
7427 *
7428 * Wrapper for EXCHANGE_ID operation.
7429 */
7430static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007431 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007432{
7433 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007434 struct rpc_message msg = {
7435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007436 .rpc_cred = cred,
7437 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007438 struct rpc_task_setup task_setup_data = {
7439 .rpc_client = clp->cl_rpcclient,
7440 .callback_ops = &nfs4_exchange_id_call_ops,
7441 .rpc_message = &msg,
7442 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7443 };
7444 struct nfs41_exchange_id_data *calldata;
7445 struct rpc_task *task;
7446 int status = -EIO;
7447
7448 if (!atomic_inc_not_zero(&clp->cl_count))
7449 goto out;
7450
7451 status = -ENOMEM;
7452 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7453 if (!calldata)
7454 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007455
Andy Adamsonad0849a2016-09-09 09:22:28 -04007456 if (!xprt)
7457 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007458
7459 status = nfs4_init_uniform_client_string(clp);
7460 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007461 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007462
7463 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007464 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007465 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007466
Andy Adamson8d89bd72016-09-09 09:22:18 -04007467 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7468 GFP_NOFS);
7469 status = -ENOMEM;
7470 if (unlikely(calldata->res.server_owner == NULL))
7471 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007472
Andy Adamson8d89bd72016-09-09 09:22:18 -04007473 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007474 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007475 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007476 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007477
Andy Adamson8d89bd72016-09-09 09:22:18 -04007478 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7479 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007480 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007481
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007482 switch (sp4_how) {
7483 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007484 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007485 break;
7486
7487 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007488 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007489 break;
7490
7491 default:
7492 /* unsupported! */
7493 WARN_ON_ONCE(1);
7494 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007495 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007496 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007497 if (xprt) {
7498 calldata->xprt = xprt;
7499 task_setup_data.rpc_xprt = xprt;
7500 task_setup_data.flags =
7501 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7502 calldata->args.verifier = &clp->cl_confirm;
7503 } else {
7504 calldata->args.verifier = &verifier;
7505 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007506 calldata->args.client = clp;
7507#ifdef CONFIG_NFS_V4_1_MIGRATION
7508 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7509 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7510 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7511#else
7512 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7513 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7514#endif
7515 msg.rpc_argp = &calldata->args;
7516 msg.rpc_resp = &calldata->res;
7517 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007518
Andy Adamson8d89bd72016-09-09 09:22:18 -04007519 task = rpc_run_task(&task_setup_data);
7520 if (IS_ERR(task)) {
7521 status = PTR_ERR(task);
7522 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007523 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007524
Andy Adamsonad0849a2016-09-09 09:22:28 -04007525 if (!xprt) {
7526 status = rpc_wait_for_completion_task(task);
7527 if (!status)
7528 status = calldata->rpc_status;
7529 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007530 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007531
Andy Adamson8d89bd72016-09-09 09:22:18 -04007532 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007533out:
Chuck Lever177313f2012-05-21 22:44:58 -04007534 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007535 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007536 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007537 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007538 clp->cl_implid->date.seconds,
7539 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007540 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007541 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007542
7543out_impl_id:
7544 kfree(calldata->res.impl_id);
7545out_server_scope:
7546 kfree(calldata->res.server_scope);
7547out_server_owner:
7548 kfree(calldata->res.server_owner);
7549out_calldata:
7550 kfree(calldata);
7551 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007552}
7553
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007554/*
7555 * nfs4_proc_exchange_id()
7556 *
7557 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7558 *
7559 * Since the clientid has expired, all compounds using sessions
7560 * associated with the stale clientid will be returning
7561 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7562 * be in some phase of session reset.
7563 *
7564 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7565 */
7566int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7567{
7568 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7569 int status;
7570
7571 /* try SP4_MACH_CRED if krb5i/p */
7572 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7573 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007574 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007575 if (!status)
7576 return 0;
7577 }
7578
7579 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007580 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007581}
7582
Andy Adamson04fa2c62016-09-09 09:22:29 -04007583/**
7584 * nfs4_test_session_trunk
7585 *
7586 * This is an add_xprt_test() test function called from
7587 * rpc_clnt_setup_test_and_add_xprt.
7588 *
7589 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7590 * and is dereferrenced in nfs4_exchange_id_release
7591 *
7592 * Upon success, add the new transport to the rpc_clnt
7593 *
7594 * @clnt: struct rpc_clnt to get new transport
7595 * @xprt: the rpc_xprt to test
7596 * @data: call data for _nfs4_proc_exchange_id.
7597 */
7598int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7599 void *data)
7600{
7601 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7602 u32 sp4_how;
7603
7604 dprintk("--> %s try %s\n", __func__,
7605 xprt->address_strings[RPC_DISPLAY_ADDR]);
7606
7607 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7608
7609 /* Test connection for session trunking. Async exchange_id call */
7610 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7611}
7612EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7613
Trond Myklebust66245532012-05-25 17:18:09 -04007614static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7615 struct rpc_cred *cred)
7616{
7617 struct rpc_message msg = {
7618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7619 .rpc_argp = clp,
7620 .rpc_cred = cred,
7621 };
7622 int status;
7623
7624 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007625 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007626 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007627 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007628 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7629 return status;
7630}
7631
7632static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7633 struct rpc_cred *cred)
7634{
7635 unsigned int loop;
7636 int ret;
7637
7638 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7639 ret = _nfs4_proc_destroy_clientid(clp, cred);
7640 switch (ret) {
7641 case -NFS4ERR_DELAY:
7642 case -NFS4ERR_CLIENTID_BUSY:
7643 ssleep(1);
7644 break;
7645 default:
7646 return ret;
7647 }
7648 }
7649 return 0;
7650}
7651
7652int nfs4_destroy_clientid(struct nfs_client *clp)
7653{
7654 struct rpc_cred *cred;
7655 int ret = 0;
7656
7657 if (clp->cl_mvops->minor_version < 1)
7658 goto out;
7659 if (clp->cl_exchange_flags == 0)
7660 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007661 if (clp->cl_preserve_clid)
7662 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007663 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007664 ret = nfs4_proc_destroy_clientid(clp, cred);
7665 if (cred)
7666 put_rpccred(cred);
7667 switch (ret) {
7668 case 0:
7669 case -NFS4ERR_STALE_CLIENTID:
7670 clp->cl_exchange_flags = 0;
7671 }
7672out:
7673 return ret;
7674}
7675
Andy Adamson2050f0c2009-04-01 09:22:30 -04007676struct nfs4_get_lease_time_data {
7677 struct nfs4_get_lease_time_args *args;
7678 struct nfs4_get_lease_time_res *res;
7679 struct nfs_client *clp;
7680};
7681
7682static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7683 void *calldata)
7684{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007685 struct nfs4_get_lease_time_data *data =
7686 (struct nfs4_get_lease_time_data *)calldata;
7687
7688 dprintk("--> %s\n", __func__);
7689 /* just setup sequence, do not trigger session recovery
7690 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007691 nfs41_setup_sequence(data->clp->cl_session,
7692 &data->args->la_seq_args,
7693 &data->res->lr_seq_res,
7694 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007695 dprintk("<-- %s\n", __func__);
7696}
7697
7698/*
7699 * Called from nfs4_state_manager thread for session setup, so don't recover
7700 * from sequence operation or clientid errors.
7701 */
7702static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7703{
7704 struct nfs4_get_lease_time_data *data =
7705 (struct nfs4_get_lease_time_data *)calldata;
7706
7707 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007708 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7709 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007710 switch (task->tk_status) {
7711 case -NFS4ERR_DELAY:
7712 case -NFS4ERR_GRACE:
7713 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7714 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7715 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007716 /* fall through */
7717 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007718 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007719 return;
7720 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007721 dprintk("<-- %s\n", __func__);
7722}
7723
Trond Myklebust17280172012-03-11 13:11:00 -04007724static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007725 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7726 .rpc_call_done = nfs4_get_lease_time_done,
7727};
7728
7729int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7730{
7731 struct rpc_task *task;
7732 struct nfs4_get_lease_time_args args;
7733 struct nfs4_get_lease_time_res res = {
7734 .lr_fsinfo = fsinfo,
7735 };
7736 struct nfs4_get_lease_time_data data = {
7737 .args = &args,
7738 .res = &res,
7739 .clp = clp,
7740 };
7741 struct rpc_message msg = {
7742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7743 .rpc_argp = &args,
7744 .rpc_resp = &res,
7745 };
7746 struct rpc_task_setup task_setup = {
7747 .rpc_client = clp->cl_rpcclient,
7748 .rpc_message = &msg,
7749 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007750 .callback_data = &data,
7751 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007752 };
7753 int status;
7754
Chuck Levera9c92d62013-08-09 12:48:18 -04007755 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007756 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007757 dprintk("--> %s\n", __func__);
7758 task = rpc_run_task(&task_setup);
7759
7760 if (IS_ERR(task))
7761 status = PTR_ERR(task);
7762 else {
7763 status = task->tk_status;
7764 rpc_put_task(task);
7765 }
7766 dprintk("<-- %s return %d\n", __func__, status);
7767
7768 return status;
7769}
7770
Andy Adamsonfc931582009-04-01 09:22:31 -04007771/*
7772 * Initialize the values to be used by the client in CREATE_SESSION
7773 * If nfs4_init_session set the fore channel request and response sizes,
7774 * use them.
7775 *
7776 * Set the back channel max_resp_sz_cached to zero to force the client to
7777 * always set csa_cachethis to FALSE because the current implementation
7778 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7779 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007780static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7781 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007782{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007783 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007784 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007785
Andy Adamson18aad3d2013-06-26 12:21:49 -04007786 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7787 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7788
Andy Adamsonfc931582009-04-01 09:22:31 -04007789 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007790 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7791 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007792 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007793 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007794
7795 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007796 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007797 __func__,
7798 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007799 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007800
7801 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007802 args->bc_attrs.max_rqst_sz = max_bc_payload;
7803 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007804 args->bc_attrs.max_resp_sz_cached = 0;
7805 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007806 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007807
7808 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7809 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7810 __func__,
7811 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7812 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7813 args->bc_attrs.max_reqs);
7814}
7815
Trond Myklebust79969dd2015-02-18 11:30:18 -08007816static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7817 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007818{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007819 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007820 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007821
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007822 if (rcvd->max_resp_sz > sent->max_resp_sz)
7823 return -EINVAL;
7824 /*
7825 * Our requested max_ops is the minimum we need; we're not
7826 * prepared to break up compounds into smaller pieces than that.
7827 * So, no point even trying to continue if the server won't
7828 * cooperate:
7829 */
7830 if (rcvd->max_ops < sent->max_ops)
7831 return -EINVAL;
7832 if (rcvd->max_reqs == 0)
7833 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007834 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7835 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007836 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007837}
7838
Trond Myklebust79969dd2015-02-18 11:30:18 -08007839static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7840 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007841{
7842 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007843 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007844
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007845 if (!(res->flags & SESSION4_BACK_CHAN))
7846 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007847 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7848 return -EINVAL;
7849 if (rcvd->max_resp_sz < sent->max_resp_sz)
7850 return -EINVAL;
7851 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7852 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007853 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007854 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007855 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007856 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007857out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007858 return 0;
7859}
Andy Adamson8d353012009-04-01 09:22:32 -04007860
Andy Adamson8d353012009-04-01 09:22:32 -04007861static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007862 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007863{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007864 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007865
Trond Myklebust79969dd2015-02-18 11:30:18 -08007866 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007867 if (ret)
7868 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007869 return nfs4_verify_back_channel_attrs(args, res);
7870}
7871
7872static void nfs4_update_session(struct nfs4_session *session,
7873 struct nfs41_create_session_res *res)
7874{
7875 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007876 /* Mark client id and session as being confirmed */
7877 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7878 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007879 session->flags = res->flags;
7880 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007881 if (res->flags & SESSION4_BACK_CHAN)
7882 memcpy(&session->bc_attrs, &res->bc_attrs,
7883 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007884}
7885
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007886static int _nfs4_proc_create_session(struct nfs_client *clp,
7887 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007888{
7889 struct nfs4_session *session = clp->cl_session;
7890 struct nfs41_create_session_args args = {
7891 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007892 .clientid = clp->cl_clientid,
7893 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007894 .cb_program = NFS4_CALLBACK,
7895 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007896 struct nfs41_create_session_res res;
7897
Andy Adamsonfc931582009-04-01 09:22:31 -04007898 struct rpc_message msg = {
7899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7900 .rpc_argp = &args,
7901 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007902 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007903 };
7904 int status;
7905
Chuck Lever6b26cc82016-05-02 14:40:40 -04007906 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007907 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007908
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007909 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007910 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007911
Trond Myklebustb519d402016-09-11 14:50:01 -04007912 switch (status) {
7913 case -NFS4ERR_STALE_CLIENTID:
7914 case -NFS4ERR_DELAY:
7915 case -ETIMEDOUT:
7916 case -EACCES:
7917 case -EAGAIN:
7918 goto out;
7919 };
7920
7921 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007922 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007923 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007924 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007925 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007926 if (status)
7927 goto out;
7928 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007929 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007930out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007931 return status;
7932}
7933
7934/*
7935 * Issues a CREATE_SESSION operation to the server.
7936 * It is the responsibility of the caller to verify the session is
7937 * expired before calling this routine.
7938 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007939int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007940{
7941 int status;
7942 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007943 struct nfs4_session *session = clp->cl_session;
7944
7945 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7946
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007947 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007948 if (status)
7949 goto out;
7950
Andy Adamsonaacd5532011-11-09 13:58:21 -05007951 /* Init or reset the session slot tables */
7952 status = nfs4_setup_session_slot_tables(session);
7953 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007954 if (status)
7955 goto out;
7956
7957 ptr = (unsigned *)&session->sess_id.data[0];
7958 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7959 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007960out:
7961 dprintk("<-- %s\n", __func__);
7962 return status;
7963}
7964
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007965/*
7966 * Issue the over-the-wire RPC DESTROY_SESSION.
7967 * The caller must serialize access to this routine.
7968 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007969int nfs4_proc_destroy_session(struct nfs4_session *session,
7970 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007971{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007972 struct rpc_message msg = {
7973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7974 .rpc_argp = session,
7975 .rpc_cred = cred,
7976 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007977 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007978
7979 dprintk("--> nfs4_proc_destroy_session\n");
7980
7981 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007982 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7983 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007984
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007985 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007986 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007987
7988 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007989 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007990 "Session has been destroyed regardless...\n", status);
7991
7992 dprintk("<-- nfs4_proc_destroy_session\n");
7993 return status;
7994}
7995
Trond Myklebust7b38c362012-05-23 13:23:31 -04007996/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007997 * Renew the cl_session lease.
7998 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007999struct nfs4_sequence_data {
8000 struct nfs_client *clp;
8001 struct nfs4_sequence_args args;
8002 struct nfs4_sequence_res res;
8003};
8004
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008005static void nfs41_sequence_release(void *data)
8006{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008007 struct nfs4_sequence_data *calldata = data;
8008 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008009
Alexandros Batsakis71358402010-02-05 03:45:05 -08008010 if (atomic_read(&clp->cl_count) > 1)
8011 nfs4_schedule_state_renewal(clp);
8012 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008013 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008014}
8015
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008016static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8017{
8018 switch(task->tk_status) {
8019 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008020 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8021 return -EAGAIN;
8022 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008023 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008024 }
8025 return 0;
8026}
8027
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008028static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008029{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008030 struct nfs4_sequence_data *calldata = data;
8031 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008032
Trond Myklebust14516c32010-07-31 14:29:06 -04008033 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8034 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008035
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008036 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008037 if (task->tk_status < 0) {
8038 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008039 if (atomic_read(&clp->cl_count) == 1)
8040 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008041
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008042 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8043 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008044 return;
8045 }
8046 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008047 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008048out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008049 dprintk("<-- %s\n", __func__);
8050}
8051
8052static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8053{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008054 struct nfs4_sequence_data *calldata = data;
8055 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008056 struct nfs4_sequence_args *args;
8057 struct nfs4_sequence_res *res;
8058
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008059 args = task->tk_msg.rpc_argp;
8060 res = task->tk_msg.rpc_resp;
8061
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008062 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008063}
8064
8065static const struct rpc_call_ops nfs41_sequence_ops = {
8066 .rpc_call_done = nfs41_sequence_call_done,
8067 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008068 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008069};
8070
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008071static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8072 struct rpc_cred *cred,
8073 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008074{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008075 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008076 struct rpc_message msg = {
8077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8078 .rpc_cred = cred,
8079 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008080 struct rpc_task_setup task_setup_data = {
8081 .rpc_client = clp->cl_rpcclient,
8082 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008083 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008084 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008085 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008086
Alexandros Batsakis71358402010-02-05 03:45:05 -08008087 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008088 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008089 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008090 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008091 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008092 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008093 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008094 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008095 if (is_privileged)
8096 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008097 msg.rpc_argp = &calldata->args;
8098 msg.rpc_resp = &calldata->res;
8099 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008100 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008101
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008102 return rpc_run_task(&task_setup_data);
8103}
8104
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008105static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008106{
8107 struct rpc_task *task;
8108 int ret = 0;
8109
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008110 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008111 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008112 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008113 if (IS_ERR(task))
8114 ret = PTR_ERR(task);
8115 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008116 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008117 dprintk("<-- %s status=%d\n", __func__, ret);
8118 return ret;
8119}
8120
8121static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8122{
8123 struct rpc_task *task;
8124 int ret;
8125
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008126 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008127 if (IS_ERR(task)) {
8128 ret = PTR_ERR(task);
8129 goto out;
8130 }
8131 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008132 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008133 ret = task->tk_status;
8134 rpc_put_task(task);
8135out:
8136 dprintk("<-- %s status=%d\n", __func__, ret);
8137 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008138}
8139
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008140struct nfs4_reclaim_complete_data {
8141 struct nfs_client *clp;
8142 struct nfs41_reclaim_complete_args arg;
8143 struct nfs41_reclaim_complete_res res;
8144};
8145
8146static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8147{
8148 struct nfs4_reclaim_complete_data *calldata = data;
8149
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008150 nfs41_setup_sequence(calldata->clp->cl_session,
8151 &calldata->arg.seq_args,
8152 &calldata->res.seq_res,
8153 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008154}
8155
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008156static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8157{
8158 switch(task->tk_status) {
8159 case 0:
8160 case -NFS4ERR_COMPLETE_ALREADY:
8161 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8162 break;
8163 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008164 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008165 /* fall through */
8166 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008167 return -EAGAIN;
8168 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008169 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008170 }
8171 return 0;
8172}
8173
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008174static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8175{
8176 struct nfs4_reclaim_complete_data *calldata = data;
8177 struct nfs_client *clp = calldata->clp;
8178 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8179
8180 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008181 if (!nfs41_sequence_done(task, res))
8182 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008183
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008184 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008185 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8186 rpc_restart_call_prepare(task);
8187 return;
8188 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008189 dprintk("<-- %s\n", __func__);
8190}
8191
8192static void nfs4_free_reclaim_complete_data(void *data)
8193{
8194 struct nfs4_reclaim_complete_data *calldata = data;
8195
8196 kfree(calldata);
8197}
8198
8199static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8200 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8201 .rpc_call_done = nfs4_reclaim_complete_done,
8202 .rpc_release = nfs4_free_reclaim_complete_data,
8203};
8204
8205/*
8206 * Issue a global reclaim complete.
8207 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008208static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8209 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008210{
8211 struct nfs4_reclaim_complete_data *calldata;
8212 struct rpc_task *task;
8213 struct rpc_message msg = {
8214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008215 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008216 };
8217 struct rpc_task_setup task_setup_data = {
8218 .rpc_client = clp->cl_rpcclient,
8219 .rpc_message = &msg,
8220 .callback_ops = &nfs4_reclaim_complete_call_ops,
8221 .flags = RPC_TASK_ASYNC,
8222 };
8223 int status = -ENOMEM;
8224
8225 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008226 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008227 if (calldata == NULL)
8228 goto out;
8229 calldata->clp = clp;
8230 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008231
Chuck Levera9c92d62013-08-09 12:48:18 -04008232 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008233 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008234 msg.rpc_argp = &calldata->arg;
8235 msg.rpc_resp = &calldata->res;
8236 task_setup_data.callback_data = calldata;
8237 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008238 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008239 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008240 goto out;
8241 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008242 status = nfs4_wait_for_completion_rpc_task(task);
8243 if (status == 0)
8244 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008245 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008246 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008247out:
8248 dprintk("<-- %s status=%d\n", __func__, status);
8249 return status;
8250}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008251
8252static void
8253nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8254{
8255 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008256 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008257 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008258
8259 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008260 nfs41_setup_sequence(session, &lgp->args.seq_args,
8261 &lgp->res.seq_res, task);
8262 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008263}
8264
8265static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8266{
8267 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008268
8269 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008270 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008271 dprintk("<-- %s\n", __func__);
8272}
8273
8274static int
8275nfs4_layoutget_handle_exception(struct rpc_task *task,
8276 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8277{
Trond Myklebustee314c22012-10-01 17:25:48 -07008278 struct inode *inode = lgp->args.inode;
8279 struct nfs_server *server = NFS_SERVER(inode);
8280 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008281 int nfs4err = task->tk_status;
8282 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008283 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008284
Boaz Harroshed7e5422014-01-22 20:34:54 +02008285 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008286
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008287 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008288 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008289 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008290
8291 /*
8292 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8293 * on the file. set tk_status to -ENODATA to tell upper layer to
8294 * retry go inband.
8295 */
8296 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008297 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008298 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008299 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008300 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8301 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8302 */
8303 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008304 status = -EOVERFLOW;
8305 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008306 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008307 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008308 * (or clients) writing to the same RAID stripe except when
8309 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008310 *
8311 * Treat it like we would RECALLCONFLICT -- we retry for a little
8312 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008313 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008314 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008315 if (lgp->args.minlength == 0) {
8316 status = -EOVERFLOW;
8317 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008318 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008319 status = -EBUSY;
8320 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008321 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008322 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008323 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008324 case -NFS4ERR_DELEG_REVOKED:
8325 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008326 case -NFS4ERR_EXPIRED:
8327 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008328 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008329 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008330 lo = NFS_I(inode)->layout;
8331 /* If the open stateid was bad, then recover it. */
8332 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8333 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008334 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008335 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008336 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008337 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008338 break;
8339 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008340
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008341 /*
8342 * Mark the bad layout state as invalid, then retry
8343 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008344 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008345 spin_unlock(&inode->i_lock);
8346 pnfs_free_lseg_list(&head);
8347 status = -EAGAIN;
8348 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008349 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008350
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008351 err = nfs4_handle_exception(server, nfs4err, exception);
8352 if (!status) {
8353 if (exception->retry)
8354 status = -EAGAIN;
8355 else
8356 status = err;
8357 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008358out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008359 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008360 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008361}
8362
Idan Kedar85541162012-08-02 11:47:10 +03008363static size_t max_response_pages(struct nfs_server *server)
8364{
8365 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8366 return nfs_page_array_len(0, max_resp_sz);
8367}
8368
8369static void nfs4_free_pages(struct page **pages, size_t size)
8370{
8371 int i;
8372
8373 if (!pages)
8374 return;
8375
8376 for (i = 0; i < size; i++) {
8377 if (!pages[i])
8378 break;
8379 __free_page(pages[i]);
8380 }
8381 kfree(pages);
8382}
8383
8384static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8385{
8386 struct page **pages;
8387 int i;
8388
8389 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8390 if (!pages) {
8391 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8392 return NULL;
8393 }
8394
8395 for (i = 0; i < size; i++) {
8396 pages[i] = alloc_page(gfp_flags);
8397 if (!pages[i]) {
8398 dprintk("%s: failed to allocate page\n", __func__);
8399 nfs4_free_pages(pages, size);
8400 return NULL;
8401 }
8402 }
8403
8404 return pages;
8405}
8406
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008407static void nfs4_layoutget_release(void *calldata)
8408{
8409 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008410 struct inode *inode = lgp->args.inode;
8411 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008412 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008413
8414 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008415 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008416 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008417 put_nfs_open_context(lgp->args.ctx);
8418 kfree(calldata);
8419 dprintk("<-- %s\n", __func__);
8420}
8421
8422static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8423 .rpc_call_prepare = nfs4_layoutget_prepare,
8424 .rpc_call_done = nfs4_layoutget_done,
8425 .rpc_release = nfs4_layoutget_release,
8426};
8427
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008428struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008429nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008430{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008431 struct inode *inode = lgp->args.inode;
8432 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008433 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008434 struct rpc_task *task;
8435 struct rpc_message msg = {
8436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8437 .rpc_argp = &lgp->args,
8438 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008439 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008440 };
8441 struct rpc_task_setup task_setup_data = {
8442 .rpc_client = server->client,
8443 .rpc_message = &msg,
8444 .callback_ops = &nfs4_layoutget_call_ops,
8445 .callback_data = lgp,
8446 .flags = RPC_TASK_ASYNC,
8447 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008448 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008449 struct nfs4_exception exception = {
8450 .inode = inode,
8451 .timeout = *timeout,
8452 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008453 int status = 0;
8454
8455 dprintk("--> %s\n", __func__);
8456
Peng Tao4bd5a982014-11-17 11:05:17 +08008457 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8458 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8459
Idan Kedar85541162012-08-02 11:47:10 +03008460 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8461 if (!lgp->args.layout.pages) {
8462 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008463 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008464 }
8465 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8466
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008467 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008468 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008469 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008470
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008471 task = rpc_run_task(&task_setup_data);
8472 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008473 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008474 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008475 if (status == 0) {
8476 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8477 *timeout = exception.timeout;
8478 }
8479
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008480 trace_nfs4_layoutget(lgp->args.ctx,
8481 &lgp->args.range,
8482 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008483 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008484 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008485
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008486 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8487 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008488 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008489 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008490 rpc_put_task(task);
8491 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008492 if (status)
8493 return ERR_PTR(status);
8494 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008495}
8496
Benny Halevycbe82602011-05-22 19:52:37 +03008497static void
8498nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8499{
8500 struct nfs4_layoutreturn *lrp = calldata;
8501
8502 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008503 nfs41_setup_sequence(lrp->clp->cl_session,
8504 &lrp->args.seq_args,
8505 &lrp->res.seq_res,
8506 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008507}
8508
8509static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8510{
8511 struct nfs4_layoutreturn *lrp = calldata;
8512 struct nfs_server *server;
8513
8514 dprintk("--> %s\n", __func__);
8515
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008516 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008517 return;
8518
8519 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008520 switch (task->tk_status) {
8521 default:
8522 task->tk_status = 0;
8523 case 0:
8524 break;
8525 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008526 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008527 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008528 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008529 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008530 return;
8531 }
Benny Halevycbe82602011-05-22 19:52:37 +03008532 dprintk("<-- %s\n", __func__);
8533}
8534
8535static void nfs4_layoutreturn_release(void *calldata)
8536{
8537 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008538 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008539 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008540
8541 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008542 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008543 if (lrp->res.lrs_present) {
8544 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8545 &lrp->args.range,
8546 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008547 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008548 } else
8549 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008550 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008551 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008552 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008553 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008554 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008555 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008556 kfree(calldata);
8557 dprintk("<-- %s\n", __func__);
8558}
8559
8560static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8561 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8562 .rpc_call_done = nfs4_layoutreturn_done,
8563 .rpc_release = nfs4_layoutreturn_release,
8564};
8565
Peng Tao6c166052014-11-17 09:30:40 +08008566int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008567{
8568 struct rpc_task *task;
8569 struct rpc_message msg = {
8570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8571 .rpc_argp = &lrp->args,
8572 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008573 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008574 };
8575 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008576 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008577 .rpc_message = &msg,
8578 .callback_ops = &nfs4_layoutreturn_call_ops,
8579 .callback_data = lrp,
8580 };
Peng Tao6c166052014-11-17 09:30:40 +08008581 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008582
Andrew Elble99ade3c2015-12-02 09:39:51 -05008583 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8584 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8585 &task_setup_data.rpc_client, &msg);
8586
Benny Halevycbe82602011-05-22 19:52:37 +03008587 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008588 if (!sync) {
8589 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8590 if (!lrp->inode) {
8591 nfs4_layoutreturn_release(lrp);
8592 return -EAGAIN;
8593 }
8594 task_setup_data.flags |= RPC_TASK_ASYNC;
8595 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008596 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008597 task = rpc_run_task(&task_setup_data);
8598 if (IS_ERR(task))
8599 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008600 if (sync)
8601 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008602 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008603 dprintk("<-- %s status=%d\n", __func__, status);
8604 rpc_put_task(task);
8605 return status;
8606}
8607
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008608static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008609_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8610 struct pnfs_device *pdev,
8611 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008612{
8613 struct nfs4_getdeviceinfo_args args = {
8614 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008615 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8616 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008617 };
8618 struct nfs4_getdeviceinfo_res res = {
8619 .pdev = pdev,
8620 };
8621 struct rpc_message msg = {
8622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8623 .rpc_argp = &args,
8624 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008625 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008626 };
8627 int status;
8628
8629 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008630 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008631 if (res.notification & ~args.notify_types)
8632 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008633 if (res.notification != args.notify_types)
8634 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008635
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008636 dprintk("<-- %s status=%d\n", __func__, status);
8637
8638 return status;
8639}
8640
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008641int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8642 struct pnfs_device *pdev,
8643 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008644{
8645 struct nfs4_exception exception = { };
8646 int err;
8647
8648 do {
8649 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008650 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008651 &exception);
8652 } while (exception.retry);
8653 return err;
8654}
8655EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8656
Andy Adamson863a3c62011-03-23 13:27:54 +00008657static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8658{
8659 struct nfs4_layoutcommit_data *data = calldata;
8660 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008661 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008662
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008663 nfs41_setup_sequence(session,
8664 &data->args.seq_args,
8665 &data->res.seq_res,
8666 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008667}
8668
8669static void
8670nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8671{
8672 struct nfs4_layoutcommit_data *data = calldata;
8673 struct nfs_server *server = NFS_SERVER(data->args.inode);
8674
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008675 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008676 return;
8677
8678 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008679 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8680 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8681 case -NFS4ERR_BADLAYOUT: /* no layout */
8682 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008683 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008684 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008685 break;
8686 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008687 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008688 rpc_restart_call_prepare(task);
8689 return;
8690 }
8691 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008692}
8693
8694static void nfs4_layoutcommit_release(void *calldata)
8695{
8696 struct nfs4_layoutcommit_data *data = calldata;
8697
Andy Adamsondb29c082011-07-30 20:52:38 -04008698 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008699 nfs_post_op_update_inode_force_wcc(data->args.inode,
8700 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008701 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008702 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008703 kfree(data);
8704}
8705
8706static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8707 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8708 .rpc_call_done = nfs4_layoutcommit_done,
8709 .rpc_release = nfs4_layoutcommit_release,
8710};
8711
8712int
Andy Adamsonef311532011-03-12 02:58:10 -05008713nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008714{
8715 struct rpc_message msg = {
8716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8717 .rpc_argp = &data->args,
8718 .rpc_resp = &data->res,
8719 .rpc_cred = data->cred,
8720 };
8721 struct rpc_task_setup task_setup_data = {
8722 .task = &data->task,
8723 .rpc_client = NFS_CLIENT(data->args.inode),
8724 .rpc_message = &msg,
8725 .callback_ops = &nfs4_layoutcommit_ops,
8726 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008727 };
8728 struct rpc_task *task;
8729 int status = 0;
8730
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008731 dprintk("NFS: initiating layoutcommit call. sync %d "
8732 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008733 data->args.lastbytewritten,
8734 data->args.inode->i_ino);
8735
Trond Myklebust472e2592015-02-05 16:50:30 -05008736 if (!sync) {
8737 data->inode = nfs_igrab_and_active(data->args.inode);
8738 if (data->inode == NULL) {
8739 nfs4_layoutcommit_release(data);
8740 return -EAGAIN;
8741 }
8742 task_setup_data.flags = RPC_TASK_ASYNC;
8743 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008744 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008745 task = rpc_run_task(&task_setup_data);
8746 if (IS_ERR(task))
8747 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008748 if (sync)
8749 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008750 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008751 dprintk("%s: status %d\n", __func__, status);
8752 rpc_put_task(task);
8753 return status;
8754}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008755
Andy Adamson97431202013-08-08 10:57:56 -04008756/**
8757 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8758 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8759 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008760static int
8761_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008762 struct nfs_fsinfo *info,
8763 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008764{
8765 struct nfs41_secinfo_no_name_args args = {
8766 .style = SECINFO_STYLE_CURRENT_FH,
8767 };
8768 struct nfs4_secinfo_res res = {
8769 .flavors = flavors,
8770 };
8771 struct rpc_message msg = {
8772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8773 .rpc_argp = &args,
8774 .rpc_resp = &res,
8775 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008776 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008777 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008778 int status;
8779
8780 if (use_integrity) {
8781 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008782 cred = nfs4_get_clid_cred(server->nfs_client);
8783 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008784 }
8785
8786 dprintk("--> %s\n", __func__);
8787 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8788 &res.seq_res, 0);
8789 dprintk("<-- %s status=%d\n", __func__, status);
8790
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008791 if (cred)
8792 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008793
8794 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008795}
8796
8797static int
8798nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8799 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8800{
8801 struct nfs4_exception exception = { };
8802 int err;
8803 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008804 /* first try using integrity protection */
8805 err = -NFS4ERR_WRONGSEC;
8806
8807 /* try to use integrity protection with machine cred */
8808 if (_nfs4_is_integrity_protected(server->nfs_client))
8809 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8810 flavors, true);
8811
8812 /*
8813 * if unable to use integrity protection, or SECINFO with
8814 * integrity protection returns NFS4ERR_WRONGSEC (which is
8815 * disallowed by spec, but exists in deployed servers) use
8816 * the current filesystem's rpc_client and the user cred.
8817 */
8818 if (err == -NFS4ERR_WRONGSEC)
8819 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8820 flavors, false);
8821
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008822 switch (err) {
8823 case 0:
8824 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008825 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008826 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008827 default:
8828 err = nfs4_handle_exception(server, err, &exception);
8829 }
8830 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008831out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008832 return err;
8833}
8834
8835static int
8836nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8837 struct nfs_fsinfo *info)
8838{
8839 int err;
8840 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008841 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008842 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008843 struct nfs4_secinfo4 *secinfo;
8844 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008845
8846 page = alloc_page(GFP_KERNEL);
8847 if (!page) {
8848 err = -ENOMEM;
8849 goto out;
8850 }
8851
8852 flavors = page_address(page);
8853 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8854
8855 /*
8856 * Fall back on "guess and check" method if
8857 * the server doesn't support SECINFO_NO_NAME
8858 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008859 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008860 err = nfs4_find_root_sec(server, fhandle, info);
8861 goto out_freepage;
8862 }
8863 if (err)
8864 goto out_freepage;
8865
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008866 for (i = 0; i < flavors->num_flavors; i++) {
8867 secinfo = &flavors->flavors[i];
8868
8869 switch (secinfo->flavor) {
8870 case RPC_AUTH_NULL:
8871 case RPC_AUTH_UNIX:
8872 case RPC_AUTH_GSS:
8873 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8874 &secinfo->flavor_info);
8875 break;
8876 default:
8877 flavor = RPC_AUTH_MAXFLAVOR;
8878 break;
8879 }
8880
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008881 if (!nfs_auth_info_match(&server->auth_info, flavor))
8882 flavor = RPC_AUTH_MAXFLAVOR;
8883
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008884 if (flavor != RPC_AUTH_MAXFLAVOR) {
8885 err = nfs4_lookup_root_sec(server, fhandle,
8886 info, flavor);
8887 if (!err)
8888 break;
8889 }
8890 }
8891
8892 if (flavor == RPC_AUTH_MAXFLAVOR)
8893 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008894
8895out_freepage:
8896 put_page(page);
8897 if (err == -EACCES)
8898 return -EPERM;
8899out:
8900 return err;
8901}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008902
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008903static int _nfs41_test_stateid(struct nfs_server *server,
8904 nfs4_stateid *stateid,
8905 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008906{
8907 int status;
8908 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008909 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008910 };
8911 struct nfs41_test_stateid_res res;
8912 struct rpc_message msg = {
8913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8914 .rpc_argp = &args,
8915 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008916 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008917 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008918 struct rpc_clnt *rpc_client = server->client;
8919
8920 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8921 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008922
Chuck Lever38527b12012-07-11 16:30:23 -04008923 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008924 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008925 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008926 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008927 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008928 if (status != NFS_OK) {
8929 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008930 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008931 }
8932 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008933 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008934}
8935
Trond Myklebust43912bb2016-09-22 13:38:56 -04008936static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8937 int err, struct nfs4_exception *exception)
8938{
8939 exception->retry = 0;
8940 switch(err) {
8941 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008942 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008943 nfs4_handle_exception(server, err, exception);
8944 break;
8945 case -NFS4ERR_BADSESSION:
8946 case -NFS4ERR_BADSLOT:
8947 case -NFS4ERR_BAD_HIGH_SLOT:
8948 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8949 case -NFS4ERR_DEADSESSION:
8950 nfs4_do_handle_exception(server, err, exception);
8951 }
8952}
8953
Chuck Lever38527b12012-07-11 16:30:23 -04008954/**
8955 * nfs41_test_stateid - perform a TEST_STATEID operation
8956 *
8957 * @server: server / transport on which to perform the operation
8958 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008959 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008960 *
8961 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8962 * Otherwise a negative NFS4ERR value is returned if the operation
8963 * failed or the state ID is not currently valid.
8964 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008965static int nfs41_test_stateid(struct nfs_server *server,
8966 nfs4_stateid *stateid,
8967 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008968{
8969 struct nfs4_exception exception = { };
8970 int err;
8971 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008972 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008973 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008974 } while (exception.retry);
8975 return err;
8976}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008977
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008978struct nfs_free_stateid_data {
8979 struct nfs_server *server;
8980 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008981 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008982};
8983
8984static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8985{
8986 struct nfs_free_stateid_data *data = calldata;
8987 nfs41_setup_sequence(nfs4_get_session(data->server),
8988 &data->args.seq_args,
8989 &data->res.seq_res,
8990 task);
8991}
8992
8993static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8994{
8995 struct nfs_free_stateid_data *data = calldata;
8996
8997 nfs41_sequence_done(task, &data->res.seq_res);
8998
8999 switch (task->tk_status) {
9000 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009001 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009002 rpc_restart_call_prepare(task);
9003 }
9004}
9005
9006static void nfs41_free_stateid_release(void *calldata)
9007{
9008 kfree(calldata);
9009}
9010
Trond Myklebust17f26b12013-08-21 15:48:42 -04009011static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009012 .rpc_call_prepare = nfs41_free_stateid_prepare,
9013 .rpc_call_done = nfs41_free_stateid_done,
9014 .rpc_release = nfs41_free_stateid_release,
9015};
9016
9017static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009018 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009019 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009020 bool privileged)
9021{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009022 struct rpc_message msg = {
9023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009024 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009025 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009026 struct rpc_task_setup task_setup = {
9027 .rpc_client = server->client,
9028 .rpc_message = &msg,
9029 .callback_ops = &nfs41_free_stateid_ops,
9030 .flags = RPC_TASK_ASYNC,
9031 };
9032 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009033
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009034 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9035 &task_setup.rpc_client, &msg);
9036
Chuck Lever38527b12012-07-11 16:30:23 -04009037 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009038 data = kmalloc(sizeof(*data), GFP_NOFS);
9039 if (!data)
9040 return ERR_PTR(-ENOMEM);
9041 data->server = server;
9042 nfs4_stateid_copy(&data->args.stateid, stateid);
9043
9044 task_setup.callback_data = data;
9045
9046 msg.rpc_argp = &data->args;
9047 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009048 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009049 if (privileged)
9050 nfs4_set_sequence_privileged(&data->args.seq_args);
9051
9052 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009053}
9054
Chuck Lever38527b12012-07-11 16:30:23 -04009055/**
9056 * nfs41_free_stateid - perform a FREE_STATEID operation
9057 *
9058 * @server: server / transport on which to perform the operation
9059 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009060 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009061 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009062 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009063 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009064 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009065static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009066 const nfs4_stateid *stateid,
9067 struct rpc_cred *cred,
9068 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009069{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009070 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009071
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009072 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009073 if (IS_ERR(task))
9074 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009075 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009076 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009077}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009078
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009079static void
9080nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009081{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009082 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009083
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009084 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009085 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009086}
9087
Trond Myklebust36281ca2012-03-04 18:13:56 -05009088static bool nfs41_match_stateid(const nfs4_stateid *s1,
9089 const nfs4_stateid *s2)
9090{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009091 if (s1->type != s2->type)
9092 return false;
9093
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009094 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009095 return false;
9096
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009097 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009098 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009099 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009100 return true;
9101
9102 return false;
9103}
9104
Andy Adamson557134a2009-04-01 09:21:53 -04009105#endif /* CONFIG_NFS_V4_1 */
9106
Trond Myklebust36281ca2012-03-04 18:13:56 -05009107static bool nfs4_match_stateid(const nfs4_stateid *s1,
9108 const nfs4_stateid *s2)
9109{
Trond Myklebustf597c532012-03-04 18:13:56 -05009110 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009111}
9112
9113
Trond Myklebust17280172012-03-11 13:11:00 -04009114static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009115 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009116 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117 .recover_open = nfs4_open_reclaim,
9118 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009119 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009120 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121};
9122
Andy Adamson591d71c2009-04-01 09:22:47 -04009123#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009124static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009125 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9126 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9127 .recover_open = nfs4_open_reclaim,
9128 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009129 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009130 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009131 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009132};
9133#endif /* CONFIG_NFS_V4_1 */
9134
Trond Myklebust17280172012-03-11 13:11:00 -04009135static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009136 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009137 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009138 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009140 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009141};
9142
Andy Adamson591d71c2009-04-01 09:22:47 -04009143#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009144static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009145 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9146 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009147 .recover_open = nfs41_open_expired,
9148 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009149 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009150};
9151#endif /* CONFIG_NFS_V4_1 */
9152
Trond Myklebust17280172012-03-11 13:11:00 -04009153static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009154 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009155 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009156 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009157};
9158
9159#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009160static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009161 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009162 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009163 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009164};
9165#endif
9166
Chuck Leverec011fe2013-10-17 14:12:39 -04009167static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009168 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009169 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009170};
9171
9172#if defined(CONFIG_NFS_V4_1)
9173static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009174 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009175 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009176};
9177#endif /* CONFIG_NFS_V4_1 */
9178
Trond Myklebust97dc1352010-06-16 09:52:26 -04009179static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9180 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009181 .init_caps = NFS_CAP_READDIRPLUS
9182 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009183 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009184 .init_client = nfs40_init_client,
9185 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009186 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009187 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009188 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009189 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009190 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009191 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009192 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9193 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9194 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009195 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009196};
9197
9198#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009199static struct nfs_seqid *
9200nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9201{
9202 return NULL;
9203}
9204
Trond Myklebust97dc1352010-06-16 09:52:26 -04009205static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9206 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009207 .init_caps = NFS_CAP_READDIRPLUS
9208 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009209 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009210 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009211 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009212 .init_client = nfs41_init_client,
9213 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009214 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009215 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009216 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009217 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009218 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009219 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009220 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009221 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9222 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9223 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009224 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009225};
9226#endif
9227
Steve Dickson42c2c422013-05-22 12:50:38 -04009228#if defined(CONFIG_NFS_V4_2)
9229static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9230 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009231 .init_caps = NFS_CAP_READDIRPLUS
9232 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009233 | NFS_CAP_POSIX_LOCK
9234 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009235 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009236 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009237 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009238 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009239 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009240 | NFS_CAP_LAYOUTSTATS
9241 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009242 .init_client = nfs41_init_client,
9243 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009244 .match_stateid = nfs41_match_stateid,
9245 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009246 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009247 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009248 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009249 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009250 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009251 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9252 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9253 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009254 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009255};
9256#endif
9257
Trond Myklebust97dc1352010-06-16 09:52:26 -04009258const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9259 [0] = &nfs_v4_0_minor_ops,
9260#if defined(CONFIG_NFS_V4_1)
9261 [1] = &nfs_v4_1_minor_ops,
9262#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009263#if defined(CONFIG_NFS_V4_2)
9264 [2] = &nfs_v4_2_minor_ops,
9265#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009266};
9267
Trond Myklebust13997822016-07-24 17:10:52 -04009268static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009269{
9270 ssize_t error, error2;
9271
9272 error = generic_listxattr(dentry, list, size);
9273 if (error < 0)
9274 return error;
9275 if (list) {
9276 list += error;
9277 size -= error;
9278 }
9279
9280 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9281 if (error2 < 0)
9282 return error2;
9283 return error + error2;
9284}
9285
Trond Myklebust17f26b12013-08-21 15:48:42 -04009286static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009287 .create = nfs_create,
9288 .lookup = nfs_lookup,
9289 .atomic_open = nfs_atomic_open,
9290 .link = nfs_link,
9291 .unlink = nfs_unlink,
9292 .symlink = nfs_symlink,
9293 .mkdir = nfs_mkdir,
9294 .rmdir = nfs_rmdir,
9295 .mknod = nfs_mknod,
9296 .rename = nfs_rename,
9297 .permission = nfs_permission,
9298 .getattr = nfs_getattr,
9299 .setattr = nfs_setattr,
9300 .getxattr = generic_getxattr,
9301 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009302 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009303 .removexattr = generic_removexattr,
9304};
9305
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009306static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009307 .permission = nfs_permission,
9308 .getattr = nfs_getattr,
9309 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009310 .getxattr = generic_getxattr,
9311 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009312 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009313 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009314};
9315
David Howells509de812006-08-22 20:06:11 -04009316const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 .version = 4, /* protocol version */
9318 .dentry_ops = &nfs4_dentry_operations,
9319 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009320 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009321 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009323 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009324 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009325 .getattr = nfs4_proc_getattr,
9326 .setattr = nfs4_proc_setattr,
9327 .lookup = nfs4_proc_lookup,
9328 .access = nfs4_proc_access,
9329 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 .create = nfs4_proc_create,
9331 .remove = nfs4_proc_remove,
9332 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009333 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009335 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009336 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009337 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338 .link = nfs4_proc_link,
9339 .symlink = nfs4_proc_symlink,
9340 .mkdir = nfs4_proc_mkdir,
9341 .rmdir = nfs4_proc_remove,
9342 .readdir = nfs4_proc_readdir,
9343 .mknod = nfs4_proc_mknod,
9344 .statfs = nfs4_proc_statfs,
9345 .fsinfo = nfs4_proc_fsinfo,
9346 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009347 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009349 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009350 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009351 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009353 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009355 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009356 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009358 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009359 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009360 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009361 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009362 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009363 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009364 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009365 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009366 .create_server = nfs4_create_server,
9367 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368};
9369
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009370static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009371 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009372 .list = nfs4_xattr_list_nfs4_acl,
9373 .get = nfs4_xattr_get_nfs4_acl,
9374 .set = nfs4_xattr_set_nfs4_acl,
9375};
9376
9377const struct xattr_handler *nfs4_xattr_handlers[] = {
9378 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009379#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9380 &nfs4_xattr_nfs4_label_handler,
9381#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009382 NULL
9383};
9384
Linus Torvalds1da177e2005-04-16 15:20:36 -07009385/*
9386 * Local variables:
9387 * c-basic-offset: 8
9388 * End:
9389 */