blob: 6f0df2c8846c3644231d65197b6e2397c97df46d [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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust4f14c192014-02-12 19:15:06 -05001390static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001391{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001392 struct nfs_client *clp = state->owner->so_server->nfs_client;
1393 bool need_recover = false;
1394
1395 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1396 need_recover = true;
1397 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1398 need_recover = true;
1399 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1400 need_recover = true;
1401 if (need_recover)
1402 nfs4_state_mark_reclaim_nograce(clp, state);
1403}
1404
Trond Myklebuste999e802014-02-10 18:20:47 -05001405static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001406 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001407{
1408 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1409 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001410 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001411 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001412 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001413 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001414 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001415 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1416 return true;
1417 return false;
1418}
1419
Trond Myklebustf95549c2015-01-23 18:06:09 -05001420static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1421{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001422 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1423 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001424 if (state->n_wronly)
1425 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1426 if (state->n_rdonly)
1427 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1428 if (state->n_rdwr)
1429 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001430 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001431}
1432
Trond Myklebust226056c2014-02-11 10:41:07 -05001433static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001434 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001435 nfs4_stateid *stateid, fmode_t fmode)
1436{
1437 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1438 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1439 case FMODE_WRITE:
1440 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1441 break;
1442 case FMODE_READ:
1443 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1444 break;
1445 case 0:
1446 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1447 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1448 clear_bit(NFS_OPEN_STATE, &state->flags);
1449 }
1450 if (stateid == NULL)
1451 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001452 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001453 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1454 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1455 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001456 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001457 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001458 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001459 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001460 nfs4_stateid_copy(&state->stateid, stateid);
1461 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001462}
1463
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001464static void nfs_clear_open_stateid(struct nfs4_state *state,
1465 nfs4_stateid *arg_stateid,
1466 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001467{
1468 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001469 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001470 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001471 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1472 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001473}
1474
Trond Myklebust1393d962016-09-22 13:39:13 -04001475static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1476 const nfs4_stateid *stateid, fmode_t fmode,
1477 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001478{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001479 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001480 case FMODE_READ:
1481 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1482 break;
1483 case FMODE_WRITE:
1484 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1485 break;
1486 case FMODE_READ|FMODE_WRITE:
1487 set_bit(NFS_O_RDWR_STATE, &state->flags);
1488 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001489 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001490 return;
1491 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1492 nfs4_stateid_copy(&state->stateid, stateid);
1493 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001494}
1495
Trond Myklebust1393d962016-09-22 13:39:13 -04001496static void __update_open_stateid(struct nfs4_state *state,
1497 const nfs4_stateid *open_stateid,
1498 const nfs4_stateid *deleg_stateid,
1499 fmode_t fmode,
1500 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001502 /*
1503 * Protect the call to nfs4_state_set_mode_locked and
1504 * serialise the stateid update
1505 */
Andrew Elble361cad32015-12-02 09:20:57 -05001506 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001507 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001508 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001509 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001510 set_bit(NFS_DELEGATED_STATE, &state->flags);
1511 }
1512 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001513 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001514 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001515 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001516 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Trond Myklebust1393d962016-09-22 13:39:13 -04001519static int update_open_stateid(struct nfs4_state *state,
1520 const nfs4_stateid *open_stateid,
1521 const nfs4_stateid *delegation,
1522 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001523{
Trond Myklebust1393d962016-09-22 13:39:13 -04001524 struct nfs_server *server = NFS_SERVER(state->inode);
1525 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001526 struct nfs_inode *nfsi = NFS_I(state->inode);
1527 struct nfs_delegation *deleg_cur;
Trond Myklebust1393d962016-09-22 13:39:13 -04001528 nfs4_stateid freeme = {0};
Trond Myklebust34310432008-12-23 15:21:38 -05001529 int ret = 0;
1530
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001531 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001532
1533 rcu_read_lock();
1534 deleg_cur = rcu_dereference(nfsi->delegation);
1535 if (deleg_cur == NULL)
1536 goto no_delegation;
1537
1538 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001539 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001540 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001542 goto no_delegation_unlock;
1543
1544 if (delegation == NULL)
1545 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001546 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001547 goto no_delegation_unlock;
1548
Trond Myklebustb7391f42008-12-23 15:21:52 -05001549 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001550 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1551 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001552 ret = 1;
1553no_delegation_unlock:
1554 spin_unlock(&deleg_cur->lock);
1555no_delegation:
1556 rcu_read_unlock();
1557
1558 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001559 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001560 ret = 1;
1561 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001562 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001563 nfs4_schedule_state_manager(clp);
1564 if (freeme.type != 0)
1565 nfs4_test_and_free_stateid(server, &freeme,
1566 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001567
1568 return ret;
1569}
1570
Trond Myklebust39071e62015-01-24 15:07:56 -05001571static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1572 const nfs4_stateid *stateid)
1573{
1574 struct nfs4_state *state = lsp->ls_state;
1575 bool ret = false;
1576
1577 spin_lock(&state->state_lock);
1578 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1579 goto out_noupdate;
1580 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1581 goto out_noupdate;
1582 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1583 ret = true;
1584out_noupdate:
1585 spin_unlock(&state->state_lock);
1586 return ret;
1587}
Trond Myklebust34310432008-12-23 15:21:38 -05001588
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001589static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590{
1591 struct nfs_delegation *delegation;
1592
1593 rcu_read_lock();
1594 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001595 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001596 rcu_read_unlock();
1597 return;
1598 }
1599 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001600 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001601}
1602
Trond Myklebust6ee41262007-07-08 14:11:36 -04001603static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001604{
1605 struct nfs4_state *state = opendata->state;
1606 struct nfs_inode *nfsi = NFS_I(state->inode);
1607 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001608 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001609 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001610 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001611 nfs4_stateid stateid;
1612 int ret = -EAGAIN;
1613
Trond Myklebustaac00a82007-07-05 19:02:21 -04001614 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001615 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001616 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001617 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001618 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001619 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001620 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001621 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001622 rcu_read_lock();
1623 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001624 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001625 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001626 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001627 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001628 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001629 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001630 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001631 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001632 if (!opendata->is_recover) {
1633 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1634 if (ret != 0)
1635 goto out;
1636 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001637 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001638
1639 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001640 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001641 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001642 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001643out:
1644 return ERR_PTR(ret);
1645out_return_state:
1646 atomic_inc(&state->count);
1647 return state;
1648}
1649
Andy Adamsone23008e2012-10-02 21:07:32 -04001650static void
1651nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1652{
1653 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1654 struct nfs_delegation *delegation;
1655 int delegation_flags = 0;
1656
1657 rcu_read_lock();
1658 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1659 if (delegation)
1660 delegation_flags = delegation->flags;
1661 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001662 switch (data->o_arg.claim) {
1663 default:
1664 break;
1665 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1666 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001667 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1668 "returning a delegation for "
1669 "OPEN(CLAIM_DELEGATE_CUR)\n",
1670 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001671 return;
1672 }
1673 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001674 nfs_inode_set_delegation(state->inode,
1675 data->owner->so_cred,
1676 &data->o_res);
1677 else
1678 nfs_inode_reclaim_delegation(state->inode,
1679 data->owner->so_cred,
1680 &data->o_res);
1681}
1682
1683/*
1684 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1685 * and update the nfs4_state.
1686 */
1687static struct nfs4_state *
1688_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1689{
1690 struct inode *inode = data->state->inode;
1691 struct nfs4_state *state = data->state;
1692 int ret;
1693
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001694 if (!data->rpc_done) {
1695 if (data->rpc_status) {
1696 ret = data->rpc_status;
1697 goto err;
1698 }
1699 /* cached opens have already been processed */
1700 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001701 }
1702
Andy Adamsone23008e2012-10-02 21:07:32 -04001703 ret = nfs_refresh_inode(inode, &data->f_attr);
1704 if (ret)
1705 goto err;
1706
1707 if (data->o_res.delegation_type != 0)
1708 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001709update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001710 update_open_stateid(state, &data->o_res.stateid, NULL,
1711 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001712 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001713
1714 return state;
1715err:
1716 return ERR_PTR(ret);
1717
1718}
1719
1720static struct nfs4_state *
1721_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001722{
1723 struct inode *inode;
1724 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001725 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001726
Trond Myklebustaac00a82007-07-05 19:02:21 -04001727 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001728 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001729 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001730 goto out;
1731 }
1732
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001733 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001734 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001735 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001736 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001737 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001738 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001739 goto err;
1740 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001741 state = nfs4_get_open_state(inode, data->owner);
1742 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001743 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001744 if (data->o_res.delegation_type != 0)
1745 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001746 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001747 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001748 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001749out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001750 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001751 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001752err_put_inode:
1753 iput(inode);
1754err:
1755 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001756}
1757
Andy Adamsone23008e2012-10-02 21:07:32 -04001758static struct nfs4_state *
1759nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1760{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001761 struct nfs4_state *ret;
1762
Andy Adamsone23008e2012-10-02 21:07:32 -04001763 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001764 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1765 else
1766 ret = _nfs4_opendata_to_nfs4_state(data);
1767 nfs4_sequence_free_slot(&data->o_res.seq_res);
1768 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001769}
1770
Trond Myklebust864472e2006-01-03 09:55:15 +01001771static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1772{
1773 struct nfs_inode *nfsi = NFS_I(state->inode);
1774 struct nfs_open_context *ctx;
1775
1776 spin_lock(&state->inode->i_lock);
1777 list_for_each_entry(ctx, &nfsi->open_files, list) {
1778 if (ctx->state != state)
1779 continue;
1780 get_nfs_open_context(ctx);
1781 spin_unlock(&state->inode->i_lock);
1782 return ctx;
1783 }
1784 spin_unlock(&state->inode->i_lock);
1785 return ERR_PTR(-ENOENT);
1786}
1787
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001788static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1789 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001790{
1791 struct nfs4_opendata *opendata;
1792
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001793 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001794 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001795 if (opendata == NULL)
1796 return ERR_PTR(-ENOMEM);
1797 opendata->state = state;
1798 atomic_inc(&state->count);
1799 return opendata;
1800}
1801
Trond Myklebust24311f82015-09-20 10:50:17 -04001802static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1803 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001804{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001805 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001806 int ret;
1807
Trond Myklebust24311f82015-09-20 10:50:17 -04001808 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001809 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001810 opendata->o_arg.open_flags = 0;
1811 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001812 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1813 NFS_SB(opendata->dentry->d_sb),
1814 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001815 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1816 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1817 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001818 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001819 if (ret != 0)
1820 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001821 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001822 if (IS_ERR(newstate))
1823 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001824 if (newstate != opendata->state)
1825 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001826 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001827 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001828}
1829
1830static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1831{
Trond Myklebust864472e2006-01-03 09:55:15 +01001832 int ret;
1833
Trond Myklebust4f14c192014-02-12 19:15:06 -05001834 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1835 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1836 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1837 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001838 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001839 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001840 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001841 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001842 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1843 if (ret != 0)
1844 return ret;
1845 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1846 if (ret != 0)
1847 return ret;
1848 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1849 if (ret != 0)
1850 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001851 /*
1852 * We may have performed cached opens for all three recoveries.
1853 * Check if we need to update the current stateid.
1854 */
1855 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001856 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001857 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001858 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001859 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001860 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001861 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001862 return 0;
1863}
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865/*
1866 * OPEN_RECLAIM:
1867 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001869static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001871 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001872 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001873 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 int status;
1875
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001876 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1877 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001878 if (IS_ERR(opendata))
1879 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001880 rcu_read_lock();
1881 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001882 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001883 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001884 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001885 opendata->o_arg.u.delegation_type = delegation_type;
1886 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001887 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return status;
1889}
1890
Trond Myklebust539cd032007-06-05 11:46:42 -04001891static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 struct nfs_server *server = NFS_SERVER(state->inode);
1894 struct nfs4_exception exception = { };
1895 int err;
1896 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001897 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001898 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001899 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1900 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001901 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001902 break;
1903 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 } while (exception.retry);
1905 return err;
1906}
1907
Trond Myklebust864472e2006-01-03 09:55:15 +01001908static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1909{
1910 struct nfs_open_context *ctx;
1911 int ret;
1912
1913 ctx = nfs4_state_find_open_context(state);
1914 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001915 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001916 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001917 put_nfs_open_context(ctx);
1918 return ret;
1919}
1920
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001921static 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 -07001922{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001923 switch (err) {
1924 default:
1925 printk(KERN_ERR "NFS: %s: unhandled error "
1926 "%d.\n", __func__, err);
1927 case 0:
1928 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001929 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001930 case -ESTALE:
1931 break;
1932 case -NFS4ERR_BADSESSION:
1933 case -NFS4ERR_BADSLOT:
1934 case -NFS4ERR_BAD_HIGH_SLOT:
1935 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1936 case -NFS4ERR_DEADSESSION:
1937 set_bit(NFS_DELEGATED_STATE, &state->flags);
1938 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1939 return -EAGAIN;
1940 case -NFS4ERR_STALE_CLIENTID:
1941 case -NFS4ERR_STALE_STATEID:
1942 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001943 /* Don't recall a delegation if it was lost */
1944 nfs4_schedule_lease_recovery(server->nfs_client);
1945 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001946 case -NFS4ERR_MOVED:
1947 nfs4_schedule_migration_recovery(server);
1948 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001949 case -NFS4ERR_LEASE_MOVED:
1950 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1951 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001952 case -NFS4ERR_DELEG_REVOKED:
1953 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001954 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001955 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001956 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001957 nfs_inode_find_state_and_recover(state->inode,
1958 stateid);
1959 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001960 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001961 case -NFS4ERR_DELAY:
1962 case -NFS4ERR_GRACE:
1963 set_bit(NFS_DELEGATED_STATE, &state->flags);
1964 ssleep(1);
1965 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001966 case -ENOMEM:
1967 case -NFS4ERR_DENIED:
1968 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1969 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return err;
1972}
1973
Trond Myklebust24311f82015-09-20 10:50:17 -04001974int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1975 struct nfs4_state *state, const nfs4_stateid *stateid,
1976 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001977{
1978 struct nfs_server *server = NFS_SERVER(state->inode);
1979 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001980 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001981
1982 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1983 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1984 if (IS_ERR(opendata))
1985 return PTR_ERR(opendata);
1986 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001987 write_seqlock(&state->seqlock);
1988 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1989 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001990 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1991 switch (type & (FMODE_READ|FMODE_WRITE)) {
1992 case FMODE_READ|FMODE_WRITE:
1993 case FMODE_WRITE:
1994 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1995 if (err)
1996 break;
1997 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1998 if (err)
1999 break;
2000 case FMODE_READ:
2001 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2002 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002003 nfs4_opendata_put(opendata);
2004 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2005}
2006
Chuck Leverbe05c862013-08-09 12:49:47 -04002007static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2008{
2009 struct nfs4_opendata *data = calldata;
2010
Peng Taocb04ad22014-06-11 05:24:15 +08002011 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2012 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002013}
2014
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002015static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2016{
2017 struct nfs4_opendata *data = calldata;
2018
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002019 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002020
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002021 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002022 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002023 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002024 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002025 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002026 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002027 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002028}
2029
2030static void nfs4_open_confirm_release(void *calldata)
2031{
2032 struct nfs4_opendata *data = calldata;
2033 struct nfs4_state *state = NULL;
2034
2035 /* If this request hasn't been cancelled, do nothing */
2036 if (data->cancelled == 0)
2037 goto out_free;
2038 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002039 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002040 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002041 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002042 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002043 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002044out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002045 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002046}
2047
2048static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002049 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002050 .rpc_call_done = nfs4_open_confirm_done,
2051 .rpc_release = nfs4_open_confirm_release,
2052};
2053
2054/*
2055 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2056 */
2057static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2058{
David Howells2b0143b2015-03-17 22:25:59 +00002059 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002060 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002061 struct rpc_message msg = {
2062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2063 .rpc_argp = &data->c_arg,
2064 .rpc_resp = &data->c_res,
2065 .rpc_cred = data->owner->so_cred,
2066 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002067 struct rpc_task_setup task_setup_data = {
2068 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002069 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002070 .callback_ops = &nfs4_open_confirm_ops,
2071 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002072 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002073 .flags = RPC_TASK_ASYNC,
2074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 int status;
2076
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002077 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002078 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002079 data->rpc_done = 0;
2080 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002081 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002082 if (data->is_recover)
2083 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002084 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002085 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002086 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002087 status = nfs4_wait_for_completion_rpc_task(task);
2088 if (status != 0) {
2089 data->cancelled = 1;
2090 smp_wmb();
2091 } else
2092 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002093 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return status;
2095}
2096
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002097static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002099 struct nfs4_opendata *data = calldata;
2100 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002101 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002102 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002103
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002104 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002105 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002106 /*
2107 * Check if we still need to send an OPEN call, or if we can use
2108 * a delegation instead.
2109 */
2110 if (data->state != NULL) {
2111 struct nfs_delegation *delegation;
2112
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002113 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002114 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002115 rcu_read_lock();
2116 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002117 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002118 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002119 rcu_read_unlock();
2120 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002121 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002122 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002123 switch (claim) {
2124 default:
2125 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002126 case NFS4_OPEN_CLAIM_PREVIOUS:
2127 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2128 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002129 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002130 case NFS4_OPEN_CLAIM_FH:
2131 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002132 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2133 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002134 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002135 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002136 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002137 &data->o_res.seq_res,
2138 task) != 0)
2139 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002140
2141 /* Set the create mode (note dependency on the session type) */
2142 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2143 if (data->o_arg.open_flags & O_EXCL) {
2144 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2145 if (nfs4_has_persistent_session(clp))
2146 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2147 else if (clp->cl_mvops->minor_version > 0)
2148 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2149 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002150 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002151unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002152 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002153 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002154out_no_action:
2155 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002156out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002157 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002158}
2159
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002160static void nfs4_open_done(struct rpc_task *task, void *calldata)
2161{
2162 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002164 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002165
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002166 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002167 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002168
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002169 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002170 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2171 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002172 case S_IFREG:
2173 break;
2174 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002175 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002176 break;
2177 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002178 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002179 break;
2180 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002181 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002182 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002183 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002184 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002185 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2186 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002187 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002188 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002189}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002190
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191static void nfs4_open_release(void *calldata)
2192{
2193 struct nfs4_opendata *data = calldata;
2194 struct nfs4_state *state = NULL;
2195
2196 /* If this request hasn't been cancelled, do nothing */
2197 if (data->cancelled == 0)
2198 goto out_free;
2199 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002200 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002201 goto out_free;
2202 /* In case we need an open_confirm, no cleanup! */
2203 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2204 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002206 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002207 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002208out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002209 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002210}
2211
2212static const struct rpc_call_ops nfs4_open_ops = {
2213 .rpc_call_prepare = nfs4_open_prepare,
2214 .rpc_call_done = nfs4_open_done,
2215 .rpc_release = nfs4_open_release,
2216};
2217
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002218static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002219{
David Howells2b0143b2015-03-17 22:25:59 +00002220 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002221 struct nfs_server *server = NFS_SERVER(dir);
2222 struct nfs_openargs *o_arg = &data->o_arg;
2223 struct nfs_openres *o_res = &data->o_res;
2224 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002225 struct rpc_message msg = {
2226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2227 .rpc_argp = o_arg,
2228 .rpc_resp = o_res,
2229 .rpc_cred = data->owner->so_cred,
2230 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002231 struct rpc_task_setup task_setup_data = {
2232 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002233 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002234 .callback_ops = &nfs4_open_ops,
2235 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002236 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002237 .flags = RPC_TASK_ASYNC,
2238 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002239 int status;
2240
Chuck Levera9c92d62013-08-09 12:48:18 -04002241 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002242 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002243 data->rpc_done = 0;
2244 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002245 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002246 data->is_recover = 0;
2247 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002248 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002249 data->is_recover = 1;
2250 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002251 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002252 if (IS_ERR(task))
2253 return PTR_ERR(task);
2254 status = nfs4_wait_for_completion_rpc_task(task);
2255 if (status != 0) {
2256 data->cancelled = 1;
2257 smp_wmb();
2258 } else
2259 status = data->rpc_status;
2260 rpc_put_task(task);
2261
2262 return status;
2263}
2264
2265static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2266{
David Howells2b0143b2015-03-17 22:25:59 +00002267 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002268 struct nfs_openres *o_res = &data->o_res;
2269 int status;
2270
2271 status = nfs4_run_open_task(data, 1);
2272 if (status != 0 || !data->rpc_done)
2273 return status;
2274
Trond Myklebust6926afd2012-01-07 13:22:46 -05002275 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2276
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002277 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2278 status = _nfs4_proc_open_confirm(data);
2279 if (status != 0)
2280 return status;
2281 }
2282
2283 return status;
2284}
2285
Trond Myklebustf3792d62014-07-10 08:54:32 -04002286/*
2287 * Additional permission checks in order to distinguish between an
2288 * open for read, and an open for execute. This works around the
2289 * fact that NFSv4 OPEN treats read and execute permissions as being
2290 * the same.
2291 * Note that in the non-execute case, we want to turn off permission
2292 * checking if we just created a new file (POSIX open() semantics).
2293 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002294static int nfs4_opendata_access(struct rpc_cred *cred,
2295 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002296 struct nfs4_state *state, fmode_t fmode,
2297 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002298{
2299 struct nfs_access_entry cache;
2300 u32 mask;
2301
2302 /* access call failed or for some reason the server doesn't
2303 * support any access modes -- defer access call until later */
2304 if (opendata->o_res.access_supported == 0)
2305 return 0;
2306
2307 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002308 /*
2309 * Use openflags to check for exec, because fmode won't
2310 * always have FMODE_EXEC set when file open for exec.
2311 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002312 if (openflags & __FMODE_EXEC) {
2313 /* ONLY check for exec rights */
2314 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002315 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002316 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002317
2318 cache.cred = cred;
2319 cache.jiffies = jiffies;
2320 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2321 nfs_access_add_cache(state->inode, &cache);
2322
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002323 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002324 return 0;
2325
2326 /* even though OPEN succeeded, access is denied. Close the file */
2327 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002328 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002329}
2330
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002331/*
2332 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2333 */
2334static int _nfs4_proc_open(struct nfs4_opendata *data)
2335{
David Howells2b0143b2015-03-17 22:25:59 +00002336 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002337 struct nfs_server *server = NFS_SERVER(dir);
2338 struct nfs_openargs *o_arg = &data->o_arg;
2339 struct nfs_openres *o_res = &data->o_res;
2340 int status;
2341
2342 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002343 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002344 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002345 if (status != 0) {
2346 if (status == -NFS4ERR_BADNAME &&
2347 !(o_arg->open_flags & O_CREAT))
2348 return -ENOENT;
2349 return status;
2350 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002351
Trond Myklebust6926afd2012-01-07 13:22:46 -05002352 nfs_fattr_map_and_free_names(server, &data->f_attr);
2353
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002354 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002355 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002356 if (o_arg->open_flags & O_EXCL)
2357 data->file_created = 1;
2358 else if (o_res->cinfo.before != o_res->cinfo.after)
2359 data->file_created = 1;
2360 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002361 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2362 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002364 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002366 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002369 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
Andy Adamsond83217c2011-03-01 01:34:17 +00002373static int nfs4_recover_expired_lease(struct nfs_server *server)
2374{
2375 return nfs4_client_recover_expired_lease(server->nfs_client);
2376}
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378/*
2379 * OPEN_EXPIRED:
2380 * reclaim state on the server after a network partition.
2381 * Assumes caller holds the appropriate lock
2382 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002383static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002385 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002386 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002388 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002389 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002390 if (IS_ERR(opendata))
2391 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002392 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002393 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002394 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002395 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002396 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002399static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002400{
Trond Myklebust539cd032007-06-05 11:46:42 -04002401 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002402 struct nfs4_exception exception = { };
2403 int err;
2404
2405 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002406 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002407 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002408 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2409 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002410 switch (err) {
2411 default:
2412 goto out;
2413 case -NFS4ERR_GRACE:
2414 case -NFS4ERR_DELAY:
2415 nfs4_handle_exception(server, err, &exception);
2416 err = 0;
2417 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002418 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002419out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002420 return err;
2421}
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2424{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002426 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Trond Myklebust864472e2006-01-03 09:55:15 +01002428 ctx = nfs4_state_find_open_context(state);
2429 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002430 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002431 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002432 put_nfs_open_context(ctx);
2433 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Trond Myklebust41020b62016-09-22 13:38:58 -04002436static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2437 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002438{
Trond Myklebust41020b62016-09-22 13:38:58 -04002439 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002440 write_seqlock(&state->seqlock);
2441 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2442 write_sequnlock(&state->seqlock);
2443 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2444}
2445
2446static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2447{
2448 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002449 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002450}
2451
2452static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2453{
2454 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2455 nfs40_clear_delegation_stateid(state);
2456 return nfs4_open_expired(sp, state);
2457}
2458
Trond Myklebust45870d62016-09-22 13:38:59 -04002459static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2460 nfs4_stateid *stateid,
2461 struct rpc_cred *cred)
2462{
2463 return -NFS4ERR_BAD_STATEID;
2464}
2465
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002466#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002467static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2468 nfs4_stateid *stateid,
2469 struct rpc_cred *cred)
2470{
2471 int status;
2472
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002473 switch (stateid->type) {
2474 default:
2475 break;
2476 case NFS4_INVALID_STATEID_TYPE:
2477 case NFS4_SPECIAL_STATEID_TYPE:
2478 return -NFS4ERR_BAD_STATEID;
2479 case NFS4_REVOKED_STATEID_TYPE:
2480 goto out_free;
2481 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002482
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002483 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002484 switch (status) {
2485 case -NFS4ERR_EXPIRED:
2486 case -NFS4ERR_ADMIN_REVOKED:
2487 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002488 break;
2489 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002490 return status;
2491 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002492out_free:
2493 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002494 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002495 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002496}
2497
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002498static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002499{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002500 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002501 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002502 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002503 struct rpc_cred *cred;
2504 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002505
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002506 /* Get the delegation credential for use by test/free_stateid */
2507 rcu_read_lock();
2508 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002509 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002510 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002511 return;
2512 }
Trond Myklebust41020b62016-09-22 13:38:58 -04002513
2514 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002515 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2516 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002517 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002518 return;
2519 }
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002520
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002521 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2522 rcu_read_unlock();
2523 return;
2524 }
2525
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002526 cred = get_rpccred(delegation->cred);
2527 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002528 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002529 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002530 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002531 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002532
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002533 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002534}
2535
2536/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002537 * nfs41_check_expired_locks - possibly free a lock stateid
2538 *
2539 * @state: NFSv4 state for an inode
2540 *
2541 * Returns NFS_OK if recovery for this stateid is now finished.
2542 * Otherwise a negative NFS4ERR value is returned.
2543 */
2544static int nfs41_check_expired_locks(struct nfs4_state *state)
2545{
2546 int status, ret = NFS_OK;
2547 struct nfs4_lock_state *lsp;
2548 struct nfs_server *server = NFS_SERVER(state->inode);
2549
2550 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2551 goto out;
2552 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2553 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2554 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2555
2556 status = nfs41_test_and_free_expired_stateid(server,
2557 &lsp->ls_stateid,
2558 cred);
2559 trace_nfs4_test_lock_stateid(state, lsp, status);
2560 if (status == -NFS4ERR_EXPIRED ||
2561 status == -NFS4ERR_BAD_STATEID) {
2562 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2563 if (!recover_lost_locks)
2564 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2565 } else if (status != NFS_OK) {
2566 ret = status;
2567 break;
2568 }
2569 }
2570 };
2571out:
2572 return ret;
2573}
2574
2575/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002576 * nfs41_check_open_stateid - possibly free an open stateid
2577 *
2578 * @state: NFSv4 state for an inode
2579 *
2580 * Returns NFS_OK if recovery for this stateid is now finished.
2581 * Otherwise a negative NFS4ERR value is returned.
2582 */
2583static int nfs41_check_open_stateid(struct nfs4_state *state)
2584{
2585 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002586 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002587 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002588 int status;
2589
Trond Myklebustb134fc42016-09-22 13:39:16 -04002590 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2591 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2592 return NFS_OK;
2593 return -NFS4ERR_BAD_STATEID;
2594 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002595 /* If a state reset has been done, test_stateid is unneeded */
2596 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2597 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2598 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2599 return -NFS4ERR_BAD_STATEID;
2600
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002601 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002602 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002603 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002604 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2605 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2606 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002607 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002608 }
2609 return status;
2610}
2611
2612static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2613{
Chuck Levereb64cf92012-07-11 16:30:05 -04002614 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002615
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002616 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002617 status = nfs41_check_expired_locks(state);
2618 if (status != NFS_OK)
2619 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002621 if (status != NFS_OK)
2622 status = nfs4_open_expired(sp, state);
2623 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002624}
2625#endif
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002628 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2629 * fields corresponding to attributes that were used to store the verifier.
2630 * Make sure we clobber those fields in the later setattr call
2631 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002632static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2633 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002634{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002635 const u32 *attrset = opendata->o_res.attrset;
2636
2637 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002638 !(sattr->ia_valid & ATTR_ATIME_SET))
2639 sattr->ia_valid |= ATTR_ATIME;
2640
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002641 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002642 !(sattr->ia_valid & ATTR_MTIME_SET))
2643 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002644
2645 /* Except MODE, it seems harmless of setting twice. */
2646 if ((attrset[1] & FATTR4_WORD1_MODE))
2647 sattr->ia_valid &= ~ATTR_MODE;
2648
2649 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2650 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002651}
2652
Trond Myklebustc21443c2013-02-07 14:26:21 -05002653static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2654 fmode_t fmode,
2655 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002656 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002657{
2658 struct nfs4_state_owner *sp = opendata->owner;
2659 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002660 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002661 struct nfs4_state *state;
2662 unsigned int seq;
2663 int ret;
2664
2665 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2666
2667 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002668 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002669 goto out;
2670
2671 state = nfs4_opendata_to_nfs4_state(opendata);
2672 ret = PTR_ERR(state);
2673 if (IS_ERR(state))
2674 goto out;
2675 if (server->caps & NFS_CAP_POSIX_LOCK)
2676 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002677 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2678 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002679
Trond Myklebust275bb302013-05-29 13:11:28 -04002680 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002681 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002682 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002683 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002684 alias = d_exact_alias(dentry, state->inode);
2685 if (!alias)
2686 alias = d_splice_alias(igrab(state->inode), dentry);
2687 /* d_splice_alias() can't fail here - it's a non-directory */
2688 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002689 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002690 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002691 }
2692 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002693 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002694 }
2695
Trond Myklebustc21443c2013-02-07 14:26:21 -05002696 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2697 if (ret != 0)
2698 goto out;
2699
Trond Myklebust3efb9722013-05-29 13:17:04 -04002700 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002701 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002702 nfs_inode_attach_open_context(ctx);
2703 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2704 nfs4_schedule_stateid_recovery(server, state);
2705 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002706out:
2707 return ret;
2708}
2709
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002710/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002711 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 */
Andy Adamson82be4172012-05-23 05:02:35 -04002713static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002714 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002715 int flags,
2716 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002717 struct nfs4_label *label,
2718 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 struct nfs4_state_owner *sp;
2721 struct nfs4_state *state = NULL;
2722 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002723 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002724 struct dentry *dentry = ctx->dentry;
2725 struct rpc_cred *cred = ctx->cred;
2726 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2727 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002728 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002729 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002730 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002734 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2735 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2737 goto out_err;
2738 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002739 status = nfs4_recover_expired_lease(server);
2740 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002741 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002742 if (d_really_is_positive(dentry))
2743 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002744 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002745 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002746 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002747 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002748 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002749 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002750 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
David Quigley14c43f72013-05-22 12:50:43 -04002752 if (label) {
2753 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2754 if (IS_ERR(olabel)) {
2755 status = PTR_ERR(olabel);
2756 goto err_opendata_put;
2757 }
2758 }
2759
Trond Myklebuste911b812014-03-26 13:24:37 -07002760 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2761 if (!opendata->f_attr.mdsthreshold) {
2762 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2763 if (!opendata->f_attr.mdsthreshold)
2764 goto err_free_label;
2765 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002766 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002767 }
David Howells2b0143b2015-03-17 22:25:59 +00002768 if (d_really_is_positive(dentry))
2769 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002770
Trond Myklebust3efb9722013-05-29 13:17:04 -04002771 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002772 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002773 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002774 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002775
NeilBrownefcbc042015-07-30 13:00:56 +10002776 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002777 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002778 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002779 /*
2780 * send create attributes which was not set by open
2781 * with an extra setattr.
2782 */
2783 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2784 nfs_fattr_init(opendata->o_res.f_attr);
2785 status = nfs4_do_setattr(state->inode, cred,
2786 opendata->o_res.f_attr, sattr,
2787 state, label, olabel);
2788 if (status == 0) {
2789 nfs_setattr_update_inode(state->inode, sattr,
2790 opendata->o_res.f_attr);
2791 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2792 }
David Quigley1775fd32013-05-22 12:50:42 -04002793 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002794 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002795 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002796 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002797
Trond Myklebuste911b812014-03-26 13:24:37 -07002798 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002799 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002800 opendata->f_attr.mdsthreshold = NULL;
2801 }
Andy Adamson82be4172012-05-23 05:02:35 -04002802
David Quigley14c43f72013-05-22 12:50:43 -04002803 nfs4_label_free(olabel);
2804
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002805 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002808err_free_label:
2809 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002810err_opendata_put:
2811 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002812err_put_state_owner:
2813 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return status;
2816}
2817
2818
Andy Adamson82be4172012-05-23 05:02:35 -04002819static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002820 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002821 int flags,
2822 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002823 struct nfs4_label *label,
2824 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002826 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 struct nfs4_exception exception = { };
2828 struct nfs4_state *res;
2829 int status;
2830
2831 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002832 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002833 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002834 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (status == 0)
2836 break;
2837 /* NOTE: BAD_SEQID means the server and client disagree about the
2838 * book-keeping w.r.t. state-changing operations
2839 * (OPEN/CLOSE/LOCK/LOCKU...)
2840 * It is actually a sign of a bug on the client or on the server.
2841 *
2842 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002843 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 * have unhashed the old state_owner for us, and that we can
2845 * therefore safely retry using a new one. We should still warn
2846 * the user though...
2847 */
2848 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002849 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002850 " returned a bad sequence-id error!\n",
2851 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 exception.retry = 1;
2853 continue;
2854 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002855 /*
2856 * BAD_STATEID on OPEN means that the server cancelled our
2857 * state before it received the OPEN_CONFIRM.
2858 * Recover by retrying the request as per the discussion
2859 * on Page 181 of RFC3530.
2860 */
2861 if (status == -NFS4ERR_BAD_STATEID) {
2862 exception.retry = 1;
2863 continue;
2864 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002865 if (status == -EAGAIN) {
2866 /* We must have found a delegation */
2867 exception.retry = 1;
2868 continue;
2869 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002870 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2871 continue;
2872 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 status, &exception));
2874 } while (exception.retry);
2875 return res;
2876}
2877
Trond Myklebust8487c472016-06-26 08:44:35 -04002878static int _nfs4_do_setattr(struct inode *inode,
2879 struct nfs_setattrargs *arg,
2880 struct nfs_setattrres *res,
2881 struct rpc_cred *cred,
2882 struct nfs4_state *state)
2883{
2884 struct nfs_server *server = NFS_SERVER(inode);
2885 struct rpc_message msg = {
2886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2887 .rpc_argp = arg,
2888 .rpc_resp = res,
2889 .rpc_cred = cred,
2890 };
2891 struct rpc_cred *delegation_cred = NULL;
2892 unsigned long timestamp = jiffies;
2893 fmode_t fmode;
2894 bool truncate;
2895 int status;
2896
2897 nfs_fattr_init(res->fattr);
2898
2899 /* Servers should only apply open mode checks for file size changes */
2900 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2901 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2902
2903 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2904 /* Use that stateid */
2905 } else if (truncate && state != NULL) {
2906 struct nfs_lockowner lockowner = {
2907 .l_owner = current->files,
2908 .l_pid = current->tgid,
2909 };
2910 if (!nfs4_valid_open_stateid(state))
2911 return -EBADF;
2912 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2913 &arg->stateid, &delegation_cred) == -EIO)
2914 return -EBADF;
2915 } else
2916 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2917 if (delegation_cred)
2918 msg.rpc_cred = delegation_cred;
2919
2920 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2921
2922 put_rpccred(delegation_cred);
2923 if (status == 0 && state != NULL)
2924 renew_lease(server, timestamp);
2925 trace_nfs4_setattr(inode, &arg->stateid, status);
2926 return status;
2927}
2928
2929static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2930 struct nfs_fattr *fattr, struct iattr *sattr,
2931 struct nfs4_state *state, struct nfs4_label *ilabel,
2932 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002934 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002936 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 .iap = sattr,
2938 .server = server,
2939 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002940 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 };
2942 struct nfs_setattrres res = {
2943 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002944 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 .server = server,
2946 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002947 struct nfs4_exception exception = {
2948 .state = state,
2949 .inode = inode,
2950 .stateid = &arg.stateid,
2951 };
2952 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
David Quigleyaa9c2662013-05-22 12:50:44 -04002954 arg.bitmask = nfs4_bitmask(server, ilabel);
2955 if (ilabel)
2956 arg.bitmask = nfs4_bitmask(server, olabel);
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002959 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002960 switch (err) {
2961 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002962 if (!(sattr->ia_valid & ATTR_SIZE)) {
2963 pr_warn_once("NFSv4: server %s is incorrectly "
2964 "applying open mode checks to "
2965 "a SETATTR that is not "
2966 "changing file size.\n",
2967 server->nfs_client->cl_hostname);
2968 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002969 if (state && !(state->state & FMODE_WRITE)) {
2970 err = -EBADF;
2971 if (sattr->ia_valid & ATTR_OPEN)
2972 err = -EACCES;
2973 goto out;
2974 }
2975 }
2976 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002978out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return err;
2980}
2981
Peng Tao500d7012015-09-22 11:35:22 +08002982static bool
2983nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2984{
2985 if (inode == NULL || !nfs_have_layout(inode))
2986 return false;
2987
2988 return pnfs_wait_on_layoutreturn(inode, task);
2989}
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991struct nfs4_closedata {
2992 struct inode *inode;
2993 struct nfs4_state *state;
2994 struct nfs_closeargs arg;
2995 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002996 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002997 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002998 bool roc;
2999 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000};
3001
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003002static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003003{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003004 struct nfs4_closedata *calldata = data;
3005 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003006 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003007
Fred Isamanf7e89172011-01-06 11:36:32 +00003008 if (calldata->roc)
3009 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003010 nfs4_put_open_state(calldata->state);
3011 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003012 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003013 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003014 kfree(calldata);
3015}
3016
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003017static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003019 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003022 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Chuck Levera3ca5652012-03-01 17:00:40 -05003024 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003025 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3026 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003027 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 /* hmm. we are done with the inode, and in the process of freeing
3029 * the state_owner. we keep this around to process errors
3030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 switch (task->tk_status) {
3032 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003033 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003034 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003035 pnfs_roc_set_barrier(state->inode,
3036 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003037 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003038 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003039 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003041 case -NFS4ERR_EXPIRED:
3042 nfs4_free_revoked_stateid(server,
3043 &calldata->arg.stateid,
3044 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003045 case -NFS4ERR_OLD_STATEID:
3046 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003047 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003048 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003049 rpc_restart_call_prepare(task);
3050 goto out_release;
3051 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003052 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003053 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003055 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003056 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003057 goto out_release;
3058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003060 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3061 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003062out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003063 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003064 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003065 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003068static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003070 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003071 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003072 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003073 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003074 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003075
Chuck Levera3ca5652012-03-01 17:00:40 -05003076 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003077 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003078 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003079
Trond Myklebust88069f72009-12-08 08:33:16 -05003080 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003081 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003082 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3083 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3084 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003085 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003086 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003087 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003088 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003089 if (state->n_rdonly == 0)
3090 call_close |= is_rdonly;
3091 else if (is_rdonly)
3092 calldata->arg.fmode |= FMODE_READ;
3093 if (state->n_wronly == 0)
3094 call_close |= is_wronly;
3095 else if (is_wronly)
3096 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003097 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3098 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003099 } else if (is_rdwr)
3100 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3101
Trond Myklebust5d422302013-03-14 16:57:48 -04003102 if (!nfs4_valid_open_stateid(state))
3103 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003104 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003105
3106 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003107 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003108 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003109 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003110
Peng Tao500d7012015-09-22 11:35:22 +08003111 if (nfs4_wait_on_layoutreturn(inode, task)) {
3112 nfs_release_seqid(calldata->arg.seqid);
3113 goto out_wait;
3114 }
3115
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003116 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003117 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003118 if (calldata->roc)
3119 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3120
Trond Myklebust6ae37332015-01-30 14:21:14 -05003121 calldata->arg.share_access =
3122 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3123 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003124
Trond Myklebust516a6af2005-10-27 22:12:41 -04003125 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003126 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003127 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003128 &calldata->arg.seq_args,
3129 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003130 task) != 0)
3131 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003132 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003133 return;
3134out_no_action:
3135 task->tk_action = NULL;
3136out_wait:
3137 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003140static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003141 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003142 .rpc_call_done = nfs4_close_done,
3143 .rpc_release = nfs4_free_closedata,
3144};
3145
Peng Taofe08c542014-07-03 13:05:01 +08003146static bool nfs4_roc(struct inode *inode)
3147{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003148 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003149 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003150 return pnfs_roc(inode);
3151}
3152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153/*
3154 * It is possible for data to be read/written from a mem-mapped file
3155 * after the sys_close call (which hits the vfs layer as a flush).
3156 * This means that we can't safely call nfsv4 close on a file until
3157 * the inode is cleared. This in turn means that we are not good
3158 * NFSv4 citizens - we do not indicate to the server to update the file's
3159 * share state even when we are done with one of the three share
3160 * stateid's in the inode.
3161 *
3162 * NOTE: Caller must be holding the sp->so_owner semaphore!
3163 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003164int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003166 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003167 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003169 struct nfs4_state_owner *sp = state->owner;
3170 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003171 struct rpc_message msg = {
3172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3173 .rpc_cred = state->owner->so_cred,
3174 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003175 struct rpc_task_setup task_setup_data = {
3176 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003177 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003178 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003179 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003180 .flags = RPC_TASK_ASYNC,
3181 };
Trond Myklebust95121352005-10-18 14:20:12 -07003182 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003184 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3185 &task_setup_data.rpc_client, &msg);
3186
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003187 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003189 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003190 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003191 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003193 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003195 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3196 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003197 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003198 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003199 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003200 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003201 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003202 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003203 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003204 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003205 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003206
Trond Myklebust1174dd12010-12-21 10:52:24 -05003207 msg.rpc_argp = &calldata->arg;
3208 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003209 task_setup_data.callback_data = calldata;
3210 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003211 if (IS_ERR(task))
3212 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003213 status = 0;
3214 if (wait)
3215 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003216 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003217 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003218out_free_calldata:
3219 kfree(calldata);
3220out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003221 nfs4_put_open_state(state);
3222 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003223 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
Trond Myklebust2b484292010-09-17 10:56:51 -04003226static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003227nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3228 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003231 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3232
3233 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Trond Myklebust565277f2007-10-15 18:17:53 -04003235 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003236 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003237
3238 nfs4_label_release_security(label);
3239
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003240 if (IS_ERR(state))
3241 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003242 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243}
3244
Trond Myklebust1185a552009-12-03 15:54:02 -05003245static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003246{
3247 if (ctx->state == NULL)
3248 return;
3249 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003250 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003251 else
Al Viro643168c2011-06-22 18:20:23 -04003252 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003253}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Trond Myklebustb944dba2013-11-04 15:20:20 -05003255#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3256#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003257#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3260{
Kinglong Mee8c612822015-08-26 21:12:58 +08003261 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003262 struct nfs4_server_caps_arg args = {
3263 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003264 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003265 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct nfs4_server_caps_res res = {};
3267 struct rpc_message msg = {
3268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003269 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 .rpc_resp = &res,
3271 };
3272 int status;
3273
Kinglong Mee8c612822015-08-26 21:12:58 +08003274 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3275 FATTR4_WORD0_FH_EXPIRE_TYPE |
3276 FATTR4_WORD0_LINK_SUPPORT |
3277 FATTR4_WORD0_SYMLINK_SUPPORT |
3278 FATTR4_WORD0_ACLSUPPORT;
3279 if (minorversion)
3280 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3281
Bryan Schumaker7c513052011-03-24 17:12:24 +00003282 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003284 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003285 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003286 case 0:
3287 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3288 res.attr_bitmask[2] = 0;
3289 break;
3290 case 1:
3291 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3292 break;
3293 case 2:
3294 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003297 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3298 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3299 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3300 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003301 NFS_CAP_CTIME|NFS_CAP_MTIME|
3302 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003303 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3304 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 server->caps |= NFS_CAP_ACLS;
3306 if (res.has_links != 0)
3307 server->caps |= NFS_CAP_HARDLINKS;
3308 if (res.has_symlinks != 0)
3309 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003310 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3311 server->caps |= NFS_CAP_FILEID;
3312 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3313 server->caps |= NFS_CAP_MODE;
3314 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3315 server->caps |= NFS_CAP_NLINK;
3316 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3317 server->caps |= NFS_CAP_OWNER;
3318 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3319 server->caps |= NFS_CAP_OWNER_GROUP;
3320 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3321 server->caps |= NFS_CAP_ATIME;
3322 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3323 server->caps |= NFS_CAP_CTIME;
3324 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3325 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003326#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3327 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3328 server->caps |= NFS_CAP_SECURITY_LABEL;
3329#endif
3330 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3331 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003332 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003333
Trond Myklebusta65318b2009-03-11 14:10:28 -04003334 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3335 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3336 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003337 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003338 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3339 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003341 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return status;
3345}
3346
Trond Myklebust55a97592006-06-09 09:34:19 -04003347int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
3349 struct nfs4_exception exception = { };
3350 int err;
3351 do {
3352 err = nfs4_handle_exception(server,
3353 _nfs4_server_capabilities(server, fhandle),
3354 &exception);
3355 } while (exception.retry);
3356 return err;
3357}
3358
3359static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3360 struct nfs_fsinfo *info)
3361{
David Quigleyaa9c2662013-05-22 12:50:44 -04003362 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003364 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 };
3366 struct nfs4_lookup_res res = {
3367 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003368 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 .fh = fhandle,
3370 };
3371 struct rpc_message msg = {
3372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3373 .rpc_argp = &args,
3374 .rpc_resp = &res,
3375 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003376
David Quigleyaa9c2662013-05-22 12:50:44 -04003377 bitmask[0] = nfs4_fattr_bitmap[0];
3378 bitmask[1] = nfs4_fattr_bitmap[1];
3379 /*
3380 * Process the label in the upcoming getfattr
3381 */
3382 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3383
Trond Myklebust0e574af2005-10-27 22:12:38 -04003384 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003385 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
3387
3388static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3389 struct nfs_fsinfo *info)
3390{
3391 struct nfs4_exception exception = { };
3392 int err;
3393 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003394 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003395 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003396 switch (err) {
3397 case 0:
3398 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003399 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003400 default:
3401 err = nfs4_handle_exception(server, err, &exception);
3402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003404out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return err;
3406}
3407
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003408static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3409 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3410{
Trond Myklebustc2190662013-08-26 19:23:04 -04003411 struct rpc_auth_create_args auth_args = {
3412 .pseudoflavor = flavor,
3413 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003414 struct rpc_auth *auth;
3415 int ret;
3416
Trond Myklebustc2190662013-08-26 19:23:04 -04003417 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003418 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003419 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003420 goto out;
3421 }
3422 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003423out:
3424 return ret;
3425}
3426
Chuck Lever9a744ba2013-03-16 15:56:02 -04003427/*
3428 * Retry pseudoroot lookup with various security flavors. We do this when:
3429 *
3430 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3431 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3432 *
3433 * Returns zero on success, or a negative NFS4ERR value, or a
3434 * negative errno value.
3435 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003436static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003437 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003439 /* Per 3530bis 15.33.5 */
3440 static const rpc_authflavor_t flav_array[] = {
3441 RPC_AUTH_GSS_KRB5P,
3442 RPC_AUTH_GSS_KRB5I,
3443 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003444 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003445 RPC_AUTH_NULL,
3446 };
3447 int status = -EPERM;
3448 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003450 if (server->auth_info.flavor_len > 0) {
3451 /* try each flavor specified by user */
3452 for (i = 0; i < server->auth_info.flavor_len; i++) {
3453 status = nfs4_lookup_root_sec(server, fhandle, info,
3454 server->auth_info.flavors[i]);
3455 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3456 continue;
3457 break;
3458 }
3459 } else {
3460 /* no flavors specified by user, try default list */
3461 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3462 status = nfs4_lookup_root_sec(server, fhandle, info,
3463 flav_array[i]);
3464 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3465 continue;
3466 break;
3467 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003468 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003469
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003470 /*
3471 * -EACCESS could mean that the user doesn't have correct permissions
3472 * to access the mount. It could also mean that we tried to mount
3473 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3474 * existing mount programs don't handle -EACCES very well so it should
3475 * be mapped to -EPERM instead.
3476 */
3477 if (status == -EACCES)
3478 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003479 return status;
3480}
3481
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003482/**
3483 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3484 * @server: initialized nfs_server handle
3485 * @fhandle: we fill in the pseudo-fs root file handle
3486 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003487 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003488 *
3489 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003490 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003491int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003492 struct nfs_fsinfo *info,
3493 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003494{
Andre Przywarac7757072015-04-23 17:17:40 +01003495 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003496
Andre Przywarac7757072015-04-23 17:17:40 +01003497 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003498 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003499
3500 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003501 status = server->nfs_client->cl_mvops->find_root_sec(server,
3502 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 if (status == 0)
3505 status = nfs4_server_capabilities(server, fhandle);
3506 if (status == 0)
3507 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003508
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003509 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Bryan Schumakerbae36242012-05-10 15:07:31 -04003512static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3513 struct nfs_fsinfo *info)
3514{
3515 int error;
3516 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003517 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003518
3519 error = nfs4_server_capabilities(server, mntfh);
3520 if (error < 0) {
3521 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3522 return error;
3523 }
3524
David Quigley14c43f72013-05-22 12:50:43 -04003525 label = nfs4_label_alloc(server, GFP_KERNEL);
3526 if (IS_ERR(label))
3527 return PTR_ERR(label);
3528
David Quigley1775fd32013-05-22 12:50:42 -04003529 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003530 if (error < 0) {
3531 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003532 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003533 }
3534
3535 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3536 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3537 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3538
David Quigley14c43f72013-05-22 12:50:43 -04003539err_free_label:
3540 nfs4_label_free(label);
3541
Bryan Schumakerbae36242012-05-10 15:07:31 -04003542 return error;
3543}
3544
Manoj Naik6b97fd32006-06-09 09:34:29 -04003545/*
3546 * Get locations and (maybe) other attributes of a referral.
3547 * Note that we'll actually follow the referral later when
3548 * we detect fsid mismatch in inode revalidation
3549 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003550static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3551 const struct qstr *name, struct nfs_fattr *fattr,
3552 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003553{
3554 int status = -ENOMEM;
3555 struct page *page = NULL;
3556 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003557
3558 page = alloc_page(GFP_KERNEL);
3559 if (page == NULL)
3560 goto out;
3561 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3562 if (locations == NULL)
3563 goto out;
3564
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003565 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003566 if (status != 0)
3567 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003568
3569 /*
3570 * If the fsid didn't change, this is a migration event, not a
3571 * referral. Cause us to drop into the exception handler, which
3572 * will kick off migration recovery.
3573 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003574 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003575 dprintk("%s: server did not return a different fsid for"
3576 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003577 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003578 goto out;
3579 }
Andy Adamson533eb462011-06-13 18:25:56 -04003580 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3581 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003582
Andy Adamson533eb462011-06-13 18:25:56 -04003583 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003584 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003585 memset(fhandle, 0, sizeof(struct nfs_fh));
3586out:
3587 if (page)
3588 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003589 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003590 return status;
3591}
3592
David Quigley1775fd32013-05-22 12:50:42 -04003593static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3594 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
3596 struct nfs4_getattr_arg args = {
3597 .fh = fhandle,
3598 .bitmask = server->attr_bitmask,
3599 };
3600 struct nfs4_getattr_res res = {
3601 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003602 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 .server = server,
3604 };
3605 struct rpc_message msg = {
3606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3607 .rpc_argp = &args,
3608 .rpc_resp = &res,
3609 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003610
3611 args.bitmask = nfs4_bitmask(server, label);
3612
Trond Myklebust0e574af2005-10-27 22:12:38 -04003613 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003614 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
David Quigley1775fd32013-05-22 12:50:42 -04003617static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3618 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
3620 struct nfs4_exception exception = { };
3621 int err;
3622 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003623 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3624 trace_nfs4_getattr(server, fhandle, fattr, err);
3625 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 &exception);
3627 } while (exception.retry);
3628 return err;
3629}
3630
3631/*
3632 * The file is not closed if it is opened due to the a request to change
3633 * the size of the file. The open call will not be needed once the
3634 * VFS layer lookup-intents are implemented.
3635 *
3636 * Close is called when the inode is destroyed.
3637 * If we haven't opened the file for O_WRONLY, we
3638 * need to in the size_change case to obtain a stateid.
3639 *
3640 * Got race?
3641 * Because OPEN is always done by name in nfsv4, it is
3642 * possible that we opened a different file by the same
3643 * name. We can recognize this race condition, but we
3644 * can't do anything about it besides returning an error.
3645 *
3646 * This will be fixed with VFS changes (lookup-intent).
3647 */
3648static int
3649nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3650 struct iattr *sattr)
3651{
David Howells2b0143b2015-03-17 22:25:59 +00003652 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003653 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003654 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003655 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 int status;
3657
Peng Tao88ac8152014-09-12 11:04:10 +08003658 if (pnfs_ld_layoutret_on_setattr(inode) &&
3659 sattr->ia_valid & ATTR_SIZE &&
3660 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003661 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003662
Trond Myklebust0e574af2005-10-27 22:12:38 -04003663 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Andy Adamson26699402012-05-30 16:12:24 -04003665 /* Deal with open(O_TRUNC) */
3666 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003667 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003668
3669 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003670 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003671 return 0;
3672
Trond Myklebustd5308382005-11-04 15:33:38 -05003673 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003674 if (sattr->ia_valid & ATTR_FILE) {
3675 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003676
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003677 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003678 if (ctx) {
3679 cred = ctx->cred;
3680 state = ctx->state;
3681 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003682 }
3683
David Quigley14c43f72013-05-22 12:50:43 -04003684 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3685 if (IS_ERR(label))
3686 return PTR_ERR(label);
3687
3688 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003689 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003690 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003691 nfs_setsecurity(inode, fattr, label);
3692 }
David Quigley14c43f72013-05-22 12:50:43 -04003693 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return status;
3695}
3696
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003697static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3698 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003699 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003700{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003701 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003702 int status;
3703 struct nfs4_lookup_arg args = {
3704 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003705 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003706 .name = name,
3707 };
3708 struct nfs4_lookup_res res = {
3709 .server = server,
3710 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003711 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003712 .fh = fhandle,
3713 };
3714 struct rpc_message msg = {
3715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3716 .rpc_argp = &args,
3717 .rpc_resp = &res,
3718 };
3719
David Quigleyaa9c2662013-05-22 12:50:44 -04003720 args.bitmask = nfs4_bitmask(server, label);
3721
David Howells2b3de442006-08-22 20:06:09 -04003722 nfs_fattr_init(fattr);
3723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003725 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 dprintk("NFS reply lookup: %d\n", status);
3727 return status;
3728}
3729
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003730static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003731{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003732 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003733 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003734 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3735 fattr->nlink = 2;
3736}
3737
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003738static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003739 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003740 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003741{
3742 struct nfs4_exception exception = { };
3743 struct rpc_clnt *client = *clnt;
3744 int err;
3745 do {
David Quigley1775fd32013-05-22 12:50:42 -04003746 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003747 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003748 switch (err) {
3749 case -NFS4ERR_BADNAME:
3750 err = -ENOENT;
3751 goto out;
3752 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003753 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003754 if (err == -NFS4ERR_MOVED)
3755 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003756 goto out;
3757 case -NFS4ERR_WRONGSEC:
3758 err = -EPERM;
3759 if (client != *clnt)
3760 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003761 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003762 if (IS_ERR(client))
3763 return PTR_ERR(client);
3764
3765 exception.retry = 1;
3766 break;
3767 default:
3768 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3769 }
3770 } while (exception.retry);
3771
3772out:
3773 if (err == 0)
3774 *clnt = client;
3775 else if (client != *clnt)
3776 rpc_shutdown_client(client);
3777
3778 return err;
3779}
3780
Al Virobeffb8f2016-07-20 16:34:42 -04003781static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003782 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3783 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003785 int status;
3786 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003787
David Quigley1775fd32013-05-22 12:50:42 -04003788 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003789 if (client != NFS_CLIENT(dir)) {
3790 rpc_shutdown_client(client);
3791 nfs_fixup_secinfo_attributes(fattr);
3792 }
3793 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003796struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003797nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003798 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3799{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003800 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003801 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003802
David Quigley1775fd32013-05-22 12:50:42 -04003803 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003804 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003805 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003806 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003807}
3808
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3810{
Trond Myklebust76b32992007-08-10 17:45:11 -04003811 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 struct nfs4_accessargs args = {
3813 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003814 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003816 struct nfs4_accessres res = {
3817 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003818 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 struct rpc_message msg = {
3820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3821 .rpc_argp = &args,
3822 .rpc_resp = &res,
3823 .rpc_cred = entry->cred,
3824 };
3825 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003826 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
3828 /*
3829 * Determine which access bits we want to ask for...
3830 */
3831 if (mode & MAY_READ)
3832 args.access |= NFS4_ACCESS_READ;
3833 if (S_ISDIR(inode->i_mode)) {
3834 if (mode & MAY_WRITE)
3835 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3836 if (mode & MAY_EXEC)
3837 args.access |= NFS4_ACCESS_LOOKUP;
3838 } else {
3839 if (mode & MAY_WRITE)
3840 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3841 if (mode & MAY_EXEC)
3842 args.access |= NFS4_ACCESS_EXECUTE;
3843 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003844
3845 res.fattr = nfs_alloc_fattr();
3846 if (res.fattr == NULL)
3847 return -ENOMEM;
3848
Bryan Schumaker7c513052011-03-24 17:12:24 +00003849 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003851 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003852 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003854 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 return status;
3856}
3857
3858static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3859{
3860 struct nfs4_exception exception = { };
3861 int err;
3862 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003863 err = _nfs4_proc_access(inode, entry);
3864 trace_nfs4_access(inode, err);
3865 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 &exception);
3867 } while (exception.retry);
3868 return err;
3869}
3870
3871/*
3872 * TODO: For the time being, we don't try to get any attributes
3873 * along with any of the zero-copy operations READ, READDIR,
3874 * READLINK, WRITE.
3875 *
3876 * In the case of the first three, we want to put the GETATTR
3877 * after the read-type operation -- this is because it is hard
3878 * to predict the length of a GETATTR response in v4, and thus
3879 * align the READ data correctly. This means that the GETATTR
3880 * may end up partially falling into the page cache, and we should
3881 * shift it into the 'tail' of the xdr_buf before processing.
3882 * To do this efficiently, we need to know the total length
3883 * of data received, which doesn't seem to be available outside
3884 * of the RPC layer.
3885 *
3886 * In the case of WRITE, we also want to put the GETATTR after
3887 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003888 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 *
3890 * Both of these changes to the XDR layer would in fact be quite
3891 * minor, but I decided to leave them for a subsequent patch.
3892 */
3893static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3894 unsigned int pgbase, unsigned int pglen)
3895{
3896 struct nfs4_readlink args = {
3897 .fh = NFS_FH(inode),
3898 .pgbase = pgbase,
3899 .pglen = pglen,
3900 .pages = &page,
3901 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003902 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 struct rpc_message msg = {
3904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3905 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003906 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 };
3908
Bryan Schumaker7c513052011-03-24 17:12:24 +00003909 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 -07003910}
3911
3912static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3913 unsigned int pgbase, unsigned int pglen)
3914{
3915 struct nfs4_exception exception = { };
3916 int err;
3917 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003918 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3919 trace_nfs4_readlink(inode, err);
3920 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 &exception);
3922 } while (exception.retry);
3923 return err;
3924}
3925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003927 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929static int
3930nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003931 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932{
David Quigleyaa9c2662013-05-22 12:50:44 -04003933 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003934 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 int status = 0;
3937
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003938 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3939 if (IS_ERR(ctx))
3940 return PTR_ERR(ctx);
3941
David Quigleyaa9c2662013-05-22 12:50:44 -04003942 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3943
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003944 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003945 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 if (IS_ERR(state)) {
3947 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003948 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003951 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003952 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 return status;
3954}
3955
Al Virobeffb8f2016-07-20 16:34:42 -04003956static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
Trond Myklebust16e42952005-10-27 22:12:44 -04003958 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003959 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003961 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003963 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003964 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003965 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3968 .rpc_argp = &args,
3969 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003971 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003972
Bryan Schumaker7c513052011-03-24 17:12:24 +00003973 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003974 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003975 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return status;
3977}
3978
Al Virobeffb8f2016-07-20 16:34:42 -04003979static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
3981 struct nfs4_exception exception = { };
3982 int err;
3983 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003984 err = _nfs4_proc_remove(dir, name);
3985 trace_nfs4_remove(dir, name, err);
3986 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 &exception);
3988 } while (exception.retry);
3989 return err;
3990}
3991
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003992static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003994 struct nfs_server *server = NFS_SERVER(dir);
3995 struct nfs_removeargs *args = msg->rpc_argp;
3996 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003998 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004000 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004001
4002 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003}
4004
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004005static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4006{
Al Viro884be172016-04-28 23:56:31 -04004007 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004008 &data->args.seq_args,
4009 &data->res.seq_res,
4010 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004013static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004015 struct nfs_unlinkdata *data = task->tk_calldata;
4016 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004017
Trond Myklebust14516c32010-07-31 14:29:06 -04004018 if (!nfs4_sequence_done(task, &res->seq_res))
4019 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004020 if (nfs4_async_handle_error(task, res->server, NULL,
4021 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004022 return 0;
4023 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004024 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
4026
Jeff Laytond3d41522010-09-17 17:31:57 -04004027static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4028{
4029 struct nfs_server *server = NFS_SERVER(dir);
4030 struct nfs_renameargs *arg = msg->rpc_argp;
4031 struct nfs_renameres *res = msg->rpc_resp;
4032
4033 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004034 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004035 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004036}
4037
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004038static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4039{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004040 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4041 &data->args.seq_args,
4042 &data->res.seq_res,
4043 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004044}
4045
4046static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4047 struct inode *new_dir)
4048{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004049 struct nfs_renamedata *data = task->tk_calldata;
4050 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004051
4052 if (!nfs4_sequence_done(task, &res->seq_res))
4053 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004054 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004055 return 0;
4056
4057 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004058 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004059 return 1;
4060}
4061
Al Virobeffb8f2016-07-20 16:34:42 -04004062static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004064 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 struct nfs4_link_arg arg = {
4066 .fh = NFS_FH(inode),
4067 .dir_fh = NFS_FH(dir),
4068 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004069 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004071 struct nfs4_link_res res = {
4072 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004073 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 struct rpc_message msg = {
4076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4077 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004078 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004080 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Trond Myklebust136f2622010-04-16 16:22:49 -04004082 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004083 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004084 goto out;
4085
David Quigley14c43f72013-05-22 12:50:43 -04004086 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4087 if (IS_ERR(res.label)) {
4088 status = PTR_ERR(res.label);
4089 goto out;
4090 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004091 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004092
Bryan Schumaker7c513052011-03-24 17:12:24 +00004093 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004094 if (!status) {
4095 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004096 status = nfs_post_op_update_inode(inode, res.fattr);
4097 if (!status)
4098 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004099 }
David Quigley14c43f72013-05-22 12:50:43 -04004100
4101
4102 nfs4_label_free(res.label);
4103
Trond Myklebust136f2622010-04-16 16:22:49 -04004104out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004105 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return status;
4107}
4108
Al Virobeffb8f2016-07-20 16:34:42 -04004109static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
4111 struct nfs4_exception exception = { };
4112 int err;
4113 do {
4114 err = nfs4_handle_exception(NFS_SERVER(inode),
4115 _nfs4_proc_link(inode, dir, name),
4116 &exception);
4117 } while (exception.retry);
4118 return err;
4119}
4120
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004121struct nfs4_createdata {
4122 struct rpc_message msg;
4123 struct nfs4_create_arg arg;
4124 struct nfs4_create_res res;
4125 struct nfs_fh fh;
4126 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004127 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004128};
4129
4130static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004131 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004132{
4133 struct nfs4_createdata *data;
4134
4135 data = kzalloc(sizeof(*data), GFP_KERNEL);
4136 if (data != NULL) {
4137 struct nfs_server *server = NFS_SERVER(dir);
4138
David Quigley14c43f72013-05-22 12:50:43 -04004139 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4140 if (IS_ERR(data->label))
4141 goto out_free;
4142
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004143 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4144 data->msg.rpc_argp = &data->arg;
4145 data->msg.rpc_resp = &data->res;
4146 data->arg.dir_fh = NFS_FH(dir);
4147 data->arg.server = server;
4148 data->arg.name = name;
4149 data->arg.attrs = sattr;
4150 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004151 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004152 data->res.server = server;
4153 data->res.fh = &data->fh;
4154 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004155 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004156 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004157 }
4158 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004159out_free:
4160 kfree(data);
4161 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004162}
4163
4164static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4165{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004166 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004167 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004168 if (status == 0) {
4169 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004170 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004171 }
4172 return status;
4173}
4174
4175static void nfs4_free_createdata(struct nfs4_createdata *data)
4176{
David Quigley14c43f72013-05-22 12:50:43 -04004177 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004178 kfree(data);
4179}
4180
Chuck Lever4f390c12006-08-22 20:06:22 -04004181static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004182 struct page *page, unsigned int len, struct iattr *sattr,
4183 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004185 struct nfs4_createdata *data;
4186 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Chuck Lever94a6d752006-08-22 20:06:23 -04004188 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004189 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004190
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004191 status = -ENOMEM;
4192 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4193 if (data == NULL)
4194 goto out;
4195
4196 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4197 data->arg.u.symlink.pages = &page;
4198 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004199 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004201 status = nfs4_do_create(dir, dentry, data);
4202
4203 nfs4_free_createdata(data);
4204out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 return status;
4206}
4207
Chuck Lever4f390c12006-08-22 20:06:22 -04004208static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004209 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210{
4211 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004212 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004214
4215 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004218 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4219 trace_nfs4_symlink(dir, &dentry->d_name, err);
4220 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 &exception);
4222 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004223
4224 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 return err;
4226}
4227
4228static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004229 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004231 struct nfs4_createdata *data;
4232 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004234 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4235 if (data == NULL)
4236 goto out;
4237
David Quigley1775fd32013-05-22 12:50:42 -04004238 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004239 status = nfs4_do_create(dir, dentry, data);
4240
4241 nfs4_free_createdata(data);
4242out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 return status;
4244}
4245
4246static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4247 struct iattr *sattr)
4248{
4249 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004250 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004252
David Quigleyaa9c2662013-05-22 12:50:44 -04004253 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4254
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004255 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004257 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4258 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4259 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 &exception);
4261 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004262 nfs4_label_release_security(label);
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 return err;
4265}
4266
4267static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004268 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
David Howells2b0143b2015-03-17 22:25:59 +00004270 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 struct nfs4_readdir_arg args = {
4272 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004273 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 .pgbase = 0,
4275 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004276 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004277 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 };
4279 struct nfs4_readdir_res res;
4280 struct rpc_message msg = {
4281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4282 .rpc_argp = &args,
4283 .rpc_resp = &res,
4284 .rpc_cred = cred,
4285 };
4286 int status;
4287
Al Viro6de14722013-09-16 10:53:17 -04004288 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4289 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004290 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004291 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004293 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 -05004294 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004295 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004296 status += args.pgbase;
4297 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004298
4299 nfs_invalidate_atime(dir);
4300
Harvey Harrison3110ff82008-05-02 13:42:44 -07004301 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return status;
4303}
4304
4305static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004306 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
4308 struct nfs4_exception exception = { };
4309 int err;
4310 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004311 err = _nfs4_proc_readdir(dentry, cred, cookie,
4312 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004313 trace_nfs4_readdir(d_inode(dentry), err);
4314 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 &exception);
4316 } while (exception.retry);
4317 return err;
4318}
4319
4320static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004321 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004323 struct nfs4_createdata *data;
4324 int mode = sattr->ia_mode;
4325 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004327 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4328 if (data == NULL)
4329 goto out;
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004332 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004334 data->arg.ftype = NF4BLK;
4335 data->arg.u.device.specdata1 = MAJOR(rdev);
4336 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 }
4338 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004339 data->arg.ftype = NF4CHR;
4340 data->arg.u.device.specdata1 = MAJOR(rdev);
4341 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004342 } else if (!S_ISSOCK(mode)) {
4343 status = -EINVAL;
4344 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 }
David Quigley1775fd32013-05-22 12:50:42 -04004346
David Quigleyaa9c2662013-05-22 12:50:44 -04004347 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004349out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004350 nfs4_free_createdata(data);
4351out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 return status;
4353}
4354
4355static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4356 struct iattr *sattr, dev_t rdev)
4357{
4358 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004359 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004361
David Quigleyaa9c2662013-05-22 12:50:44 -04004362 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4363
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004364 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004366 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4367 trace_nfs4_mknod(dir, &dentry->d_name, err);
4368 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 &exception);
4370 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004371
4372 nfs4_label_release_security(label);
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return err;
4375}
4376
4377static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4378 struct nfs_fsstat *fsstat)
4379{
4380 struct nfs4_statfs_arg args = {
4381 .fh = fhandle,
4382 .bitmask = server->attr_bitmask,
4383 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004384 struct nfs4_statfs_res res = {
4385 .fsstat = fsstat,
4386 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 struct rpc_message msg = {
4388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4389 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004390 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 };
4392
Trond Myklebust0e574af2005-10-27 22:12:38 -04004393 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004394 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395}
4396
4397static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4398{
4399 struct nfs4_exception exception = { };
4400 int err;
4401 do {
4402 err = nfs4_handle_exception(server,
4403 _nfs4_proc_statfs(server, fhandle, fsstat),
4404 &exception);
4405 } while (exception.retry);
4406 return err;
4407}
4408
4409static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4410 struct nfs_fsinfo *fsinfo)
4411{
4412 struct nfs4_fsinfo_arg args = {
4413 .fh = fhandle,
4414 .bitmask = server->attr_bitmask,
4415 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004416 struct nfs4_fsinfo_res res = {
4417 .fsinfo = fsinfo,
4418 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 struct rpc_message msg = {
4420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4421 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004422 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 };
4424
Bryan Schumaker7c513052011-03-24 17:12:24 +00004425 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
4427
4428static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4429{
4430 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004431 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 int err;
4433
4434 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004435 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004436 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004437 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004438 nfs4_set_lease_period(server->nfs_client,
4439 fsinfo->lease_time * HZ,
4440 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004441 break;
4442 }
4443 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 } while (exception.retry);
4445 return err;
4446}
4447
4448static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4449{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004450 int error;
4451
Trond Myklebust0e574af2005-10-27 22:12:38 -04004452 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004453 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004454 if (error == 0) {
4455 /* block layout checks this! */
4456 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004457 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004458 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004459
4460 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461}
4462
4463static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4464 struct nfs_pathconf *pathconf)
4465{
4466 struct nfs4_pathconf_arg args = {
4467 .fh = fhandle,
4468 .bitmask = server->attr_bitmask,
4469 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004470 struct nfs4_pathconf_res res = {
4471 .pathconf = pathconf,
4472 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 struct rpc_message msg = {
4474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4475 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004476 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 };
4478
4479 /* None of the pathconf attributes are mandatory to implement */
4480 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4481 memset(pathconf, 0, sizeof(*pathconf));
4482 return 0;
4483 }
4484
Trond Myklebust0e574af2005-10-27 22:12:38 -04004485 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004486 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
4489static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4490 struct nfs_pathconf *pathconf)
4491{
4492 struct nfs4_exception exception = { };
4493 int err;
4494
4495 do {
4496 err = nfs4_handle_exception(server,
4497 _nfs4_proc_pathconf(server, fhandle, pathconf),
4498 &exception);
4499 } while (exception.retry);
4500 return err;
4501}
4502
Trond Myklebust5521abf2013-03-16 20:54:34 -04004503int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004504 const struct nfs_open_context *ctx,
4505 const struct nfs_lock_context *l_ctx,
4506 fmode_t fmode)
4507{
4508 const struct nfs_lockowner *lockowner = NULL;
4509
4510 if (l_ctx != NULL)
4511 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004512 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004513}
4514EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4515
Trond Myklebust5521abf2013-03-16 20:54:34 -04004516static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4517 const struct nfs_open_context *ctx,
4518 const struct nfs_lock_context *l_ctx,
4519 fmode_t fmode)
4520{
4521 nfs4_stateid current_stateid;
4522
Trond Myklebuste1253be2014-03-05 08:44:23 -05004523 /* If the current stateid represents a lost lock, then exit */
4524 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4525 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004526 return nfs4_stateid_match(stateid, &current_stateid);
4527}
4528
4529static bool nfs4_error_stateid_expired(int err)
4530{
4531 switch (err) {
4532 case -NFS4ERR_DELEG_REVOKED:
4533 case -NFS4ERR_ADMIN_REVOKED:
4534 case -NFS4ERR_BAD_STATEID:
4535 case -NFS4ERR_STALE_STATEID:
4536 case -NFS4ERR_OLD_STATEID:
4537 case -NFS4ERR_OPENMODE:
4538 case -NFS4ERR_EXPIRED:
4539 return true;
4540 }
4541 return false;
4542}
4543
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004544void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004545{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004546 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004547}
4548
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004549static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004551 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004553 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004554 if (task->tk_status < 0) {
4555 struct nfs4_exception exception = {
4556 .inode = hdr->inode,
4557 .state = hdr->args.context->state,
4558 .stateid = &hdr->args.stateid,
4559 };
4560 task->tk_status = nfs4_async_handle_exception(task,
4561 server, task->tk_status, &exception);
4562 if (exception.retry) {
4563 rpc_restart_call_prepare(task);
4564 return -EAGAIN;
4565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004567
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004568 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004570 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572}
4573
Trond Myklebust5521abf2013-03-16 20:54:34 -04004574static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004575 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004576{
4577
4578 if (!nfs4_error_stateid_expired(task->tk_status) ||
4579 nfs4_stateid_is_current(&args->stateid,
4580 args->context,
4581 args->lock_context,
4582 FMODE_READ))
4583 return false;
4584 rpc_restart_call_prepare(task);
4585 return true;
4586}
4587
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004588static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004589{
4590
4591 dprintk("--> %s\n", __func__);
4592
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004593 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004594 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004595 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004596 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004597 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4598 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004599}
4600
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004601static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4602 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004604 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004605 if (!hdr->pgio_done_cb)
4606 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004607 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004608 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609}
4610
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004611static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4612 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004613{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004614 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4615 &hdr->args.seq_args,
4616 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004617 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004618 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004619 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4620 hdr->args.lock_context,
4621 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004622 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004624 return -EIO;
4625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626}
4627
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004628static int nfs4_write_done_cb(struct rpc_task *task,
4629 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004631 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004632
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004633 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004634 if (task->tk_status < 0) {
4635 struct nfs4_exception exception = {
4636 .inode = hdr->inode,
4637 .state = hdr->args.context->state,
4638 .stateid = &hdr->args.stateid,
4639 };
4640 task->tk_status = nfs4_async_handle_exception(task,
4641 NFS_SERVER(inode), task->tk_status,
4642 &exception);
4643 if (exception.retry) {
4644 rpc_restart_call_prepare(task);
4645 return -EAGAIN;
4646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004648 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004649 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004650 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004651 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
Trond Myklebust5521abf2013-03-16 20:54:34 -04004655static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004656 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004657{
4658
4659 if (!nfs4_error_stateid_expired(task->tk_status) ||
4660 nfs4_stateid_is_current(&args->stateid,
4661 args->context,
4662 args->lock_context,
4663 FMODE_WRITE))
4664 return false;
4665 rpc_restart_call_prepare(task);
4666 return true;
4667}
4668
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004669static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004670{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004671 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004672 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004673 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004674 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4676 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004677}
4678
Trond Myklebust5a37f852012-04-28 14:55:16 -04004679static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004680bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004681{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004682 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004683 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004684 return false;
4685 /* Otherwise, request attributes if and only if we don't hold
4686 * a delegation
4687 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004688 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004689}
Fred Isamana69aef12011-03-03 15:13:47 +00004690
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4692 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004694 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004695
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004696 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4697 hdr->args.bitmask = NULL;
4698 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004699 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004700 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004701
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702 if (!hdr->pgio_done_cb)
4703 hdr->pgio_done_cb = nfs4_write_done_cb;
4704 hdr->res.server = server;
4705 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004707 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004708 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709}
4710
Fred Isaman0b7c0152012-04-20 14:47:39 -04004711static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4712{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004713 nfs4_setup_sequence(NFS_SERVER(data->inode),
4714 &data->args.seq_args,
4715 &data->res.seq_res,
4716 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717}
4718
Fred Isaman0b7c0152012-04-20 14:47:39 -04004719static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004722
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004723 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004724 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4725 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004726 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004727 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730}
4731
Fred Isaman0b7c0152012-04-20 14:47:39 -04004732static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004733{
4734 if (!nfs4_sequence_done(task, &data->res.seq_res))
4735 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004736 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004737}
4738
Fred Isaman0b7c0152012-04-20 14:47:39 -04004739static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004741 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004742
Fred Isaman0b7c0152012-04-20 14:47:39 -04004743 if (data->commit_done_cb == NULL)
4744 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004745 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004746 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004747 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748}
4749
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004750struct nfs4_renewdata {
4751 struct nfs_client *client;
4752 unsigned long timestamp;
4753};
4754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755/*
4756 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4757 * standalone procedure for queueing an asynchronous RENEW.
4758 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004759static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004760{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004761 struct nfs4_renewdata *data = calldata;
4762 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004763
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004764 if (atomic_read(&clp->cl_count) > 1)
4765 nfs4_schedule_state_renewal(clp);
4766 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004767 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004768}
4769
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004770static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004772 struct nfs4_renewdata *data = calldata;
4773 struct nfs_client *clp = data->client;
4774 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004776 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004777 switch (task->tk_status) {
4778 case 0:
4779 break;
4780 case -NFS4ERR_LEASE_MOVED:
4781 nfs4_schedule_lease_moved_recovery(clp);
4782 break;
4783 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004784 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004785 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4786 return;
4787 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004788 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004789 return;
4790 }
4791 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004793 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794}
4795
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004796static const struct rpc_call_ops nfs4_renew_ops = {
4797 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004798 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004799};
4800
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004801static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802{
4803 struct rpc_message msg = {
4804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4805 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004806 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004808 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004810 if (renew_flags == 0)
4811 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004812 if (!atomic_inc_not_zero(&clp->cl_count))
4813 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004814 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004815 if (data == NULL)
4816 return -ENOMEM;
4817 data->client = clp;
4818 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004819 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004820 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004823static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824{
4825 struct rpc_message msg = {
4826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4827 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004828 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 };
4830 unsigned long now = jiffies;
4831 int status;
4832
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004833 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 if (status < 0)
4835 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004836 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 return 0;
4838}
4839
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004840static inline int nfs4_server_supports_acls(struct nfs_server *server)
4841{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004842 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004843}
4844
Trond Myklebust21f498c2012-08-24 10:59:25 -04004845/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4846 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004847 * the stack.
4848 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004849#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004850
Neil Hormane9e3d722011-03-04 19:26:03 -05004851static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004852 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004853{
4854 struct page *newpage, **spages;
4855 int rc = 0;
4856 size_t len;
4857 spages = pages;
4858
4859 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004860 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004861 newpage = alloc_page(GFP_KERNEL);
4862
4863 if (newpage == NULL)
4864 goto unwind;
4865 memcpy(page_address(newpage), buf, len);
4866 buf += len;
4867 buflen -= len;
4868 *pages++ = newpage;
4869 rc++;
4870 } while (buflen != 0);
4871
4872 return rc;
4873
4874unwind:
4875 for(; rc > 0; rc--)
4876 __free_page(spages[rc-1]);
4877 return -ENOMEM;
4878}
4879
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004880struct nfs4_cached_acl {
4881 int cached;
4882 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004883 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004884};
4885
4886static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004887{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004888 struct nfs_inode *nfsi = NFS_I(inode);
4889
4890 spin_lock(&inode->i_lock);
4891 kfree(nfsi->nfs4_acl);
4892 nfsi->nfs4_acl = acl;
4893 spin_unlock(&inode->i_lock);
4894}
4895
4896static void nfs4_zap_acl_attr(struct inode *inode)
4897{
4898 nfs4_set_cached_acl(inode, NULL);
4899}
4900
4901static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4902{
4903 struct nfs_inode *nfsi = NFS_I(inode);
4904 struct nfs4_cached_acl *acl;
4905 int ret = -ENOENT;
4906
4907 spin_lock(&inode->i_lock);
4908 acl = nfsi->nfs4_acl;
4909 if (acl == NULL)
4910 goto out;
4911 if (buf == NULL) /* user is just asking for length */
4912 goto out_len;
4913 if (acl->cached == 0)
4914 goto out;
4915 ret = -ERANGE; /* see getxattr(2) man page */
4916 if (acl->len > buflen)
4917 goto out;
4918 memcpy(buf, acl->data, acl->len);
4919out_len:
4920 ret = acl->len;
4921out:
4922 spin_unlock(&inode->i_lock);
4923 return ret;
4924}
4925
Sachin Prabhu5794d212012-04-17 14:36:40 +01004926static 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 +00004927{
4928 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004929 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004930
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004931 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004932 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004933 if (acl == NULL)
4934 goto out;
4935 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004936 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004937 } else {
4938 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4939 if (acl == NULL)
4940 goto out;
4941 acl->cached = 0;
4942 }
4943 acl->len = acl_len;
4944out:
4945 nfs4_set_cached_acl(inode, acl);
4946}
4947
Andy Adamsonbf118a32011-12-07 11:55:27 -05004948/*
4949 * The getxattr API returns the required buffer length when called with a
4950 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4951 * the required buf. On a NULL buf, we send a page of data to the server
4952 * guessing that the ACL request can be serviced by a page. If so, we cache
4953 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4954 * the cache. If not so, we throw away the page, and cache the required
4955 * length. The next getxattr call will then produce another round trip to
4956 * the server, this time with the input buf of the required size.
4957 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004958static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004959{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004960 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004961 struct nfs_getaclargs args = {
4962 .fh = NFS_FH(inode),
4963 .acl_pages = pages,
4964 .acl_len = buflen,
4965 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004966 struct nfs_getaclres res = {
4967 .acl_len = buflen,
4968 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004969 struct rpc_message msg = {
4970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4971 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004972 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004973 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004974 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4975 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004976
Andy Adamsonbf118a32011-12-07 11:55:27 -05004977 /* As long as we're doing a round trip to the server anyway,
4978 * let's be prepared for a page of acl data. */
4979 if (npages == 0)
4980 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004981 if (npages > ARRAY_SIZE(pages))
4982 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004983
Andy Adamsonbf118a32011-12-07 11:55:27 -05004984 for (i = 0; i < npages; i++) {
4985 pages[i] = alloc_page(GFP_KERNEL);
4986 if (!pages[i])
4987 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004988 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004989
4990 /* for decoding across pages */
4991 res.acl_scratch = alloc_page(GFP_KERNEL);
4992 if (!res.acl_scratch)
4993 goto out_free;
4994
Andy Adamsonbf118a32011-12-07 11:55:27 -05004995 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004996
Peng Taode040be2012-01-10 22:42:47 +08004997 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004998 __func__, buf, buflen, npages, args.acl_len);
4999 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5000 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005001 if (ret)
5002 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005003
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005004 /* Handle the case where the passed-in buffer is too short */
5005 if (res.acl_flags & NFS4_ACL_TRUNC) {
5006 /* Did the user only issue a request for the acl length? */
5007 if (buf == NULL)
5008 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005009 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005010 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005011 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005012 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005013 if (buf) {
5014 if (res.acl_len > buflen) {
5015 ret = -ERANGE;
5016 goto out_free;
5017 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005018 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005019 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005020out_ok:
5021 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005022out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005023 for (i = 0; i < npages; i++)
5024 if (pages[i])
5025 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005026 if (res.acl_scratch)
5027 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005028 return ret;
5029}
5030
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005031static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5032{
5033 struct nfs4_exception exception = { };
5034 ssize_t ret;
5035 do {
5036 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005037 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005038 if (ret >= 0)
5039 break;
5040 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5041 } while (exception.retry);
5042 return ret;
5043}
5044
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005045static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5046{
5047 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005048 int ret;
5049
5050 if (!nfs4_server_supports_acls(server))
5051 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005052 ret = nfs_revalidate_inode(server, inode);
5053 if (ret < 0)
5054 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005055 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5056 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005057 ret = nfs4_read_cached_acl(inode, buf, buflen);
5058 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005059 /* -ENOENT is returned if there is no ACL or if there is an ACL
5060 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005061 return ret;
5062 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005063}
5064
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005065static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005066{
5067 struct nfs_server *server = NFS_SERVER(inode);
5068 struct page *pages[NFS4ACL_MAXPAGES];
5069 struct nfs_setaclargs arg = {
5070 .fh = NFS_FH(inode),
5071 .acl_pages = pages,
5072 .acl_len = buflen,
5073 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005074 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005075 struct rpc_message msg = {
5076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5077 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005078 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005079 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005080 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005081 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005082
5083 if (!nfs4_server_supports_acls(server))
5084 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005085 if (npages > ARRAY_SIZE(pages))
5086 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005087 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005088 if (i < 0)
5089 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005090 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005091 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005092
5093 /*
5094 * Free each page after tx, so the only ref left is
5095 * held by the network stack
5096 */
5097 for (; i > 0; i--)
5098 put_page(pages[i-1]);
5099
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005100 /*
5101 * Acl update can result in inode attribute update.
5102 * so mark the attribute cache invalid.
5103 */
5104 spin_lock(&inode->i_lock);
5105 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5106 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005107 nfs_access_zap_cache(inode);
5108 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005109 return ret;
5110}
5111
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005112static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5113{
5114 struct nfs4_exception exception = { };
5115 int err;
5116 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005117 err = __nfs4_proc_set_acl(inode, buf, buflen);
5118 trace_nfs4_set_acl(inode, err);
5119 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005120 &exception);
5121 } while (exception.retry);
5122 return err;
5123}
5124
David Quigleyaa9c2662013-05-22 12:50:44 -04005125#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5126static int _nfs4_get_security_label(struct inode *inode, void *buf,
5127 size_t buflen)
5128{
5129 struct nfs_server *server = NFS_SERVER(inode);
5130 struct nfs_fattr fattr;
5131 struct nfs4_label label = {0, 0, buflen, buf};
5132
5133 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005134 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005135 .fh = NFS_FH(inode),
5136 .bitmask = bitmask,
5137 };
5138 struct nfs4_getattr_res res = {
5139 .fattr = &fattr,
5140 .label = &label,
5141 .server = server,
5142 };
5143 struct rpc_message msg = {
5144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005145 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005146 .rpc_resp = &res,
5147 };
5148 int ret;
5149
5150 nfs_fattr_init(&fattr);
5151
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005152 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005153 if (ret)
5154 return ret;
5155 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5156 return -ENOENT;
5157 if (buflen < label.len)
5158 return -ERANGE;
5159 return 0;
5160}
5161
5162static int nfs4_get_security_label(struct inode *inode, void *buf,
5163 size_t buflen)
5164{
5165 struct nfs4_exception exception = { };
5166 int err;
5167
5168 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5169 return -EOPNOTSUPP;
5170
5171 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005172 err = _nfs4_get_security_label(inode, buf, buflen);
5173 trace_nfs4_get_security_label(inode, err);
5174 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005175 &exception);
5176 } while (exception.retry);
5177 return err;
5178}
5179
5180static int _nfs4_do_set_security_label(struct inode *inode,
5181 struct nfs4_label *ilabel,
5182 struct nfs_fattr *fattr,
5183 struct nfs4_label *olabel)
5184{
5185
5186 struct iattr sattr = {0};
5187 struct nfs_server *server = NFS_SERVER(inode);
5188 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005189 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005190 .fh = NFS_FH(inode),
5191 .iap = &sattr,
5192 .server = server,
5193 .bitmask = bitmask,
5194 .label = ilabel,
5195 };
5196 struct nfs_setattrres res = {
5197 .fattr = fattr,
5198 .label = olabel,
5199 .server = server,
5200 };
5201 struct rpc_message msg = {
5202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005203 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005204 .rpc_resp = &res,
5205 };
5206 int status;
5207
Jeff Layton12207f62013-11-01 10:49:32 -04005208 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005209
Jeff Layton12207f62013-11-01 10:49:32 -04005210 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005211 if (status)
5212 dprintk("%s failed: %d\n", __func__, status);
5213
5214 return status;
5215}
5216
5217static int nfs4_do_set_security_label(struct inode *inode,
5218 struct nfs4_label *ilabel,
5219 struct nfs_fattr *fattr,
5220 struct nfs4_label *olabel)
5221{
5222 struct nfs4_exception exception = { };
5223 int err;
5224
5225 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005226 err = _nfs4_do_set_security_label(inode, ilabel,
5227 fattr, olabel);
5228 trace_nfs4_set_security_label(inode, err);
5229 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005230 &exception);
5231 } while (exception.retry);
5232 return err;
5233}
5234
5235static int
Al Viro59301222016-05-27 10:19:30 -04005236nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005237{
5238 struct nfs4_label ilabel, *olabel = NULL;
5239 struct nfs_fattr fattr;
5240 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005241 int status;
5242
5243 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5244 return -EOPNOTSUPP;
5245
5246 nfs_fattr_init(&fattr);
5247
5248 ilabel.pi = 0;
5249 ilabel.lfs = 0;
5250 ilabel.label = (char *)buf;
5251 ilabel.len = buflen;
5252
5253 cred = rpc_lookup_cred();
5254 if (IS_ERR(cred))
5255 return PTR_ERR(cred);
5256
5257 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5258 if (IS_ERR(olabel)) {
5259 status = -PTR_ERR(olabel);
5260 goto out;
5261 }
5262
5263 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5264 if (status == 0)
5265 nfs_setsecurity(inode, &fattr, olabel);
5266
5267 nfs4_label_free(olabel);
5268out:
5269 put_rpccred(cred);
5270 return status;
5271}
5272#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5273
5274
Chuck Leverf0920752012-05-21 22:45:41 -04005275static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5276 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005277{
5278 __be32 verf[2];
5279
Chuck Lever2c820d92012-05-21 22:45:33 -04005280 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5281 /* An impossible timestamp guarantees this value
5282 * will never match a generated boot time. */
5283 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005284 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005285 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005286 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005287 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5288 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005289 }
Chuck Levercd937102012-03-02 17:14:31 -05005290 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5291}
5292
Jeff Laytona3192682015-06-09 19:43:59 -04005293static int
5294nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005295{
Jeff Laytona3192682015-06-09 19:43:59 -04005296 size_t len;
5297 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005298
Trond Myklebustceb3a162015-01-03 15:16:04 -05005299 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005300 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005301
Jeff Laytona3192682015-06-09 19:43:59 -04005302 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005303 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005304 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5305 1 +
5306 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5307 1;
5308 rcu_read_unlock();
5309
5310 if (len > NFS4_OPAQUE_LIMIT + 1)
5311 return -EINVAL;
5312
5313 /*
5314 * Since this string is allocated at mount time, and held until the
5315 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5316 * about a memory-reclaim deadlock.
5317 */
5318 str = kmalloc(len, GFP_KERNEL);
5319 if (!str)
5320 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005321
Chuck Levere984a552012-09-14 17:24:21 -04005322 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005323 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005324 clp->cl_ipaddr,
5325 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5326 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005327 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005328
Jeff Laytona3192682015-06-09 19:43:59 -04005329 clp->cl_owner_id = str;
5330 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005331}
5332
Jeff Layton873e3852015-06-09 19:44:00 -04005333static int
5334nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005335{
Jeff Layton873e3852015-06-09 19:44:00 -04005336 size_t len;
5337 char *str;
5338
5339 len = 10 + 10 + 1 + 10 + 1 +
5340 strlen(nfs4_client_id_uniquifier) + 1 +
5341 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5342
5343 if (len > NFS4_OPAQUE_LIMIT + 1)
5344 return -EINVAL;
5345
5346 /*
5347 * Since this string is allocated at mount time, and held until the
5348 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5349 * about a memory-reclaim deadlock.
5350 */
5351 str = kmalloc(len, GFP_KERNEL);
5352 if (!str)
5353 return -ENOMEM;
5354
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005355 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005356 clp->rpc_ops->version, clp->cl_minorversion,
5357 nfs4_client_id_uniquifier,
5358 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005359 clp->cl_owner_id = str;
5360 return 0;
5361}
5362
5363static int
5364nfs4_init_uniform_client_string(struct nfs_client *clp)
5365{
Jeff Layton873e3852015-06-09 19:44:00 -04005366 size_t len;
5367 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005368
5369 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005370 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005371
5372 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005373 return nfs4_init_uniquifier_client_string(clp);
5374
5375 len = 10 + 10 + 1 + 10 + 1 +
5376 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5377
5378 if (len > NFS4_OPAQUE_LIMIT + 1)
5379 return -EINVAL;
5380
5381 /*
5382 * Since this string is allocated at mount time, and held until the
5383 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5384 * about a memory-reclaim deadlock.
5385 */
5386 str = kmalloc(len, GFP_KERNEL);
5387 if (!str)
5388 return -ENOMEM;
5389
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005390 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005391 clp->rpc_ops->version, clp->cl_minorversion,
5392 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005393 clp->cl_owner_id = str;
5394 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005395}
5396
Chuck Lever706cb8d2014-03-12 12:51:47 -04005397/*
5398 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5399 * services. Advertise one based on the address family of the
5400 * clientaddr.
5401 */
5402static unsigned int
5403nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5404{
5405 if (strchr(clp->cl_ipaddr, ':') != NULL)
5406 return scnprintf(buf, len, "tcp6");
5407 else
5408 return scnprintf(buf, len, "tcp");
5409}
5410
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005411static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5412{
5413 struct nfs4_setclientid *sc = calldata;
5414
5415 if (task->tk_status == 0)
5416 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5417}
5418
5419static const struct rpc_call_ops nfs4_setclientid_ops = {
5420 .rpc_call_done = nfs4_setclientid_done,
5421};
5422
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005423/**
5424 * nfs4_proc_setclientid - Negotiate client ID
5425 * @clp: state data structure
5426 * @program: RPC program for NFSv4 callback service
5427 * @port: IP port number for NFS4 callback service
5428 * @cred: RPC credential to use for this call
5429 * @res: where to place the result
5430 *
5431 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5432 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005433int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5434 unsigned short port, struct rpc_cred *cred,
5435 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
5437 nfs4_verifier sc_verifier;
5438 struct nfs4_setclientid setclientid = {
5439 .sc_verifier = &sc_verifier,
5440 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005441 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 };
5443 struct rpc_message msg = {
5444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5445 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005446 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005447 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005449 struct rpc_task *task;
5450 struct rpc_task_setup task_setup_data = {
5451 .rpc_client = clp->cl_rpcclient,
5452 .rpc_message = &msg,
5453 .callback_ops = &nfs4_setclientid_ops,
5454 .callback_data = &setclientid,
5455 .flags = RPC_TASK_TIMEOUT,
5456 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005457 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Chuck Leverde734832012-07-11 16:30:50 -04005459 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005460 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005461
5462 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5463 status = nfs4_init_uniform_client_string(clp);
5464 else
Jeff Laytona3192682015-06-09 19:43:59 -04005465 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005466
5467 if (status)
5468 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005469
Chuck Leverde734832012-07-11 16:30:50 -04005470 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005471 setclientid.sc_netid_len =
5472 nfs4_init_callback_netid(clp,
5473 setclientid.sc_netid,
5474 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005475 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005476 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 clp->cl_ipaddr, port >> 8, port & 255);
5478
Jeff Layton3a6bb732015-06-09 19:43:57 -04005479 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005480 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005481 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005482 task = rpc_run_task(&task_setup_data);
5483 if (IS_ERR(task)) {
5484 status = PTR_ERR(task);
5485 goto out;
5486 }
5487 status = task->tk_status;
5488 if (setclientid.sc_cred) {
5489 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5490 put_rpccred(setclientid.sc_cred);
5491 }
5492 rpc_put_task(task);
5493out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005494 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005495 dprintk("NFS reply setclientid: %d\n", status);
5496 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497}
5498
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005499/**
5500 * nfs4_proc_setclientid_confirm - Confirm client ID
5501 * @clp: state data structure
5502 * @res: result of a previous SETCLIENTID
5503 * @cred: RPC credential to use for this call
5504 *
5505 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5506 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005507int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005508 struct nfs4_setclientid_res *arg,
5509 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 struct rpc_message msg = {
5512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005513 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005514 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 int status;
5517
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005518 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5519 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5520 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005521 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005522 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005523 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 return status;
5525}
5526
Trond Myklebustfe650402006-01-03 09:55:18 +01005527struct nfs4_delegreturndata {
5528 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005529 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005530 struct nfs_fh fh;
5531 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005532 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005533 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005534 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005535 struct inode *inode;
5536 bool roc;
5537 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005538};
5539
Trond Myklebustfe650402006-01-03 09:55:18 +01005540static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5541{
5542 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005543
Trond Myklebust14516c32010-07-31 14:29:06 -04005544 if (!nfs4_sequence_done(task, &data->res.seq_res))
5545 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005546
Trond Myklebustca8acf82013-08-13 10:36:56 -04005547 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005548 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005549 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005550 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005551 case -NFS4ERR_ADMIN_REVOKED:
5552 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005553 case -NFS4ERR_EXPIRED:
5554 nfs4_free_revoked_stateid(data->res.server,
5555 data->args.stateid,
5556 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005557 case -NFS4ERR_BAD_STATEID:
5558 case -NFS4ERR_OLD_STATEID:
5559 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005560 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005561 if (data->roc)
5562 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005563 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005564 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005565 if (nfs4_async_handle_error(task, data->res.server,
5566 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005567 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005568 return;
5569 }
5570 }
5571 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005572}
5573
5574static void nfs4_delegreturn_release(void *calldata)
5575{
Peng Tao039b7562014-07-03 13:05:02 +08005576 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005577 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005578
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005579 if (inode) {
5580 if (data->roc)
5581 pnfs_roc_release(inode);
5582 nfs_iput_and_deactive(inode);
5583 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005584 kfree(calldata);
5585}
5586
Andy Adamson938e1012009-04-01 09:22:28 -04005587static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5588{
5589 struct nfs4_delegreturndata *d_data;
5590
5591 d_data = (struct nfs4_delegreturndata *)data;
5592
Peng Tao500d7012015-09-22 11:35:22 +08005593 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5594 return;
5595
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005596 if (d_data->roc)
5597 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005598
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005599 nfs4_setup_sequence(d_data->res.server,
5600 &d_data->args.seq_args,
5601 &d_data->res.seq_res,
5602 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005603}
Andy Adamson938e1012009-04-01 09:22:28 -04005604
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005605static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005606 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005607 .rpc_call_done = nfs4_delegreturn_done,
5608 .rpc_release = nfs4_delegreturn_release,
5609};
5610
Trond Myklebuste6f81072008-01-24 18:14:34 -05005611static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005612{
5613 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005614 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005615 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005616 struct rpc_message msg = {
5617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5618 .rpc_cred = cred,
5619 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005620 struct rpc_task_setup task_setup_data = {
5621 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005622 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005623 .callback_ops = &nfs4_delegreturn_ops,
5624 .flags = RPC_TASK_ASYNC,
5625 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005626 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005627
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005628 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005629 if (data == NULL)
5630 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005631 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005632
5633 nfs4_state_protect(server->nfs_client,
5634 NFS_SP4_MACH_CRED_CLEANUP,
5635 &task_setup_data.rpc_client, &msg);
5636
Trond Myklebustfe650402006-01-03 09:55:18 +01005637 data->args.fhandle = &data->fh;
5638 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005639 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005640 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005641 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005642 data->res.fattr = &data->fattr;
5643 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005644 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005645 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005646 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005647 data->inode = nfs_igrab_and_active(inode);
5648 if (data->inode)
5649 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005650
Trond Myklebustc970aa82007-07-14 15:39:59 -04005651 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005652 msg.rpc_argp = &data->args;
5653 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005654 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005655 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005656 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005657 if (!issync)
5658 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005659 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005660 if (status != 0)
5661 goto out;
5662 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005663 if (status == 0)
5664 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5665 else
5666 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005667out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005668 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005669 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670}
5671
Trond Myklebuste6f81072008-01-24 18:14:34 -05005672int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673{
5674 struct nfs_server *server = NFS_SERVER(inode);
5675 struct nfs4_exception exception = { };
5676 int err;
5677 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005678 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005679 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 switch (err) {
5681 case -NFS4ERR_STALE_STATEID:
5682 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 case 0:
5684 return 0;
5685 }
5686 err = nfs4_handle_exception(server, err, &exception);
5687 } while (exception.retry);
5688 return err;
5689}
5690
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5692{
5693 struct inode *inode = state->inode;
5694 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005695 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005696 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005698 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005700 struct nfs_lockt_res res = {
5701 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 };
5703 struct rpc_message msg = {
5704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5705 .rpc_argp = &arg,
5706 .rpc_resp = &res,
5707 .rpc_cred = state->owner->so_cred,
5708 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 struct nfs4_lock_state *lsp;
5710 int status;
5711
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005712 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005713 status = nfs4_set_lock_state(state, request);
5714 if (status != 0)
5715 goto out;
5716 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005717 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005718 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005719 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005720 switch (status) {
5721 case 0:
5722 request->fl_type = F_UNLCK;
5723 break;
5724 case -NFS4ERR_DENIED:
5725 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005727 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005728 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005729out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 return status;
5731}
5732
5733static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5734{
5735 struct nfs4_exception exception = { };
5736 int err;
5737
5738 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005739 err = _nfs4_proc_getlk(state, cmd, request);
5740 trace_nfs4_get_lock(request, state, cmd, err);
5741 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 &exception);
5743 } while (exception.retry);
5744 return err;
5745}
5746
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005747struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005748 struct nfs_locku_args arg;
5749 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005750 struct nfs4_lock_state *lsp;
5751 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005752 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005753 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005754 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005755};
5756
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005757static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5758 struct nfs_open_context *ctx,
5759 struct nfs4_lock_state *lsp,
5760 struct nfs_seqid *seqid)
5761{
5762 struct nfs4_unlockdata *p;
5763 struct inode *inode = lsp->ls_state->inode;
5764
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005765 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005766 if (p == NULL)
5767 return NULL;
5768 p->arg.fh = NFS_FH(inode);
5769 p->arg.fl = &p->fl;
5770 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005771 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005772 p->lsp = lsp;
5773 atomic_inc(&lsp->ls_count);
5774 /* Ensure we don't close file until we're done freeing locks! */
5775 p->ctx = get_nfs_open_context(ctx);
5776 memcpy(&p->fl, fl, sizeof(p->fl));
5777 p->server = NFS_SERVER(inode);
5778 return p;
5779}
5780
Trond Myklebust06f814a2006-01-03 09:55:07 +01005781static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005782{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005783 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005784 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005785 nfs4_put_lock_state(calldata->lsp);
5786 put_nfs_open_context(calldata->ctx);
5787 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005788}
5789
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005790static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005791{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005792 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005793
Trond Myklebust14516c32010-07-31 14:29:06 -04005794 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5795 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005796 switch (task->tk_status) {
5797 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005798 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005799 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005800 if (nfs4_update_lock_stateid(calldata->lsp,
5801 &calldata->res.stateid))
5802 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005803 case -NFS4ERR_ADMIN_REVOKED:
5804 case -NFS4ERR_EXPIRED:
5805 nfs4_free_revoked_stateid(calldata->server,
5806 &calldata->arg.stateid,
5807 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005808 case -NFS4ERR_BAD_STATEID:
5809 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005810 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005811 if (!nfs4_stateid_match(&calldata->arg.stateid,
5812 &calldata->lsp->ls_stateid))
5813 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005814 break;
5815 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005816 if (nfs4_async_handle_error(task, calldata->server,
5817 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005818 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005819 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005820 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005821}
5822
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005823static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005824{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005825 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005827 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005828 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005829 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005830 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005831 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005832 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005833 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005834 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005835 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005836 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005837 &calldata->res.seq_res,
5838 task) != 0)
5839 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005840 return;
5841out_no_action:
5842 task->tk_action = NULL;
5843out_wait:
5844 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845}
5846
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005847static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005848 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005849 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005850 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005851};
5852
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005853static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5854 struct nfs_open_context *ctx,
5855 struct nfs4_lock_state *lsp,
5856 struct nfs_seqid *seqid)
5857{
5858 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005859 struct rpc_message msg = {
5860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5861 .rpc_cred = ctx->cred,
5862 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005863 struct rpc_task_setup task_setup_data = {
5864 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005865 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005866 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005867 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005868 .flags = RPC_TASK_ASYNC,
5869 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005870
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005871 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5872 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5873
Frank Filz137d6ac2007-07-09 15:32:29 -07005874 /* Ensure this is an unlock - when canceling a lock, the
5875 * canceled lock is passed in, and it won't be an unlock.
5876 */
5877 fl->fl_type = F_UNLCK;
5878
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005879 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5880 if (data == NULL) {
5881 nfs_free_seqid(seqid);
5882 return ERR_PTR(-ENOMEM);
5883 }
5884
Chuck Levera9c92d62013-08-09 12:48:18 -04005885 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005886 msg.rpc_argp = &data->arg;
5887 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005888 task_setup_data.callback_data = data;
5889 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005890}
5891
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5893{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005894 struct inode *inode = state->inode;
5895 struct nfs4_state_owner *sp = state->owner;
5896 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005897 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005898 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005899 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005900 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005901 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005902 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
Trond Myklebust9b073572006-06-29 16:38:34 -04005904 status = nfs4_set_lock_state(state, request);
5905 /* Unlock _before_ we do the RPC call */
5906 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005907 /* Exclude nfs_delegation_claim_locks() */
5908 mutex_lock(&sp->so_delegreturn_mutex);
5909 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005910 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005911 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005912 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005913 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005914 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005915 }
5916 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005917 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005918 if (status != 0)
5919 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005920 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005921 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005922 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5923 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005924 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5925 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005926 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005927 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005928 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005929 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005930 status = PTR_ERR(task);
5931 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005932 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005933 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005934 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005935out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005936 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005937 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005938 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939}
5940
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005941struct nfs4_lockdata {
5942 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005943 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005944 struct nfs4_lock_state *lsp;
5945 struct nfs_open_context *ctx;
5946 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005947 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005948 int rpc_status;
5949 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005950 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005951};
5952
5953static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005954 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5955 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005956{
5957 struct nfs4_lockdata *p;
5958 struct inode *inode = lsp->ls_state->inode;
5959 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005960 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005961
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005962 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005963 if (p == NULL)
5964 return NULL;
5965
5966 p->arg.fh = NFS_FH(inode);
5967 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005968 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005969 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005970 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005971 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5972 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005973 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005974 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005975 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005976 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005977 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005978 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005979 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005980 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005981 atomic_inc(&lsp->ls_count);
5982 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005983 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005984 memcpy(&p->fl, fl, sizeof(p->fl));
5985 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005986out_free_seqid:
5987 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005988out_free:
5989 kfree(p);
5990 return NULL;
5991}
5992
5993static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5994{
5995 struct nfs4_lockdata *data = calldata;
5996 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
Harvey Harrison3110ff82008-05-02 13:42:44 -07005998 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005999 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006000 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006001 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006002 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006003 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006004 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006005 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006006 nfs4_stateid_copy(&data->arg.open_stateid,
6007 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006008 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006009 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006010 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006011 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006012 nfs4_stateid_copy(&data->arg.lock_stateid,
6013 &data->lsp->ls_stateid);
6014 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006015 if (!nfs4_valid_open_stateid(state)) {
6016 data->rpc_status = -EBADF;
6017 task->tk_action = NULL;
6018 goto out_release_open_seqid;
6019 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006020 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006021 if (nfs4_setup_sequence(data->server,
6022 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006023 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006024 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006025 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006026out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006027 nfs_release_seqid(data->arg.open_seqid);
6028out_release_lock_seqid:
6029 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006030out_wait:
6031 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006032 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006033}
6034
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006035static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6036{
6037 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006038 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006039
Harvey Harrison3110ff82008-05-02 13:42:44 -07006040 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006041
Trond Myklebust14516c32010-07-31 14:29:06 -04006042 if (!nfs4_sequence_done(task, &data->res.seq_res))
6043 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006044
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006045 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006046 switch (task->tk_status) {
6047 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006048 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006049 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006050 if (data->arg.new_lock) {
6051 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006052 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006053 rpc_restart_call_prepare(task);
6054 break;
6055 }
6056 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006057 if (data->arg.new_lock_owner != 0) {
6058 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6059 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6060 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6061 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6062 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006063 break;
6064 case -NFS4ERR_BAD_STATEID:
6065 case -NFS4ERR_OLD_STATEID:
6066 case -NFS4ERR_STALE_STATEID:
6067 case -NFS4ERR_EXPIRED:
6068 if (data->arg.new_lock_owner != 0) {
6069 if (!nfs4_stateid_match(&data->arg.open_stateid,
6070 &lsp->ls_state->open_stateid))
6071 rpc_restart_call_prepare(task);
6072 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6073 &lsp->ls_stateid))
6074 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006075 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006076 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006077}
6078
6079static void nfs4_lock_release(void *calldata)
6080{
6081 struct nfs4_lockdata *data = calldata;
6082
Harvey Harrison3110ff82008-05-02 13:42:44 -07006083 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006084 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006085 if (data->cancelled != 0) {
6086 struct rpc_task *task;
6087 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6088 data->arg.lock_seqid);
6089 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006090 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006091 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006092 } else
6093 nfs_free_seqid(data->arg.lock_seqid);
6094 nfs4_put_lock_state(data->lsp);
6095 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006096 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006097 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006098 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006099}
6100
6101static const struct rpc_call_ops nfs4_lock_ops = {
6102 .rpc_call_prepare = nfs4_lock_prepare,
6103 .rpc_call_done = nfs4_lock_done,
6104 .rpc_release = nfs4_lock_release,
6105};
6106
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006107static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6108{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006109 switch (error) {
6110 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006111 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006112 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006113 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006114 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006115 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006116 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006117 break;
6118 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006119 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006120 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006121 };
6122}
6123
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006124static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006125{
6126 struct nfs4_lockdata *data;
6127 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006128 struct rpc_message msg = {
6129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6130 .rpc_cred = state->owner->so_cred,
6131 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006132 struct rpc_task_setup task_setup_data = {
6133 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006134 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006135 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006136 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006137 .flags = RPC_TASK_ASYNC,
6138 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006139 int ret;
6140
Harvey Harrison3110ff82008-05-02 13:42:44 -07006141 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006142 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006143 fl->fl_u.nfs4_fl.owner,
6144 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006145 if (data == NULL)
6146 return -ENOMEM;
6147 if (IS_SETLKW(cmd))
6148 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006149 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006150 msg.rpc_argp = &data->arg;
6151 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006152 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006153 if (recovery_type > NFS_LOCK_NEW) {
6154 if (recovery_type == NFS_LOCK_RECLAIM)
6155 data->arg.reclaim = NFS_LOCK_RECLAIM;
6156 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006157 } else
6158 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006159 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006160 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006161 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006162 ret = nfs4_wait_for_completion_rpc_task(task);
6163 if (ret == 0) {
6164 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006165 if (ret)
6166 nfs4_handle_setlk_error(data->server, data->lsp,
6167 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006168 } else
6169 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006170 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006171 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006172 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006173 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174}
6175
6176static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6177{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006178 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006179 struct nfs4_exception exception = {
6180 .inode = state->inode,
6181 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006182 int err;
6183
6184 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006185 /* Cache the lock if possible... */
6186 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6187 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006188 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006189 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006190 break;
6191 nfs4_handle_exception(server, err, &exception);
6192 } while (exception.retry);
6193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194}
6195
6196static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6197{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006198 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006199 struct nfs4_exception exception = {
6200 .inode = state->inode,
6201 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006202 int err;
6203
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006204 err = nfs4_set_lock_state(state, request);
6205 if (err != 0)
6206 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006207 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006208 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6209 return 0;
6210 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006211 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006212 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6213 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006214 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006215 switch (err) {
6216 default:
6217 goto out;
6218 case -NFS4ERR_GRACE:
6219 case -NFS4ERR_DELAY:
6220 nfs4_handle_exception(server, err, &exception);
6221 err = 0;
6222 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006223 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006224out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226}
6227
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006228#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006229static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6230{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006231 struct nfs4_lock_state *lsp;
6232 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006233
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006234 status = nfs4_set_lock_state(state, request);
6235 if (status != 0)
6236 return status;
6237 lsp = request->fl_u.nfs4_fl.owner;
6238 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6239 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6240 return 0;
6241 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006242 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006243}
6244#endif
6245
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6247{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006248 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006249 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006250 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006251 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252
Trond Myklebust01c3b862006-06-29 16:38:39 -04006253 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006254 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006255 if (status < 0)
6256 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006257 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006258 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006259 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006260 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006261 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006262 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006263 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006264 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006265 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006266 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006267 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006268 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006269 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006270 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006271out:
6272 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 return status;
6274}
6275
6276static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6277{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006278 struct nfs4_exception exception = {
6279 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006280 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006281 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 int err;
6283
6284 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006285 err = _nfs4_proc_setlk(state, cmd, request);
6286 if (err == -NFS4ERR_DENIED)
6287 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006289 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 } while (exception.retry);
6291 return err;
6292}
6293
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006294#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6295#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6296
6297static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006298nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6299 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006300{
6301 int status = -ERESTARTSYS;
6302 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6303
6304 while(!signalled()) {
6305 status = nfs4_proc_setlk(state, cmd, request);
6306 if ((status != -EAGAIN) || IS_SETLK(cmd))
6307 break;
6308 freezable_schedule_timeout_interruptible(timeout);
6309 timeout *= 2;
6310 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6311 status = -ERESTARTSYS;
6312 }
6313 return status;
6314}
6315
Jeff Laytona1d617d82016-09-17 18:17:39 -04006316#ifdef CONFIG_NFS_V4_1
6317struct nfs4_lock_waiter {
6318 struct task_struct *task;
6319 struct inode *inode;
6320 struct nfs_lowner *owner;
6321 bool notified;
6322};
6323
6324static int
6325nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6326{
6327 int ret;
6328 struct cb_notify_lock_args *cbnl = key;
6329 struct nfs4_lock_waiter *waiter = wait->private;
6330 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6331 *wowner = waiter->owner;
6332
6333 /* Only wake if the callback was for the same owner */
6334 if (lowner->clientid != wowner->clientid ||
6335 lowner->id != wowner->id ||
6336 lowner->s_dev != wowner->s_dev)
6337 return 0;
6338
6339 /* Make sure it's for the right inode */
6340 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6341 return 0;
6342
6343 waiter->notified = true;
6344
6345 /* override "private" so we can use default_wake_function */
6346 wait->private = waiter->task;
6347 ret = autoremove_wake_function(wait, mode, flags, key);
6348 wait->private = waiter;
6349 return ret;
6350}
6351
6352static int
6353nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6354{
6355 int status = -ERESTARTSYS;
6356 unsigned long flags;
6357 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6358 struct nfs_server *server = NFS_SERVER(state->inode);
6359 struct nfs_client *clp = server->nfs_client;
6360 wait_queue_head_t *q = &clp->cl_lock_waitq;
6361 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6362 .id = lsp->ls_seqid.owner_id,
6363 .s_dev = server->s_dev };
6364 struct nfs4_lock_waiter waiter = { .task = current,
6365 .inode = state->inode,
6366 .owner = &owner,
6367 .notified = false };
6368 wait_queue_t wait;
6369
6370 /* Don't bother with waitqueue if we don't expect a callback */
6371 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6372 return nfs4_retry_setlk_simple(state, cmd, request);
6373
6374 init_wait(&wait);
6375 wait.private = &waiter;
6376 wait.func = nfs4_wake_lock_waiter;
6377 add_wait_queue(q, &wait);
6378
6379 while(!signalled()) {
6380 status = nfs4_proc_setlk(state, cmd, request);
6381 if ((status != -EAGAIN) || IS_SETLK(cmd))
6382 break;
6383
6384 status = -ERESTARTSYS;
6385 spin_lock_irqsave(&q->lock, flags);
6386 if (waiter.notified) {
6387 spin_unlock_irqrestore(&q->lock, flags);
6388 continue;
6389 }
6390 set_current_state(TASK_INTERRUPTIBLE);
6391 spin_unlock_irqrestore(&q->lock, flags);
6392
6393 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6394 }
6395
6396 finish_wait(q, &wait);
6397 return status;
6398}
6399#else /* !CONFIG_NFS_V4_1 */
6400static inline int
6401nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6402{
6403 return nfs4_retry_setlk_simple(state, cmd, request);
6404}
6405#endif
6406
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407static int
6408nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6409{
6410 struct nfs_open_context *ctx;
6411 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 int status;
6413
6414 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006415 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 state = ctx->state;
6417
6418 if (request->fl_start < 0 || request->fl_end < 0)
6419 return -EINVAL;
6420
Trond Myklebustd9531262009-07-21 19:22:38 -04006421 if (IS_GETLK(cmd)) {
6422 if (state != NULL)
6423 return nfs4_proc_getlk(state, F_GETLK, request);
6424 return 0;
6425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
6427 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6428 return -EINVAL;
6429
Trond Myklebustd9531262009-07-21 19:22:38 -04006430 if (request->fl_type == F_UNLCK) {
6431 if (state != NULL)
6432 return nfs4_proc_unlck(state, cmd, request);
6433 return 0;
6434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435
Trond Myklebustd9531262009-07-21 19:22:38 -04006436 if (state == NULL)
6437 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006438
6439 if ((request->fl_flags & FL_POSIX) &&
6440 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6441 return -ENOLCK;
6442
Trond Myklebust55725512012-04-18 12:48:35 -04006443 /*
6444 * Don't rely on the VFS having checked the file open mode,
6445 * since it won't do this for flock() locks.
6446 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006447 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006448 case F_RDLCK:
6449 if (!(filp->f_mode & FMODE_READ))
6450 return -EBADF;
6451 break;
6452 case F_WRLCK:
6453 if (!(filp->f_mode & FMODE_WRITE))
6454 return -EBADF;
6455 }
6456
Jeff Layton1ea67db2016-09-17 18:17:37 -04006457 status = nfs4_set_lock_state(state, request);
6458 if (status != 0)
6459 return status;
6460
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006461 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462}
6463
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006464int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006465{
6466 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006467 int err;
6468
6469 err = nfs4_set_lock_state(state, fl);
6470 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006471 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006472 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006473 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006474}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006475
Trond Myklebustcf470c32012-03-07 13:49:12 -05006476struct nfs_release_lockowner_data {
6477 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006478 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006479 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006480 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006481 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006482};
6483
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006484static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6485{
6486 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006487 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006488 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6489 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006490 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006491 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006492}
6493
6494static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6495{
6496 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006497 struct nfs_server *server = data->server;
6498
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006499 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006500
6501 switch (task->tk_status) {
6502 case 0:
6503 renew_lease(server, data->timestamp);
6504 break;
6505 case -NFS4ERR_STALE_CLIENTID:
6506 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006507 nfs4_schedule_lease_recovery(server->nfs_client);
6508 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006509 case -NFS4ERR_LEASE_MOVED:
6510 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006511 if (nfs4_async_handle_error(task, server,
6512 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006513 rpc_restart_call_prepare(task);
6514 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006515}
6516
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006517static void nfs4_release_lockowner_release(void *calldata)
6518{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006519 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006520 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006521 kfree(calldata);
6522}
6523
Trond Myklebust17280172012-03-11 13:11:00 -04006524static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006525 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6526 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006527 .rpc_release = nfs4_release_lockowner_release,
6528};
6529
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006530static void
6531nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006532{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006533 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006534 struct rpc_message msg = {
6535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6536 };
6537
6538 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006539 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006540
Trond Myklebustcf470c32012-03-07 13:49:12 -05006541 data = kmalloc(sizeof(*data), GFP_NOFS);
6542 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006543 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006544 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006545 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006546 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6547 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6548 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006549
Trond Myklebustcf470c32012-03-07 13:49:12 -05006550 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006551 msg.rpc_resp = &data->res;
6552 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006553 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006554}
6555
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006556#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6557
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006558static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006559 struct dentry *unused, struct inode *inode,
6560 const char *key, const void *buf,
6561 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006562{
Al Viro59301222016-05-27 10:19:30 -04006563 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006564}
6565
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006566static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006567 struct dentry *unused, struct inode *inode,
6568 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006569{
Al Virob2968212016-04-10 20:48:24 -04006570 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006571}
6572
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006573static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006574{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006575 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006576}
6577
David Quigleyc9bccef2013-05-22 12:50:45 -04006578#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006579
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006580static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006581 struct dentry *unused, struct inode *inode,
6582 const char *key, const void *buf,
6583 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006584{
6585 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006586 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006587
6588 return -EOPNOTSUPP;
6589}
6590
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006591static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006592 struct dentry *unused, struct inode *inode,
6593 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006594{
6595 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006596 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006597 return -EOPNOTSUPP;
6598}
6599
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006600static ssize_t
6601nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006602{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006603 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006604
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006605 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6606 len = security_inode_listsecurity(inode, list, list_len);
6607 if (list_len && len > list_len)
6608 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006609 }
6610 return len;
6611}
6612
6613static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6614 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006615 .get = nfs4_xattr_get_nfs4_label,
6616 .set = nfs4_xattr_set_nfs4_label,
6617};
David Quigleyc9bccef2013-05-22 12:50:45 -04006618
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006619#else
6620
6621static ssize_t
6622nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6623{
6624 return 0;
6625}
6626
6627#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006628
Andy Adamson533eb462011-06-13 18:25:56 -04006629/*
6630 * nfs_fhget will use either the mounted_on_fileid or the fileid
6631 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006632static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6633{
Andy Adamson533eb462011-06-13 18:25:56 -04006634 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6635 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6636 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006637 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006638 return;
6639
6640 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006641 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006642 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6643 fattr->nlink = 2;
6644}
6645
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006646static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6647 const struct qstr *name,
6648 struct nfs4_fs_locations *fs_locations,
6649 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006650{
6651 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006652 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006653 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006654 };
6655 struct nfs4_fs_locations_arg args = {
6656 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006657 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006658 .page = page,
6659 .bitmask = bitmask,
6660 };
Benny Halevy22958462009-04-01 09:22:02 -04006661 struct nfs4_fs_locations_res res = {
6662 .fs_locations = fs_locations,
6663 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006664 struct rpc_message msg = {
6665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6666 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006667 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006668 };
6669 int status;
6670
Harvey Harrison3110ff82008-05-02 13:42:44 -07006671 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006672
6673 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6674 * is not supported */
6675 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6676 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6677 else
6678 bitmask[0] |= FATTR4_WORD0_FILEID;
6679
Trond Myklebustc228fd32007-01-13 02:28:11 -05006680 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006681 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006682 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006683 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006684 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006685 return status;
6686}
6687
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006688int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6689 const struct qstr *name,
6690 struct nfs4_fs_locations *fs_locations,
6691 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006692{
6693 struct nfs4_exception exception = { };
6694 int err;
6695 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006696 err = _nfs4_proc_fs_locations(client, dir, name,
6697 fs_locations, page);
6698 trace_nfs4_get_fs_locations(dir, name, err);
6699 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006700 &exception);
6701 } while (exception.retry);
6702 return err;
6703}
6704
Chuck Leverb03d7352013-10-17 14:12:50 -04006705/*
6706 * This operation also signals the server that this client is
6707 * performing migration recovery. The server can stop returning
6708 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6709 * appended to this compound to identify the client ID which is
6710 * performing recovery.
6711 */
6712static int _nfs40_proc_get_locations(struct inode *inode,
6713 struct nfs4_fs_locations *locations,
6714 struct page *page, struct rpc_cred *cred)
6715{
6716 struct nfs_server *server = NFS_SERVER(inode);
6717 struct rpc_clnt *clnt = server->client;
6718 u32 bitmask[2] = {
6719 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6720 };
6721 struct nfs4_fs_locations_arg args = {
6722 .clientid = server->nfs_client->cl_clientid,
6723 .fh = NFS_FH(inode),
6724 .page = page,
6725 .bitmask = bitmask,
6726 .migration = 1, /* skip LOOKUP */
6727 .renew = 1, /* append RENEW */
6728 };
6729 struct nfs4_fs_locations_res res = {
6730 .fs_locations = locations,
6731 .migration = 1,
6732 .renew = 1,
6733 };
6734 struct rpc_message msg = {
6735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6736 .rpc_argp = &args,
6737 .rpc_resp = &res,
6738 .rpc_cred = cred,
6739 };
6740 unsigned long now = jiffies;
6741 int status;
6742
6743 nfs_fattr_init(&locations->fattr);
6744 locations->server = server;
6745 locations->nlocations = 0;
6746
6747 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6748 nfs4_set_sequence_privileged(&args.seq_args);
6749 status = nfs4_call_sync_sequence(clnt, server, &msg,
6750 &args.seq_args, &res.seq_res);
6751 if (status)
6752 return status;
6753
6754 renew_lease(server, now);
6755 return 0;
6756}
6757
6758#ifdef CONFIG_NFS_V4_1
6759
6760/*
6761 * This operation also signals the server that this client is
6762 * performing migration recovery. The server can stop asserting
6763 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6764 * performing this operation is identified in the SEQUENCE
6765 * operation in this compound.
6766 *
6767 * When the client supports GETATTR(fs_locations_info), it can
6768 * be plumbed in here.
6769 */
6770static int _nfs41_proc_get_locations(struct inode *inode,
6771 struct nfs4_fs_locations *locations,
6772 struct page *page, struct rpc_cred *cred)
6773{
6774 struct nfs_server *server = NFS_SERVER(inode);
6775 struct rpc_clnt *clnt = server->client;
6776 u32 bitmask[2] = {
6777 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6778 };
6779 struct nfs4_fs_locations_arg args = {
6780 .fh = NFS_FH(inode),
6781 .page = page,
6782 .bitmask = bitmask,
6783 .migration = 1, /* skip LOOKUP */
6784 };
6785 struct nfs4_fs_locations_res res = {
6786 .fs_locations = locations,
6787 .migration = 1,
6788 };
6789 struct rpc_message msg = {
6790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6791 .rpc_argp = &args,
6792 .rpc_resp = &res,
6793 .rpc_cred = cred,
6794 };
6795 int status;
6796
6797 nfs_fattr_init(&locations->fattr);
6798 locations->server = server;
6799 locations->nlocations = 0;
6800
6801 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6802 nfs4_set_sequence_privileged(&args.seq_args);
6803 status = nfs4_call_sync_sequence(clnt, server, &msg,
6804 &args.seq_args, &res.seq_res);
6805 if (status == NFS4_OK &&
6806 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6807 status = -NFS4ERR_LEASE_MOVED;
6808 return status;
6809}
6810
6811#endif /* CONFIG_NFS_V4_1 */
6812
6813/**
6814 * nfs4_proc_get_locations - discover locations for a migrated FSID
6815 * @inode: inode on FSID that is migrating
6816 * @locations: result of query
6817 * @page: buffer
6818 * @cred: credential to use for this operation
6819 *
6820 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6821 * operation failed, or a negative errno if a local error occurred.
6822 *
6823 * On success, "locations" is filled in, but if the server has
6824 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6825 * asserted.
6826 *
6827 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6828 * from this client that require migration recovery.
6829 */
6830int nfs4_proc_get_locations(struct inode *inode,
6831 struct nfs4_fs_locations *locations,
6832 struct page *page, struct rpc_cred *cred)
6833{
6834 struct nfs_server *server = NFS_SERVER(inode);
6835 struct nfs_client *clp = server->nfs_client;
6836 const struct nfs4_mig_recovery_ops *ops =
6837 clp->cl_mvops->mig_recovery_ops;
6838 struct nfs4_exception exception = { };
6839 int status;
6840
6841 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6842 (unsigned long long)server->fsid.major,
6843 (unsigned long long)server->fsid.minor,
6844 clp->cl_hostname);
6845 nfs_display_fhandle(NFS_FH(inode), __func__);
6846
6847 do {
6848 status = ops->get_locations(inode, locations, page, cred);
6849 if (status != -NFS4ERR_DELAY)
6850 break;
6851 nfs4_handle_exception(server, status, &exception);
6852 } while (exception.retry);
6853 return status;
6854}
6855
Chuck Lever44c99932013-10-17 14:13:30 -04006856/*
6857 * This operation also signals the server that this client is
6858 * performing "lease moved" recovery. The server can stop
6859 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6860 * is appended to this compound to identify the client ID which is
6861 * performing recovery.
6862 */
6863static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6864{
6865 struct nfs_server *server = NFS_SERVER(inode);
6866 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6867 struct rpc_clnt *clnt = server->client;
6868 struct nfs4_fsid_present_arg args = {
6869 .fh = NFS_FH(inode),
6870 .clientid = clp->cl_clientid,
6871 .renew = 1, /* append RENEW */
6872 };
6873 struct nfs4_fsid_present_res res = {
6874 .renew = 1,
6875 };
6876 struct rpc_message msg = {
6877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6878 .rpc_argp = &args,
6879 .rpc_resp = &res,
6880 .rpc_cred = cred,
6881 };
6882 unsigned long now = jiffies;
6883 int status;
6884
6885 res.fh = nfs_alloc_fhandle();
6886 if (res.fh == NULL)
6887 return -ENOMEM;
6888
6889 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6890 nfs4_set_sequence_privileged(&args.seq_args);
6891 status = nfs4_call_sync_sequence(clnt, server, &msg,
6892 &args.seq_args, &res.seq_res);
6893 nfs_free_fhandle(res.fh);
6894 if (status)
6895 return status;
6896
6897 do_renew_lease(clp, now);
6898 return 0;
6899}
6900
6901#ifdef CONFIG_NFS_V4_1
6902
6903/*
6904 * This operation also signals the server that this client is
6905 * performing "lease moved" recovery. The server can stop asserting
6906 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6907 * this operation is identified in the SEQUENCE operation in this
6908 * compound.
6909 */
6910static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6911{
6912 struct nfs_server *server = NFS_SERVER(inode);
6913 struct rpc_clnt *clnt = server->client;
6914 struct nfs4_fsid_present_arg args = {
6915 .fh = NFS_FH(inode),
6916 };
6917 struct nfs4_fsid_present_res res = {
6918 };
6919 struct rpc_message msg = {
6920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6921 .rpc_argp = &args,
6922 .rpc_resp = &res,
6923 .rpc_cred = cred,
6924 };
6925 int status;
6926
6927 res.fh = nfs_alloc_fhandle();
6928 if (res.fh == NULL)
6929 return -ENOMEM;
6930
6931 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6932 nfs4_set_sequence_privileged(&args.seq_args);
6933 status = nfs4_call_sync_sequence(clnt, server, &msg,
6934 &args.seq_args, &res.seq_res);
6935 nfs_free_fhandle(res.fh);
6936 if (status == NFS4_OK &&
6937 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6938 status = -NFS4ERR_LEASE_MOVED;
6939 return status;
6940}
6941
6942#endif /* CONFIG_NFS_V4_1 */
6943
6944/**
6945 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6946 * @inode: inode on FSID to check
6947 * @cred: credential to use for this operation
6948 *
6949 * Server indicates whether the FSID is present, moved, or not
6950 * recognized. This operation is necessary to clear a LEASE_MOVED
6951 * condition for this client ID.
6952 *
6953 * Returns NFS4_OK if the FSID is present on this server,
6954 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6955 * NFS4ERR code if some error occurred on the server, or a
6956 * negative errno if a local failure occurred.
6957 */
6958int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6959{
6960 struct nfs_server *server = NFS_SERVER(inode);
6961 struct nfs_client *clp = server->nfs_client;
6962 const struct nfs4_mig_recovery_ops *ops =
6963 clp->cl_mvops->mig_recovery_ops;
6964 struct nfs4_exception exception = { };
6965 int status;
6966
6967 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6968 (unsigned long long)server->fsid.major,
6969 (unsigned long long)server->fsid.minor,
6970 clp->cl_hostname);
6971 nfs_display_fhandle(NFS_FH(inode), __func__);
6972
6973 do {
6974 status = ops->fsid_present(inode, cred);
6975 if (status != -NFS4ERR_DELAY)
6976 break;
6977 nfs4_handle_exception(server, status, &exception);
6978 } while (exception.retry);
6979 return status;
6980}
6981
Andy Adamson5ec16a82013-08-08 10:57:55 -04006982/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006983 * If 'use_integrity' is true and the state managment nfs_client
6984 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6985 * and the machine credential as per RFC3530bis and RFC5661 Security
6986 * Considerations sections. Otherwise, just use the user cred with the
6987 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006988 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006989static 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 +00006990{
6991 int status;
6992 struct nfs4_secinfo_arg args = {
6993 .dir_fh = NFS_FH(dir),
6994 .name = name,
6995 };
6996 struct nfs4_secinfo_res res = {
6997 .flavors = flavors,
6998 };
6999 struct rpc_message msg = {
7000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7001 .rpc_argp = &args,
7002 .rpc_resp = &res,
7003 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007004 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007005 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007006
7007 if (use_integrity) {
7008 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007009 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7010 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007011 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007012
7013 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007014
7015 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7016 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7017
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007018 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7019 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007020 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007021
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007022 if (cred)
7023 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007024
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007025 return status;
7026}
7027
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007028int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7029 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007030{
7031 struct nfs4_exception exception = { };
7032 int err;
7033 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007034 err = -NFS4ERR_WRONGSEC;
7035
7036 /* try to use integrity protection with machine cred */
7037 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7038 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7039
7040 /*
7041 * if unable to use integrity protection, or SECINFO with
7042 * integrity protection returns NFS4ERR_WRONGSEC (which is
7043 * disallowed by spec, but exists in deployed servers) use
7044 * the current filesystem's rpc_client and the user cred.
7045 */
7046 if (err == -NFS4ERR_WRONGSEC)
7047 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7048
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007049 trace_nfs4_secinfo(dir, name, err);
7050 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007051 &exception);
7052 } while (exception.retry);
7053 return err;
7054}
7055
Andy Adamson557134a2009-04-01 09:21:53 -04007056#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007057/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007058 * Check the exchange flags returned by the server for invalid flags, having
7059 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7060 * DS flags set.
7061 */
7062static int nfs4_check_cl_exchange_flags(u32 flags)
7063{
7064 if (flags & ~EXCHGID4_FLAG_MASK_R)
7065 goto out_inval;
7066 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7067 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7068 goto out_inval;
7069 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7070 goto out_inval;
7071 return NFS_OK;
7072out_inval:
7073 return -NFS4ERR_INVAL;
7074}
7075
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007076static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007077nfs41_same_server_scope(struct nfs41_server_scope *a,
7078 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007079{
7080 if (a->server_scope_sz == b->server_scope_sz &&
7081 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7082 return true;
7083
7084 return false;
7085}
7086
Andy Adamson02a95de2016-02-05 16:08:37 -05007087static void
7088nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7089{
7090}
7091
7092static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7093 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7094};
7095
Andy Adamson357f54d2010-12-14 10:11:57 -05007096/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007097 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007098 *
7099 * The 4.1 client currently uses the same TCP connection for the
7100 * fore and backchannel.
7101 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007102static
7103int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7104 struct rpc_xprt *xprt,
7105 struct nfs_client *clp,
7106 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007107{
7108 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007109 struct nfs41_bind_conn_to_session_args args = {
7110 .client = clp,
7111 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7112 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007113 struct nfs41_bind_conn_to_session_res res;
7114 struct rpc_message msg = {
7115 .rpc_proc =
7116 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007117 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007118 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007119 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007120 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007121 struct rpc_task_setup task_setup_data = {
7122 .rpc_client = clnt,
7123 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007124 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007125 .rpc_message = &msg,
7126 .flags = RPC_TASK_TIMEOUT,
7127 };
7128 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007129
7130 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007131
Trond Myklebust71a097c2015-02-18 09:27:18 -08007132 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7133 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7134 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007135
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007136 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7137 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7138 args.dir = NFS4_CDFC4_FORE;
7139
7140 task = rpc_run_task(&task_setup_data);
7141 if (!IS_ERR(task)) {
7142 status = task->tk_status;
7143 rpc_put_task(task);
7144 } else
7145 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007146 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007147 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007148 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007149 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7150 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7151 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007152 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007153 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007154 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007155 dprintk("NFS: %s: Unexpected direction from server\n",
7156 __func__);
7157 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007158 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007159 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007160 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007161 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7162 __func__);
7163 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007164 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007165 }
7166 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007167out:
7168 dprintk("<-- %s status= %d\n", __func__, status);
7169 return status;
7170}
7171
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007172struct rpc_bind_conn_calldata {
7173 struct nfs_client *clp;
7174 struct rpc_cred *cred;
7175};
7176
7177static int
7178nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7179 struct rpc_xprt *xprt,
7180 void *calldata)
7181{
7182 struct rpc_bind_conn_calldata *p = calldata;
7183
7184 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7185}
7186
7187int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7188{
7189 struct rpc_bind_conn_calldata data = {
7190 .clp = clp,
7191 .cred = cred,
7192 };
7193 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7194 nfs4_proc_bind_conn_to_session_callback, &data);
7195}
7196
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007197/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007198 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7199 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007200 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007201static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7202 .how = SP4_MACH_CRED,
7203 .enforce.u.words = {
7204 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7205 1 << (OP_EXCHANGE_ID - 32) |
7206 1 << (OP_CREATE_SESSION - 32) |
7207 1 << (OP_DESTROY_SESSION - 32) |
7208 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007209 },
7210 .allow.u.words = {
7211 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007212 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007213 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007214 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007215 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007216 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007217 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007218 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007219 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007220 1 << (OP_FREE_STATEID - 32) |
7221 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007222 }
7223};
7224
7225/*
7226 * Select the state protection mode for client `clp' given the server results
7227 * from exchange_id in `sp'.
7228 *
7229 * Returns 0 on success, negative errno otherwise.
7230 */
7231static int nfs4_sp4_select_mode(struct nfs_client *clp,
7232 struct nfs41_state_protection *sp)
7233{
7234 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7235 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7236 1 << (OP_EXCHANGE_ID - 32) |
7237 1 << (OP_CREATE_SESSION - 32) |
7238 1 << (OP_DESTROY_SESSION - 32) |
7239 1 << (OP_DESTROY_CLIENTID - 32)
7240 };
7241 unsigned int i;
7242
7243 if (sp->how == SP4_MACH_CRED) {
7244 /* Print state protect result */
7245 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7246 for (i = 0; i <= LAST_NFS4_OP; i++) {
7247 if (test_bit(i, sp->enforce.u.longs))
7248 dfprintk(MOUNT, " enforce op %d\n", i);
7249 if (test_bit(i, sp->allow.u.longs))
7250 dfprintk(MOUNT, " allow op %d\n", i);
7251 }
7252
7253 /* make sure nothing is on enforce list that isn't supported */
7254 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7255 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7256 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7257 return -EINVAL;
7258 }
7259 }
7260
7261 /*
7262 * Minimal mode - state operations are allowed to use machine
7263 * credential. Note this already happens by default, so the
7264 * client doesn't have to do anything more than the negotiation.
7265 *
7266 * NOTE: we don't care if EXCHANGE_ID is in the list -
7267 * we're already using the machine cred for exchange_id
7268 * and will never use a different cred.
7269 */
7270 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7271 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7272 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7273 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7274 dfprintk(MOUNT, "sp4_mach_cred:\n");
7275 dfprintk(MOUNT, " minimal mode enabled\n");
7276 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7277 } else {
7278 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7279 return -EINVAL;
7280 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007281
7282 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007283 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7284 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007285 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7286 dfprintk(MOUNT, " cleanup mode enabled\n");
7287 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7288 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007289
Andrew Elble99ade3c2015-12-02 09:39:51 -05007290 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7291 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7292 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7293 &clp->cl_sp4_flags);
7294 }
7295
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007296 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7297 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7298 dfprintk(MOUNT, " secinfo mode enabled\n");
7299 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7300 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007301
7302 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7303 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7304 dfprintk(MOUNT, " stateid mode enabled\n");
7305 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7306 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007307
7308 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7309 dfprintk(MOUNT, " write mode enabled\n");
7310 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7311 }
7312
7313 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7314 dfprintk(MOUNT, " commit mode enabled\n");
7315 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7316 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007317 }
7318
7319 return 0;
7320}
7321
Andy Adamson8d89bd72016-09-09 09:22:18 -04007322struct nfs41_exchange_id_data {
7323 struct nfs41_exchange_id_res res;
7324 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007325 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007326 int rpc_status;
7327};
7328
7329static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7330{
7331 struct nfs41_exchange_id_data *cdata =
7332 (struct nfs41_exchange_id_data *)data;
7333 struct nfs_client *clp = cdata->args.client;
7334 int status = task->tk_status;
7335
7336 trace_nfs4_exchange_id(clp, status);
7337
7338 if (status == 0)
7339 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007340
7341 if (cdata->xprt && status == 0) {
7342 status = nfs4_detect_session_trunking(clp, &cdata->res,
7343 cdata->xprt);
7344 goto out;
7345 }
7346
Andy Adamson8d89bd72016-09-09 09:22:18 -04007347 if (status == 0)
7348 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7349
7350 if (status == 0) {
7351 clp->cl_clientid = cdata->res.clientid;
7352 clp->cl_exchange_flags = cdata->res.flags;
7353 /* Client ID is not confirmed */
7354 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7355 clear_bit(NFS4_SESSION_ESTABLISHED,
7356 &clp->cl_session->session_state);
7357 clp->cl_seqid = cdata->res.seqid;
7358 }
7359
7360 kfree(clp->cl_serverowner);
7361 clp->cl_serverowner = cdata->res.server_owner;
7362 cdata->res.server_owner = NULL;
7363
7364 /* use the most recent implementation id */
7365 kfree(clp->cl_implid);
7366 clp->cl_implid = cdata->res.impl_id;
7367 cdata->res.impl_id = NULL;
7368
7369 if (clp->cl_serverscope != NULL &&
7370 !nfs41_same_server_scope(clp->cl_serverscope,
7371 cdata->res.server_scope)) {
7372 dprintk("%s: server_scope mismatch detected\n",
7373 __func__);
7374 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7375 kfree(clp->cl_serverscope);
7376 clp->cl_serverscope = NULL;
7377 }
7378
7379 if (clp->cl_serverscope == NULL) {
7380 clp->cl_serverscope = cdata->res.server_scope;
7381 cdata->res.server_scope = NULL;
7382 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007383 /* Save the EXCHANGE_ID verifier session trunk tests */
7384 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7385 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007386 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007387out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007388 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007389 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007390}
7391
7392static void nfs4_exchange_id_release(void *data)
7393{
7394 struct nfs41_exchange_id_data *cdata =
7395 (struct nfs41_exchange_id_data *)data;
7396
7397 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007398 if (cdata->xprt) {
7399 xprt_put(cdata->xprt);
7400 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7401 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007402 kfree(cdata->res.impl_id);
7403 kfree(cdata->res.server_scope);
7404 kfree(cdata->res.server_owner);
7405 kfree(cdata);
7406}
7407
7408static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7409 .rpc_call_done = nfs4_exchange_id_done,
7410 .rpc_release = nfs4_exchange_id_release,
7411};
7412
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007413/*
7414 * _nfs4_proc_exchange_id()
7415 *
7416 * Wrapper for EXCHANGE_ID operation.
7417 */
7418static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007419 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007420{
7421 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007422 struct rpc_message msg = {
7423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007424 .rpc_cred = cred,
7425 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007426 struct rpc_task_setup task_setup_data = {
7427 .rpc_client = clp->cl_rpcclient,
7428 .callback_ops = &nfs4_exchange_id_call_ops,
7429 .rpc_message = &msg,
7430 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7431 };
7432 struct nfs41_exchange_id_data *calldata;
7433 struct rpc_task *task;
7434 int status = -EIO;
7435
7436 if (!atomic_inc_not_zero(&clp->cl_count))
7437 goto out;
7438
7439 status = -ENOMEM;
7440 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7441 if (!calldata)
7442 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007443
Andy Adamsonad0849a2016-09-09 09:22:28 -04007444 if (!xprt)
7445 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007446
7447 status = nfs4_init_uniform_client_string(clp);
7448 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007449 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007450
7451 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007452 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007453 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007454
Andy Adamson8d89bd72016-09-09 09:22:18 -04007455 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7456 GFP_NOFS);
7457 status = -ENOMEM;
7458 if (unlikely(calldata->res.server_owner == NULL))
7459 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007460
Andy Adamson8d89bd72016-09-09 09:22:18 -04007461 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007462 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007463 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007464 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007465
Andy Adamson8d89bd72016-09-09 09:22:18 -04007466 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7467 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007468 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007469
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007470 switch (sp4_how) {
7471 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007472 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007473 break;
7474
7475 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007476 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007477 break;
7478
7479 default:
7480 /* unsupported! */
7481 WARN_ON_ONCE(1);
7482 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007483 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007484 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007485 if (xprt) {
7486 calldata->xprt = xprt;
7487 task_setup_data.rpc_xprt = xprt;
7488 task_setup_data.flags =
7489 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7490 calldata->args.verifier = &clp->cl_confirm;
7491 } else {
7492 calldata->args.verifier = &verifier;
7493 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007494 calldata->args.client = clp;
7495#ifdef CONFIG_NFS_V4_1_MIGRATION
7496 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7497 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7498 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7499#else
7500 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7501 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7502#endif
7503 msg.rpc_argp = &calldata->args;
7504 msg.rpc_resp = &calldata->res;
7505 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007506
Andy Adamson8d89bd72016-09-09 09:22:18 -04007507 task = rpc_run_task(&task_setup_data);
7508 if (IS_ERR(task)) {
7509 status = PTR_ERR(task);
7510 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007511 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007512
Andy Adamsonad0849a2016-09-09 09:22:28 -04007513 if (!xprt) {
7514 status = rpc_wait_for_completion_task(task);
7515 if (!status)
7516 status = calldata->rpc_status;
7517 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007518 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007519
Andy Adamson8d89bd72016-09-09 09:22:18 -04007520 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007521out:
Chuck Lever177313f2012-05-21 22:44:58 -04007522 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007523 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007524 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007525 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007526 clp->cl_implid->date.seconds,
7527 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007528 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007529 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007530
7531out_impl_id:
7532 kfree(calldata->res.impl_id);
7533out_server_scope:
7534 kfree(calldata->res.server_scope);
7535out_server_owner:
7536 kfree(calldata->res.server_owner);
7537out_calldata:
7538 kfree(calldata);
7539 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007540}
7541
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007542/*
7543 * nfs4_proc_exchange_id()
7544 *
7545 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7546 *
7547 * Since the clientid has expired, all compounds using sessions
7548 * associated with the stale clientid will be returning
7549 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7550 * be in some phase of session reset.
7551 *
7552 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7553 */
7554int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7555{
7556 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7557 int status;
7558
7559 /* try SP4_MACH_CRED if krb5i/p */
7560 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7561 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007562 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007563 if (!status)
7564 return 0;
7565 }
7566
7567 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007568 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007569}
7570
Andy Adamson04fa2c62016-09-09 09:22:29 -04007571/**
7572 * nfs4_test_session_trunk
7573 *
7574 * This is an add_xprt_test() test function called from
7575 * rpc_clnt_setup_test_and_add_xprt.
7576 *
7577 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7578 * and is dereferrenced in nfs4_exchange_id_release
7579 *
7580 * Upon success, add the new transport to the rpc_clnt
7581 *
7582 * @clnt: struct rpc_clnt to get new transport
7583 * @xprt: the rpc_xprt to test
7584 * @data: call data for _nfs4_proc_exchange_id.
7585 */
7586int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7587 void *data)
7588{
7589 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7590 u32 sp4_how;
7591
7592 dprintk("--> %s try %s\n", __func__,
7593 xprt->address_strings[RPC_DISPLAY_ADDR]);
7594
7595 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7596
7597 /* Test connection for session trunking. Async exchange_id call */
7598 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7599}
7600EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7601
Trond Myklebust66245532012-05-25 17:18:09 -04007602static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7603 struct rpc_cred *cred)
7604{
7605 struct rpc_message msg = {
7606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7607 .rpc_argp = clp,
7608 .rpc_cred = cred,
7609 };
7610 int status;
7611
7612 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007613 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007614 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007615 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007616 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7617 return status;
7618}
7619
7620static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7621 struct rpc_cred *cred)
7622{
7623 unsigned int loop;
7624 int ret;
7625
7626 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7627 ret = _nfs4_proc_destroy_clientid(clp, cred);
7628 switch (ret) {
7629 case -NFS4ERR_DELAY:
7630 case -NFS4ERR_CLIENTID_BUSY:
7631 ssleep(1);
7632 break;
7633 default:
7634 return ret;
7635 }
7636 }
7637 return 0;
7638}
7639
7640int nfs4_destroy_clientid(struct nfs_client *clp)
7641{
7642 struct rpc_cred *cred;
7643 int ret = 0;
7644
7645 if (clp->cl_mvops->minor_version < 1)
7646 goto out;
7647 if (clp->cl_exchange_flags == 0)
7648 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007649 if (clp->cl_preserve_clid)
7650 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007651 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007652 ret = nfs4_proc_destroy_clientid(clp, cred);
7653 if (cred)
7654 put_rpccred(cred);
7655 switch (ret) {
7656 case 0:
7657 case -NFS4ERR_STALE_CLIENTID:
7658 clp->cl_exchange_flags = 0;
7659 }
7660out:
7661 return ret;
7662}
7663
Andy Adamson2050f0c2009-04-01 09:22:30 -04007664struct nfs4_get_lease_time_data {
7665 struct nfs4_get_lease_time_args *args;
7666 struct nfs4_get_lease_time_res *res;
7667 struct nfs_client *clp;
7668};
7669
7670static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7671 void *calldata)
7672{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007673 struct nfs4_get_lease_time_data *data =
7674 (struct nfs4_get_lease_time_data *)calldata;
7675
7676 dprintk("--> %s\n", __func__);
7677 /* just setup sequence, do not trigger session recovery
7678 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007679 nfs41_setup_sequence(data->clp->cl_session,
7680 &data->args->la_seq_args,
7681 &data->res->lr_seq_res,
7682 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007683 dprintk("<-- %s\n", __func__);
7684}
7685
7686/*
7687 * Called from nfs4_state_manager thread for session setup, so don't recover
7688 * from sequence operation or clientid errors.
7689 */
7690static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7691{
7692 struct nfs4_get_lease_time_data *data =
7693 (struct nfs4_get_lease_time_data *)calldata;
7694
7695 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007696 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7697 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007698 switch (task->tk_status) {
7699 case -NFS4ERR_DELAY:
7700 case -NFS4ERR_GRACE:
7701 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7702 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7703 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007704 /* fall through */
7705 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007706 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007707 return;
7708 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007709 dprintk("<-- %s\n", __func__);
7710}
7711
Trond Myklebust17280172012-03-11 13:11:00 -04007712static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007713 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7714 .rpc_call_done = nfs4_get_lease_time_done,
7715};
7716
7717int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7718{
7719 struct rpc_task *task;
7720 struct nfs4_get_lease_time_args args;
7721 struct nfs4_get_lease_time_res res = {
7722 .lr_fsinfo = fsinfo,
7723 };
7724 struct nfs4_get_lease_time_data data = {
7725 .args = &args,
7726 .res = &res,
7727 .clp = clp,
7728 };
7729 struct rpc_message msg = {
7730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7731 .rpc_argp = &args,
7732 .rpc_resp = &res,
7733 };
7734 struct rpc_task_setup task_setup = {
7735 .rpc_client = clp->cl_rpcclient,
7736 .rpc_message = &msg,
7737 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007738 .callback_data = &data,
7739 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007740 };
7741 int status;
7742
Chuck Levera9c92d62013-08-09 12:48:18 -04007743 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007744 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007745 dprintk("--> %s\n", __func__);
7746 task = rpc_run_task(&task_setup);
7747
7748 if (IS_ERR(task))
7749 status = PTR_ERR(task);
7750 else {
7751 status = task->tk_status;
7752 rpc_put_task(task);
7753 }
7754 dprintk("<-- %s return %d\n", __func__, status);
7755
7756 return status;
7757}
7758
Andy Adamsonfc931582009-04-01 09:22:31 -04007759/*
7760 * Initialize the values to be used by the client in CREATE_SESSION
7761 * If nfs4_init_session set the fore channel request and response sizes,
7762 * use them.
7763 *
7764 * Set the back channel max_resp_sz_cached to zero to force the client to
7765 * always set csa_cachethis to FALSE because the current implementation
7766 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7767 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007768static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7769 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007770{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007771 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007772 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007773
Andy Adamson18aad3d2013-06-26 12:21:49 -04007774 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7775 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7776
Andy Adamsonfc931582009-04-01 09:22:31 -04007777 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007778 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7779 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007780 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007781 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007782
7783 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007784 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007785 __func__,
7786 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007787 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007788
7789 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007790 args->bc_attrs.max_rqst_sz = max_bc_payload;
7791 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007792 args->bc_attrs.max_resp_sz_cached = 0;
7793 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007794 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007795
7796 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7797 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7798 __func__,
7799 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7800 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7801 args->bc_attrs.max_reqs);
7802}
7803
Trond Myklebust79969dd2015-02-18 11:30:18 -08007804static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7805 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007806{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007807 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007808 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007809
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007810 if (rcvd->max_resp_sz > sent->max_resp_sz)
7811 return -EINVAL;
7812 /*
7813 * Our requested max_ops is the minimum we need; we're not
7814 * prepared to break up compounds into smaller pieces than that.
7815 * So, no point even trying to continue if the server won't
7816 * cooperate:
7817 */
7818 if (rcvd->max_ops < sent->max_ops)
7819 return -EINVAL;
7820 if (rcvd->max_reqs == 0)
7821 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007822 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7823 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007824 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007825}
7826
Trond Myklebust79969dd2015-02-18 11:30:18 -08007827static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7828 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007829{
7830 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007831 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007832
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007833 if (!(res->flags & SESSION4_BACK_CHAN))
7834 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007835 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7836 return -EINVAL;
7837 if (rcvd->max_resp_sz < sent->max_resp_sz)
7838 return -EINVAL;
7839 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7840 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007841 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007842 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007843 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007844 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007845out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007846 return 0;
7847}
Andy Adamson8d353012009-04-01 09:22:32 -04007848
Andy Adamson8d353012009-04-01 09:22:32 -04007849static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007850 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007851{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007852 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007853
Trond Myklebust79969dd2015-02-18 11:30:18 -08007854 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007855 if (ret)
7856 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007857 return nfs4_verify_back_channel_attrs(args, res);
7858}
7859
7860static void nfs4_update_session(struct nfs4_session *session,
7861 struct nfs41_create_session_res *res)
7862{
7863 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007864 /* Mark client id and session as being confirmed */
7865 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7866 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007867 session->flags = res->flags;
7868 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007869 if (res->flags & SESSION4_BACK_CHAN)
7870 memcpy(&session->bc_attrs, &res->bc_attrs,
7871 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007872}
7873
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007874static int _nfs4_proc_create_session(struct nfs_client *clp,
7875 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007876{
7877 struct nfs4_session *session = clp->cl_session;
7878 struct nfs41_create_session_args args = {
7879 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007880 .clientid = clp->cl_clientid,
7881 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007882 .cb_program = NFS4_CALLBACK,
7883 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007884 struct nfs41_create_session_res res;
7885
Andy Adamsonfc931582009-04-01 09:22:31 -04007886 struct rpc_message msg = {
7887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7888 .rpc_argp = &args,
7889 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007890 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007891 };
7892 int status;
7893
Chuck Lever6b26cc82016-05-02 14:40:40 -04007894 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007895 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007896
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007897 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007898 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007899
Trond Myklebustb519d402016-09-11 14:50:01 -04007900 switch (status) {
7901 case -NFS4ERR_STALE_CLIENTID:
7902 case -NFS4ERR_DELAY:
7903 case -ETIMEDOUT:
7904 case -EACCES:
7905 case -EAGAIN:
7906 goto out;
7907 };
7908
7909 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007910 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007911 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007912 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007913 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007914 if (status)
7915 goto out;
7916 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007917 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007918out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007919 return status;
7920}
7921
7922/*
7923 * Issues a CREATE_SESSION operation to the server.
7924 * It is the responsibility of the caller to verify the session is
7925 * expired before calling this routine.
7926 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007927int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007928{
7929 int status;
7930 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007931 struct nfs4_session *session = clp->cl_session;
7932
7933 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7934
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007935 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007936 if (status)
7937 goto out;
7938
Andy Adamsonaacd5532011-11-09 13:58:21 -05007939 /* Init or reset the session slot tables */
7940 status = nfs4_setup_session_slot_tables(session);
7941 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007942 if (status)
7943 goto out;
7944
7945 ptr = (unsigned *)&session->sess_id.data[0];
7946 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7947 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007948out:
7949 dprintk("<-- %s\n", __func__);
7950 return status;
7951}
7952
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007953/*
7954 * Issue the over-the-wire RPC DESTROY_SESSION.
7955 * The caller must serialize access to this routine.
7956 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007957int nfs4_proc_destroy_session(struct nfs4_session *session,
7958 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007959{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007960 struct rpc_message msg = {
7961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7962 .rpc_argp = session,
7963 .rpc_cred = cred,
7964 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007965 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007966
7967 dprintk("--> nfs4_proc_destroy_session\n");
7968
7969 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007970 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7971 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007972
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007973 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007974 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007975
7976 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007977 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007978 "Session has been destroyed regardless...\n", status);
7979
7980 dprintk("<-- nfs4_proc_destroy_session\n");
7981 return status;
7982}
7983
Trond Myklebust7b38c362012-05-23 13:23:31 -04007984/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007985 * Renew the cl_session lease.
7986 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007987struct nfs4_sequence_data {
7988 struct nfs_client *clp;
7989 struct nfs4_sequence_args args;
7990 struct nfs4_sequence_res res;
7991};
7992
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007993static void nfs41_sequence_release(void *data)
7994{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007995 struct nfs4_sequence_data *calldata = data;
7996 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007997
Alexandros Batsakis71358402010-02-05 03:45:05 -08007998 if (atomic_read(&clp->cl_count) > 1)
7999 nfs4_schedule_state_renewal(clp);
8000 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008001 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008002}
8003
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008004static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8005{
8006 switch(task->tk_status) {
8007 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008008 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8009 return -EAGAIN;
8010 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008011 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008012 }
8013 return 0;
8014}
8015
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008016static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008017{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008018 struct nfs4_sequence_data *calldata = data;
8019 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008020
Trond Myklebust14516c32010-07-31 14:29:06 -04008021 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8022 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008023
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008024 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008025 if (task->tk_status < 0) {
8026 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008027 if (atomic_read(&clp->cl_count) == 1)
8028 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008029
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008030 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8031 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008032 return;
8033 }
8034 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008035 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008036out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008037 dprintk("<-- %s\n", __func__);
8038}
8039
8040static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8041{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008042 struct nfs4_sequence_data *calldata = data;
8043 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008044 struct nfs4_sequence_args *args;
8045 struct nfs4_sequence_res *res;
8046
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008047 args = task->tk_msg.rpc_argp;
8048 res = task->tk_msg.rpc_resp;
8049
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008050 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008051}
8052
8053static const struct rpc_call_ops nfs41_sequence_ops = {
8054 .rpc_call_done = nfs41_sequence_call_done,
8055 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008056 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008057};
8058
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008059static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8060 struct rpc_cred *cred,
8061 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008062{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008063 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008064 struct rpc_message msg = {
8065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8066 .rpc_cred = cred,
8067 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008068 struct rpc_task_setup task_setup_data = {
8069 .rpc_client = clp->cl_rpcclient,
8070 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008071 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008072 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008073 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008074
Alexandros Batsakis71358402010-02-05 03:45:05 -08008075 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008076 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008077 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008078 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008079 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008080 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008081 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008082 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008083 if (is_privileged)
8084 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008085 msg.rpc_argp = &calldata->args;
8086 msg.rpc_resp = &calldata->res;
8087 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008088 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008089
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008090 return rpc_run_task(&task_setup_data);
8091}
8092
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008093static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008094{
8095 struct rpc_task *task;
8096 int ret = 0;
8097
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008098 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008099 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008100 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008101 if (IS_ERR(task))
8102 ret = PTR_ERR(task);
8103 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008104 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008105 dprintk("<-- %s status=%d\n", __func__, ret);
8106 return ret;
8107}
8108
8109static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8110{
8111 struct rpc_task *task;
8112 int ret;
8113
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008114 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008115 if (IS_ERR(task)) {
8116 ret = PTR_ERR(task);
8117 goto out;
8118 }
8119 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008120 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008121 ret = task->tk_status;
8122 rpc_put_task(task);
8123out:
8124 dprintk("<-- %s status=%d\n", __func__, ret);
8125 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008126}
8127
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008128struct nfs4_reclaim_complete_data {
8129 struct nfs_client *clp;
8130 struct nfs41_reclaim_complete_args arg;
8131 struct nfs41_reclaim_complete_res res;
8132};
8133
8134static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8135{
8136 struct nfs4_reclaim_complete_data *calldata = data;
8137
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008138 nfs41_setup_sequence(calldata->clp->cl_session,
8139 &calldata->arg.seq_args,
8140 &calldata->res.seq_res,
8141 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008142}
8143
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008144static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8145{
8146 switch(task->tk_status) {
8147 case 0:
8148 case -NFS4ERR_COMPLETE_ALREADY:
8149 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8150 break;
8151 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008152 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008153 /* fall through */
8154 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008155 return -EAGAIN;
8156 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008157 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008158 }
8159 return 0;
8160}
8161
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008162static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8163{
8164 struct nfs4_reclaim_complete_data *calldata = data;
8165 struct nfs_client *clp = calldata->clp;
8166 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8167
8168 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008169 if (!nfs41_sequence_done(task, res))
8170 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008171
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008172 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008173 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8174 rpc_restart_call_prepare(task);
8175 return;
8176 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008177 dprintk("<-- %s\n", __func__);
8178}
8179
8180static void nfs4_free_reclaim_complete_data(void *data)
8181{
8182 struct nfs4_reclaim_complete_data *calldata = data;
8183
8184 kfree(calldata);
8185}
8186
8187static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8188 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8189 .rpc_call_done = nfs4_reclaim_complete_done,
8190 .rpc_release = nfs4_free_reclaim_complete_data,
8191};
8192
8193/*
8194 * Issue a global reclaim complete.
8195 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008196static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8197 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008198{
8199 struct nfs4_reclaim_complete_data *calldata;
8200 struct rpc_task *task;
8201 struct rpc_message msg = {
8202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008203 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008204 };
8205 struct rpc_task_setup task_setup_data = {
8206 .rpc_client = clp->cl_rpcclient,
8207 .rpc_message = &msg,
8208 .callback_ops = &nfs4_reclaim_complete_call_ops,
8209 .flags = RPC_TASK_ASYNC,
8210 };
8211 int status = -ENOMEM;
8212
8213 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008214 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008215 if (calldata == NULL)
8216 goto out;
8217 calldata->clp = clp;
8218 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008219
Chuck Levera9c92d62013-08-09 12:48:18 -04008220 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008221 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008222 msg.rpc_argp = &calldata->arg;
8223 msg.rpc_resp = &calldata->res;
8224 task_setup_data.callback_data = calldata;
8225 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008226 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008227 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008228 goto out;
8229 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008230 status = nfs4_wait_for_completion_rpc_task(task);
8231 if (status == 0)
8232 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008233 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008234 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008235out:
8236 dprintk("<-- %s status=%d\n", __func__, status);
8237 return status;
8238}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008239
8240static void
8241nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8242{
8243 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008244 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008245 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008246
8247 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008248 nfs41_setup_sequence(session, &lgp->args.seq_args,
8249 &lgp->res.seq_res, task);
8250 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008251}
8252
8253static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8254{
8255 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008256
8257 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008258 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008259 dprintk("<-- %s\n", __func__);
8260}
8261
8262static int
8263nfs4_layoutget_handle_exception(struct rpc_task *task,
8264 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8265{
Trond Myklebustee314c22012-10-01 17:25:48 -07008266 struct inode *inode = lgp->args.inode;
8267 struct nfs_server *server = NFS_SERVER(inode);
8268 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008269 int nfs4err = task->tk_status;
8270 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008271 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008272
Boaz Harroshed7e5422014-01-22 20:34:54 +02008273 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008274
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008275 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008276 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008277 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008278
8279 /*
8280 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8281 * on the file. set tk_status to -ENODATA to tell upper layer to
8282 * retry go inband.
8283 */
8284 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008285 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008286 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008287 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008288 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8289 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8290 */
8291 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008292 status = -EOVERFLOW;
8293 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008294 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008295 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008296 * (or clients) writing to the same RAID stripe except when
8297 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008298 *
8299 * Treat it like we would RECALLCONFLICT -- we retry for a little
8300 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008301 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008302 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008303 if (lgp->args.minlength == 0) {
8304 status = -EOVERFLOW;
8305 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008306 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008307 status = -EBUSY;
8308 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008309 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008310 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008311 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008312 case -NFS4ERR_DELEG_REVOKED:
8313 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008314 case -NFS4ERR_EXPIRED:
8315 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008316 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008317 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008318 lo = NFS_I(inode)->layout;
8319 /* If the open stateid was bad, then recover it. */
8320 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8321 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008322 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008323 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008324 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008325 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008326 break;
8327 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008328
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008329 /*
8330 * Mark the bad layout state as invalid, then retry
8331 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008332 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008333 spin_unlock(&inode->i_lock);
8334 pnfs_free_lseg_list(&head);
8335 status = -EAGAIN;
8336 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008337 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008338
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008339 err = nfs4_handle_exception(server, nfs4err, exception);
8340 if (!status) {
8341 if (exception->retry)
8342 status = -EAGAIN;
8343 else
8344 status = err;
8345 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008346out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008347 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008348 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008349}
8350
Idan Kedar85541162012-08-02 11:47:10 +03008351static size_t max_response_pages(struct nfs_server *server)
8352{
8353 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8354 return nfs_page_array_len(0, max_resp_sz);
8355}
8356
8357static void nfs4_free_pages(struct page **pages, size_t size)
8358{
8359 int i;
8360
8361 if (!pages)
8362 return;
8363
8364 for (i = 0; i < size; i++) {
8365 if (!pages[i])
8366 break;
8367 __free_page(pages[i]);
8368 }
8369 kfree(pages);
8370}
8371
8372static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8373{
8374 struct page **pages;
8375 int i;
8376
8377 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8378 if (!pages) {
8379 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8380 return NULL;
8381 }
8382
8383 for (i = 0; i < size; i++) {
8384 pages[i] = alloc_page(gfp_flags);
8385 if (!pages[i]) {
8386 dprintk("%s: failed to allocate page\n", __func__);
8387 nfs4_free_pages(pages, size);
8388 return NULL;
8389 }
8390 }
8391
8392 return pages;
8393}
8394
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008395static void nfs4_layoutget_release(void *calldata)
8396{
8397 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008398 struct inode *inode = lgp->args.inode;
8399 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008400 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008401
8402 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008403 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008404 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008405 put_nfs_open_context(lgp->args.ctx);
8406 kfree(calldata);
8407 dprintk("<-- %s\n", __func__);
8408}
8409
8410static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8411 .rpc_call_prepare = nfs4_layoutget_prepare,
8412 .rpc_call_done = nfs4_layoutget_done,
8413 .rpc_release = nfs4_layoutget_release,
8414};
8415
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008416struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008417nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008418{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008419 struct inode *inode = lgp->args.inode;
8420 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008421 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008422 struct rpc_task *task;
8423 struct rpc_message msg = {
8424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8425 .rpc_argp = &lgp->args,
8426 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008427 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008428 };
8429 struct rpc_task_setup task_setup_data = {
8430 .rpc_client = server->client,
8431 .rpc_message = &msg,
8432 .callback_ops = &nfs4_layoutget_call_ops,
8433 .callback_data = lgp,
8434 .flags = RPC_TASK_ASYNC,
8435 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008436 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008437 struct nfs4_exception exception = {
8438 .inode = inode,
8439 .timeout = *timeout,
8440 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008441 int status = 0;
8442
8443 dprintk("--> %s\n", __func__);
8444
Peng Tao4bd5a982014-11-17 11:05:17 +08008445 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8446 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8447
Idan Kedar85541162012-08-02 11:47:10 +03008448 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8449 if (!lgp->args.layout.pages) {
8450 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008451 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008452 }
8453 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8454
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008455 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008456 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008457 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008458
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008459 task = rpc_run_task(&task_setup_data);
8460 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008461 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008462 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008463 if (status == 0) {
8464 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8465 *timeout = exception.timeout;
8466 }
8467
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008468 trace_nfs4_layoutget(lgp->args.ctx,
8469 &lgp->args.range,
8470 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008471 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008472 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008473
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008474 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8475 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008476 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008477 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008478 rpc_put_task(task);
8479 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008480 if (status)
8481 return ERR_PTR(status);
8482 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008483}
8484
Benny Halevycbe82602011-05-22 19:52:37 +03008485static void
8486nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8487{
8488 struct nfs4_layoutreturn *lrp = calldata;
8489
8490 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008491 nfs41_setup_sequence(lrp->clp->cl_session,
8492 &lrp->args.seq_args,
8493 &lrp->res.seq_res,
8494 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008495}
8496
8497static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8498{
8499 struct nfs4_layoutreturn *lrp = calldata;
8500 struct nfs_server *server;
8501
8502 dprintk("--> %s\n", __func__);
8503
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008504 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008505 return;
8506
8507 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008508 switch (task->tk_status) {
8509 default:
8510 task->tk_status = 0;
8511 case 0:
8512 break;
8513 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008514 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008515 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008516 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008517 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008518 return;
8519 }
Benny Halevycbe82602011-05-22 19:52:37 +03008520 dprintk("<-- %s\n", __func__);
8521}
8522
8523static void nfs4_layoutreturn_release(void *calldata)
8524{
8525 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008526 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008527 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008528
8529 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008530 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008531 if (lrp->res.lrs_present) {
8532 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8533 &lrp->args.range,
8534 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008535 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008536 } else
8537 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008538 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008539 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008540 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008541 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008542 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008543 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008544 kfree(calldata);
8545 dprintk("<-- %s\n", __func__);
8546}
8547
8548static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8549 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8550 .rpc_call_done = nfs4_layoutreturn_done,
8551 .rpc_release = nfs4_layoutreturn_release,
8552};
8553
Peng Tao6c166052014-11-17 09:30:40 +08008554int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008555{
8556 struct rpc_task *task;
8557 struct rpc_message msg = {
8558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8559 .rpc_argp = &lrp->args,
8560 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008561 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008562 };
8563 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008564 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008565 .rpc_message = &msg,
8566 .callback_ops = &nfs4_layoutreturn_call_ops,
8567 .callback_data = lrp,
8568 };
Peng Tao6c166052014-11-17 09:30:40 +08008569 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008570
Andrew Elble99ade3c2015-12-02 09:39:51 -05008571 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8572 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8573 &task_setup_data.rpc_client, &msg);
8574
Benny Halevycbe82602011-05-22 19:52:37 +03008575 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008576 if (!sync) {
8577 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8578 if (!lrp->inode) {
8579 nfs4_layoutreturn_release(lrp);
8580 return -EAGAIN;
8581 }
8582 task_setup_data.flags |= RPC_TASK_ASYNC;
8583 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008584 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008585 task = rpc_run_task(&task_setup_data);
8586 if (IS_ERR(task))
8587 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008588 if (sync)
8589 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008590 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008591 dprintk("<-- %s status=%d\n", __func__, status);
8592 rpc_put_task(task);
8593 return status;
8594}
8595
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008596static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008597_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8598 struct pnfs_device *pdev,
8599 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008600{
8601 struct nfs4_getdeviceinfo_args args = {
8602 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008603 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8604 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008605 };
8606 struct nfs4_getdeviceinfo_res res = {
8607 .pdev = pdev,
8608 };
8609 struct rpc_message msg = {
8610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8611 .rpc_argp = &args,
8612 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008613 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008614 };
8615 int status;
8616
8617 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008618 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008619 if (res.notification & ~args.notify_types)
8620 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008621 if (res.notification != args.notify_types)
8622 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008623
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008624 dprintk("<-- %s status=%d\n", __func__, status);
8625
8626 return status;
8627}
8628
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008629int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8630 struct pnfs_device *pdev,
8631 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008632{
8633 struct nfs4_exception exception = { };
8634 int err;
8635
8636 do {
8637 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008638 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008639 &exception);
8640 } while (exception.retry);
8641 return err;
8642}
8643EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8644
Andy Adamson863a3c62011-03-23 13:27:54 +00008645static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8646{
8647 struct nfs4_layoutcommit_data *data = calldata;
8648 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008649 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008650
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008651 nfs41_setup_sequence(session,
8652 &data->args.seq_args,
8653 &data->res.seq_res,
8654 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008655}
8656
8657static void
8658nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8659{
8660 struct nfs4_layoutcommit_data *data = calldata;
8661 struct nfs_server *server = NFS_SERVER(data->args.inode);
8662
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008663 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008664 return;
8665
8666 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008667 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8668 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8669 case -NFS4ERR_BADLAYOUT: /* no layout */
8670 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008671 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008672 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008673 break;
8674 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008675 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008676 rpc_restart_call_prepare(task);
8677 return;
8678 }
8679 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008680}
8681
8682static void nfs4_layoutcommit_release(void *calldata)
8683{
8684 struct nfs4_layoutcommit_data *data = calldata;
8685
Andy Adamsondb29c082011-07-30 20:52:38 -04008686 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008687 nfs_post_op_update_inode_force_wcc(data->args.inode,
8688 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008689 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008690 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008691 kfree(data);
8692}
8693
8694static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8695 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8696 .rpc_call_done = nfs4_layoutcommit_done,
8697 .rpc_release = nfs4_layoutcommit_release,
8698};
8699
8700int
Andy Adamsonef311532011-03-12 02:58:10 -05008701nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008702{
8703 struct rpc_message msg = {
8704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8705 .rpc_argp = &data->args,
8706 .rpc_resp = &data->res,
8707 .rpc_cred = data->cred,
8708 };
8709 struct rpc_task_setup task_setup_data = {
8710 .task = &data->task,
8711 .rpc_client = NFS_CLIENT(data->args.inode),
8712 .rpc_message = &msg,
8713 .callback_ops = &nfs4_layoutcommit_ops,
8714 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008715 };
8716 struct rpc_task *task;
8717 int status = 0;
8718
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008719 dprintk("NFS: initiating layoutcommit call. sync %d "
8720 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008721 data->args.lastbytewritten,
8722 data->args.inode->i_ino);
8723
Trond Myklebust472e2592015-02-05 16:50:30 -05008724 if (!sync) {
8725 data->inode = nfs_igrab_and_active(data->args.inode);
8726 if (data->inode == NULL) {
8727 nfs4_layoutcommit_release(data);
8728 return -EAGAIN;
8729 }
8730 task_setup_data.flags = RPC_TASK_ASYNC;
8731 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008732 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008733 task = rpc_run_task(&task_setup_data);
8734 if (IS_ERR(task))
8735 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008736 if (sync)
8737 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008738 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008739 dprintk("%s: status %d\n", __func__, status);
8740 rpc_put_task(task);
8741 return status;
8742}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008743
Andy Adamson97431202013-08-08 10:57:56 -04008744/**
8745 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8746 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8747 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008748static int
8749_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008750 struct nfs_fsinfo *info,
8751 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008752{
8753 struct nfs41_secinfo_no_name_args args = {
8754 .style = SECINFO_STYLE_CURRENT_FH,
8755 };
8756 struct nfs4_secinfo_res res = {
8757 .flavors = flavors,
8758 };
8759 struct rpc_message msg = {
8760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8761 .rpc_argp = &args,
8762 .rpc_resp = &res,
8763 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008764 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008765 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008766 int status;
8767
8768 if (use_integrity) {
8769 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008770 cred = nfs4_get_clid_cred(server->nfs_client);
8771 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008772 }
8773
8774 dprintk("--> %s\n", __func__);
8775 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8776 &res.seq_res, 0);
8777 dprintk("<-- %s status=%d\n", __func__, status);
8778
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008779 if (cred)
8780 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008781
8782 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008783}
8784
8785static int
8786nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8787 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8788{
8789 struct nfs4_exception exception = { };
8790 int err;
8791 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008792 /* first try using integrity protection */
8793 err = -NFS4ERR_WRONGSEC;
8794
8795 /* try to use integrity protection with machine cred */
8796 if (_nfs4_is_integrity_protected(server->nfs_client))
8797 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8798 flavors, true);
8799
8800 /*
8801 * if unable to use integrity protection, or SECINFO with
8802 * integrity protection returns NFS4ERR_WRONGSEC (which is
8803 * disallowed by spec, but exists in deployed servers) use
8804 * the current filesystem's rpc_client and the user cred.
8805 */
8806 if (err == -NFS4ERR_WRONGSEC)
8807 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8808 flavors, false);
8809
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008810 switch (err) {
8811 case 0:
8812 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008813 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008814 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008815 default:
8816 err = nfs4_handle_exception(server, err, &exception);
8817 }
8818 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008819out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008820 return err;
8821}
8822
8823static int
8824nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8825 struct nfs_fsinfo *info)
8826{
8827 int err;
8828 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008829 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008830 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008831 struct nfs4_secinfo4 *secinfo;
8832 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008833
8834 page = alloc_page(GFP_KERNEL);
8835 if (!page) {
8836 err = -ENOMEM;
8837 goto out;
8838 }
8839
8840 flavors = page_address(page);
8841 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8842
8843 /*
8844 * Fall back on "guess and check" method if
8845 * the server doesn't support SECINFO_NO_NAME
8846 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008847 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008848 err = nfs4_find_root_sec(server, fhandle, info);
8849 goto out_freepage;
8850 }
8851 if (err)
8852 goto out_freepage;
8853
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008854 for (i = 0; i < flavors->num_flavors; i++) {
8855 secinfo = &flavors->flavors[i];
8856
8857 switch (secinfo->flavor) {
8858 case RPC_AUTH_NULL:
8859 case RPC_AUTH_UNIX:
8860 case RPC_AUTH_GSS:
8861 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8862 &secinfo->flavor_info);
8863 break;
8864 default:
8865 flavor = RPC_AUTH_MAXFLAVOR;
8866 break;
8867 }
8868
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008869 if (!nfs_auth_info_match(&server->auth_info, flavor))
8870 flavor = RPC_AUTH_MAXFLAVOR;
8871
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008872 if (flavor != RPC_AUTH_MAXFLAVOR) {
8873 err = nfs4_lookup_root_sec(server, fhandle,
8874 info, flavor);
8875 if (!err)
8876 break;
8877 }
8878 }
8879
8880 if (flavor == RPC_AUTH_MAXFLAVOR)
8881 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008882
8883out_freepage:
8884 put_page(page);
8885 if (err == -EACCES)
8886 return -EPERM;
8887out:
8888 return err;
8889}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008890
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008891static int _nfs41_test_stateid(struct nfs_server *server,
8892 nfs4_stateid *stateid,
8893 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008894{
8895 int status;
8896 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008897 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008898 };
8899 struct nfs41_test_stateid_res res;
8900 struct rpc_message msg = {
8901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8902 .rpc_argp = &args,
8903 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008904 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008905 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008906 struct rpc_clnt *rpc_client = server->client;
8907
8908 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8909 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008910
Chuck Lever38527b12012-07-11 16:30:23 -04008911 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008912 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008913 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008914 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008915 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008916 if (status != NFS_OK) {
8917 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008918 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008919 }
8920 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008921 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008922}
8923
Trond Myklebust43912bb2016-09-22 13:38:56 -04008924static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8925 int err, struct nfs4_exception *exception)
8926{
8927 exception->retry = 0;
8928 switch(err) {
8929 case -NFS4ERR_DELAY:
8930 nfs4_handle_exception(server, err, exception);
8931 break;
8932 case -NFS4ERR_BADSESSION:
8933 case -NFS4ERR_BADSLOT:
8934 case -NFS4ERR_BAD_HIGH_SLOT:
8935 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8936 case -NFS4ERR_DEADSESSION:
8937 nfs4_do_handle_exception(server, err, exception);
8938 }
8939}
8940
Chuck Lever38527b12012-07-11 16:30:23 -04008941/**
8942 * nfs41_test_stateid - perform a TEST_STATEID operation
8943 *
8944 * @server: server / transport on which to perform the operation
8945 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008946 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008947 *
8948 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8949 * Otherwise a negative NFS4ERR value is returned if the operation
8950 * failed or the state ID is not currently valid.
8951 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008952static int nfs41_test_stateid(struct nfs_server *server,
8953 nfs4_stateid *stateid,
8954 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008955{
8956 struct nfs4_exception exception = { };
8957 int err;
8958 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008959 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008960 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008961 } while (exception.retry);
8962 return err;
8963}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008964
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008965struct nfs_free_stateid_data {
8966 struct nfs_server *server;
8967 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008968 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008969};
8970
8971static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8972{
8973 struct nfs_free_stateid_data *data = calldata;
8974 nfs41_setup_sequence(nfs4_get_session(data->server),
8975 &data->args.seq_args,
8976 &data->res.seq_res,
8977 task);
8978}
8979
8980static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8981{
8982 struct nfs_free_stateid_data *data = calldata;
8983
8984 nfs41_sequence_done(task, &data->res.seq_res);
8985
8986 switch (task->tk_status) {
8987 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008988 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008989 rpc_restart_call_prepare(task);
8990 }
8991}
8992
8993static void nfs41_free_stateid_release(void *calldata)
8994{
8995 kfree(calldata);
8996}
8997
Trond Myklebust17f26b12013-08-21 15:48:42 -04008998static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008999 .rpc_call_prepare = nfs41_free_stateid_prepare,
9000 .rpc_call_done = nfs41_free_stateid_done,
9001 .rpc_release = nfs41_free_stateid_release,
9002};
9003
9004static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009005 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009006 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009007 bool privileged)
9008{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009009 struct rpc_message msg = {
9010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009011 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009012 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009013 struct rpc_task_setup task_setup = {
9014 .rpc_client = server->client,
9015 .rpc_message = &msg,
9016 .callback_ops = &nfs41_free_stateid_ops,
9017 .flags = RPC_TASK_ASYNC,
9018 };
9019 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009020
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009021 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9022 &task_setup.rpc_client, &msg);
9023
Chuck Lever38527b12012-07-11 16:30:23 -04009024 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009025 data = kmalloc(sizeof(*data), GFP_NOFS);
9026 if (!data)
9027 return ERR_PTR(-ENOMEM);
9028 data->server = server;
9029 nfs4_stateid_copy(&data->args.stateid, stateid);
9030
9031 task_setup.callback_data = data;
9032
9033 msg.rpc_argp = &data->args;
9034 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04009035 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009036 if (privileged)
9037 nfs4_set_sequence_privileged(&data->args.seq_args);
9038
9039 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009040}
9041
Chuck Lever38527b12012-07-11 16:30:23 -04009042/**
9043 * nfs41_free_stateid - perform a FREE_STATEID operation
9044 *
9045 * @server: server / transport on which to perform the operation
9046 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009047 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009048 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009049 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009050 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009051 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009052static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009053 const nfs4_stateid *stateid,
9054 struct rpc_cred *cred,
9055 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009056{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009057 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009058
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009059 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009060 if (IS_ERR(task))
9061 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009062 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009063 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009064}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009065
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009066static void
9067nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009068{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009069 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009070
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009071 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009072 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009073}
9074
Trond Myklebust36281ca2012-03-04 18:13:56 -05009075static bool nfs41_match_stateid(const nfs4_stateid *s1,
9076 const nfs4_stateid *s2)
9077{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009078 if (s1->type != s2->type)
9079 return false;
9080
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009081 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009082 return false;
9083
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009084 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009085 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009086 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009087 return true;
9088
9089 return false;
9090}
9091
Andy Adamson557134a2009-04-01 09:21:53 -04009092#endif /* CONFIG_NFS_V4_1 */
9093
Trond Myklebust36281ca2012-03-04 18:13:56 -05009094static bool nfs4_match_stateid(const nfs4_stateid *s1,
9095 const nfs4_stateid *s2)
9096{
Trond Myklebustf597c532012-03-04 18:13:56 -05009097 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009098}
9099
9100
Trond Myklebust17280172012-03-11 13:11:00 -04009101static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009102 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009103 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 .recover_open = nfs4_open_reclaim,
9105 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009106 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009107 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009108};
9109
Andy Adamson591d71c2009-04-01 09:22:47 -04009110#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009111static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009112 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9113 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9114 .recover_open = nfs4_open_reclaim,
9115 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009116 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009117 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009118 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009119};
9120#endif /* CONFIG_NFS_V4_1 */
9121
Trond Myklebust17280172012-03-11 13:11:00 -04009122static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009123 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009124 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009125 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009126 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009127 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009128};
9129
Andy Adamson591d71c2009-04-01 09:22:47 -04009130#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009131static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009132 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9133 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009134 .recover_open = nfs41_open_expired,
9135 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009136 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009137};
9138#endif /* CONFIG_NFS_V4_1 */
9139
Trond Myklebust17280172012-03-11 13:11:00 -04009140static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009141 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009142 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009143 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009144};
9145
9146#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009147static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009148 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009149 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009150 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009151};
9152#endif
9153
Chuck Leverec011fe2013-10-17 14:12:39 -04009154static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009155 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009156 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009157};
9158
9159#if defined(CONFIG_NFS_V4_1)
9160static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009161 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009162 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009163};
9164#endif /* CONFIG_NFS_V4_1 */
9165
Trond Myklebust97dc1352010-06-16 09:52:26 -04009166static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9167 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009168 .init_caps = NFS_CAP_READDIRPLUS
9169 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009170 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009171 .init_client = nfs40_init_client,
9172 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009173 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009174 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009175 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009176 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009177 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009178 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009179 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9180 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9181 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009182 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009183};
9184
9185#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009186static struct nfs_seqid *
9187nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9188{
9189 return NULL;
9190}
9191
Trond Myklebust97dc1352010-06-16 09:52:26 -04009192static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9193 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009194 .init_caps = NFS_CAP_READDIRPLUS
9195 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009196 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009197 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009198 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009199 .init_client = nfs41_init_client,
9200 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009201 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009202 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009203 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009204 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009205 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009206 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009207 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009208 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9209 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9210 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009211 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009212};
9213#endif
9214
Steve Dickson42c2c422013-05-22 12:50:38 -04009215#if defined(CONFIG_NFS_V4_2)
9216static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9217 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009218 .init_caps = NFS_CAP_READDIRPLUS
9219 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009220 | NFS_CAP_POSIX_LOCK
9221 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009222 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009223 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009224 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009225 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009226 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009227 | NFS_CAP_LAYOUTSTATS
9228 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009229 .init_client = nfs41_init_client,
9230 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009231 .match_stateid = nfs41_match_stateid,
9232 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009233 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009234 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009235 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009236 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009237 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009238 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9239 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9240 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009241 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009242};
9243#endif
9244
Trond Myklebust97dc1352010-06-16 09:52:26 -04009245const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9246 [0] = &nfs_v4_0_minor_ops,
9247#if defined(CONFIG_NFS_V4_1)
9248 [1] = &nfs_v4_1_minor_ops,
9249#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009250#if defined(CONFIG_NFS_V4_2)
9251 [2] = &nfs_v4_2_minor_ops,
9252#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009253};
9254
Trond Myklebust13997822016-07-24 17:10:52 -04009255static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009256{
9257 ssize_t error, error2;
9258
9259 error = generic_listxattr(dentry, list, size);
9260 if (error < 0)
9261 return error;
9262 if (list) {
9263 list += error;
9264 size -= error;
9265 }
9266
9267 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9268 if (error2 < 0)
9269 return error2;
9270 return error + error2;
9271}
9272
Trond Myklebust17f26b12013-08-21 15:48:42 -04009273static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009274 .create = nfs_create,
9275 .lookup = nfs_lookup,
9276 .atomic_open = nfs_atomic_open,
9277 .link = nfs_link,
9278 .unlink = nfs_unlink,
9279 .symlink = nfs_symlink,
9280 .mkdir = nfs_mkdir,
9281 .rmdir = nfs_rmdir,
9282 .mknod = nfs_mknod,
9283 .rename = nfs_rename,
9284 .permission = nfs_permission,
9285 .getattr = nfs_getattr,
9286 .setattr = nfs_setattr,
9287 .getxattr = generic_getxattr,
9288 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009289 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009290 .removexattr = generic_removexattr,
9291};
9292
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009293static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009294 .permission = nfs_permission,
9295 .getattr = nfs_getattr,
9296 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009297 .getxattr = generic_getxattr,
9298 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009299 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009300 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009301};
9302
David Howells509de812006-08-22 20:06:11 -04009303const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304 .version = 4, /* protocol version */
9305 .dentry_ops = &nfs4_dentry_operations,
9306 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009307 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009308 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009309 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009310 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009311 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 .getattr = nfs4_proc_getattr,
9313 .setattr = nfs4_proc_setattr,
9314 .lookup = nfs4_proc_lookup,
9315 .access = nfs4_proc_access,
9316 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 .create = nfs4_proc_create,
9318 .remove = nfs4_proc_remove,
9319 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009320 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009322 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009323 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009324 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009325 .link = nfs4_proc_link,
9326 .symlink = nfs4_proc_symlink,
9327 .mkdir = nfs4_proc_mkdir,
9328 .rmdir = nfs4_proc_remove,
9329 .readdir = nfs4_proc_readdir,
9330 .mknod = nfs4_proc_mknod,
9331 .statfs = nfs4_proc_statfs,
9332 .fsinfo = nfs4_proc_fsinfo,
9333 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009334 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009336 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009337 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009338 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009340 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009341 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009342 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009343 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009345 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009346 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009347 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009348 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009349 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009350 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009351 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009352 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009353 .create_server = nfs4_create_server,
9354 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355};
9356
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009357static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009358 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009359 .list = nfs4_xattr_list_nfs4_acl,
9360 .get = nfs4_xattr_get_nfs4_acl,
9361 .set = nfs4_xattr_set_nfs4_acl,
9362};
9363
9364const struct xattr_handler *nfs4_xattr_handlers[] = {
9365 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009366#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9367 &nfs4_xattr_nfs4_label_handler,
9368#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009369 NULL
9370};
9371
Linus Torvalds1da177e2005-04-16 15:20:36 -07009372/*
9373 * Local variables:
9374 * c-basic-offset: 8
9375 * End:
9376 */