blob: 409b9fa2b84471973a21aafdc5eba49bb2f09b6d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400102static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Trond Myklebust26d36302016-09-22 13:39:05 -0400331static void nfs4_test_and_free_stateid(struct nfs_server *server,
332 nfs4_stateid *stateid,
333 struct rpc_cred *cred)
334{
335 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
336
337 ops->test_and_free_expired(server, stateid, cred);
338}
339
340static void __nfs4_free_revoked_stateid(struct nfs_server *server,
341 nfs4_stateid *stateid,
342 struct rpc_cred *cred)
343{
344 stateid->type = NFS4_REVOKED_STATEID_TYPE;
345 nfs4_test_and_free_stateid(server, stateid, cred);
346}
347
348static void nfs4_free_revoked_stateid(struct nfs_server *server,
349 const nfs4_stateid *stateid,
350 struct rpc_cred *cred)
351{
352 nfs4_stateid tmp;
353
354 nfs4_stateid_copy(&tmp, stateid);
355 __nfs4_free_revoked_stateid(server, &tmp, cred);
356}
357
NeilBrown8478eaa2014-09-18 16:09:27 +1000358static long nfs4_update_delay(long *timeout)
359{
360 long ret;
361 if (!timeout)
362 return NFS4_POLL_RETRY_MAX;
363 if (*timeout <= 0)
364 *timeout = NFS4_POLL_RETRY_MIN;
365 if (*timeout > NFS4_POLL_RETRY_MAX)
366 *timeout = NFS4_POLL_RETRY_MAX;
367 ret = *timeout;
368 *timeout <<= 1;
369 return ret;
370}
371
Trond Myklebust65de8722008-12-23 15:21:44 -0500372static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
373{
374 int res = 0;
375
376 might_sleep();
377
NeilBrown8478eaa2014-09-18 16:09:27 +1000378 freezable_schedule_timeout_killable_unsafe(
379 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 if (fatal_signal_pending(current))
381 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 return res;
383}
384
385/* This is the error handling routine for processes that are allowed
386 * to sleep.
387 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400388static int nfs4_do_handle_exception(struct nfs_server *server,
389 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500390{
391 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500392 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400393 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500394 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 int ret = errorcode;
396
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400397 exception->delay = 0;
398 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400400
401 if (stateid == NULL && state != NULL)
402 stateid = &state->stateid;
403
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 switch(errorcode) {
405 case 0:
406 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400407 case -NFS4ERR_DELEG_REVOKED:
408 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400410 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400411 if (inode != NULL && stateid != NULL) {
412 nfs_inode_find_state_and_recover(inode,
413 stateid);
414 goto wait_on_recovery;
415 }
416 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400417 if (inode) {
418 int err;
419
420 err = nfs_async_inode_return_delegation(inode,
421 stateid);
422 if (err == 0)
423 goto wait_on_recovery;
424 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
425 exception->retry = 1;
426 break;
427 }
428 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500429 if (state == NULL)
430 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400431 ret = nfs4_schedule_stateid_recovery(server, state);
432 if (ret < 0)
433 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500434 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500436 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500437 nfs4_schedule_lease_recovery(clp);
438 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400439 case -NFS4ERR_MOVED:
440 ret = nfs4_schedule_migration_recovery(server);
441 if (ret < 0)
442 break;
443 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400444 case -NFS4ERR_LEASE_MOVED:
445 nfs4_schedule_lease_moved_recovery(clp);
446 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500447#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400448 case -NFS4ERR_BADSESSION:
449 case -NFS4ERR_BADSLOT:
450 case -NFS4ERR_BAD_HIGH_SLOT:
451 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
452 case -NFS4ERR_DEADSESSION:
453 case -NFS4ERR_SEQ_FALSE_RETRY:
454 case -NFS4ERR_SEQ_MISORDERED:
455 dprintk("%s ERROR: %d Reset session\n", __func__,
456 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400457 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400458 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500459#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500460 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500461 if (exception->timeout > HZ) {
462 /* We have retried a decent amount, time to
463 * fail
464 */
465 ret = -EBUSY;
466 break;
467 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400469 nfs_inc_server_stats(server, NFSIOS_DELAY);
470 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400471 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400472 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400473 exception->delay = 1;
474 return 0;
475
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400476 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500477 case -NFS4ERR_OLD_STATEID:
478 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800479 break;
480 case -NFS4ERR_BADOWNER:
481 /* The following works around a Linux server bug! */
482 case -NFS4ERR_BADNAME:
483 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
484 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
485 exception->retry = 1;
486 printk(KERN_WARNING "NFS: v4 server %s "
487 "does not accept raw "
488 "uid/gids. "
489 "Reenabling the idmapper.\n",
490 server->nfs_client->cl_hostname);
491 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500492 }
493 /* We failed to handle the error */
494 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500495wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400496 exception->recovering = 1;
497 return 0;
498}
499
500/* This is the error handling routine for processes that are allowed
501 * to sleep.
502 */
503int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
504{
505 struct nfs_client *clp = server->nfs_client;
506 int ret;
507
508 ret = nfs4_do_handle_exception(server, errorcode, exception);
509 if (exception->delay) {
510 ret = nfs4_delay(server->client, &exception->timeout);
511 goto out_retry;
512 }
513 if (exception->recovering) {
514 ret = nfs4_wait_clnt_recover(clp);
515 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
516 return -EIO;
517 goto out_retry;
518 }
519 return ret;
520out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500521 if (ret == 0)
522 exception->retry = 1;
523 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500524}
525
Trond Myklebust037fc982015-09-20 15:51:00 -0400526static int
527nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
528 int errorcode, struct nfs4_exception *exception)
529{
530 struct nfs_client *clp = server->nfs_client;
531 int ret;
532
533 ret = nfs4_do_handle_exception(server, errorcode, exception);
534 if (exception->delay) {
535 rpc_delay(task, nfs4_update_delay(&exception->timeout));
536 goto out_retry;
537 }
538 if (exception->recovering) {
539 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
540 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
541 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
542 goto out_retry;
543 }
544 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
545 ret = -EIO;
546 return ret;
547out_retry:
548 if (ret == 0)
549 exception->retry = 1;
550 return ret;
551}
552
553static int
554nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
555 struct nfs4_state *state, long *timeout)
556{
557 struct nfs4_exception exception = {
558 .state = state,
559 };
560
561 if (task->tk_status >= 0)
562 return 0;
563 if (timeout)
564 exception.timeout = *timeout;
565 task->tk_status = nfs4_async_handle_exception(task, server,
566 task->tk_status,
567 &exception);
568 if (exception.delay && timeout)
569 *timeout = exception.timeout;
570 if (exception.retry)
571 return -EAGAIN;
572 return 0;
573}
574
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400575/*
576 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
577 * or 'false' otherwise.
578 */
579static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
580{
581 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
582
583 if (flavor == RPC_AUTH_GSS_KRB5I ||
584 flavor == RPC_AUTH_GSS_KRB5P)
585 return true;
586
587 return false;
588}
Trond Myklebust65de8722008-12-23 15:21:44 -0500589
Trond Myklebust452e9352010-07-31 14:29:06 -0400590static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock(&clp->cl_lock);
593 if (time_before(clp->cl_last_renewal,timestamp))
594 clp->cl_last_renewal = timestamp;
595 spin_unlock(&clp->cl_lock);
596}
597
Trond Myklebust452e9352010-07-31 14:29:06 -0400598static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
599{
Trond Myklebustbe824162015-07-05 14:50:46 -0400600 struct nfs_client *clp = server->nfs_client;
601
602 if (!nfs4_has_session(clp))
603 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400604}
605
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400606struct nfs4_call_sync_data {
607 const struct nfs_server *seq_server;
608 struct nfs4_sequence_args *seq_args;
609 struct nfs4_sequence_res *seq_res;
610};
611
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800612void nfs4_init_sequence(struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400614{
615 args->sa_slot = NULL;
616 args->sa_cache_this = cache_reply;
617 args->sa_privileged = 0;
618
619 res->sr_slot = NULL;
620}
621
622static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
623{
624 args->sa_privileged = 1;
625}
626
Peng Taocb04ad22014-06-11 05:24:15 +0800627int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
628 struct nfs4_sequence_args *args,
629 struct nfs4_sequence_res *res,
630 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400631{
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 struct nfs4_slot *slot;
633
634 /* slot already allocated? */
635 if (res->sr_slot != NULL)
636 goto out_start;
637
638 spin_lock(&tbl->slot_tbl_lock);
639 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
640 goto out_sleep;
641
642 slot = nfs4_alloc_slot(tbl);
643 if (IS_ERR(slot)) {
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
646 goto out_sleep;
647 }
648 spin_unlock(&tbl->slot_tbl_lock);
649
Trond Myklebust0a014a42016-09-22 13:38:51 -0400650 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400651 args->sa_slot = slot;
652 res->sr_slot = slot;
653
654out_start:
655 rpc_call_start(task);
656 return 0;
657
658out_sleep:
659 if (args->sa_privileged)
660 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
661 NULL, RPC_PRIORITY_PRIVILEGED);
662 else
663 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
664 spin_unlock(&tbl->slot_tbl_lock);
665 return -EAGAIN;
666}
Peng Taocb04ad22014-06-11 05:24:15 +0800667EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400668
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400669static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400670{
671 struct nfs4_slot *slot = res->sr_slot;
672 struct nfs4_slot_table *tbl;
673
Chuck Lever3bd23842013-08-09 12:49:19 -0400674 tbl = slot->table;
675 spin_lock(&tbl->slot_tbl_lock);
676 if (!nfs41_wake_and_assign_slot(tbl, slot))
677 nfs4_free_slot(tbl, slot);
678 spin_unlock(&tbl->slot_tbl_lock);
679
680 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400681}
682
683static int nfs40_sequence_done(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
685{
686 if (res->sr_slot != NULL)
687 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400688 return 1;
689}
690
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400691#if defined(CONFIG_NFS_V4_1)
692
Trond Myklebustd185a332010-06-16 09:52:25 -0400693static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400694{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500695 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400696 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500697 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500698 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400699
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500701 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500702
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400703 /* Bump the slot sequence number */
704 if (slot->seq_done)
705 slot->seq_nr++;
706 slot->seq_done = 0;
707
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500708 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500709 /* Be nice to the server: try to ensure that the last transmitted
710 * value for highest_user_slotid <= target_highest_slotid
711 */
712 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
713 send_new_highest_used_slotid = true;
714
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500716 send_new_highest_used_slotid = false;
717 goto out_unlock;
718 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500719 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500720
721 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
722 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500723out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500724 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400725 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500726 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400727 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400728 if (waitqueue_active(&tbl->slot_waitq))
729 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400730}
731
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400732static int nfs41_sequence_process(struct rpc_task *task,
733 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400734{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500735 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500736 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400737 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500738 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500739 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400740
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500741 if (slot == NULL)
742 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400743 /* don't increment the sequence number if the task wasn't sent */
744 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400745 goto out;
746
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500747 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500748
Trond Myklebustac20d162012-12-15 15:36:07 -0500749 if (slot->interrupted) {
750 slot->interrupted = 0;
751 interrupted = true;
752 }
753
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400754 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500755 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400756 switch (res->sr_status) {
757 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400758 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400759 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500760 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500761 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500762 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400763 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
764 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500765 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400766 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500767 case 1:
768 /*
769 * sr_status remains 1 if an RPC level error occurred.
770 * The server may or may not have processed the sequence
771 * operation..
772 * Mark the slot as having hosted an interrupted RPC call.
773 */
774 slot->interrupted = 1;
775 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 case -NFS4ERR_DELAY:
777 /* The server detected a resend of the RPC call and
778 * returned NFS4ERR_DELAY as per Section 2.10.6.2
779 * of RFC5661.
780 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500781 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400782 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500783 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500784 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400785 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500786 case -NFS4ERR_BADSLOT:
787 /*
788 * The slot id we used was probably retired. Try again
789 * using a different slot id.
790 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500791 goto retry_nowait;
792 case -NFS4ERR_SEQ_MISORDERED:
793 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500794 * Was the last operation on this sequence interrupted?
795 * If so, retry after bumping the sequence number.
796 */
797 if (interrupted) {
798 ++slot->seq_nr;
799 goto retry_nowait;
800 }
801 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500802 * Could this slot have been previously retired?
803 * If so, then the server may be expecting seq_nr = 1!
804 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500805 if (slot->seq_nr != 1) {
806 slot->seq_nr = 1;
807 goto retry_nowait;
808 }
809 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500810 case -NFS4ERR_SEQ_FALSE_RETRY:
811 ++slot->seq_nr;
812 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400813 default:
814 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400815 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400816 }
817out:
818 /* The session may be reset by one of the error handlers. */
819 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500820out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500821 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500822retry_nowait:
823 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400824 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500825 task->tk_status = 0;
826 ret = 0;
827 }
828 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400829out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400830 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 goto out;
832 rpc_delay(task, NFS4_POLL_RETRY_MAX);
833 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400834}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400835
836int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
837{
838 if (!nfs41_sequence_process(task, res))
839 return 0;
840 if (res->sr_slot != NULL)
841 nfs41_sequence_free_slot(res);
842 return 1;
843
844}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500845EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400846
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400847static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
848{
849 if (res->sr_slot == NULL)
850 return 1;
851 if (res->sr_slot->table->session != NULL)
852 return nfs41_sequence_process(task, res);
853 return nfs40_sequence_done(task, res);
854}
855
856static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
857{
858 if (res->sr_slot != NULL) {
859 if (res->sr_slot->table->session != NULL)
860 nfs41_sequence_free_slot(res);
861 else
862 nfs40_sequence_free_slot(res);
863 }
864}
865
Peng Tao2c4b1312014-06-11 05:24:16 +0800866int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400867{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500868 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400869 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400870 if (!res->sr_slot->table->session)
871 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400872 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400873}
Peng Tao2c4b1312014-06-11 05:24:16 +0800874EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400875
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000876int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400877 struct nfs4_sequence_args *args,
878 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400879 struct rpc_task *task)
880{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400881 struct nfs4_slot *slot;
882 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400883
884 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400885 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400886 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400887 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400888
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400889 tbl = &session->fc_slot_table;
890
Trond Myklebust69d206b2012-11-22 13:21:02 -0500891 task->tk_timeout = 0;
892
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400894 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400895 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500896 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500897 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400898 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400899 }
900
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500901 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500902 if (IS_ERR(slot)) {
903 /* If out of memory, try again in 1/4 second */
904 if (slot == ERR_PTR(-ENOMEM))
905 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400906 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400907 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400908 }
909 spin_unlock(&tbl->slot_tbl_lock);
910
Trond Myklebust0a014a42016-09-22 13:38:51 -0400911 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500912 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400913
Chuck Levere8d92382013-08-09 12:47:51 -0400914 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500915 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916
Benny Halevydfb4f3092010-09-24 09:17:01 -0400917 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500918 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400919 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400920 /*
921 * sr_status is only set in decode_sequence, and so will remain
922 * set to 1 if an rpc level failure occurs.
923 */
924 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400925 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400926out_success:
927 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400928 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400929out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400930 /* Privileged tasks are queued with top priority */
931 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400932 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
933 NULL, RPC_PRIORITY_PRIVILEGED);
934 else
935 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400936 spin_unlock(&tbl->slot_tbl_lock);
937 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000939EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400940
Chuck Lever5a580e02013-08-09 12:48:09 -0400941static int nfs4_setup_sequence(const struct nfs_server *server,
942 struct nfs4_sequence_args *args,
943 struct nfs4_sequence_res *res,
944 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400945{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400946 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947 int ret = 0;
948
Chuck Lever3bd23842013-08-09 12:49:19 -0400949 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800950 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
951 args, res, task);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400952
Chuck Levere8d92382013-08-09 12:47:51 -0400953 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400954 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400955 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400956
Trond Myklebust9d12b212012-01-17 22:04:25 -0500957 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400958
Andy Adamsonce5039c2009-04-01 09:22:13 -0400959 dprintk("<-- %s status=%d\n", __func__, ret);
960 return ret;
961}
962
Andy Adamsonce5039c2009-04-01 09:22:13 -0400963static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
964{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400965 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400966 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400967
Trond Myklebust035168ab2010-06-16 09:52:26 -0400968 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
969
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400970 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400971}
972
Andy Adamson69ab40c2009-04-01 09:22:19 -0400973static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400976
Trond Myklebust14516c32010-07-31 14:29:06 -0400977 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400978}
979
Trond Myklebust17280172012-03-11 13:11:00 -0400980static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400982 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400983};
984
Chuck Lever3bd23842013-08-09 12:49:19 -0400985#else /* !CONFIG_NFS_V4_1 */
986
Chuck Lever5a580e02013-08-09 12:48:09 -0400987static int nfs4_setup_sequence(const struct nfs_server *server,
988 struct nfs4_sequence_args *args,
989 struct nfs4_sequence_res *res,
990 struct rpc_task *task)
991{
Peng Taocb04ad22014-06-11 05:24:15 +0800992 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
993 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400994}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400995
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400996static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
997{
998 return nfs40_sequence_done(task, res);
999}
1000
1001static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1002{
1003 if (res->sr_slot != NULL)
1004 nfs40_sequence_free_slot(res);
1005}
1006
Peng Tao2c4b1312014-06-11 05:24:16 +08001007int nfs4_sequence_done(struct rpc_task *task,
1008 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001009{
Chuck Lever3bd23842013-08-09 12:49:19 -04001010 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001011}
Peng Tao2c4b1312014-06-11 05:24:16 +08001012EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001013
1014#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001015
Chuck Lever9915ea72013-08-09 12:48:27 -04001016static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1017{
1018 struct nfs4_call_sync_data *data = calldata;
1019 nfs4_setup_sequence(data->seq_server,
1020 data->seq_args, data->seq_res, task);
1021}
1022
1023static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1024{
1025 struct nfs4_call_sync_data *data = calldata;
1026 nfs4_sequence_done(task, data->seq_res);
1027}
1028
1029static const struct rpc_call_ops nfs40_call_sync_ops = {
1030 .rpc_call_prepare = nfs40_call_sync_prepare,
1031 .rpc_call_done = nfs40_call_sync_done,
1032};
1033
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001034static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001035 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001039{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001040 int ret;
1041 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001042 struct nfs_client *clp = server->nfs_client;
1043 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001044 .seq_server = server,
1045 .seq_args = args,
1046 .seq_res = res,
1047 };
1048 struct rpc_task_setup task_setup = {
1049 .rpc_client = clnt,
1050 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001051 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 .callback_data = &data
1053 };
1054
1055 task = rpc_run_task(&task_setup);
1056 if (IS_ERR(task))
1057 ret = PTR_ERR(task);
1058 else {
1059 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001060 rpc_put_task(task);
1061 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 return ret;
1063}
1064
Bryan Schumaker7c513052011-03-24 17:12:24 +00001065int nfs4_call_sync(struct rpc_clnt *clnt,
1066 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001067 struct rpc_message *msg,
1068 struct nfs4_sequence_args *args,
1069 struct nfs4_sequence_res *res,
1070 int cache_reply)
1071{
Chuck Levera9c92d62013-08-09 12:48:18 -04001072 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001073 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1077{
1078 struct nfs_inode *nfsi = NFS_I(dir);
1079
1080 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001081 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001082 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001084 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001085 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001086 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 spin_unlock(&dir->i_lock);
1088}
1089
1090struct nfs4_opendata {
1091 struct kref kref;
1092 struct nfs_openargs o_arg;
1093 struct nfs_openres o_res;
1094 struct nfs_open_confirmargs c_arg;
1095 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001096 struct nfs4_string owner_name;
1097 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001098 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001100 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001102 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct nfs4_state_owner *owner;
1104 struct nfs4_state *state;
1105 struct iattr attrs;
1106 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001107 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001108 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001109 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001110 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 int cancelled;
1112};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001113
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001114static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1115 int err, struct nfs4_exception *exception)
1116{
1117 if (err != -EINVAL)
1118 return false;
1119 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1120 return false;
1121 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1122 exception->retry = 1;
1123 return true;
1124}
1125
Trond Myklebust6ae37332015-01-30 14:21:14 -05001126static u32
1127nfs4_map_atomic_open_share(struct nfs_server *server,
1128 fmode_t fmode, int openflags)
1129{
1130 u32 res = 0;
1131
1132 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1133 case FMODE_READ:
1134 res = NFS4_SHARE_ACCESS_READ;
1135 break;
1136 case FMODE_WRITE:
1137 res = NFS4_SHARE_ACCESS_WRITE;
1138 break;
1139 case FMODE_READ|FMODE_WRITE:
1140 res = NFS4_SHARE_ACCESS_BOTH;
1141 }
1142 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1143 goto out;
1144 /* Want no delegation if we're using O_DIRECT */
1145 if (openflags & O_DIRECT)
1146 res |= NFS4_SHARE_WANT_NO_DELEG;
1147out:
1148 return res;
1149}
1150
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001151static enum open_claim_type4
1152nfs4_map_atomic_open_claim(struct nfs_server *server,
1153 enum open_claim_type4 claim)
1154{
1155 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1156 return claim;
1157 switch (claim) {
1158 default:
1159 return claim;
1160 case NFS4_OPEN_CLAIM_FH:
1161 return NFS4_OPEN_CLAIM_NULL;
1162 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1163 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1164 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1165 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1166 }
1167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170{
1171 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001172 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173 p->o_res.seqid = p->o_arg.seqid;
1174 p->c_res.seqid = p->c_arg.seqid;
1175 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001176 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001177 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001178 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001179}
1180
Al Viro82a2c1b2011-06-22 18:30:55 -04001181static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001182 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001183 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001184 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001185 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001186 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187{
Al Viro82a2c1b2011-06-22 18:30:55 -04001188 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001189 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001190 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001191 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 struct nfs4_opendata *p;
1193
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001194 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 if (p == NULL)
1196 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001197
1198 p->f_label = nfs4_label_alloc(server, gfp_mask);
1199 if (IS_ERR(p->f_label))
1200 goto err_free_p;
1201
Kinglong Meea49c2692015-07-27 15:31:38 +08001202 p->a_label = nfs4_label_alloc(server, gfp_mask);
1203 if (IS_ERR(p->a_label))
1204 goto err_free_f;
1205
Trond Myklebust63f5f792015-01-23 19:19:25 -05001206 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1207 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001208 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001209 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001210 nfs_sb_active(dentry->d_sb);
1211 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001212 p->dir = parent;
1213 p->owner = sp;
1214 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 p->o_arg.open_flags = flags;
1216 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001217 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1218 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001219 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1220 * will return permission denied for all bits until close */
1221 if (!(flags & O_EXCL)) {
1222 /* ask server to check for all possible rights as results
1223 * are cached */
1224 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1225 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001226 }
David Howells7539bba2006-08-22 20:06:09 -04001227 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001228 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1229 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001230 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001231 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001232 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001233 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001234 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001235 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1236 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001237 case NFS4_OPEN_CLAIM_NULL:
1238 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1239 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1240 p->o_arg.fh = NFS_FH(dir);
1241 break;
1242 case NFS4_OPEN_CLAIM_PREVIOUS:
1243 case NFS4_OPEN_CLAIM_FH:
1244 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1245 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001246 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001247 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001248 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001249 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001250
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001251 p->o_arg.u.attrs = &p->attrs;
1252 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001253
1254 verf[0] = jiffies;
1255 verf[1] = current->pid;
1256 memcpy(p->o_arg.u.verifier.data, verf,
1257 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001258 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001259 p->c_arg.fh = &p->o_res.fh;
1260 p->c_arg.stateid = &p->o_res.stateid;
1261 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001262 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001264 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001265
1266err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001267 nfs4_label_free(p->a_label);
1268err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001269 nfs4_label_free(p->f_label);
1270err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 kfree(p);
1272err:
1273 dput(parent);
1274 return NULL;
1275}
1276
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001279 struct nfs4_opendata *p = container_of(kref,
1280 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001281 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001282
1283 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001284 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001285 if (p->state != NULL)
1286 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001287 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001288
Kinglong Meea49c2692015-07-27 15:31:38 +08001289 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001290 nfs4_label_free(p->f_label);
1291
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001293 dput(p->dentry);
1294 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001295 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001296 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001297 kfree(p);
1298}
1299
1300static void nfs4_opendata_put(struct nfs4_opendata *p)
1301{
1302 if (p != NULL)
1303 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304}
1305
Trond Myklebust06f814a2006-01-03 09:55:07 +01001306static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1307{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001308 int ret;
1309
Trond Myklebust06f814a2006-01-03 09:55:07 +01001310 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001311 return ret;
1312}
1313
Trond Myklebust24311f82015-09-20 10:50:17 -04001314static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1315 fmode_t fmode)
1316{
1317 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1318 case FMODE_READ|FMODE_WRITE:
1319 return state->n_rdwr != 0;
1320 case FMODE_WRITE:
1321 return state->n_wronly != 0;
1322 case FMODE_READ:
1323 return state->n_rdonly != 0;
1324 }
1325 WARN_ON_ONCE(1);
1326 return false;
1327}
1328
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001329static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001330{
1331 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001332
Trond Myklebust536e43d2012-01-17 22:04:26 -05001333 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334 goto out;
1335 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001336 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001337 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1338 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001339 break;
1340 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001341 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1342 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001343 break;
1344 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001345 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1346 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001347 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001348out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001349 return ret;
1350}
1351
Trond Myklebust2a606182015-08-19 22:30:00 -05001352static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1353 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001354{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001355 if (delegation == NULL)
1356 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001358 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001359 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1360 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001361 switch (claim) {
1362 case NFS4_OPEN_CLAIM_NULL:
1363 case NFS4_OPEN_CLAIM_FH:
1364 break;
1365 case NFS4_OPEN_CLAIM_PREVIOUS:
1366 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1367 break;
1368 default:
1369 return 0;
1370 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001371 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001372 return 1;
1373}
1374
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001375static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001376{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001377 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001378 case FMODE_WRITE:
1379 state->n_wronly++;
1380 break;
1381 case FMODE_READ:
1382 state->n_rdonly++;
1383 break;
1384 case FMODE_READ|FMODE_WRITE:
1385 state->n_rdwr++;
1386 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001388}
1389
Trond 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 Schumaker61beef72014-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 Schumaker61beef72014-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 Schumaker61beef72014-09-03 14:15:40 -04001619 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001620 }
Anna Schumaker61beef72014-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 Myklebustdb4f2e62013-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 Myklebustdb4f2e62013-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 Myklebustdb4f2e62013-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 Myklebustdb4f2e62013-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 Myklebustdb4f2e62013-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 Myklebustdb4f2e62013-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 Myklebust035168ab2010-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
2590 /* If a state reset has been done, test_stateid is unneeded */
2591 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2592 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2593 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2594 return -NFS4ERR_BAD_STATEID;
2595
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002596 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002597 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002598 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002599 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2600 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2601 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002602 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002603 }
2604 return status;
2605}
2606
2607static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2608{
Chuck Levereb64cf92012-07-11 16:30:05 -04002609 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002610
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002611 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002612 status = nfs41_check_expired_locks(state);
2613 if (status != NFS_OK)
2614 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002615 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002616 if (status != NFS_OK)
2617 status = nfs4_open_expired(sp, state);
2618 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002619}
2620#endif
2621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002623 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2624 * fields corresponding to attributes that were used to store the verifier.
2625 * Make sure we clobber those fields in the later setattr call
2626 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002627static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2628 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002629{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002630 const u32 *attrset = opendata->o_res.attrset;
2631
2632 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002633 !(sattr->ia_valid & ATTR_ATIME_SET))
2634 sattr->ia_valid |= ATTR_ATIME;
2635
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002636 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002637 !(sattr->ia_valid & ATTR_MTIME_SET))
2638 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002639
2640 /* Except MODE, it seems harmless of setting twice. */
2641 if ((attrset[1] & FATTR4_WORD1_MODE))
2642 sattr->ia_valid &= ~ATTR_MODE;
2643
2644 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2645 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002646}
2647
Trond Myklebustc21443c2013-02-07 14:26:21 -05002648static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2649 fmode_t fmode,
2650 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002651 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002652{
2653 struct nfs4_state_owner *sp = opendata->owner;
2654 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002655 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002656 struct nfs4_state *state;
2657 unsigned int seq;
2658 int ret;
2659
2660 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2661
2662 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002663 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002664 goto out;
2665
2666 state = nfs4_opendata_to_nfs4_state(opendata);
2667 ret = PTR_ERR(state);
2668 if (IS_ERR(state))
2669 goto out;
2670 if (server->caps & NFS_CAP_POSIX_LOCK)
2671 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002672 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2673 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002674
Trond Myklebust275bb302013-05-29 13:11:28 -04002675 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002676 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002677 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002678 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002679 alias = d_exact_alias(dentry, state->inode);
2680 if (!alias)
2681 alias = d_splice_alias(igrab(state->inode), dentry);
2682 /* d_splice_alias() can't fail here - it's a non-directory */
2683 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002684 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002685 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002686 }
2687 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002688 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002689 }
2690
Trond Myklebustc21443c2013-02-07 14:26:21 -05002691 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2692 if (ret != 0)
2693 goto out;
2694
Trond Myklebust3efb9722013-05-29 13:17:04 -04002695 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002696 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002697 nfs_inode_attach_open_context(ctx);
2698 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2699 nfs4_schedule_stateid_recovery(server, state);
2700 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002701out:
2702 return ret;
2703}
2704
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002705/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002706 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 */
Andy Adamson82be4172012-05-23 05:02:35 -04002708static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002709 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002710 int flags,
2711 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002712 struct nfs4_label *label,
2713 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
2715 struct nfs4_state_owner *sp;
2716 struct nfs4_state *state = NULL;
2717 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002718 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002719 struct dentry *dentry = ctx->dentry;
2720 struct rpc_cred *cred = ctx->cred;
2721 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2722 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002723 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002724 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002725 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002729 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2730 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2732 goto out_err;
2733 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002734 status = nfs4_recover_expired_lease(server);
2735 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002736 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002737 if (d_really_is_positive(dentry))
2738 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002739 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002740 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002741 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002742 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002743 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002744 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002745 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
David Quigley14c43f72013-05-22 12:50:43 -04002747 if (label) {
2748 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2749 if (IS_ERR(olabel)) {
2750 status = PTR_ERR(olabel);
2751 goto err_opendata_put;
2752 }
2753 }
2754
Trond Myklebuste911b812014-03-26 13:24:37 -07002755 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2756 if (!opendata->f_attr.mdsthreshold) {
2757 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2758 if (!opendata->f_attr.mdsthreshold)
2759 goto err_free_label;
2760 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002761 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002762 }
David Howells2b0143b2015-03-17 22:25:59 +00002763 if (d_really_is_positive(dentry))
2764 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002765
Trond Myklebust3efb9722013-05-29 13:17:04 -04002766 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002767 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002768 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002769 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002770
NeilBrownefcbc042015-07-30 13:00:56 +10002771 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002772 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002773 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002774 /*
2775 * send create attributes which was not set by open
2776 * with an extra setattr.
2777 */
2778 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2779 nfs_fattr_init(opendata->o_res.f_attr);
2780 status = nfs4_do_setattr(state->inode, cred,
2781 opendata->o_res.f_attr, sattr,
2782 state, label, olabel);
2783 if (status == 0) {
2784 nfs_setattr_update_inode(state->inode, sattr,
2785 opendata->o_res.f_attr);
2786 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2787 }
David Quigley1775fd32013-05-22 12:50:42 -04002788 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002789 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002790 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002791 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002792
Trond Myklebuste911b812014-03-26 13:24:37 -07002793 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002794 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002795 opendata->f_attr.mdsthreshold = NULL;
2796 }
Andy Adamson82be4172012-05-23 05:02:35 -04002797
David Quigley14c43f72013-05-22 12:50:43 -04002798 nfs4_label_free(olabel);
2799
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002800 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002803err_free_label:
2804 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002805err_opendata_put:
2806 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002807err_put_state_owner:
2808 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return status;
2811}
2812
2813
Andy Adamson82be4172012-05-23 05:02:35 -04002814static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002815 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002816 int flags,
2817 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002818 struct nfs4_label *label,
2819 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002821 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 struct nfs4_exception exception = { };
2823 struct nfs4_state *res;
2824 int status;
2825
2826 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002827 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002828 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002829 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if (status == 0)
2831 break;
2832 /* NOTE: BAD_SEQID means the server and client disagree about the
2833 * book-keeping w.r.t. state-changing operations
2834 * (OPEN/CLOSE/LOCK/LOCKU...)
2835 * It is actually a sign of a bug on the client or on the server.
2836 *
2837 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002838 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 * have unhashed the old state_owner for us, and that we can
2840 * therefore safely retry using a new one. We should still warn
2841 * the user though...
2842 */
2843 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002844 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002845 " returned a bad sequence-id error!\n",
2846 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 exception.retry = 1;
2848 continue;
2849 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002850 /*
2851 * BAD_STATEID on OPEN means that the server cancelled our
2852 * state before it received the OPEN_CONFIRM.
2853 * Recover by retrying the request as per the discussion
2854 * on Page 181 of RFC3530.
2855 */
2856 if (status == -NFS4ERR_BAD_STATEID) {
2857 exception.retry = 1;
2858 continue;
2859 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002860 if (status == -EAGAIN) {
2861 /* We must have found a delegation */
2862 exception.retry = 1;
2863 continue;
2864 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002865 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2866 continue;
2867 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 status, &exception));
2869 } while (exception.retry);
2870 return res;
2871}
2872
Trond Myklebust8487c472016-06-26 08:44:35 -04002873static int _nfs4_do_setattr(struct inode *inode,
2874 struct nfs_setattrargs *arg,
2875 struct nfs_setattrres *res,
2876 struct rpc_cred *cred,
2877 struct nfs4_state *state)
2878{
2879 struct nfs_server *server = NFS_SERVER(inode);
2880 struct rpc_message msg = {
2881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2882 .rpc_argp = arg,
2883 .rpc_resp = res,
2884 .rpc_cred = cred,
2885 };
2886 struct rpc_cred *delegation_cred = NULL;
2887 unsigned long timestamp = jiffies;
2888 fmode_t fmode;
2889 bool truncate;
2890 int status;
2891
2892 nfs_fattr_init(res->fattr);
2893
2894 /* Servers should only apply open mode checks for file size changes */
2895 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2896 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2897
2898 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2899 /* Use that stateid */
2900 } else if (truncate && state != NULL) {
2901 struct nfs_lockowner lockowner = {
2902 .l_owner = current->files,
2903 .l_pid = current->tgid,
2904 };
2905 if (!nfs4_valid_open_stateid(state))
2906 return -EBADF;
2907 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2908 &arg->stateid, &delegation_cred) == -EIO)
2909 return -EBADF;
2910 } else
2911 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2912 if (delegation_cred)
2913 msg.rpc_cred = delegation_cred;
2914
2915 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2916
2917 put_rpccred(delegation_cred);
2918 if (status == 0 && state != NULL)
2919 renew_lease(server, timestamp);
2920 trace_nfs4_setattr(inode, &arg->stateid, status);
2921 return status;
2922}
2923
2924static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2925 struct nfs_fattr *fattr, struct iattr *sattr,
2926 struct nfs4_state *state, struct nfs4_label *ilabel,
2927 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002929 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002931 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 .iap = sattr,
2933 .server = server,
2934 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002935 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 };
2937 struct nfs_setattrres res = {
2938 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002939 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 .server = server,
2941 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002942 struct nfs4_exception exception = {
2943 .state = state,
2944 .inode = inode,
2945 .stateid = &arg.stateid,
2946 };
2947 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
David Quigleyaa9c2662013-05-22 12:50:44 -04002949 arg.bitmask = nfs4_bitmask(server, ilabel);
2950 if (ilabel)
2951 arg.bitmask = nfs4_bitmask(server, olabel);
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002954 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002955 switch (err) {
2956 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002957 if (!(sattr->ia_valid & ATTR_SIZE)) {
2958 pr_warn_once("NFSv4: server %s is incorrectly "
2959 "applying open mode checks to "
2960 "a SETATTR that is not "
2961 "changing file size.\n",
2962 server->nfs_client->cl_hostname);
2963 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002964 if (state && !(state->state & FMODE_WRITE)) {
2965 err = -EBADF;
2966 if (sattr->ia_valid & ATTR_OPEN)
2967 err = -EACCES;
2968 goto out;
2969 }
2970 }
2971 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002973out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return err;
2975}
2976
Peng Tao500d7012015-09-22 11:35:22 +08002977static bool
2978nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2979{
2980 if (inode == NULL || !nfs_have_layout(inode))
2981 return false;
2982
2983 return pnfs_wait_on_layoutreturn(inode, task);
2984}
2985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986struct nfs4_closedata {
2987 struct inode *inode;
2988 struct nfs4_state *state;
2989 struct nfs_closeargs arg;
2990 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002991 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002992 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002993 bool roc;
2994 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995};
2996
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002997static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002998{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002999 struct nfs4_closedata *calldata = data;
3000 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003001 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003002
Fred Isamanf7e89172011-01-06 11:36:32 +00003003 if (calldata->roc)
3004 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003005 nfs4_put_open_state(calldata->state);
3006 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003007 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003008 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003009 kfree(calldata);
3010}
3011
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003012static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003014 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003017 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Chuck Levera3ca5652012-03-01 17:00:40 -05003019 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003020 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3021 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003022 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 /* hmm. we are done with the inode, and in the process of freeing
3024 * the state_owner. we keep this around to process errors
3025 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 switch (task->tk_status) {
3027 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003028 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003029 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003030 pnfs_roc_set_barrier(state->inode,
3031 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003032 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003033 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003034 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003036 case -NFS4ERR_EXPIRED:
3037 nfs4_free_revoked_stateid(server,
3038 &calldata->arg.stateid,
3039 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003040 case -NFS4ERR_OLD_STATEID:
3041 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003042 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003043 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003044 rpc_restart_call_prepare(task);
3045 goto out_release;
3046 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003047 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003050 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003051 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003052 goto out_release;
3053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003055 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3056 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003057out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003058 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003059 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003060 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003063static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003065 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003066 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003067 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003068 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003069 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003070
Chuck Levera3ca5652012-03-01 17:00:40 -05003071 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003072 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003073 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003074
Trond Myklebust88069f72009-12-08 08:33:16 -05003075 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003076 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003077 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3078 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3079 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003080 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003081 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003082 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003083 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003084 if (state->n_rdonly == 0)
3085 call_close |= is_rdonly;
3086 else if (is_rdonly)
3087 calldata->arg.fmode |= FMODE_READ;
3088 if (state->n_wronly == 0)
3089 call_close |= is_wronly;
3090 else if (is_wronly)
3091 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003092 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3093 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003094 } else if (is_rdwr)
3095 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3096
Trond Myklebust5d422302013-03-14 16:57:48 -04003097 if (!nfs4_valid_open_stateid(state))
3098 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003099 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003100
3101 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003102 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003103 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003104 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003105
Peng Tao500d7012015-09-22 11:35:22 +08003106 if (nfs4_wait_on_layoutreturn(inode, task)) {
3107 nfs_release_seqid(calldata->arg.seqid);
3108 goto out_wait;
3109 }
3110
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003111 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003112 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003113 if (calldata->roc)
3114 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3115
Trond Myklebust6ae37332015-01-30 14:21:14 -05003116 calldata->arg.share_access =
3117 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3118 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003119
Trond Myklebust516a6af2005-10-27 22:12:41 -04003120 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003121 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003122 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003123 &calldata->arg.seq_args,
3124 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003125 task) != 0)
3126 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003127 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003128 return;
3129out_no_action:
3130 task->tk_action = NULL;
3131out_wait:
3132 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133}
3134
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003135static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003136 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003137 .rpc_call_done = nfs4_close_done,
3138 .rpc_release = nfs4_free_closedata,
3139};
3140
Peng Taofe08c542014-07-03 13:05:01 +08003141static bool nfs4_roc(struct inode *inode)
3142{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003143 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003144 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003145 return pnfs_roc(inode);
3146}
3147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148/*
3149 * It is possible for data to be read/written from a mem-mapped file
3150 * after the sys_close call (which hits the vfs layer as a flush).
3151 * This means that we can't safely call nfsv4 close on a file until
3152 * the inode is cleared. This in turn means that we are not good
3153 * NFSv4 citizens - we do not indicate to the server to update the file's
3154 * share state even when we are done with one of the three share
3155 * stateid's in the inode.
3156 *
3157 * NOTE: Caller must be holding the sp->so_owner semaphore!
3158 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003159int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003161 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003162 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003164 struct nfs4_state_owner *sp = state->owner;
3165 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003166 struct rpc_message msg = {
3167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3168 .rpc_cred = state->owner->so_cred,
3169 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003170 struct rpc_task_setup task_setup_data = {
3171 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003172 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003173 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003174 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003175 .flags = RPC_TASK_ASYNC,
3176 };
Trond Myklebust95121352005-10-18 14:20:12 -07003177 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003179 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3180 &task_setup_data.rpc_client, &msg);
3181
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003182 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003184 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003185 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003186 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003188 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003190 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3191 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003192 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003193 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003194 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003195 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003196 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003197 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003198 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003199 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003200 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003201
Trond Myklebust1174dd12010-12-21 10:52:24 -05003202 msg.rpc_argp = &calldata->arg;
3203 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003204 task_setup_data.callback_data = calldata;
3205 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003206 if (IS_ERR(task))
3207 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003208 status = 0;
3209 if (wait)
3210 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003211 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003212 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003213out_free_calldata:
3214 kfree(calldata);
3215out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003216 nfs4_put_open_state(state);
3217 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003218 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219}
3220
Trond Myklebust2b484292010-09-17 10:56:51 -04003221static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003222nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3223 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003226 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3227
3228 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Trond Myklebust565277f2007-10-15 18:17:53 -04003230 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003231 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003232
3233 nfs4_label_release_security(label);
3234
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003235 if (IS_ERR(state))
3236 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003237 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
Trond Myklebust1185a552009-12-03 15:54:02 -05003240static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003241{
3242 if (ctx->state == NULL)
3243 return;
3244 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003245 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003246 else
Al Viro643168c2011-06-22 18:20:23 -04003247 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Trond Myklebustb944dba2013-11-04 15:20:20 -05003250#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3251#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003252#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3255{
Kinglong Mee8c612822015-08-26 21:12:58 +08003256 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003257 struct nfs4_server_caps_arg args = {
3258 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003259 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003260 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 struct nfs4_server_caps_res res = {};
3262 struct rpc_message msg = {
3263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003264 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 .rpc_resp = &res,
3266 };
3267 int status;
3268
Kinglong Mee8c612822015-08-26 21:12:58 +08003269 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3270 FATTR4_WORD0_FH_EXPIRE_TYPE |
3271 FATTR4_WORD0_LINK_SUPPORT |
3272 FATTR4_WORD0_SYMLINK_SUPPORT |
3273 FATTR4_WORD0_ACLSUPPORT;
3274 if (minorversion)
3275 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3276
Bryan Schumaker7c513052011-03-24 17:12:24 +00003277 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003279 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003280 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003281 case 0:
3282 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3283 res.attr_bitmask[2] = 0;
3284 break;
3285 case 1:
3286 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3287 break;
3288 case 2:
3289 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003292 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3293 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3294 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3295 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003296 NFS_CAP_CTIME|NFS_CAP_MTIME|
3297 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003298 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3299 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 server->caps |= NFS_CAP_ACLS;
3301 if (res.has_links != 0)
3302 server->caps |= NFS_CAP_HARDLINKS;
3303 if (res.has_symlinks != 0)
3304 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003305 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3306 server->caps |= NFS_CAP_FILEID;
3307 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3308 server->caps |= NFS_CAP_MODE;
3309 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3310 server->caps |= NFS_CAP_NLINK;
3311 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3312 server->caps |= NFS_CAP_OWNER;
3313 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3314 server->caps |= NFS_CAP_OWNER_GROUP;
3315 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3316 server->caps |= NFS_CAP_ATIME;
3317 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3318 server->caps |= NFS_CAP_CTIME;
3319 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3320 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003321#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3322 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3323 server->caps |= NFS_CAP_SECURITY_LABEL;
3324#endif
3325 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3326 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003327 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003328
Trond Myklebusta65318b2009-03-11 14:10:28 -04003329 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3330 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3331 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003332 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003333 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3334 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003336 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 return status;
3340}
3341
Trond Myklebust55a97592006-06-09 09:34:19 -04003342int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
3344 struct nfs4_exception exception = { };
3345 int err;
3346 do {
3347 err = nfs4_handle_exception(server,
3348 _nfs4_server_capabilities(server, fhandle),
3349 &exception);
3350 } while (exception.retry);
3351 return err;
3352}
3353
3354static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3355 struct nfs_fsinfo *info)
3356{
David Quigleyaa9c2662013-05-22 12:50:44 -04003357 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003359 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 };
3361 struct nfs4_lookup_res res = {
3362 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003363 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 .fh = fhandle,
3365 };
3366 struct rpc_message msg = {
3367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3368 .rpc_argp = &args,
3369 .rpc_resp = &res,
3370 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003371
David Quigleyaa9c2662013-05-22 12:50:44 -04003372 bitmask[0] = nfs4_fattr_bitmap[0];
3373 bitmask[1] = nfs4_fattr_bitmap[1];
3374 /*
3375 * Process the label in the upcoming getfattr
3376 */
3377 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3378
Trond Myklebust0e574af2005-10-27 22:12:38 -04003379 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003380 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381}
3382
3383static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3384 struct nfs_fsinfo *info)
3385{
3386 struct nfs4_exception exception = { };
3387 int err;
3388 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003389 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003390 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003391 switch (err) {
3392 case 0:
3393 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003394 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003395 default:
3396 err = nfs4_handle_exception(server, err, &exception);
3397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003399out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 return err;
3401}
3402
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003403static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3404 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3405{
Trond Myklebustc2190662013-08-26 19:23:04 -04003406 struct rpc_auth_create_args auth_args = {
3407 .pseudoflavor = flavor,
3408 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003409 struct rpc_auth *auth;
3410 int ret;
3411
Trond Myklebustc2190662013-08-26 19:23:04 -04003412 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003413 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003414 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003415 goto out;
3416 }
3417 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003418out:
3419 return ret;
3420}
3421
Chuck Lever9a744ba2013-03-16 15:56:02 -04003422/*
3423 * Retry pseudoroot lookup with various security flavors. We do this when:
3424 *
3425 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3426 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3427 *
3428 * Returns zero on success, or a negative NFS4ERR value, or a
3429 * negative errno value.
3430 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003431static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003432 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003434 /* Per 3530bis 15.33.5 */
3435 static const rpc_authflavor_t flav_array[] = {
3436 RPC_AUTH_GSS_KRB5P,
3437 RPC_AUTH_GSS_KRB5I,
3438 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003439 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003440 RPC_AUTH_NULL,
3441 };
3442 int status = -EPERM;
3443 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003445 if (server->auth_info.flavor_len > 0) {
3446 /* try each flavor specified by user */
3447 for (i = 0; i < server->auth_info.flavor_len; i++) {
3448 status = nfs4_lookup_root_sec(server, fhandle, info,
3449 server->auth_info.flavors[i]);
3450 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3451 continue;
3452 break;
3453 }
3454 } else {
3455 /* no flavors specified by user, try default list */
3456 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3457 status = nfs4_lookup_root_sec(server, fhandle, info,
3458 flav_array[i]);
3459 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3460 continue;
3461 break;
3462 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003463 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003464
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003465 /*
3466 * -EACCESS could mean that the user doesn't have correct permissions
3467 * to access the mount. It could also mean that we tried to mount
3468 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3469 * existing mount programs don't handle -EACCES very well so it should
3470 * be mapped to -EPERM instead.
3471 */
3472 if (status == -EACCES)
3473 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003474 return status;
3475}
3476
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003477/**
3478 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3479 * @server: initialized nfs_server handle
3480 * @fhandle: we fill in the pseudo-fs root file handle
3481 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003482 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003483 *
3484 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003485 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003486int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003487 struct nfs_fsinfo *info,
3488 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003489{
Andre Przywarac7757072015-04-23 17:17:40 +01003490 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003491
Andre Przywarac7757072015-04-23 17:17:40 +01003492 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003493 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003494
3495 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003496 status = server->nfs_client->cl_mvops->find_root_sec(server,
3497 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (status == 0)
3500 status = nfs4_server_capabilities(server, fhandle);
3501 if (status == 0)
3502 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003503
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003504 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505}
3506
Bryan Schumakerbae36242012-05-10 15:07:31 -04003507static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3508 struct nfs_fsinfo *info)
3509{
3510 int error;
3511 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003512 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003513
3514 error = nfs4_server_capabilities(server, mntfh);
3515 if (error < 0) {
3516 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3517 return error;
3518 }
3519
David Quigley14c43f72013-05-22 12:50:43 -04003520 label = nfs4_label_alloc(server, GFP_KERNEL);
3521 if (IS_ERR(label))
3522 return PTR_ERR(label);
3523
David Quigley1775fd32013-05-22 12:50:42 -04003524 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003525 if (error < 0) {
3526 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003527 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003528 }
3529
3530 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3531 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3532 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3533
David Quigley14c43f72013-05-22 12:50:43 -04003534err_free_label:
3535 nfs4_label_free(label);
3536
Bryan Schumakerbae36242012-05-10 15:07:31 -04003537 return error;
3538}
3539
Manoj Naik6b97fd32006-06-09 09:34:29 -04003540/*
3541 * Get locations and (maybe) other attributes of a referral.
3542 * Note that we'll actually follow the referral later when
3543 * we detect fsid mismatch in inode revalidation
3544 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003545static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3546 const struct qstr *name, struct nfs_fattr *fattr,
3547 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003548{
3549 int status = -ENOMEM;
3550 struct page *page = NULL;
3551 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003552
3553 page = alloc_page(GFP_KERNEL);
3554 if (page == NULL)
3555 goto out;
3556 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3557 if (locations == NULL)
3558 goto out;
3559
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003560 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003561 if (status != 0)
3562 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003563
3564 /*
3565 * If the fsid didn't change, this is a migration event, not a
3566 * referral. Cause us to drop into the exception handler, which
3567 * will kick off migration recovery.
3568 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003569 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003570 dprintk("%s: server did not return a different fsid for"
3571 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003572 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003573 goto out;
3574 }
Andy Adamson533eb462011-06-13 18:25:56 -04003575 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3576 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003577
Andy Adamson533eb462011-06-13 18:25:56 -04003578 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003579 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003580 memset(fhandle, 0, sizeof(struct nfs_fh));
3581out:
3582 if (page)
3583 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003584 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003585 return status;
3586}
3587
David Quigley1775fd32013-05-22 12:50:42 -04003588static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3589 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
3591 struct nfs4_getattr_arg args = {
3592 .fh = fhandle,
3593 .bitmask = server->attr_bitmask,
3594 };
3595 struct nfs4_getattr_res res = {
3596 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003597 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 .server = server,
3599 };
3600 struct rpc_message msg = {
3601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3602 .rpc_argp = &args,
3603 .rpc_resp = &res,
3604 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003605
3606 args.bitmask = nfs4_bitmask(server, label);
3607
Trond Myklebust0e574af2005-10-27 22:12:38 -04003608 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003609 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
David Quigley1775fd32013-05-22 12:50:42 -04003612static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3613 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614{
3615 struct nfs4_exception exception = { };
3616 int err;
3617 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003618 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3619 trace_nfs4_getattr(server, fhandle, fattr, err);
3620 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 &exception);
3622 } while (exception.retry);
3623 return err;
3624}
3625
3626/*
3627 * The file is not closed if it is opened due to the a request to change
3628 * the size of the file. The open call will not be needed once the
3629 * VFS layer lookup-intents are implemented.
3630 *
3631 * Close is called when the inode is destroyed.
3632 * If we haven't opened the file for O_WRONLY, we
3633 * need to in the size_change case to obtain a stateid.
3634 *
3635 * Got race?
3636 * Because OPEN is always done by name in nfsv4, it is
3637 * possible that we opened a different file by the same
3638 * name. We can recognize this race condition, but we
3639 * can't do anything about it besides returning an error.
3640 *
3641 * This will be fixed with VFS changes (lookup-intent).
3642 */
3643static int
3644nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3645 struct iattr *sattr)
3646{
David Howells2b0143b2015-03-17 22:25:59 +00003647 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003648 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003649 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003650 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 int status;
3652
Peng Tao88ac8152014-09-12 11:04:10 +08003653 if (pnfs_ld_layoutret_on_setattr(inode) &&
3654 sattr->ia_valid & ATTR_SIZE &&
3655 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003656 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003657
Trond Myklebust0e574af2005-10-27 22:12:38 -04003658 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Andy Adamson26699402012-05-30 16:12:24 -04003660 /* Deal with open(O_TRUNC) */
3661 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003662 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003663
3664 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003665 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003666 return 0;
3667
Trond Myklebustd5308382005-11-04 15:33:38 -05003668 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003669 if (sattr->ia_valid & ATTR_FILE) {
3670 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003671
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003672 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003673 if (ctx) {
3674 cred = ctx->cred;
3675 state = ctx->state;
3676 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003677 }
3678
David Quigley14c43f72013-05-22 12:50:43 -04003679 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3680 if (IS_ERR(label))
3681 return PTR_ERR(label);
3682
3683 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003684 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003685 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003686 nfs_setsecurity(inode, fattr, label);
3687 }
David Quigley14c43f72013-05-22 12:50:43 -04003688 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return status;
3690}
3691
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003692static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3693 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003694 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003695{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003696 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003697 int status;
3698 struct nfs4_lookup_arg args = {
3699 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003700 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003701 .name = name,
3702 };
3703 struct nfs4_lookup_res res = {
3704 .server = server,
3705 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003706 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003707 .fh = fhandle,
3708 };
3709 struct rpc_message msg = {
3710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3711 .rpc_argp = &args,
3712 .rpc_resp = &res,
3713 };
3714
David Quigleyaa9c2662013-05-22 12:50:44 -04003715 args.bitmask = nfs4_bitmask(server, label);
3716
David Howells2b3de442006-08-22 20:06:09 -04003717 nfs_fattr_init(fattr);
3718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003720 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 dprintk("NFS reply lookup: %d\n", status);
3722 return status;
3723}
3724
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003725static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003726{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003727 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003728 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003729 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3730 fattr->nlink = 2;
3731}
3732
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003733static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003734 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003735 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003736{
3737 struct nfs4_exception exception = { };
3738 struct rpc_clnt *client = *clnt;
3739 int err;
3740 do {
David Quigley1775fd32013-05-22 12:50:42 -04003741 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003742 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003743 switch (err) {
3744 case -NFS4ERR_BADNAME:
3745 err = -ENOENT;
3746 goto out;
3747 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003748 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003749 if (err == -NFS4ERR_MOVED)
3750 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003751 goto out;
3752 case -NFS4ERR_WRONGSEC:
3753 err = -EPERM;
3754 if (client != *clnt)
3755 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003756 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003757 if (IS_ERR(client))
3758 return PTR_ERR(client);
3759
3760 exception.retry = 1;
3761 break;
3762 default:
3763 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3764 }
3765 } while (exception.retry);
3766
3767out:
3768 if (err == 0)
3769 *clnt = client;
3770 else if (client != *clnt)
3771 rpc_shutdown_client(client);
3772
3773 return err;
3774}
3775
Al Virobeffb8f2016-07-20 16:34:42 -04003776static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003777 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3778 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003780 int status;
3781 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003782
David Quigley1775fd32013-05-22 12:50:42 -04003783 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003784 if (client != NFS_CLIENT(dir)) {
3785 rpc_shutdown_client(client);
3786 nfs_fixup_secinfo_attributes(fattr);
3787 }
3788 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789}
3790
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003791struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003792nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003793 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3794{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003795 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003796 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003797
David Quigley1775fd32013-05-22 12:50:42 -04003798 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003799 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003800 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003801 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003802}
3803
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3805{
Trond Myklebust76b32992007-08-10 17:45:11 -04003806 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 struct nfs4_accessargs args = {
3808 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003809 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003811 struct nfs4_accessres res = {
3812 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003813 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 struct rpc_message msg = {
3815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3816 .rpc_argp = &args,
3817 .rpc_resp = &res,
3818 .rpc_cred = entry->cred,
3819 };
3820 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003821 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
3823 /*
3824 * Determine which access bits we want to ask for...
3825 */
3826 if (mode & MAY_READ)
3827 args.access |= NFS4_ACCESS_READ;
3828 if (S_ISDIR(inode->i_mode)) {
3829 if (mode & MAY_WRITE)
3830 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3831 if (mode & MAY_EXEC)
3832 args.access |= NFS4_ACCESS_LOOKUP;
3833 } else {
3834 if (mode & MAY_WRITE)
3835 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3836 if (mode & MAY_EXEC)
3837 args.access |= NFS4_ACCESS_EXECUTE;
3838 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003839
3840 res.fattr = nfs_alloc_fattr();
3841 if (res.fattr == NULL)
3842 return -ENOMEM;
3843
Bryan Schumaker7c513052011-03-24 17:12:24 +00003844 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003846 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003847 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003849 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 return status;
3851}
3852
3853static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3854{
3855 struct nfs4_exception exception = { };
3856 int err;
3857 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003858 err = _nfs4_proc_access(inode, entry);
3859 trace_nfs4_access(inode, err);
3860 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 &exception);
3862 } while (exception.retry);
3863 return err;
3864}
3865
3866/*
3867 * TODO: For the time being, we don't try to get any attributes
3868 * along with any of the zero-copy operations READ, READDIR,
3869 * READLINK, WRITE.
3870 *
3871 * In the case of the first three, we want to put the GETATTR
3872 * after the read-type operation -- this is because it is hard
3873 * to predict the length of a GETATTR response in v4, and thus
3874 * align the READ data correctly. This means that the GETATTR
3875 * may end up partially falling into the page cache, and we should
3876 * shift it into the 'tail' of the xdr_buf before processing.
3877 * To do this efficiently, we need to know the total length
3878 * of data received, which doesn't seem to be available outside
3879 * of the RPC layer.
3880 *
3881 * In the case of WRITE, we also want to put the GETATTR after
3882 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003883 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 *
3885 * Both of these changes to the XDR layer would in fact be quite
3886 * minor, but I decided to leave them for a subsequent patch.
3887 */
3888static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3889 unsigned int pgbase, unsigned int pglen)
3890{
3891 struct nfs4_readlink args = {
3892 .fh = NFS_FH(inode),
3893 .pgbase = pgbase,
3894 .pglen = pglen,
3895 .pages = &page,
3896 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003897 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 struct rpc_message msg = {
3899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3900 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003901 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 };
3903
Bryan Schumaker7c513052011-03-24 17:12:24 +00003904 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 -07003905}
3906
3907static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3908 unsigned int pgbase, unsigned int pglen)
3909{
3910 struct nfs4_exception exception = { };
3911 int err;
3912 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003913 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3914 trace_nfs4_readlink(inode, err);
3915 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 &exception);
3917 } while (exception.retry);
3918 return err;
3919}
3920
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003922 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924static int
3925nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003926 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
David Quigleyaa9c2662013-05-22 12:50:44 -04003928 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003929 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 int status = 0;
3932
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003933 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3934 if (IS_ERR(ctx))
3935 return PTR_ERR(ctx);
3936
David Quigleyaa9c2662013-05-22 12:50:44 -04003937 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3938
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003939 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003940 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 if (IS_ERR(state)) {
3942 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003946 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003947 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 return status;
3949}
3950
Al Virobeffb8f2016-07-20 16:34:42 -04003951static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952{
Trond Myklebust16e42952005-10-27 22:12:44 -04003953 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003954 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003956 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003958 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003959 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003960 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3963 .rpc_argp = &args,
3964 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003966 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003967
Bryan Schumaker7c513052011-03-24 17:12:24 +00003968 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003969 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003970 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 return status;
3972}
3973
Al Virobeffb8f2016-07-20 16:34:42 -04003974static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975{
3976 struct nfs4_exception exception = { };
3977 int err;
3978 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003979 err = _nfs4_proc_remove(dir, name);
3980 trace_nfs4_remove(dir, name, err);
3981 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 &exception);
3983 } while (exception.retry);
3984 return err;
3985}
3986
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003987static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003989 struct nfs_server *server = NFS_SERVER(dir);
3990 struct nfs_removeargs *args = msg->rpc_argp;
3991 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003993 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003995 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003996
3997 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998}
3999
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004000static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4001{
Al Viro884be172016-04-28 23:56:31 -04004002 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004003 &data->args.seq_args,
4004 &data->res.seq_res,
4005 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004008static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004010 struct nfs_unlinkdata *data = task->tk_calldata;
4011 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004012
Trond Myklebust14516c32010-07-31 14:29:06 -04004013 if (!nfs4_sequence_done(task, &res->seq_res))
4014 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004015 if (nfs4_async_handle_error(task, res->server, NULL,
4016 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004017 return 0;
4018 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004019 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020}
4021
Jeff Laytond3d41522010-09-17 17:31:57 -04004022static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4023{
4024 struct nfs_server *server = NFS_SERVER(dir);
4025 struct nfs_renameargs *arg = msg->rpc_argp;
4026 struct nfs_renameres *res = msg->rpc_resp;
4027
4028 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004029 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004030 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004031}
4032
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004033static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4034{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004035 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4036 &data->args.seq_args,
4037 &data->res.seq_res,
4038 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004039}
4040
4041static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4042 struct inode *new_dir)
4043{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004044 struct nfs_renamedata *data = task->tk_calldata;
4045 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004046
4047 if (!nfs4_sequence_done(task, &res->seq_res))
4048 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004049 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004050 return 0;
4051
4052 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004053 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004054 return 1;
4055}
4056
Al Virobeffb8f2016-07-20 16:34:42 -04004057static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004059 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 struct nfs4_link_arg arg = {
4061 .fh = NFS_FH(inode),
4062 .dir_fh = NFS_FH(dir),
4063 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004064 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004066 struct nfs4_link_res res = {
4067 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004068 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004069 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 struct rpc_message msg = {
4071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4072 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004073 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004075 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Trond Myklebust136f2622010-04-16 16:22:49 -04004077 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004078 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004079 goto out;
4080
David Quigley14c43f72013-05-22 12:50:43 -04004081 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4082 if (IS_ERR(res.label)) {
4083 status = PTR_ERR(res.label);
4084 goto out;
4085 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004086 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004087
Bryan Schumaker7c513052011-03-24 17:12:24 +00004088 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004089 if (!status) {
4090 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004091 status = nfs_post_op_update_inode(inode, res.fattr);
4092 if (!status)
4093 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004094 }
David Quigley14c43f72013-05-22 12:50:43 -04004095
4096
4097 nfs4_label_free(res.label);
4098
Trond Myklebust136f2622010-04-16 16:22:49 -04004099out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004100 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 return status;
4102}
4103
Al Virobeffb8f2016-07-20 16:34:42 -04004104static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
4106 struct nfs4_exception exception = { };
4107 int err;
4108 do {
4109 err = nfs4_handle_exception(NFS_SERVER(inode),
4110 _nfs4_proc_link(inode, dir, name),
4111 &exception);
4112 } while (exception.retry);
4113 return err;
4114}
4115
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004116struct nfs4_createdata {
4117 struct rpc_message msg;
4118 struct nfs4_create_arg arg;
4119 struct nfs4_create_res res;
4120 struct nfs_fh fh;
4121 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004122 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004123};
4124
4125static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004126 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004127{
4128 struct nfs4_createdata *data;
4129
4130 data = kzalloc(sizeof(*data), GFP_KERNEL);
4131 if (data != NULL) {
4132 struct nfs_server *server = NFS_SERVER(dir);
4133
David Quigley14c43f72013-05-22 12:50:43 -04004134 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4135 if (IS_ERR(data->label))
4136 goto out_free;
4137
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004138 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4139 data->msg.rpc_argp = &data->arg;
4140 data->msg.rpc_resp = &data->res;
4141 data->arg.dir_fh = NFS_FH(dir);
4142 data->arg.server = server;
4143 data->arg.name = name;
4144 data->arg.attrs = sattr;
4145 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004146 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004147 data->res.server = server;
4148 data->res.fh = &data->fh;
4149 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004150 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004151 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004152 }
4153 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004154out_free:
4155 kfree(data);
4156 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004157}
4158
4159static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4160{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004161 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004162 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004163 if (status == 0) {
4164 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004165 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004166 }
4167 return status;
4168}
4169
4170static void nfs4_free_createdata(struct nfs4_createdata *data)
4171{
David Quigley14c43f72013-05-22 12:50:43 -04004172 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004173 kfree(data);
4174}
4175
Chuck Lever4f390c12006-08-22 20:06:22 -04004176static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004177 struct page *page, unsigned int len, struct iattr *sattr,
4178 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004180 struct nfs4_createdata *data;
4181 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Chuck Lever94a6d752006-08-22 20:06:23 -04004183 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004184 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004185
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004186 status = -ENOMEM;
4187 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4188 if (data == NULL)
4189 goto out;
4190
4191 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4192 data->arg.u.symlink.pages = &page;
4193 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004194 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004196 status = nfs4_do_create(dir, dentry, data);
4197
4198 nfs4_free_createdata(data);
4199out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 return status;
4201}
4202
Chuck Lever4f390c12006-08-22 20:06:22 -04004203static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004204 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
4206 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004207 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004209
4210 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004213 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4214 trace_nfs4_symlink(dir, &dentry->d_name, err);
4215 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 &exception);
4217 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004218
4219 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return err;
4221}
4222
4223static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004224 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004226 struct nfs4_createdata *data;
4227 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004229 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4230 if (data == NULL)
4231 goto out;
4232
David Quigley1775fd32013-05-22 12:50:42 -04004233 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004234 status = nfs4_do_create(dir, dentry, data);
4235
4236 nfs4_free_createdata(data);
4237out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 return status;
4239}
4240
4241static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4242 struct iattr *sattr)
4243{
4244 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004245 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004247
David Quigleyaa9c2662013-05-22 12:50:44 -04004248 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4249
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004250 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004252 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4253 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4254 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 &exception);
4256 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004257 nfs4_label_release_security(label);
4258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 return err;
4260}
4261
4262static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004263 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
David Howells2b0143b2015-03-17 22:25:59 +00004265 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 struct nfs4_readdir_arg args = {
4267 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004268 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 .pgbase = 0,
4270 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004271 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004272 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 };
4274 struct nfs4_readdir_res res;
4275 struct rpc_message msg = {
4276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4277 .rpc_argp = &args,
4278 .rpc_resp = &res,
4279 .rpc_cred = cred,
4280 };
4281 int status;
4282
Al Viro6de14722013-09-16 10:53:17 -04004283 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4284 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004285 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004286 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004288 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 -05004289 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004290 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004291 status += args.pgbase;
4292 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004293
4294 nfs_invalidate_atime(dir);
4295
Harvey Harrison3110ff82008-05-02 13:42:44 -07004296 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 return status;
4298}
4299
4300static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004301 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
4303 struct nfs4_exception exception = { };
4304 int err;
4305 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004306 err = _nfs4_proc_readdir(dentry, cred, cookie,
4307 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004308 trace_nfs4_readdir(d_inode(dentry), err);
4309 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 &exception);
4311 } while (exception.retry);
4312 return err;
4313}
4314
4315static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004316 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004318 struct nfs4_createdata *data;
4319 int mode = sattr->ia_mode;
4320 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004322 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4323 if (data == NULL)
4324 goto out;
4325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004327 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004329 data->arg.ftype = NF4BLK;
4330 data->arg.u.device.specdata1 = MAJOR(rdev);
4331 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 }
4333 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004334 data->arg.ftype = NF4CHR;
4335 data->arg.u.device.specdata1 = MAJOR(rdev);
4336 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004337 } else if (!S_ISSOCK(mode)) {
4338 status = -EINVAL;
4339 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 }
David Quigley1775fd32013-05-22 12:50:42 -04004341
David Quigleyaa9c2662013-05-22 12:50:44 -04004342 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004343 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004344out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004345 nfs4_free_createdata(data);
4346out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return status;
4348}
4349
4350static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4351 struct iattr *sattr, dev_t rdev)
4352{
4353 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004354 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004356
David Quigleyaa9c2662013-05-22 12:50:44 -04004357 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4358
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004359 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004361 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4362 trace_nfs4_mknod(dir, &dentry->d_name, err);
4363 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 &exception);
4365 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004366
4367 nfs4_label_release_security(label);
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 return err;
4370}
4371
4372static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4373 struct nfs_fsstat *fsstat)
4374{
4375 struct nfs4_statfs_arg args = {
4376 .fh = fhandle,
4377 .bitmask = server->attr_bitmask,
4378 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004379 struct nfs4_statfs_res res = {
4380 .fsstat = fsstat,
4381 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 struct rpc_message msg = {
4383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4384 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004385 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 };
4387
Trond Myklebust0e574af2005-10-27 22:12:38 -04004388 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004389 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390}
4391
4392static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4393{
4394 struct nfs4_exception exception = { };
4395 int err;
4396 do {
4397 err = nfs4_handle_exception(server,
4398 _nfs4_proc_statfs(server, fhandle, fsstat),
4399 &exception);
4400 } while (exception.retry);
4401 return err;
4402}
4403
4404static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4405 struct nfs_fsinfo *fsinfo)
4406{
4407 struct nfs4_fsinfo_arg args = {
4408 .fh = fhandle,
4409 .bitmask = server->attr_bitmask,
4410 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004411 struct nfs4_fsinfo_res res = {
4412 .fsinfo = fsinfo,
4413 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 struct rpc_message msg = {
4415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4416 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004417 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 };
4419
Bryan Schumaker7c513052011-03-24 17:12:24 +00004420 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421}
4422
4423static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4424{
4425 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004426 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 int err;
4428
4429 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004430 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004431 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004432 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004433 nfs4_set_lease_period(server->nfs_client,
4434 fsinfo->lease_time * HZ,
4435 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004436 break;
4437 }
4438 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 } while (exception.retry);
4440 return err;
4441}
4442
4443static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4444{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004445 int error;
4446
Trond Myklebust0e574af2005-10-27 22:12:38 -04004447 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004448 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004449 if (error == 0) {
4450 /* block layout checks this! */
4451 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004452 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004453 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004454
4455 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456}
4457
4458static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4459 struct nfs_pathconf *pathconf)
4460{
4461 struct nfs4_pathconf_arg args = {
4462 .fh = fhandle,
4463 .bitmask = server->attr_bitmask,
4464 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004465 struct nfs4_pathconf_res res = {
4466 .pathconf = pathconf,
4467 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 struct rpc_message msg = {
4469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4470 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004471 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 };
4473
4474 /* None of the pathconf attributes are mandatory to implement */
4475 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4476 memset(pathconf, 0, sizeof(*pathconf));
4477 return 0;
4478 }
4479
Trond Myklebust0e574af2005-10-27 22:12:38 -04004480 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004481 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482}
4483
4484static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4485 struct nfs_pathconf *pathconf)
4486{
4487 struct nfs4_exception exception = { };
4488 int err;
4489
4490 do {
4491 err = nfs4_handle_exception(server,
4492 _nfs4_proc_pathconf(server, fhandle, pathconf),
4493 &exception);
4494 } while (exception.retry);
4495 return err;
4496}
4497
Trond Myklebust5521abf2013-03-16 20:54:34 -04004498int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004499 const struct nfs_open_context *ctx,
4500 const struct nfs_lock_context *l_ctx,
4501 fmode_t fmode)
4502{
4503 const struct nfs_lockowner *lockowner = NULL;
4504
4505 if (l_ctx != NULL)
4506 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004507 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004508}
4509EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4510
Trond Myklebust5521abf2013-03-16 20:54:34 -04004511static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4512 const struct nfs_open_context *ctx,
4513 const struct nfs_lock_context *l_ctx,
4514 fmode_t fmode)
4515{
4516 nfs4_stateid current_stateid;
4517
Trond Myklebuste1253be2014-03-05 08:44:23 -05004518 /* If the current stateid represents a lost lock, then exit */
4519 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4520 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004521 return nfs4_stateid_match(stateid, &current_stateid);
4522}
4523
4524static bool nfs4_error_stateid_expired(int err)
4525{
4526 switch (err) {
4527 case -NFS4ERR_DELEG_REVOKED:
4528 case -NFS4ERR_ADMIN_REVOKED:
4529 case -NFS4ERR_BAD_STATEID:
4530 case -NFS4ERR_STALE_STATEID:
4531 case -NFS4ERR_OLD_STATEID:
4532 case -NFS4ERR_OPENMODE:
4533 case -NFS4ERR_EXPIRED:
4534 return true;
4535 }
4536 return false;
4537}
4538
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004539void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004540{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004541 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004542}
4543
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004544static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004546 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004548 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004549 if (task->tk_status < 0) {
4550 struct nfs4_exception exception = {
4551 .inode = hdr->inode,
4552 .state = hdr->args.context->state,
4553 .stateid = &hdr->args.stateid,
4554 };
4555 task->tk_status = nfs4_async_handle_exception(task,
4556 server, task->tk_status, &exception);
4557 if (exception.retry) {
4558 rpc_restart_call_prepare(task);
4559 return -EAGAIN;
4560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004562
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004563 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004565 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
4568
Trond Myklebust5521abf2013-03-16 20:54:34 -04004569static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004570 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004571{
4572
4573 if (!nfs4_error_stateid_expired(task->tk_status) ||
4574 nfs4_stateid_is_current(&args->stateid,
4575 args->context,
4576 args->lock_context,
4577 FMODE_READ))
4578 return false;
4579 rpc_restart_call_prepare(task);
4580 return true;
4581}
4582
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004583static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004584{
4585
4586 dprintk("--> %s\n", __func__);
4587
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004588 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004589 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004590 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004591 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004592 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4593 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004594}
4595
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004596static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4597 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004599 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004600 if (!hdr->pgio_done_cb)
4601 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004602 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004603 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004606static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4607 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004608{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004609 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4610 &hdr->args.seq_args,
4611 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004612 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004613 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004614 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4615 hdr->args.lock_context,
4616 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004617 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004618 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004619 return -EIO;
4620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621}
4622
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623static int nfs4_write_done_cb(struct rpc_task *task,
4624 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004626 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004627
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004628 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004629 if (task->tk_status < 0) {
4630 struct nfs4_exception exception = {
4631 .inode = hdr->inode,
4632 .state = hdr->args.context->state,
4633 .stateid = &hdr->args.stateid,
4634 };
4635 task->tk_status = nfs4_async_handle_exception(task,
4636 NFS_SERVER(inode), task->tk_status,
4637 &exception);
4638 if (exception.retry) {
4639 rpc_restart_call_prepare(task);
4640 return -EAGAIN;
4641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004643 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004644 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004645 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004646 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648}
4649
Trond Myklebust5521abf2013-03-16 20:54:34 -04004650static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004651 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004652{
4653
4654 if (!nfs4_error_stateid_expired(task->tk_status) ||
4655 nfs4_stateid_is_current(&args->stateid,
4656 args->context,
4657 args->lock_context,
4658 FMODE_WRITE))
4659 return false;
4660 rpc_restart_call_prepare(task);
4661 return true;
4662}
4663
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004664static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004665{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004666 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004667 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004669 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004670 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4671 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004672}
4673
Trond Myklebust5a37f852012-04-28 14:55:16 -04004674static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004676{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004677 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004678 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004679 return false;
4680 /* Otherwise, request attributes if and only if we don't hold
4681 * a delegation
4682 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004683 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004684}
Fred Isamana69aef12011-03-03 15:13:47 +00004685
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004686static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4687 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004689 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004690
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4692 hdr->args.bitmask = NULL;
4693 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004694 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004695 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004696
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697 if (!hdr->pgio_done_cb)
4698 hdr->pgio_done_cb = nfs4_write_done_cb;
4699 hdr->res.server = server;
4700 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004702 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704}
4705
Fred Isaman0b7c0152012-04-20 14:47:39 -04004706static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4707{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004708 nfs4_setup_sequence(NFS_SERVER(data->inode),
4709 &data->args.seq_args,
4710 &data->res.seq_res,
4711 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
Fred Isaman0b7c0152012-04-20 14:47:39 -04004714static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004717
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004718 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004719 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4720 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004721 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004722 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
Fred Isaman0b7c0152012-04-20 14:47:39 -04004727static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004728{
4729 if (!nfs4_sequence_done(task, &data->res.seq_res))
4730 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004731 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004732}
4733
Fred Isaman0b7c0152012-04-20 14:47:39 -04004734static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004736 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004737
Fred Isaman0b7c0152012-04-20 14:47:39 -04004738 if (data->commit_done_cb == NULL)
4739 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004740 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004741 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004742 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004745struct nfs4_renewdata {
4746 struct nfs_client *client;
4747 unsigned long timestamp;
4748};
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750/*
4751 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4752 * standalone procedure for queueing an asynchronous RENEW.
4753 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004754static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004755{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004756 struct nfs4_renewdata *data = calldata;
4757 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004758
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004759 if (atomic_read(&clp->cl_count) > 1)
4760 nfs4_schedule_state_renewal(clp);
4761 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004762 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004763}
4764
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004765static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004767 struct nfs4_renewdata *data = calldata;
4768 struct nfs_client *clp = data->client;
4769 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004771 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004772 switch (task->tk_status) {
4773 case 0:
4774 break;
4775 case -NFS4ERR_LEASE_MOVED:
4776 nfs4_schedule_lease_moved_recovery(clp);
4777 break;
4778 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004779 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004780 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4781 return;
4782 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004783 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004784 return;
4785 }
4786 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004788 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004791static const struct rpc_call_ops nfs4_renew_ops = {
4792 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004793 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004794};
4795
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004796static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
4798 struct rpc_message msg = {
4799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4800 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004801 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004803 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004805 if (renew_flags == 0)
4806 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004807 if (!atomic_inc_not_zero(&clp->cl_count))
4808 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004809 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004810 if (data == NULL)
4811 return -ENOMEM;
4812 data->client = clp;
4813 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004814 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004815 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004818static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819{
4820 struct rpc_message msg = {
4821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4822 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004823 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 };
4825 unsigned long now = jiffies;
4826 int status;
4827
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004828 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 if (status < 0)
4830 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004831 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 return 0;
4833}
4834
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004835static inline int nfs4_server_supports_acls(struct nfs_server *server)
4836{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004837 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004838}
4839
Trond Myklebust21f498c2012-08-24 10:59:25 -04004840/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4841 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004842 * the stack.
4843 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004844#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004845
Neil Hormane9e3d722011-03-04 19:26:03 -05004846static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004847 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004848{
4849 struct page *newpage, **spages;
4850 int rc = 0;
4851 size_t len;
4852 spages = pages;
4853
4854 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004855 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004856 newpage = alloc_page(GFP_KERNEL);
4857
4858 if (newpage == NULL)
4859 goto unwind;
4860 memcpy(page_address(newpage), buf, len);
4861 buf += len;
4862 buflen -= len;
4863 *pages++ = newpage;
4864 rc++;
4865 } while (buflen != 0);
4866
4867 return rc;
4868
4869unwind:
4870 for(; rc > 0; rc--)
4871 __free_page(spages[rc-1]);
4872 return -ENOMEM;
4873}
4874
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004875struct nfs4_cached_acl {
4876 int cached;
4877 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004878 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004879};
4880
4881static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004882{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004883 struct nfs_inode *nfsi = NFS_I(inode);
4884
4885 spin_lock(&inode->i_lock);
4886 kfree(nfsi->nfs4_acl);
4887 nfsi->nfs4_acl = acl;
4888 spin_unlock(&inode->i_lock);
4889}
4890
4891static void nfs4_zap_acl_attr(struct inode *inode)
4892{
4893 nfs4_set_cached_acl(inode, NULL);
4894}
4895
4896static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4897{
4898 struct nfs_inode *nfsi = NFS_I(inode);
4899 struct nfs4_cached_acl *acl;
4900 int ret = -ENOENT;
4901
4902 spin_lock(&inode->i_lock);
4903 acl = nfsi->nfs4_acl;
4904 if (acl == NULL)
4905 goto out;
4906 if (buf == NULL) /* user is just asking for length */
4907 goto out_len;
4908 if (acl->cached == 0)
4909 goto out;
4910 ret = -ERANGE; /* see getxattr(2) man page */
4911 if (acl->len > buflen)
4912 goto out;
4913 memcpy(buf, acl->data, acl->len);
4914out_len:
4915 ret = acl->len;
4916out:
4917 spin_unlock(&inode->i_lock);
4918 return ret;
4919}
4920
Sachin Prabhu5794d212012-04-17 14:36:40 +01004921static 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 +00004922{
4923 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004924 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004925
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004926 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004927 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004928 if (acl == NULL)
4929 goto out;
4930 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004931 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004932 } else {
4933 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4934 if (acl == NULL)
4935 goto out;
4936 acl->cached = 0;
4937 }
4938 acl->len = acl_len;
4939out:
4940 nfs4_set_cached_acl(inode, acl);
4941}
4942
Andy Adamsonbf118a32011-12-07 11:55:27 -05004943/*
4944 * The getxattr API returns the required buffer length when called with a
4945 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4946 * the required buf. On a NULL buf, we send a page of data to the server
4947 * guessing that the ACL request can be serviced by a page. If so, we cache
4948 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4949 * the cache. If not so, we throw away the page, and cache the required
4950 * length. The next getxattr call will then produce another round trip to
4951 * the server, this time with the input buf of the required size.
4952 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004953static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004954{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004955 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004956 struct nfs_getaclargs args = {
4957 .fh = NFS_FH(inode),
4958 .acl_pages = pages,
4959 .acl_len = buflen,
4960 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004961 struct nfs_getaclres res = {
4962 .acl_len = buflen,
4963 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004964 struct rpc_message msg = {
4965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4966 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004967 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004968 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004969 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4970 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004971
Andy Adamsonbf118a32011-12-07 11:55:27 -05004972 /* As long as we're doing a round trip to the server anyway,
4973 * let's be prepared for a page of acl data. */
4974 if (npages == 0)
4975 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004976 if (npages > ARRAY_SIZE(pages))
4977 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004978
Andy Adamsonbf118a32011-12-07 11:55:27 -05004979 for (i = 0; i < npages; i++) {
4980 pages[i] = alloc_page(GFP_KERNEL);
4981 if (!pages[i])
4982 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004983 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004984
4985 /* for decoding across pages */
4986 res.acl_scratch = alloc_page(GFP_KERNEL);
4987 if (!res.acl_scratch)
4988 goto out_free;
4989
Andy Adamsonbf118a32011-12-07 11:55:27 -05004990 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004991
Peng Taode040be2012-01-10 22:42:47 +08004992 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004993 __func__, buf, buflen, npages, args.acl_len);
4994 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4995 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004996 if (ret)
4997 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004998
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004999 /* Handle the case where the passed-in buffer is too short */
5000 if (res.acl_flags & NFS4_ACL_TRUNC) {
5001 /* Did the user only issue a request for the acl length? */
5002 if (buf == NULL)
5003 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005004 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005005 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005006 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005007 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005008 if (buf) {
5009 if (res.acl_len > buflen) {
5010 ret = -ERANGE;
5011 goto out_free;
5012 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005013 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005014 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005015out_ok:
5016 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005017out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005018 for (i = 0; i < npages; i++)
5019 if (pages[i])
5020 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005021 if (res.acl_scratch)
5022 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005023 return ret;
5024}
5025
Trond Myklebust16b42892006-08-24 12:27:15 -04005026static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5027{
5028 struct nfs4_exception exception = { };
5029 ssize_t ret;
5030 do {
5031 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005032 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005033 if (ret >= 0)
5034 break;
5035 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5036 } while (exception.retry);
5037 return ret;
5038}
5039
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005040static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5041{
5042 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005043 int ret;
5044
5045 if (!nfs4_server_supports_acls(server))
5046 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005047 ret = nfs_revalidate_inode(server, inode);
5048 if (ret < 0)
5049 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005050 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5051 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005052 ret = nfs4_read_cached_acl(inode, buf, buflen);
5053 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005054 /* -ENOENT is returned if there is no ACL or if there is an ACL
5055 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005056 return ret;
5057 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005058}
5059
Trond Myklebust16b42892006-08-24 12:27:15 -04005060static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005061{
5062 struct nfs_server *server = NFS_SERVER(inode);
5063 struct page *pages[NFS4ACL_MAXPAGES];
5064 struct nfs_setaclargs arg = {
5065 .fh = NFS_FH(inode),
5066 .acl_pages = pages,
5067 .acl_len = buflen,
5068 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005069 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005070 struct rpc_message msg = {
5071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5072 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005073 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005074 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005075 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005076 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005077
5078 if (!nfs4_server_supports_acls(server))
5079 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005080 if (npages > ARRAY_SIZE(pages))
5081 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005082 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005083 if (i < 0)
5084 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005085 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005086 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005087
5088 /*
5089 * Free each page after tx, so the only ref left is
5090 * held by the network stack
5091 */
5092 for (; i > 0; i--)
5093 put_page(pages[i-1]);
5094
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005095 /*
5096 * Acl update can result in inode attribute update.
5097 * so mark the attribute cache invalid.
5098 */
5099 spin_lock(&inode->i_lock);
5100 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5101 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005102 nfs_access_zap_cache(inode);
5103 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005104 return ret;
5105}
5106
Trond Myklebust16b42892006-08-24 12:27:15 -04005107static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5108{
5109 struct nfs4_exception exception = { };
5110 int err;
5111 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005112 err = __nfs4_proc_set_acl(inode, buf, buflen);
5113 trace_nfs4_set_acl(inode, err);
5114 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005115 &exception);
5116 } while (exception.retry);
5117 return err;
5118}
5119
David Quigleyaa9c2662013-05-22 12:50:44 -04005120#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5121static int _nfs4_get_security_label(struct inode *inode, void *buf,
5122 size_t buflen)
5123{
5124 struct nfs_server *server = NFS_SERVER(inode);
5125 struct nfs_fattr fattr;
5126 struct nfs4_label label = {0, 0, buflen, buf};
5127
5128 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005129 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005130 .fh = NFS_FH(inode),
5131 .bitmask = bitmask,
5132 };
5133 struct nfs4_getattr_res res = {
5134 .fattr = &fattr,
5135 .label = &label,
5136 .server = server,
5137 };
5138 struct rpc_message msg = {
5139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005140 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005141 .rpc_resp = &res,
5142 };
5143 int ret;
5144
5145 nfs_fattr_init(&fattr);
5146
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005147 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005148 if (ret)
5149 return ret;
5150 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5151 return -ENOENT;
5152 if (buflen < label.len)
5153 return -ERANGE;
5154 return 0;
5155}
5156
5157static int nfs4_get_security_label(struct inode *inode, void *buf,
5158 size_t buflen)
5159{
5160 struct nfs4_exception exception = { };
5161 int err;
5162
5163 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5164 return -EOPNOTSUPP;
5165
5166 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005167 err = _nfs4_get_security_label(inode, buf, buflen);
5168 trace_nfs4_get_security_label(inode, err);
5169 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005170 &exception);
5171 } while (exception.retry);
5172 return err;
5173}
5174
5175static int _nfs4_do_set_security_label(struct inode *inode,
5176 struct nfs4_label *ilabel,
5177 struct nfs_fattr *fattr,
5178 struct nfs4_label *olabel)
5179{
5180
5181 struct iattr sattr = {0};
5182 struct nfs_server *server = NFS_SERVER(inode);
5183 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005184 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005185 .fh = NFS_FH(inode),
5186 .iap = &sattr,
5187 .server = server,
5188 .bitmask = bitmask,
5189 .label = ilabel,
5190 };
5191 struct nfs_setattrres res = {
5192 .fattr = fattr,
5193 .label = olabel,
5194 .server = server,
5195 };
5196 struct rpc_message msg = {
5197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005198 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005199 .rpc_resp = &res,
5200 };
5201 int status;
5202
Jeff Layton12207f62013-11-01 10:49:32 -04005203 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005204
Jeff Layton12207f62013-11-01 10:49:32 -04005205 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005206 if (status)
5207 dprintk("%s failed: %d\n", __func__, status);
5208
5209 return status;
5210}
5211
5212static int nfs4_do_set_security_label(struct inode *inode,
5213 struct nfs4_label *ilabel,
5214 struct nfs_fattr *fattr,
5215 struct nfs4_label *olabel)
5216{
5217 struct nfs4_exception exception = { };
5218 int err;
5219
5220 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005221 err = _nfs4_do_set_security_label(inode, ilabel,
5222 fattr, olabel);
5223 trace_nfs4_set_security_label(inode, err);
5224 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005225 &exception);
5226 } while (exception.retry);
5227 return err;
5228}
5229
5230static int
Al Viro59301222016-05-27 10:19:30 -04005231nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005232{
5233 struct nfs4_label ilabel, *olabel = NULL;
5234 struct nfs_fattr fattr;
5235 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005236 int status;
5237
5238 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5239 return -EOPNOTSUPP;
5240
5241 nfs_fattr_init(&fattr);
5242
5243 ilabel.pi = 0;
5244 ilabel.lfs = 0;
5245 ilabel.label = (char *)buf;
5246 ilabel.len = buflen;
5247
5248 cred = rpc_lookup_cred();
5249 if (IS_ERR(cred))
5250 return PTR_ERR(cred);
5251
5252 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5253 if (IS_ERR(olabel)) {
5254 status = -PTR_ERR(olabel);
5255 goto out;
5256 }
5257
5258 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5259 if (status == 0)
5260 nfs_setsecurity(inode, &fattr, olabel);
5261
5262 nfs4_label_free(olabel);
5263out:
5264 put_rpccred(cred);
5265 return status;
5266}
5267#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5268
5269
Chuck Leverf0920752012-05-21 22:45:41 -04005270static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5271 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005272{
5273 __be32 verf[2];
5274
Chuck Lever2c820d92012-05-21 22:45:33 -04005275 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5276 /* An impossible timestamp guarantees this value
5277 * will never match a generated boot time. */
5278 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005279 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005280 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005281 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005282 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5283 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005284 }
Chuck Levercd937102012-03-02 17:14:31 -05005285 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5286}
5287
Jeff Laytona3192682015-06-09 19:43:59 -04005288static int
5289nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005290{
Jeff Laytona3192682015-06-09 19:43:59 -04005291 size_t len;
5292 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005293
Trond Myklebustceb3a162015-01-03 15:16:04 -05005294 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005295 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005296
Jeff Laytona3192682015-06-09 19:43:59 -04005297 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005298 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005299 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5300 1 +
5301 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5302 1;
5303 rcu_read_unlock();
5304
5305 if (len > NFS4_OPAQUE_LIMIT + 1)
5306 return -EINVAL;
5307
5308 /*
5309 * Since this string is allocated at mount time, and held until the
5310 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5311 * about a memory-reclaim deadlock.
5312 */
5313 str = kmalloc(len, GFP_KERNEL);
5314 if (!str)
5315 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005316
Chuck Levere984a552012-09-14 17:24:21 -04005317 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005318 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005319 clp->cl_ipaddr,
5320 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5321 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005322 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005323
Jeff Laytona3192682015-06-09 19:43:59 -04005324 clp->cl_owner_id = str;
5325 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005326}
5327
Jeff Layton873e3852015-06-09 19:44:00 -04005328static int
5329nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005330{
Jeff Layton873e3852015-06-09 19:44:00 -04005331 size_t len;
5332 char *str;
5333
5334 len = 10 + 10 + 1 + 10 + 1 +
5335 strlen(nfs4_client_id_uniquifier) + 1 +
5336 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5337
5338 if (len > NFS4_OPAQUE_LIMIT + 1)
5339 return -EINVAL;
5340
5341 /*
5342 * Since this string is allocated at mount time, and held until the
5343 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5344 * about a memory-reclaim deadlock.
5345 */
5346 str = kmalloc(len, GFP_KERNEL);
5347 if (!str)
5348 return -ENOMEM;
5349
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005350 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005351 clp->rpc_ops->version, clp->cl_minorversion,
5352 nfs4_client_id_uniquifier,
5353 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005354 clp->cl_owner_id = str;
5355 return 0;
5356}
5357
5358static int
5359nfs4_init_uniform_client_string(struct nfs_client *clp)
5360{
Jeff Layton873e3852015-06-09 19:44:00 -04005361 size_t len;
5362 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005363
5364 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005365 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005366
5367 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005368 return nfs4_init_uniquifier_client_string(clp);
5369
5370 len = 10 + 10 + 1 + 10 + 1 +
5371 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5372
5373 if (len > NFS4_OPAQUE_LIMIT + 1)
5374 return -EINVAL;
5375
5376 /*
5377 * Since this string is allocated at mount time, and held until the
5378 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5379 * about a memory-reclaim deadlock.
5380 */
5381 str = kmalloc(len, GFP_KERNEL);
5382 if (!str)
5383 return -ENOMEM;
5384
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005385 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005386 clp->rpc_ops->version, clp->cl_minorversion,
5387 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005388 clp->cl_owner_id = str;
5389 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005390}
5391
Chuck Lever706cb8d2014-03-12 12:51:47 -04005392/*
5393 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5394 * services. Advertise one based on the address family of the
5395 * clientaddr.
5396 */
5397static unsigned int
5398nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5399{
5400 if (strchr(clp->cl_ipaddr, ':') != NULL)
5401 return scnprintf(buf, len, "tcp6");
5402 else
5403 return scnprintf(buf, len, "tcp");
5404}
5405
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005406static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5407{
5408 struct nfs4_setclientid *sc = calldata;
5409
5410 if (task->tk_status == 0)
5411 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5412}
5413
5414static const struct rpc_call_ops nfs4_setclientid_ops = {
5415 .rpc_call_done = nfs4_setclientid_done,
5416};
5417
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005418/**
5419 * nfs4_proc_setclientid - Negotiate client ID
5420 * @clp: state data structure
5421 * @program: RPC program for NFSv4 callback service
5422 * @port: IP port number for NFS4 callback service
5423 * @cred: RPC credential to use for this call
5424 * @res: where to place the result
5425 *
5426 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5427 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005428int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5429 unsigned short port, struct rpc_cred *cred,
5430 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431{
5432 nfs4_verifier sc_verifier;
5433 struct nfs4_setclientid setclientid = {
5434 .sc_verifier = &sc_verifier,
5435 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005436 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 };
5438 struct rpc_message msg = {
5439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5440 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005441 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005442 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005444 struct rpc_task *task;
5445 struct rpc_task_setup task_setup_data = {
5446 .rpc_client = clp->cl_rpcclient,
5447 .rpc_message = &msg,
5448 .callback_ops = &nfs4_setclientid_ops,
5449 .callback_data = &setclientid,
5450 .flags = RPC_TASK_TIMEOUT,
5451 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005452 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Chuck Leverde734832012-07-11 16:30:50 -04005454 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005455 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005456
5457 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5458 status = nfs4_init_uniform_client_string(clp);
5459 else
Jeff Laytona3192682015-06-09 19:43:59 -04005460 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005461
5462 if (status)
5463 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005464
Chuck Leverde734832012-07-11 16:30:50 -04005465 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005466 setclientid.sc_netid_len =
5467 nfs4_init_callback_netid(clp,
5468 setclientid.sc_netid,
5469 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005470 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005471 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 clp->cl_ipaddr, port >> 8, port & 255);
5473
Jeff Layton3a6bb732015-06-09 19:43:57 -04005474 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005475 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005476 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005477 task = rpc_run_task(&task_setup_data);
5478 if (IS_ERR(task)) {
5479 status = PTR_ERR(task);
5480 goto out;
5481 }
5482 status = task->tk_status;
5483 if (setclientid.sc_cred) {
5484 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5485 put_rpccred(setclientid.sc_cred);
5486 }
5487 rpc_put_task(task);
5488out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005489 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005490 dprintk("NFS reply setclientid: %d\n", status);
5491 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492}
5493
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005494/**
5495 * nfs4_proc_setclientid_confirm - Confirm client ID
5496 * @clp: state data structure
5497 * @res: result of a previous SETCLIENTID
5498 * @cred: RPC credential to use for this call
5499 *
5500 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5501 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005502int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005503 struct nfs4_setclientid_res *arg,
5504 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 struct rpc_message msg = {
5507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005508 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005509 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 int status;
5512
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005513 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5514 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5515 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005516 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005517 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005518 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 return status;
5520}
5521
Trond Myklebustfe650402006-01-03 09:55:18 +01005522struct nfs4_delegreturndata {
5523 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005524 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005525 struct nfs_fh fh;
5526 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005527 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005528 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005529 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005530 struct inode *inode;
5531 bool roc;
5532 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005533};
5534
Trond Myklebustfe650402006-01-03 09:55:18 +01005535static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5536{
5537 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005538
Trond Myklebust14516c32010-07-31 14:29:06 -04005539 if (!nfs4_sequence_done(task, &data->res.seq_res))
5540 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005541
Trond Myklebustca8acf82013-08-13 10:36:56 -04005542 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005543 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005544 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005545 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005546 case -NFS4ERR_ADMIN_REVOKED:
5547 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005548 case -NFS4ERR_EXPIRED:
5549 nfs4_free_revoked_stateid(data->res.server,
5550 data->args.stateid,
5551 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005552 case -NFS4ERR_BAD_STATEID:
5553 case -NFS4ERR_OLD_STATEID:
5554 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005555 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005556 if (data->roc)
5557 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005558 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005559 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005560 if (nfs4_async_handle_error(task, data->res.server,
5561 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005562 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005563 return;
5564 }
5565 }
5566 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005567}
5568
5569static void nfs4_delegreturn_release(void *calldata)
5570{
Peng Tao039b7562014-07-03 13:05:02 +08005571 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005572 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005573
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005574 if (inode) {
5575 if (data->roc)
5576 pnfs_roc_release(inode);
5577 nfs_iput_and_deactive(inode);
5578 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005579 kfree(calldata);
5580}
5581
Andy Adamson938e1012009-04-01 09:22:28 -04005582static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5583{
5584 struct nfs4_delegreturndata *d_data;
5585
5586 d_data = (struct nfs4_delegreturndata *)data;
5587
Peng Tao500d7012015-09-22 11:35:22 +08005588 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5589 return;
5590
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005591 if (d_data->roc)
5592 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005593
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005594 nfs4_setup_sequence(d_data->res.server,
5595 &d_data->args.seq_args,
5596 &d_data->res.seq_res,
5597 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005598}
Andy Adamson938e1012009-04-01 09:22:28 -04005599
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005600static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005601 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005602 .rpc_call_done = nfs4_delegreturn_done,
5603 .rpc_release = nfs4_delegreturn_release,
5604};
5605
Trond Myklebuste6f81072008-01-24 18:14:34 -05005606static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005607{
5608 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005609 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005610 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005611 struct rpc_message msg = {
5612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5613 .rpc_cred = cred,
5614 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005615 struct rpc_task_setup task_setup_data = {
5616 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005617 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005618 .callback_ops = &nfs4_delegreturn_ops,
5619 .flags = RPC_TASK_ASYNC,
5620 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005621 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005622
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005623 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005624 if (data == NULL)
5625 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005626 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005627
5628 nfs4_state_protect(server->nfs_client,
5629 NFS_SP4_MACH_CRED_CLEANUP,
5630 &task_setup_data.rpc_client, &msg);
5631
Trond Myklebustfe650402006-01-03 09:55:18 +01005632 data->args.fhandle = &data->fh;
5633 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005634 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005635 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005636 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005637 data->res.fattr = &data->fattr;
5638 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005639 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005640 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005641 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005642 data->inode = nfs_igrab_and_active(inode);
5643 if (data->inode)
5644 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005645
Trond Myklebustc970aa82007-07-14 15:39:59 -04005646 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005647 msg.rpc_argp = &data->args;
5648 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005649 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005650 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005651 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005652 if (!issync)
5653 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005654 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005655 if (status != 0)
5656 goto out;
5657 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005658 if (status == 0)
5659 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5660 else
5661 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005662out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005663 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005664 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665}
5666
Trond Myklebuste6f81072008-01-24 18:14:34 -05005667int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668{
5669 struct nfs_server *server = NFS_SERVER(inode);
5670 struct nfs4_exception exception = { };
5671 int err;
5672 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005673 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005674 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 switch (err) {
5676 case -NFS4ERR_STALE_STATEID:
5677 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 case 0:
5679 return 0;
5680 }
5681 err = nfs4_handle_exception(server, err, &exception);
5682 } while (exception.retry);
5683 return err;
5684}
5685
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5687{
5688 struct inode *inode = state->inode;
5689 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005690 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005691 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005693 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005695 struct nfs_lockt_res res = {
5696 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 };
5698 struct rpc_message msg = {
5699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5700 .rpc_argp = &arg,
5701 .rpc_resp = &res,
5702 .rpc_cred = state->owner->so_cred,
5703 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 struct nfs4_lock_state *lsp;
5705 int status;
5706
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005707 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005708 status = nfs4_set_lock_state(state, request);
5709 if (status != 0)
5710 goto out;
5711 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005712 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005713 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005714 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005715 switch (status) {
5716 case 0:
5717 request->fl_type = F_UNLCK;
5718 break;
5719 case -NFS4ERR_DENIED:
5720 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005722 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005723 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005724out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 return status;
5726}
5727
5728static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5729{
5730 struct nfs4_exception exception = { };
5731 int err;
5732
5733 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005734 err = _nfs4_proc_getlk(state, cmd, request);
5735 trace_nfs4_get_lock(request, state, cmd, err);
5736 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 &exception);
5738 } while (exception.retry);
5739 return err;
5740}
5741
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005742struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005743 struct nfs_locku_args arg;
5744 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005745 struct nfs4_lock_state *lsp;
5746 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005747 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005748 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005749 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005750};
5751
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005752static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5753 struct nfs_open_context *ctx,
5754 struct nfs4_lock_state *lsp,
5755 struct nfs_seqid *seqid)
5756{
5757 struct nfs4_unlockdata *p;
5758 struct inode *inode = lsp->ls_state->inode;
5759
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005760 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005761 if (p == NULL)
5762 return NULL;
5763 p->arg.fh = NFS_FH(inode);
5764 p->arg.fl = &p->fl;
5765 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005766 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005767 p->lsp = lsp;
5768 atomic_inc(&lsp->ls_count);
5769 /* Ensure we don't close file until we're done freeing locks! */
5770 p->ctx = get_nfs_open_context(ctx);
5771 memcpy(&p->fl, fl, sizeof(p->fl));
5772 p->server = NFS_SERVER(inode);
5773 return p;
5774}
5775
Trond Myklebust06f814a2006-01-03 09:55:07 +01005776static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005777{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005778 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005779 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005780 nfs4_put_lock_state(calldata->lsp);
5781 put_nfs_open_context(calldata->ctx);
5782 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005783}
5784
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005785static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005786{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005787 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005788
Trond Myklebust14516c32010-07-31 14:29:06 -04005789 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5790 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005791 switch (task->tk_status) {
5792 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005793 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005794 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005795 if (nfs4_update_lock_stateid(calldata->lsp,
5796 &calldata->res.stateid))
5797 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005798 case -NFS4ERR_ADMIN_REVOKED:
5799 case -NFS4ERR_EXPIRED:
5800 nfs4_free_revoked_stateid(calldata->server,
5801 &calldata->arg.stateid,
5802 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005803 case -NFS4ERR_BAD_STATEID:
5804 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005805 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005806 if (!nfs4_stateid_match(&calldata->arg.stateid,
5807 &calldata->lsp->ls_stateid))
5808 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005809 break;
5810 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005811 if (nfs4_async_handle_error(task, calldata->server,
5812 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005813 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005814 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005815 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005816}
5817
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005818static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005819{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005820 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005822 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005823 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005824 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005825 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005826 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005827 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005828 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005829 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005830 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005831 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005832 &calldata->res.seq_res,
5833 task) != 0)
5834 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005835 return;
5836out_no_action:
5837 task->tk_action = NULL;
5838out_wait:
5839 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840}
5841
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005842static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005843 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005844 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005845 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005846};
5847
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005848static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5849 struct nfs_open_context *ctx,
5850 struct nfs4_lock_state *lsp,
5851 struct nfs_seqid *seqid)
5852{
5853 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005854 struct rpc_message msg = {
5855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5856 .rpc_cred = ctx->cred,
5857 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005858 struct rpc_task_setup task_setup_data = {
5859 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005860 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005861 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005862 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005863 .flags = RPC_TASK_ASYNC,
5864 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005865
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005866 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5867 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5868
Frank Filz137d6ac2007-07-09 15:32:29 -07005869 /* Ensure this is an unlock - when canceling a lock, the
5870 * canceled lock is passed in, and it won't be an unlock.
5871 */
5872 fl->fl_type = F_UNLCK;
5873
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005874 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5875 if (data == NULL) {
5876 nfs_free_seqid(seqid);
5877 return ERR_PTR(-ENOMEM);
5878 }
5879
Chuck Levera9c92d62013-08-09 12:48:18 -04005880 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005881 msg.rpc_argp = &data->arg;
5882 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005883 task_setup_data.callback_data = data;
5884 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005885}
5886
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5888{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005889 struct inode *inode = state->inode;
5890 struct nfs4_state_owner *sp = state->owner;
5891 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005892 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005893 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005894 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005895 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005896 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005897 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898
Trond Myklebust9b073572006-06-29 16:38:34 -04005899 status = nfs4_set_lock_state(state, request);
5900 /* Unlock _before_ we do the RPC call */
5901 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005902 /* Exclude nfs_delegation_claim_locks() */
5903 mutex_lock(&sp->so_delegreturn_mutex);
5904 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005905 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005906 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005907 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005908 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005909 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005910 }
5911 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005912 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005913 if (status != 0)
5914 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005915 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005916 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005917 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5918 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005919 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5920 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005921 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005922 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005923 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005924 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005925 status = PTR_ERR(task);
5926 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005927 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005928 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005929 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005930out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005931 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005932 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005933 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934}
5935
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005936struct nfs4_lockdata {
5937 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005938 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005939 struct nfs4_lock_state *lsp;
5940 struct nfs_open_context *ctx;
5941 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005942 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005943 int rpc_status;
5944 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005945 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005946};
5947
5948static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005949 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5950 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005951{
5952 struct nfs4_lockdata *p;
5953 struct inode *inode = lsp->ls_state->inode;
5954 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005955 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005956
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005957 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005958 if (p == NULL)
5959 return NULL;
5960
5961 p->arg.fh = NFS_FH(inode);
5962 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005963 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005964 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005965 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005966 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5967 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005968 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005969 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005970 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005971 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005972 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005973 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005974 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005975 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005976 atomic_inc(&lsp->ls_count);
5977 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005978 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005979 memcpy(&p->fl, fl, sizeof(p->fl));
5980 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005981out_free_seqid:
5982 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005983out_free:
5984 kfree(p);
5985 return NULL;
5986}
5987
5988static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5989{
5990 struct nfs4_lockdata *data = calldata;
5991 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
Harvey Harrison3110ff82008-05-02 13:42:44 -07005993 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005994 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005995 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005996 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005997 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005998 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005999 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006000 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006001 nfs4_stateid_copy(&data->arg.open_stateid,
6002 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006003 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006004 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006005 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006006 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006007 nfs4_stateid_copy(&data->arg.lock_stateid,
6008 &data->lsp->ls_stateid);
6009 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006010 if (!nfs4_valid_open_stateid(state)) {
6011 data->rpc_status = -EBADF;
6012 task->tk_action = NULL;
6013 goto out_release_open_seqid;
6014 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006015 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04006016 if (nfs4_setup_sequence(data->server,
6017 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006018 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006019 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006020 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006021out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006022 nfs_release_seqid(data->arg.open_seqid);
6023out_release_lock_seqid:
6024 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006025out_wait:
6026 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006027 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006028}
6029
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006030static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6031{
6032 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006033 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006034
Harvey Harrison3110ff82008-05-02 13:42:44 -07006035 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006036
Trond Myklebust14516c32010-07-31 14:29:06 -04006037 if (!nfs4_sequence_done(task, &data->res.seq_res))
6038 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006039
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006040 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006041 switch (task->tk_status) {
6042 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006043 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006044 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006045 if (data->arg.new_lock) {
6046 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006047 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006048 rpc_restart_call_prepare(task);
6049 break;
6050 }
6051 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006052 if (data->arg.new_lock_owner != 0) {
6053 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6054 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6055 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6056 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6057 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006058 break;
6059 case -NFS4ERR_BAD_STATEID:
6060 case -NFS4ERR_OLD_STATEID:
6061 case -NFS4ERR_STALE_STATEID:
6062 case -NFS4ERR_EXPIRED:
6063 if (data->arg.new_lock_owner != 0) {
6064 if (!nfs4_stateid_match(&data->arg.open_stateid,
6065 &lsp->ls_state->open_stateid))
6066 rpc_restart_call_prepare(task);
6067 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6068 &lsp->ls_stateid))
6069 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006070 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006071 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006072}
6073
6074static void nfs4_lock_release(void *calldata)
6075{
6076 struct nfs4_lockdata *data = calldata;
6077
Harvey Harrison3110ff82008-05-02 13:42:44 -07006078 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006079 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006080 if (data->cancelled != 0) {
6081 struct rpc_task *task;
6082 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6083 data->arg.lock_seqid);
6084 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006085 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006086 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087 } else
6088 nfs_free_seqid(data->arg.lock_seqid);
6089 nfs4_put_lock_state(data->lsp);
6090 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006091 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006092 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006093 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094}
6095
6096static const struct rpc_call_ops nfs4_lock_ops = {
6097 .rpc_call_prepare = nfs4_lock_prepare,
6098 .rpc_call_done = nfs4_lock_done,
6099 .rpc_release = nfs4_lock_release,
6100};
6101
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006102static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6103{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006104 switch (error) {
6105 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006106 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006107 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006108 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006109 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006110 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006111 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006112 break;
6113 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006114 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006115 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006116 };
6117}
6118
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006119static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006120{
6121 struct nfs4_lockdata *data;
6122 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006123 struct rpc_message msg = {
6124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6125 .rpc_cred = state->owner->so_cred,
6126 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006127 struct rpc_task_setup task_setup_data = {
6128 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006129 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006130 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006131 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006132 .flags = RPC_TASK_ASYNC,
6133 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006134 int ret;
6135
Harvey Harrison3110ff82008-05-02 13:42:44 -07006136 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006137 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006138 fl->fl_u.nfs4_fl.owner,
6139 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006140 if (data == NULL)
6141 return -ENOMEM;
6142 if (IS_SETLKW(cmd))
6143 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006144 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006145 msg.rpc_argp = &data->arg;
6146 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006147 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006148 if (recovery_type > NFS_LOCK_NEW) {
6149 if (recovery_type == NFS_LOCK_RECLAIM)
6150 data->arg.reclaim = NFS_LOCK_RECLAIM;
6151 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006152 } else
6153 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006154 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006155 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006156 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006157 ret = nfs4_wait_for_completion_rpc_task(task);
6158 if (ret == 0) {
6159 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006160 if (ret)
6161 nfs4_handle_setlk_error(data->server, data->lsp,
6162 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006163 } else
6164 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006165 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006166 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006167 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006168 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169}
6170
6171static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6172{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006173 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006174 struct nfs4_exception exception = {
6175 .inode = state->inode,
6176 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006177 int err;
6178
6179 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006180 /* Cache the lock if possible... */
6181 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6182 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006183 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006184 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006185 break;
6186 nfs4_handle_exception(server, err, &exception);
6187 } while (exception.retry);
6188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189}
6190
6191static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6192{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006193 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006194 struct nfs4_exception exception = {
6195 .inode = state->inode,
6196 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006197 int err;
6198
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006199 err = nfs4_set_lock_state(state, request);
6200 if (err != 0)
6201 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006202 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006203 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6204 return 0;
6205 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006206 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006207 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6208 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006209 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006210 switch (err) {
6211 default:
6212 goto out;
6213 case -NFS4ERR_GRACE:
6214 case -NFS4ERR_DELAY:
6215 nfs4_handle_exception(server, err, &exception);
6216 err = 0;
6217 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006218 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006219out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006220 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221}
6222
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006223#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006224static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6225{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006226 struct nfs4_lock_state *lsp;
6227 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006228
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006229 status = nfs4_set_lock_state(state, request);
6230 if (status != 0)
6231 return status;
6232 lsp = request->fl_u.nfs4_fl.owner;
6233 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6234 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6235 return 0;
6236 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006237 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006238}
6239#endif
6240
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6242{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006243 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006244 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006245 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006246 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247
Trond Myklebust01c3b862006-06-29 16:38:39 -04006248 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006249 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006250 if (status < 0)
6251 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006252 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006253 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006254 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006255 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006256 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006257 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006258 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006259 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006260 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006261 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006262 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006263 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006264 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006265 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006266out:
6267 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 return status;
6269}
6270
6271static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6272{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006273 struct nfs4_exception exception = {
6274 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006275 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006276 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 int err;
6278
6279 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006280 err = _nfs4_proc_setlk(state, cmd, request);
6281 if (err == -NFS4ERR_DENIED)
6282 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006284 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 } while (exception.retry);
6286 return err;
6287}
6288
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006289#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6290#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6291
6292static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006293nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6294 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006295{
6296 int status = -ERESTARTSYS;
6297 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6298
6299 while(!signalled()) {
6300 status = nfs4_proc_setlk(state, cmd, request);
6301 if ((status != -EAGAIN) || IS_SETLK(cmd))
6302 break;
6303 freezable_schedule_timeout_interruptible(timeout);
6304 timeout *= 2;
6305 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6306 status = -ERESTARTSYS;
6307 }
6308 return status;
6309}
6310
Jeff Laytona1d617d82016-09-17 18:17:39 -04006311#ifdef CONFIG_NFS_V4_1
6312struct nfs4_lock_waiter {
6313 struct task_struct *task;
6314 struct inode *inode;
6315 struct nfs_lowner *owner;
6316 bool notified;
6317};
6318
6319static int
6320nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6321{
6322 int ret;
6323 struct cb_notify_lock_args *cbnl = key;
6324 struct nfs4_lock_waiter *waiter = wait->private;
6325 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6326 *wowner = waiter->owner;
6327
6328 /* Only wake if the callback was for the same owner */
6329 if (lowner->clientid != wowner->clientid ||
6330 lowner->id != wowner->id ||
6331 lowner->s_dev != wowner->s_dev)
6332 return 0;
6333
6334 /* Make sure it's for the right inode */
6335 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6336 return 0;
6337
6338 waiter->notified = true;
6339
6340 /* override "private" so we can use default_wake_function */
6341 wait->private = waiter->task;
6342 ret = autoremove_wake_function(wait, mode, flags, key);
6343 wait->private = waiter;
6344 return ret;
6345}
6346
6347static int
6348nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6349{
6350 int status = -ERESTARTSYS;
6351 unsigned long flags;
6352 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6353 struct nfs_server *server = NFS_SERVER(state->inode);
6354 struct nfs_client *clp = server->nfs_client;
6355 wait_queue_head_t *q = &clp->cl_lock_waitq;
6356 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6357 .id = lsp->ls_seqid.owner_id,
6358 .s_dev = server->s_dev };
6359 struct nfs4_lock_waiter waiter = { .task = current,
6360 .inode = state->inode,
6361 .owner = &owner,
6362 .notified = false };
6363 wait_queue_t wait;
6364
6365 /* Don't bother with waitqueue if we don't expect a callback */
6366 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6367 return nfs4_retry_setlk_simple(state, cmd, request);
6368
6369 init_wait(&wait);
6370 wait.private = &waiter;
6371 wait.func = nfs4_wake_lock_waiter;
6372 add_wait_queue(q, &wait);
6373
6374 while(!signalled()) {
6375 status = nfs4_proc_setlk(state, cmd, request);
6376 if ((status != -EAGAIN) || IS_SETLK(cmd))
6377 break;
6378
6379 status = -ERESTARTSYS;
6380 spin_lock_irqsave(&q->lock, flags);
6381 if (waiter.notified) {
6382 spin_unlock_irqrestore(&q->lock, flags);
6383 continue;
6384 }
6385 set_current_state(TASK_INTERRUPTIBLE);
6386 spin_unlock_irqrestore(&q->lock, flags);
6387
6388 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6389 }
6390
6391 finish_wait(q, &wait);
6392 return status;
6393}
6394#else /* !CONFIG_NFS_V4_1 */
6395static inline int
6396nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6397{
6398 return nfs4_retry_setlk_simple(state, cmd, request);
6399}
6400#endif
6401
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402static int
6403nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6404{
6405 struct nfs_open_context *ctx;
6406 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 int status;
6408
6409 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006410 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 state = ctx->state;
6412
6413 if (request->fl_start < 0 || request->fl_end < 0)
6414 return -EINVAL;
6415
Trond Myklebustd9531262009-07-21 19:22:38 -04006416 if (IS_GETLK(cmd)) {
6417 if (state != NULL)
6418 return nfs4_proc_getlk(state, F_GETLK, request);
6419 return 0;
6420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
6422 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6423 return -EINVAL;
6424
Trond Myklebustd9531262009-07-21 19:22:38 -04006425 if (request->fl_type == F_UNLCK) {
6426 if (state != NULL)
6427 return nfs4_proc_unlck(state, cmd, request);
6428 return 0;
6429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430
Trond Myklebustd9531262009-07-21 19:22:38 -04006431 if (state == NULL)
6432 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006433
6434 if ((request->fl_flags & FL_POSIX) &&
6435 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6436 return -ENOLCK;
6437
Trond Myklebust55725512012-04-18 12:48:35 -04006438 /*
6439 * Don't rely on the VFS having checked the file open mode,
6440 * since it won't do this for flock() locks.
6441 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006442 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006443 case F_RDLCK:
6444 if (!(filp->f_mode & FMODE_READ))
6445 return -EBADF;
6446 break;
6447 case F_WRLCK:
6448 if (!(filp->f_mode & FMODE_WRITE))
6449 return -EBADF;
6450 }
6451
Jeff Layton1ea67db2016-09-17 18:17:37 -04006452 status = nfs4_set_lock_state(state, request);
6453 if (status != 0)
6454 return status;
6455
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006456 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457}
6458
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006459int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006460{
6461 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006462 int err;
6463
6464 err = nfs4_set_lock_state(state, fl);
6465 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006466 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006467 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006468 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006469}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006470
Trond Myklebustcf470c32012-03-07 13:49:12 -05006471struct nfs_release_lockowner_data {
6472 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006473 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006474 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006475 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006476 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006477};
6478
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006479static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6480{
6481 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006482 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006483 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6484 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006485 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006486 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006487}
6488
6489static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6490{
6491 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006492 struct nfs_server *server = data->server;
6493
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006494 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006495
6496 switch (task->tk_status) {
6497 case 0:
6498 renew_lease(server, data->timestamp);
6499 break;
6500 case -NFS4ERR_STALE_CLIENTID:
6501 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006502 nfs4_schedule_lease_recovery(server->nfs_client);
6503 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006504 case -NFS4ERR_LEASE_MOVED:
6505 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006506 if (nfs4_async_handle_error(task, server,
6507 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006508 rpc_restart_call_prepare(task);
6509 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006510}
6511
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006512static void nfs4_release_lockowner_release(void *calldata)
6513{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006514 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006515 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006516 kfree(calldata);
6517}
6518
Trond Myklebust17280172012-03-11 13:11:00 -04006519static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006520 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6521 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006522 .rpc_release = nfs4_release_lockowner_release,
6523};
6524
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006525static void
6526nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006527{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006528 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006529 struct rpc_message msg = {
6530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6531 };
6532
6533 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006534 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006535
Trond Myklebustcf470c32012-03-07 13:49:12 -05006536 data = kmalloc(sizeof(*data), GFP_NOFS);
6537 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006538 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006539 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006540 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006541 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6542 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6543 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006544
Trond Myklebustcf470c32012-03-07 13:49:12 -05006545 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006546 msg.rpc_resp = &data->res;
6547 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006548 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006549}
6550
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006551#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6552
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006553static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006554 struct dentry *unused, struct inode *inode,
6555 const char *key, const void *buf,
6556 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006557{
Al Viro59301222016-05-27 10:19:30 -04006558 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006559}
6560
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006561static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006562 struct dentry *unused, struct inode *inode,
6563 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006564{
Al Virob2968212016-04-10 20:48:24 -04006565 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006566}
6567
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006568static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006569{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006570 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006571}
6572
David Quigleyc9bccef2013-05-22 12:50:45 -04006573#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006574
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006575static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006576 struct dentry *unused, struct inode *inode,
6577 const char *key, const void *buf,
6578 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006579{
6580 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006581 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006582
6583 return -EOPNOTSUPP;
6584}
6585
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006586static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006587 struct dentry *unused, struct inode *inode,
6588 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006589{
6590 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006591 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006592 return -EOPNOTSUPP;
6593}
6594
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006595static ssize_t
6596nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006597{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006598 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006599
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006600 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6601 len = security_inode_listsecurity(inode, list, list_len);
6602 if (list_len && len > list_len)
6603 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006604 }
6605 return len;
6606}
6607
6608static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6609 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006610 .get = nfs4_xattr_get_nfs4_label,
6611 .set = nfs4_xattr_set_nfs4_label,
6612};
David Quigleyc9bccef2013-05-22 12:50:45 -04006613
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006614#else
6615
6616static ssize_t
6617nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6618{
6619 return 0;
6620}
6621
6622#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006623
Andy Adamson533eb462011-06-13 18:25:56 -04006624/*
6625 * nfs_fhget will use either the mounted_on_fileid or the fileid
6626 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006627static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6628{
Andy Adamson533eb462011-06-13 18:25:56 -04006629 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6630 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6631 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006632 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006633 return;
6634
6635 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006636 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006637 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6638 fattr->nlink = 2;
6639}
6640
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006641static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6642 const struct qstr *name,
6643 struct nfs4_fs_locations *fs_locations,
6644 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006645{
6646 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006647 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006648 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006649 };
6650 struct nfs4_fs_locations_arg args = {
6651 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006652 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006653 .page = page,
6654 .bitmask = bitmask,
6655 };
Benny Halevy22958462009-04-01 09:22:02 -04006656 struct nfs4_fs_locations_res res = {
6657 .fs_locations = fs_locations,
6658 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006659 struct rpc_message msg = {
6660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6661 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006662 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006663 };
6664 int status;
6665
Harvey Harrison3110ff82008-05-02 13:42:44 -07006666 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006667
6668 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6669 * is not supported */
6670 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6671 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6672 else
6673 bitmask[0] |= FATTR4_WORD0_FILEID;
6674
Trond Myklebustc228fd32007-01-13 02:28:11 -05006675 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006676 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006677 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006678 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006679 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006680 return status;
6681}
6682
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006683int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6684 const struct qstr *name,
6685 struct nfs4_fs_locations *fs_locations,
6686 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006687{
6688 struct nfs4_exception exception = { };
6689 int err;
6690 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006691 err = _nfs4_proc_fs_locations(client, dir, name,
6692 fs_locations, page);
6693 trace_nfs4_get_fs_locations(dir, name, err);
6694 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006695 &exception);
6696 } while (exception.retry);
6697 return err;
6698}
6699
Chuck Leverb03d7352013-10-17 14:12:50 -04006700/*
6701 * This operation also signals the server that this client is
6702 * performing migration recovery. The server can stop returning
6703 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6704 * appended to this compound to identify the client ID which is
6705 * performing recovery.
6706 */
6707static int _nfs40_proc_get_locations(struct inode *inode,
6708 struct nfs4_fs_locations *locations,
6709 struct page *page, struct rpc_cred *cred)
6710{
6711 struct nfs_server *server = NFS_SERVER(inode);
6712 struct rpc_clnt *clnt = server->client;
6713 u32 bitmask[2] = {
6714 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6715 };
6716 struct nfs4_fs_locations_arg args = {
6717 .clientid = server->nfs_client->cl_clientid,
6718 .fh = NFS_FH(inode),
6719 .page = page,
6720 .bitmask = bitmask,
6721 .migration = 1, /* skip LOOKUP */
6722 .renew = 1, /* append RENEW */
6723 };
6724 struct nfs4_fs_locations_res res = {
6725 .fs_locations = locations,
6726 .migration = 1,
6727 .renew = 1,
6728 };
6729 struct rpc_message msg = {
6730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6731 .rpc_argp = &args,
6732 .rpc_resp = &res,
6733 .rpc_cred = cred,
6734 };
6735 unsigned long now = jiffies;
6736 int status;
6737
6738 nfs_fattr_init(&locations->fattr);
6739 locations->server = server;
6740 locations->nlocations = 0;
6741
6742 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6743 nfs4_set_sequence_privileged(&args.seq_args);
6744 status = nfs4_call_sync_sequence(clnt, server, &msg,
6745 &args.seq_args, &res.seq_res);
6746 if (status)
6747 return status;
6748
6749 renew_lease(server, now);
6750 return 0;
6751}
6752
6753#ifdef CONFIG_NFS_V4_1
6754
6755/*
6756 * This operation also signals the server that this client is
6757 * performing migration recovery. The server can stop asserting
6758 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6759 * performing this operation is identified in the SEQUENCE
6760 * operation in this compound.
6761 *
6762 * When the client supports GETATTR(fs_locations_info), it can
6763 * be plumbed in here.
6764 */
6765static int _nfs41_proc_get_locations(struct inode *inode,
6766 struct nfs4_fs_locations *locations,
6767 struct page *page, struct rpc_cred *cred)
6768{
6769 struct nfs_server *server = NFS_SERVER(inode);
6770 struct rpc_clnt *clnt = server->client;
6771 u32 bitmask[2] = {
6772 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6773 };
6774 struct nfs4_fs_locations_arg args = {
6775 .fh = NFS_FH(inode),
6776 .page = page,
6777 .bitmask = bitmask,
6778 .migration = 1, /* skip LOOKUP */
6779 };
6780 struct nfs4_fs_locations_res res = {
6781 .fs_locations = locations,
6782 .migration = 1,
6783 };
6784 struct rpc_message msg = {
6785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6786 .rpc_argp = &args,
6787 .rpc_resp = &res,
6788 .rpc_cred = cred,
6789 };
6790 int status;
6791
6792 nfs_fattr_init(&locations->fattr);
6793 locations->server = server;
6794 locations->nlocations = 0;
6795
6796 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6797 nfs4_set_sequence_privileged(&args.seq_args);
6798 status = nfs4_call_sync_sequence(clnt, server, &msg,
6799 &args.seq_args, &res.seq_res);
6800 if (status == NFS4_OK &&
6801 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6802 status = -NFS4ERR_LEASE_MOVED;
6803 return status;
6804}
6805
6806#endif /* CONFIG_NFS_V4_1 */
6807
6808/**
6809 * nfs4_proc_get_locations - discover locations for a migrated FSID
6810 * @inode: inode on FSID that is migrating
6811 * @locations: result of query
6812 * @page: buffer
6813 * @cred: credential to use for this operation
6814 *
6815 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6816 * operation failed, or a negative errno if a local error occurred.
6817 *
6818 * On success, "locations" is filled in, but if the server has
6819 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6820 * asserted.
6821 *
6822 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6823 * from this client that require migration recovery.
6824 */
6825int nfs4_proc_get_locations(struct inode *inode,
6826 struct nfs4_fs_locations *locations,
6827 struct page *page, struct rpc_cred *cred)
6828{
6829 struct nfs_server *server = NFS_SERVER(inode);
6830 struct nfs_client *clp = server->nfs_client;
6831 const struct nfs4_mig_recovery_ops *ops =
6832 clp->cl_mvops->mig_recovery_ops;
6833 struct nfs4_exception exception = { };
6834 int status;
6835
6836 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6837 (unsigned long long)server->fsid.major,
6838 (unsigned long long)server->fsid.minor,
6839 clp->cl_hostname);
6840 nfs_display_fhandle(NFS_FH(inode), __func__);
6841
6842 do {
6843 status = ops->get_locations(inode, locations, page, cred);
6844 if (status != -NFS4ERR_DELAY)
6845 break;
6846 nfs4_handle_exception(server, status, &exception);
6847 } while (exception.retry);
6848 return status;
6849}
6850
Chuck Lever44c99932013-10-17 14:13:30 -04006851/*
6852 * This operation also signals the server that this client is
6853 * performing "lease moved" recovery. The server can stop
6854 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6855 * is appended to this compound to identify the client ID which is
6856 * performing recovery.
6857 */
6858static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6859{
6860 struct nfs_server *server = NFS_SERVER(inode);
6861 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6862 struct rpc_clnt *clnt = server->client;
6863 struct nfs4_fsid_present_arg args = {
6864 .fh = NFS_FH(inode),
6865 .clientid = clp->cl_clientid,
6866 .renew = 1, /* append RENEW */
6867 };
6868 struct nfs4_fsid_present_res res = {
6869 .renew = 1,
6870 };
6871 struct rpc_message msg = {
6872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6873 .rpc_argp = &args,
6874 .rpc_resp = &res,
6875 .rpc_cred = cred,
6876 };
6877 unsigned long now = jiffies;
6878 int status;
6879
6880 res.fh = nfs_alloc_fhandle();
6881 if (res.fh == NULL)
6882 return -ENOMEM;
6883
6884 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6885 nfs4_set_sequence_privileged(&args.seq_args);
6886 status = nfs4_call_sync_sequence(clnt, server, &msg,
6887 &args.seq_args, &res.seq_res);
6888 nfs_free_fhandle(res.fh);
6889 if (status)
6890 return status;
6891
6892 do_renew_lease(clp, now);
6893 return 0;
6894}
6895
6896#ifdef CONFIG_NFS_V4_1
6897
6898/*
6899 * This operation also signals the server that this client is
6900 * performing "lease moved" recovery. The server can stop asserting
6901 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6902 * this operation is identified in the SEQUENCE operation in this
6903 * compound.
6904 */
6905static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6906{
6907 struct nfs_server *server = NFS_SERVER(inode);
6908 struct rpc_clnt *clnt = server->client;
6909 struct nfs4_fsid_present_arg args = {
6910 .fh = NFS_FH(inode),
6911 };
6912 struct nfs4_fsid_present_res res = {
6913 };
6914 struct rpc_message msg = {
6915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6916 .rpc_argp = &args,
6917 .rpc_resp = &res,
6918 .rpc_cred = cred,
6919 };
6920 int status;
6921
6922 res.fh = nfs_alloc_fhandle();
6923 if (res.fh == NULL)
6924 return -ENOMEM;
6925
6926 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6927 nfs4_set_sequence_privileged(&args.seq_args);
6928 status = nfs4_call_sync_sequence(clnt, server, &msg,
6929 &args.seq_args, &res.seq_res);
6930 nfs_free_fhandle(res.fh);
6931 if (status == NFS4_OK &&
6932 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6933 status = -NFS4ERR_LEASE_MOVED;
6934 return status;
6935}
6936
6937#endif /* CONFIG_NFS_V4_1 */
6938
6939/**
6940 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6941 * @inode: inode on FSID to check
6942 * @cred: credential to use for this operation
6943 *
6944 * Server indicates whether the FSID is present, moved, or not
6945 * recognized. This operation is necessary to clear a LEASE_MOVED
6946 * condition for this client ID.
6947 *
6948 * Returns NFS4_OK if the FSID is present on this server,
6949 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6950 * NFS4ERR code if some error occurred on the server, or a
6951 * negative errno if a local failure occurred.
6952 */
6953int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6954{
6955 struct nfs_server *server = NFS_SERVER(inode);
6956 struct nfs_client *clp = server->nfs_client;
6957 const struct nfs4_mig_recovery_ops *ops =
6958 clp->cl_mvops->mig_recovery_ops;
6959 struct nfs4_exception exception = { };
6960 int status;
6961
6962 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6963 (unsigned long long)server->fsid.major,
6964 (unsigned long long)server->fsid.minor,
6965 clp->cl_hostname);
6966 nfs_display_fhandle(NFS_FH(inode), __func__);
6967
6968 do {
6969 status = ops->fsid_present(inode, cred);
6970 if (status != -NFS4ERR_DELAY)
6971 break;
6972 nfs4_handle_exception(server, status, &exception);
6973 } while (exception.retry);
6974 return status;
6975}
6976
Andy Adamson5ec16a82013-08-08 10:57:55 -04006977/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006978 * If 'use_integrity' is true and the state managment nfs_client
6979 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6980 * and the machine credential as per RFC3530bis and RFC5661 Security
6981 * Considerations sections. Otherwise, just use the user cred with the
6982 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006983 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006984static 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 +00006985{
6986 int status;
6987 struct nfs4_secinfo_arg args = {
6988 .dir_fh = NFS_FH(dir),
6989 .name = name,
6990 };
6991 struct nfs4_secinfo_res res = {
6992 .flavors = flavors,
6993 };
6994 struct rpc_message msg = {
6995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6996 .rpc_argp = &args,
6997 .rpc_resp = &res,
6998 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006999 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007000 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007001
7002 if (use_integrity) {
7003 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007004 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7005 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007006 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007007
7008 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007009
7010 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7011 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7012
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007013 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7014 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007015 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007016
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007017 if (cred)
7018 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007019
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007020 return status;
7021}
7022
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007023int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7024 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007025{
7026 struct nfs4_exception exception = { };
7027 int err;
7028 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007029 err = -NFS4ERR_WRONGSEC;
7030
7031 /* try to use integrity protection with machine cred */
7032 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7033 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7034
7035 /*
7036 * if unable to use integrity protection, or SECINFO with
7037 * integrity protection returns NFS4ERR_WRONGSEC (which is
7038 * disallowed by spec, but exists in deployed servers) use
7039 * the current filesystem's rpc_client and the user cred.
7040 */
7041 if (err == -NFS4ERR_WRONGSEC)
7042 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7043
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007044 trace_nfs4_secinfo(dir, name, err);
7045 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007046 &exception);
7047 } while (exception.retry);
7048 return err;
7049}
7050
Andy Adamson557134a2009-04-01 09:21:53 -04007051#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007052/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007053 * Check the exchange flags returned by the server for invalid flags, having
7054 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7055 * DS flags set.
7056 */
7057static int nfs4_check_cl_exchange_flags(u32 flags)
7058{
7059 if (flags & ~EXCHGID4_FLAG_MASK_R)
7060 goto out_inval;
7061 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7062 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7063 goto out_inval;
7064 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7065 goto out_inval;
7066 return NFS_OK;
7067out_inval:
7068 return -NFS4ERR_INVAL;
7069}
7070
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007071static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007072nfs41_same_server_scope(struct nfs41_server_scope *a,
7073 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007074{
7075 if (a->server_scope_sz == b->server_scope_sz &&
7076 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7077 return true;
7078
7079 return false;
7080}
7081
Andy Adamson02a95de2016-02-05 16:08:37 -05007082static void
7083nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7084{
7085}
7086
7087static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7088 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7089};
7090
Andy Adamson357f54d2010-12-14 10:11:57 -05007091/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007092 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007093 *
7094 * The 4.1 client currently uses the same TCP connection for the
7095 * fore and backchannel.
7096 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007097static
7098int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7099 struct rpc_xprt *xprt,
7100 struct nfs_client *clp,
7101 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007102{
7103 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007104 struct nfs41_bind_conn_to_session_args args = {
7105 .client = clp,
7106 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7107 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007108 struct nfs41_bind_conn_to_session_res res;
7109 struct rpc_message msg = {
7110 .rpc_proc =
7111 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007112 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007113 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007114 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007115 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007116 struct rpc_task_setup task_setup_data = {
7117 .rpc_client = clnt,
7118 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007119 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007120 .rpc_message = &msg,
7121 .flags = RPC_TASK_TIMEOUT,
7122 };
7123 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007124
7125 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007126
Trond Myklebust71a097c2015-02-18 09:27:18 -08007127 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7128 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7129 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007130
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007131 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7132 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7133 args.dir = NFS4_CDFC4_FORE;
7134
7135 task = rpc_run_task(&task_setup_data);
7136 if (!IS_ERR(task)) {
7137 status = task->tk_status;
7138 rpc_put_task(task);
7139 } else
7140 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007141 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007142 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007143 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007144 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7145 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7146 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007147 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007148 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007149 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007150 dprintk("NFS: %s: Unexpected direction from server\n",
7151 __func__);
7152 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007153 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007154 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007155 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007156 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7157 __func__);
7158 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007159 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007160 }
7161 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007162out:
7163 dprintk("<-- %s status= %d\n", __func__, status);
7164 return status;
7165}
7166
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007167struct rpc_bind_conn_calldata {
7168 struct nfs_client *clp;
7169 struct rpc_cred *cred;
7170};
7171
7172static int
7173nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7174 struct rpc_xprt *xprt,
7175 void *calldata)
7176{
7177 struct rpc_bind_conn_calldata *p = calldata;
7178
7179 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7180}
7181
7182int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7183{
7184 struct rpc_bind_conn_calldata data = {
7185 .clp = clp,
7186 .cred = cred,
7187 };
7188 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7189 nfs4_proc_bind_conn_to_session_callback, &data);
7190}
7191
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007192/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007193 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7194 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007195 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007196static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7197 .how = SP4_MACH_CRED,
7198 .enforce.u.words = {
7199 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7200 1 << (OP_EXCHANGE_ID - 32) |
7201 1 << (OP_CREATE_SESSION - 32) |
7202 1 << (OP_DESTROY_SESSION - 32) |
7203 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007204 },
7205 .allow.u.words = {
7206 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007207 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007208 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007209 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007210 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007211 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007212 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007213 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007214 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007215 1 << (OP_FREE_STATEID - 32) |
7216 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007217 }
7218};
7219
7220/*
7221 * Select the state protection mode for client `clp' given the server results
7222 * from exchange_id in `sp'.
7223 *
7224 * Returns 0 on success, negative errno otherwise.
7225 */
7226static int nfs4_sp4_select_mode(struct nfs_client *clp,
7227 struct nfs41_state_protection *sp)
7228{
7229 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7230 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7231 1 << (OP_EXCHANGE_ID - 32) |
7232 1 << (OP_CREATE_SESSION - 32) |
7233 1 << (OP_DESTROY_SESSION - 32) |
7234 1 << (OP_DESTROY_CLIENTID - 32)
7235 };
7236 unsigned int i;
7237
7238 if (sp->how == SP4_MACH_CRED) {
7239 /* Print state protect result */
7240 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7241 for (i = 0; i <= LAST_NFS4_OP; i++) {
7242 if (test_bit(i, sp->enforce.u.longs))
7243 dfprintk(MOUNT, " enforce op %d\n", i);
7244 if (test_bit(i, sp->allow.u.longs))
7245 dfprintk(MOUNT, " allow op %d\n", i);
7246 }
7247
7248 /* make sure nothing is on enforce list that isn't supported */
7249 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7250 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7251 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7252 return -EINVAL;
7253 }
7254 }
7255
7256 /*
7257 * Minimal mode - state operations are allowed to use machine
7258 * credential. Note this already happens by default, so the
7259 * client doesn't have to do anything more than the negotiation.
7260 *
7261 * NOTE: we don't care if EXCHANGE_ID is in the list -
7262 * we're already using the machine cred for exchange_id
7263 * and will never use a different cred.
7264 */
7265 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7266 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7267 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7268 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7269 dfprintk(MOUNT, "sp4_mach_cred:\n");
7270 dfprintk(MOUNT, " minimal mode enabled\n");
7271 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7272 } else {
7273 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7274 return -EINVAL;
7275 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007276
7277 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007278 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7279 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007280 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7281 dfprintk(MOUNT, " cleanup mode enabled\n");
7282 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7283 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007284
Andrew Elble99ade3c2015-12-02 09:39:51 -05007285 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7286 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7287 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7288 &clp->cl_sp4_flags);
7289 }
7290
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007291 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7292 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7293 dfprintk(MOUNT, " secinfo mode enabled\n");
7294 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7295 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007296
7297 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7298 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7299 dfprintk(MOUNT, " stateid mode enabled\n");
7300 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7301 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007302
7303 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7304 dfprintk(MOUNT, " write mode enabled\n");
7305 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7306 }
7307
7308 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7309 dfprintk(MOUNT, " commit mode enabled\n");
7310 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7311 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007312 }
7313
7314 return 0;
7315}
7316
Andy Adamson8d89bd72016-09-09 09:22:18 -04007317struct nfs41_exchange_id_data {
7318 struct nfs41_exchange_id_res res;
7319 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007320 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007321 int rpc_status;
7322};
7323
7324static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7325{
7326 struct nfs41_exchange_id_data *cdata =
7327 (struct nfs41_exchange_id_data *)data;
7328 struct nfs_client *clp = cdata->args.client;
7329 int status = task->tk_status;
7330
7331 trace_nfs4_exchange_id(clp, status);
7332
7333 if (status == 0)
7334 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007335
7336 if (cdata->xprt && status == 0) {
7337 status = nfs4_detect_session_trunking(clp, &cdata->res,
7338 cdata->xprt);
7339 goto out;
7340 }
7341
Andy Adamson8d89bd72016-09-09 09:22:18 -04007342 if (status == 0)
7343 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7344
7345 if (status == 0) {
7346 clp->cl_clientid = cdata->res.clientid;
7347 clp->cl_exchange_flags = cdata->res.flags;
7348 /* Client ID is not confirmed */
7349 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7350 clear_bit(NFS4_SESSION_ESTABLISHED,
7351 &clp->cl_session->session_state);
7352 clp->cl_seqid = cdata->res.seqid;
7353 }
7354
7355 kfree(clp->cl_serverowner);
7356 clp->cl_serverowner = cdata->res.server_owner;
7357 cdata->res.server_owner = NULL;
7358
7359 /* use the most recent implementation id */
7360 kfree(clp->cl_implid);
7361 clp->cl_implid = cdata->res.impl_id;
7362 cdata->res.impl_id = NULL;
7363
7364 if (clp->cl_serverscope != NULL &&
7365 !nfs41_same_server_scope(clp->cl_serverscope,
7366 cdata->res.server_scope)) {
7367 dprintk("%s: server_scope mismatch detected\n",
7368 __func__);
7369 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7370 kfree(clp->cl_serverscope);
7371 clp->cl_serverscope = NULL;
7372 }
7373
7374 if (clp->cl_serverscope == NULL) {
7375 clp->cl_serverscope = cdata->res.server_scope;
7376 cdata->res.server_scope = NULL;
7377 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007378 /* Save the EXCHANGE_ID verifier session trunk tests */
7379 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7380 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007381 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007382out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007383 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007384 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007385}
7386
7387static void nfs4_exchange_id_release(void *data)
7388{
7389 struct nfs41_exchange_id_data *cdata =
7390 (struct nfs41_exchange_id_data *)data;
7391
7392 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007393 if (cdata->xprt) {
7394 xprt_put(cdata->xprt);
7395 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7396 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007397 kfree(cdata->res.impl_id);
7398 kfree(cdata->res.server_scope);
7399 kfree(cdata->res.server_owner);
7400 kfree(cdata);
7401}
7402
7403static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7404 .rpc_call_done = nfs4_exchange_id_done,
7405 .rpc_release = nfs4_exchange_id_release,
7406};
7407
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007408/*
7409 * _nfs4_proc_exchange_id()
7410 *
7411 * Wrapper for EXCHANGE_ID operation.
7412 */
7413static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007414 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007415{
7416 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007417 struct rpc_message msg = {
7418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007419 .rpc_cred = cred,
7420 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007421 struct rpc_task_setup task_setup_data = {
7422 .rpc_client = clp->cl_rpcclient,
7423 .callback_ops = &nfs4_exchange_id_call_ops,
7424 .rpc_message = &msg,
7425 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7426 };
7427 struct nfs41_exchange_id_data *calldata;
7428 struct rpc_task *task;
7429 int status = -EIO;
7430
7431 if (!atomic_inc_not_zero(&clp->cl_count))
7432 goto out;
7433
7434 status = -ENOMEM;
7435 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7436 if (!calldata)
7437 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007438
Andy Adamsonad0849a2016-09-09 09:22:28 -04007439 if (!xprt)
7440 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007441
7442 status = nfs4_init_uniform_client_string(clp);
7443 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007444 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007445
7446 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007447 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007448 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007449
Andy Adamson8d89bd72016-09-09 09:22:18 -04007450 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7451 GFP_NOFS);
7452 status = -ENOMEM;
7453 if (unlikely(calldata->res.server_owner == NULL))
7454 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007455
Andy Adamson8d89bd72016-09-09 09:22:18 -04007456 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007457 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007458 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007459 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007460
Andy Adamson8d89bd72016-09-09 09:22:18 -04007461 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7462 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007463 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007464
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007465 switch (sp4_how) {
7466 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007467 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007468 break;
7469
7470 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007472 break;
7473
7474 default:
7475 /* unsupported! */
7476 WARN_ON_ONCE(1);
7477 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007478 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007479 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007480 if (xprt) {
7481 calldata->xprt = xprt;
7482 task_setup_data.rpc_xprt = xprt;
7483 task_setup_data.flags =
7484 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7485 calldata->args.verifier = &clp->cl_confirm;
7486 } else {
7487 calldata->args.verifier = &verifier;
7488 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007489 calldata->args.client = clp;
7490#ifdef CONFIG_NFS_V4_1_MIGRATION
7491 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7492 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7493 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7494#else
7495 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7496 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7497#endif
7498 msg.rpc_argp = &calldata->args;
7499 msg.rpc_resp = &calldata->res;
7500 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007501
Andy Adamson8d89bd72016-09-09 09:22:18 -04007502 task = rpc_run_task(&task_setup_data);
7503 if (IS_ERR(task)) {
7504 status = PTR_ERR(task);
7505 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007506 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007507
Andy Adamsonad0849a2016-09-09 09:22:28 -04007508 if (!xprt) {
7509 status = rpc_wait_for_completion_task(task);
7510 if (!status)
7511 status = calldata->rpc_status;
7512 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007513 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007514
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007516out:
Chuck Lever177313f2012-05-21 22:44:58 -04007517 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007518 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007519 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007520 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007521 clp->cl_implid->date.seconds,
7522 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007523 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007524 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007525
7526out_impl_id:
7527 kfree(calldata->res.impl_id);
7528out_server_scope:
7529 kfree(calldata->res.server_scope);
7530out_server_owner:
7531 kfree(calldata->res.server_owner);
7532out_calldata:
7533 kfree(calldata);
7534 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007535}
7536
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007537/*
7538 * nfs4_proc_exchange_id()
7539 *
7540 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7541 *
7542 * Since the clientid has expired, all compounds using sessions
7543 * associated with the stale clientid will be returning
7544 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7545 * be in some phase of session reset.
7546 *
7547 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7548 */
7549int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7550{
7551 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7552 int status;
7553
7554 /* try SP4_MACH_CRED if krb5i/p */
7555 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7556 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007557 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007558 if (!status)
7559 return 0;
7560 }
7561
7562 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007563 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007564}
7565
Andy Adamson04fa2c62016-09-09 09:22:29 -04007566/**
7567 * nfs4_test_session_trunk
7568 *
7569 * This is an add_xprt_test() test function called from
7570 * rpc_clnt_setup_test_and_add_xprt.
7571 *
7572 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7573 * and is dereferrenced in nfs4_exchange_id_release
7574 *
7575 * Upon success, add the new transport to the rpc_clnt
7576 *
7577 * @clnt: struct rpc_clnt to get new transport
7578 * @xprt: the rpc_xprt to test
7579 * @data: call data for _nfs4_proc_exchange_id.
7580 */
7581int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7582 void *data)
7583{
7584 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7585 u32 sp4_how;
7586
7587 dprintk("--> %s try %s\n", __func__,
7588 xprt->address_strings[RPC_DISPLAY_ADDR]);
7589
7590 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7591
7592 /* Test connection for session trunking. Async exchange_id call */
7593 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7594}
7595EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7596
Trond Myklebust66245532012-05-25 17:18:09 -04007597static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7598 struct rpc_cred *cred)
7599{
7600 struct rpc_message msg = {
7601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7602 .rpc_argp = clp,
7603 .rpc_cred = cred,
7604 };
7605 int status;
7606
7607 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007608 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007609 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007610 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007611 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7612 return status;
7613}
7614
7615static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7616 struct rpc_cred *cred)
7617{
7618 unsigned int loop;
7619 int ret;
7620
7621 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7622 ret = _nfs4_proc_destroy_clientid(clp, cred);
7623 switch (ret) {
7624 case -NFS4ERR_DELAY:
7625 case -NFS4ERR_CLIENTID_BUSY:
7626 ssleep(1);
7627 break;
7628 default:
7629 return ret;
7630 }
7631 }
7632 return 0;
7633}
7634
7635int nfs4_destroy_clientid(struct nfs_client *clp)
7636{
7637 struct rpc_cred *cred;
7638 int ret = 0;
7639
7640 if (clp->cl_mvops->minor_version < 1)
7641 goto out;
7642 if (clp->cl_exchange_flags == 0)
7643 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007644 if (clp->cl_preserve_clid)
7645 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007646 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007647 ret = nfs4_proc_destroy_clientid(clp, cred);
7648 if (cred)
7649 put_rpccred(cred);
7650 switch (ret) {
7651 case 0:
7652 case -NFS4ERR_STALE_CLIENTID:
7653 clp->cl_exchange_flags = 0;
7654 }
7655out:
7656 return ret;
7657}
7658
Andy Adamson2050f0c2009-04-01 09:22:30 -04007659struct nfs4_get_lease_time_data {
7660 struct nfs4_get_lease_time_args *args;
7661 struct nfs4_get_lease_time_res *res;
7662 struct nfs_client *clp;
7663};
7664
7665static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7666 void *calldata)
7667{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007668 struct nfs4_get_lease_time_data *data =
7669 (struct nfs4_get_lease_time_data *)calldata;
7670
7671 dprintk("--> %s\n", __func__);
7672 /* just setup sequence, do not trigger session recovery
7673 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007674 nfs41_setup_sequence(data->clp->cl_session,
7675 &data->args->la_seq_args,
7676 &data->res->lr_seq_res,
7677 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007678 dprintk("<-- %s\n", __func__);
7679}
7680
7681/*
7682 * Called from nfs4_state_manager thread for session setup, so don't recover
7683 * from sequence operation or clientid errors.
7684 */
7685static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7686{
7687 struct nfs4_get_lease_time_data *data =
7688 (struct nfs4_get_lease_time_data *)calldata;
7689
7690 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007691 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7692 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007693 switch (task->tk_status) {
7694 case -NFS4ERR_DELAY:
7695 case -NFS4ERR_GRACE:
7696 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7697 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7698 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007699 /* fall through */
7700 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007701 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007702 return;
7703 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007704 dprintk("<-- %s\n", __func__);
7705}
7706
Trond Myklebust17280172012-03-11 13:11:00 -04007707static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007708 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7709 .rpc_call_done = nfs4_get_lease_time_done,
7710};
7711
7712int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7713{
7714 struct rpc_task *task;
7715 struct nfs4_get_lease_time_args args;
7716 struct nfs4_get_lease_time_res res = {
7717 .lr_fsinfo = fsinfo,
7718 };
7719 struct nfs4_get_lease_time_data data = {
7720 .args = &args,
7721 .res = &res,
7722 .clp = clp,
7723 };
7724 struct rpc_message msg = {
7725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7726 .rpc_argp = &args,
7727 .rpc_resp = &res,
7728 };
7729 struct rpc_task_setup task_setup = {
7730 .rpc_client = clp->cl_rpcclient,
7731 .rpc_message = &msg,
7732 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007733 .callback_data = &data,
7734 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007735 };
7736 int status;
7737
Chuck Levera9c92d62013-08-09 12:48:18 -04007738 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007739 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007740 dprintk("--> %s\n", __func__);
7741 task = rpc_run_task(&task_setup);
7742
7743 if (IS_ERR(task))
7744 status = PTR_ERR(task);
7745 else {
7746 status = task->tk_status;
7747 rpc_put_task(task);
7748 }
7749 dprintk("<-- %s return %d\n", __func__, status);
7750
7751 return status;
7752}
7753
Andy Adamsonfc931582009-04-01 09:22:31 -04007754/*
7755 * Initialize the values to be used by the client in CREATE_SESSION
7756 * If nfs4_init_session set the fore channel request and response sizes,
7757 * use them.
7758 *
7759 * Set the back channel max_resp_sz_cached to zero to force the client to
7760 * always set csa_cachethis to FALSE because the current implementation
7761 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7762 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007763static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7764 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007765{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007766 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007767 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007768
Andy Adamson18aad3d2013-06-26 12:21:49 -04007769 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7770 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7771
Andy Adamsonfc931582009-04-01 09:22:31 -04007772 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007773 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7774 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007775 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007776 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007777
7778 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007779 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007780 __func__,
7781 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007782 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007783
7784 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007785 args->bc_attrs.max_rqst_sz = max_bc_payload;
7786 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007787 args->bc_attrs.max_resp_sz_cached = 0;
7788 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007789 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007790
7791 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7792 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7793 __func__,
7794 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7795 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7796 args->bc_attrs.max_reqs);
7797}
7798
Trond Myklebust79969dd2015-02-18 11:30:18 -08007799static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7800 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007801{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007802 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007803 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007804
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007805 if (rcvd->max_resp_sz > sent->max_resp_sz)
7806 return -EINVAL;
7807 /*
7808 * Our requested max_ops is the minimum we need; we're not
7809 * prepared to break up compounds into smaller pieces than that.
7810 * So, no point even trying to continue if the server won't
7811 * cooperate:
7812 */
7813 if (rcvd->max_ops < sent->max_ops)
7814 return -EINVAL;
7815 if (rcvd->max_reqs == 0)
7816 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007817 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7818 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007819 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007820}
7821
Trond Myklebust79969dd2015-02-18 11:30:18 -08007822static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7823 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007824{
7825 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007826 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007827
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007828 if (!(res->flags & SESSION4_BACK_CHAN))
7829 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007830 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7831 return -EINVAL;
7832 if (rcvd->max_resp_sz < sent->max_resp_sz)
7833 return -EINVAL;
7834 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7835 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007836 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007837 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007838 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007839 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007840out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007841 return 0;
7842}
Andy Adamson8d353012009-04-01 09:22:32 -04007843
Andy Adamson8d353012009-04-01 09:22:32 -04007844static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007845 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007846{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007847 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007848
Trond Myklebust79969dd2015-02-18 11:30:18 -08007849 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007850 if (ret)
7851 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007852 return nfs4_verify_back_channel_attrs(args, res);
7853}
7854
7855static void nfs4_update_session(struct nfs4_session *session,
7856 struct nfs41_create_session_res *res)
7857{
7858 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007859 /* Mark client id and session as being confirmed */
7860 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7861 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007862 session->flags = res->flags;
7863 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007864 if (res->flags & SESSION4_BACK_CHAN)
7865 memcpy(&session->bc_attrs, &res->bc_attrs,
7866 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007867}
7868
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007869static int _nfs4_proc_create_session(struct nfs_client *clp,
7870 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007871{
7872 struct nfs4_session *session = clp->cl_session;
7873 struct nfs41_create_session_args args = {
7874 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007875 .clientid = clp->cl_clientid,
7876 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007877 .cb_program = NFS4_CALLBACK,
7878 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007879 struct nfs41_create_session_res res;
7880
Andy Adamsonfc931582009-04-01 09:22:31 -04007881 struct rpc_message msg = {
7882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7883 .rpc_argp = &args,
7884 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007885 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007886 };
7887 int status;
7888
Chuck Lever6b26cc82016-05-02 14:40:40 -04007889 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007890 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007891
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007892 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007893 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007894
Trond Myklebustb519d402016-09-11 14:50:01 -04007895 switch (status) {
7896 case -NFS4ERR_STALE_CLIENTID:
7897 case -NFS4ERR_DELAY:
7898 case -ETIMEDOUT:
7899 case -EACCES:
7900 case -EAGAIN:
7901 goto out;
7902 };
7903
7904 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007905 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007906 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007907 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007908 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007909 if (status)
7910 goto out;
7911 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007912 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007913out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007914 return status;
7915}
7916
7917/*
7918 * Issues a CREATE_SESSION operation to the server.
7919 * It is the responsibility of the caller to verify the session is
7920 * expired before calling this routine.
7921 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007922int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007923{
7924 int status;
7925 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007926 struct nfs4_session *session = clp->cl_session;
7927
7928 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7929
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007930 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007931 if (status)
7932 goto out;
7933
Andy Adamsonaacd5532011-11-09 13:58:21 -05007934 /* Init or reset the session slot tables */
7935 status = nfs4_setup_session_slot_tables(session);
7936 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007937 if (status)
7938 goto out;
7939
7940 ptr = (unsigned *)&session->sess_id.data[0];
7941 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7942 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007943out:
7944 dprintk("<-- %s\n", __func__);
7945 return status;
7946}
7947
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007948/*
7949 * Issue the over-the-wire RPC DESTROY_SESSION.
7950 * The caller must serialize access to this routine.
7951 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007952int nfs4_proc_destroy_session(struct nfs4_session *session,
7953 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007954{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007955 struct rpc_message msg = {
7956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7957 .rpc_argp = session,
7958 .rpc_cred = cred,
7959 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007960 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007961
7962 dprintk("--> nfs4_proc_destroy_session\n");
7963
7964 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007965 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7966 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007967
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007968 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007969 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007970
7971 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007972 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007973 "Session has been destroyed regardless...\n", status);
7974
7975 dprintk("<-- nfs4_proc_destroy_session\n");
7976 return status;
7977}
7978
Trond Myklebust7b38c362012-05-23 13:23:31 -04007979/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007980 * Renew the cl_session lease.
7981 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007982struct nfs4_sequence_data {
7983 struct nfs_client *clp;
7984 struct nfs4_sequence_args args;
7985 struct nfs4_sequence_res res;
7986};
7987
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007988static void nfs41_sequence_release(void *data)
7989{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007990 struct nfs4_sequence_data *calldata = data;
7991 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007992
Alexandros Batsakis71358402010-02-05 03:45:05 -08007993 if (atomic_read(&clp->cl_count) > 1)
7994 nfs4_schedule_state_renewal(clp);
7995 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007996 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007997}
7998
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007999static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8000{
8001 switch(task->tk_status) {
8002 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008003 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8004 return -EAGAIN;
8005 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008006 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008007 }
8008 return 0;
8009}
8010
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008011static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008012{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008013 struct nfs4_sequence_data *calldata = data;
8014 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008015
Trond Myklebust14516c32010-07-31 14:29:06 -04008016 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8017 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008018
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008019 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008020 if (task->tk_status < 0) {
8021 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008022 if (atomic_read(&clp->cl_count) == 1)
8023 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008024
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008025 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8026 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008027 return;
8028 }
8029 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008030 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008031out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008032 dprintk("<-- %s\n", __func__);
8033}
8034
8035static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8036{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008037 struct nfs4_sequence_data *calldata = data;
8038 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008039 struct nfs4_sequence_args *args;
8040 struct nfs4_sequence_res *res;
8041
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008042 args = task->tk_msg.rpc_argp;
8043 res = task->tk_msg.rpc_resp;
8044
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008045 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008046}
8047
8048static const struct rpc_call_ops nfs41_sequence_ops = {
8049 .rpc_call_done = nfs41_sequence_call_done,
8050 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008051 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008052};
8053
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008054static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8055 struct rpc_cred *cred,
8056 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008057{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008058 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008059 struct rpc_message msg = {
8060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8061 .rpc_cred = cred,
8062 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008063 struct rpc_task_setup task_setup_data = {
8064 .rpc_client = clp->cl_rpcclient,
8065 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008066 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008067 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008068 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008069
Alexandros Batsakis71358402010-02-05 03:45:05 -08008070 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008071 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008072 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008073 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008074 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008075 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008076 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008077 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008078 if (is_privileged)
8079 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008080 msg.rpc_argp = &calldata->args;
8081 msg.rpc_resp = &calldata->res;
8082 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008083 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008084
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008085 return rpc_run_task(&task_setup_data);
8086}
8087
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008088static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008089{
8090 struct rpc_task *task;
8091 int ret = 0;
8092
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008093 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008094 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008095 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008096 if (IS_ERR(task))
8097 ret = PTR_ERR(task);
8098 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008099 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008100 dprintk("<-- %s status=%d\n", __func__, ret);
8101 return ret;
8102}
8103
8104static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8105{
8106 struct rpc_task *task;
8107 int ret;
8108
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008109 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008110 if (IS_ERR(task)) {
8111 ret = PTR_ERR(task);
8112 goto out;
8113 }
8114 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008115 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008116 ret = task->tk_status;
8117 rpc_put_task(task);
8118out:
8119 dprintk("<-- %s status=%d\n", __func__, ret);
8120 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008121}
8122
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008123struct nfs4_reclaim_complete_data {
8124 struct nfs_client *clp;
8125 struct nfs41_reclaim_complete_args arg;
8126 struct nfs41_reclaim_complete_res res;
8127};
8128
8129static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8130{
8131 struct nfs4_reclaim_complete_data *calldata = data;
8132
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008133 nfs41_setup_sequence(calldata->clp->cl_session,
8134 &calldata->arg.seq_args,
8135 &calldata->res.seq_res,
8136 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008137}
8138
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008139static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8140{
8141 switch(task->tk_status) {
8142 case 0:
8143 case -NFS4ERR_COMPLETE_ALREADY:
8144 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8145 break;
8146 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008147 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008148 /* fall through */
8149 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008150 return -EAGAIN;
8151 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008152 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008153 }
8154 return 0;
8155}
8156
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008157static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8158{
8159 struct nfs4_reclaim_complete_data *calldata = data;
8160 struct nfs_client *clp = calldata->clp;
8161 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8162
8163 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008164 if (!nfs41_sequence_done(task, res))
8165 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008166
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008167 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008168 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8169 rpc_restart_call_prepare(task);
8170 return;
8171 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008172 dprintk("<-- %s\n", __func__);
8173}
8174
8175static void nfs4_free_reclaim_complete_data(void *data)
8176{
8177 struct nfs4_reclaim_complete_data *calldata = data;
8178
8179 kfree(calldata);
8180}
8181
8182static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8183 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8184 .rpc_call_done = nfs4_reclaim_complete_done,
8185 .rpc_release = nfs4_free_reclaim_complete_data,
8186};
8187
8188/*
8189 * Issue a global reclaim complete.
8190 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008191static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8192 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008193{
8194 struct nfs4_reclaim_complete_data *calldata;
8195 struct rpc_task *task;
8196 struct rpc_message msg = {
8197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008198 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008199 };
8200 struct rpc_task_setup task_setup_data = {
8201 .rpc_client = clp->cl_rpcclient,
8202 .rpc_message = &msg,
8203 .callback_ops = &nfs4_reclaim_complete_call_ops,
8204 .flags = RPC_TASK_ASYNC,
8205 };
8206 int status = -ENOMEM;
8207
8208 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008209 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008210 if (calldata == NULL)
8211 goto out;
8212 calldata->clp = clp;
8213 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008214
Chuck Levera9c92d62013-08-09 12:48:18 -04008215 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008216 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008217 msg.rpc_argp = &calldata->arg;
8218 msg.rpc_resp = &calldata->res;
8219 task_setup_data.callback_data = calldata;
8220 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008221 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008222 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008223 goto out;
8224 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008225 status = nfs4_wait_for_completion_rpc_task(task);
8226 if (status == 0)
8227 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008228 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008229 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008230out:
8231 dprintk("<-- %s status=%d\n", __func__, status);
8232 return status;
8233}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008234
8235static void
8236nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8237{
8238 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008239 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008240 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008241
8242 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008243 nfs41_setup_sequence(session, &lgp->args.seq_args,
8244 &lgp->res.seq_res, task);
8245 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008246}
8247
8248static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8249{
8250 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008251
8252 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008253 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008254 dprintk("<-- %s\n", __func__);
8255}
8256
8257static int
8258nfs4_layoutget_handle_exception(struct rpc_task *task,
8259 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8260{
Trond Myklebustee314c22012-10-01 17:25:48 -07008261 struct inode *inode = lgp->args.inode;
8262 struct nfs_server *server = NFS_SERVER(inode);
8263 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008264 int nfs4err = task->tk_status;
8265 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008266 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008267
Boaz Harroshed7e5422014-01-22 20:34:54 +02008268 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008269
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008270 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008271 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008272 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008273
8274 /*
8275 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8276 * on the file. set tk_status to -ENODATA to tell upper layer to
8277 * retry go inband.
8278 */
8279 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008280 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008281 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008282 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008283 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8284 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8285 */
8286 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008287 status = -EOVERFLOW;
8288 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008289 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008290 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008291 * (or clients) writing to the same RAID stripe except when
8292 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008293 *
8294 * Treat it like we would RECALLCONFLICT -- we retry for a little
8295 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008296 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008297 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008298 if (lgp->args.minlength == 0) {
8299 status = -EOVERFLOW;
8300 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008301 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008302 status = -EBUSY;
8303 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008304 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008305 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008306 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008307 case -NFS4ERR_DELEG_REVOKED:
8308 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008309 case -NFS4ERR_EXPIRED:
8310 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008311 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008312 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008313 lo = NFS_I(inode)->layout;
8314 /* If the open stateid was bad, then recover it. */
8315 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8316 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008317 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008318 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008319 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008320 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008321 break;
8322 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008323
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008324 /*
8325 * Mark the bad layout state as invalid, then retry
8326 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008327 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008328 spin_unlock(&inode->i_lock);
8329 pnfs_free_lseg_list(&head);
8330 status = -EAGAIN;
8331 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008332 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008333
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008334 err = nfs4_handle_exception(server, nfs4err, exception);
8335 if (!status) {
8336 if (exception->retry)
8337 status = -EAGAIN;
8338 else
8339 status = err;
8340 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008341out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008342 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008343 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008344}
8345
Idan Kedar85541162012-08-02 11:47:10 +03008346static size_t max_response_pages(struct nfs_server *server)
8347{
8348 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8349 return nfs_page_array_len(0, max_resp_sz);
8350}
8351
8352static void nfs4_free_pages(struct page **pages, size_t size)
8353{
8354 int i;
8355
8356 if (!pages)
8357 return;
8358
8359 for (i = 0; i < size; i++) {
8360 if (!pages[i])
8361 break;
8362 __free_page(pages[i]);
8363 }
8364 kfree(pages);
8365}
8366
8367static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8368{
8369 struct page **pages;
8370 int i;
8371
8372 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8373 if (!pages) {
8374 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8375 return NULL;
8376 }
8377
8378 for (i = 0; i < size; i++) {
8379 pages[i] = alloc_page(gfp_flags);
8380 if (!pages[i]) {
8381 dprintk("%s: failed to allocate page\n", __func__);
8382 nfs4_free_pages(pages, size);
8383 return NULL;
8384 }
8385 }
8386
8387 return pages;
8388}
8389
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008390static void nfs4_layoutget_release(void *calldata)
8391{
8392 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008393 struct inode *inode = lgp->args.inode;
8394 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008395 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008396
8397 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008398 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008399 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008400 put_nfs_open_context(lgp->args.ctx);
8401 kfree(calldata);
8402 dprintk("<-- %s\n", __func__);
8403}
8404
8405static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8406 .rpc_call_prepare = nfs4_layoutget_prepare,
8407 .rpc_call_done = nfs4_layoutget_done,
8408 .rpc_release = nfs4_layoutget_release,
8409};
8410
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008411struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008412nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008413{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008414 struct inode *inode = lgp->args.inode;
8415 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008416 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008417 struct rpc_task *task;
8418 struct rpc_message msg = {
8419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8420 .rpc_argp = &lgp->args,
8421 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008422 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008423 };
8424 struct rpc_task_setup task_setup_data = {
8425 .rpc_client = server->client,
8426 .rpc_message = &msg,
8427 .callback_ops = &nfs4_layoutget_call_ops,
8428 .callback_data = lgp,
8429 .flags = RPC_TASK_ASYNC,
8430 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008431 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008432 struct nfs4_exception exception = {
8433 .inode = inode,
8434 .timeout = *timeout,
8435 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008436 int status = 0;
8437
8438 dprintk("--> %s\n", __func__);
8439
Peng Tao4bd5a982014-11-17 11:05:17 +08008440 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8441 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8442
Idan Kedar85541162012-08-02 11:47:10 +03008443 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8444 if (!lgp->args.layout.pages) {
8445 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008446 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008447 }
8448 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8449
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008450 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008451 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008452 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008453
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008454 task = rpc_run_task(&task_setup_data);
8455 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008456 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008457 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008458 if (status == 0) {
8459 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8460 *timeout = exception.timeout;
8461 }
8462
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008463 trace_nfs4_layoutget(lgp->args.ctx,
8464 &lgp->args.range,
8465 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008466 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008467 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008468
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008469 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8470 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008471 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008472 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008473 rpc_put_task(task);
8474 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008475 if (status)
8476 return ERR_PTR(status);
8477 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008478}
8479
Benny Halevycbe82602011-05-22 19:52:37 +03008480static void
8481nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8482{
8483 struct nfs4_layoutreturn *lrp = calldata;
8484
8485 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008486 nfs41_setup_sequence(lrp->clp->cl_session,
8487 &lrp->args.seq_args,
8488 &lrp->res.seq_res,
8489 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008490}
8491
8492static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8493{
8494 struct nfs4_layoutreturn *lrp = calldata;
8495 struct nfs_server *server;
8496
8497 dprintk("--> %s\n", __func__);
8498
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008499 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008500 return;
8501
8502 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008503 switch (task->tk_status) {
8504 default:
8505 task->tk_status = 0;
8506 case 0:
8507 break;
8508 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008509 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008510 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008511 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008512 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008513 return;
8514 }
Benny Halevycbe82602011-05-22 19:52:37 +03008515 dprintk("<-- %s\n", __func__);
8516}
8517
8518static void nfs4_layoutreturn_release(void *calldata)
8519{
8520 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008521 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008522 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008523
8524 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008525 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008526 if (lrp->res.lrs_present) {
8527 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8528 &lrp->args.range,
8529 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008530 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008531 } else
8532 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008533 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008534 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008535 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008536 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008537 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008538 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008539 kfree(calldata);
8540 dprintk("<-- %s\n", __func__);
8541}
8542
8543static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8544 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8545 .rpc_call_done = nfs4_layoutreturn_done,
8546 .rpc_release = nfs4_layoutreturn_release,
8547};
8548
Peng Tao6c166052014-11-17 09:30:40 +08008549int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008550{
8551 struct rpc_task *task;
8552 struct rpc_message msg = {
8553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8554 .rpc_argp = &lrp->args,
8555 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008556 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008557 };
8558 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008559 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008560 .rpc_message = &msg,
8561 .callback_ops = &nfs4_layoutreturn_call_ops,
8562 .callback_data = lrp,
8563 };
Peng Tao6c166052014-11-17 09:30:40 +08008564 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008565
Andrew Elble99ade3c2015-12-02 09:39:51 -05008566 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8567 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8568 &task_setup_data.rpc_client, &msg);
8569
Benny Halevycbe82602011-05-22 19:52:37 +03008570 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008571 if (!sync) {
8572 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8573 if (!lrp->inode) {
8574 nfs4_layoutreturn_release(lrp);
8575 return -EAGAIN;
8576 }
8577 task_setup_data.flags |= RPC_TASK_ASYNC;
8578 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008579 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008580 task = rpc_run_task(&task_setup_data);
8581 if (IS_ERR(task))
8582 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008583 if (sync)
8584 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008585 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008586 dprintk("<-- %s status=%d\n", __func__, status);
8587 rpc_put_task(task);
8588 return status;
8589}
8590
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008591static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008592_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8593 struct pnfs_device *pdev,
8594 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008595{
8596 struct nfs4_getdeviceinfo_args args = {
8597 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008598 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8599 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008600 };
8601 struct nfs4_getdeviceinfo_res res = {
8602 .pdev = pdev,
8603 };
8604 struct rpc_message msg = {
8605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8606 .rpc_argp = &args,
8607 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008608 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008609 };
8610 int status;
8611
8612 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008613 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008614 if (res.notification & ~args.notify_types)
8615 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008616 if (res.notification != args.notify_types)
8617 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008618
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008619 dprintk("<-- %s status=%d\n", __func__, status);
8620
8621 return status;
8622}
8623
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008624int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8625 struct pnfs_device *pdev,
8626 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008627{
8628 struct nfs4_exception exception = { };
8629 int err;
8630
8631 do {
8632 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008633 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008634 &exception);
8635 } while (exception.retry);
8636 return err;
8637}
8638EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8639
Andy Adamson863a3c62011-03-23 13:27:54 +00008640static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8641{
8642 struct nfs4_layoutcommit_data *data = calldata;
8643 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008644 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008645
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008646 nfs41_setup_sequence(session,
8647 &data->args.seq_args,
8648 &data->res.seq_res,
8649 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008650}
8651
8652static void
8653nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8654{
8655 struct nfs4_layoutcommit_data *data = calldata;
8656 struct nfs_server *server = NFS_SERVER(data->args.inode);
8657
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008658 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008659 return;
8660
8661 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008662 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8663 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8664 case -NFS4ERR_BADLAYOUT: /* no layout */
8665 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008666 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008667 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008668 break;
8669 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008670 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008671 rpc_restart_call_prepare(task);
8672 return;
8673 }
8674 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008675}
8676
8677static void nfs4_layoutcommit_release(void *calldata)
8678{
8679 struct nfs4_layoutcommit_data *data = calldata;
8680
Andy Adamsondb29c082011-07-30 20:52:38 -04008681 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008682 nfs_post_op_update_inode_force_wcc(data->args.inode,
8683 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008684 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008685 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008686 kfree(data);
8687}
8688
8689static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8690 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8691 .rpc_call_done = nfs4_layoutcommit_done,
8692 .rpc_release = nfs4_layoutcommit_release,
8693};
8694
8695int
Andy Adamsonef311532011-03-12 02:58:10 -05008696nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008697{
8698 struct rpc_message msg = {
8699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8700 .rpc_argp = &data->args,
8701 .rpc_resp = &data->res,
8702 .rpc_cred = data->cred,
8703 };
8704 struct rpc_task_setup task_setup_data = {
8705 .task = &data->task,
8706 .rpc_client = NFS_CLIENT(data->args.inode),
8707 .rpc_message = &msg,
8708 .callback_ops = &nfs4_layoutcommit_ops,
8709 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008710 };
8711 struct rpc_task *task;
8712 int status = 0;
8713
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008714 dprintk("NFS: initiating layoutcommit call. sync %d "
8715 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008716 data->args.lastbytewritten,
8717 data->args.inode->i_ino);
8718
Trond Myklebust472e2592015-02-05 16:50:30 -05008719 if (!sync) {
8720 data->inode = nfs_igrab_and_active(data->args.inode);
8721 if (data->inode == NULL) {
8722 nfs4_layoutcommit_release(data);
8723 return -EAGAIN;
8724 }
8725 task_setup_data.flags = RPC_TASK_ASYNC;
8726 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008727 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008728 task = rpc_run_task(&task_setup_data);
8729 if (IS_ERR(task))
8730 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008731 if (sync)
8732 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008733 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008734 dprintk("%s: status %d\n", __func__, status);
8735 rpc_put_task(task);
8736 return status;
8737}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008738
Andy Adamson97431202013-08-08 10:57:56 -04008739/**
8740 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8741 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8742 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008743static int
8744_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008745 struct nfs_fsinfo *info,
8746 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008747{
8748 struct nfs41_secinfo_no_name_args args = {
8749 .style = SECINFO_STYLE_CURRENT_FH,
8750 };
8751 struct nfs4_secinfo_res res = {
8752 .flavors = flavors,
8753 };
8754 struct rpc_message msg = {
8755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8756 .rpc_argp = &args,
8757 .rpc_resp = &res,
8758 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008759 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008760 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008761 int status;
8762
8763 if (use_integrity) {
8764 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008765 cred = nfs4_get_clid_cred(server->nfs_client);
8766 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008767 }
8768
8769 dprintk("--> %s\n", __func__);
8770 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8771 &res.seq_res, 0);
8772 dprintk("<-- %s status=%d\n", __func__, status);
8773
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008774 if (cred)
8775 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008776
8777 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008778}
8779
8780static int
8781nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8782 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8783{
8784 struct nfs4_exception exception = { };
8785 int err;
8786 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008787 /* first try using integrity protection */
8788 err = -NFS4ERR_WRONGSEC;
8789
8790 /* try to use integrity protection with machine cred */
8791 if (_nfs4_is_integrity_protected(server->nfs_client))
8792 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8793 flavors, true);
8794
8795 /*
8796 * if unable to use integrity protection, or SECINFO with
8797 * integrity protection returns NFS4ERR_WRONGSEC (which is
8798 * disallowed by spec, but exists in deployed servers) use
8799 * the current filesystem's rpc_client and the user cred.
8800 */
8801 if (err == -NFS4ERR_WRONGSEC)
8802 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8803 flavors, false);
8804
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008805 switch (err) {
8806 case 0:
8807 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008808 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008809 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008810 default:
8811 err = nfs4_handle_exception(server, err, &exception);
8812 }
8813 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008814out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008815 return err;
8816}
8817
8818static int
8819nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8820 struct nfs_fsinfo *info)
8821{
8822 int err;
8823 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008824 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008825 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008826 struct nfs4_secinfo4 *secinfo;
8827 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008828
8829 page = alloc_page(GFP_KERNEL);
8830 if (!page) {
8831 err = -ENOMEM;
8832 goto out;
8833 }
8834
8835 flavors = page_address(page);
8836 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8837
8838 /*
8839 * Fall back on "guess and check" method if
8840 * the server doesn't support SECINFO_NO_NAME
8841 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008842 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008843 err = nfs4_find_root_sec(server, fhandle, info);
8844 goto out_freepage;
8845 }
8846 if (err)
8847 goto out_freepage;
8848
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008849 for (i = 0; i < flavors->num_flavors; i++) {
8850 secinfo = &flavors->flavors[i];
8851
8852 switch (secinfo->flavor) {
8853 case RPC_AUTH_NULL:
8854 case RPC_AUTH_UNIX:
8855 case RPC_AUTH_GSS:
8856 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8857 &secinfo->flavor_info);
8858 break;
8859 default:
8860 flavor = RPC_AUTH_MAXFLAVOR;
8861 break;
8862 }
8863
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008864 if (!nfs_auth_info_match(&server->auth_info, flavor))
8865 flavor = RPC_AUTH_MAXFLAVOR;
8866
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008867 if (flavor != RPC_AUTH_MAXFLAVOR) {
8868 err = nfs4_lookup_root_sec(server, fhandle,
8869 info, flavor);
8870 if (!err)
8871 break;
8872 }
8873 }
8874
8875 if (flavor == RPC_AUTH_MAXFLAVOR)
8876 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008877
8878out_freepage:
8879 put_page(page);
8880 if (err == -EACCES)
8881 return -EPERM;
8882out:
8883 return err;
8884}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008885
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008886static int _nfs41_test_stateid(struct nfs_server *server,
8887 nfs4_stateid *stateid,
8888 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008889{
8890 int status;
8891 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008892 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008893 };
8894 struct nfs41_test_stateid_res res;
8895 struct rpc_message msg = {
8896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8897 .rpc_argp = &args,
8898 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008899 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008900 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008901 struct rpc_clnt *rpc_client = server->client;
8902
8903 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8904 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008905
Chuck Lever38527b12012-07-11 16:30:23 -04008906 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008907 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008908 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008909 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008910 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008911 if (status != NFS_OK) {
8912 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008913 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008914 }
8915 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008916 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008917}
8918
Trond Myklebust43912bb2016-09-22 13:38:56 -04008919static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8920 int err, struct nfs4_exception *exception)
8921{
8922 exception->retry = 0;
8923 switch(err) {
8924 case -NFS4ERR_DELAY:
8925 nfs4_handle_exception(server, err, exception);
8926 break;
8927 case -NFS4ERR_BADSESSION:
8928 case -NFS4ERR_BADSLOT:
8929 case -NFS4ERR_BAD_HIGH_SLOT:
8930 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8931 case -NFS4ERR_DEADSESSION:
8932 nfs4_do_handle_exception(server, err, exception);
8933 }
8934}
8935
Chuck Lever38527b12012-07-11 16:30:23 -04008936/**
8937 * nfs41_test_stateid - perform a TEST_STATEID operation
8938 *
8939 * @server: server / transport on which to perform the operation
8940 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008941 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008942 *
8943 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8944 * Otherwise a negative NFS4ERR value is returned if the operation
8945 * failed or the state ID is not currently valid.
8946 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008947static int nfs41_test_stateid(struct nfs_server *server,
8948 nfs4_stateid *stateid,
8949 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008950{
8951 struct nfs4_exception exception = { };
8952 int err;
8953 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008954 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008955 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008956 } while (exception.retry);
8957 return err;
8958}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008959
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008960struct nfs_free_stateid_data {
8961 struct nfs_server *server;
8962 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008963 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008964};
8965
8966static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8967{
8968 struct nfs_free_stateid_data *data = calldata;
8969 nfs41_setup_sequence(nfs4_get_session(data->server),
8970 &data->args.seq_args,
8971 &data->res.seq_res,
8972 task);
8973}
8974
8975static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8976{
8977 struct nfs_free_stateid_data *data = calldata;
8978
8979 nfs41_sequence_done(task, &data->res.seq_res);
8980
8981 switch (task->tk_status) {
8982 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008983 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008984 rpc_restart_call_prepare(task);
8985 }
8986}
8987
8988static void nfs41_free_stateid_release(void *calldata)
8989{
8990 kfree(calldata);
8991}
8992
Trond Myklebust17f26b12013-08-21 15:48:42 -04008993static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008994 .rpc_call_prepare = nfs41_free_stateid_prepare,
8995 .rpc_call_done = nfs41_free_stateid_done,
8996 .rpc_release = nfs41_free_stateid_release,
8997};
8998
8999static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009000 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009001 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009002 bool privileged)
9003{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009004 struct rpc_message msg = {
9005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009006 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009007 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009008 struct rpc_task_setup task_setup = {
9009 .rpc_client = server->client,
9010 .rpc_message = &msg,
9011 .callback_ops = &nfs41_free_stateid_ops,
9012 .flags = RPC_TASK_ASYNC,
9013 };
9014 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009015
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009016 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9017 &task_setup.rpc_client, &msg);
9018
Chuck Lever38527b12012-07-11 16:30:23 -04009019 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009020 data = kmalloc(sizeof(*data), GFP_NOFS);
9021 if (!data)
9022 return ERR_PTR(-ENOMEM);
9023 data->server = server;
9024 nfs4_stateid_copy(&data->args.stateid, stateid);
9025
9026 task_setup.callback_data = data;
9027
9028 msg.rpc_argp = &data->args;
9029 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04009030 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009031 if (privileged)
9032 nfs4_set_sequence_privileged(&data->args.seq_args);
9033
9034 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009035}
9036
Chuck Lever38527b12012-07-11 16:30:23 -04009037/**
9038 * nfs41_free_stateid - perform a FREE_STATEID operation
9039 *
9040 * @server: server / transport on which to perform the operation
9041 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009042 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009043 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009044 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009045 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009046 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009047static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009048 const nfs4_stateid *stateid,
9049 struct rpc_cred *cred,
9050 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009051{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009052 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009053
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009054 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009055 if (IS_ERR(task))
9056 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009057 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009058 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009059}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009060
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009061static void
9062nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009063{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009064 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009065
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009066 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009067 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009068}
9069
Trond Myklebust36281ca2012-03-04 18:13:56 -05009070static bool nfs41_match_stateid(const nfs4_stateid *s1,
9071 const nfs4_stateid *s2)
9072{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009073 if (s1->type != s2->type)
9074 return false;
9075
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009076 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009077 return false;
9078
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009079 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009080 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009081 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009082 return true;
9083
9084 return false;
9085}
9086
Andy Adamson557134a2009-04-01 09:21:53 -04009087#endif /* CONFIG_NFS_V4_1 */
9088
Trond Myklebust36281ca2012-03-04 18:13:56 -05009089static bool nfs4_match_stateid(const nfs4_stateid *s1,
9090 const nfs4_stateid *s2)
9091{
Trond Myklebustf597c532012-03-04 18:13:56 -05009092 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009093}
9094
9095
Trond Myklebust17280172012-03-11 13:11:00 -04009096static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009097 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009098 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099 .recover_open = nfs4_open_reclaim,
9100 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009101 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009102 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103};
9104
Andy Adamson591d71c2009-04-01 09:22:47 -04009105#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009106static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009107 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9108 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9109 .recover_open = nfs4_open_reclaim,
9110 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009111 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009112 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009113 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009114};
9115#endif /* CONFIG_NFS_V4_1 */
9116
Trond Myklebust17280172012-03-11 13:11:00 -04009117static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009118 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009119 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009120 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009122 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009123};
9124
Andy Adamson591d71c2009-04-01 09:22:47 -04009125#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009126static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009127 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9128 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009129 .recover_open = nfs41_open_expired,
9130 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009131 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009132};
9133#endif /* CONFIG_NFS_V4_1 */
9134
Trond Myklebust17280172012-03-11 13:11:00 -04009135static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009136 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009137 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009138 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009139};
9140
9141#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009142static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009143 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009144 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009145 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009146};
9147#endif
9148
Chuck Leverec011fe2013-10-17 14:12:39 -04009149static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009150 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009151 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009152};
9153
9154#if defined(CONFIG_NFS_V4_1)
9155static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009156 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009157 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009158};
9159#endif /* CONFIG_NFS_V4_1 */
9160
Trond Myklebust97dc1352010-06-16 09:52:26 -04009161static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9162 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009163 .init_caps = NFS_CAP_READDIRPLUS
9164 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009165 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009166 .init_client = nfs40_init_client,
9167 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009168 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009169 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009170 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009171 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009172 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009173 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009174 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9175 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9176 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009177 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009178};
9179
9180#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009181static struct nfs_seqid *
9182nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9183{
9184 return NULL;
9185}
9186
Trond Myklebust97dc1352010-06-16 09:52:26 -04009187static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9188 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009189 .init_caps = NFS_CAP_READDIRPLUS
9190 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009191 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009192 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009193 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009194 .init_client = nfs41_init_client,
9195 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009196 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009197 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009198 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009199 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009200 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009201 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009202 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009203 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9204 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9205 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009206 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009207};
9208#endif
9209
Steve Dickson42c2c422013-05-22 12:50:38 -04009210#if defined(CONFIG_NFS_V4_2)
9211static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9212 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009213 .init_caps = NFS_CAP_READDIRPLUS
9214 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009215 | NFS_CAP_POSIX_LOCK
9216 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009217 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009218 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009219 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009220 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009221 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009222 | NFS_CAP_LAYOUTSTATS
9223 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009224 .init_client = nfs41_init_client,
9225 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009226 .match_stateid = nfs41_match_stateid,
9227 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009228 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009229 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009230 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009231 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009232 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009233 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9234 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9235 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009236 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009237};
9238#endif
9239
Trond Myklebust97dc1352010-06-16 09:52:26 -04009240const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9241 [0] = &nfs_v4_0_minor_ops,
9242#if defined(CONFIG_NFS_V4_1)
9243 [1] = &nfs_v4_1_minor_ops,
9244#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009245#if defined(CONFIG_NFS_V4_2)
9246 [2] = &nfs_v4_2_minor_ops,
9247#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009248};
9249
Trond Myklebust13997822016-07-24 17:10:52 -04009250static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009251{
9252 ssize_t error, error2;
9253
9254 error = generic_listxattr(dentry, list, size);
9255 if (error < 0)
9256 return error;
9257 if (list) {
9258 list += error;
9259 size -= error;
9260 }
9261
9262 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9263 if (error2 < 0)
9264 return error2;
9265 return error + error2;
9266}
9267
Trond Myklebust17f26b12013-08-21 15:48:42 -04009268static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009269 .create = nfs_create,
9270 .lookup = nfs_lookup,
9271 .atomic_open = nfs_atomic_open,
9272 .link = nfs_link,
9273 .unlink = nfs_unlink,
9274 .symlink = nfs_symlink,
9275 .mkdir = nfs_mkdir,
9276 .rmdir = nfs_rmdir,
9277 .mknod = nfs_mknod,
9278 .rename = nfs_rename,
9279 .permission = nfs_permission,
9280 .getattr = nfs_getattr,
9281 .setattr = nfs_setattr,
9282 .getxattr = generic_getxattr,
9283 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009284 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009285 .removexattr = generic_removexattr,
9286};
9287
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009288static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009289 .permission = nfs_permission,
9290 .getattr = nfs_getattr,
9291 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009292 .getxattr = generic_getxattr,
9293 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009294 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009295 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009296};
9297
David Howells509de812006-08-22 20:06:11 -04009298const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299 .version = 4, /* protocol version */
9300 .dentry_ops = &nfs4_dentry_operations,
9301 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009302 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009303 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009305 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009306 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307 .getattr = nfs4_proc_getattr,
9308 .setattr = nfs4_proc_setattr,
9309 .lookup = nfs4_proc_lookup,
9310 .access = nfs4_proc_access,
9311 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 .create = nfs4_proc_create,
9313 .remove = nfs4_proc_remove,
9314 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009315 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009317 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009318 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009319 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320 .link = nfs4_proc_link,
9321 .symlink = nfs4_proc_symlink,
9322 .mkdir = nfs4_proc_mkdir,
9323 .rmdir = nfs4_proc_remove,
9324 .readdir = nfs4_proc_readdir,
9325 .mknod = nfs4_proc_mknod,
9326 .statfs = nfs4_proc_statfs,
9327 .fsinfo = nfs4_proc_fsinfo,
9328 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009329 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009331 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009333 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009335 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009336 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009337 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009338 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009340 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009341 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009342 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009343 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009344 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009345 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009346 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009347 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009348 .create_server = nfs4_create_server,
9349 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009350};
9351
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009352static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009353 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009354 .list = nfs4_xattr_list_nfs4_acl,
9355 .get = nfs4_xattr_get_nfs4_acl,
9356 .set = nfs4_xattr_set_nfs4_acl,
9357};
9358
9359const struct xattr_handler *nfs4_xattr_handlers[] = {
9360 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009361#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9362 &nfs4_xattr_nfs4_label_handler,
9363#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009364 NULL
9365};
9366
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367/*
9368 * Local variables:
9369 * c-basic-offset: 8
9370 * End:
9371 */