blob: 79109c84f18d1dc157e1b4ddc365ddbfd91a4677 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400102static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Trond Myklebust26d36302016-09-22 13:39:05 -0400331static void nfs4_test_and_free_stateid(struct nfs_server *server,
332 nfs4_stateid *stateid,
333 struct rpc_cred *cred)
334{
335 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
336
337 ops->test_and_free_expired(server, stateid, cred);
338}
339
340static void __nfs4_free_revoked_stateid(struct nfs_server *server,
341 nfs4_stateid *stateid,
342 struct rpc_cred *cred)
343{
344 stateid->type = NFS4_REVOKED_STATEID_TYPE;
345 nfs4_test_and_free_stateid(server, stateid, cred);
346}
347
348static void nfs4_free_revoked_stateid(struct nfs_server *server,
349 const nfs4_stateid *stateid,
350 struct rpc_cred *cred)
351{
352 nfs4_stateid tmp;
353
354 nfs4_stateid_copy(&tmp, stateid);
355 __nfs4_free_revoked_stateid(server, &tmp, cred);
356}
357
NeilBrown8478eaa2014-09-18 16:09:27 +1000358static long nfs4_update_delay(long *timeout)
359{
360 long ret;
361 if (!timeout)
362 return NFS4_POLL_RETRY_MAX;
363 if (*timeout <= 0)
364 *timeout = NFS4_POLL_RETRY_MIN;
365 if (*timeout > NFS4_POLL_RETRY_MAX)
366 *timeout = NFS4_POLL_RETRY_MAX;
367 ret = *timeout;
368 *timeout <<= 1;
369 return ret;
370}
371
Trond Myklebust65de8722008-12-23 15:21:44 -0500372static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
373{
374 int res = 0;
375
376 might_sleep();
377
NeilBrown8478eaa2014-09-18 16:09:27 +1000378 freezable_schedule_timeout_killable_unsafe(
379 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 if (fatal_signal_pending(current))
381 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 return res;
383}
384
385/* This is the error handling routine for processes that are allowed
386 * to sleep.
387 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400388static int nfs4_do_handle_exception(struct nfs_server *server,
389 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500390{
391 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500392 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400393 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500394 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 int ret = errorcode;
396
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400397 exception->delay = 0;
398 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400400
401 if (stateid == NULL && state != NULL)
402 stateid = &state->stateid;
403
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 switch(errorcode) {
405 case 0:
406 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400407 case -NFS4ERR_DELEG_REVOKED:
408 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400410 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400411 if (inode != NULL && stateid != NULL) {
412 nfs_inode_find_state_and_recover(inode,
413 stateid);
414 goto wait_on_recovery;
415 }
416 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400417 if (inode) {
418 int err;
419
420 err = nfs_async_inode_return_delegation(inode,
421 stateid);
422 if (err == 0)
423 goto wait_on_recovery;
424 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
425 exception->retry = 1;
426 break;
427 }
428 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500429 if (state == NULL)
430 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400431 ret = nfs4_schedule_stateid_recovery(server, state);
432 if (ret < 0)
433 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500434 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500436 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500437 nfs4_schedule_lease_recovery(clp);
438 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400439 case -NFS4ERR_MOVED:
440 ret = nfs4_schedule_migration_recovery(server);
441 if (ret < 0)
442 break;
443 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400444 case -NFS4ERR_LEASE_MOVED:
445 nfs4_schedule_lease_moved_recovery(clp);
446 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500447#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400448 case -NFS4ERR_BADSESSION:
449 case -NFS4ERR_BADSLOT:
450 case -NFS4ERR_BAD_HIGH_SLOT:
451 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
452 case -NFS4ERR_DEADSESSION:
453 case -NFS4ERR_SEQ_FALSE_RETRY:
454 case -NFS4ERR_SEQ_MISORDERED:
455 dprintk("%s ERROR: %d Reset session\n", __func__,
456 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400457 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400458 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500459#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500460 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500461 if (exception->timeout > HZ) {
462 /* We have retried a decent amount, time to
463 * fail
464 */
465 ret = -EBUSY;
466 break;
467 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400469 nfs_inc_server_stats(server, NFSIOS_DELAY);
470 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400471 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400472 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400473 exception->delay = 1;
474 return 0;
475
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400476 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500477 case -NFS4ERR_OLD_STATEID:
478 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800479 break;
480 case -NFS4ERR_BADOWNER:
481 /* The following works around a Linux server bug! */
482 case -NFS4ERR_BADNAME:
483 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
484 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
485 exception->retry = 1;
486 printk(KERN_WARNING "NFS: v4 server %s "
487 "does not accept raw "
488 "uid/gids. "
489 "Reenabling the idmapper.\n",
490 server->nfs_client->cl_hostname);
491 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500492 }
493 /* We failed to handle the error */
494 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500495wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400496 exception->recovering = 1;
497 return 0;
498}
499
500/* This is the error handling routine for processes that are allowed
501 * to sleep.
502 */
503int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
504{
505 struct nfs_client *clp = server->nfs_client;
506 int ret;
507
508 ret = nfs4_do_handle_exception(server, errorcode, exception);
509 if (exception->delay) {
510 ret = nfs4_delay(server->client, &exception->timeout);
511 goto out_retry;
512 }
513 if (exception->recovering) {
514 ret = nfs4_wait_clnt_recover(clp);
515 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
516 return -EIO;
517 goto out_retry;
518 }
519 return ret;
520out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500521 if (ret == 0)
522 exception->retry = 1;
523 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500524}
525
Trond Myklebust037fc982015-09-20 15:51:00 -0400526static int
527nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
528 int errorcode, struct nfs4_exception *exception)
529{
530 struct nfs_client *clp = server->nfs_client;
531 int ret;
532
533 ret = nfs4_do_handle_exception(server, errorcode, exception);
534 if (exception->delay) {
535 rpc_delay(task, nfs4_update_delay(&exception->timeout));
536 goto out_retry;
537 }
538 if (exception->recovering) {
539 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
540 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
541 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
542 goto out_retry;
543 }
544 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
545 ret = -EIO;
546 return ret;
547out_retry:
548 if (ret == 0)
549 exception->retry = 1;
550 return ret;
551}
552
553static int
554nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
555 struct nfs4_state *state, long *timeout)
556{
557 struct nfs4_exception exception = {
558 .state = state,
559 };
560
561 if (task->tk_status >= 0)
562 return 0;
563 if (timeout)
564 exception.timeout = *timeout;
565 task->tk_status = nfs4_async_handle_exception(task, server,
566 task->tk_status,
567 &exception);
568 if (exception.delay && timeout)
569 *timeout = exception.timeout;
570 if (exception.retry)
571 return -EAGAIN;
572 return 0;
573}
574
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400575/*
576 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
577 * or 'false' otherwise.
578 */
579static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
580{
581 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
582
583 if (flavor == RPC_AUTH_GSS_KRB5I ||
584 flavor == RPC_AUTH_GSS_KRB5P)
585 return true;
586
587 return false;
588}
Trond Myklebust65de8722008-12-23 15:21:44 -0500589
Trond Myklebust452e9352010-07-31 14:29:06 -0400590static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock(&clp->cl_lock);
593 if (time_before(clp->cl_last_renewal,timestamp))
594 clp->cl_last_renewal = timestamp;
595 spin_unlock(&clp->cl_lock);
596}
597
Trond Myklebust452e9352010-07-31 14:29:06 -0400598static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
599{
Trond Myklebustbe824162015-07-05 14:50:46 -0400600 struct nfs_client *clp = server->nfs_client;
601
602 if (!nfs4_has_session(clp))
603 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400604}
605
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400606struct nfs4_call_sync_data {
607 const struct nfs_server *seq_server;
608 struct nfs4_sequence_args *seq_args;
609 struct nfs4_sequence_res *seq_res;
610};
611
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800612void nfs4_init_sequence(struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400614{
615 args->sa_slot = NULL;
616 args->sa_cache_this = cache_reply;
617 args->sa_privileged = 0;
618
619 res->sr_slot = NULL;
620}
621
622static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
623{
624 args->sa_privileged = 1;
625}
626
Peng Taocb04ad22014-06-11 05:24:15 +0800627int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
628 struct nfs4_sequence_args *args,
629 struct nfs4_sequence_res *res,
630 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400631{
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 struct nfs4_slot *slot;
633
634 /* slot already allocated? */
635 if (res->sr_slot != NULL)
636 goto out_start;
637
638 spin_lock(&tbl->slot_tbl_lock);
639 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
640 goto out_sleep;
641
642 slot = nfs4_alloc_slot(tbl);
643 if (IS_ERR(slot)) {
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
646 goto out_sleep;
647 }
648 spin_unlock(&tbl->slot_tbl_lock);
649
Trond Myklebust0a014a42016-09-22 13:38:51 -0400650 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400651 args->sa_slot = slot;
652 res->sr_slot = slot;
653
654out_start:
655 rpc_call_start(task);
656 return 0;
657
658out_sleep:
659 if (args->sa_privileged)
660 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
661 NULL, RPC_PRIORITY_PRIVILEGED);
662 else
663 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
664 spin_unlock(&tbl->slot_tbl_lock);
665 return -EAGAIN;
666}
Peng Taocb04ad22014-06-11 05:24:15 +0800667EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400668
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400669static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400670{
671 struct nfs4_slot *slot = res->sr_slot;
672 struct nfs4_slot_table *tbl;
673
Chuck Lever3bd23842013-08-09 12:49:19 -0400674 tbl = slot->table;
675 spin_lock(&tbl->slot_tbl_lock);
676 if (!nfs41_wake_and_assign_slot(tbl, slot))
677 nfs4_free_slot(tbl, slot);
678 spin_unlock(&tbl->slot_tbl_lock);
679
680 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400681}
682
683static int nfs40_sequence_done(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
685{
686 if (res->sr_slot != NULL)
687 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400688 return 1;
689}
690
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400691#if defined(CONFIG_NFS_V4_1)
692
Trond Myklebustd185a332010-06-16 09:52:25 -0400693static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400694{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500695 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400696 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500697 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500698 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400699
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500701 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500702
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400703 /* Bump the slot sequence number */
704 if (slot->seq_done)
705 slot->seq_nr++;
706 slot->seq_done = 0;
707
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500708 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500709 /* Be nice to the server: try to ensure that the last transmitted
710 * value for highest_user_slotid <= target_highest_slotid
711 */
712 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
713 send_new_highest_used_slotid = true;
714
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500716 send_new_highest_used_slotid = false;
717 goto out_unlock;
718 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500719 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500720
721 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
722 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500723out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500724 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400725 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500726 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400727 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400728 if (waitqueue_active(&tbl->slot_waitq))
729 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400730}
731
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400732static int nfs41_sequence_process(struct rpc_task *task,
733 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400734{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500735 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500736 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400737 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500738 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500739 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400740
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500741 if (slot == NULL)
742 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400743 /* don't increment the sequence number if the task wasn't sent */
744 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400745 goto out;
746
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500747 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500748
Trond Myklebustac20d162012-12-15 15:36:07 -0500749 if (slot->interrupted) {
750 slot->interrupted = 0;
751 interrupted = true;
752 }
753
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400754 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500755 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400756 switch (res->sr_status) {
757 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400758 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400759 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500760 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500761 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500762 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400763 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
764 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500765 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400766 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500767 case 1:
768 /*
769 * sr_status remains 1 if an RPC level error occurred.
770 * The server may or may not have processed the sequence
771 * operation..
772 * Mark the slot as having hosted an interrupted RPC call.
773 */
774 slot->interrupted = 1;
775 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 case -NFS4ERR_DELAY:
777 /* The server detected a resend of the RPC call and
778 * returned NFS4ERR_DELAY as per Section 2.10.6.2
779 * of RFC5661.
780 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500781 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400782 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500783 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500784 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400785 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500786 case -NFS4ERR_BADSLOT:
787 /*
788 * The slot id we used was probably retired. Try again
789 * using a different slot id.
790 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500791 goto retry_nowait;
792 case -NFS4ERR_SEQ_MISORDERED:
793 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500794 * Was the last operation on this sequence interrupted?
795 * If so, retry after bumping the sequence number.
796 */
797 if (interrupted) {
798 ++slot->seq_nr;
799 goto retry_nowait;
800 }
801 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500802 * Could this slot have been previously retired?
803 * If so, then the server may be expecting seq_nr = 1!
804 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500805 if (slot->seq_nr != 1) {
806 slot->seq_nr = 1;
807 goto retry_nowait;
808 }
809 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500810 case -NFS4ERR_SEQ_FALSE_RETRY:
811 ++slot->seq_nr;
812 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400813 default:
814 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400815 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400816 }
817out:
818 /* The session may be reset by one of the error handlers. */
819 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500820out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500821 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500822retry_nowait:
823 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400824 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500825 task->tk_status = 0;
826 ret = 0;
827 }
828 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400829out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400830 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 goto out;
832 rpc_delay(task, NFS4_POLL_RETRY_MAX);
833 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400834}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400835
836int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
837{
838 if (!nfs41_sequence_process(task, res))
839 return 0;
840 if (res->sr_slot != NULL)
841 nfs41_sequence_free_slot(res);
842 return 1;
843
844}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500845EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400846
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400847static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
848{
849 if (res->sr_slot == NULL)
850 return 1;
851 if (res->sr_slot->table->session != NULL)
852 return nfs41_sequence_process(task, res);
853 return nfs40_sequence_done(task, res);
854}
855
856static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
857{
858 if (res->sr_slot != NULL) {
859 if (res->sr_slot->table->session != NULL)
860 nfs41_sequence_free_slot(res);
861 else
862 nfs40_sequence_free_slot(res);
863 }
864}
865
Peng Tao2c4b1312014-06-11 05:24:16 +0800866int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400867{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500868 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400869 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400870 if (!res->sr_slot->table->session)
871 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400872 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400873}
Peng Tao2c4b1312014-06-11 05:24:16 +0800874EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400875
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000876int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400877 struct nfs4_sequence_args *args,
878 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400879 struct rpc_task *task)
880{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400881 struct nfs4_slot *slot;
882 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400883
884 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400885 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400886 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400887 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400888
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400889 tbl = &session->fc_slot_table;
890
Trond Myklebust69d206b2012-11-22 13:21:02 -0500891 task->tk_timeout = 0;
892
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400894 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400895 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500896 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500897 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400898 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400899 }
900
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500901 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500902 if (IS_ERR(slot)) {
903 /* If out of memory, try again in 1/4 second */
904 if (slot == ERR_PTR(-ENOMEM))
905 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400906 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400907 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400908 }
909 spin_unlock(&tbl->slot_tbl_lock);
910
Trond Myklebust0a014a42016-09-22 13:38:51 -0400911 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500912 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400913
Chuck Levere8d92382013-08-09 12:47:51 -0400914 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500915 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916
Benny Halevydfb4f3092010-09-24 09:17:01 -0400917 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500918 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400919 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400920 /*
921 * sr_status is only set in decode_sequence, and so will remain
922 * set to 1 if an rpc level failure occurs.
923 */
924 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400925 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400926out_success:
927 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400928 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400929out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400930 /* Privileged tasks are queued with top priority */
931 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400932 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
933 NULL, RPC_PRIORITY_PRIVILEGED);
934 else
935 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400936 spin_unlock(&tbl->slot_tbl_lock);
937 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000939EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400940
Chuck Lever5a580e02013-08-09 12:48:09 -0400941static int nfs4_setup_sequence(const struct nfs_server *server,
942 struct nfs4_sequence_args *args,
943 struct nfs4_sequence_res *res,
944 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400945{
Trond Myklebust035168a2010-06-16 09:52:26 -0400946 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947 int ret = 0;
948
Chuck Lever3bd23842013-08-09 12:49:19 -0400949 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800950 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
951 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400952
Chuck Levere8d92382013-08-09 12:47:51 -0400953 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400954 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400955 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400956
Trond Myklebust9d12b212012-01-17 22:04:25 -0500957 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400958
Andy Adamsonce5039c2009-04-01 09:22:13 -0400959 dprintk("<-- %s status=%d\n", __func__, ret);
960 return ret;
961}
962
Andy Adamsonce5039c2009-04-01 09:22:13 -0400963static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
964{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400965 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400966 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400967
Trond Myklebust035168a2010-06-16 09:52:26 -0400968 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
969
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400970 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400971}
972
Andy Adamson69ab40c2009-04-01 09:22:19 -0400973static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400976
Trond Myklebust14516c32010-07-31 14:29:06 -0400977 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400978}
979
Trond Myklebust17280172012-03-11 13:11:00 -0400980static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400982 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400983};
984
Chuck Lever3bd23842013-08-09 12:49:19 -0400985#else /* !CONFIG_NFS_V4_1 */
986
Chuck Lever5a580e02013-08-09 12:48:09 -0400987static int nfs4_setup_sequence(const struct nfs_server *server,
988 struct nfs4_sequence_args *args,
989 struct nfs4_sequence_res *res,
990 struct rpc_task *task)
991{
Peng Taocb04ad22014-06-11 05:24:15 +0800992 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
993 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400994}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400995
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400996static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
997{
998 return nfs40_sequence_done(task, res);
999}
1000
1001static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1002{
1003 if (res->sr_slot != NULL)
1004 nfs40_sequence_free_slot(res);
1005}
1006
Peng Tao2c4b1312014-06-11 05:24:16 +08001007int nfs4_sequence_done(struct rpc_task *task,
1008 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001009{
Chuck Lever3bd23842013-08-09 12:49:19 -04001010 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001011}
Peng Tao2c4b1312014-06-11 05:24:16 +08001012EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001013
1014#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001015
Chuck Lever9915ea72013-08-09 12:48:27 -04001016static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1017{
1018 struct nfs4_call_sync_data *data = calldata;
1019 nfs4_setup_sequence(data->seq_server,
1020 data->seq_args, data->seq_res, task);
1021}
1022
1023static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1024{
1025 struct nfs4_call_sync_data *data = calldata;
1026 nfs4_sequence_done(task, data->seq_res);
1027}
1028
1029static const struct rpc_call_ops nfs40_call_sync_ops = {
1030 .rpc_call_prepare = nfs40_call_sync_prepare,
1031 .rpc_call_done = nfs40_call_sync_done,
1032};
1033
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001034static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001035 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001039{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001040 int ret;
1041 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001042 struct nfs_client *clp = server->nfs_client;
1043 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001044 .seq_server = server,
1045 .seq_args = args,
1046 .seq_res = res,
1047 };
1048 struct rpc_task_setup task_setup = {
1049 .rpc_client = clnt,
1050 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001051 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 .callback_data = &data
1053 };
1054
1055 task = rpc_run_task(&task_setup);
1056 if (IS_ERR(task))
1057 ret = PTR_ERR(task);
1058 else {
1059 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001060 rpc_put_task(task);
1061 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 return ret;
1063}
1064
Bryan Schumaker7c513052011-03-24 17:12:24 +00001065int nfs4_call_sync(struct rpc_clnt *clnt,
1066 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001067 struct rpc_message *msg,
1068 struct nfs4_sequence_args *args,
1069 struct nfs4_sequence_res *res,
1070 int cache_reply)
1071{
Chuck Levera9c92d62013-08-09 12:48:18 -04001072 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001073 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1077{
1078 struct nfs_inode *nfsi = NFS_I(dir);
1079
1080 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001081 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001082 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001084 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001085 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001086 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 spin_unlock(&dir->i_lock);
1088}
1089
1090struct nfs4_opendata {
1091 struct kref kref;
1092 struct nfs_openargs o_arg;
1093 struct nfs_openres o_res;
1094 struct nfs_open_confirmargs c_arg;
1095 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001096 struct nfs4_string owner_name;
1097 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001098 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001100 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001102 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct nfs4_state_owner *owner;
1104 struct nfs4_state *state;
1105 struct iattr attrs;
1106 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001107 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001108 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001109 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001110 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 int cancelled;
1112};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001113
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001114static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1115 int err, struct nfs4_exception *exception)
1116{
1117 if (err != -EINVAL)
1118 return false;
1119 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1120 return false;
1121 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1122 exception->retry = 1;
1123 return true;
1124}
1125
Trond Myklebust6ae37332015-01-30 14:21:14 -05001126static u32
1127nfs4_map_atomic_open_share(struct nfs_server *server,
1128 fmode_t fmode, int openflags)
1129{
1130 u32 res = 0;
1131
1132 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1133 case FMODE_READ:
1134 res = NFS4_SHARE_ACCESS_READ;
1135 break;
1136 case FMODE_WRITE:
1137 res = NFS4_SHARE_ACCESS_WRITE;
1138 break;
1139 case FMODE_READ|FMODE_WRITE:
1140 res = NFS4_SHARE_ACCESS_BOTH;
1141 }
1142 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1143 goto out;
1144 /* Want no delegation if we're using O_DIRECT */
1145 if (openflags & O_DIRECT)
1146 res |= NFS4_SHARE_WANT_NO_DELEG;
1147out:
1148 return res;
1149}
1150
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001151static enum open_claim_type4
1152nfs4_map_atomic_open_claim(struct nfs_server *server,
1153 enum open_claim_type4 claim)
1154{
1155 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1156 return claim;
1157 switch (claim) {
1158 default:
1159 return claim;
1160 case NFS4_OPEN_CLAIM_FH:
1161 return NFS4_OPEN_CLAIM_NULL;
1162 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1163 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1164 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1165 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1166 }
1167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170{
1171 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001172 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173 p->o_res.seqid = p->o_arg.seqid;
1174 p->c_res.seqid = p->c_arg.seqid;
1175 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001176 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001177 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001178 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001179}
1180
Al Viro82a2c1b2011-06-22 18:30:55 -04001181static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001182 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001183 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001184 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001185 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001186 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187{
Al Viro82a2c1b2011-06-22 18:30:55 -04001188 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001189 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001190 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001191 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 struct nfs4_opendata *p;
1193
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001194 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 if (p == NULL)
1196 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001197
1198 p->f_label = nfs4_label_alloc(server, gfp_mask);
1199 if (IS_ERR(p->f_label))
1200 goto err_free_p;
1201
Kinglong Meea49c2692015-07-27 15:31:38 +08001202 p->a_label = nfs4_label_alloc(server, gfp_mask);
1203 if (IS_ERR(p->a_label))
1204 goto err_free_f;
1205
Trond Myklebust63f5f792015-01-23 19:19:25 -05001206 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1207 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001208 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001209 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001210 nfs_sb_active(dentry->d_sb);
1211 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001212 p->dir = parent;
1213 p->owner = sp;
1214 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 p->o_arg.open_flags = flags;
1216 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001217 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1218 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001219 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1220 * will return permission denied for all bits until close */
1221 if (!(flags & O_EXCL)) {
1222 /* ask server to check for all possible rights as results
1223 * are cached */
1224 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1225 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001226 }
David Howells7539bba2006-08-22 20:06:09 -04001227 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001228 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1229 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001230 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001231 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001232 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001233 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001234 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001235 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1236 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001237 case NFS4_OPEN_CLAIM_NULL:
1238 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1239 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1240 p->o_arg.fh = NFS_FH(dir);
1241 break;
1242 case NFS4_OPEN_CLAIM_PREVIOUS:
1243 case NFS4_OPEN_CLAIM_FH:
1244 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1245 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001246 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001247 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001248 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001249 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001250
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001251 p->o_arg.u.attrs = &p->attrs;
1252 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001253
1254 verf[0] = jiffies;
1255 verf[1] = current->pid;
1256 memcpy(p->o_arg.u.verifier.data, verf,
1257 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001258 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001259 p->c_arg.fh = &p->o_res.fh;
1260 p->c_arg.stateid = &p->o_res.stateid;
1261 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001262 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001264 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001265
1266err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001267 nfs4_label_free(p->a_label);
1268err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001269 nfs4_label_free(p->f_label);
1270err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 kfree(p);
1272err:
1273 dput(parent);
1274 return NULL;
1275}
1276
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001279 struct nfs4_opendata *p = container_of(kref,
1280 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001281 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001282
1283 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001284 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001285 if (p->state != NULL)
1286 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001287 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001288
Kinglong Meea49c2692015-07-27 15:31:38 +08001289 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001290 nfs4_label_free(p->f_label);
1291
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001293 dput(p->dentry);
1294 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001295 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001296 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001297 kfree(p);
1298}
1299
1300static void nfs4_opendata_put(struct nfs4_opendata *p)
1301{
1302 if (p != NULL)
1303 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304}
1305
Trond Myklebust06f814a2006-01-03 09:55:07 +01001306static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1307{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001308 int ret;
1309
Trond Myklebust06f814a2006-01-03 09:55:07 +01001310 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001311 return ret;
1312}
1313
Trond Myklebust24311f82015-09-20 10:50:17 -04001314static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1315 fmode_t fmode)
1316{
1317 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1318 case FMODE_READ|FMODE_WRITE:
1319 return state->n_rdwr != 0;
1320 case FMODE_WRITE:
1321 return state->n_wronly != 0;
1322 case FMODE_READ:
1323 return state->n_rdonly != 0;
1324 }
1325 WARN_ON_ONCE(1);
1326 return false;
1327}
1328
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001329static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001330{
1331 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001332
Trond Myklebust536e43d2012-01-17 22:04:26 -05001333 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334 goto out;
1335 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001336 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001337 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1338 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001339 break;
1340 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001341 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1342 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001343 break;
1344 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001345 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1346 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001347 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001348out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001349 return ret;
1350}
1351
Trond Myklebust2a606182015-08-19 22:30:00 -05001352static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1353 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001354{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001355 if (delegation == NULL)
1356 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001358 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001359 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1360 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001361 switch (claim) {
1362 case NFS4_OPEN_CLAIM_NULL:
1363 case NFS4_OPEN_CLAIM_FH:
1364 break;
1365 case NFS4_OPEN_CLAIM_PREVIOUS:
1366 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1367 break;
1368 default:
1369 return 0;
1370 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001371 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001372 return 1;
1373}
1374
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001375static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001376{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001377 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001378 case FMODE_WRITE:
1379 state->n_wronly++;
1380 break;
1381 case FMODE_READ:
1382 state->n_rdonly++;
1383 break;
1384 case FMODE_READ|FMODE_WRITE:
1385 state->n_rdwr++;
1386 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001388}
1389
Trond Myklebust4f14c192014-02-12 19:15:06 -05001390static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001391{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001392 struct nfs_client *clp = state->owner->so_server->nfs_client;
1393 bool need_recover = false;
1394
1395 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1396 need_recover = true;
1397 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1398 need_recover = true;
1399 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1400 need_recover = true;
1401 if (need_recover)
1402 nfs4_state_mark_reclaim_nograce(clp, state);
1403}
1404
Trond Myklebuste999e802014-02-10 18:20:47 -05001405static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001406 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001407{
1408 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1409 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001410 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001411 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001412 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001413 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001414 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001415 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1416 return true;
1417 return false;
1418}
1419
Trond Myklebustf95549c2015-01-23 18:06:09 -05001420static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1421{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001422 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1423 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001424 if (state->n_wronly)
1425 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1426 if (state->n_rdonly)
1427 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1428 if (state->n_rdwr)
1429 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001430 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001431}
1432
Trond Myklebust226056c2014-02-11 10:41:07 -05001433static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001434 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001435 nfs4_stateid *stateid, fmode_t fmode)
1436{
1437 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1438 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1439 case FMODE_WRITE:
1440 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1441 break;
1442 case FMODE_READ:
1443 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1444 break;
1445 case 0:
1446 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1447 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1448 clear_bit(NFS_OPEN_STATE, &state->flags);
1449 }
1450 if (stateid == NULL)
1451 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001452 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001453 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1454 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1455 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001456 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001457 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001458 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001459 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001460 nfs4_stateid_copy(&state->stateid, stateid);
1461 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001462}
1463
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001464static void nfs_clear_open_stateid(struct nfs4_state *state,
1465 nfs4_stateid *arg_stateid,
1466 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001467{
1468 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001469 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001470 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001471 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1472 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001473}
1474
Trond Myklebust1393d962016-09-22 13:39:13 -04001475static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1476 const nfs4_stateid *stateid, fmode_t fmode,
1477 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001478{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001479 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001480 case FMODE_READ:
1481 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1482 break;
1483 case FMODE_WRITE:
1484 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1485 break;
1486 case FMODE_READ|FMODE_WRITE:
1487 set_bit(NFS_O_RDWR_STATE, &state->flags);
1488 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001489 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001490 return;
1491 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1492 nfs4_stateid_copy(&state->stateid, stateid);
1493 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001494}
1495
Trond Myklebust1393d962016-09-22 13:39:13 -04001496static void __update_open_stateid(struct nfs4_state *state,
1497 const nfs4_stateid *open_stateid,
1498 const nfs4_stateid *deleg_stateid,
1499 fmode_t fmode,
1500 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001502 /*
1503 * Protect the call to nfs4_state_set_mode_locked and
1504 * serialise the stateid update
1505 */
Andrew Elble361cad32015-12-02 09:20:57 -05001506 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001507 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001508 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001509 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001510 set_bit(NFS_DELEGATED_STATE, &state->flags);
1511 }
1512 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001513 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001514 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001515 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001516 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Trond Myklebust1393d962016-09-22 13:39:13 -04001519static int update_open_stateid(struct nfs4_state *state,
1520 const nfs4_stateid *open_stateid,
1521 const nfs4_stateid *delegation,
1522 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001523{
Trond Myklebust1393d962016-09-22 13:39:13 -04001524 struct nfs_server *server = NFS_SERVER(state->inode);
1525 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001526 struct nfs_inode *nfsi = NFS_I(state->inode);
1527 struct nfs_delegation *deleg_cur;
Trond Myklebust1393d962016-09-22 13:39:13 -04001528 nfs4_stateid freeme = {0};
Trond Myklebust34310432008-12-23 15:21:38 -05001529 int ret = 0;
1530
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001531 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001532
1533 rcu_read_lock();
1534 deleg_cur = rcu_dereference(nfsi->delegation);
1535 if (deleg_cur == NULL)
1536 goto no_delegation;
1537
1538 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001539 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001540 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001542 goto no_delegation_unlock;
1543
1544 if (delegation == NULL)
1545 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001546 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001547 goto no_delegation_unlock;
1548
Trond Myklebustb7391f42008-12-23 15:21:52 -05001549 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001550 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1551 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001552 ret = 1;
1553no_delegation_unlock:
1554 spin_unlock(&deleg_cur->lock);
1555no_delegation:
1556 rcu_read_unlock();
1557
1558 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001559 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001560 ret = 1;
1561 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001562 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001563 nfs4_schedule_state_manager(clp);
1564 if (freeme.type != 0)
1565 nfs4_test_and_free_stateid(server, &freeme,
1566 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001567
1568 return ret;
1569}
1570
Trond Myklebust39071e62015-01-24 15:07:56 -05001571static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1572 const nfs4_stateid *stateid)
1573{
1574 struct nfs4_state *state = lsp->ls_state;
1575 bool ret = false;
1576
1577 spin_lock(&state->state_lock);
1578 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1579 goto out_noupdate;
1580 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1581 goto out_noupdate;
1582 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1583 ret = true;
1584out_noupdate:
1585 spin_unlock(&state->state_lock);
1586 return ret;
1587}
Trond Myklebust34310432008-12-23 15:21:38 -05001588
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001589static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590{
1591 struct nfs_delegation *delegation;
1592
1593 rcu_read_lock();
1594 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001595 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001596 rcu_read_unlock();
1597 return;
1598 }
1599 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001600 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001601}
1602
Trond Myklebust6ee41262007-07-08 14:11:36 -04001603static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001604{
1605 struct nfs4_state *state = opendata->state;
1606 struct nfs_inode *nfsi = NFS_I(state->inode);
1607 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001608 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001609 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001610 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001611 nfs4_stateid stateid;
1612 int ret = -EAGAIN;
1613
Trond Myklebustaac00a82007-07-05 19:02:21 -04001614 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001615 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001616 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001617 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001618 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001619 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001620 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001621 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001622 rcu_read_lock();
1623 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001624 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001625 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001626 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001627 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001628 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001629 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001630 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001631 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001632 if (!opendata->is_recover) {
1633 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1634 if (ret != 0)
1635 goto out;
1636 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001637 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001638
1639 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001640 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001641 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001642 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001643out:
1644 return ERR_PTR(ret);
1645out_return_state:
1646 atomic_inc(&state->count);
1647 return state;
1648}
1649
Andy Adamsone23008e2012-10-02 21:07:32 -04001650static void
1651nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1652{
1653 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1654 struct nfs_delegation *delegation;
1655 int delegation_flags = 0;
1656
1657 rcu_read_lock();
1658 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1659 if (delegation)
1660 delegation_flags = delegation->flags;
1661 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001662 switch (data->o_arg.claim) {
1663 default:
1664 break;
1665 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1666 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001667 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1668 "returning a delegation for "
1669 "OPEN(CLAIM_DELEGATE_CUR)\n",
1670 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001671 return;
1672 }
1673 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001674 nfs_inode_set_delegation(state->inode,
1675 data->owner->so_cred,
1676 &data->o_res);
1677 else
1678 nfs_inode_reclaim_delegation(state->inode,
1679 data->owner->so_cred,
1680 &data->o_res);
1681}
1682
1683/*
1684 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1685 * and update the nfs4_state.
1686 */
1687static struct nfs4_state *
1688_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1689{
1690 struct inode *inode = data->state->inode;
1691 struct nfs4_state *state = data->state;
1692 int ret;
1693
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001694 if (!data->rpc_done) {
1695 if (data->rpc_status) {
1696 ret = data->rpc_status;
1697 goto err;
1698 }
1699 /* cached opens have already been processed */
1700 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001701 }
1702
Andy Adamsone23008e2012-10-02 21:07:32 -04001703 ret = nfs_refresh_inode(inode, &data->f_attr);
1704 if (ret)
1705 goto err;
1706
1707 if (data->o_res.delegation_type != 0)
1708 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001709update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001710 update_open_stateid(state, &data->o_res.stateid, NULL,
1711 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001712 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001713
1714 return state;
1715err:
1716 return ERR_PTR(ret);
1717
1718}
1719
1720static struct nfs4_state *
1721_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001722{
1723 struct inode *inode;
1724 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001725 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001726
Trond Myklebustaac00a82007-07-05 19:02:21 -04001727 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001728 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001729 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001730 goto out;
1731 }
1732
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001733 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001734 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001735 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001736 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001737 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001738 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001739 goto err;
1740 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001741 state = nfs4_get_open_state(inode, data->owner);
1742 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001743 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001744 if (data->o_res.delegation_type != 0)
1745 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001746 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001747 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001748 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001749out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001750 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001751 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001752err_put_inode:
1753 iput(inode);
1754err:
1755 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001756}
1757
Andy Adamsone23008e2012-10-02 21:07:32 -04001758static struct nfs4_state *
1759nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1760{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001761 struct nfs4_state *ret;
1762
Andy Adamsone23008e2012-10-02 21:07:32 -04001763 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001764 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1765 else
1766 ret = _nfs4_opendata_to_nfs4_state(data);
1767 nfs4_sequence_free_slot(&data->o_res.seq_res);
1768 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001769}
1770
Trond Myklebust864472e2006-01-03 09:55:15 +01001771static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1772{
1773 struct nfs_inode *nfsi = NFS_I(state->inode);
1774 struct nfs_open_context *ctx;
1775
1776 spin_lock(&state->inode->i_lock);
1777 list_for_each_entry(ctx, &nfsi->open_files, list) {
1778 if (ctx->state != state)
1779 continue;
1780 get_nfs_open_context(ctx);
1781 spin_unlock(&state->inode->i_lock);
1782 return ctx;
1783 }
1784 spin_unlock(&state->inode->i_lock);
1785 return ERR_PTR(-ENOENT);
1786}
1787
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001788static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1789 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001790{
1791 struct nfs4_opendata *opendata;
1792
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001793 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001794 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001795 if (opendata == NULL)
1796 return ERR_PTR(-ENOMEM);
1797 opendata->state = state;
1798 atomic_inc(&state->count);
1799 return opendata;
1800}
1801
Trond Myklebust24311f82015-09-20 10:50:17 -04001802static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1803 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001804{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001805 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001806 int ret;
1807
Trond Myklebust24311f82015-09-20 10:50:17 -04001808 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001809 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001810 opendata->o_arg.open_flags = 0;
1811 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001812 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1813 NFS_SB(opendata->dentry->d_sb),
1814 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001815 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1816 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1817 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001818 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001819 if (ret != 0)
1820 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001821 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001822 if (IS_ERR(newstate))
1823 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001824 if (newstate != opendata->state)
1825 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001826 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001827 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001828}
1829
1830static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1831{
Trond Myklebust864472e2006-01-03 09:55:15 +01001832 int ret;
1833
Trond Myklebust4f14c192014-02-12 19:15:06 -05001834 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1835 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1836 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1837 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001838 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001839 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001840 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001841 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001842 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1843 if (ret != 0)
1844 return ret;
1845 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1846 if (ret != 0)
1847 return ret;
1848 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1849 if (ret != 0)
1850 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001851 /*
1852 * We may have performed cached opens for all three recoveries.
1853 * Check if we need to update the current stateid.
1854 */
1855 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001856 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001857 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001858 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001859 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001860 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001861 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001862 return 0;
1863}
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865/*
1866 * OPEN_RECLAIM:
1867 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001869static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001871 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001872 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001873 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 int status;
1875
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001876 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1877 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001878 if (IS_ERR(opendata))
1879 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001880 rcu_read_lock();
1881 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001882 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001883 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001884 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001885 opendata->o_arg.u.delegation_type = delegation_type;
1886 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001887 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return status;
1889}
1890
Trond Myklebust539cd032007-06-05 11:46:42 -04001891static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 struct nfs_server *server = NFS_SERVER(state->inode);
1894 struct nfs4_exception exception = { };
1895 int err;
1896 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001897 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001898 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001899 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1900 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001901 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001902 break;
1903 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 } while (exception.retry);
1905 return err;
1906}
1907
Trond Myklebust864472e2006-01-03 09:55:15 +01001908static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1909{
1910 struct nfs_open_context *ctx;
1911 int ret;
1912
1913 ctx = nfs4_state_find_open_context(state);
1914 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001915 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001916 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001917 put_nfs_open_context(ctx);
1918 return ret;
1919}
1920
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001921static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001923 switch (err) {
1924 default:
1925 printk(KERN_ERR "NFS: %s: unhandled error "
1926 "%d.\n", __func__, err);
1927 case 0:
1928 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001929 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001930 case -ESTALE:
1931 break;
1932 case -NFS4ERR_BADSESSION:
1933 case -NFS4ERR_BADSLOT:
1934 case -NFS4ERR_BAD_HIGH_SLOT:
1935 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1936 case -NFS4ERR_DEADSESSION:
1937 set_bit(NFS_DELEGATED_STATE, &state->flags);
1938 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1939 return -EAGAIN;
1940 case -NFS4ERR_STALE_CLIENTID:
1941 case -NFS4ERR_STALE_STATEID:
1942 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001943 /* Don't recall a delegation if it was lost */
1944 nfs4_schedule_lease_recovery(server->nfs_client);
1945 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001946 case -NFS4ERR_MOVED:
1947 nfs4_schedule_migration_recovery(server);
1948 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001949 case -NFS4ERR_LEASE_MOVED:
1950 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1951 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001952 case -NFS4ERR_DELEG_REVOKED:
1953 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001954 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001955 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001956 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001957 nfs_inode_find_state_and_recover(state->inode,
1958 stateid);
1959 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001960 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001961 case -NFS4ERR_DELAY:
1962 case -NFS4ERR_GRACE:
1963 set_bit(NFS_DELEGATED_STATE, &state->flags);
1964 ssleep(1);
1965 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001966 case -ENOMEM:
1967 case -NFS4ERR_DENIED:
1968 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1969 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return err;
1972}
1973
Trond Myklebust24311f82015-09-20 10:50:17 -04001974int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1975 struct nfs4_state *state, const nfs4_stateid *stateid,
1976 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001977{
1978 struct nfs_server *server = NFS_SERVER(state->inode);
1979 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001980 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001981
1982 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1983 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1984 if (IS_ERR(opendata))
1985 return PTR_ERR(opendata);
1986 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001987 write_seqlock(&state->seqlock);
1988 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1989 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001990 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1991 switch (type & (FMODE_READ|FMODE_WRITE)) {
1992 case FMODE_READ|FMODE_WRITE:
1993 case FMODE_WRITE:
1994 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1995 if (err)
1996 break;
1997 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1998 if (err)
1999 break;
2000 case FMODE_READ:
2001 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2002 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002003 nfs4_opendata_put(opendata);
2004 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2005}
2006
Chuck Leverbe05c862013-08-09 12:49:47 -04002007static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2008{
2009 struct nfs4_opendata *data = calldata;
2010
Peng Taocb04ad22014-06-11 05:24:15 +08002011 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2012 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002013}
2014
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002015static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2016{
2017 struct nfs4_opendata *data = calldata;
2018
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002019 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002020
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002021 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002022 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002023 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002024 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002025 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002026 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002027 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002028}
2029
2030static void nfs4_open_confirm_release(void *calldata)
2031{
2032 struct nfs4_opendata *data = calldata;
2033 struct nfs4_state *state = NULL;
2034
2035 /* If this request hasn't been cancelled, do nothing */
2036 if (data->cancelled == 0)
2037 goto out_free;
2038 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002039 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002040 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002041 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002042 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002043 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002044out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002045 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002046}
2047
2048static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002049 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002050 .rpc_call_done = nfs4_open_confirm_done,
2051 .rpc_release = nfs4_open_confirm_release,
2052};
2053
2054/*
2055 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2056 */
2057static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2058{
David Howells2b0143b2015-03-17 22:25:59 +00002059 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002060 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002061 struct rpc_message msg = {
2062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2063 .rpc_argp = &data->c_arg,
2064 .rpc_resp = &data->c_res,
2065 .rpc_cred = data->owner->so_cred,
2066 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002067 struct rpc_task_setup task_setup_data = {
2068 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002069 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002070 .callback_ops = &nfs4_open_confirm_ops,
2071 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002072 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002073 .flags = RPC_TASK_ASYNC,
2074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 int status;
2076
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002077 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002078 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002079 data->rpc_done = 0;
2080 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002081 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002082 if (data->is_recover)
2083 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002084 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002085 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002086 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002087 status = nfs4_wait_for_completion_rpc_task(task);
2088 if (status != 0) {
2089 data->cancelled = 1;
2090 smp_wmb();
2091 } else
2092 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002093 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return status;
2095}
2096
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002097static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002099 struct nfs4_opendata *data = calldata;
2100 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002101 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002102 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002103
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002104 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002105 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002106 /*
2107 * Check if we still need to send an OPEN call, or if we can use
2108 * a delegation instead.
2109 */
2110 if (data->state != NULL) {
2111 struct nfs_delegation *delegation;
2112
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002113 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002114 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002115 rcu_read_lock();
2116 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002117 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002118 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002119 rcu_read_unlock();
2120 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002121 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002122 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002123 switch (claim) {
2124 default:
2125 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002126 case NFS4_OPEN_CLAIM_PREVIOUS:
2127 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2128 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002129 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002130 case NFS4_OPEN_CLAIM_FH:
2131 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002132 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2133 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002134 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002135 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002136 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002137 &data->o_res.seq_res,
2138 task) != 0)
2139 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002140
2141 /* Set the create mode (note dependency on the session type) */
2142 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2143 if (data->o_arg.open_flags & O_EXCL) {
2144 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2145 if (nfs4_has_persistent_session(clp))
2146 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2147 else if (clp->cl_mvops->minor_version > 0)
2148 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2149 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002150 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002151unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002152 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002153 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002154out_no_action:
2155 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002156out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002157 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002158}
2159
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002160static void nfs4_open_done(struct rpc_task *task, void *calldata)
2161{
2162 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002164 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002165
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002166 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002167 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002168
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002169 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002170 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2171 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002172 case S_IFREG:
2173 break;
2174 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002175 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002176 break;
2177 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002178 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002179 break;
2180 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002181 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002182 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002183 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002184 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002185 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2186 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002187 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002188 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002189}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002190
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191static void nfs4_open_release(void *calldata)
2192{
2193 struct nfs4_opendata *data = calldata;
2194 struct nfs4_state *state = NULL;
2195
2196 /* If this request hasn't been cancelled, do nothing */
2197 if (data->cancelled == 0)
2198 goto out_free;
2199 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002200 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002201 goto out_free;
2202 /* In case we need an open_confirm, no cleanup! */
2203 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2204 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002206 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002207 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002208out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002209 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002210}
2211
2212static const struct rpc_call_ops nfs4_open_ops = {
2213 .rpc_call_prepare = nfs4_open_prepare,
2214 .rpc_call_done = nfs4_open_done,
2215 .rpc_release = nfs4_open_release,
2216};
2217
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002218static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002219{
David Howells2b0143b2015-03-17 22:25:59 +00002220 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002221 struct nfs_server *server = NFS_SERVER(dir);
2222 struct nfs_openargs *o_arg = &data->o_arg;
2223 struct nfs_openres *o_res = &data->o_res;
2224 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002225 struct rpc_message msg = {
2226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2227 .rpc_argp = o_arg,
2228 .rpc_resp = o_res,
2229 .rpc_cred = data->owner->so_cred,
2230 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002231 struct rpc_task_setup task_setup_data = {
2232 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002233 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002234 .callback_ops = &nfs4_open_ops,
2235 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002236 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002237 .flags = RPC_TASK_ASYNC,
2238 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002239 int status;
2240
Chuck Levera9c92d62013-08-09 12:48:18 -04002241 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002242 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002243 data->rpc_done = 0;
2244 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002245 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002246 data->is_recover = 0;
2247 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002248 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002249 data->is_recover = 1;
2250 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002251 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002252 if (IS_ERR(task))
2253 return PTR_ERR(task);
2254 status = nfs4_wait_for_completion_rpc_task(task);
2255 if (status != 0) {
2256 data->cancelled = 1;
2257 smp_wmb();
2258 } else
2259 status = data->rpc_status;
2260 rpc_put_task(task);
2261
2262 return status;
2263}
2264
2265static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2266{
David Howells2b0143b2015-03-17 22:25:59 +00002267 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002268 struct nfs_openres *o_res = &data->o_res;
2269 int status;
2270
2271 status = nfs4_run_open_task(data, 1);
2272 if (status != 0 || !data->rpc_done)
2273 return status;
2274
Trond Myklebust6926afd2012-01-07 13:22:46 -05002275 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2276
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002277 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2278 status = _nfs4_proc_open_confirm(data);
2279 if (status != 0)
2280 return status;
2281 }
2282
2283 return status;
2284}
2285
Trond Myklebustf3792d62014-07-10 08:54:32 -04002286/*
2287 * Additional permission checks in order to distinguish between an
2288 * open for read, and an open for execute. This works around the
2289 * fact that NFSv4 OPEN treats read and execute permissions as being
2290 * the same.
2291 * Note that in the non-execute case, we want to turn off permission
2292 * checking if we just created a new file (POSIX open() semantics).
2293 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002294static int nfs4_opendata_access(struct rpc_cred *cred,
2295 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002296 struct nfs4_state *state, fmode_t fmode,
2297 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002298{
2299 struct nfs_access_entry cache;
2300 u32 mask;
2301
2302 /* access call failed or for some reason the server doesn't
2303 * support any access modes -- defer access call until later */
2304 if (opendata->o_res.access_supported == 0)
2305 return 0;
2306
2307 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002308 /*
2309 * Use openflags to check for exec, because fmode won't
2310 * always have FMODE_EXEC set when file open for exec.
2311 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002312 if (openflags & __FMODE_EXEC) {
2313 /* ONLY check for exec rights */
2314 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002315 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002316 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002317
2318 cache.cred = cred;
2319 cache.jiffies = jiffies;
2320 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2321 nfs_access_add_cache(state->inode, &cache);
2322
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002323 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002324 return 0;
2325
2326 /* even though OPEN succeeded, access is denied. Close the file */
2327 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002328 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002329}
2330
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002331/*
2332 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2333 */
2334static int _nfs4_proc_open(struct nfs4_opendata *data)
2335{
David Howells2b0143b2015-03-17 22:25:59 +00002336 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002337 struct nfs_server *server = NFS_SERVER(dir);
2338 struct nfs_openargs *o_arg = &data->o_arg;
2339 struct nfs_openres *o_res = &data->o_res;
2340 int status;
2341
2342 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002343 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002344 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002345 if (status != 0) {
2346 if (status == -NFS4ERR_BADNAME &&
2347 !(o_arg->open_flags & O_CREAT))
2348 return -ENOENT;
2349 return status;
2350 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002351
Trond Myklebust6926afd2012-01-07 13:22:46 -05002352 nfs_fattr_map_and_free_names(server, &data->f_attr);
2353
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002354 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002355 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002356 if (o_arg->open_flags & O_EXCL)
2357 data->file_created = 1;
2358 else if (o_res->cinfo.before != o_res->cinfo.after)
2359 data->file_created = 1;
2360 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002361 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2362 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002364 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002366 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002369 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
Andy Adamsond83217c2011-03-01 01:34:17 +00002373static int nfs4_recover_expired_lease(struct nfs_server *server)
2374{
2375 return nfs4_client_recover_expired_lease(server->nfs_client);
2376}
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378/*
2379 * OPEN_EXPIRED:
2380 * reclaim state on the server after a network partition.
2381 * Assumes caller holds the appropriate lock
2382 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002383static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002385 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002386 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002388 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002389 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002390 if (IS_ERR(opendata))
2391 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002392 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002393 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002394 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002395 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002396 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002399static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002400{
Trond Myklebust539cd032007-06-05 11:46:42 -04002401 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002402 struct nfs4_exception exception = { };
2403 int err;
2404
2405 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002406 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002407 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002408 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2409 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002410 switch (err) {
2411 default:
2412 goto out;
2413 case -NFS4ERR_GRACE:
2414 case -NFS4ERR_DELAY:
2415 nfs4_handle_exception(server, err, &exception);
2416 err = 0;
2417 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002418 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002419out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002420 return err;
2421}
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2424{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002426 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Trond Myklebust864472e2006-01-03 09:55:15 +01002428 ctx = nfs4_state_find_open_context(state);
2429 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002430 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002431 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002432 put_nfs_open_context(ctx);
2433 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Trond Myklebust41020b62016-09-22 13:38:58 -04002436static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2437 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002438{
Trond Myklebust41020b62016-09-22 13:38:58 -04002439 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002440 write_seqlock(&state->seqlock);
2441 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2442 write_sequnlock(&state->seqlock);
2443 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2444}
2445
2446static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2447{
2448 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002449 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002450}
2451
2452static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2453{
2454 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2455 nfs40_clear_delegation_stateid(state);
2456 return nfs4_open_expired(sp, state);
2457}
2458
Trond Myklebust45870d62016-09-22 13:38:59 -04002459static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2460 nfs4_stateid *stateid,
2461 struct rpc_cred *cred)
2462{
2463 return -NFS4ERR_BAD_STATEID;
2464}
2465
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002466#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002467static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2468 nfs4_stateid *stateid,
2469 struct rpc_cred *cred)
2470{
2471 int status;
2472
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002473 switch (stateid->type) {
2474 default:
2475 break;
2476 case NFS4_INVALID_STATEID_TYPE:
2477 case NFS4_SPECIAL_STATEID_TYPE:
2478 return -NFS4ERR_BAD_STATEID;
2479 case NFS4_REVOKED_STATEID_TYPE:
2480 goto out_free;
2481 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002482
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002483 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002484 switch (status) {
2485 case -NFS4ERR_EXPIRED:
2486 case -NFS4ERR_ADMIN_REVOKED:
2487 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002488 break;
2489 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002490 return status;
2491 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002492out_free:
2493 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002494 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002495 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002496}
2497
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002498static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002499{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002500 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002501 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002502 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002503 struct rpc_cred *cred;
2504 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002505
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002506 /* Get the delegation credential for use by test/free_stateid */
2507 rcu_read_lock();
2508 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002509 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002510 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002511 return;
2512 }
Trond Myklebust41020b62016-09-22 13:38:58 -04002513
2514 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002515 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2516 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002517 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002518 return;
2519 }
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002520
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002521 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2522 rcu_read_unlock();
2523 return;
2524 }
2525
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002526 cred = get_rpccred(delegation->cred);
2527 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002528 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002529 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002530 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002531 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002532
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002533 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002534}
2535
2536/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002537 * nfs41_check_expired_locks - possibly free a lock stateid
2538 *
2539 * @state: NFSv4 state for an inode
2540 *
2541 * Returns NFS_OK if recovery for this stateid is now finished.
2542 * Otherwise a negative NFS4ERR value is returned.
2543 */
2544static int nfs41_check_expired_locks(struct nfs4_state *state)
2545{
2546 int status, ret = NFS_OK;
2547 struct nfs4_lock_state *lsp;
2548 struct nfs_server *server = NFS_SERVER(state->inode);
2549
2550 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2551 goto out;
2552 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2553 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2554 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2555
2556 status = nfs41_test_and_free_expired_stateid(server,
2557 &lsp->ls_stateid,
2558 cred);
2559 trace_nfs4_test_lock_stateid(state, lsp, status);
2560 if (status == -NFS4ERR_EXPIRED ||
2561 status == -NFS4ERR_BAD_STATEID) {
2562 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002563 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002564 if (!recover_lost_locks)
2565 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2566 } else if (status != NFS_OK) {
2567 ret = status;
2568 break;
2569 }
2570 }
2571 };
2572out:
2573 return ret;
2574}
2575
2576/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002577 * nfs41_check_open_stateid - possibly free an open stateid
2578 *
2579 * @state: NFSv4 state for an inode
2580 *
2581 * Returns NFS_OK if recovery for this stateid is now finished.
2582 * Otherwise a negative NFS4ERR value is returned.
2583 */
2584static int nfs41_check_open_stateid(struct nfs4_state *state)
2585{
2586 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002587 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002588 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002589 int status;
2590
Trond Myklebustb134fc42016-09-22 13:39:16 -04002591 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2592 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2593 return NFS_OK;
2594 return -NFS4ERR_BAD_STATEID;
2595 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002596 /* If a state reset has been done, test_stateid is unneeded */
2597 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2598 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2599 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2600 return -NFS4ERR_BAD_STATEID;
2601
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002602 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002603 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002604 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002605 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2606 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2607 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002608 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002609 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002610 }
2611 return status;
2612}
2613
2614static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2615{
Chuck Levereb64cf92012-07-11 16:30:05 -04002616 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002617
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002618 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002619 status = nfs41_check_expired_locks(state);
2620 if (status != NFS_OK)
2621 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002622 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002623 if (status != NFS_OK)
2624 status = nfs4_open_expired(sp, state);
2625 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002626}
2627#endif
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002630 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2631 * fields corresponding to attributes that were used to store the verifier.
2632 * Make sure we clobber those fields in the later setattr call
2633 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002634static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2635 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002636{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002637 const u32 *attrset = opendata->o_res.attrset;
2638
2639 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002640 !(sattr->ia_valid & ATTR_ATIME_SET))
2641 sattr->ia_valid |= ATTR_ATIME;
2642
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002643 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002644 !(sattr->ia_valid & ATTR_MTIME_SET))
2645 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002646
2647 /* Except MODE, it seems harmless of setting twice. */
2648 if ((attrset[1] & FATTR4_WORD1_MODE))
2649 sattr->ia_valid &= ~ATTR_MODE;
2650
2651 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2652 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002653}
2654
Trond Myklebustc21443c2013-02-07 14:26:21 -05002655static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2656 fmode_t fmode,
2657 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002658 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002659{
2660 struct nfs4_state_owner *sp = opendata->owner;
2661 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002662 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002663 struct nfs4_state *state;
2664 unsigned int seq;
2665 int ret;
2666
2667 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2668
2669 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002670 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002671 goto out;
2672
2673 state = nfs4_opendata_to_nfs4_state(opendata);
2674 ret = PTR_ERR(state);
2675 if (IS_ERR(state))
2676 goto out;
2677 if (server->caps & NFS_CAP_POSIX_LOCK)
2678 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002679 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2680 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002681
Trond Myklebust275bb302013-05-29 13:11:28 -04002682 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002683 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002684 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002685 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002686 alias = d_exact_alias(dentry, state->inode);
2687 if (!alias)
2688 alias = d_splice_alias(igrab(state->inode), dentry);
2689 /* d_splice_alias() can't fail here - it's a non-directory */
2690 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002691 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002692 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002693 }
2694 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002695 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002696 }
2697
Trond Myklebustc21443c2013-02-07 14:26:21 -05002698 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2699 if (ret != 0)
2700 goto out;
2701
Trond Myklebust3efb9722013-05-29 13:17:04 -04002702 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002703 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002704 nfs_inode_attach_open_context(ctx);
2705 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2706 nfs4_schedule_stateid_recovery(server, state);
2707 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002708out:
2709 return ret;
2710}
2711
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002712/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002713 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 */
Andy Adamson82be4172012-05-23 05:02:35 -04002715static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002716 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002717 int flags,
2718 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002719 struct nfs4_label *label,
2720 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
2722 struct nfs4_state_owner *sp;
2723 struct nfs4_state *state = NULL;
2724 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002725 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002726 struct dentry *dentry = ctx->dentry;
2727 struct rpc_cred *cred = ctx->cred;
2728 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2729 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002730 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002731 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002732 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002736 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2737 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2739 goto out_err;
2740 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002741 status = nfs4_recover_expired_lease(server);
2742 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002743 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002744 if (d_really_is_positive(dentry))
2745 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002746 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002747 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002748 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002749 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002750 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002751 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002752 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
David Quigley14c43f72013-05-22 12:50:43 -04002754 if (label) {
2755 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2756 if (IS_ERR(olabel)) {
2757 status = PTR_ERR(olabel);
2758 goto err_opendata_put;
2759 }
2760 }
2761
Trond Myklebuste911b812014-03-26 13:24:37 -07002762 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2763 if (!opendata->f_attr.mdsthreshold) {
2764 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2765 if (!opendata->f_attr.mdsthreshold)
2766 goto err_free_label;
2767 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002768 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002769 }
David Howells2b0143b2015-03-17 22:25:59 +00002770 if (d_really_is_positive(dentry))
2771 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002772
Trond Myklebust3efb9722013-05-29 13:17:04 -04002773 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002774 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002775 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002776 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002777
NeilBrownefcbc042015-07-30 13:00:56 +10002778 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002779 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002780 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002781 /*
2782 * send create attributes which was not set by open
2783 * with an extra setattr.
2784 */
2785 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2786 nfs_fattr_init(opendata->o_res.f_attr);
2787 status = nfs4_do_setattr(state->inode, cred,
2788 opendata->o_res.f_attr, sattr,
2789 state, label, olabel);
2790 if (status == 0) {
2791 nfs_setattr_update_inode(state->inode, sattr,
2792 opendata->o_res.f_attr);
2793 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2794 }
David Quigley1775fd32013-05-22 12:50:42 -04002795 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002796 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002797 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002798 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002799
Trond Myklebuste911b812014-03-26 13:24:37 -07002800 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002801 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002802 opendata->f_attr.mdsthreshold = NULL;
2803 }
Andy Adamson82be4172012-05-23 05:02:35 -04002804
David Quigley14c43f72013-05-22 12:50:43 -04002805 nfs4_label_free(olabel);
2806
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002807 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002810err_free_label:
2811 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002812err_opendata_put:
2813 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002814err_put_state_owner:
2815 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return status;
2818}
2819
2820
Andy Adamson82be4172012-05-23 05:02:35 -04002821static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002822 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002823 int flags,
2824 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002825 struct nfs4_label *label,
2826 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002828 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 struct nfs4_exception exception = { };
2830 struct nfs4_state *res;
2831 int status;
2832
2833 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002834 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002835 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002836 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (status == 0)
2838 break;
2839 /* NOTE: BAD_SEQID means the server and client disagree about the
2840 * book-keeping w.r.t. state-changing operations
2841 * (OPEN/CLOSE/LOCK/LOCKU...)
2842 * It is actually a sign of a bug on the client or on the server.
2843 *
2844 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002845 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 * have unhashed the old state_owner for us, and that we can
2847 * therefore safely retry using a new one. We should still warn
2848 * the user though...
2849 */
2850 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002851 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002852 " returned a bad sequence-id error!\n",
2853 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 exception.retry = 1;
2855 continue;
2856 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002857 /*
2858 * BAD_STATEID on OPEN means that the server cancelled our
2859 * state before it received the OPEN_CONFIRM.
2860 * Recover by retrying the request as per the discussion
2861 * on Page 181 of RFC3530.
2862 */
2863 if (status == -NFS4ERR_BAD_STATEID) {
2864 exception.retry = 1;
2865 continue;
2866 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002867 if (status == -EAGAIN) {
2868 /* We must have found a delegation */
2869 exception.retry = 1;
2870 continue;
2871 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002872 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2873 continue;
2874 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 status, &exception));
2876 } while (exception.retry);
2877 return res;
2878}
2879
Trond Myklebust8487c472016-06-26 08:44:35 -04002880static int _nfs4_do_setattr(struct inode *inode,
2881 struct nfs_setattrargs *arg,
2882 struct nfs_setattrres *res,
2883 struct rpc_cred *cred,
2884 struct nfs4_state *state)
2885{
2886 struct nfs_server *server = NFS_SERVER(inode);
2887 struct rpc_message msg = {
2888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2889 .rpc_argp = arg,
2890 .rpc_resp = res,
2891 .rpc_cred = cred,
2892 };
2893 struct rpc_cred *delegation_cred = NULL;
2894 unsigned long timestamp = jiffies;
2895 fmode_t fmode;
2896 bool truncate;
2897 int status;
2898
2899 nfs_fattr_init(res->fattr);
2900
2901 /* Servers should only apply open mode checks for file size changes */
2902 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2903 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2904
2905 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2906 /* Use that stateid */
2907 } else if (truncate && state != NULL) {
2908 struct nfs_lockowner lockowner = {
2909 .l_owner = current->files,
2910 .l_pid = current->tgid,
2911 };
2912 if (!nfs4_valid_open_stateid(state))
2913 return -EBADF;
2914 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2915 &arg->stateid, &delegation_cred) == -EIO)
2916 return -EBADF;
2917 } else
2918 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2919 if (delegation_cred)
2920 msg.rpc_cred = delegation_cred;
2921
2922 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2923
2924 put_rpccred(delegation_cred);
2925 if (status == 0 && state != NULL)
2926 renew_lease(server, timestamp);
2927 trace_nfs4_setattr(inode, &arg->stateid, status);
2928 return status;
2929}
2930
2931static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2932 struct nfs_fattr *fattr, struct iattr *sattr,
2933 struct nfs4_state *state, struct nfs4_label *ilabel,
2934 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002936 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002938 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 .iap = sattr,
2940 .server = server,
2941 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002942 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 };
2944 struct nfs_setattrres res = {
2945 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002946 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 .server = server,
2948 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002949 struct nfs4_exception exception = {
2950 .state = state,
2951 .inode = inode,
2952 .stateid = &arg.stateid,
2953 };
2954 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
David Quigleyaa9c2662013-05-22 12:50:44 -04002956 arg.bitmask = nfs4_bitmask(server, ilabel);
2957 if (ilabel)
2958 arg.bitmask = nfs4_bitmask(server, olabel);
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002961 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002962 switch (err) {
2963 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002964 if (!(sattr->ia_valid & ATTR_SIZE)) {
2965 pr_warn_once("NFSv4: server %s is incorrectly "
2966 "applying open mode checks to "
2967 "a SETATTR that is not "
2968 "changing file size.\n",
2969 server->nfs_client->cl_hostname);
2970 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002971 if (state && !(state->state & FMODE_WRITE)) {
2972 err = -EBADF;
2973 if (sattr->ia_valid & ATTR_OPEN)
2974 err = -EACCES;
2975 goto out;
2976 }
2977 }
2978 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002980out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return err;
2982}
2983
Peng Tao500d7012015-09-22 11:35:22 +08002984static bool
2985nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2986{
2987 if (inode == NULL || !nfs_have_layout(inode))
2988 return false;
2989
2990 return pnfs_wait_on_layoutreturn(inode, task);
2991}
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993struct nfs4_closedata {
2994 struct inode *inode;
2995 struct nfs4_state *state;
2996 struct nfs_closeargs arg;
2997 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002998 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002999 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003000 bool roc;
3001 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002};
3003
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003004static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003005{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003006 struct nfs4_closedata *calldata = data;
3007 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003008 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003009
Fred Isamanf7e89172011-01-06 11:36:32 +00003010 if (calldata->roc)
3011 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003012 nfs4_put_open_state(calldata->state);
3013 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003014 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003015 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003016 kfree(calldata);
3017}
3018
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003019static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003021 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003024 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Chuck Levera3ca5652012-03-01 17:00:40 -05003026 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003027 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3028 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003029 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 /* hmm. we are done with the inode, and in the process of freeing
3031 * the state_owner. we keep this around to process errors
3032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 switch (task->tk_status) {
3034 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003035 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003036 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003037 pnfs_roc_set_barrier(state->inode,
3038 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003039 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003040 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003041 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003043 case -NFS4ERR_EXPIRED:
3044 nfs4_free_revoked_stateid(server,
3045 &calldata->arg.stateid,
3046 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003047 case -NFS4ERR_OLD_STATEID:
3048 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003049 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003050 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003051 rpc_restart_call_prepare(task);
3052 goto out_release;
3053 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003054 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003055 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003057 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003058 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003059 goto out_release;
3060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003062 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3063 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003064out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003065 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003066 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003067 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003070static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003072 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003073 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003074 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003075 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003076 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003077
Chuck Levera3ca5652012-03-01 17:00:40 -05003078 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003079 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003080 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003081
Trond Myklebust88069f72009-12-08 08:33:16 -05003082 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003083 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003084 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3085 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3086 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003087 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003088 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003089 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003090 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003091 if (state->n_rdonly == 0)
3092 call_close |= is_rdonly;
3093 else if (is_rdonly)
3094 calldata->arg.fmode |= FMODE_READ;
3095 if (state->n_wronly == 0)
3096 call_close |= is_wronly;
3097 else if (is_wronly)
3098 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003099 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3100 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003101 } else if (is_rdwr)
3102 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3103
Trond Myklebust5d422302013-03-14 16:57:48 -04003104 if (!nfs4_valid_open_stateid(state))
3105 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003106 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003107
3108 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003109 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003110 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003111 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003112
Peng Tao500d7012015-09-22 11:35:22 +08003113 if (nfs4_wait_on_layoutreturn(inode, task)) {
3114 nfs_release_seqid(calldata->arg.seqid);
3115 goto out_wait;
3116 }
3117
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003118 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003119 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003120 if (calldata->roc)
3121 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3122
Trond Myklebust6ae37332015-01-30 14:21:14 -05003123 calldata->arg.share_access =
3124 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3125 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003126
Trond Myklebust516a6af2005-10-27 22:12:41 -04003127 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003128 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003129 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003130 &calldata->arg.seq_args,
3131 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003132 task) != 0)
3133 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003134 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003135 return;
3136out_no_action:
3137 task->tk_action = NULL;
3138out_wait:
3139 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003142static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003143 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003144 .rpc_call_done = nfs4_close_done,
3145 .rpc_release = nfs4_free_closedata,
3146};
3147
Peng Taofe08c542014-07-03 13:05:01 +08003148static bool nfs4_roc(struct inode *inode)
3149{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003150 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003151 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003152 return pnfs_roc(inode);
3153}
3154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155/*
3156 * It is possible for data to be read/written from a mem-mapped file
3157 * after the sys_close call (which hits the vfs layer as a flush).
3158 * This means that we can't safely call nfsv4 close on a file until
3159 * the inode is cleared. This in turn means that we are not good
3160 * NFSv4 citizens - we do not indicate to the server to update the file's
3161 * share state even when we are done with one of the three share
3162 * stateid's in the inode.
3163 *
3164 * NOTE: Caller must be holding the sp->so_owner semaphore!
3165 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003166int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003168 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003169 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003171 struct nfs4_state_owner *sp = state->owner;
3172 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003173 struct rpc_message msg = {
3174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3175 .rpc_cred = state->owner->so_cred,
3176 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003177 struct rpc_task_setup task_setup_data = {
3178 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003179 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003180 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003181 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003182 .flags = RPC_TASK_ASYNC,
3183 };
Trond Myklebust95121352005-10-18 14:20:12 -07003184 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003186 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3187 &task_setup_data.rpc_client, &msg);
3188
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003189 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003191 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003192 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003193 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003195 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003197 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3198 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003199 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003200 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003201 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003202 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003203 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003204 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003205 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003206 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003207 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003208
Trond Myklebust1174dd12010-12-21 10:52:24 -05003209 msg.rpc_argp = &calldata->arg;
3210 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003211 task_setup_data.callback_data = calldata;
3212 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003213 if (IS_ERR(task))
3214 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003215 status = 0;
3216 if (wait)
3217 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003218 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003219 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003220out_free_calldata:
3221 kfree(calldata);
3222out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003223 nfs4_put_open_state(state);
3224 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003225 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
Trond Myklebust2b484292010-09-17 10:56:51 -04003228static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003229nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3230 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003233 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3234
3235 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Trond Myklebust565277f2007-10-15 18:17:53 -04003237 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003238 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003239
3240 nfs4_label_release_security(label);
3241
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003242 if (IS_ERR(state))
3243 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003244 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
Trond Myklebust1185a552009-12-03 15:54:02 -05003247static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003248{
3249 if (ctx->state == NULL)
3250 return;
3251 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003252 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003253 else
Al Viro643168c2011-06-22 18:20:23 -04003254 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Trond Myklebustb944dba2013-11-04 15:20:20 -05003257#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3258#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003259#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3262{
Kinglong Mee8c612822015-08-26 21:12:58 +08003263 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003264 struct nfs4_server_caps_arg args = {
3265 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003266 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003267 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct nfs4_server_caps_res res = {};
3269 struct rpc_message msg = {
3270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003271 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 .rpc_resp = &res,
3273 };
3274 int status;
3275
Kinglong Mee8c612822015-08-26 21:12:58 +08003276 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3277 FATTR4_WORD0_FH_EXPIRE_TYPE |
3278 FATTR4_WORD0_LINK_SUPPORT |
3279 FATTR4_WORD0_SYMLINK_SUPPORT |
3280 FATTR4_WORD0_ACLSUPPORT;
3281 if (minorversion)
3282 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3283
Bryan Schumaker7c513052011-03-24 17:12:24 +00003284 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003286 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003287 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003288 case 0:
3289 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3290 res.attr_bitmask[2] = 0;
3291 break;
3292 case 1:
3293 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3294 break;
3295 case 2:
3296 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003299 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3300 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3301 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3302 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003303 NFS_CAP_CTIME|NFS_CAP_MTIME|
3304 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003305 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3306 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 server->caps |= NFS_CAP_ACLS;
3308 if (res.has_links != 0)
3309 server->caps |= NFS_CAP_HARDLINKS;
3310 if (res.has_symlinks != 0)
3311 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003312 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3313 server->caps |= NFS_CAP_FILEID;
3314 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3315 server->caps |= NFS_CAP_MODE;
3316 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3317 server->caps |= NFS_CAP_NLINK;
3318 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3319 server->caps |= NFS_CAP_OWNER;
3320 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3321 server->caps |= NFS_CAP_OWNER_GROUP;
3322 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3323 server->caps |= NFS_CAP_ATIME;
3324 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3325 server->caps |= NFS_CAP_CTIME;
3326 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3327 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003328#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3329 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3330 server->caps |= NFS_CAP_SECURITY_LABEL;
3331#endif
3332 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3333 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003334 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003335
Trond Myklebusta65318b2009-03-11 14:10:28 -04003336 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3337 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3338 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003339 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003340 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3341 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003343 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return status;
3347}
3348
Trond Myklebust55a97592006-06-09 09:34:19 -04003349int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
3351 struct nfs4_exception exception = { };
3352 int err;
3353 do {
3354 err = nfs4_handle_exception(server,
3355 _nfs4_server_capabilities(server, fhandle),
3356 &exception);
3357 } while (exception.retry);
3358 return err;
3359}
3360
3361static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3362 struct nfs_fsinfo *info)
3363{
David Quigleyaa9c2662013-05-22 12:50:44 -04003364 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003366 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 };
3368 struct nfs4_lookup_res res = {
3369 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003370 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 .fh = fhandle,
3372 };
3373 struct rpc_message msg = {
3374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3375 .rpc_argp = &args,
3376 .rpc_resp = &res,
3377 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003378
David Quigleyaa9c2662013-05-22 12:50:44 -04003379 bitmask[0] = nfs4_fattr_bitmap[0];
3380 bitmask[1] = nfs4_fattr_bitmap[1];
3381 /*
3382 * Process the label in the upcoming getfattr
3383 */
3384 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3385
Trond Myklebust0e574af2005-10-27 22:12:38 -04003386 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003387 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389
3390static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3391 struct nfs_fsinfo *info)
3392{
3393 struct nfs4_exception exception = { };
3394 int err;
3395 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003396 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003397 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003398 switch (err) {
3399 case 0:
3400 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003401 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003402 default:
3403 err = nfs4_handle_exception(server, err, &exception);
3404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003406out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return err;
3408}
3409
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003410static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3411 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3412{
Trond Myklebustc2190662013-08-26 19:23:04 -04003413 struct rpc_auth_create_args auth_args = {
3414 .pseudoflavor = flavor,
3415 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003416 struct rpc_auth *auth;
3417 int ret;
3418
Trond Myklebustc2190662013-08-26 19:23:04 -04003419 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003420 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003421 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003422 goto out;
3423 }
3424 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003425out:
3426 return ret;
3427}
3428
Chuck Lever9a744ba2013-03-16 15:56:02 -04003429/*
3430 * Retry pseudoroot lookup with various security flavors. We do this when:
3431 *
3432 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3433 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3434 *
3435 * Returns zero on success, or a negative NFS4ERR value, or a
3436 * negative errno value.
3437 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003438static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003439 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003441 /* Per 3530bis 15.33.5 */
3442 static const rpc_authflavor_t flav_array[] = {
3443 RPC_AUTH_GSS_KRB5P,
3444 RPC_AUTH_GSS_KRB5I,
3445 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003446 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003447 RPC_AUTH_NULL,
3448 };
3449 int status = -EPERM;
3450 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003452 if (server->auth_info.flavor_len > 0) {
3453 /* try each flavor specified by user */
3454 for (i = 0; i < server->auth_info.flavor_len; i++) {
3455 status = nfs4_lookup_root_sec(server, fhandle, info,
3456 server->auth_info.flavors[i]);
3457 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3458 continue;
3459 break;
3460 }
3461 } else {
3462 /* no flavors specified by user, try default list */
3463 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3464 status = nfs4_lookup_root_sec(server, fhandle, info,
3465 flav_array[i]);
3466 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3467 continue;
3468 break;
3469 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003470 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003471
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003472 /*
3473 * -EACCESS could mean that the user doesn't have correct permissions
3474 * to access the mount. It could also mean that we tried to mount
3475 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3476 * existing mount programs don't handle -EACCES very well so it should
3477 * be mapped to -EPERM instead.
3478 */
3479 if (status == -EACCES)
3480 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003481 return status;
3482}
3483
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003484/**
3485 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3486 * @server: initialized nfs_server handle
3487 * @fhandle: we fill in the pseudo-fs root file handle
3488 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003489 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003490 *
3491 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003492 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003493int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003494 struct nfs_fsinfo *info,
3495 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003496{
Andre Przywarac7757072015-04-23 17:17:40 +01003497 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003498
Andre Przywarac7757072015-04-23 17:17:40 +01003499 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003500 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003501
3502 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003503 status = server->nfs_client->cl_mvops->find_root_sec(server,
3504 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 if (status == 0)
3507 status = nfs4_server_capabilities(server, fhandle);
3508 if (status == 0)
3509 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003510
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003511 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Bryan Schumakerbae36242012-05-10 15:07:31 -04003514static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3515 struct nfs_fsinfo *info)
3516{
3517 int error;
3518 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003519 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003520
3521 error = nfs4_server_capabilities(server, mntfh);
3522 if (error < 0) {
3523 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3524 return error;
3525 }
3526
David Quigley14c43f72013-05-22 12:50:43 -04003527 label = nfs4_label_alloc(server, GFP_KERNEL);
3528 if (IS_ERR(label))
3529 return PTR_ERR(label);
3530
David Quigley1775fd32013-05-22 12:50:42 -04003531 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003532 if (error < 0) {
3533 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003534 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003535 }
3536
3537 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3538 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3539 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3540
David Quigley14c43f72013-05-22 12:50:43 -04003541err_free_label:
3542 nfs4_label_free(label);
3543
Bryan Schumakerbae36242012-05-10 15:07:31 -04003544 return error;
3545}
3546
Manoj Naik6b97fd32006-06-09 09:34:29 -04003547/*
3548 * Get locations and (maybe) other attributes of a referral.
3549 * Note that we'll actually follow the referral later when
3550 * we detect fsid mismatch in inode revalidation
3551 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003552static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3553 const struct qstr *name, struct nfs_fattr *fattr,
3554 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003555{
3556 int status = -ENOMEM;
3557 struct page *page = NULL;
3558 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003559
3560 page = alloc_page(GFP_KERNEL);
3561 if (page == NULL)
3562 goto out;
3563 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3564 if (locations == NULL)
3565 goto out;
3566
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003567 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003568 if (status != 0)
3569 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003570
3571 /*
3572 * If the fsid didn't change, this is a migration event, not a
3573 * referral. Cause us to drop into the exception handler, which
3574 * will kick off migration recovery.
3575 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003576 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003577 dprintk("%s: server did not return a different fsid for"
3578 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003579 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003580 goto out;
3581 }
Andy Adamson533eb462011-06-13 18:25:56 -04003582 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3583 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003584
Andy Adamson533eb462011-06-13 18:25:56 -04003585 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003586 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003587 memset(fhandle, 0, sizeof(struct nfs_fh));
3588out:
3589 if (page)
3590 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003591 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003592 return status;
3593}
3594
David Quigley1775fd32013-05-22 12:50:42 -04003595static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3596 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
3598 struct nfs4_getattr_arg args = {
3599 .fh = fhandle,
3600 .bitmask = server->attr_bitmask,
3601 };
3602 struct nfs4_getattr_res res = {
3603 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003604 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 .server = server,
3606 };
3607 struct rpc_message msg = {
3608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3609 .rpc_argp = &args,
3610 .rpc_resp = &res,
3611 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003612
3613 args.bitmask = nfs4_bitmask(server, label);
3614
Trond Myklebust0e574af2005-10-27 22:12:38 -04003615 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003616 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
3618
David Quigley1775fd32013-05-22 12:50:42 -04003619static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3620 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
3622 struct nfs4_exception exception = { };
3623 int err;
3624 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003625 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3626 trace_nfs4_getattr(server, fhandle, fattr, err);
3627 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 &exception);
3629 } while (exception.retry);
3630 return err;
3631}
3632
3633/*
3634 * The file is not closed if it is opened due to the a request to change
3635 * the size of the file. The open call will not be needed once the
3636 * VFS layer lookup-intents are implemented.
3637 *
3638 * Close is called when the inode is destroyed.
3639 * If we haven't opened the file for O_WRONLY, we
3640 * need to in the size_change case to obtain a stateid.
3641 *
3642 * Got race?
3643 * Because OPEN is always done by name in nfsv4, it is
3644 * possible that we opened a different file by the same
3645 * name. We can recognize this race condition, but we
3646 * can't do anything about it besides returning an error.
3647 *
3648 * This will be fixed with VFS changes (lookup-intent).
3649 */
3650static int
3651nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3652 struct iattr *sattr)
3653{
David Howells2b0143b2015-03-17 22:25:59 +00003654 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003655 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003656 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003657 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 int status;
3659
Peng Tao88ac8152014-09-12 11:04:10 +08003660 if (pnfs_ld_layoutret_on_setattr(inode) &&
3661 sattr->ia_valid & ATTR_SIZE &&
3662 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003663 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003664
Trond Myklebust0e574af2005-10-27 22:12:38 -04003665 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Andy Adamson26699402012-05-30 16:12:24 -04003667 /* Deal with open(O_TRUNC) */
3668 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003669 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003670
3671 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003672 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003673 return 0;
3674
Trond Myklebustd5308382005-11-04 15:33:38 -05003675 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003676 if (sattr->ia_valid & ATTR_FILE) {
3677 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003678
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003679 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003680 if (ctx) {
3681 cred = ctx->cred;
3682 state = ctx->state;
3683 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003684 }
3685
David Quigley14c43f72013-05-22 12:50:43 -04003686 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3687 if (IS_ERR(label))
3688 return PTR_ERR(label);
3689
3690 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003691 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003692 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003693 nfs_setsecurity(inode, fattr, label);
3694 }
David Quigley14c43f72013-05-22 12:50:43 -04003695 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return status;
3697}
3698
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003699static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3700 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003701 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003702{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003703 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003704 int status;
3705 struct nfs4_lookup_arg args = {
3706 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003707 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003708 .name = name,
3709 };
3710 struct nfs4_lookup_res res = {
3711 .server = server,
3712 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003713 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003714 .fh = fhandle,
3715 };
3716 struct rpc_message msg = {
3717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3718 .rpc_argp = &args,
3719 .rpc_resp = &res,
3720 };
3721
David Quigleyaa9c2662013-05-22 12:50:44 -04003722 args.bitmask = nfs4_bitmask(server, label);
3723
David Howells2b3de442006-08-22 20:06:09 -04003724 nfs_fattr_init(fattr);
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003727 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 dprintk("NFS reply lookup: %d\n", status);
3729 return status;
3730}
3731
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003732static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003733{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003734 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003735 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003736 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3737 fattr->nlink = 2;
3738}
3739
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003740static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003741 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003742 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003743{
3744 struct nfs4_exception exception = { };
3745 struct rpc_clnt *client = *clnt;
3746 int err;
3747 do {
David Quigley1775fd32013-05-22 12:50:42 -04003748 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003749 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003750 switch (err) {
3751 case -NFS4ERR_BADNAME:
3752 err = -ENOENT;
3753 goto out;
3754 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003755 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003756 if (err == -NFS4ERR_MOVED)
3757 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003758 goto out;
3759 case -NFS4ERR_WRONGSEC:
3760 err = -EPERM;
3761 if (client != *clnt)
3762 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003763 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003764 if (IS_ERR(client))
3765 return PTR_ERR(client);
3766
3767 exception.retry = 1;
3768 break;
3769 default:
3770 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3771 }
3772 } while (exception.retry);
3773
3774out:
3775 if (err == 0)
3776 *clnt = client;
3777 else if (client != *clnt)
3778 rpc_shutdown_client(client);
3779
3780 return err;
3781}
3782
Al Virobeffb8f2016-07-20 16:34:42 -04003783static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003784 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3785 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003787 int status;
3788 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003789
David Quigley1775fd32013-05-22 12:50:42 -04003790 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003791 if (client != NFS_CLIENT(dir)) {
3792 rpc_shutdown_client(client);
3793 nfs_fixup_secinfo_attributes(fattr);
3794 }
3795 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003798struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003799nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003800 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3801{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003802 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003803 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003804
David Quigley1775fd32013-05-22 12:50:42 -04003805 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003806 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003807 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003808 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003809}
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3812{
Trond Myklebust76b32992007-08-10 17:45:11 -04003813 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 struct nfs4_accessargs args = {
3815 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003816 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003818 struct nfs4_accessres res = {
3819 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003820 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 struct rpc_message msg = {
3822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3823 .rpc_argp = &args,
3824 .rpc_resp = &res,
3825 .rpc_cred = entry->cred,
3826 };
3827 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003828 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830 /*
3831 * Determine which access bits we want to ask for...
3832 */
3833 if (mode & MAY_READ)
3834 args.access |= NFS4_ACCESS_READ;
3835 if (S_ISDIR(inode->i_mode)) {
3836 if (mode & MAY_WRITE)
3837 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3838 if (mode & MAY_EXEC)
3839 args.access |= NFS4_ACCESS_LOOKUP;
3840 } else {
3841 if (mode & MAY_WRITE)
3842 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3843 if (mode & MAY_EXEC)
3844 args.access |= NFS4_ACCESS_EXECUTE;
3845 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003846
3847 res.fattr = nfs_alloc_fattr();
3848 if (res.fattr == NULL)
3849 return -ENOMEM;
3850
Bryan Schumaker7c513052011-03-24 17:12:24 +00003851 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003853 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003854 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003856 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 return status;
3858}
3859
3860static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3861{
3862 struct nfs4_exception exception = { };
3863 int err;
3864 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003865 err = _nfs4_proc_access(inode, entry);
3866 trace_nfs4_access(inode, err);
3867 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 &exception);
3869 } while (exception.retry);
3870 return err;
3871}
3872
3873/*
3874 * TODO: For the time being, we don't try to get any attributes
3875 * along with any of the zero-copy operations READ, READDIR,
3876 * READLINK, WRITE.
3877 *
3878 * In the case of the first three, we want to put the GETATTR
3879 * after the read-type operation -- this is because it is hard
3880 * to predict the length of a GETATTR response in v4, and thus
3881 * align the READ data correctly. This means that the GETATTR
3882 * may end up partially falling into the page cache, and we should
3883 * shift it into the 'tail' of the xdr_buf before processing.
3884 * To do this efficiently, we need to know the total length
3885 * of data received, which doesn't seem to be available outside
3886 * of the RPC layer.
3887 *
3888 * In the case of WRITE, we also want to put the GETATTR after
3889 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003890 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 *
3892 * Both of these changes to the XDR layer would in fact be quite
3893 * minor, but I decided to leave them for a subsequent patch.
3894 */
3895static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3896 unsigned int pgbase, unsigned int pglen)
3897{
3898 struct nfs4_readlink args = {
3899 .fh = NFS_FH(inode),
3900 .pgbase = pgbase,
3901 .pglen = pglen,
3902 .pages = &page,
3903 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003904 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 struct rpc_message msg = {
3906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3907 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003908 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 };
3910
Bryan Schumaker7c513052011-03-24 17:12:24 +00003911 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 -07003912}
3913
3914static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3915 unsigned int pgbase, unsigned int pglen)
3916{
3917 struct nfs4_exception exception = { };
3918 int err;
3919 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003920 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3921 trace_nfs4_readlink(inode, err);
3922 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 &exception);
3924 } while (exception.retry);
3925 return err;
3926}
3927
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003929 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931static int
3932nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003933 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
David Quigleyaa9c2662013-05-22 12:50:44 -04003935 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003936 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 int status = 0;
3939
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003940 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3941 if (IS_ERR(ctx))
3942 return PTR_ERR(ctx);
3943
David Quigleyaa9c2662013-05-22 12:50:44 -04003944 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3945
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003946 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003947 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 if (IS_ERR(state)) {
3949 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003950 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003953 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003954 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 return status;
3956}
3957
Al Virobeffb8f2016-07-20 16:34:42 -04003958static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959{
Trond Myklebust16e42952005-10-27 22:12:44 -04003960 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003961 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003963 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003965 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003966 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003967 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3970 .rpc_argp = &args,
3971 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003973 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003974
Bryan Schumaker7c513052011-03-24 17:12:24 +00003975 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003976 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003977 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 return status;
3979}
3980
Al Virobeffb8f2016-07-20 16:34:42 -04003981static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
3983 struct nfs4_exception exception = { };
3984 int err;
3985 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003986 err = _nfs4_proc_remove(dir, name);
3987 trace_nfs4_remove(dir, name, err);
3988 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 &exception);
3990 } while (exception.retry);
3991 return err;
3992}
3993
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003994static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003996 struct nfs_server *server = NFS_SERVER(dir);
3997 struct nfs_removeargs *args = msg->rpc_argp;
3998 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004000 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004002 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004003
4004 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005}
4006
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004007static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4008{
Al Viro884be172016-04-28 23:56:31 -04004009 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004010 &data->args.seq_args,
4011 &data->res.seq_res,
4012 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013}
4014
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004015static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004017 struct nfs_unlinkdata *data = task->tk_calldata;
4018 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004019
Trond Myklebust14516c32010-07-31 14:29:06 -04004020 if (!nfs4_sequence_done(task, &res->seq_res))
4021 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004022 if (nfs4_async_handle_error(task, res->server, NULL,
4023 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004024 return 0;
4025 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004026 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
Jeff Laytond3d41522010-09-17 17:31:57 -04004029static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4030{
4031 struct nfs_server *server = NFS_SERVER(dir);
4032 struct nfs_renameargs *arg = msg->rpc_argp;
4033 struct nfs_renameres *res = msg->rpc_resp;
4034
4035 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004036 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004037 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004038}
4039
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004040static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4041{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004042 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4043 &data->args.seq_args,
4044 &data->res.seq_res,
4045 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004046}
4047
4048static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4049 struct inode *new_dir)
4050{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004051 struct nfs_renamedata *data = task->tk_calldata;
4052 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004053
4054 if (!nfs4_sequence_done(task, &res->seq_res))
4055 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004056 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004057 return 0;
4058
4059 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004060 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004061 return 1;
4062}
4063
Al Virobeffb8f2016-07-20 16:34:42 -04004064static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004066 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 struct nfs4_link_arg arg = {
4068 .fh = NFS_FH(inode),
4069 .dir_fh = NFS_FH(dir),
4070 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004071 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004073 struct nfs4_link_res res = {
4074 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004075 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004076 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 struct rpc_message msg = {
4078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4079 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004080 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004082 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Trond Myklebust136f2622010-04-16 16:22:49 -04004084 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004085 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004086 goto out;
4087
David Quigley14c43f72013-05-22 12:50:43 -04004088 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4089 if (IS_ERR(res.label)) {
4090 status = PTR_ERR(res.label);
4091 goto out;
4092 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004093 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004094
Bryan Schumaker7c513052011-03-24 17:12:24 +00004095 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004096 if (!status) {
4097 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004098 status = nfs_post_op_update_inode(inode, res.fattr);
4099 if (!status)
4100 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004101 }
David Quigley14c43f72013-05-22 12:50:43 -04004102
4103
4104 nfs4_label_free(res.label);
4105
Trond Myklebust136f2622010-04-16 16:22:49 -04004106out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004107 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 return status;
4109}
4110
Al Virobeffb8f2016-07-20 16:34:42 -04004111static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113 struct nfs4_exception exception = { };
4114 int err;
4115 do {
4116 err = nfs4_handle_exception(NFS_SERVER(inode),
4117 _nfs4_proc_link(inode, dir, name),
4118 &exception);
4119 } while (exception.retry);
4120 return err;
4121}
4122
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004123struct nfs4_createdata {
4124 struct rpc_message msg;
4125 struct nfs4_create_arg arg;
4126 struct nfs4_create_res res;
4127 struct nfs_fh fh;
4128 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004129 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004130};
4131
4132static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004133 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004134{
4135 struct nfs4_createdata *data;
4136
4137 data = kzalloc(sizeof(*data), GFP_KERNEL);
4138 if (data != NULL) {
4139 struct nfs_server *server = NFS_SERVER(dir);
4140
David Quigley14c43f72013-05-22 12:50:43 -04004141 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4142 if (IS_ERR(data->label))
4143 goto out_free;
4144
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004145 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4146 data->msg.rpc_argp = &data->arg;
4147 data->msg.rpc_resp = &data->res;
4148 data->arg.dir_fh = NFS_FH(dir);
4149 data->arg.server = server;
4150 data->arg.name = name;
4151 data->arg.attrs = sattr;
4152 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004153 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004154 data->res.server = server;
4155 data->res.fh = &data->fh;
4156 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004157 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004158 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004159 }
4160 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004161out_free:
4162 kfree(data);
4163 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004164}
4165
4166static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4167{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004168 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004169 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004170 if (status == 0) {
4171 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004172 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004173 }
4174 return status;
4175}
4176
4177static void nfs4_free_createdata(struct nfs4_createdata *data)
4178{
David Quigley14c43f72013-05-22 12:50:43 -04004179 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004180 kfree(data);
4181}
4182
Chuck Lever4f390c12006-08-22 20:06:22 -04004183static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004184 struct page *page, unsigned int len, struct iattr *sattr,
4185 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187 struct nfs4_createdata *data;
4188 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
Chuck Lever94a6d752006-08-22 20:06:23 -04004190 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004191 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004192
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004193 status = -ENOMEM;
4194 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4195 if (data == NULL)
4196 goto out;
4197
4198 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4199 data->arg.u.symlink.pages = &page;
4200 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004201 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004203 status = nfs4_do_create(dir, dentry, data);
4204
4205 nfs4_free_createdata(data);
4206out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 return status;
4208}
4209
Chuck Lever4f390c12006-08-22 20:06:22 -04004210static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004211 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212{
4213 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004214 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004216
4217 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004220 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4221 trace_nfs4_symlink(dir, &dentry->d_name, err);
4222 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 &exception);
4224 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004225
4226 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 return err;
4228}
4229
4230static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004231 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004233 struct nfs4_createdata *data;
4234 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004236 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4237 if (data == NULL)
4238 goto out;
4239
David Quigley1775fd32013-05-22 12:50:42 -04004240 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004241 status = nfs4_do_create(dir, dentry, data);
4242
4243 nfs4_free_createdata(data);
4244out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 return status;
4246}
4247
4248static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4249 struct iattr *sattr)
4250{
4251 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004252 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004254
David Quigleyaa9c2662013-05-22 12:50:44 -04004255 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4256
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004257 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004259 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4260 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4261 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 &exception);
4263 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004264 nfs4_label_release_security(label);
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return err;
4267}
4268
4269static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004270 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
David Howells2b0143b2015-03-17 22:25:59 +00004272 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 struct nfs4_readdir_arg args = {
4274 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004275 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 .pgbase = 0,
4277 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004278 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004279 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 };
4281 struct nfs4_readdir_res res;
4282 struct rpc_message msg = {
4283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4284 .rpc_argp = &args,
4285 .rpc_resp = &res,
4286 .rpc_cred = cred,
4287 };
4288 int status;
4289
Al Viro6de14722013-09-16 10:53:17 -04004290 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4291 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004292 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004293 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004295 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 -05004296 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004297 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004298 status += args.pgbase;
4299 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004300
4301 nfs_invalidate_atime(dir);
4302
Harvey Harrison3110ff82008-05-02 13:42:44 -07004303 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 return status;
4305}
4306
4307static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004308 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309{
4310 struct nfs4_exception exception = { };
4311 int err;
4312 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004313 err = _nfs4_proc_readdir(dentry, cred, cookie,
4314 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004315 trace_nfs4_readdir(d_inode(dentry), err);
4316 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 &exception);
4318 } while (exception.retry);
4319 return err;
4320}
4321
4322static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004323 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004325 struct nfs4_createdata *data;
4326 int mode = sattr->ia_mode;
4327 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004329 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4330 if (data == NULL)
4331 goto out;
4332
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004334 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004336 data->arg.ftype = NF4BLK;
4337 data->arg.u.device.specdata1 = MAJOR(rdev);
4338 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 }
4340 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004341 data->arg.ftype = NF4CHR;
4342 data->arg.u.device.specdata1 = MAJOR(rdev);
4343 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004344 } else if (!S_ISSOCK(mode)) {
4345 status = -EINVAL;
4346 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
David Quigley1775fd32013-05-22 12:50:42 -04004348
David Quigleyaa9c2662013-05-22 12:50:44 -04004349 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004350 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004351out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004352 nfs4_free_createdata(data);
4353out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 return status;
4355}
4356
4357static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4358 struct iattr *sattr, dev_t rdev)
4359{
4360 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004361 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004363
David Quigleyaa9c2662013-05-22 12:50:44 -04004364 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4365
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004366 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004368 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4369 trace_nfs4_mknod(dir, &dentry->d_name, err);
4370 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 &exception);
4372 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004373
4374 nfs4_label_release_security(label);
4375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 return err;
4377}
4378
4379static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4380 struct nfs_fsstat *fsstat)
4381{
4382 struct nfs4_statfs_arg args = {
4383 .fh = fhandle,
4384 .bitmask = server->attr_bitmask,
4385 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004386 struct nfs4_statfs_res res = {
4387 .fsstat = fsstat,
4388 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 struct rpc_message msg = {
4390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4391 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004392 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 };
4394
Trond Myklebust0e574af2005-10-27 22:12:38 -04004395 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004396 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397}
4398
4399static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4400{
4401 struct nfs4_exception exception = { };
4402 int err;
4403 do {
4404 err = nfs4_handle_exception(server,
4405 _nfs4_proc_statfs(server, fhandle, fsstat),
4406 &exception);
4407 } while (exception.retry);
4408 return err;
4409}
4410
4411static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4412 struct nfs_fsinfo *fsinfo)
4413{
4414 struct nfs4_fsinfo_arg args = {
4415 .fh = fhandle,
4416 .bitmask = server->attr_bitmask,
4417 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004418 struct nfs4_fsinfo_res res = {
4419 .fsinfo = fsinfo,
4420 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 struct rpc_message msg = {
4422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4423 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004424 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 };
4426
Bryan Schumaker7c513052011-03-24 17:12:24 +00004427 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
4430static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4431{
4432 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004433 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 int err;
4435
4436 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004437 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004438 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004439 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004440 nfs4_set_lease_period(server->nfs_client,
4441 fsinfo->lease_time * HZ,
4442 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004443 break;
4444 }
4445 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 } while (exception.retry);
4447 return err;
4448}
4449
4450static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4451{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004452 int error;
4453
Trond Myklebust0e574af2005-10-27 22:12:38 -04004454 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004455 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004456 if (error == 0) {
4457 /* block layout checks this! */
4458 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004459 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004460 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004461
4462 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463}
4464
4465static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4466 struct nfs_pathconf *pathconf)
4467{
4468 struct nfs4_pathconf_arg args = {
4469 .fh = fhandle,
4470 .bitmask = server->attr_bitmask,
4471 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004472 struct nfs4_pathconf_res res = {
4473 .pathconf = pathconf,
4474 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 struct rpc_message msg = {
4476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4477 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004478 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 };
4480
4481 /* None of the pathconf attributes are mandatory to implement */
4482 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4483 memset(pathconf, 0, sizeof(*pathconf));
4484 return 0;
4485 }
4486
Trond Myklebust0e574af2005-10-27 22:12:38 -04004487 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004488 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489}
4490
4491static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4492 struct nfs_pathconf *pathconf)
4493{
4494 struct nfs4_exception exception = { };
4495 int err;
4496
4497 do {
4498 err = nfs4_handle_exception(server,
4499 _nfs4_proc_pathconf(server, fhandle, pathconf),
4500 &exception);
4501 } while (exception.retry);
4502 return err;
4503}
4504
Trond Myklebust5521abf2013-03-16 20:54:34 -04004505int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004506 const struct nfs_open_context *ctx,
4507 const struct nfs_lock_context *l_ctx,
4508 fmode_t fmode)
4509{
4510 const struct nfs_lockowner *lockowner = NULL;
4511
4512 if (l_ctx != NULL)
4513 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004514 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004515}
4516EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4517
Trond Myklebust5521abf2013-03-16 20:54:34 -04004518static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4519 const struct nfs_open_context *ctx,
4520 const struct nfs_lock_context *l_ctx,
4521 fmode_t fmode)
4522{
4523 nfs4_stateid current_stateid;
4524
Trond Myklebuste1253be2014-03-05 08:44:23 -05004525 /* If the current stateid represents a lost lock, then exit */
4526 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4527 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004528 return nfs4_stateid_match(stateid, &current_stateid);
4529}
4530
4531static bool nfs4_error_stateid_expired(int err)
4532{
4533 switch (err) {
4534 case -NFS4ERR_DELEG_REVOKED:
4535 case -NFS4ERR_ADMIN_REVOKED:
4536 case -NFS4ERR_BAD_STATEID:
4537 case -NFS4ERR_STALE_STATEID:
4538 case -NFS4ERR_OLD_STATEID:
4539 case -NFS4ERR_OPENMODE:
4540 case -NFS4ERR_EXPIRED:
4541 return true;
4542 }
4543 return false;
4544}
4545
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004546void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004547{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004548 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004549}
4550
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004551static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004553 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004555 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004556 if (task->tk_status < 0) {
4557 struct nfs4_exception exception = {
4558 .inode = hdr->inode,
4559 .state = hdr->args.context->state,
4560 .stateid = &hdr->args.stateid,
4561 };
4562 task->tk_status = nfs4_async_handle_exception(task,
4563 server, task->tk_status, &exception);
4564 if (exception.retry) {
4565 rpc_restart_call_prepare(task);
4566 return -EAGAIN;
4567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004569
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004570 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004572 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574}
4575
Trond Myklebust5521abf2013-03-16 20:54:34 -04004576static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004577 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004578{
4579
4580 if (!nfs4_error_stateid_expired(task->tk_status) ||
4581 nfs4_stateid_is_current(&args->stateid,
4582 args->context,
4583 args->lock_context,
4584 FMODE_READ))
4585 return false;
4586 rpc_restart_call_prepare(task);
4587 return true;
4588}
4589
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004590static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004591{
4592
4593 dprintk("--> %s\n", __func__);
4594
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004595 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004596 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004597 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004598 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004599 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4600 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004601}
4602
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004603static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4604 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004606 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004607 if (!hdr->pgio_done_cb)
4608 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004609 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004610 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611}
4612
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004613static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4614 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004615{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004616 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4617 &hdr->args.seq_args,
4618 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004619 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004620 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004621 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4622 hdr->args.lock_context,
4623 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004624 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004625 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004626 return -EIO;
4627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628}
4629
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630static int nfs4_write_done_cb(struct rpc_task *task,
4631 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004633 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004634
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004635 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004636 if (task->tk_status < 0) {
4637 struct nfs4_exception exception = {
4638 .inode = hdr->inode,
4639 .state = hdr->args.context->state,
4640 .stateid = &hdr->args.stateid,
4641 };
4642 task->tk_status = nfs4_async_handle_exception(task,
4643 NFS_SERVER(inode), task->tk_status,
4644 &exception);
4645 if (exception.retry) {
4646 rpc_restart_call_prepare(task);
4647 return -EAGAIN;
4648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004650 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004651 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004652 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004653 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655}
4656
Trond Myklebust5521abf2013-03-16 20:54:34 -04004657static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004658 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004659{
4660
4661 if (!nfs4_error_stateid_expired(task->tk_status) ||
4662 nfs4_stateid_is_current(&args->stateid,
4663 args->context,
4664 args->lock_context,
4665 FMODE_WRITE))
4666 return false;
4667 rpc_restart_call_prepare(task);
4668 return true;
4669}
4670
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004671static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004672{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004673 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004674 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004676 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004677 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4678 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004679}
4680
Trond Myklebust5a37f852012-04-28 14:55:16 -04004681static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004682bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004683{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004684 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004685 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004686 return false;
4687 /* Otherwise, request attributes if and only if we don't hold
4688 * a delegation
4689 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004690 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004691}
Fred Isamana69aef12011-03-03 15:13:47 +00004692
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004693static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4694 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004696 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004697
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004698 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4699 hdr->args.bitmask = NULL;
4700 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004701 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004703
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004704 if (!hdr->pgio_done_cb)
4705 hdr->pgio_done_cb = nfs4_write_done_cb;
4706 hdr->res.server = server;
4707 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004709 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004710 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711}
4712
Fred Isaman0b7c0152012-04-20 14:47:39 -04004713static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4714{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004715 nfs4_setup_sequence(NFS_SERVER(data->inode),
4716 &data->args.seq_args,
4717 &data->res.seq_res,
4718 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719}
4720
Fred Isaman0b7c0152012-04-20 14:47:39 -04004721static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004724
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004725 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004726 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4727 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004728 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004729 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732}
4733
Fred Isaman0b7c0152012-04-20 14:47:39 -04004734static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004735{
4736 if (!nfs4_sequence_done(task, &data->res.seq_res))
4737 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004738 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004739}
4740
Fred Isaman0b7c0152012-04-20 14:47:39 -04004741static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004743 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004744
Fred Isaman0b7c0152012-04-20 14:47:39 -04004745 if (data->commit_done_cb == NULL)
4746 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004747 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004748 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004749 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750}
4751
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004752struct nfs4_renewdata {
4753 struct nfs_client *client;
4754 unsigned long timestamp;
4755};
4756
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757/*
4758 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4759 * standalone procedure for queueing an asynchronous RENEW.
4760 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004761static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004762{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004763 struct nfs4_renewdata *data = calldata;
4764 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004765
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004766 if (atomic_read(&clp->cl_count) > 1)
4767 nfs4_schedule_state_renewal(clp);
4768 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004769 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004770}
4771
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004772static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004774 struct nfs4_renewdata *data = calldata;
4775 struct nfs_client *clp = data->client;
4776 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004778 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004779 switch (task->tk_status) {
4780 case 0:
4781 break;
4782 case -NFS4ERR_LEASE_MOVED:
4783 nfs4_schedule_lease_moved_recovery(clp);
4784 break;
4785 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004786 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004787 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4788 return;
4789 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004790 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004791 return;
4792 }
4793 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004795 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796}
4797
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004798static const struct rpc_call_ops nfs4_renew_ops = {
4799 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004800 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004801};
4802
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004803static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804{
4805 struct rpc_message msg = {
4806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4807 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004808 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004810 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004812 if (renew_flags == 0)
4813 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004814 if (!atomic_inc_not_zero(&clp->cl_count))
4815 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004816 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004817 if (data == NULL)
4818 return -ENOMEM;
4819 data->client = clp;
4820 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004821 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004822 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004825static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
4827 struct rpc_message msg = {
4828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4829 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004830 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 };
4832 unsigned long now = jiffies;
4833 int status;
4834
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004835 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 if (status < 0)
4837 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004838 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 return 0;
4840}
4841
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004842static inline int nfs4_server_supports_acls(struct nfs_server *server)
4843{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004844 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004845}
4846
Trond Myklebust21f498c2012-08-24 10:59:25 -04004847/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4848 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004849 * the stack.
4850 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004851#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004852
Neil Hormane9e3d722011-03-04 19:26:03 -05004853static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004854 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004855{
4856 struct page *newpage, **spages;
4857 int rc = 0;
4858 size_t len;
4859 spages = pages;
4860
4861 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004862 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004863 newpage = alloc_page(GFP_KERNEL);
4864
4865 if (newpage == NULL)
4866 goto unwind;
4867 memcpy(page_address(newpage), buf, len);
4868 buf += len;
4869 buflen -= len;
4870 *pages++ = newpage;
4871 rc++;
4872 } while (buflen != 0);
4873
4874 return rc;
4875
4876unwind:
4877 for(; rc > 0; rc--)
4878 __free_page(spages[rc-1]);
4879 return -ENOMEM;
4880}
4881
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004882struct nfs4_cached_acl {
4883 int cached;
4884 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004885 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004886};
4887
4888static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004889{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004890 struct nfs_inode *nfsi = NFS_I(inode);
4891
4892 spin_lock(&inode->i_lock);
4893 kfree(nfsi->nfs4_acl);
4894 nfsi->nfs4_acl = acl;
4895 spin_unlock(&inode->i_lock);
4896}
4897
4898static void nfs4_zap_acl_attr(struct inode *inode)
4899{
4900 nfs4_set_cached_acl(inode, NULL);
4901}
4902
4903static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4904{
4905 struct nfs_inode *nfsi = NFS_I(inode);
4906 struct nfs4_cached_acl *acl;
4907 int ret = -ENOENT;
4908
4909 spin_lock(&inode->i_lock);
4910 acl = nfsi->nfs4_acl;
4911 if (acl == NULL)
4912 goto out;
4913 if (buf == NULL) /* user is just asking for length */
4914 goto out_len;
4915 if (acl->cached == 0)
4916 goto out;
4917 ret = -ERANGE; /* see getxattr(2) man page */
4918 if (acl->len > buflen)
4919 goto out;
4920 memcpy(buf, acl->data, acl->len);
4921out_len:
4922 ret = acl->len;
4923out:
4924 spin_unlock(&inode->i_lock);
4925 return ret;
4926}
4927
Sachin Prabhu5794d212012-04-17 14:36:40 +01004928static 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 +00004929{
4930 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004931 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004932
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004933 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004934 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004935 if (acl == NULL)
4936 goto out;
4937 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004938 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004939 } else {
4940 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4941 if (acl == NULL)
4942 goto out;
4943 acl->cached = 0;
4944 }
4945 acl->len = acl_len;
4946out:
4947 nfs4_set_cached_acl(inode, acl);
4948}
4949
Andy Adamsonbf118a32011-12-07 11:55:27 -05004950/*
4951 * The getxattr API returns the required buffer length when called with a
4952 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4953 * the required buf. On a NULL buf, we send a page of data to the server
4954 * guessing that the ACL request can be serviced by a page. If so, we cache
4955 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4956 * the cache. If not so, we throw away the page, and cache the required
4957 * length. The next getxattr call will then produce another round trip to
4958 * the server, this time with the input buf of the required size.
4959 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004960static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004961{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004962 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004963 struct nfs_getaclargs args = {
4964 .fh = NFS_FH(inode),
4965 .acl_pages = pages,
4966 .acl_len = buflen,
4967 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004968 struct nfs_getaclres res = {
4969 .acl_len = buflen,
4970 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004971 struct rpc_message msg = {
4972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4973 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004974 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004975 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004976 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4977 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004978
Andy Adamsonbf118a32011-12-07 11:55:27 -05004979 /* As long as we're doing a round trip to the server anyway,
4980 * let's be prepared for a page of acl data. */
4981 if (npages == 0)
4982 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004983 if (npages > ARRAY_SIZE(pages))
4984 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004985
Andy Adamsonbf118a32011-12-07 11:55:27 -05004986 for (i = 0; i < npages; i++) {
4987 pages[i] = alloc_page(GFP_KERNEL);
4988 if (!pages[i])
4989 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004990 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004991
4992 /* for decoding across pages */
4993 res.acl_scratch = alloc_page(GFP_KERNEL);
4994 if (!res.acl_scratch)
4995 goto out_free;
4996
Andy Adamsonbf118a32011-12-07 11:55:27 -05004997 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004998
Peng Taode040be2012-01-10 22:42:47 +08004999 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005000 __func__, buf, buflen, npages, args.acl_len);
5001 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5002 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005003 if (ret)
5004 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005005
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005006 /* Handle the case where the passed-in buffer is too short */
5007 if (res.acl_flags & NFS4_ACL_TRUNC) {
5008 /* Did the user only issue a request for the acl length? */
5009 if (buf == NULL)
5010 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005011 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005012 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005013 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005014 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005015 if (buf) {
5016 if (res.acl_len > buflen) {
5017 ret = -ERANGE;
5018 goto out_free;
5019 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005020 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005021 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005022out_ok:
5023 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005024out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005025 for (i = 0; i < npages; i++)
5026 if (pages[i])
5027 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005028 if (res.acl_scratch)
5029 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005030 return ret;
5031}
5032
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005033static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5034{
5035 struct nfs4_exception exception = { };
5036 ssize_t ret;
5037 do {
5038 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005039 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005040 if (ret >= 0)
5041 break;
5042 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5043 } while (exception.retry);
5044 return ret;
5045}
5046
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005047static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5048{
5049 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005050 int ret;
5051
5052 if (!nfs4_server_supports_acls(server))
5053 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005054 ret = nfs_revalidate_inode(server, inode);
5055 if (ret < 0)
5056 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005057 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5058 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005059 ret = nfs4_read_cached_acl(inode, buf, buflen);
5060 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005061 /* -ENOENT is returned if there is no ACL or if there is an ACL
5062 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005063 return ret;
5064 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005065}
5066
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005067static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005068{
5069 struct nfs_server *server = NFS_SERVER(inode);
5070 struct page *pages[NFS4ACL_MAXPAGES];
5071 struct nfs_setaclargs arg = {
5072 .fh = NFS_FH(inode),
5073 .acl_pages = pages,
5074 .acl_len = buflen,
5075 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005076 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005077 struct rpc_message msg = {
5078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5079 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005080 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005081 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005082 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005083 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005084
5085 if (!nfs4_server_supports_acls(server))
5086 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005087 if (npages > ARRAY_SIZE(pages))
5088 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005089 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005090 if (i < 0)
5091 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005092 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005093 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005094
5095 /*
5096 * Free each page after tx, so the only ref left is
5097 * held by the network stack
5098 */
5099 for (; i > 0; i--)
5100 put_page(pages[i-1]);
5101
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005102 /*
5103 * Acl update can result in inode attribute update.
5104 * so mark the attribute cache invalid.
5105 */
5106 spin_lock(&inode->i_lock);
5107 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5108 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005109 nfs_access_zap_cache(inode);
5110 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005111 return ret;
5112}
5113
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005114static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5115{
5116 struct nfs4_exception exception = { };
5117 int err;
5118 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005119 err = __nfs4_proc_set_acl(inode, buf, buflen);
5120 trace_nfs4_set_acl(inode, err);
5121 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005122 &exception);
5123 } while (exception.retry);
5124 return err;
5125}
5126
David Quigleyaa9c2662013-05-22 12:50:44 -04005127#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5128static int _nfs4_get_security_label(struct inode *inode, void *buf,
5129 size_t buflen)
5130{
5131 struct nfs_server *server = NFS_SERVER(inode);
5132 struct nfs_fattr fattr;
5133 struct nfs4_label label = {0, 0, buflen, buf};
5134
5135 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005136 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005137 .fh = NFS_FH(inode),
5138 .bitmask = bitmask,
5139 };
5140 struct nfs4_getattr_res res = {
5141 .fattr = &fattr,
5142 .label = &label,
5143 .server = server,
5144 };
5145 struct rpc_message msg = {
5146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005147 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005148 .rpc_resp = &res,
5149 };
5150 int ret;
5151
5152 nfs_fattr_init(&fattr);
5153
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005154 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005155 if (ret)
5156 return ret;
5157 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5158 return -ENOENT;
5159 if (buflen < label.len)
5160 return -ERANGE;
5161 return 0;
5162}
5163
5164static int nfs4_get_security_label(struct inode *inode, void *buf,
5165 size_t buflen)
5166{
5167 struct nfs4_exception exception = { };
5168 int err;
5169
5170 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5171 return -EOPNOTSUPP;
5172
5173 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005174 err = _nfs4_get_security_label(inode, buf, buflen);
5175 trace_nfs4_get_security_label(inode, err);
5176 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005177 &exception);
5178 } while (exception.retry);
5179 return err;
5180}
5181
5182static int _nfs4_do_set_security_label(struct inode *inode,
5183 struct nfs4_label *ilabel,
5184 struct nfs_fattr *fattr,
5185 struct nfs4_label *olabel)
5186{
5187
5188 struct iattr sattr = {0};
5189 struct nfs_server *server = NFS_SERVER(inode);
5190 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005191 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005192 .fh = NFS_FH(inode),
5193 .iap = &sattr,
5194 .server = server,
5195 .bitmask = bitmask,
5196 .label = ilabel,
5197 };
5198 struct nfs_setattrres res = {
5199 .fattr = fattr,
5200 .label = olabel,
5201 .server = server,
5202 };
5203 struct rpc_message msg = {
5204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005205 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005206 .rpc_resp = &res,
5207 };
5208 int status;
5209
Jeff Layton12207f62013-11-01 10:49:32 -04005210 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005211
Jeff Layton12207f62013-11-01 10:49:32 -04005212 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005213 if (status)
5214 dprintk("%s failed: %d\n", __func__, status);
5215
5216 return status;
5217}
5218
5219static int nfs4_do_set_security_label(struct inode *inode,
5220 struct nfs4_label *ilabel,
5221 struct nfs_fattr *fattr,
5222 struct nfs4_label *olabel)
5223{
5224 struct nfs4_exception exception = { };
5225 int err;
5226
5227 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005228 err = _nfs4_do_set_security_label(inode, ilabel,
5229 fattr, olabel);
5230 trace_nfs4_set_security_label(inode, err);
5231 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005232 &exception);
5233 } while (exception.retry);
5234 return err;
5235}
5236
5237static int
Al Viro59301222016-05-27 10:19:30 -04005238nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005239{
5240 struct nfs4_label ilabel, *olabel = NULL;
5241 struct nfs_fattr fattr;
5242 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005243 int status;
5244
5245 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5246 return -EOPNOTSUPP;
5247
5248 nfs_fattr_init(&fattr);
5249
5250 ilabel.pi = 0;
5251 ilabel.lfs = 0;
5252 ilabel.label = (char *)buf;
5253 ilabel.len = buflen;
5254
5255 cred = rpc_lookup_cred();
5256 if (IS_ERR(cred))
5257 return PTR_ERR(cred);
5258
5259 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5260 if (IS_ERR(olabel)) {
5261 status = -PTR_ERR(olabel);
5262 goto out;
5263 }
5264
5265 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5266 if (status == 0)
5267 nfs_setsecurity(inode, &fattr, olabel);
5268
5269 nfs4_label_free(olabel);
5270out:
5271 put_rpccred(cred);
5272 return status;
5273}
5274#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5275
5276
Chuck Leverf0920752012-05-21 22:45:41 -04005277static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5278 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005279{
5280 __be32 verf[2];
5281
Chuck Lever2c820d92012-05-21 22:45:33 -04005282 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5283 /* An impossible timestamp guarantees this value
5284 * will never match a generated boot time. */
5285 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005286 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005287 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005288 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005289 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5290 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005291 }
Chuck Levercd937102012-03-02 17:14:31 -05005292 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5293}
5294
Jeff Laytona3192682015-06-09 19:43:59 -04005295static int
5296nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005297{
Jeff Laytona3192682015-06-09 19:43:59 -04005298 size_t len;
5299 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005300
Trond Myklebustceb3a162015-01-03 15:16:04 -05005301 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005302 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005303
Jeff Laytona3192682015-06-09 19:43:59 -04005304 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005305 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005306 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5307 1 +
5308 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5309 1;
5310 rcu_read_unlock();
5311
5312 if (len > NFS4_OPAQUE_LIMIT + 1)
5313 return -EINVAL;
5314
5315 /*
5316 * Since this string is allocated at mount time, and held until the
5317 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5318 * about a memory-reclaim deadlock.
5319 */
5320 str = kmalloc(len, GFP_KERNEL);
5321 if (!str)
5322 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005323
Chuck Levere984a552012-09-14 17:24:21 -04005324 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005325 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005326 clp->cl_ipaddr,
5327 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5328 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005329 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005330
Jeff Laytona3192682015-06-09 19:43:59 -04005331 clp->cl_owner_id = str;
5332 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005333}
5334
Jeff Layton873e3852015-06-09 19:44:00 -04005335static int
5336nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005337{
Jeff Layton873e3852015-06-09 19:44:00 -04005338 size_t len;
5339 char *str;
5340
5341 len = 10 + 10 + 1 + 10 + 1 +
5342 strlen(nfs4_client_id_uniquifier) + 1 +
5343 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5344
5345 if (len > NFS4_OPAQUE_LIMIT + 1)
5346 return -EINVAL;
5347
5348 /*
5349 * Since this string is allocated at mount time, and held until the
5350 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5351 * about a memory-reclaim deadlock.
5352 */
5353 str = kmalloc(len, GFP_KERNEL);
5354 if (!str)
5355 return -ENOMEM;
5356
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005357 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005358 clp->rpc_ops->version, clp->cl_minorversion,
5359 nfs4_client_id_uniquifier,
5360 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005361 clp->cl_owner_id = str;
5362 return 0;
5363}
5364
5365static int
5366nfs4_init_uniform_client_string(struct nfs_client *clp)
5367{
Jeff Layton873e3852015-06-09 19:44:00 -04005368 size_t len;
5369 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005370
5371 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005372 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005373
5374 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005375 return nfs4_init_uniquifier_client_string(clp);
5376
5377 len = 10 + 10 + 1 + 10 + 1 +
5378 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5379
5380 if (len > NFS4_OPAQUE_LIMIT + 1)
5381 return -EINVAL;
5382
5383 /*
5384 * Since this string is allocated at mount time, and held until the
5385 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5386 * about a memory-reclaim deadlock.
5387 */
5388 str = kmalloc(len, GFP_KERNEL);
5389 if (!str)
5390 return -ENOMEM;
5391
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005392 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005393 clp->rpc_ops->version, clp->cl_minorversion,
5394 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005395 clp->cl_owner_id = str;
5396 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005397}
5398
Chuck Lever706cb8d2014-03-12 12:51:47 -04005399/*
5400 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5401 * services. Advertise one based on the address family of the
5402 * clientaddr.
5403 */
5404static unsigned int
5405nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5406{
5407 if (strchr(clp->cl_ipaddr, ':') != NULL)
5408 return scnprintf(buf, len, "tcp6");
5409 else
5410 return scnprintf(buf, len, "tcp");
5411}
5412
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005413static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5414{
5415 struct nfs4_setclientid *sc = calldata;
5416
5417 if (task->tk_status == 0)
5418 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5419}
5420
5421static const struct rpc_call_ops nfs4_setclientid_ops = {
5422 .rpc_call_done = nfs4_setclientid_done,
5423};
5424
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005425/**
5426 * nfs4_proc_setclientid - Negotiate client ID
5427 * @clp: state data structure
5428 * @program: RPC program for NFSv4 callback service
5429 * @port: IP port number for NFS4 callback service
5430 * @cred: RPC credential to use for this call
5431 * @res: where to place the result
5432 *
5433 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5434 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005435int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5436 unsigned short port, struct rpc_cred *cred,
5437 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438{
5439 nfs4_verifier sc_verifier;
5440 struct nfs4_setclientid setclientid = {
5441 .sc_verifier = &sc_verifier,
5442 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005443 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 };
5445 struct rpc_message msg = {
5446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5447 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005448 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005449 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005451 struct rpc_task *task;
5452 struct rpc_task_setup task_setup_data = {
5453 .rpc_client = clp->cl_rpcclient,
5454 .rpc_message = &msg,
5455 .callback_ops = &nfs4_setclientid_ops,
5456 .callback_data = &setclientid,
5457 .flags = RPC_TASK_TIMEOUT,
5458 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005459 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
Chuck Leverde734832012-07-11 16:30:50 -04005461 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005462 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005463
5464 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5465 status = nfs4_init_uniform_client_string(clp);
5466 else
Jeff Laytona3192682015-06-09 19:43:59 -04005467 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005468
5469 if (status)
5470 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005471
Chuck Leverde734832012-07-11 16:30:50 -04005472 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005473 setclientid.sc_netid_len =
5474 nfs4_init_callback_netid(clp,
5475 setclientid.sc_netid,
5476 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005477 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005478 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 clp->cl_ipaddr, port >> 8, port & 255);
5480
Jeff Layton3a6bb732015-06-09 19:43:57 -04005481 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005482 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005483 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005484 task = rpc_run_task(&task_setup_data);
5485 if (IS_ERR(task)) {
5486 status = PTR_ERR(task);
5487 goto out;
5488 }
5489 status = task->tk_status;
5490 if (setclientid.sc_cred) {
5491 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5492 put_rpccred(setclientid.sc_cred);
5493 }
5494 rpc_put_task(task);
5495out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005496 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005497 dprintk("NFS reply setclientid: %d\n", status);
5498 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499}
5500
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005501/**
5502 * nfs4_proc_setclientid_confirm - Confirm client ID
5503 * @clp: state data structure
5504 * @res: result of a previous SETCLIENTID
5505 * @cred: RPC credential to use for this call
5506 *
5507 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5508 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005509int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005510 struct nfs4_setclientid_res *arg,
5511 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 struct rpc_message msg = {
5514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005515 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005516 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 int status;
5519
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005520 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5521 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5522 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005523 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005524 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005525 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 return status;
5527}
5528
Trond Myklebustfe650402006-01-03 09:55:18 +01005529struct nfs4_delegreturndata {
5530 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005531 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005532 struct nfs_fh fh;
5533 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005534 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005535 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005536 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005537 struct inode *inode;
5538 bool roc;
5539 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005540};
5541
Trond Myklebustfe650402006-01-03 09:55:18 +01005542static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5543{
5544 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005545
Trond Myklebust14516c32010-07-31 14:29:06 -04005546 if (!nfs4_sequence_done(task, &data->res.seq_res))
5547 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005548
Trond Myklebustca8acf82013-08-13 10:36:56 -04005549 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005550 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005551 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005552 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005553 case -NFS4ERR_ADMIN_REVOKED:
5554 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005555 case -NFS4ERR_EXPIRED:
5556 nfs4_free_revoked_stateid(data->res.server,
5557 data->args.stateid,
5558 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005559 case -NFS4ERR_BAD_STATEID:
5560 case -NFS4ERR_OLD_STATEID:
5561 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005562 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005563 if (data->roc)
5564 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005565 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005566 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005567 if (nfs4_async_handle_error(task, data->res.server,
5568 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005569 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005570 return;
5571 }
5572 }
5573 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005574}
5575
5576static void nfs4_delegreturn_release(void *calldata)
5577{
Peng Tao039b7562014-07-03 13:05:02 +08005578 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005579 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005580
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005581 if (inode) {
5582 if (data->roc)
5583 pnfs_roc_release(inode);
5584 nfs_iput_and_deactive(inode);
5585 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005586 kfree(calldata);
5587}
5588
Andy Adamson938e1012009-04-01 09:22:28 -04005589static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5590{
5591 struct nfs4_delegreturndata *d_data;
5592
5593 d_data = (struct nfs4_delegreturndata *)data;
5594
Peng Tao500d7012015-09-22 11:35:22 +08005595 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5596 return;
5597
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005598 if (d_data->roc)
5599 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005600
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005601 nfs4_setup_sequence(d_data->res.server,
5602 &d_data->args.seq_args,
5603 &d_data->res.seq_res,
5604 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005605}
Andy Adamson938e1012009-04-01 09:22:28 -04005606
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005607static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005608 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005609 .rpc_call_done = nfs4_delegreturn_done,
5610 .rpc_release = nfs4_delegreturn_release,
5611};
5612
Trond Myklebuste6f81072008-01-24 18:14:34 -05005613static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005614{
5615 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005616 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005617 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005618 struct rpc_message msg = {
5619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5620 .rpc_cred = cred,
5621 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005622 struct rpc_task_setup task_setup_data = {
5623 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005624 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005625 .callback_ops = &nfs4_delegreturn_ops,
5626 .flags = RPC_TASK_ASYNC,
5627 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005628 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005629
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005630 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005631 if (data == NULL)
5632 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005633 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005634
5635 nfs4_state_protect(server->nfs_client,
5636 NFS_SP4_MACH_CRED_CLEANUP,
5637 &task_setup_data.rpc_client, &msg);
5638
Trond Myklebustfe650402006-01-03 09:55:18 +01005639 data->args.fhandle = &data->fh;
5640 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005641 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005642 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005643 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005644 data->res.fattr = &data->fattr;
5645 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005646 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005647 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005648 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005649 data->inode = nfs_igrab_and_active(inode);
5650 if (data->inode)
5651 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005652
Trond Myklebustc970aa82007-07-14 15:39:59 -04005653 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005654 msg.rpc_argp = &data->args;
5655 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005656 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005657 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005658 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005659 if (!issync)
5660 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005661 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005662 if (status != 0)
5663 goto out;
5664 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005665 if (status == 0)
5666 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5667 else
5668 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005669out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005670 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005671 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672}
5673
Trond Myklebuste6f81072008-01-24 18:14:34 -05005674int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
5676 struct nfs_server *server = NFS_SERVER(inode);
5677 struct nfs4_exception exception = { };
5678 int err;
5679 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005680 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005681 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 switch (err) {
5683 case -NFS4ERR_STALE_STATEID:
5684 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 case 0:
5686 return 0;
5687 }
5688 err = nfs4_handle_exception(server, err, &exception);
5689 } while (exception.retry);
5690 return err;
5691}
5692
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5694{
5695 struct inode *inode = state->inode;
5696 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005697 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005698 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005700 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005702 struct nfs_lockt_res res = {
5703 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 };
5705 struct rpc_message msg = {
5706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5707 .rpc_argp = &arg,
5708 .rpc_resp = &res,
5709 .rpc_cred = state->owner->so_cred,
5710 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 struct nfs4_lock_state *lsp;
5712 int status;
5713
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005714 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005715 status = nfs4_set_lock_state(state, request);
5716 if (status != 0)
5717 goto out;
5718 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005719 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005720 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005721 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005722 switch (status) {
5723 case 0:
5724 request->fl_type = F_UNLCK;
5725 break;
5726 case -NFS4ERR_DENIED:
5727 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005729 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005730 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005731out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 return status;
5733}
5734
5735static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5736{
5737 struct nfs4_exception exception = { };
5738 int err;
5739
5740 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005741 err = _nfs4_proc_getlk(state, cmd, request);
5742 trace_nfs4_get_lock(request, state, cmd, err);
5743 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 &exception);
5745 } while (exception.retry);
5746 return err;
5747}
5748
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005749struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005750 struct nfs_locku_args arg;
5751 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005752 struct nfs4_lock_state *lsp;
5753 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005754 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005755 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005756 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005757};
5758
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005759static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5760 struct nfs_open_context *ctx,
5761 struct nfs4_lock_state *lsp,
5762 struct nfs_seqid *seqid)
5763{
5764 struct nfs4_unlockdata *p;
5765 struct inode *inode = lsp->ls_state->inode;
5766
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005767 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005768 if (p == NULL)
5769 return NULL;
5770 p->arg.fh = NFS_FH(inode);
5771 p->arg.fl = &p->fl;
5772 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005773 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005774 p->lsp = lsp;
5775 atomic_inc(&lsp->ls_count);
5776 /* Ensure we don't close file until we're done freeing locks! */
5777 p->ctx = get_nfs_open_context(ctx);
5778 memcpy(&p->fl, fl, sizeof(p->fl));
5779 p->server = NFS_SERVER(inode);
5780 return p;
5781}
5782
Trond Myklebust06f814a2006-01-03 09:55:07 +01005783static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005784{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005785 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005786 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005787 nfs4_put_lock_state(calldata->lsp);
5788 put_nfs_open_context(calldata->ctx);
5789 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005790}
5791
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005792static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005793{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005794 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005795
Trond Myklebust14516c32010-07-31 14:29:06 -04005796 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5797 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005798 switch (task->tk_status) {
5799 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005800 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005801 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005802 if (nfs4_update_lock_stateid(calldata->lsp,
5803 &calldata->res.stateid))
5804 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005805 case -NFS4ERR_ADMIN_REVOKED:
5806 case -NFS4ERR_EXPIRED:
5807 nfs4_free_revoked_stateid(calldata->server,
5808 &calldata->arg.stateid,
5809 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005810 case -NFS4ERR_BAD_STATEID:
5811 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005812 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005813 if (!nfs4_stateid_match(&calldata->arg.stateid,
5814 &calldata->lsp->ls_stateid))
5815 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005816 break;
5817 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005818 if (nfs4_async_handle_error(task, calldata->server,
5819 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005820 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005821 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005822 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005823}
5824
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005825static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005826{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005827 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005829 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005830 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005831 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005832 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005833 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005834 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005835 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005836 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005837 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005838 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005839 &calldata->res.seq_res,
5840 task) != 0)
5841 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005842 return;
5843out_no_action:
5844 task->tk_action = NULL;
5845out_wait:
5846 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847}
5848
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005849static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005850 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005851 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005852 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005853};
5854
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005855static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5856 struct nfs_open_context *ctx,
5857 struct nfs4_lock_state *lsp,
5858 struct nfs_seqid *seqid)
5859{
5860 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005861 struct rpc_message msg = {
5862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5863 .rpc_cred = ctx->cred,
5864 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005865 struct rpc_task_setup task_setup_data = {
5866 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005867 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005868 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005869 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005870 .flags = RPC_TASK_ASYNC,
5871 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005872
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005873 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5874 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5875
Frank Filz137d6ac2007-07-09 15:32:29 -07005876 /* Ensure this is an unlock - when canceling a lock, the
5877 * canceled lock is passed in, and it won't be an unlock.
5878 */
5879 fl->fl_type = F_UNLCK;
5880
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005881 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5882 if (data == NULL) {
5883 nfs_free_seqid(seqid);
5884 return ERR_PTR(-ENOMEM);
5885 }
5886
Chuck Levera9c92d62013-08-09 12:48:18 -04005887 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005888 msg.rpc_argp = &data->arg;
5889 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005890 task_setup_data.callback_data = data;
5891 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005892}
5893
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5895{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005896 struct inode *inode = state->inode;
5897 struct nfs4_state_owner *sp = state->owner;
5898 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005899 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005900 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005901 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005902 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005903 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005904 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
Trond Myklebust9b073572006-06-29 16:38:34 -04005906 status = nfs4_set_lock_state(state, request);
5907 /* Unlock _before_ we do the RPC call */
5908 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005909 /* Exclude nfs_delegation_claim_locks() */
5910 mutex_lock(&sp->so_delegreturn_mutex);
5911 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005912 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005913 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005914 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005915 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005916 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005917 }
5918 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005919 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005920 if (status != 0)
5921 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005922 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005923 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005924 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5925 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005926 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5927 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005928 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005929 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005930 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005931 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005932 status = PTR_ERR(task);
5933 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005934 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005935 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005936 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005937out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005938 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005939 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005940 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941}
5942
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005943struct nfs4_lockdata {
5944 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005945 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005946 struct nfs4_lock_state *lsp;
5947 struct nfs_open_context *ctx;
5948 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005949 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005950 int rpc_status;
5951 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005952 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005953};
5954
5955static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005956 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5957 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005958{
5959 struct nfs4_lockdata *p;
5960 struct inode *inode = lsp->ls_state->inode;
5961 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005962 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005963
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005964 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005965 if (p == NULL)
5966 return NULL;
5967
5968 p->arg.fh = NFS_FH(inode);
5969 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005970 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005971 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005972 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005973 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5974 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005975 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005976 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005977 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005978 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005979 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005980 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005981 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005982 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005983 atomic_inc(&lsp->ls_count);
5984 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005985 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005986 memcpy(&p->fl, fl, sizeof(p->fl));
5987 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005988out_free_seqid:
5989 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005990out_free:
5991 kfree(p);
5992 return NULL;
5993}
5994
5995static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5996{
5997 struct nfs4_lockdata *data = calldata;
5998 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Harvey Harrison3110ff82008-05-02 13:42:44 -07006000 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006001 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006002 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006003 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006004 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006005 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006006 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006007 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006008 nfs4_stateid_copy(&data->arg.open_stateid,
6009 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006010 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006011 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006012 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006013 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006014 nfs4_stateid_copy(&data->arg.lock_stateid,
6015 &data->lsp->ls_stateid);
6016 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006017 if (!nfs4_valid_open_stateid(state)) {
6018 data->rpc_status = -EBADF;
6019 task->tk_action = NULL;
6020 goto out_release_open_seqid;
6021 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006022 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006023 if (nfs4_setup_sequence(data->server,
6024 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006025 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006026 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006027 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006028out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006029 nfs_release_seqid(data->arg.open_seqid);
6030out_release_lock_seqid:
6031 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006032out_wait:
6033 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006034 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006035}
6036
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006037static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6038{
6039 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006040 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006041
Harvey Harrison3110ff82008-05-02 13:42:44 -07006042 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006043
Trond Myklebust14516c32010-07-31 14:29:06 -04006044 if (!nfs4_sequence_done(task, &data->res.seq_res))
6045 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006046
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006047 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006048 switch (task->tk_status) {
6049 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006050 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006051 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006052 if (data->arg.new_lock) {
6053 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006054 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006055 rpc_restart_call_prepare(task);
6056 break;
6057 }
6058 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006059 if (data->arg.new_lock_owner != 0) {
6060 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6061 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6062 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6063 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6064 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006065 break;
6066 case -NFS4ERR_BAD_STATEID:
6067 case -NFS4ERR_OLD_STATEID:
6068 case -NFS4ERR_STALE_STATEID:
6069 case -NFS4ERR_EXPIRED:
6070 if (data->arg.new_lock_owner != 0) {
6071 if (!nfs4_stateid_match(&data->arg.open_stateid,
6072 &lsp->ls_state->open_stateid))
6073 rpc_restart_call_prepare(task);
6074 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6075 &lsp->ls_stateid))
6076 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006077 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006078 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006079}
6080
6081static void nfs4_lock_release(void *calldata)
6082{
6083 struct nfs4_lockdata *data = calldata;
6084
Harvey Harrison3110ff82008-05-02 13:42:44 -07006085 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006086 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087 if (data->cancelled != 0) {
6088 struct rpc_task *task;
6089 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6090 data->arg.lock_seqid);
6091 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006092 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006093 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094 } else
6095 nfs_free_seqid(data->arg.lock_seqid);
6096 nfs4_put_lock_state(data->lsp);
6097 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006098 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006099 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006100 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006101}
6102
6103static const struct rpc_call_ops nfs4_lock_ops = {
6104 .rpc_call_prepare = nfs4_lock_prepare,
6105 .rpc_call_done = nfs4_lock_done,
6106 .rpc_release = nfs4_lock_release,
6107};
6108
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006109static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6110{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006111 switch (error) {
6112 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006113 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006114 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006115 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006116 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006117 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006118 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006119 break;
6120 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006121 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006122 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006123 };
6124}
6125
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006126static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006127{
6128 struct nfs4_lockdata *data;
6129 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006130 struct rpc_message msg = {
6131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6132 .rpc_cred = state->owner->so_cred,
6133 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006134 struct rpc_task_setup task_setup_data = {
6135 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006136 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006137 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006138 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006139 .flags = RPC_TASK_ASYNC,
6140 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006141 int ret;
6142
Harvey Harrison3110ff82008-05-02 13:42:44 -07006143 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006144 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006145 fl->fl_u.nfs4_fl.owner,
6146 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006147 if (data == NULL)
6148 return -ENOMEM;
6149 if (IS_SETLKW(cmd))
6150 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006151 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006152 msg.rpc_argp = &data->arg;
6153 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006154 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006155 if (recovery_type > NFS_LOCK_NEW) {
6156 if (recovery_type == NFS_LOCK_RECLAIM)
6157 data->arg.reclaim = NFS_LOCK_RECLAIM;
6158 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006159 } else
6160 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006161 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006162 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006163 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006164 ret = nfs4_wait_for_completion_rpc_task(task);
6165 if (ret == 0) {
6166 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006167 if (ret)
6168 nfs4_handle_setlk_error(data->server, data->lsp,
6169 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006170 } else
6171 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006172 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006173 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006174 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006175 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176}
6177
6178static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6179{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006180 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006181 struct nfs4_exception exception = {
6182 .inode = state->inode,
6183 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006184 int err;
6185
6186 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006187 /* Cache the lock if possible... */
6188 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6189 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006190 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006191 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006192 break;
6193 nfs4_handle_exception(server, err, &exception);
6194 } while (exception.retry);
6195 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196}
6197
6198static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6199{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006200 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006201 struct nfs4_exception exception = {
6202 .inode = state->inode,
6203 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006204 int err;
6205
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006206 err = nfs4_set_lock_state(state, request);
6207 if (err != 0)
6208 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006209 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006210 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6211 return 0;
6212 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006213 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006214 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6215 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006216 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006217 switch (err) {
6218 default:
6219 goto out;
6220 case -NFS4ERR_GRACE:
6221 case -NFS4ERR_DELAY:
6222 nfs4_handle_exception(server, err, &exception);
6223 err = 0;
6224 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006225 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006226out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006227 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228}
6229
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006230#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006231static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6232{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006233 struct nfs4_lock_state *lsp;
6234 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006235
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006236 status = nfs4_set_lock_state(state, request);
6237 if (status != 0)
6238 return status;
6239 lsp = request->fl_u.nfs4_fl.owner;
6240 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6241 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6242 return 0;
6243 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006244 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006245}
6246#endif
6247
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6249{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006250 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006251 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006252 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006253 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
Trond Myklebust01c3b862006-06-29 16:38:39 -04006255 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006256 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006257 if (status < 0)
6258 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006259 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006260 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006261 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006262 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006263 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006264 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006265 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006266 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006267 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006268 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006269 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006270 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006271 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006272 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006273out:
6274 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 return status;
6276}
6277
6278static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6279{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006280 struct nfs4_exception exception = {
6281 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006282 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006283 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 int err;
6285
6286 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006287 err = _nfs4_proc_setlk(state, cmd, request);
6288 if (err == -NFS4ERR_DENIED)
6289 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006291 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 } while (exception.retry);
6293 return err;
6294}
6295
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006296#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6297#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6298
6299static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006300nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6301 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006302{
6303 int status = -ERESTARTSYS;
6304 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6305
6306 while(!signalled()) {
6307 status = nfs4_proc_setlk(state, cmd, request);
6308 if ((status != -EAGAIN) || IS_SETLK(cmd))
6309 break;
6310 freezable_schedule_timeout_interruptible(timeout);
6311 timeout *= 2;
6312 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6313 status = -ERESTARTSYS;
6314 }
6315 return status;
6316}
6317
Jeff Laytona1d617d82016-09-17 18:17:39 -04006318#ifdef CONFIG_NFS_V4_1
6319struct nfs4_lock_waiter {
6320 struct task_struct *task;
6321 struct inode *inode;
6322 struct nfs_lowner *owner;
6323 bool notified;
6324};
6325
6326static int
6327nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6328{
6329 int ret;
6330 struct cb_notify_lock_args *cbnl = key;
6331 struct nfs4_lock_waiter *waiter = wait->private;
6332 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6333 *wowner = waiter->owner;
6334
6335 /* Only wake if the callback was for the same owner */
6336 if (lowner->clientid != wowner->clientid ||
6337 lowner->id != wowner->id ||
6338 lowner->s_dev != wowner->s_dev)
6339 return 0;
6340
6341 /* Make sure it's for the right inode */
6342 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6343 return 0;
6344
6345 waiter->notified = true;
6346
6347 /* override "private" so we can use default_wake_function */
6348 wait->private = waiter->task;
6349 ret = autoremove_wake_function(wait, mode, flags, key);
6350 wait->private = waiter;
6351 return ret;
6352}
6353
6354static int
6355nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6356{
6357 int status = -ERESTARTSYS;
6358 unsigned long flags;
6359 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6360 struct nfs_server *server = NFS_SERVER(state->inode);
6361 struct nfs_client *clp = server->nfs_client;
6362 wait_queue_head_t *q = &clp->cl_lock_waitq;
6363 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6364 .id = lsp->ls_seqid.owner_id,
6365 .s_dev = server->s_dev };
6366 struct nfs4_lock_waiter waiter = { .task = current,
6367 .inode = state->inode,
6368 .owner = &owner,
6369 .notified = false };
6370 wait_queue_t wait;
6371
6372 /* Don't bother with waitqueue if we don't expect a callback */
6373 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6374 return nfs4_retry_setlk_simple(state, cmd, request);
6375
6376 init_wait(&wait);
6377 wait.private = &waiter;
6378 wait.func = nfs4_wake_lock_waiter;
6379 add_wait_queue(q, &wait);
6380
6381 while(!signalled()) {
6382 status = nfs4_proc_setlk(state, cmd, request);
6383 if ((status != -EAGAIN) || IS_SETLK(cmd))
6384 break;
6385
6386 status = -ERESTARTSYS;
6387 spin_lock_irqsave(&q->lock, flags);
6388 if (waiter.notified) {
6389 spin_unlock_irqrestore(&q->lock, flags);
6390 continue;
6391 }
6392 set_current_state(TASK_INTERRUPTIBLE);
6393 spin_unlock_irqrestore(&q->lock, flags);
6394
6395 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6396 }
6397
6398 finish_wait(q, &wait);
6399 return status;
6400}
6401#else /* !CONFIG_NFS_V4_1 */
6402static inline int
6403nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6404{
6405 return nfs4_retry_setlk_simple(state, cmd, request);
6406}
6407#endif
6408
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409static int
6410nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6411{
6412 struct nfs_open_context *ctx;
6413 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 int status;
6415
6416 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006417 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 state = ctx->state;
6419
6420 if (request->fl_start < 0 || request->fl_end < 0)
6421 return -EINVAL;
6422
Trond Myklebustd9531262009-07-21 19:22:38 -04006423 if (IS_GETLK(cmd)) {
6424 if (state != NULL)
6425 return nfs4_proc_getlk(state, F_GETLK, request);
6426 return 0;
6427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428
6429 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6430 return -EINVAL;
6431
Trond Myklebustd9531262009-07-21 19:22:38 -04006432 if (request->fl_type == F_UNLCK) {
6433 if (state != NULL)
6434 return nfs4_proc_unlck(state, cmd, request);
6435 return 0;
6436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437
Trond Myklebustd9531262009-07-21 19:22:38 -04006438 if (state == NULL)
6439 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006440
6441 if ((request->fl_flags & FL_POSIX) &&
6442 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6443 return -ENOLCK;
6444
Trond Myklebust55725512012-04-18 12:48:35 -04006445 /*
6446 * Don't rely on the VFS having checked the file open mode,
6447 * since it won't do this for flock() locks.
6448 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006449 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006450 case F_RDLCK:
6451 if (!(filp->f_mode & FMODE_READ))
6452 return -EBADF;
6453 break;
6454 case F_WRLCK:
6455 if (!(filp->f_mode & FMODE_WRITE))
6456 return -EBADF;
6457 }
6458
Jeff Layton1ea67db2016-09-17 18:17:37 -04006459 status = nfs4_set_lock_state(state, request);
6460 if (status != 0)
6461 return status;
6462
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006463 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464}
6465
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006466int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006467{
6468 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006469 int err;
6470
6471 err = nfs4_set_lock_state(state, fl);
6472 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006473 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006474 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006475 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006476}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006477
Trond Myklebustcf470c32012-03-07 13:49:12 -05006478struct nfs_release_lockowner_data {
6479 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006480 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006481 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006482 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006483 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006484};
6485
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006486static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6487{
6488 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006489 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006490 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6491 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006492 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006493 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006494}
6495
6496static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6497{
6498 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006499 struct nfs_server *server = data->server;
6500
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006501 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006502
6503 switch (task->tk_status) {
6504 case 0:
6505 renew_lease(server, data->timestamp);
6506 break;
6507 case -NFS4ERR_STALE_CLIENTID:
6508 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006509 nfs4_schedule_lease_recovery(server->nfs_client);
6510 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006511 case -NFS4ERR_LEASE_MOVED:
6512 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006513 if (nfs4_async_handle_error(task, server,
6514 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006515 rpc_restart_call_prepare(task);
6516 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006517}
6518
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006519static void nfs4_release_lockowner_release(void *calldata)
6520{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006521 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006522 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006523 kfree(calldata);
6524}
6525
Trond Myklebust17280172012-03-11 13:11:00 -04006526static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006527 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6528 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006529 .rpc_release = nfs4_release_lockowner_release,
6530};
6531
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006532static void
6533nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006534{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006535 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006536 struct rpc_message msg = {
6537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6538 };
6539
6540 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006541 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006542
Trond Myklebustcf470c32012-03-07 13:49:12 -05006543 data = kmalloc(sizeof(*data), GFP_NOFS);
6544 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006545 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006546 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006547 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006548 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6549 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6550 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006551
Trond Myklebustcf470c32012-03-07 13:49:12 -05006552 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006553 msg.rpc_resp = &data->res;
6554 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006555 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006556}
6557
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006558#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6559
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006560static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006561 struct dentry *unused, struct inode *inode,
6562 const char *key, const void *buf,
6563 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006564{
Al Viro59301222016-05-27 10:19:30 -04006565 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006566}
6567
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006568static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006569 struct dentry *unused, struct inode *inode,
6570 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006571{
Al Virob2968212016-04-10 20:48:24 -04006572 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006573}
6574
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006575static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006576{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006577 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006578}
6579
David Quigleyc9bccef2013-05-22 12:50:45 -04006580#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006581
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006582static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006583 struct dentry *unused, struct inode *inode,
6584 const char *key, const void *buf,
6585 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006586{
6587 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006588 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006589
6590 return -EOPNOTSUPP;
6591}
6592
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006593static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006594 struct dentry *unused, struct inode *inode,
6595 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006596{
6597 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006598 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006599 return -EOPNOTSUPP;
6600}
6601
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006602static ssize_t
6603nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006604{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006605 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006606
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006607 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6608 len = security_inode_listsecurity(inode, list, list_len);
6609 if (list_len && len > list_len)
6610 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006611 }
6612 return len;
6613}
6614
6615static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6616 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006617 .get = nfs4_xattr_get_nfs4_label,
6618 .set = nfs4_xattr_set_nfs4_label,
6619};
David Quigleyc9bccef2013-05-22 12:50:45 -04006620
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006621#else
6622
6623static ssize_t
6624nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6625{
6626 return 0;
6627}
6628
6629#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006630
Andy Adamson533eb462011-06-13 18:25:56 -04006631/*
6632 * nfs_fhget will use either the mounted_on_fileid or the fileid
6633 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006634static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6635{
Andy Adamson533eb462011-06-13 18:25:56 -04006636 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6637 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6638 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006639 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006640 return;
6641
6642 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006643 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006644 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6645 fattr->nlink = 2;
6646}
6647
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006648static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6649 const struct qstr *name,
6650 struct nfs4_fs_locations *fs_locations,
6651 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006652{
6653 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006654 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006655 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006656 };
6657 struct nfs4_fs_locations_arg args = {
6658 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006659 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006660 .page = page,
6661 .bitmask = bitmask,
6662 };
Benny Halevy22958462009-04-01 09:22:02 -04006663 struct nfs4_fs_locations_res res = {
6664 .fs_locations = fs_locations,
6665 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006666 struct rpc_message msg = {
6667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6668 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006669 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006670 };
6671 int status;
6672
Harvey Harrison3110ff82008-05-02 13:42:44 -07006673 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006674
6675 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6676 * is not supported */
6677 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6678 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6679 else
6680 bitmask[0] |= FATTR4_WORD0_FILEID;
6681
Trond Myklebustc228fd32007-01-13 02:28:11 -05006682 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006683 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006684 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006685 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006686 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006687 return status;
6688}
6689
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006690int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6691 const struct qstr *name,
6692 struct nfs4_fs_locations *fs_locations,
6693 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006694{
6695 struct nfs4_exception exception = { };
6696 int err;
6697 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006698 err = _nfs4_proc_fs_locations(client, dir, name,
6699 fs_locations, page);
6700 trace_nfs4_get_fs_locations(dir, name, err);
6701 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006702 &exception);
6703 } while (exception.retry);
6704 return err;
6705}
6706
Chuck Leverb03d7352013-10-17 14:12:50 -04006707/*
6708 * This operation also signals the server that this client is
6709 * performing migration recovery. The server can stop returning
6710 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6711 * appended to this compound to identify the client ID which is
6712 * performing recovery.
6713 */
6714static int _nfs40_proc_get_locations(struct inode *inode,
6715 struct nfs4_fs_locations *locations,
6716 struct page *page, struct rpc_cred *cred)
6717{
6718 struct nfs_server *server = NFS_SERVER(inode);
6719 struct rpc_clnt *clnt = server->client;
6720 u32 bitmask[2] = {
6721 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6722 };
6723 struct nfs4_fs_locations_arg args = {
6724 .clientid = server->nfs_client->cl_clientid,
6725 .fh = NFS_FH(inode),
6726 .page = page,
6727 .bitmask = bitmask,
6728 .migration = 1, /* skip LOOKUP */
6729 .renew = 1, /* append RENEW */
6730 };
6731 struct nfs4_fs_locations_res res = {
6732 .fs_locations = locations,
6733 .migration = 1,
6734 .renew = 1,
6735 };
6736 struct rpc_message msg = {
6737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6738 .rpc_argp = &args,
6739 .rpc_resp = &res,
6740 .rpc_cred = cred,
6741 };
6742 unsigned long now = jiffies;
6743 int status;
6744
6745 nfs_fattr_init(&locations->fattr);
6746 locations->server = server;
6747 locations->nlocations = 0;
6748
6749 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6750 nfs4_set_sequence_privileged(&args.seq_args);
6751 status = nfs4_call_sync_sequence(clnt, server, &msg,
6752 &args.seq_args, &res.seq_res);
6753 if (status)
6754 return status;
6755
6756 renew_lease(server, now);
6757 return 0;
6758}
6759
6760#ifdef CONFIG_NFS_V4_1
6761
6762/*
6763 * This operation also signals the server that this client is
6764 * performing migration recovery. The server can stop asserting
6765 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6766 * performing this operation is identified in the SEQUENCE
6767 * operation in this compound.
6768 *
6769 * When the client supports GETATTR(fs_locations_info), it can
6770 * be plumbed in here.
6771 */
6772static int _nfs41_proc_get_locations(struct inode *inode,
6773 struct nfs4_fs_locations *locations,
6774 struct page *page, struct rpc_cred *cred)
6775{
6776 struct nfs_server *server = NFS_SERVER(inode);
6777 struct rpc_clnt *clnt = server->client;
6778 u32 bitmask[2] = {
6779 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6780 };
6781 struct nfs4_fs_locations_arg args = {
6782 .fh = NFS_FH(inode),
6783 .page = page,
6784 .bitmask = bitmask,
6785 .migration = 1, /* skip LOOKUP */
6786 };
6787 struct nfs4_fs_locations_res res = {
6788 .fs_locations = locations,
6789 .migration = 1,
6790 };
6791 struct rpc_message msg = {
6792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6793 .rpc_argp = &args,
6794 .rpc_resp = &res,
6795 .rpc_cred = cred,
6796 };
6797 int status;
6798
6799 nfs_fattr_init(&locations->fattr);
6800 locations->server = server;
6801 locations->nlocations = 0;
6802
6803 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6804 nfs4_set_sequence_privileged(&args.seq_args);
6805 status = nfs4_call_sync_sequence(clnt, server, &msg,
6806 &args.seq_args, &res.seq_res);
6807 if (status == NFS4_OK &&
6808 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6809 status = -NFS4ERR_LEASE_MOVED;
6810 return status;
6811}
6812
6813#endif /* CONFIG_NFS_V4_1 */
6814
6815/**
6816 * nfs4_proc_get_locations - discover locations for a migrated FSID
6817 * @inode: inode on FSID that is migrating
6818 * @locations: result of query
6819 * @page: buffer
6820 * @cred: credential to use for this operation
6821 *
6822 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6823 * operation failed, or a negative errno if a local error occurred.
6824 *
6825 * On success, "locations" is filled in, but if the server has
6826 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6827 * asserted.
6828 *
6829 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6830 * from this client that require migration recovery.
6831 */
6832int nfs4_proc_get_locations(struct inode *inode,
6833 struct nfs4_fs_locations *locations,
6834 struct page *page, struct rpc_cred *cred)
6835{
6836 struct nfs_server *server = NFS_SERVER(inode);
6837 struct nfs_client *clp = server->nfs_client;
6838 const struct nfs4_mig_recovery_ops *ops =
6839 clp->cl_mvops->mig_recovery_ops;
6840 struct nfs4_exception exception = { };
6841 int status;
6842
6843 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6844 (unsigned long long)server->fsid.major,
6845 (unsigned long long)server->fsid.minor,
6846 clp->cl_hostname);
6847 nfs_display_fhandle(NFS_FH(inode), __func__);
6848
6849 do {
6850 status = ops->get_locations(inode, locations, page, cred);
6851 if (status != -NFS4ERR_DELAY)
6852 break;
6853 nfs4_handle_exception(server, status, &exception);
6854 } while (exception.retry);
6855 return status;
6856}
6857
Chuck Lever44c99932013-10-17 14:13:30 -04006858/*
6859 * This operation also signals the server that this client is
6860 * performing "lease moved" recovery. The server can stop
6861 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6862 * is appended to this compound to identify the client ID which is
6863 * performing recovery.
6864 */
6865static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6866{
6867 struct nfs_server *server = NFS_SERVER(inode);
6868 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6869 struct rpc_clnt *clnt = server->client;
6870 struct nfs4_fsid_present_arg args = {
6871 .fh = NFS_FH(inode),
6872 .clientid = clp->cl_clientid,
6873 .renew = 1, /* append RENEW */
6874 };
6875 struct nfs4_fsid_present_res res = {
6876 .renew = 1,
6877 };
6878 struct rpc_message msg = {
6879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6880 .rpc_argp = &args,
6881 .rpc_resp = &res,
6882 .rpc_cred = cred,
6883 };
6884 unsigned long now = jiffies;
6885 int status;
6886
6887 res.fh = nfs_alloc_fhandle();
6888 if (res.fh == NULL)
6889 return -ENOMEM;
6890
6891 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6892 nfs4_set_sequence_privileged(&args.seq_args);
6893 status = nfs4_call_sync_sequence(clnt, server, &msg,
6894 &args.seq_args, &res.seq_res);
6895 nfs_free_fhandle(res.fh);
6896 if (status)
6897 return status;
6898
6899 do_renew_lease(clp, now);
6900 return 0;
6901}
6902
6903#ifdef CONFIG_NFS_V4_1
6904
6905/*
6906 * This operation also signals the server that this client is
6907 * performing "lease moved" recovery. The server can stop asserting
6908 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6909 * this operation is identified in the SEQUENCE operation in this
6910 * compound.
6911 */
6912static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6913{
6914 struct nfs_server *server = NFS_SERVER(inode);
6915 struct rpc_clnt *clnt = server->client;
6916 struct nfs4_fsid_present_arg args = {
6917 .fh = NFS_FH(inode),
6918 };
6919 struct nfs4_fsid_present_res res = {
6920 };
6921 struct rpc_message msg = {
6922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6923 .rpc_argp = &args,
6924 .rpc_resp = &res,
6925 .rpc_cred = cred,
6926 };
6927 int status;
6928
6929 res.fh = nfs_alloc_fhandle();
6930 if (res.fh == NULL)
6931 return -ENOMEM;
6932
6933 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6934 nfs4_set_sequence_privileged(&args.seq_args);
6935 status = nfs4_call_sync_sequence(clnt, server, &msg,
6936 &args.seq_args, &res.seq_res);
6937 nfs_free_fhandle(res.fh);
6938 if (status == NFS4_OK &&
6939 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6940 status = -NFS4ERR_LEASE_MOVED;
6941 return status;
6942}
6943
6944#endif /* CONFIG_NFS_V4_1 */
6945
6946/**
6947 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6948 * @inode: inode on FSID to check
6949 * @cred: credential to use for this operation
6950 *
6951 * Server indicates whether the FSID is present, moved, or not
6952 * recognized. This operation is necessary to clear a LEASE_MOVED
6953 * condition for this client ID.
6954 *
6955 * Returns NFS4_OK if the FSID is present on this server,
6956 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6957 * NFS4ERR code if some error occurred on the server, or a
6958 * negative errno if a local failure occurred.
6959 */
6960int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6961{
6962 struct nfs_server *server = NFS_SERVER(inode);
6963 struct nfs_client *clp = server->nfs_client;
6964 const struct nfs4_mig_recovery_ops *ops =
6965 clp->cl_mvops->mig_recovery_ops;
6966 struct nfs4_exception exception = { };
6967 int status;
6968
6969 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6970 (unsigned long long)server->fsid.major,
6971 (unsigned long long)server->fsid.minor,
6972 clp->cl_hostname);
6973 nfs_display_fhandle(NFS_FH(inode), __func__);
6974
6975 do {
6976 status = ops->fsid_present(inode, cred);
6977 if (status != -NFS4ERR_DELAY)
6978 break;
6979 nfs4_handle_exception(server, status, &exception);
6980 } while (exception.retry);
6981 return status;
6982}
6983
Andy Adamson5ec16a82013-08-08 10:57:55 -04006984/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006985 * If 'use_integrity' is true and the state managment nfs_client
6986 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6987 * and the machine credential as per RFC3530bis and RFC5661 Security
6988 * Considerations sections. Otherwise, just use the user cred with the
6989 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006990 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006991static 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 +00006992{
6993 int status;
6994 struct nfs4_secinfo_arg args = {
6995 .dir_fh = NFS_FH(dir),
6996 .name = name,
6997 };
6998 struct nfs4_secinfo_res res = {
6999 .flavors = flavors,
7000 };
7001 struct rpc_message msg = {
7002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7003 .rpc_argp = &args,
7004 .rpc_resp = &res,
7005 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007006 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007007 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007008
7009 if (use_integrity) {
7010 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007011 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7012 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007013 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007014
7015 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007016
7017 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7018 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7019
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007020 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7021 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007022 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007023
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007024 if (cred)
7025 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007026
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007027 return status;
7028}
7029
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007030int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7031 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007032{
7033 struct nfs4_exception exception = { };
7034 int err;
7035 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007036 err = -NFS4ERR_WRONGSEC;
7037
7038 /* try to use integrity protection with machine cred */
7039 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7040 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7041
7042 /*
7043 * if unable to use integrity protection, or SECINFO with
7044 * integrity protection returns NFS4ERR_WRONGSEC (which is
7045 * disallowed by spec, but exists in deployed servers) use
7046 * the current filesystem's rpc_client and the user cred.
7047 */
7048 if (err == -NFS4ERR_WRONGSEC)
7049 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7050
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007051 trace_nfs4_secinfo(dir, name, err);
7052 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007053 &exception);
7054 } while (exception.retry);
7055 return err;
7056}
7057
Andy Adamson557134a2009-04-01 09:21:53 -04007058#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007059/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007060 * Check the exchange flags returned by the server for invalid flags, having
7061 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7062 * DS flags set.
7063 */
7064static int nfs4_check_cl_exchange_flags(u32 flags)
7065{
7066 if (flags & ~EXCHGID4_FLAG_MASK_R)
7067 goto out_inval;
7068 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7069 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7070 goto out_inval;
7071 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7072 goto out_inval;
7073 return NFS_OK;
7074out_inval:
7075 return -NFS4ERR_INVAL;
7076}
7077
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007078static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007079nfs41_same_server_scope(struct nfs41_server_scope *a,
7080 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007081{
7082 if (a->server_scope_sz == b->server_scope_sz &&
7083 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7084 return true;
7085
7086 return false;
7087}
7088
Andy Adamson02a95de2016-02-05 16:08:37 -05007089static void
7090nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7091{
7092}
7093
7094static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7095 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7096};
7097
Andy Adamson357f54d2010-12-14 10:11:57 -05007098/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007099 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007100 *
7101 * The 4.1 client currently uses the same TCP connection for the
7102 * fore and backchannel.
7103 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007104static
7105int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7106 struct rpc_xprt *xprt,
7107 struct nfs_client *clp,
7108 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007109{
7110 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007111 struct nfs41_bind_conn_to_session_args args = {
7112 .client = clp,
7113 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7114 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007115 struct nfs41_bind_conn_to_session_res res;
7116 struct rpc_message msg = {
7117 .rpc_proc =
7118 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007119 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007120 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007121 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007122 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007123 struct rpc_task_setup task_setup_data = {
7124 .rpc_client = clnt,
7125 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007126 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007127 .rpc_message = &msg,
7128 .flags = RPC_TASK_TIMEOUT,
7129 };
7130 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007131
7132 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007133
Trond Myklebust71a097c2015-02-18 09:27:18 -08007134 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7135 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7136 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007137
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007138 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7139 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7140 args.dir = NFS4_CDFC4_FORE;
7141
7142 task = rpc_run_task(&task_setup_data);
7143 if (!IS_ERR(task)) {
7144 status = task->tk_status;
7145 rpc_put_task(task);
7146 } else
7147 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007148 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007149 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007150 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007151 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7152 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7153 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007154 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007155 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007156 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007157 dprintk("NFS: %s: Unexpected direction from server\n",
7158 __func__);
7159 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007160 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007161 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007162 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007163 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7164 __func__);
7165 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007166 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007167 }
7168 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007169out:
7170 dprintk("<-- %s status= %d\n", __func__, status);
7171 return status;
7172}
7173
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007174struct rpc_bind_conn_calldata {
7175 struct nfs_client *clp;
7176 struct rpc_cred *cred;
7177};
7178
7179static int
7180nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7181 struct rpc_xprt *xprt,
7182 void *calldata)
7183{
7184 struct rpc_bind_conn_calldata *p = calldata;
7185
7186 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7187}
7188
7189int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7190{
7191 struct rpc_bind_conn_calldata data = {
7192 .clp = clp,
7193 .cred = cred,
7194 };
7195 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7196 nfs4_proc_bind_conn_to_session_callback, &data);
7197}
7198
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007199/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007200 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7201 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007202 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007203static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7204 .how = SP4_MACH_CRED,
7205 .enforce.u.words = {
7206 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7207 1 << (OP_EXCHANGE_ID - 32) |
7208 1 << (OP_CREATE_SESSION - 32) |
7209 1 << (OP_DESTROY_SESSION - 32) |
7210 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007211 },
7212 .allow.u.words = {
7213 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007214 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007215 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007216 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007217 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007218 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007219 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007220 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007221 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007222 1 << (OP_FREE_STATEID - 32) |
7223 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007224 }
7225};
7226
7227/*
7228 * Select the state protection mode for client `clp' given the server results
7229 * from exchange_id in `sp'.
7230 *
7231 * Returns 0 on success, negative errno otherwise.
7232 */
7233static int nfs4_sp4_select_mode(struct nfs_client *clp,
7234 struct nfs41_state_protection *sp)
7235{
7236 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7237 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7238 1 << (OP_EXCHANGE_ID - 32) |
7239 1 << (OP_CREATE_SESSION - 32) |
7240 1 << (OP_DESTROY_SESSION - 32) |
7241 1 << (OP_DESTROY_CLIENTID - 32)
7242 };
7243 unsigned int i;
7244
7245 if (sp->how == SP4_MACH_CRED) {
7246 /* Print state protect result */
7247 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7248 for (i = 0; i <= LAST_NFS4_OP; i++) {
7249 if (test_bit(i, sp->enforce.u.longs))
7250 dfprintk(MOUNT, " enforce op %d\n", i);
7251 if (test_bit(i, sp->allow.u.longs))
7252 dfprintk(MOUNT, " allow op %d\n", i);
7253 }
7254
7255 /* make sure nothing is on enforce list that isn't supported */
7256 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7257 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7258 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7259 return -EINVAL;
7260 }
7261 }
7262
7263 /*
7264 * Minimal mode - state operations are allowed to use machine
7265 * credential. Note this already happens by default, so the
7266 * client doesn't have to do anything more than the negotiation.
7267 *
7268 * NOTE: we don't care if EXCHANGE_ID is in the list -
7269 * we're already using the machine cred for exchange_id
7270 * and will never use a different cred.
7271 */
7272 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7273 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7274 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7275 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7276 dfprintk(MOUNT, "sp4_mach_cred:\n");
7277 dfprintk(MOUNT, " minimal mode enabled\n");
7278 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7279 } else {
7280 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7281 return -EINVAL;
7282 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007283
7284 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007285 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7286 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007287 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7288 dfprintk(MOUNT, " cleanup mode enabled\n");
7289 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7290 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007291
Andrew Elble99ade3c2015-12-02 09:39:51 -05007292 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7293 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7294 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7295 &clp->cl_sp4_flags);
7296 }
7297
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007298 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7299 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7300 dfprintk(MOUNT, " secinfo mode enabled\n");
7301 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7302 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007303
7304 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7305 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7306 dfprintk(MOUNT, " stateid mode enabled\n");
7307 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7308 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007309
7310 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7311 dfprintk(MOUNT, " write mode enabled\n");
7312 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7313 }
7314
7315 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7316 dfprintk(MOUNT, " commit mode enabled\n");
7317 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7318 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007319 }
7320
7321 return 0;
7322}
7323
Andy Adamson8d89bd72016-09-09 09:22:18 -04007324struct nfs41_exchange_id_data {
7325 struct nfs41_exchange_id_res res;
7326 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007327 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007328 int rpc_status;
7329};
7330
7331static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7332{
7333 struct nfs41_exchange_id_data *cdata =
7334 (struct nfs41_exchange_id_data *)data;
7335 struct nfs_client *clp = cdata->args.client;
7336 int status = task->tk_status;
7337
7338 trace_nfs4_exchange_id(clp, status);
7339
7340 if (status == 0)
7341 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007342
7343 if (cdata->xprt && status == 0) {
7344 status = nfs4_detect_session_trunking(clp, &cdata->res,
7345 cdata->xprt);
7346 goto out;
7347 }
7348
Andy Adamson8d89bd72016-09-09 09:22:18 -04007349 if (status == 0)
7350 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7351
7352 if (status == 0) {
7353 clp->cl_clientid = cdata->res.clientid;
7354 clp->cl_exchange_flags = cdata->res.flags;
7355 /* Client ID is not confirmed */
7356 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7357 clear_bit(NFS4_SESSION_ESTABLISHED,
7358 &clp->cl_session->session_state);
7359 clp->cl_seqid = cdata->res.seqid;
7360 }
7361
7362 kfree(clp->cl_serverowner);
7363 clp->cl_serverowner = cdata->res.server_owner;
7364 cdata->res.server_owner = NULL;
7365
7366 /* use the most recent implementation id */
7367 kfree(clp->cl_implid);
7368 clp->cl_implid = cdata->res.impl_id;
7369 cdata->res.impl_id = NULL;
7370
7371 if (clp->cl_serverscope != NULL &&
7372 !nfs41_same_server_scope(clp->cl_serverscope,
7373 cdata->res.server_scope)) {
7374 dprintk("%s: server_scope mismatch detected\n",
7375 __func__);
7376 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7377 kfree(clp->cl_serverscope);
7378 clp->cl_serverscope = NULL;
7379 }
7380
7381 if (clp->cl_serverscope == NULL) {
7382 clp->cl_serverscope = cdata->res.server_scope;
7383 cdata->res.server_scope = NULL;
7384 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007385 /* Save the EXCHANGE_ID verifier session trunk tests */
7386 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7387 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007388 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007389out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007390 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007391 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007392}
7393
7394static void nfs4_exchange_id_release(void *data)
7395{
7396 struct nfs41_exchange_id_data *cdata =
7397 (struct nfs41_exchange_id_data *)data;
7398
7399 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007400 if (cdata->xprt) {
7401 xprt_put(cdata->xprt);
7402 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7403 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007404 kfree(cdata->res.impl_id);
7405 kfree(cdata->res.server_scope);
7406 kfree(cdata->res.server_owner);
7407 kfree(cdata);
7408}
7409
7410static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7411 .rpc_call_done = nfs4_exchange_id_done,
7412 .rpc_release = nfs4_exchange_id_release,
7413};
7414
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007415/*
7416 * _nfs4_proc_exchange_id()
7417 *
7418 * Wrapper for EXCHANGE_ID operation.
7419 */
7420static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007421 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007422{
7423 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007424 struct rpc_message msg = {
7425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007426 .rpc_cred = cred,
7427 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007428 struct rpc_task_setup task_setup_data = {
7429 .rpc_client = clp->cl_rpcclient,
7430 .callback_ops = &nfs4_exchange_id_call_ops,
7431 .rpc_message = &msg,
7432 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7433 };
7434 struct nfs41_exchange_id_data *calldata;
7435 struct rpc_task *task;
7436 int status = -EIO;
7437
7438 if (!atomic_inc_not_zero(&clp->cl_count))
7439 goto out;
7440
7441 status = -ENOMEM;
7442 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7443 if (!calldata)
7444 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007445
Andy Adamsonad0849a2016-09-09 09:22:28 -04007446 if (!xprt)
7447 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007448
7449 status = nfs4_init_uniform_client_string(clp);
7450 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007451 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007452
7453 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007454 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007455 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007456
Andy Adamson8d89bd72016-09-09 09:22:18 -04007457 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7458 GFP_NOFS);
7459 status = -ENOMEM;
7460 if (unlikely(calldata->res.server_owner == NULL))
7461 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007462
Andy Adamson8d89bd72016-09-09 09:22:18 -04007463 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007464 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007465 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007466 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007467
Andy Adamson8d89bd72016-09-09 09:22:18 -04007468 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7469 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007470 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007471
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007472 switch (sp4_how) {
7473 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007474 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007475 break;
7476
7477 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007478 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007479 break;
7480
7481 default:
7482 /* unsupported! */
7483 WARN_ON_ONCE(1);
7484 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007485 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007486 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007487 if (xprt) {
7488 calldata->xprt = xprt;
7489 task_setup_data.rpc_xprt = xprt;
7490 task_setup_data.flags =
7491 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7492 calldata->args.verifier = &clp->cl_confirm;
7493 } else {
7494 calldata->args.verifier = &verifier;
7495 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007496 calldata->args.client = clp;
7497#ifdef CONFIG_NFS_V4_1_MIGRATION
7498 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7499 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7500 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7501#else
7502 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7503 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7504#endif
7505 msg.rpc_argp = &calldata->args;
7506 msg.rpc_resp = &calldata->res;
7507 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007508
Andy Adamson8d89bd72016-09-09 09:22:18 -04007509 task = rpc_run_task(&task_setup_data);
7510 if (IS_ERR(task)) {
7511 status = PTR_ERR(task);
7512 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007513 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007514
Andy Adamsonad0849a2016-09-09 09:22:28 -04007515 if (!xprt) {
7516 status = rpc_wait_for_completion_task(task);
7517 if (!status)
7518 status = calldata->rpc_status;
7519 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007520 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007521
Andy Adamson8d89bd72016-09-09 09:22:18 -04007522 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007523out:
Chuck Lever177313f2012-05-21 22:44:58 -04007524 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007525 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007526 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007527 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007528 clp->cl_implid->date.seconds,
7529 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007530 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007531 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007532
7533out_impl_id:
7534 kfree(calldata->res.impl_id);
7535out_server_scope:
7536 kfree(calldata->res.server_scope);
7537out_server_owner:
7538 kfree(calldata->res.server_owner);
7539out_calldata:
7540 kfree(calldata);
7541 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007542}
7543
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007544/*
7545 * nfs4_proc_exchange_id()
7546 *
7547 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7548 *
7549 * Since the clientid has expired, all compounds using sessions
7550 * associated with the stale clientid will be returning
7551 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7552 * be in some phase of session reset.
7553 *
7554 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7555 */
7556int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7557{
7558 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7559 int status;
7560
7561 /* try SP4_MACH_CRED if krb5i/p */
7562 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7563 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007564 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007565 if (!status)
7566 return 0;
7567 }
7568
7569 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007570 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007571}
7572
Andy Adamson04fa2c62016-09-09 09:22:29 -04007573/**
7574 * nfs4_test_session_trunk
7575 *
7576 * This is an add_xprt_test() test function called from
7577 * rpc_clnt_setup_test_and_add_xprt.
7578 *
7579 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7580 * and is dereferrenced in nfs4_exchange_id_release
7581 *
7582 * Upon success, add the new transport to the rpc_clnt
7583 *
7584 * @clnt: struct rpc_clnt to get new transport
7585 * @xprt: the rpc_xprt to test
7586 * @data: call data for _nfs4_proc_exchange_id.
7587 */
7588int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7589 void *data)
7590{
7591 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7592 u32 sp4_how;
7593
7594 dprintk("--> %s try %s\n", __func__,
7595 xprt->address_strings[RPC_DISPLAY_ADDR]);
7596
7597 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7598
7599 /* Test connection for session trunking. Async exchange_id call */
7600 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7601}
7602EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7603
Trond Myklebust66245532012-05-25 17:18:09 -04007604static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7605 struct rpc_cred *cred)
7606{
7607 struct rpc_message msg = {
7608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7609 .rpc_argp = clp,
7610 .rpc_cred = cred,
7611 };
7612 int status;
7613
7614 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007615 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007616 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007617 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007618 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7619 return status;
7620}
7621
7622static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7623 struct rpc_cred *cred)
7624{
7625 unsigned int loop;
7626 int ret;
7627
7628 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7629 ret = _nfs4_proc_destroy_clientid(clp, cred);
7630 switch (ret) {
7631 case -NFS4ERR_DELAY:
7632 case -NFS4ERR_CLIENTID_BUSY:
7633 ssleep(1);
7634 break;
7635 default:
7636 return ret;
7637 }
7638 }
7639 return 0;
7640}
7641
7642int nfs4_destroy_clientid(struct nfs_client *clp)
7643{
7644 struct rpc_cred *cred;
7645 int ret = 0;
7646
7647 if (clp->cl_mvops->minor_version < 1)
7648 goto out;
7649 if (clp->cl_exchange_flags == 0)
7650 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007651 if (clp->cl_preserve_clid)
7652 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007653 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007654 ret = nfs4_proc_destroy_clientid(clp, cred);
7655 if (cred)
7656 put_rpccred(cred);
7657 switch (ret) {
7658 case 0:
7659 case -NFS4ERR_STALE_CLIENTID:
7660 clp->cl_exchange_flags = 0;
7661 }
7662out:
7663 return ret;
7664}
7665
Andy Adamson2050f0c2009-04-01 09:22:30 -04007666struct nfs4_get_lease_time_data {
7667 struct nfs4_get_lease_time_args *args;
7668 struct nfs4_get_lease_time_res *res;
7669 struct nfs_client *clp;
7670};
7671
7672static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7673 void *calldata)
7674{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007675 struct nfs4_get_lease_time_data *data =
7676 (struct nfs4_get_lease_time_data *)calldata;
7677
7678 dprintk("--> %s\n", __func__);
7679 /* just setup sequence, do not trigger session recovery
7680 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007681 nfs41_setup_sequence(data->clp->cl_session,
7682 &data->args->la_seq_args,
7683 &data->res->lr_seq_res,
7684 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007685 dprintk("<-- %s\n", __func__);
7686}
7687
7688/*
7689 * Called from nfs4_state_manager thread for session setup, so don't recover
7690 * from sequence operation or clientid errors.
7691 */
7692static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7693{
7694 struct nfs4_get_lease_time_data *data =
7695 (struct nfs4_get_lease_time_data *)calldata;
7696
7697 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007698 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7699 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007700 switch (task->tk_status) {
7701 case -NFS4ERR_DELAY:
7702 case -NFS4ERR_GRACE:
7703 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7704 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7705 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007706 /* fall through */
7707 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007708 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007709 return;
7710 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007711 dprintk("<-- %s\n", __func__);
7712}
7713
Trond Myklebust17280172012-03-11 13:11:00 -04007714static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007715 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7716 .rpc_call_done = nfs4_get_lease_time_done,
7717};
7718
7719int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7720{
7721 struct rpc_task *task;
7722 struct nfs4_get_lease_time_args args;
7723 struct nfs4_get_lease_time_res res = {
7724 .lr_fsinfo = fsinfo,
7725 };
7726 struct nfs4_get_lease_time_data data = {
7727 .args = &args,
7728 .res = &res,
7729 .clp = clp,
7730 };
7731 struct rpc_message msg = {
7732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7733 .rpc_argp = &args,
7734 .rpc_resp = &res,
7735 };
7736 struct rpc_task_setup task_setup = {
7737 .rpc_client = clp->cl_rpcclient,
7738 .rpc_message = &msg,
7739 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007740 .callback_data = &data,
7741 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007742 };
7743 int status;
7744
Chuck Levera9c92d62013-08-09 12:48:18 -04007745 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007746 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007747 dprintk("--> %s\n", __func__);
7748 task = rpc_run_task(&task_setup);
7749
7750 if (IS_ERR(task))
7751 status = PTR_ERR(task);
7752 else {
7753 status = task->tk_status;
7754 rpc_put_task(task);
7755 }
7756 dprintk("<-- %s return %d\n", __func__, status);
7757
7758 return status;
7759}
7760
Andy Adamsonfc931582009-04-01 09:22:31 -04007761/*
7762 * Initialize the values to be used by the client in CREATE_SESSION
7763 * If nfs4_init_session set the fore channel request and response sizes,
7764 * use them.
7765 *
7766 * Set the back channel max_resp_sz_cached to zero to force the client to
7767 * always set csa_cachethis to FALSE because the current implementation
7768 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7769 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007770static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7771 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007772{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007773 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007774 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007775
Andy Adamson18aad3d2013-06-26 12:21:49 -04007776 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7777 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7778
Andy Adamsonfc931582009-04-01 09:22:31 -04007779 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007780 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7781 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007782 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007783 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007784
7785 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007786 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007787 __func__,
7788 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007789 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007790
7791 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007792 args->bc_attrs.max_rqst_sz = max_bc_payload;
7793 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007794 args->bc_attrs.max_resp_sz_cached = 0;
7795 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007796 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007797
7798 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7799 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7800 __func__,
7801 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7802 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7803 args->bc_attrs.max_reqs);
7804}
7805
Trond Myklebust79969dd2015-02-18 11:30:18 -08007806static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7807 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007808{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007809 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007810 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007811
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007812 if (rcvd->max_resp_sz > sent->max_resp_sz)
7813 return -EINVAL;
7814 /*
7815 * Our requested max_ops is the minimum we need; we're not
7816 * prepared to break up compounds into smaller pieces than that.
7817 * So, no point even trying to continue if the server won't
7818 * cooperate:
7819 */
7820 if (rcvd->max_ops < sent->max_ops)
7821 return -EINVAL;
7822 if (rcvd->max_reqs == 0)
7823 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007824 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7825 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007826 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007827}
7828
Trond Myklebust79969dd2015-02-18 11:30:18 -08007829static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7830 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007831{
7832 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007833 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007834
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007835 if (!(res->flags & SESSION4_BACK_CHAN))
7836 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007837 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7838 return -EINVAL;
7839 if (rcvd->max_resp_sz < sent->max_resp_sz)
7840 return -EINVAL;
7841 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7842 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007843 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007844 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007845 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007846 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007847out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007848 return 0;
7849}
Andy Adamson8d353012009-04-01 09:22:32 -04007850
Andy Adamson8d353012009-04-01 09:22:32 -04007851static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007852 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007853{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007854 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007855
Trond Myklebust79969dd2015-02-18 11:30:18 -08007856 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007857 if (ret)
7858 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007859 return nfs4_verify_back_channel_attrs(args, res);
7860}
7861
7862static void nfs4_update_session(struct nfs4_session *session,
7863 struct nfs41_create_session_res *res)
7864{
7865 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007866 /* Mark client id and session as being confirmed */
7867 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7868 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007869 session->flags = res->flags;
7870 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007871 if (res->flags & SESSION4_BACK_CHAN)
7872 memcpy(&session->bc_attrs, &res->bc_attrs,
7873 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007874}
7875
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007876static int _nfs4_proc_create_session(struct nfs_client *clp,
7877 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007878{
7879 struct nfs4_session *session = clp->cl_session;
7880 struct nfs41_create_session_args args = {
7881 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007882 .clientid = clp->cl_clientid,
7883 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007884 .cb_program = NFS4_CALLBACK,
7885 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007886 struct nfs41_create_session_res res;
7887
Andy Adamsonfc931582009-04-01 09:22:31 -04007888 struct rpc_message msg = {
7889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7890 .rpc_argp = &args,
7891 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007892 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007893 };
7894 int status;
7895
Chuck Lever6b26cc82016-05-02 14:40:40 -04007896 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007897 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007898
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007899 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007900 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007901
Trond Myklebustb519d402016-09-11 14:50:01 -04007902 switch (status) {
7903 case -NFS4ERR_STALE_CLIENTID:
7904 case -NFS4ERR_DELAY:
7905 case -ETIMEDOUT:
7906 case -EACCES:
7907 case -EAGAIN:
7908 goto out;
7909 };
7910
7911 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007912 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007913 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007914 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007915 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007916 if (status)
7917 goto out;
7918 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007919 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007920out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007921 return status;
7922}
7923
7924/*
7925 * Issues a CREATE_SESSION operation to the server.
7926 * It is the responsibility of the caller to verify the session is
7927 * expired before calling this routine.
7928 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007929int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007930{
7931 int status;
7932 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007933 struct nfs4_session *session = clp->cl_session;
7934
7935 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7936
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007937 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007938 if (status)
7939 goto out;
7940
Andy Adamsonaacd5532011-11-09 13:58:21 -05007941 /* Init or reset the session slot tables */
7942 status = nfs4_setup_session_slot_tables(session);
7943 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007944 if (status)
7945 goto out;
7946
7947 ptr = (unsigned *)&session->sess_id.data[0];
7948 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7949 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007950out:
7951 dprintk("<-- %s\n", __func__);
7952 return status;
7953}
7954
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007955/*
7956 * Issue the over-the-wire RPC DESTROY_SESSION.
7957 * The caller must serialize access to this routine.
7958 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007959int nfs4_proc_destroy_session(struct nfs4_session *session,
7960 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007961{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007962 struct rpc_message msg = {
7963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7964 .rpc_argp = session,
7965 .rpc_cred = cred,
7966 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007967 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007968
7969 dprintk("--> nfs4_proc_destroy_session\n");
7970
7971 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007972 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7973 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007974
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007975 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007976 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007977
7978 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007979 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007980 "Session has been destroyed regardless...\n", status);
7981
7982 dprintk("<-- nfs4_proc_destroy_session\n");
7983 return status;
7984}
7985
Trond Myklebust7b38c362012-05-23 13:23:31 -04007986/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007987 * Renew the cl_session lease.
7988 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007989struct nfs4_sequence_data {
7990 struct nfs_client *clp;
7991 struct nfs4_sequence_args args;
7992 struct nfs4_sequence_res res;
7993};
7994
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007995static void nfs41_sequence_release(void *data)
7996{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007997 struct nfs4_sequence_data *calldata = data;
7998 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007999
Alexandros Batsakis71358402010-02-05 03:45:05 -08008000 if (atomic_read(&clp->cl_count) > 1)
8001 nfs4_schedule_state_renewal(clp);
8002 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008003 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008004}
8005
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008006static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8007{
8008 switch(task->tk_status) {
8009 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008010 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8011 return -EAGAIN;
8012 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008013 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008014 }
8015 return 0;
8016}
8017
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008018static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008019{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008020 struct nfs4_sequence_data *calldata = data;
8021 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008022
Trond Myklebust14516c32010-07-31 14:29:06 -04008023 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8024 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008025
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008026 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008027 if (task->tk_status < 0) {
8028 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008029 if (atomic_read(&clp->cl_count) == 1)
8030 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008031
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008032 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8033 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008034 return;
8035 }
8036 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008037 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008038out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008039 dprintk("<-- %s\n", __func__);
8040}
8041
8042static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8043{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008044 struct nfs4_sequence_data *calldata = data;
8045 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008046 struct nfs4_sequence_args *args;
8047 struct nfs4_sequence_res *res;
8048
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008049 args = task->tk_msg.rpc_argp;
8050 res = task->tk_msg.rpc_resp;
8051
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008052 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008053}
8054
8055static const struct rpc_call_ops nfs41_sequence_ops = {
8056 .rpc_call_done = nfs41_sequence_call_done,
8057 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008058 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008059};
8060
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008061static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8062 struct rpc_cred *cred,
8063 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008064{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008065 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008066 struct rpc_message msg = {
8067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8068 .rpc_cred = cred,
8069 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008070 struct rpc_task_setup task_setup_data = {
8071 .rpc_client = clp->cl_rpcclient,
8072 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008073 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008074 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008075 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008076
Alexandros Batsakis71358402010-02-05 03:45:05 -08008077 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008078 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008079 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008080 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008081 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008082 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008083 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008084 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008085 if (is_privileged)
8086 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008087 msg.rpc_argp = &calldata->args;
8088 msg.rpc_resp = &calldata->res;
8089 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008090 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008091
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008092 return rpc_run_task(&task_setup_data);
8093}
8094
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008095static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008096{
8097 struct rpc_task *task;
8098 int ret = 0;
8099
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008100 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008101 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008102 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008103 if (IS_ERR(task))
8104 ret = PTR_ERR(task);
8105 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008106 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008107 dprintk("<-- %s status=%d\n", __func__, ret);
8108 return ret;
8109}
8110
8111static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8112{
8113 struct rpc_task *task;
8114 int ret;
8115
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008116 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008117 if (IS_ERR(task)) {
8118 ret = PTR_ERR(task);
8119 goto out;
8120 }
8121 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008122 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008123 ret = task->tk_status;
8124 rpc_put_task(task);
8125out:
8126 dprintk("<-- %s status=%d\n", __func__, ret);
8127 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008128}
8129
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008130struct nfs4_reclaim_complete_data {
8131 struct nfs_client *clp;
8132 struct nfs41_reclaim_complete_args arg;
8133 struct nfs41_reclaim_complete_res res;
8134};
8135
8136static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8137{
8138 struct nfs4_reclaim_complete_data *calldata = data;
8139
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008140 nfs41_setup_sequence(calldata->clp->cl_session,
8141 &calldata->arg.seq_args,
8142 &calldata->res.seq_res,
8143 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008144}
8145
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008146static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8147{
8148 switch(task->tk_status) {
8149 case 0:
8150 case -NFS4ERR_COMPLETE_ALREADY:
8151 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8152 break;
8153 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008154 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008155 /* fall through */
8156 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008157 return -EAGAIN;
8158 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008159 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008160 }
8161 return 0;
8162}
8163
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008164static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8165{
8166 struct nfs4_reclaim_complete_data *calldata = data;
8167 struct nfs_client *clp = calldata->clp;
8168 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8169
8170 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008171 if (!nfs41_sequence_done(task, res))
8172 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008173
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008174 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008175 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8176 rpc_restart_call_prepare(task);
8177 return;
8178 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008179 dprintk("<-- %s\n", __func__);
8180}
8181
8182static void nfs4_free_reclaim_complete_data(void *data)
8183{
8184 struct nfs4_reclaim_complete_data *calldata = data;
8185
8186 kfree(calldata);
8187}
8188
8189static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8190 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8191 .rpc_call_done = nfs4_reclaim_complete_done,
8192 .rpc_release = nfs4_free_reclaim_complete_data,
8193};
8194
8195/*
8196 * Issue a global reclaim complete.
8197 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008198static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8199 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008200{
8201 struct nfs4_reclaim_complete_data *calldata;
8202 struct rpc_task *task;
8203 struct rpc_message msg = {
8204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008205 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008206 };
8207 struct rpc_task_setup task_setup_data = {
8208 .rpc_client = clp->cl_rpcclient,
8209 .rpc_message = &msg,
8210 .callback_ops = &nfs4_reclaim_complete_call_ops,
8211 .flags = RPC_TASK_ASYNC,
8212 };
8213 int status = -ENOMEM;
8214
8215 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008216 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008217 if (calldata == NULL)
8218 goto out;
8219 calldata->clp = clp;
8220 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008221
Chuck Levera9c92d62013-08-09 12:48:18 -04008222 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008223 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008224 msg.rpc_argp = &calldata->arg;
8225 msg.rpc_resp = &calldata->res;
8226 task_setup_data.callback_data = calldata;
8227 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008228 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008229 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008230 goto out;
8231 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008232 status = nfs4_wait_for_completion_rpc_task(task);
8233 if (status == 0)
8234 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008235 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008236 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008237out:
8238 dprintk("<-- %s status=%d\n", __func__, status);
8239 return status;
8240}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008241
8242static void
8243nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8244{
8245 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008246 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008247 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008248
8249 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008250 nfs41_setup_sequence(session, &lgp->args.seq_args,
8251 &lgp->res.seq_res, task);
8252 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008253}
8254
8255static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8256{
8257 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008258
8259 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008260 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008261 dprintk("<-- %s\n", __func__);
8262}
8263
8264static int
8265nfs4_layoutget_handle_exception(struct rpc_task *task,
8266 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8267{
Trond Myklebustee314c22012-10-01 17:25:48 -07008268 struct inode *inode = lgp->args.inode;
8269 struct nfs_server *server = NFS_SERVER(inode);
8270 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008271 int nfs4err = task->tk_status;
8272 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008273 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008274
Boaz Harroshed7e5422014-01-22 20:34:54 +02008275 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008276
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008277 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008278 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008279 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008280
8281 /*
8282 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8283 * on the file. set tk_status to -ENODATA to tell upper layer to
8284 * retry go inband.
8285 */
8286 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008287 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008288 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008289 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008290 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8291 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8292 */
8293 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008294 status = -EOVERFLOW;
8295 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008296 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008297 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008298 * (or clients) writing to the same RAID stripe except when
8299 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008300 *
8301 * Treat it like we would RECALLCONFLICT -- we retry for a little
8302 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008303 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008304 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008305 if (lgp->args.minlength == 0) {
8306 status = -EOVERFLOW;
8307 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008308 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008309 status = -EBUSY;
8310 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008311 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008312 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008313 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008314 case -NFS4ERR_DELEG_REVOKED:
8315 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008316 case -NFS4ERR_EXPIRED:
8317 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008318 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008319 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008320 lo = NFS_I(inode)->layout;
8321 /* If the open stateid was bad, then recover it. */
8322 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8323 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008324 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008325 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008326 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008327 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008328 break;
8329 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008330
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008331 /*
8332 * Mark the bad layout state as invalid, then retry
8333 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008334 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008335 spin_unlock(&inode->i_lock);
8336 pnfs_free_lseg_list(&head);
8337 status = -EAGAIN;
8338 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008339 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008340
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008341 err = nfs4_handle_exception(server, nfs4err, exception);
8342 if (!status) {
8343 if (exception->retry)
8344 status = -EAGAIN;
8345 else
8346 status = err;
8347 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008348out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008349 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008350 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008351}
8352
Idan Kedar85541162012-08-02 11:47:10 +03008353static size_t max_response_pages(struct nfs_server *server)
8354{
8355 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8356 return nfs_page_array_len(0, max_resp_sz);
8357}
8358
8359static void nfs4_free_pages(struct page **pages, size_t size)
8360{
8361 int i;
8362
8363 if (!pages)
8364 return;
8365
8366 for (i = 0; i < size; i++) {
8367 if (!pages[i])
8368 break;
8369 __free_page(pages[i]);
8370 }
8371 kfree(pages);
8372}
8373
8374static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8375{
8376 struct page **pages;
8377 int i;
8378
8379 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8380 if (!pages) {
8381 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8382 return NULL;
8383 }
8384
8385 for (i = 0; i < size; i++) {
8386 pages[i] = alloc_page(gfp_flags);
8387 if (!pages[i]) {
8388 dprintk("%s: failed to allocate page\n", __func__);
8389 nfs4_free_pages(pages, size);
8390 return NULL;
8391 }
8392 }
8393
8394 return pages;
8395}
8396
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008397static void nfs4_layoutget_release(void *calldata)
8398{
8399 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008400 struct inode *inode = lgp->args.inode;
8401 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008402 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008403
8404 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008405 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008406 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008407 put_nfs_open_context(lgp->args.ctx);
8408 kfree(calldata);
8409 dprintk("<-- %s\n", __func__);
8410}
8411
8412static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8413 .rpc_call_prepare = nfs4_layoutget_prepare,
8414 .rpc_call_done = nfs4_layoutget_done,
8415 .rpc_release = nfs4_layoutget_release,
8416};
8417
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008418struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008419nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008420{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008421 struct inode *inode = lgp->args.inode;
8422 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008423 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008424 struct rpc_task *task;
8425 struct rpc_message msg = {
8426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8427 .rpc_argp = &lgp->args,
8428 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008429 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008430 };
8431 struct rpc_task_setup task_setup_data = {
8432 .rpc_client = server->client,
8433 .rpc_message = &msg,
8434 .callback_ops = &nfs4_layoutget_call_ops,
8435 .callback_data = lgp,
8436 .flags = RPC_TASK_ASYNC,
8437 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008438 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008439 struct nfs4_exception exception = {
8440 .inode = inode,
8441 .timeout = *timeout,
8442 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008443 int status = 0;
8444
8445 dprintk("--> %s\n", __func__);
8446
Peng Tao4bd5a982014-11-17 11:05:17 +08008447 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8448 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8449
Idan Kedar85541162012-08-02 11:47:10 +03008450 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8451 if (!lgp->args.layout.pages) {
8452 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008453 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008454 }
8455 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8456
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008457 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008458 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008459 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008460
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008461 task = rpc_run_task(&task_setup_data);
8462 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008463 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008464 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008465 if (status == 0) {
8466 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8467 *timeout = exception.timeout;
8468 }
8469
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008470 trace_nfs4_layoutget(lgp->args.ctx,
8471 &lgp->args.range,
8472 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008473 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008474 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008475
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008476 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8477 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008478 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008479 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008480 rpc_put_task(task);
8481 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008482 if (status)
8483 return ERR_PTR(status);
8484 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008485}
8486
Benny Halevycbe82602011-05-22 19:52:37 +03008487static void
8488nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8489{
8490 struct nfs4_layoutreturn *lrp = calldata;
8491
8492 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008493 nfs41_setup_sequence(lrp->clp->cl_session,
8494 &lrp->args.seq_args,
8495 &lrp->res.seq_res,
8496 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008497}
8498
8499static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8500{
8501 struct nfs4_layoutreturn *lrp = calldata;
8502 struct nfs_server *server;
8503
8504 dprintk("--> %s\n", __func__);
8505
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008506 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008507 return;
8508
8509 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008510 switch (task->tk_status) {
8511 default:
8512 task->tk_status = 0;
8513 case 0:
8514 break;
8515 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008516 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008517 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008518 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008519 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008520 return;
8521 }
Benny Halevycbe82602011-05-22 19:52:37 +03008522 dprintk("<-- %s\n", __func__);
8523}
8524
8525static void nfs4_layoutreturn_release(void *calldata)
8526{
8527 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008528 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008529 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008530
8531 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008532 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008533 if (lrp->res.lrs_present) {
8534 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8535 &lrp->args.range,
8536 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008537 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008538 } else
8539 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008540 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008541 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008542 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008543 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008544 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008545 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008546 kfree(calldata);
8547 dprintk("<-- %s\n", __func__);
8548}
8549
8550static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8551 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8552 .rpc_call_done = nfs4_layoutreturn_done,
8553 .rpc_release = nfs4_layoutreturn_release,
8554};
8555
Peng Tao6c166052014-11-17 09:30:40 +08008556int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008557{
8558 struct rpc_task *task;
8559 struct rpc_message msg = {
8560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8561 .rpc_argp = &lrp->args,
8562 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008563 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008564 };
8565 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008566 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008567 .rpc_message = &msg,
8568 .callback_ops = &nfs4_layoutreturn_call_ops,
8569 .callback_data = lrp,
8570 };
Peng Tao6c166052014-11-17 09:30:40 +08008571 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008572
Andrew Elble99ade3c2015-12-02 09:39:51 -05008573 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8574 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8575 &task_setup_data.rpc_client, &msg);
8576
Benny Halevycbe82602011-05-22 19:52:37 +03008577 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008578 if (!sync) {
8579 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8580 if (!lrp->inode) {
8581 nfs4_layoutreturn_release(lrp);
8582 return -EAGAIN;
8583 }
8584 task_setup_data.flags |= RPC_TASK_ASYNC;
8585 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008586 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008587 task = rpc_run_task(&task_setup_data);
8588 if (IS_ERR(task))
8589 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008590 if (sync)
8591 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008592 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008593 dprintk("<-- %s status=%d\n", __func__, status);
8594 rpc_put_task(task);
8595 return status;
8596}
8597
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008598static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008599_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8600 struct pnfs_device *pdev,
8601 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008602{
8603 struct nfs4_getdeviceinfo_args args = {
8604 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008605 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8606 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008607 };
8608 struct nfs4_getdeviceinfo_res res = {
8609 .pdev = pdev,
8610 };
8611 struct rpc_message msg = {
8612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8613 .rpc_argp = &args,
8614 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008615 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008616 };
8617 int status;
8618
8619 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008620 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008621 if (res.notification & ~args.notify_types)
8622 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008623 if (res.notification != args.notify_types)
8624 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008625
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008626 dprintk("<-- %s status=%d\n", __func__, status);
8627
8628 return status;
8629}
8630
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008631int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8632 struct pnfs_device *pdev,
8633 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008634{
8635 struct nfs4_exception exception = { };
8636 int err;
8637
8638 do {
8639 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008640 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008641 &exception);
8642 } while (exception.retry);
8643 return err;
8644}
8645EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8646
Andy Adamson863a3c62011-03-23 13:27:54 +00008647static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8648{
8649 struct nfs4_layoutcommit_data *data = calldata;
8650 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008651 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008652
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008653 nfs41_setup_sequence(session,
8654 &data->args.seq_args,
8655 &data->res.seq_res,
8656 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008657}
8658
8659static void
8660nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8661{
8662 struct nfs4_layoutcommit_data *data = calldata;
8663 struct nfs_server *server = NFS_SERVER(data->args.inode);
8664
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008665 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008666 return;
8667
8668 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008669 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8670 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8671 case -NFS4ERR_BADLAYOUT: /* no layout */
8672 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008673 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008674 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008675 break;
8676 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008677 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008678 rpc_restart_call_prepare(task);
8679 return;
8680 }
8681 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008682}
8683
8684static void nfs4_layoutcommit_release(void *calldata)
8685{
8686 struct nfs4_layoutcommit_data *data = calldata;
8687
Andy Adamsondb29c082011-07-30 20:52:38 -04008688 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008689 nfs_post_op_update_inode_force_wcc(data->args.inode,
8690 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008691 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008692 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008693 kfree(data);
8694}
8695
8696static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8697 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8698 .rpc_call_done = nfs4_layoutcommit_done,
8699 .rpc_release = nfs4_layoutcommit_release,
8700};
8701
8702int
Andy Adamsonef311532011-03-12 02:58:10 -05008703nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008704{
8705 struct rpc_message msg = {
8706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8707 .rpc_argp = &data->args,
8708 .rpc_resp = &data->res,
8709 .rpc_cred = data->cred,
8710 };
8711 struct rpc_task_setup task_setup_data = {
8712 .task = &data->task,
8713 .rpc_client = NFS_CLIENT(data->args.inode),
8714 .rpc_message = &msg,
8715 .callback_ops = &nfs4_layoutcommit_ops,
8716 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008717 };
8718 struct rpc_task *task;
8719 int status = 0;
8720
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008721 dprintk("NFS: initiating layoutcommit call. sync %d "
8722 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008723 data->args.lastbytewritten,
8724 data->args.inode->i_ino);
8725
Trond Myklebust472e2592015-02-05 16:50:30 -05008726 if (!sync) {
8727 data->inode = nfs_igrab_and_active(data->args.inode);
8728 if (data->inode == NULL) {
8729 nfs4_layoutcommit_release(data);
8730 return -EAGAIN;
8731 }
8732 task_setup_data.flags = RPC_TASK_ASYNC;
8733 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008734 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008735 task = rpc_run_task(&task_setup_data);
8736 if (IS_ERR(task))
8737 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008738 if (sync)
8739 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008740 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008741 dprintk("%s: status %d\n", __func__, status);
8742 rpc_put_task(task);
8743 return status;
8744}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008745
Andy Adamson97431202013-08-08 10:57:56 -04008746/**
8747 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8748 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8749 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008750static int
8751_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008752 struct nfs_fsinfo *info,
8753 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008754{
8755 struct nfs41_secinfo_no_name_args args = {
8756 .style = SECINFO_STYLE_CURRENT_FH,
8757 };
8758 struct nfs4_secinfo_res res = {
8759 .flavors = flavors,
8760 };
8761 struct rpc_message msg = {
8762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8763 .rpc_argp = &args,
8764 .rpc_resp = &res,
8765 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008766 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008767 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008768 int status;
8769
8770 if (use_integrity) {
8771 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008772 cred = nfs4_get_clid_cred(server->nfs_client);
8773 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008774 }
8775
8776 dprintk("--> %s\n", __func__);
8777 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8778 &res.seq_res, 0);
8779 dprintk("<-- %s status=%d\n", __func__, status);
8780
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008781 if (cred)
8782 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008783
8784 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008785}
8786
8787static int
8788nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8789 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8790{
8791 struct nfs4_exception exception = { };
8792 int err;
8793 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008794 /* first try using integrity protection */
8795 err = -NFS4ERR_WRONGSEC;
8796
8797 /* try to use integrity protection with machine cred */
8798 if (_nfs4_is_integrity_protected(server->nfs_client))
8799 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8800 flavors, true);
8801
8802 /*
8803 * if unable to use integrity protection, or SECINFO with
8804 * integrity protection returns NFS4ERR_WRONGSEC (which is
8805 * disallowed by spec, but exists in deployed servers) use
8806 * the current filesystem's rpc_client and the user cred.
8807 */
8808 if (err == -NFS4ERR_WRONGSEC)
8809 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8810 flavors, false);
8811
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008812 switch (err) {
8813 case 0:
8814 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008815 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008816 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008817 default:
8818 err = nfs4_handle_exception(server, err, &exception);
8819 }
8820 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008821out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008822 return err;
8823}
8824
8825static int
8826nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8827 struct nfs_fsinfo *info)
8828{
8829 int err;
8830 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008831 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008832 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008833 struct nfs4_secinfo4 *secinfo;
8834 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008835
8836 page = alloc_page(GFP_KERNEL);
8837 if (!page) {
8838 err = -ENOMEM;
8839 goto out;
8840 }
8841
8842 flavors = page_address(page);
8843 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8844
8845 /*
8846 * Fall back on "guess and check" method if
8847 * the server doesn't support SECINFO_NO_NAME
8848 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008849 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008850 err = nfs4_find_root_sec(server, fhandle, info);
8851 goto out_freepage;
8852 }
8853 if (err)
8854 goto out_freepage;
8855
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008856 for (i = 0; i < flavors->num_flavors; i++) {
8857 secinfo = &flavors->flavors[i];
8858
8859 switch (secinfo->flavor) {
8860 case RPC_AUTH_NULL:
8861 case RPC_AUTH_UNIX:
8862 case RPC_AUTH_GSS:
8863 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8864 &secinfo->flavor_info);
8865 break;
8866 default:
8867 flavor = RPC_AUTH_MAXFLAVOR;
8868 break;
8869 }
8870
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008871 if (!nfs_auth_info_match(&server->auth_info, flavor))
8872 flavor = RPC_AUTH_MAXFLAVOR;
8873
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008874 if (flavor != RPC_AUTH_MAXFLAVOR) {
8875 err = nfs4_lookup_root_sec(server, fhandle,
8876 info, flavor);
8877 if (!err)
8878 break;
8879 }
8880 }
8881
8882 if (flavor == RPC_AUTH_MAXFLAVOR)
8883 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008884
8885out_freepage:
8886 put_page(page);
8887 if (err == -EACCES)
8888 return -EPERM;
8889out:
8890 return err;
8891}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008892
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008893static int _nfs41_test_stateid(struct nfs_server *server,
8894 nfs4_stateid *stateid,
8895 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008896{
8897 int status;
8898 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008899 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008900 };
8901 struct nfs41_test_stateid_res res;
8902 struct rpc_message msg = {
8903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8904 .rpc_argp = &args,
8905 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008906 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008907 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008908 struct rpc_clnt *rpc_client = server->client;
8909
8910 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8911 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008912
Chuck Lever38527b12012-07-11 16:30:23 -04008913 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008914 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008915 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008916 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008917 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008918 if (status != NFS_OK) {
8919 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008920 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008921 }
8922 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008923 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008924}
8925
Trond Myklebust43912bb2016-09-22 13:38:56 -04008926static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8927 int err, struct nfs4_exception *exception)
8928{
8929 exception->retry = 0;
8930 switch(err) {
8931 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008932 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008933 nfs4_handle_exception(server, err, exception);
8934 break;
8935 case -NFS4ERR_BADSESSION:
8936 case -NFS4ERR_BADSLOT:
8937 case -NFS4ERR_BAD_HIGH_SLOT:
8938 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8939 case -NFS4ERR_DEADSESSION:
8940 nfs4_do_handle_exception(server, err, exception);
8941 }
8942}
8943
Chuck Lever38527b12012-07-11 16:30:23 -04008944/**
8945 * nfs41_test_stateid - perform a TEST_STATEID operation
8946 *
8947 * @server: server / transport on which to perform the operation
8948 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008949 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008950 *
8951 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8952 * Otherwise a negative NFS4ERR value is returned if the operation
8953 * failed or the state ID is not currently valid.
8954 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008955static int nfs41_test_stateid(struct nfs_server *server,
8956 nfs4_stateid *stateid,
8957 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008958{
8959 struct nfs4_exception exception = { };
8960 int err;
8961 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008962 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008963 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008964 } while (exception.retry);
8965 return err;
8966}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008967
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008968struct nfs_free_stateid_data {
8969 struct nfs_server *server;
8970 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008971 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008972};
8973
8974static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8975{
8976 struct nfs_free_stateid_data *data = calldata;
8977 nfs41_setup_sequence(nfs4_get_session(data->server),
8978 &data->args.seq_args,
8979 &data->res.seq_res,
8980 task);
8981}
8982
8983static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8984{
8985 struct nfs_free_stateid_data *data = calldata;
8986
8987 nfs41_sequence_done(task, &data->res.seq_res);
8988
8989 switch (task->tk_status) {
8990 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008991 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008992 rpc_restart_call_prepare(task);
8993 }
8994}
8995
8996static void nfs41_free_stateid_release(void *calldata)
8997{
8998 kfree(calldata);
8999}
9000
Trond Myklebust17f26b12013-08-21 15:48:42 -04009001static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009002 .rpc_call_prepare = nfs41_free_stateid_prepare,
9003 .rpc_call_done = nfs41_free_stateid_done,
9004 .rpc_release = nfs41_free_stateid_release,
9005};
9006
9007static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009008 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009009 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009010 bool privileged)
9011{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009012 struct rpc_message msg = {
9013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009014 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009015 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009016 struct rpc_task_setup task_setup = {
9017 .rpc_client = server->client,
9018 .rpc_message = &msg,
9019 .callback_ops = &nfs41_free_stateid_ops,
9020 .flags = RPC_TASK_ASYNC,
9021 };
9022 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009023
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009024 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9025 &task_setup.rpc_client, &msg);
9026
Chuck Lever38527b12012-07-11 16:30:23 -04009027 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009028 data = kmalloc(sizeof(*data), GFP_NOFS);
9029 if (!data)
9030 return ERR_PTR(-ENOMEM);
9031 data->server = server;
9032 nfs4_stateid_copy(&data->args.stateid, stateid);
9033
9034 task_setup.callback_data = data;
9035
9036 msg.rpc_argp = &data->args;
9037 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009038 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009039 if (privileged)
9040 nfs4_set_sequence_privileged(&data->args.seq_args);
9041
9042 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009043}
9044
Chuck Lever38527b12012-07-11 16:30:23 -04009045/**
9046 * nfs41_free_stateid - perform a FREE_STATEID operation
9047 *
9048 * @server: server / transport on which to perform the operation
9049 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009050 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009051 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009052 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009053 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009054 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009055static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009056 const nfs4_stateid *stateid,
9057 struct rpc_cred *cred,
9058 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009059{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009060 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009061
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009062 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009063 if (IS_ERR(task))
9064 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009065 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009066 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009067}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009068
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009069static void
9070nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009071{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009072 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009073
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009074 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009075 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009076}
9077
Trond Myklebust36281ca2012-03-04 18:13:56 -05009078static bool nfs41_match_stateid(const nfs4_stateid *s1,
9079 const nfs4_stateid *s2)
9080{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009081 if (s1->type != s2->type)
9082 return false;
9083
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009084 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009085 return false;
9086
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009087 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009088 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009089 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009090 return true;
9091
9092 return false;
9093}
9094
Andy Adamson557134a2009-04-01 09:21:53 -04009095#endif /* CONFIG_NFS_V4_1 */
9096
Trond Myklebust36281ca2012-03-04 18:13:56 -05009097static bool nfs4_match_stateid(const nfs4_stateid *s1,
9098 const nfs4_stateid *s2)
9099{
Trond Myklebustf597c532012-03-04 18:13:56 -05009100 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009101}
9102
9103
Trond Myklebust17280172012-03-11 13:11:00 -04009104static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009105 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009106 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009107 .recover_open = nfs4_open_reclaim,
9108 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009109 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009110 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009111};
9112
Andy Adamson591d71c2009-04-01 09:22:47 -04009113#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009114static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009115 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9116 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9117 .recover_open = nfs4_open_reclaim,
9118 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009119 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009120 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009121 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009122};
9123#endif /* CONFIG_NFS_V4_1 */
9124
Trond Myklebust17280172012-03-11 13:11:00 -04009125static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009126 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009127 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009128 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009129 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009130 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131};
9132
Andy Adamson591d71c2009-04-01 09:22:47 -04009133#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009134static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009135 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9136 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009137 .recover_open = nfs41_open_expired,
9138 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009139 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009140};
9141#endif /* CONFIG_NFS_V4_1 */
9142
Trond Myklebust17280172012-03-11 13:11:00 -04009143static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009144 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009145 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009146 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009147};
9148
9149#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009150static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009151 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009152 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009153 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009154};
9155#endif
9156
Chuck Leverec011fe2013-10-17 14:12:39 -04009157static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009158 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009159 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009160};
9161
9162#if defined(CONFIG_NFS_V4_1)
9163static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009164 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009165 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009166};
9167#endif /* CONFIG_NFS_V4_1 */
9168
Trond Myklebust97dc1352010-06-16 09:52:26 -04009169static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9170 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009171 .init_caps = NFS_CAP_READDIRPLUS
9172 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009173 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009174 .init_client = nfs40_init_client,
9175 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009176 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009177 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009178 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009179 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009180 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009181 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009182 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9183 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9184 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009185 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009186};
9187
9188#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009189static struct nfs_seqid *
9190nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9191{
9192 return NULL;
9193}
9194
Trond Myklebust97dc1352010-06-16 09:52:26 -04009195static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9196 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009197 .init_caps = NFS_CAP_READDIRPLUS
9198 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009199 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009200 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009201 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009202 .init_client = nfs41_init_client,
9203 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009204 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009205 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009206 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009207 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009208 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009209 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009210 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009211 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9212 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9213 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009214 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009215};
9216#endif
9217
Steve Dickson42c2c422013-05-22 12:50:38 -04009218#if defined(CONFIG_NFS_V4_2)
9219static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9220 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009221 .init_caps = NFS_CAP_READDIRPLUS
9222 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009223 | NFS_CAP_POSIX_LOCK
9224 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009225 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009226 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009227 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009228 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009229 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009230 | NFS_CAP_LAYOUTSTATS
9231 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009232 .init_client = nfs41_init_client,
9233 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009234 .match_stateid = nfs41_match_stateid,
9235 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009236 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009237 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009238 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009239 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009240 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009241 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9242 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9243 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009244 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009245};
9246#endif
9247
Trond Myklebust97dc1352010-06-16 09:52:26 -04009248const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9249 [0] = &nfs_v4_0_minor_ops,
9250#if defined(CONFIG_NFS_V4_1)
9251 [1] = &nfs_v4_1_minor_ops,
9252#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009253#if defined(CONFIG_NFS_V4_2)
9254 [2] = &nfs_v4_2_minor_ops,
9255#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009256};
9257
Trond Myklebust13997822016-07-24 17:10:52 -04009258static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009259{
9260 ssize_t error, error2;
9261
9262 error = generic_listxattr(dentry, list, size);
9263 if (error < 0)
9264 return error;
9265 if (list) {
9266 list += error;
9267 size -= error;
9268 }
9269
9270 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9271 if (error2 < 0)
9272 return error2;
9273 return error + error2;
9274}
9275
Trond Myklebust17f26b12013-08-21 15:48:42 -04009276static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009277 .create = nfs_create,
9278 .lookup = nfs_lookup,
9279 .atomic_open = nfs_atomic_open,
9280 .link = nfs_link,
9281 .unlink = nfs_unlink,
9282 .symlink = nfs_symlink,
9283 .mkdir = nfs_mkdir,
9284 .rmdir = nfs_rmdir,
9285 .mknod = nfs_mknod,
9286 .rename = nfs_rename,
9287 .permission = nfs_permission,
9288 .getattr = nfs_getattr,
9289 .setattr = nfs_setattr,
9290 .getxattr = generic_getxattr,
9291 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009292 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009293 .removexattr = generic_removexattr,
9294};
9295
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009296static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009297 .permission = nfs_permission,
9298 .getattr = nfs_getattr,
9299 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009300 .getxattr = generic_getxattr,
9301 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009302 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009303 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009304};
9305
David Howells509de812006-08-22 20:06:11 -04009306const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307 .version = 4, /* protocol version */
9308 .dentry_ops = &nfs4_dentry_operations,
9309 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009310 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009311 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009313 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009314 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315 .getattr = nfs4_proc_getattr,
9316 .setattr = nfs4_proc_setattr,
9317 .lookup = nfs4_proc_lookup,
9318 .access = nfs4_proc_access,
9319 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320 .create = nfs4_proc_create,
9321 .remove = nfs4_proc_remove,
9322 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009323 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009325 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009326 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009327 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328 .link = nfs4_proc_link,
9329 .symlink = nfs4_proc_symlink,
9330 .mkdir = nfs4_proc_mkdir,
9331 .rmdir = nfs4_proc_remove,
9332 .readdir = nfs4_proc_readdir,
9333 .mknod = nfs4_proc_mknod,
9334 .statfs = nfs4_proc_statfs,
9335 .fsinfo = nfs4_proc_fsinfo,
9336 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009337 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009339 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009340 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009341 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009343 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009345 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009346 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009348 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009349 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009350 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009351 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009352 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009353 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009354 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009355 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009356 .create_server = nfs4_create_server,
9357 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009358};
9359
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009360static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009361 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009362 .list = nfs4_xattr_list_nfs4_acl,
9363 .get = nfs4_xattr_get_nfs4_acl,
9364 .set = nfs4_xattr_set_nfs4_acl,
9365};
9366
9367const struct xattr_handler *nfs4_xattr_handlers[] = {
9368 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009369#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9370 &nfs4_xattr_nfs4_label_handler,
9371#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009372 NULL
9373};
9374
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375/*
9376 * Local variables:
9377 * c-basic-offset: 8
9378 * End:
9379 */