blob: 2a155ec05f6d022b9a1aa807b6046970b0b8e646 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust516285eb2015-09-20 16:15:24 -040081static int nfs4_async_handle_error(struct rpc_task *, struct nfs_server *, struct nfs4_state *, long *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040083static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
84static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040085static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040087 struct nfs4_state *state, struct nfs4_label *ilabel,
88 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040090static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 struct rpc_cred *);
92static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
93 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040094#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040095
96#ifdef CONFIG_NFS_V4_SECURITY_LABEL
97static inline struct nfs4_label *
98nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
99 struct iattr *sattr, struct nfs4_label *label)
100{
101 int err;
102
103 if (label == NULL)
104 return NULL;
105
106 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
107 return NULL;
108
David Quigleyaa9c2662013-05-22 12:50:44 -0400109 err = security_dentry_init_security(dentry, sattr->ia_mode,
110 &dentry->d_name, (void **)&label->label, &label->len);
111 if (err == 0)
112 return label;
113
114 return NULL;
115}
116static inline void
117nfs4_label_release_security(struct nfs4_label *label)
118{
119 if (label)
120 security_release_secctx(label->label, label->len);
121}
122static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
123{
124 if (label)
125 return server->attr_bitmask;
126
127 return server->attr_bitmask_nl;
128}
129#else
130static inline struct nfs4_label *
131nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
132 struct iattr *sattr, struct nfs4_label *l)
133{ return NULL; }
134static inline void
135nfs4_label_release_security(struct nfs4_label *label)
136{ return; }
137static inline u32 *
138nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139{ return server->attr_bitmask; }
140#endif
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500143static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Trond Myklebust52567b02009-10-23 14:46:42 -0400145 if (err >= -1000)
146 return err;
147 switch (err) {
148 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500149 case -NFS4ERR_LAYOUTTRYLATER:
150 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400151 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000152 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400153 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800155 case -NFS4ERR_BADOWNER:
156 case -NFS4ERR_BADNAME:
157 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400158 case -NFS4ERR_SHARE_DENIED:
159 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400160 case -NFS4ERR_MINOR_VERS_MISMATCH:
161 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400162 case -NFS4ERR_FILE_OPEN:
163 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400164 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700166 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400169 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * This is our standard bitmap for GETATTR requests.
174 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400175const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 FATTR4_WORD0_TYPE
177 | FATTR4_WORD0_CHANGE
178 | FATTR4_WORD0_SIZE
179 | FATTR4_WORD0_FSID
180 | FATTR4_WORD0_FILEID,
181 FATTR4_WORD1_MODE
182 | FATTR4_WORD1_NUMLINKS
183 | FATTR4_WORD1_OWNER
184 | FATTR4_WORD1_OWNER_GROUP
185 | FATTR4_WORD1_RAWDEV
186 | FATTR4_WORD1_SPACE_USED
187 | FATTR4_WORD1_TIME_ACCESS
188 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400189 | FATTR4_WORD1_TIME_MODIFY
190 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400191#ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Trond Myklebust1549210f2012-06-05 09:16:47 -0400196static const u32 nfs4_pnfs_open_bitmap[3] = {
197 FATTR4_WORD0_TYPE
198 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_SIZE
200 | FATTR4_WORD0_FSID
201 | FATTR4_WORD0_FILEID,
202 FATTR4_WORD1_MODE
203 | FATTR4_WORD1_NUMLINKS
204 | FATTR4_WORD1_OWNER
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
212};
213
Andy Adamsone23008e2012-10-02 21:07:32 -0400214static const u32 nfs4_open_noattr_bitmap[3] = {
215 FATTR4_WORD0_TYPE
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
218};
219
David Quigleya09df2c2013-05-22 12:50:41 -0400220const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
227};
228
David Quigleya09df2c2013-05-22 12:50:41 -0400229const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 FATTR4_WORD0_MAXLINK
231 | FATTR4_WORD0_MAXNAME,
232 0
233};
234
Fred Isamandae100c2011-07-30 20:52:37 -0400235const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700239 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400245 FATTR4_WORD0_TYPE
246 | FATTR4_WORD0_CHANGE
247 | FATTR4_WORD0_SIZE
248 | FATTR4_WORD0_FSID
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
251 FATTR4_WORD1_MODE
252 | FATTR4_WORD1_NUMLINKS
253 | FATTR4_WORD1_OWNER
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400261};
262
Al Virobc4785c2006-10-19 23:28:51 -0700263static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct nfs4_readdir_arg *readdir)
265{
Al Viro0dbb4c62006-10-19 23:28:49 -0700266 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000269 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
271 return;
272 }
273
274 readdir->cookie = 0;
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
276 if (cookie == 2)
277 return;
278
279 /*
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
284 * instead of 1 or 2.
285 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800286 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (cookie == 0) {
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
294 p++;
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000298 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800314 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
NeilBrown8478eaa2014-09-18 16:09:27 +1000317static long nfs4_update_delay(long *timeout)
318{
319 long ret;
320 if (!timeout)
321 return NFS4_POLL_RETRY_MAX;
322 if (*timeout <= 0)
323 *timeout = NFS4_POLL_RETRY_MIN;
324 if (*timeout > NFS4_POLL_RETRY_MAX)
325 *timeout = NFS4_POLL_RETRY_MAX;
326 ret = *timeout;
327 *timeout <<= 1;
328 return ret;
329}
330
Trond Myklebust65de8722008-12-23 15:21:44 -0500331static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
332{
333 int res = 0;
334
335 might_sleep();
336
NeilBrown8478eaa2014-09-18 16:09:27 +1000337 freezable_schedule_timeout_killable_unsafe(
338 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 if (fatal_signal_pending(current))
340 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 return res;
342}
343
344/* This is the error handling routine for processes that are allowed
345 * to sleep.
346 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400347static int nfs4_do_handle_exception(struct nfs_server *server,
348 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500349{
350 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500351 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500352 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 int ret = errorcode;
354
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400355 exception->delay = 0;
356 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500357 exception->retry = 0;
358 switch(errorcode) {
359 case 0:
360 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500361 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400362 case -NFS4ERR_DELEG_REVOKED:
363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
Trond Myklebust4816fda2015-09-20 14:58:42 -0400365 if (inode && nfs_async_inode_return_delegation(inode,
366 NULL) == 0)
367 goto wait_on_recovery;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500368 if (state == NULL)
369 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400370 ret = nfs4_schedule_stateid_recovery(server, state);
371 if (ret < 0)
372 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500373 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400374 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400375 if (state != NULL) {
376 ret = nfs4_schedule_stateid_recovery(server, state);
377 if (ret < 0)
378 break;
379 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500381 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500382 nfs4_schedule_lease_recovery(clp);
383 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400384 case -NFS4ERR_MOVED:
385 ret = nfs4_schedule_migration_recovery(server);
386 if (ret < 0)
387 break;
388 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400389 case -NFS4ERR_LEASE_MOVED:
390 nfs4_schedule_lease_moved_recovery(clp);
391 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500392#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
401 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400403 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500404#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
408 * fail
409 */
410 ret = -EBUSY;
411 break;
412 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400414 nfs_inc_server_stats(server, NFSIOS_DELAY);
415 case -NFS4ERR_GRACE:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400416 exception->delay = 1;
417 return 0;
418
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400419 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500420 case -NFS4ERR_OLD_STATEID:
421 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800422 break;
423 case -NFS4ERR_BADOWNER:
424 /* The following works around a Linux server bug! */
425 case -NFS4ERR_BADNAME:
426 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
427 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
428 exception->retry = 1;
429 printk(KERN_WARNING "NFS: v4 server %s "
430 "does not accept raw "
431 "uid/gids. "
432 "Reenabling the idmapper.\n",
433 server->nfs_client->cl_hostname);
434 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 }
436 /* We failed to handle the error */
437 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500438wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400439 exception->recovering = 1;
440 return 0;
441}
442
443/* This is the error handling routine for processes that are allowed
444 * to sleep.
445 */
446int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
447{
448 struct nfs_client *clp = server->nfs_client;
449 int ret;
450
451 ret = nfs4_do_handle_exception(server, errorcode, exception);
452 if (exception->delay) {
453 ret = nfs4_delay(server->client, &exception->timeout);
454 goto out_retry;
455 }
456 if (exception->recovering) {
457 ret = nfs4_wait_clnt_recover(clp);
458 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
459 return -EIO;
460 goto out_retry;
461 }
462 return ret;
463out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500464 if (ret == 0)
465 exception->retry = 1;
466 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500467}
468
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400469/*
470 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
471 * or 'false' otherwise.
472 */
473static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
474{
475 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
476
477 if (flavor == RPC_AUTH_GSS_KRB5I ||
478 flavor == RPC_AUTH_GSS_KRB5P)
479 return true;
480
481 return false;
482}
Trond Myklebust65de8722008-12-23 15:21:44 -0500483
Trond Myklebust452e9352010-07-31 14:29:06 -0400484static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 spin_lock(&clp->cl_lock);
487 if (time_before(clp->cl_last_renewal,timestamp))
488 clp->cl_last_renewal = timestamp;
489 spin_unlock(&clp->cl_lock);
490}
491
Trond Myklebust452e9352010-07-31 14:29:06 -0400492static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
493{
Trond Myklebustbe824162015-07-05 14:50:46 -0400494 struct nfs_client *clp = server->nfs_client;
495
496 if (!nfs4_has_session(clp))
497 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400498}
499
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400500struct nfs4_call_sync_data {
501 const struct nfs_server *seq_server;
502 struct nfs4_sequence_args *seq_args;
503 struct nfs4_sequence_res *seq_res;
504};
505
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800506void nfs4_init_sequence(struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400508{
509 args->sa_slot = NULL;
510 args->sa_cache_this = cache_reply;
511 args->sa_privileged = 0;
512
513 res->sr_slot = NULL;
514}
515
516static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
517{
518 args->sa_privileged = 1;
519}
520
Peng Taocb04ad22014-06-11 05:24:15 +0800521int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
522 struct nfs4_sequence_args *args,
523 struct nfs4_sequence_res *res,
524 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400525{
Chuck Lever3bd23842013-08-09 12:49:19 -0400526 struct nfs4_slot *slot;
527
528 /* slot already allocated? */
529 if (res->sr_slot != NULL)
530 goto out_start;
531
532 spin_lock(&tbl->slot_tbl_lock);
533 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
534 goto out_sleep;
535
536 slot = nfs4_alloc_slot(tbl);
537 if (IS_ERR(slot)) {
538 if (slot == ERR_PTR(-ENOMEM))
539 task->tk_timeout = HZ >> 2;
540 goto out_sleep;
541 }
542 spin_unlock(&tbl->slot_tbl_lock);
543
544 args->sa_slot = slot;
545 res->sr_slot = slot;
546
547out_start:
548 rpc_call_start(task);
549 return 0;
550
551out_sleep:
552 if (args->sa_privileged)
553 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
554 NULL, RPC_PRIORITY_PRIVILEGED);
555 else
556 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
557 spin_unlock(&tbl->slot_tbl_lock);
558 return -EAGAIN;
559}
Peng Taocb04ad22014-06-11 05:24:15 +0800560EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400561
562static int nfs40_sequence_done(struct rpc_task *task,
563 struct nfs4_sequence_res *res)
564{
565 struct nfs4_slot *slot = res->sr_slot;
566 struct nfs4_slot_table *tbl;
567
Trond Myklebustcab92c12014-01-29 12:12:15 -0500568 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400569 goto out;
570
571 tbl = slot->table;
572 spin_lock(&tbl->slot_tbl_lock);
573 if (!nfs41_wake_and_assign_slot(tbl, slot))
574 nfs4_free_slot(tbl, slot);
575 spin_unlock(&tbl->slot_tbl_lock);
576
577 res->sr_slot = NULL;
578out:
579 return 1;
580}
581
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400582#if defined(CONFIG_NFS_V4_1)
583
Trond Myklebustd185a332010-06-16 09:52:25 -0400584static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400585{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500586 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400587 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500588 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500589 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400590
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500591 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500592 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500593
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500594 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500595 /* Be nice to the server: try to ensure that the last transmitted
596 * value for highest_user_slotid <= target_highest_slotid
597 */
598 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
599 send_new_highest_used_slotid = true;
600
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500601 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500602 send_new_highest_used_slotid = false;
603 goto out_unlock;
604 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500605 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500606
607 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
608 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500609out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500610 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400611 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500612 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400613 nfs41_notify_server(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400614}
615
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500616int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400617{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500618 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500619 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400620 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500621 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500622 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400623
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500624 if (slot == NULL)
625 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400626 /* don't increment the sequence number if the task wasn't sent */
627 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400628 goto out;
629
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500630 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500631
Trond Myklebustac20d162012-12-15 15:36:07 -0500632 if (slot->interrupted) {
633 slot->interrupted = 0;
634 interrupted = true;
635 }
636
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400637 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500638 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400639 switch (res->sr_status) {
640 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400641 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500642 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500643 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500644 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500645 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400646 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500647 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400648 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500649 case 1:
650 /*
651 * sr_status remains 1 if an RPC level error occurred.
652 * The server may or may not have processed the sequence
653 * operation..
654 * Mark the slot as having hosted an interrupted RPC call.
655 */
656 slot->interrupted = 1;
657 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400658 case -NFS4ERR_DELAY:
659 /* The server detected a resend of the RPC call and
660 * returned NFS4ERR_DELAY as per Section 2.10.6.2
661 * of RFC5661.
662 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500663 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400664 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500665 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500666 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400667 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500668 case -NFS4ERR_BADSLOT:
669 /*
670 * The slot id we used was probably retired. Try again
671 * using a different slot id.
672 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500673 goto retry_nowait;
674 case -NFS4ERR_SEQ_MISORDERED:
675 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500676 * Was the last operation on this sequence interrupted?
677 * If so, retry after bumping the sequence number.
678 */
679 if (interrupted) {
680 ++slot->seq_nr;
681 goto retry_nowait;
682 }
683 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500684 * Could this slot have been previously retired?
685 * If so, then the server may be expecting seq_nr = 1!
686 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500687 if (slot->seq_nr != 1) {
688 slot->seq_nr = 1;
689 goto retry_nowait;
690 }
691 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500692 case -NFS4ERR_SEQ_FALSE_RETRY:
693 ++slot->seq_nr;
694 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400695 default:
696 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500697 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400698 }
699out:
700 /* The session may be reset by one of the error handlers. */
701 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400702 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500703out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500704 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500705retry_nowait:
706 if (rpc_restart_call_prepare(task)) {
707 task->tk_status = 0;
708 ret = 0;
709 }
710 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400711out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400712 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400713 goto out;
714 rpc_delay(task, NFS4_POLL_RETRY_MAX);
715 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400716}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500717EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400718
Peng Tao2c4b1312014-06-11 05:24:16 +0800719int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400720{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500721 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400722 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400723 if (!res->sr_slot->table->session)
724 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400725 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400726}
Peng Tao2c4b1312014-06-11 05:24:16 +0800727EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400728
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000729int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400730 struct nfs4_sequence_args *args,
731 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400732 struct rpc_task *task)
733{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400734 struct nfs4_slot *slot;
735 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400736
737 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400738 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400739 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400740 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400741
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400742 tbl = &session->fc_slot_table;
743
Trond Myklebust69d206b2012-11-22 13:21:02 -0500744 task->tk_timeout = 0;
745
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400746 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400747 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400748 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500749 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500750 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400751 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400752 }
753
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500754 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500755 if (IS_ERR(slot)) {
756 /* If out of memory, try again in 1/4 second */
757 if (slot == ERR_PTR(-ENOMEM))
758 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400759 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400760 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400761 }
762 spin_unlock(&tbl->slot_tbl_lock);
763
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500764 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400765
Chuck Levere8d92382013-08-09 12:47:51 -0400766 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500767 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400768
Benny Halevydfb4f3092010-09-24 09:17:01 -0400769 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500770 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400771 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400772 /*
773 * sr_status is only set in decode_sequence, and so will remain
774 * set to 1 if an rpc level failure occurs.
775 */
776 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400777 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400778out_success:
779 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400780 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400781out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400782 /* Privileged tasks are queued with top priority */
783 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400784 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
785 NULL, RPC_PRIORITY_PRIVILEGED);
786 else
787 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400788 spin_unlock(&tbl->slot_tbl_lock);
789 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400790}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000791EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400792
Chuck Lever5a580e02013-08-09 12:48:09 -0400793static int nfs4_setup_sequence(const struct nfs_server *server,
794 struct nfs4_sequence_args *args,
795 struct nfs4_sequence_res *res,
796 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400797{
Trond Myklebust035168a2010-06-16 09:52:26 -0400798 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400799 int ret = 0;
800
Chuck Lever3bd23842013-08-09 12:49:19 -0400801 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800802 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
803 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400804
Chuck Levere8d92382013-08-09 12:47:51 -0400805 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400806 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400807 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400808
Trond Myklebust9d12b212012-01-17 22:04:25 -0500809 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400810
Andy Adamsonce5039c2009-04-01 09:22:13 -0400811 dprintk("<-- %s status=%d\n", __func__, ret);
812 return ret;
813}
814
Andy Adamsonce5039c2009-04-01 09:22:13 -0400815static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
816{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400817 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400818 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400819
Trond Myklebust035168a2010-06-16 09:52:26 -0400820 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
821
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400822 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400823}
824
Andy Adamson69ab40c2009-04-01 09:22:19 -0400825static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
826{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400827 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400828
Trond Myklebust14516c32010-07-31 14:29:06 -0400829 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400830}
831
Trond Myklebust17280172012-03-11 13:11:00 -0400832static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400833 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400834 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400835};
836
Chuck Lever3bd23842013-08-09 12:49:19 -0400837#else /* !CONFIG_NFS_V4_1 */
838
Chuck Lever5a580e02013-08-09 12:48:09 -0400839static int nfs4_setup_sequence(const struct nfs_server *server,
840 struct nfs4_sequence_args *args,
841 struct nfs4_sequence_res *res,
842 struct rpc_task *task)
843{
Peng Taocb04ad22014-06-11 05:24:15 +0800844 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
845 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400846}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400847
Peng Tao2c4b1312014-06-11 05:24:16 +0800848int nfs4_sequence_done(struct rpc_task *task,
849 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400850{
Chuck Lever3bd23842013-08-09 12:49:19 -0400851 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400852}
Peng Tao2c4b1312014-06-11 05:24:16 +0800853EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400854
855#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400856
Chuck Lever9915ea72013-08-09 12:48:27 -0400857static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
858{
859 struct nfs4_call_sync_data *data = calldata;
860 nfs4_setup_sequence(data->seq_server,
861 data->seq_args, data->seq_res, task);
862}
863
864static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
865{
866 struct nfs4_call_sync_data *data = calldata;
867 nfs4_sequence_done(task, data->seq_res);
868}
869
870static const struct rpc_call_ops nfs40_call_sync_ops = {
871 .rpc_call_prepare = nfs40_call_sync_prepare,
872 .rpc_call_done = nfs40_call_sync_done,
873};
874
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400875static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400876 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500877 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100878 struct nfs4_sequence_args *args,
879 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400880{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 int ret;
882 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400883 struct nfs_client *clp = server->nfs_client;
884 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100885 .seq_server = server,
886 .seq_args = args,
887 .seq_res = res,
888 };
889 struct rpc_task_setup task_setup = {
890 .rpc_client = clnt,
891 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400892 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893 .callback_data = &data
894 };
895
896 task = rpc_run_task(&task_setup);
897 if (IS_ERR(task))
898 ret = PTR_ERR(task);
899 else {
900 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400901 rpc_put_task(task);
902 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100903 return ret;
904}
905
Bryan Schumaker7c513052011-03-24 17:12:24 +0000906int nfs4_call_sync(struct rpc_clnt *clnt,
907 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000908 struct rpc_message *msg,
909 struct nfs4_sequence_args *args,
910 struct nfs4_sequence_res *res,
911 int cache_reply)
912{
Chuck Levera9c92d62013-08-09 12:48:18 -0400913 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400914 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000915}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
918{
919 struct nfs_inode *nfsi = NFS_I(dir);
920
921 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400922 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700923 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700925 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500926 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000927 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 spin_unlock(&dir->i_lock);
929}
930
931struct nfs4_opendata {
932 struct kref kref;
933 struct nfs_openargs o_arg;
934 struct nfs_openres o_res;
935 struct nfs_open_confirmargs c_arg;
936 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500937 struct nfs4_string owner_name;
938 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +0800939 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400941 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400943 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct nfs4_state_owner *owner;
945 struct nfs4_state *state;
946 struct iattr attrs;
947 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400948 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400949 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400950 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400951 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 int cancelled;
953};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400954
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400955static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
956 int err, struct nfs4_exception *exception)
957{
958 if (err != -EINVAL)
959 return false;
960 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
961 return false;
962 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
963 exception->retry = 1;
964 return true;
965}
966
Trond Myklebust6ae37332015-01-30 14:21:14 -0500967static u32
968nfs4_map_atomic_open_share(struct nfs_server *server,
969 fmode_t fmode, int openflags)
970{
971 u32 res = 0;
972
973 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
974 case FMODE_READ:
975 res = NFS4_SHARE_ACCESS_READ;
976 break;
977 case FMODE_WRITE:
978 res = NFS4_SHARE_ACCESS_WRITE;
979 break;
980 case FMODE_READ|FMODE_WRITE:
981 res = NFS4_SHARE_ACCESS_BOTH;
982 }
983 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
984 goto out;
985 /* Want no delegation if we're using O_DIRECT */
986 if (openflags & O_DIRECT)
987 res |= NFS4_SHARE_WANT_NO_DELEG;
988out:
989 return res;
990}
991
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400992static enum open_claim_type4
993nfs4_map_atomic_open_claim(struct nfs_server *server,
994 enum open_claim_type4 claim)
995{
996 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
997 return claim;
998 switch (claim) {
999 default:
1000 return claim;
1001 case NFS4_OPEN_CLAIM_FH:
1002 return NFS4_OPEN_CLAIM_NULL;
1003 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1004 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1005 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1006 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1007 }
1008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001011{
1012 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001013 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001014 p->o_res.seqid = p->o_arg.seqid;
1015 p->c_res.seqid = p->c_arg.seqid;
1016 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001017 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001018 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001019 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001020}
1021
Al Viro82a2c1b2011-06-22 18:30:55 -04001022static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001023 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001024 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001025 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001026 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001027 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001028{
Al Viro82a2c1b2011-06-22 18:30:55 -04001029 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001030 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001031 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001032 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001033 struct nfs4_opendata *p;
1034
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001035 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001036 if (p == NULL)
1037 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001038
1039 p->f_label = nfs4_label_alloc(server, gfp_mask);
1040 if (IS_ERR(p->f_label))
1041 goto err_free_p;
1042
Kinglong Meea49c2692015-07-27 15:31:38 +08001043 p->a_label = nfs4_label_alloc(server, gfp_mask);
1044 if (IS_ERR(p->a_label))
1045 goto err_free_f;
1046
Trond Myklebust63f5f792015-01-23 19:19:25 -05001047 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1048 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001049 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001050 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001051 nfs_sb_active(dentry->d_sb);
1052 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001053 p->dir = parent;
1054 p->owner = sp;
1055 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 p->o_arg.open_flags = flags;
1057 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001058 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1059 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001060 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1061 * will return permission denied for all bits until close */
1062 if (!(flags & O_EXCL)) {
1063 /* ask server to check for all possible rights as results
1064 * are cached */
1065 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1066 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001067 }
David Howells7539bba2006-08-22 20:06:09 -04001068 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001069 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1070 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001071 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001072 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001073 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001074 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001075 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001076 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1077 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001078 case NFS4_OPEN_CLAIM_NULL:
1079 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1080 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1081 p->o_arg.fh = NFS_FH(dir);
1082 break;
1083 case NFS4_OPEN_CLAIM_PREVIOUS:
1084 case NFS4_OPEN_CLAIM_FH:
1085 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1086 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001087 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001088 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001089 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001090 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001091
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001092 p->o_arg.u.attrs = &p->attrs;
1093 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001094
1095 verf[0] = jiffies;
1096 verf[1] = current->pid;
1097 memcpy(p->o_arg.u.verifier.data, verf,
1098 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001099 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001100 p->c_arg.fh = &p->o_res.fh;
1101 p->c_arg.stateid = &p->o_res.stateid;
1102 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001103 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001104 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001105 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001106
1107err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001108 nfs4_label_free(p->a_label);
1109err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001110 nfs4_label_free(p->f_label);
1111err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112 kfree(p);
1113err:
1114 dput(parent);
1115 return NULL;
1116}
1117
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001118static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001119{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001120 struct nfs4_opendata *p = container_of(kref,
1121 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001122 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001123
1124 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001125 if (p->state != NULL)
1126 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001127 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001128
Kinglong Meea49c2692015-07-27 15:31:38 +08001129 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001130 nfs4_label_free(p->f_label);
1131
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001132 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001133 dput(p->dentry);
1134 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001135 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001136 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001137 kfree(p);
1138}
1139
1140static void nfs4_opendata_put(struct nfs4_opendata *p)
1141{
1142 if (p != NULL)
1143 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001144}
1145
Trond Myklebust06f814a2006-01-03 09:55:07 +01001146static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1147{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001148 int ret;
1149
Trond Myklebust06f814a2006-01-03 09:55:07 +01001150 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001151 return ret;
1152}
1153
Trond Myklebust24311f82015-09-20 10:50:17 -04001154static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1155 fmode_t fmode)
1156{
1157 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1158 case FMODE_READ|FMODE_WRITE:
1159 return state->n_rdwr != 0;
1160 case FMODE_WRITE:
1161 return state->n_wronly != 0;
1162 case FMODE_READ:
1163 return state->n_rdonly != 0;
1164 }
1165 WARN_ON_ONCE(1);
1166 return false;
1167}
1168
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001169static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001170{
1171 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001172
Trond Myklebust536e43d2012-01-17 22:04:26 -05001173 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001174 goto out;
1175 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001176 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001177 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1178 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001179 break;
1180 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001181 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1182 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001183 break;
1184 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001185 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1186 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001187 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001188out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001189 return ret;
1190}
1191
Trond Myklebust2a606182015-08-19 22:30:00 -05001192static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1193 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001194{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001195 if (delegation == NULL)
1196 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001197 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001198 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001199 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1200 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001201 switch (claim) {
1202 case NFS4_OPEN_CLAIM_NULL:
1203 case NFS4_OPEN_CLAIM_FH:
1204 break;
1205 case NFS4_OPEN_CLAIM_PREVIOUS:
1206 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1207 break;
1208 default:
1209 return 0;
1210 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001211 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001212 return 1;
1213}
1214
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001216{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001217 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001218 case FMODE_WRITE:
1219 state->n_wronly++;
1220 break;
1221 case FMODE_READ:
1222 state->n_rdonly++;
1223 break;
1224 case FMODE_READ|FMODE_WRITE:
1225 state->n_rdwr++;
1226 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001227 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001228}
1229
Trond Myklebust4f14c192014-02-12 19:15:06 -05001230static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001231{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001232 struct nfs_client *clp = state->owner->so_server->nfs_client;
1233 bool need_recover = false;
1234
1235 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1236 need_recover = true;
1237 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1238 need_recover = true;
1239 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1240 need_recover = true;
1241 if (need_recover)
1242 nfs4_state_mark_reclaim_nograce(clp, state);
1243}
1244
Trond Myklebuste999e802014-02-10 18:20:47 -05001245static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1246 nfs4_stateid *stateid)
1247{
1248 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1249 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001250 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1251 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001252 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001253 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001254 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1255 return true;
1256 return false;
1257}
1258
Trond Myklebustf95549c2015-01-23 18:06:09 -05001259static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1260{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001261 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1262 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001263 if (state->n_wronly)
1264 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1265 if (state->n_rdonly)
1266 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1267 if (state->n_rdwr)
1268 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001269 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001270}
1271
Trond Myklebust226056c2014-02-11 10:41:07 -05001272static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001273 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001274 nfs4_stateid *stateid, fmode_t fmode)
1275{
1276 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1277 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1278 case FMODE_WRITE:
1279 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1280 break;
1281 case FMODE_READ:
1282 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1283 break;
1284 case 0:
1285 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1286 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1287 clear_bit(NFS_OPEN_STATE, &state->flags);
1288 }
1289 if (stateid == NULL)
1290 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001291 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001292 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1293 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1294 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001295 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001296 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001297 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001298 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001299 nfs4_stateid_copy(&state->stateid, stateid);
1300 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001301}
1302
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001303static void nfs_clear_open_stateid(struct nfs4_state *state,
1304 nfs4_stateid *arg_stateid,
1305 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001306{
1307 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001308 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001309 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001310 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1311 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001312}
1313
Trond Myklebust003707c2007-07-05 18:07:55 -04001314static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1315{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001316 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001317 case FMODE_READ:
1318 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1319 break;
1320 case FMODE_WRITE:
1321 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1322 break;
1323 case FMODE_READ|FMODE_WRITE:
1324 set_bit(NFS_O_RDWR_STATE, &state->flags);
1325 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001326 if (!nfs_need_update_open_stateid(state, stateid))
1327 return;
1328 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1329 nfs4_stateid_copy(&state->stateid, stateid);
1330 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001331}
1332
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001333static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001335 /*
1336 * Protect the call to nfs4_state_set_mode_locked and
1337 * serialise the stateid update
1338 */
1339 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001340 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001341 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001342 set_bit(NFS_DELEGATED_STATE, &state->flags);
1343 }
1344 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001345 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001346 write_sequnlock(&state->seqlock);
1347 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001348 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001349 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001353{
1354 struct nfs_inode *nfsi = NFS_I(state->inode);
1355 struct nfs_delegation *deleg_cur;
1356 int ret = 0;
1357
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001359
1360 rcu_read_lock();
1361 deleg_cur = rcu_dereference(nfsi->delegation);
1362 if (deleg_cur == NULL)
1363 goto no_delegation;
1364
1365 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001366 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001367 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001368 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001369 goto no_delegation_unlock;
1370
1371 if (delegation == NULL)
1372 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001373 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001374 goto no_delegation_unlock;
1375
Trond Myklebustb7391f42008-12-23 15:21:52 -05001376 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001377 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001378 ret = 1;
1379no_delegation_unlock:
1380 spin_unlock(&deleg_cur->lock);
1381no_delegation:
1382 rcu_read_unlock();
1383
1384 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001385 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001386 ret = 1;
1387 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001388 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1389 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001390
1391 return ret;
1392}
1393
Trond Myklebust39071e62015-01-24 15:07:56 -05001394static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1395 const nfs4_stateid *stateid)
1396{
1397 struct nfs4_state *state = lsp->ls_state;
1398 bool ret = false;
1399
1400 spin_lock(&state->state_lock);
1401 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1402 goto out_noupdate;
1403 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1404 goto out_noupdate;
1405 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1406 ret = true;
1407out_noupdate:
1408 spin_unlock(&state->state_lock);
1409 return ret;
1410}
Trond Myklebust34310432008-12-23 15:21:38 -05001411
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001412static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001413{
1414 struct nfs_delegation *delegation;
1415
1416 rcu_read_lock();
1417 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001418 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001419 rcu_read_unlock();
1420 return;
1421 }
1422 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001423 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001424}
1425
Trond Myklebust6ee41262007-07-08 14:11:36 -04001426static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001427{
1428 struct nfs4_state *state = opendata->state;
1429 struct nfs_inode *nfsi = NFS_I(state->inode);
1430 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001431 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001432 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001433 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001434 nfs4_stateid stateid;
1435 int ret = -EAGAIN;
1436
Trond Myklebustaac00a82007-07-05 19:02:21 -04001437 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001438 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001439 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001440 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001441 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001442 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001443 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001444 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001445 rcu_read_lock();
1446 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001447 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001448 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001449 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001450 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001451 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001452 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001453 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001454 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001455 if (!opendata->is_recover) {
1456 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1457 if (ret != 0)
1458 goto out;
1459 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001460 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001461
1462 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001463 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001464 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001465 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001466out:
1467 return ERR_PTR(ret);
1468out_return_state:
1469 atomic_inc(&state->count);
1470 return state;
1471}
1472
Andy Adamsone23008e2012-10-02 21:07:32 -04001473static void
1474nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1475{
1476 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1477 struct nfs_delegation *delegation;
1478 int delegation_flags = 0;
1479
1480 rcu_read_lock();
1481 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1482 if (delegation)
1483 delegation_flags = delegation->flags;
1484 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001485 switch (data->o_arg.claim) {
1486 default:
1487 break;
1488 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1489 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001490 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1491 "returning a delegation for "
1492 "OPEN(CLAIM_DELEGATE_CUR)\n",
1493 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001494 return;
1495 }
1496 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001497 nfs_inode_set_delegation(state->inode,
1498 data->owner->so_cred,
1499 &data->o_res);
1500 else
1501 nfs_inode_reclaim_delegation(state->inode,
1502 data->owner->so_cred,
1503 &data->o_res);
1504}
1505
1506/*
1507 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1508 * and update the nfs4_state.
1509 */
1510static struct nfs4_state *
1511_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1512{
1513 struct inode *inode = data->state->inode;
1514 struct nfs4_state *state = data->state;
1515 int ret;
1516
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001517 if (!data->rpc_done) {
1518 if (data->rpc_status) {
1519 ret = data->rpc_status;
1520 goto err;
1521 }
1522 /* cached opens have already been processed */
1523 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001524 }
1525
Andy Adamsone23008e2012-10-02 21:07:32 -04001526 ret = nfs_refresh_inode(inode, &data->f_attr);
1527 if (ret)
1528 goto err;
1529
1530 if (data->o_res.delegation_type != 0)
1531 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001532update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001533 update_open_stateid(state, &data->o_res.stateid, NULL,
1534 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001535 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001536
1537 return state;
1538err:
1539 return ERR_PTR(ret);
1540
1541}
1542
1543static struct nfs4_state *
1544_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545{
1546 struct inode *inode;
1547 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001548 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549
Trond Myklebustaac00a82007-07-05 19:02:21 -04001550 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001551 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001552 goto out;
1553 }
1554
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001555 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001556 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001557 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001558 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001559 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001560 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001561 goto err;
1562 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001563 state = nfs4_get_open_state(inode, data->owner);
1564 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001565 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001566 if (data->o_res.delegation_type != 0)
1567 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001568 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001569 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001570 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001571out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001572 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001573 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001574err_put_inode:
1575 iput(inode);
1576err:
1577 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001578}
1579
Andy Adamsone23008e2012-10-02 21:07:32 -04001580static struct nfs4_state *
1581nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1582{
1583 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1584 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1585 return _nfs4_opendata_to_nfs4_state(data);
1586}
1587
Trond Myklebust864472e2006-01-03 09:55:15 +01001588static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1589{
1590 struct nfs_inode *nfsi = NFS_I(state->inode);
1591 struct nfs_open_context *ctx;
1592
1593 spin_lock(&state->inode->i_lock);
1594 list_for_each_entry(ctx, &nfsi->open_files, list) {
1595 if (ctx->state != state)
1596 continue;
1597 get_nfs_open_context(ctx);
1598 spin_unlock(&state->inode->i_lock);
1599 return ctx;
1600 }
1601 spin_unlock(&state->inode->i_lock);
1602 return ERR_PTR(-ENOENT);
1603}
1604
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001605static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1606 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001607{
1608 struct nfs4_opendata *opendata;
1609
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001610 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001611 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001612 if (opendata == NULL)
1613 return ERR_PTR(-ENOMEM);
1614 opendata->state = state;
1615 atomic_inc(&state->count);
1616 return opendata;
1617}
1618
Trond Myklebust24311f82015-09-20 10:50:17 -04001619static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1620 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001621{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001622 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001623 int ret;
1624
Trond Myklebust24311f82015-09-20 10:50:17 -04001625 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001626 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001627 opendata->o_arg.open_flags = 0;
1628 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001629 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1630 NFS_SB(opendata->dentry->d_sb),
1631 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001632 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1633 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1634 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001635 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001636 if (ret != 0)
1637 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001638 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001639 if (IS_ERR(newstate))
1640 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001641 if (newstate != opendata->state)
1642 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001643 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001644 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001645}
1646
1647static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1648{
Trond Myklebust864472e2006-01-03 09:55:15 +01001649 int ret;
1650
Trond Myklebust4f14c192014-02-12 19:15:06 -05001651 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1652 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1653 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1654 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001655 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001656 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001657 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001658 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001659 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1660 if (ret != 0)
1661 return ret;
1662 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1663 if (ret != 0)
1664 return ret;
1665 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1666 if (ret != 0)
1667 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001668 /*
1669 * We may have performed cached opens for all three recoveries.
1670 * Check if we need to update the current stateid.
1671 */
1672 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001673 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001674 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001675 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001676 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001677 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001678 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001679 return 0;
1680}
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682/*
1683 * OPEN_RECLAIM:
1684 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001686static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001688 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001689 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001690 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 int status;
1692
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001693 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1694 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001695 if (IS_ERR(opendata))
1696 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001697 rcu_read_lock();
1698 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001699 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001700 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001701 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001702 opendata->o_arg.u.delegation_type = delegation_type;
1703 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001704 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return status;
1706}
1707
Trond Myklebust539cd032007-06-05 11:46:42 -04001708static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 struct nfs_server *server = NFS_SERVER(state->inode);
1711 struct nfs4_exception exception = { };
1712 int err;
1713 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001714 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001715 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001716 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1717 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001718 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001719 break;
1720 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 } while (exception.retry);
1722 return err;
1723}
1724
Trond Myklebust864472e2006-01-03 09:55:15 +01001725static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1726{
1727 struct nfs_open_context *ctx;
1728 int ret;
1729
1730 ctx = nfs4_state_find_open_context(state);
1731 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001732 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001733 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001734 put_nfs_open_context(ctx);
1735 return ret;
1736}
1737
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001738static 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 -07001739{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001740 switch (err) {
1741 default:
1742 printk(KERN_ERR "NFS: %s: unhandled error "
1743 "%d.\n", __func__, err);
1744 case 0:
1745 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001746 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001747 case -ESTALE:
1748 break;
1749 case -NFS4ERR_BADSESSION:
1750 case -NFS4ERR_BADSLOT:
1751 case -NFS4ERR_BAD_HIGH_SLOT:
1752 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1753 case -NFS4ERR_DEADSESSION:
1754 set_bit(NFS_DELEGATED_STATE, &state->flags);
1755 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1756 return -EAGAIN;
1757 case -NFS4ERR_STALE_CLIENTID:
1758 case -NFS4ERR_STALE_STATEID:
1759 set_bit(NFS_DELEGATED_STATE, &state->flags);
1760 case -NFS4ERR_EXPIRED:
1761 /* Don't recall a delegation if it was lost */
1762 nfs4_schedule_lease_recovery(server->nfs_client);
1763 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001764 case -NFS4ERR_MOVED:
1765 nfs4_schedule_migration_recovery(server);
1766 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001767 case -NFS4ERR_LEASE_MOVED:
1768 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1769 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001770 case -NFS4ERR_DELEG_REVOKED:
1771 case -NFS4ERR_ADMIN_REVOKED:
1772 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001773 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001774 nfs_inode_find_state_and_recover(state->inode,
1775 stateid);
1776 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001777 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001778 case -NFS4ERR_DELAY:
1779 case -NFS4ERR_GRACE:
1780 set_bit(NFS_DELEGATED_STATE, &state->flags);
1781 ssleep(1);
1782 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001783 case -ENOMEM:
1784 case -NFS4ERR_DENIED:
1785 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1786 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return err;
1789}
1790
Trond Myklebust24311f82015-09-20 10:50:17 -04001791int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1792 struct nfs4_state *state, const nfs4_stateid *stateid,
1793 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001794{
1795 struct nfs_server *server = NFS_SERVER(state->inode);
1796 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001797 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001798
1799 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1800 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1801 if (IS_ERR(opendata))
1802 return PTR_ERR(opendata);
1803 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001804 write_seqlock(&state->seqlock);
1805 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1806 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001807 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1808 switch (type & (FMODE_READ|FMODE_WRITE)) {
1809 case FMODE_READ|FMODE_WRITE:
1810 case FMODE_WRITE:
1811 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1812 if (err)
1813 break;
1814 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1815 if (err)
1816 break;
1817 case FMODE_READ:
1818 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1819 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001820 nfs4_opendata_put(opendata);
1821 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1822}
1823
Chuck Leverbe05c862013-08-09 12:49:47 -04001824static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1825{
1826 struct nfs4_opendata *data = calldata;
1827
Peng Taocb04ad22014-06-11 05:24:15 +08001828 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1829 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001830}
1831
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001832static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1833{
1834 struct nfs4_opendata *data = calldata;
1835
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001836 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001837
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001838 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001839 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001840 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001841 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001842 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001843 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001844 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001845}
1846
1847static void nfs4_open_confirm_release(void *calldata)
1848{
1849 struct nfs4_opendata *data = calldata;
1850 struct nfs4_state *state = NULL;
1851
1852 /* If this request hasn't been cancelled, do nothing */
1853 if (data->cancelled == 0)
1854 goto out_free;
1855 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001856 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001857 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001858 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001859 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001860 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001861out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001862 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001863}
1864
1865static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001866 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001867 .rpc_call_done = nfs4_open_confirm_done,
1868 .rpc_release = nfs4_open_confirm_release,
1869};
1870
1871/*
1872 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1873 */
1874static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1875{
David Howells2b0143b2015-03-17 22:25:59 +00001876 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001877 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001878 struct rpc_message msg = {
1879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1880 .rpc_argp = &data->c_arg,
1881 .rpc_resp = &data->c_res,
1882 .rpc_cred = data->owner->so_cred,
1883 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001884 struct rpc_task_setup task_setup_data = {
1885 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001886 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001887 .callback_ops = &nfs4_open_confirm_ops,
1888 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001889 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001890 .flags = RPC_TASK_ASYNC,
1891 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 int status;
1893
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001894 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001895 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001896 data->rpc_done = 0;
1897 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001898 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04001899 if (data->is_recover)
1900 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001901 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001902 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001903 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001904 status = nfs4_wait_for_completion_rpc_task(task);
1905 if (status != 0) {
1906 data->cancelled = 1;
1907 smp_wmb();
1908 } else
1909 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001910 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return status;
1912}
1913
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001914static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001916 struct nfs4_opendata *data = calldata;
1917 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001918 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05001919 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001920
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001921 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001922 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001923 /*
1924 * Check if we still need to send an OPEN call, or if we can use
1925 * a delegation instead.
1926 */
1927 if (data->state != NULL) {
1928 struct nfs_delegation *delegation;
1929
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001930 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001931 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001932 rcu_read_lock();
1933 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001934 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05001935 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001936 rcu_read_unlock();
1937 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001938 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001939 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05001940 switch (claim) {
1941 default:
1942 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04001943 case NFS4_OPEN_CLAIM_PREVIOUS:
1944 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1945 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001946 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001947 case NFS4_OPEN_CLAIM_FH:
1948 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001949 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1950 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001951 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001952 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001953 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001954 &data->o_res.seq_res,
1955 task) != 0)
1956 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001957
1958 /* Set the create mode (note dependency on the session type) */
1959 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1960 if (data->o_arg.open_flags & O_EXCL) {
1961 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1962 if (nfs4_has_persistent_session(clp))
1963 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1964 else if (clp->cl_mvops->minor_version > 0)
1965 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1966 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001967 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001968unlock_no_action:
1969 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001970out_no_action:
1971 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001972out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001973 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001974}
1975
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001976static void nfs4_open_done(struct rpc_task *task, void *calldata)
1977{
1978 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001980 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001981
Trond Myklebust14516c32010-07-31 14:29:06 -04001982 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1983 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001984
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001985 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001986 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1987 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001988 case S_IFREG:
1989 break;
1990 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001991 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001992 break;
1993 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001994 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001995 break;
1996 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001997 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001998 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001999 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002000 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002001 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2002 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002003 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002004 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002005}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002006
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002007static void nfs4_open_release(void *calldata)
2008{
2009 struct nfs4_opendata *data = calldata;
2010 struct nfs4_state *state = NULL;
2011
2012 /* If this request hasn't been cancelled, do nothing */
2013 if (data->cancelled == 0)
2014 goto out_free;
2015 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002016 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002017 goto out_free;
2018 /* In case we need an open_confirm, no cleanup! */
2019 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2020 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002021 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002022 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002023 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002024out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002025 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002026}
2027
2028static const struct rpc_call_ops nfs4_open_ops = {
2029 .rpc_call_prepare = nfs4_open_prepare,
2030 .rpc_call_done = nfs4_open_done,
2031 .rpc_release = nfs4_open_release,
2032};
2033
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002034static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002035{
David Howells2b0143b2015-03-17 22:25:59 +00002036 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002037 struct nfs_server *server = NFS_SERVER(dir);
2038 struct nfs_openargs *o_arg = &data->o_arg;
2039 struct nfs_openres *o_res = &data->o_res;
2040 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002041 struct rpc_message msg = {
2042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2043 .rpc_argp = o_arg,
2044 .rpc_resp = o_res,
2045 .rpc_cred = data->owner->so_cred,
2046 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002047 struct rpc_task_setup task_setup_data = {
2048 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002049 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002050 .callback_ops = &nfs4_open_ops,
2051 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002052 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002053 .flags = RPC_TASK_ASYNC,
2054 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002055 int status;
2056
Chuck Levera9c92d62013-08-09 12:48:18 -04002057 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002058 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002059 data->rpc_done = 0;
2060 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002061 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002062 data->is_recover = 0;
2063 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002064 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002065 data->is_recover = 1;
2066 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002067 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002068 if (IS_ERR(task))
2069 return PTR_ERR(task);
2070 status = nfs4_wait_for_completion_rpc_task(task);
2071 if (status != 0) {
2072 data->cancelled = 1;
2073 smp_wmb();
2074 } else
2075 status = data->rpc_status;
2076 rpc_put_task(task);
2077
2078 return status;
2079}
2080
2081static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2082{
David Howells2b0143b2015-03-17 22:25:59 +00002083 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002084 struct nfs_openres *o_res = &data->o_res;
2085 int status;
2086
2087 status = nfs4_run_open_task(data, 1);
2088 if (status != 0 || !data->rpc_done)
2089 return status;
2090
Trond Myklebust6926afd2012-01-07 13:22:46 -05002091 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2092
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002093 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2094 status = _nfs4_proc_open_confirm(data);
2095 if (status != 0)
2096 return status;
2097 }
2098
2099 return status;
2100}
2101
Trond Myklebustf3792d62014-07-10 08:54:32 -04002102/*
2103 * Additional permission checks in order to distinguish between an
2104 * open for read, and an open for execute. This works around the
2105 * fact that NFSv4 OPEN treats read and execute permissions as being
2106 * the same.
2107 * Note that in the non-execute case, we want to turn off permission
2108 * checking if we just created a new file (POSIX open() semantics).
2109 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002110static int nfs4_opendata_access(struct rpc_cred *cred,
2111 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002112 struct nfs4_state *state, fmode_t fmode,
2113 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002114{
2115 struct nfs_access_entry cache;
2116 u32 mask;
2117
2118 /* access call failed or for some reason the server doesn't
2119 * support any access modes -- defer access call until later */
2120 if (opendata->o_res.access_supported == 0)
2121 return 0;
2122
2123 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002124 /*
2125 * Use openflags to check for exec, because fmode won't
2126 * always have FMODE_EXEC set when file open for exec.
2127 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002128 if (openflags & __FMODE_EXEC) {
2129 /* ONLY check for exec rights */
2130 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002131 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002132 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002133
2134 cache.cred = cred;
2135 cache.jiffies = jiffies;
2136 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2137 nfs_access_add_cache(state->inode, &cache);
2138
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002139 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002140 return 0;
2141
2142 /* even though OPEN succeeded, access is denied. Close the file */
2143 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002144 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002145}
2146
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002147/*
2148 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2149 */
2150static int _nfs4_proc_open(struct nfs4_opendata *data)
2151{
David Howells2b0143b2015-03-17 22:25:59 +00002152 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002153 struct nfs_server *server = NFS_SERVER(dir);
2154 struct nfs_openargs *o_arg = &data->o_arg;
2155 struct nfs_openres *o_res = &data->o_res;
2156 int status;
2157
2158 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002159 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002160 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002161 if (status != 0) {
2162 if (status == -NFS4ERR_BADNAME &&
2163 !(o_arg->open_flags & O_CREAT))
2164 return -ENOENT;
2165 return status;
2166 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002167
Trond Myklebust6926afd2012-01-07 13:22:46 -05002168 nfs_fattr_map_and_free_names(server, &data->f_attr);
2169
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002170 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002171 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002172 if (o_arg->open_flags & O_EXCL)
2173 data->file_created = 1;
2174 else if (o_res->cinfo.before != o_res->cinfo.after)
2175 data->file_created = 1;
2176 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002177 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2178 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002180 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002182 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002185 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
Andy Adamsond83217c2011-03-01 01:34:17 +00002189static int nfs4_recover_expired_lease(struct nfs_server *server)
2190{
2191 return nfs4_client_recover_expired_lease(server->nfs_client);
2192}
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194/*
2195 * OPEN_EXPIRED:
2196 * reclaim state on the server after a network partition.
2197 * Assumes caller holds the appropriate lock
2198 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002199static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002201 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002202 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002204 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002205 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002206 if (IS_ERR(opendata))
2207 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002208 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002209 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002210 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002211 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002212 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002215static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002216{
Trond Myklebust539cd032007-06-05 11:46:42 -04002217 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002218 struct nfs4_exception exception = { };
2219 int err;
2220
2221 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002222 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002223 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002224 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2225 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002226 switch (err) {
2227 default:
2228 goto out;
2229 case -NFS4ERR_GRACE:
2230 case -NFS4ERR_DELAY:
2231 nfs4_handle_exception(server, err, &exception);
2232 err = 0;
2233 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002234 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002235out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002236 return err;
2237}
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2240{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002242 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Trond Myklebust864472e2006-01-03 09:55:15 +01002244 ctx = nfs4_state_find_open_context(state);
2245 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002246 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002247 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002248 put_nfs_open_context(ctx);
2249 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002252static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2253{
2254 nfs_remove_bad_delegation(state->inode);
2255 write_seqlock(&state->seqlock);
2256 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2257 write_sequnlock(&state->seqlock);
2258 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2259}
2260
2261static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2262{
2263 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2264 nfs_finish_clear_delegation_stateid(state);
2265}
2266
2267static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2268{
2269 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2270 nfs40_clear_delegation_stateid(state);
2271 return nfs4_open_expired(sp, state);
2272}
2273
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002274#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002275static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002276{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002277 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002278 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002279 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002280 struct rpc_cred *cred;
2281 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002282
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002283 /* Get the delegation credential for use by test/free_stateid */
2284 rcu_read_lock();
2285 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002286 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002287 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002288 return;
2289 }
2290
2291 nfs4_stateid_copy(&stateid, &delegation->stateid);
2292 cred = get_rpccred(delegation->cred);
2293 rcu_read_unlock();
2294 status = nfs41_test_stateid(server, &stateid, cred);
2295 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002296
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002297 if (status != NFS_OK) {
2298 /* Free the stateid unless the server explicitly
2299 * informs us the stateid is unrecognized. */
2300 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002301 nfs41_free_stateid(server, &stateid, cred);
2302 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002303 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002304
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002305 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002306}
2307
2308/**
2309 * nfs41_check_open_stateid - possibly free an open stateid
2310 *
2311 * @state: NFSv4 state for an inode
2312 *
2313 * Returns NFS_OK if recovery for this stateid is now finished.
2314 * Otherwise a negative NFS4ERR value is returned.
2315 */
2316static int nfs41_check_open_stateid(struct nfs4_state *state)
2317{
2318 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002319 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002320 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002321 int status;
2322
2323 /* If a state reset has been done, test_stateid is unneeded */
2324 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2325 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2326 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2327 return -NFS4ERR_BAD_STATEID;
2328
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002329 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002330 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002331 if (status != NFS_OK) {
2332 /* Free the stateid unless the server explicitly
2333 * informs us the stateid is unrecognized. */
2334 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002335 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002336
2337 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2338 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2339 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002340 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002341 }
2342 return status;
2343}
2344
2345static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2346{
Chuck Levereb64cf92012-07-11 16:30:05 -04002347 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002348
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002349 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002350 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002351 if (status != NFS_OK)
2352 status = nfs4_open_expired(sp, state);
2353 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002354}
2355#endif
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002358 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2359 * fields corresponding to attributes that were used to store the verifier.
2360 * Make sure we clobber those fields in the later setattr call
2361 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002362static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2363 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002364{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002365 const u32 *attrset = opendata->o_res.attrset;
2366
2367 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002368 !(sattr->ia_valid & ATTR_ATIME_SET))
2369 sattr->ia_valid |= ATTR_ATIME;
2370
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002371 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002372 !(sattr->ia_valid & ATTR_MTIME_SET))
2373 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002374
2375 /* Except MODE, it seems harmless of setting twice. */
2376 if ((attrset[1] & FATTR4_WORD1_MODE))
2377 sattr->ia_valid &= ~ATTR_MODE;
2378
2379 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2380 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002381}
2382
Trond Myklebustc21443c2013-02-07 14:26:21 -05002383static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2384 fmode_t fmode,
2385 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002386 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002387{
2388 struct nfs4_state_owner *sp = opendata->owner;
2389 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002390 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002391 struct nfs4_state *state;
2392 unsigned int seq;
2393 int ret;
2394
2395 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2396
2397 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002398 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002399 goto out;
2400
2401 state = nfs4_opendata_to_nfs4_state(opendata);
2402 ret = PTR_ERR(state);
2403 if (IS_ERR(state))
2404 goto out;
2405 if (server->caps & NFS_CAP_POSIX_LOCK)
2406 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2407
Trond Myklebust275bb302013-05-29 13:11:28 -04002408 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002409 if (d_really_is_negative(dentry)) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002410 /* FIXME: Is this d_drop() ever needed? */
2411 d_drop(dentry);
2412 dentry = d_add_unique(dentry, igrab(state->inode));
2413 if (dentry == NULL) {
2414 dentry = opendata->dentry;
2415 } else if (dentry != ctx->dentry) {
2416 dput(ctx->dentry);
2417 ctx->dentry = dget(dentry);
2418 }
2419 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002420 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002421 }
2422
Trond Myklebustc21443c2013-02-07 14:26:21 -05002423 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2424 if (ret != 0)
2425 goto out;
2426
Trond Myklebust3efb9722013-05-29 13:17:04 -04002427 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002428 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002429 nfs_inode_attach_open_context(ctx);
2430 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2431 nfs4_schedule_stateid_recovery(server, state);
2432 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002433out:
2434 return ret;
2435}
2436
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002437/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002438 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 */
Andy Adamson82be4172012-05-23 05:02:35 -04002440static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002441 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002442 int flags,
2443 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002444 struct nfs4_label *label,
2445 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
2447 struct nfs4_state_owner *sp;
2448 struct nfs4_state *state = NULL;
2449 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002450 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002451 struct dentry *dentry = ctx->dentry;
2452 struct rpc_cred *cred = ctx->cred;
2453 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2454 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002455 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002456 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002457 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002461 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2462 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2464 goto out_err;
2465 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002466 status = nfs4_recover_expired_lease(server);
2467 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002468 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002469 if (d_really_is_positive(dentry))
2470 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002471 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002472 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002473 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002474 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002475 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002476 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002477 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
David Quigley14c43f72013-05-22 12:50:43 -04002479 if (label) {
2480 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2481 if (IS_ERR(olabel)) {
2482 status = PTR_ERR(olabel);
2483 goto err_opendata_put;
2484 }
2485 }
2486
Trond Myklebuste911b812014-03-26 13:24:37 -07002487 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2488 if (!opendata->f_attr.mdsthreshold) {
2489 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2490 if (!opendata->f_attr.mdsthreshold)
2491 goto err_free_label;
2492 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002493 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002494 }
David Howells2b0143b2015-03-17 22:25:59 +00002495 if (d_really_is_positive(dentry))
2496 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002497
Trond Myklebust3efb9722013-05-29 13:17:04 -04002498 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002499 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002500 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002501 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002502
NeilBrownefcbc042015-07-30 13:00:56 +10002503 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002504 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002505 nfs4_exclusive_attrset(opendata, sattr, &label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002506
2507 nfs_fattr_init(opendata->o_res.f_attr);
2508 status = nfs4_do_setattr(state->inode, cred,
2509 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002510 state, label, olabel);
2511 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05002512 nfs_setattr_update_inode(state->inode, sattr,
2513 opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002514 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002515 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002516 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002517 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002518 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002519
Trond Myklebuste911b812014-03-26 13:24:37 -07002520 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002521 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002522 opendata->f_attr.mdsthreshold = NULL;
2523 }
Andy Adamson82be4172012-05-23 05:02:35 -04002524
David Quigley14c43f72013-05-22 12:50:43 -04002525 nfs4_label_free(olabel);
2526
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002527 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002530err_free_label:
2531 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002532err_opendata_put:
2533 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002534err_put_state_owner:
2535 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return status;
2538}
2539
2540
Andy Adamson82be4172012-05-23 05:02:35 -04002541static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002542 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002543 int flags,
2544 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002545 struct nfs4_label *label,
2546 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002548 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 struct nfs4_exception exception = { };
2550 struct nfs4_state *res;
2551 int status;
2552
2553 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002554 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002555 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002556 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (status == 0)
2558 break;
2559 /* NOTE: BAD_SEQID means the server and client disagree about the
2560 * book-keeping w.r.t. state-changing operations
2561 * (OPEN/CLOSE/LOCK/LOCKU...)
2562 * It is actually a sign of a bug on the client or on the server.
2563 *
2564 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002565 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 * have unhashed the old state_owner for us, and that we can
2567 * therefore safely retry using a new one. We should still warn
2568 * the user though...
2569 */
2570 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002571 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002572 " returned a bad sequence-id error!\n",
2573 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 exception.retry = 1;
2575 continue;
2576 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002577 /*
2578 * BAD_STATEID on OPEN means that the server cancelled our
2579 * state before it received the OPEN_CONFIRM.
2580 * Recover by retrying the request as per the discussion
2581 * on Page 181 of RFC3530.
2582 */
2583 if (status == -NFS4ERR_BAD_STATEID) {
2584 exception.retry = 1;
2585 continue;
2586 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002587 if (status == -EAGAIN) {
2588 /* We must have found a delegation */
2589 exception.retry = 1;
2590 continue;
2591 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002592 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2593 continue;
2594 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 status, &exception));
2596 } while (exception.retry);
2597 return res;
2598}
2599
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002600static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2601 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002602 struct nfs4_state *state, struct nfs4_label *ilabel,
2603 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002605 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002607 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 .iap = sattr,
2609 .server = server,
2610 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002611 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 };
2613 struct nfs_setattrres res = {
2614 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002615 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 .server = server,
2617 };
2618 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2620 .rpc_argp = &arg,
2621 .rpc_resp = &res,
2622 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002624 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002625 fmode_t fmode;
2626 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002627 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
David Quigleyaa9c2662013-05-22 12:50:44 -04002629 arg.bitmask = nfs4_bitmask(server, ilabel);
2630 if (ilabel)
2631 arg.bitmask = nfs4_bitmask(server, olabel);
2632
Trond Myklebust0e574af2005-10-27 22:12:38 -04002633 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Trond Myklebustee3ae842013-04-29 10:35:36 -04002635 /* Servers should only apply open mode checks for file size changes */
2636 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2637 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2638
2639 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2640 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002641 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002642 struct nfs_lockowner lockowner = {
2643 .l_owner = current->files,
2644 .l_pid = current->tgid,
2645 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002646 if (!nfs4_valid_open_stateid(state))
2647 return -EBADF;
2648 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2649 &lockowner) == -EIO)
2650 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002651 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002652 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Bryan Schumaker7c513052011-03-24 17:12:24 +00002654 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002655 if (status == 0 && state != NULL)
2656 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002657 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002660static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2661 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002662 struct nfs4_state *state, struct nfs4_label *ilabel,
2663 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002665 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002666 struct nfs4_exception exception = {
2667 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002668 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002669 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 int err;
2671 do {
David Quigley1775fd32013-05-22 12:50:42 -04002672 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002673 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002674 switch (err) {
2675 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002676 if (!(sattr->ia_valid & ATTR_SIZE)) {
2677 pr_warn_once("NFSv4: server %s is incorrectly "
2678 "applying open mode checks to "
2679 "a SETATTR that is not "
2680 "changing file size.\n",
2681 server->nfs_client->cl_hostname);
2682 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002683 if (state && !(state->state & FMODE_WRITE)) {
2684 err = -EBADF;
2685 if (sattr->ia_valid & ATTR_OPEN)
2686 err = -EACCES;
2687 goto out;
2688 }
2689 }
2690 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002692out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return err;
2694}
2695
Peng Tao500d7012015-09-22 11:35:22 +08002696static bool
2697nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2698{
2699 if (inode == NULL || !nfs_have_layout(inode))
2700 return false;
2701
2702 return pnfs_wait_on_layoutreturn(inode, task);
2703}
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705struct nfs4_closedata {
2706 struct inode *inode;
2707 struct nfs4_state *state;
2708 struct nfs_closeargs arg;
2709 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002710 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002711 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002712 bool roc;
2713 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714};
2715
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002716static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002717{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002718 struct nfs4_closedata *calldata = data;
2719 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002720 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002721
Fred Isamanf7e89172011-01-06 11:36:32 +00002722 if (calldata->roc)
2723 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002724 nfs4_put_open_state(calldata->state);
2725 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002726 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002727 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002728 kfree(calldata);
2729}
2730
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002731static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002733 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002736 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Chuck Levera3ca5652012-03-01 17:00:40 -05002738 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002739 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2740 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002741 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /* hmm. we are done with the inode, and in the process of freeing
2743 * the state_owner. we keep this around to process errors
2744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 switch (task->tk_status) {
2746 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002747 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002748 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002749 pnfs_roc_set_barrier(state->inode,
2750 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002751 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002752 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002753 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002755 case -NFS4ERR_OLD_STATEID:
2756 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002758 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002759 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002760 rpc_restart_call_prepare(task);
2761 goto out_release;
2762 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002763 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002766 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002767 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002768 goto out_release;
2769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002771 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2772 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002773out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002774 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002775 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002776 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002779static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002781 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002782 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002783 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002784 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002785 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002786
Chuck Levera3ca5652012-03-01 17:00:40 -05002787 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002788 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002789 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002790
Trond Myklebust88069f72009-12-08 08:33:16 -05002791 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002792 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002793 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2794 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2795 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002796 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002797 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002798 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002799 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002800 if (state->n_rdonly == 0)
2801 call_close |= is_rdonly;
2802 else if (is_rdonly)
2803 calldata->arg.fmode |= FMODE_READ;
2804 if (state->n_wronly == 0)
2805 call_close |= is_wronly;
2806 else if (is_wronly)
2807 calldata->arg.fmode |= FMODE_WRITE;
2808 } else if (is_rdwr)
2809 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2810
2811 if (calldata->arg.fmode == 0)
2812 call_close |= is_rdwr;
2813
Trond Myklebust5d422302013-03-14 16:57:48 -04002814 if (!nfs4_valid_open_stateid(state))
2815 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002816 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002817
2818 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002819 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002820 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002821 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002822
Peng Tao500d7012015-09-22 11:35:22 +08002823 if (nfs4_wait_on_layoutreturn(inode, task)) {
2824 nfs_release_seqid(calldata->arg.seqid);
2825 goto out_wait;
2826 }
2827
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002828 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002829 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002830 if (calldata->roc)
2831 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2832
Trond Myklebust6ae37332015-01-30 14:21:14 -05002833 calldata->arg.share_access =
2834 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2835 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002836
Trond Myklebust516a6af2005-10-27 22:12:41 -04002837 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002838 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002839 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002840 &calldata->arg.seq_args,
2841 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002842 task) != 0)
2843 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002844 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002845 return;
2846out_no_action:
2847 task->tk_action = NULL;
2848out_wait:
2849 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002852static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002853 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002854 .rpc_call_done = nfs4_close_done,
2855 .rpc_release = nfs4_free_closedata,
2856};
2857
Peng Taofe08c542014-07-03 13:05:01 +08002858static bool nfs4_roc(struct inode *inode)
2859{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002860 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002861 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002862 return pnfs_roc(inode);
2863}
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865/*
2866 * It is possible for data to be read/written from a mem-mapped file
2867 * after the sys_close call (which hits the vfs layer as a flush).
2868 * This means that we can't safely call nfsv4 close on a file until
2869 * the inode is cleared. This in turn means that we are not good
2870 * NFSv4 citizens - we do not indicate to the server to update the file's
2871 * share state even when we are done with one of the three share
2872 * stateid's in the inode.
2873 *
2874 * NOTE: Caller must be holding the sp->so_owner semaphore!
2875 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002876int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002878 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002879 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002881 struct nfs4_state_owner *sp = state->owner;
2882 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002883 struct rpc_message msg = {
2884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2885 .rpc_cred = state->owner->so_cred,
2886 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002887 struct rpc_task_setup task_setup_data = {
2888 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002889 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002890 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002891 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002892 .flags = RPC_TASK_ASYNC,
2893 };
Trond Myklebust95121352005-10-18 14:20:12 -07002894 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002896 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2897 &task_setup_data.rpc_client, &msg);
2898
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002899 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002901 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002902 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002903 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002905 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002907 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2908 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002909 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002910 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002911 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002912 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002913 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002914 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002915 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002916 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002917 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002918
Trond Myklebust1174dd12010-12-21 10:52:24 -05002919 msg.rpc_argp = &calldata->arg;
2920 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002921 task_setup_data.callback_data = calldata;
2922 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002923 if (IS_ERR(task))
2924 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002925 status = 0;
2926 if (wait)
2927 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002928 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002929 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002930out_free_calldata:
2931 kfree(calldata);
2932out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002933 nfs4_put_open_state(state);
2934 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002935 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936}
2937
Trond Myklebust2b484292010-09-17 10:56:51 -04002938static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002939nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2940 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002943 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2944
2945 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Trond Myklebust565277f2007-10-15 18:17:53 -04002947 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002948 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002949
2950 nfs4_label_release_security(label);
2951
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002952 if (IS_ERR(state))
2953 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002954 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956
Trond Myklebust1185a552009-12-03 15:54:02 -05002957static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002958{
2959 if (ctx->state == NULL)
2960 return;
2961 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002962 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002963 else
Al Viro643168c2011-06-22 18:20:23 -04002964 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002965}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Trond Myklebustb944dba2013-11-04 15:20:20 -05002967#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2968#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002969#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2972{
Kinglong Mee8c612822015-08-26 21:12:58 +08002973 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04002974 struct nfs4_server_caps_arg args = {
2975 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08002976 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04002977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 struct nfs4_server_caps_res res = {};
2979 struct rpc_message msg = {
2980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002981 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 .rpc_resp = &res,
2983 };
2984 int status;
2985
Kinglong Mee8c612822015-08-26 21:12:58 +08002986 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
2987 FATTR4_WORD0_FH_EXPIRE_TYPE |
2988 FATTR4_WORD0_LINK_SUPPORT |
2989 FATTR4_WORD0_SYMLINK_SUPPORT |
2990 FATTR4_WORD0_ACLSUPPORT;
2991 if (minorversion)
2992 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
2993
Bryan Schumaker7c513052011-03-24 17:12:24 +00002994 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002996 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08002997 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002998 case 0:
2999 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3000 res.attr_bitmask[2] = 0;
3001 break;
3002 case 1:
3003 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3004 break;
3005 case 2:
3006 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003009 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3010 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3011 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3012 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003013 NFS_CAP_CTIME|NFS_CAP_MTIME|
3014 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003015 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3016 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 server->caps |= NFS_CAP_ACLS;
3018 if (res.has_links != 0)
3019 server->caps |= NFS_CAP_HARDLINKS;
3020 if (res.has_symlinks != 0)
3021 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003022 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3023 server->caps |= NFS_CAP_FILEID;
3024 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3025 server->caps |= NFS_CAP_MODE;
3026 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3027 server->caps |= NFS_CAP_NLINK;
3028 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3029 server->caps |= NFS_CAP_OWNER;
3030 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3031 server->caps |= NFS_CAP_OWNER_GROUP;
3032 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3033 server->caps |= NFS_CAP_ATIME;
3034 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3035 server->caps |= NFS_CAP_CTIME;
3036 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3037 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003038#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3039 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3040 server->caps |= NFS_CAP_SECURITY_LABEL;
3041#endif
3042 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3043 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003044 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003045
Trond Myklebusta65318b2009-03-11 14:10:28 -04003046 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3047 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3048 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003049 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003050 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3051 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003053 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return status;
3057}
3058
Trond Myklebust55a97592006-06-09 09:34:19 -04003059int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
3061 struct nfs4_exception exception = { };
3062 int err;
3063 do {
3064 err = nfs4_handle_exception(server,
3065 _nfs4_server_capabilities(server, fhandle),
3066 &exception);
3067 } while (exception.retry);
3068 return err;
3069}
3070
3071static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3072 struct nfs_fsinfo *info)
3073{
David Quigleyaa9c2662013-05-22 12:50:44 -04003074 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003076 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 };
3078 struct nfs4_lookup_res res = {
3079 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003080 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 .fh = fhandle,
3082 };
3083 struct rpc_message msg = {
3084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3085 .rpc_argp = &args,
3086 .rpc_resp = &res,
3087 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003088
David Quigleyaa9c2662013-05-22 12:50:44 -04003089 bitmask[0] = nfs4_fattr_bitmap[0];
3090 bitmask[1] = nfs4_fattr_bitmap[1];
3091 /*
3092 * Process the label in the upcoming getfattr
3093 */
3094 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3095
Trond Myklebust0e574af2005-10-27 22:12:38 -04003096 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003097 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098}
3099
3100static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3101 struct nfs_fsinfo *info)
3102{
3103 struct nfs4_exception exception = { };
3104 int err;
3105 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003106 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003107 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003108 switch (err) {
3109 case 0:
3110 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003111 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003112 default:
3113 err = nfs4_handle_exception(server, err, &exception);
3114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003116out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return err;
3118}
3119
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003120static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3121 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3122{
Trond Myklebustc2190662013-08-26 19:23:04 -04003123 struct rpc_auth_create_args auth_args = {
3124 .pseudoflavor = flavor,
3125 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003126 struct rpc_auth *auth;
3127 int ret;
3128
Trond Myklebustc2190662013-08-26 19:23:04 -04003129 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003130 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003131 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003132 goto out;
3133 }
3134 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003135out:
3136 return ret;
3137}
3138
Chuck Lever9a744ba2013-03-16 15:56:02 -04003139/*
3140 * Retry pseudoroot lookup with various security flavors. We do this when:
3141 *
3142 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3143 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3144 *
3145 * Returns zero on success, or a negative NFS4ERR value, or a
3146 * negative errno value.
3147 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003148static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003149 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003151 /* Per 3530bis 15.33.5 */
3152 static const rpc_authflavor_t flav_array[] = {
3153 RPC_AUTH_GSS_KRB5P,
3154 RPC_AUTH_GSS_KRB5I,
3155 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003156 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003157 RPC_AUTH_NULL,
3158 };
3159 int status = -EPERM;
3160 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003162 if (server->auth_info.flavor_len > 0) {
3163 /* try each flavor specified by user */
3164 for (i = 0; i < server->auth_info.flavor_len; i++) {
3165 status = nfs4_lookup_root_sec(server, fhandle, info,
3166 server->auth_info.flavors[i]);
3167 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3168 continue;
3169 break;
3170 }
3171 } else {
3172 /* no flavors specified by user, try default list */
3173 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3174 status = nfs4_lookup_root_sec(server, fhandle, info,
3175 flav_array[i]);
3176 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3177 continue;
3178 break;
3179 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003180 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003181
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003182 /*
3183 * -EACCESS could mean that the user doesn't have correct permissions
3184 * to access the mount. It could also mean that we tried to mount
3185 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3186 * existing mount programs don't handle -EACCES very well so it should
3187 * be mapped to -EPERM instead.
3188 */
3189 if (status == -EACCES)
3190 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003191 return status;
3192}
3193
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003194static int nfs4_do_find_root_sec(struct nfs_server *server,
3195 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3196{
3197 int mv = server->nfs_client->cl_minorversion;
3198 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3199}
3200
3201/**
3202 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3203 * @server: initialized nfs_server handle
3204 * @fhandle: we fill in the pseudo-fs root file handle
3205 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003206 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003207 *
3208 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003209 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003210int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003211 struct nfs_fsinfo *info,
3212 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003213{
Andre Przywarac7757072015-04-23 17:17:40 +01003214 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003215
Andre Przywarac7757072015-04-23 17:17:40 +01003216 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003217 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003218
3219 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003220 status = nfs4_do_find_root_sec(server, fhandle, info);
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 if (status == 0)
3223 status = nfs4_server_capabilities(server, fhandle);
3224 if (status == 0)
3225 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003226
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003227 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Bryan Schumakerbae36242012-05-10 15:07:31 -04003230static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3231 struct nfs_fsinfo *info)
3232{
3233 int error;
3234 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003235 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003236
3237 error = nfs4_server_capabilities(server, mntfh);
3238 if (error < 0) {
3239 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3240 return error;
3241 }
3242
David Quigley14c43f72013-05-22 12:50:43 -04003243 label = nfs4_label_alloc(server, GFP_KERNEL);
3244 if (IS_ERR(label))
3245 return PTR_ERR(label);
3246
David Quigley1775fd32013-05-22 12:50:42 -04003247 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003248 if (error < 0) {
3249 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003250 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003251 }
3252
3253 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3254 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3255 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3256
David Quigley14c43f72013-05-22 12:50:43 -04003257err_free_label:
3258 nfs4_label_free(label);
3259
Bryan Schumakerbae36242012-05-10 15:07:31 -04003260 return error;
3261}
3262
Manoj Naik6b97fd32006-06-09 09:34:29 -04003263/*
3264 * Get locations and (maybe) other attributes of a referral.
3265 * Note that we'll actually follow the referral later when
3266 * we detect fsid mismatch in inode revalidation
3267 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003268static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3269 const struct qstr *name, struct nfs_fattr *fattr,
3270 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003271{
3272 int status = -ENOMEM;
3273 struct page *page = NULL;
3274 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003275
3276 page = alloc_page(GFP_KERNEL);
3277 if (page == NULL)
3278 goto out;
3279 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3280 if (locations == NULL)
3281 goto out;
3282
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003283 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003284 if (status != 0)
3285 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003286
3287 /*
3288 * If the fsid didn't change, this is a migration event, not a
3289 * referral. Cause us to drop into the exception handler, which
3290 * will kick off migration recovery.
3291 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003292 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003293 dprintk("%s: server did not return a different fsid for"
3294 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003295 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003296 goto out;
3297 }
Andy Adamson533eb462011-06-13 18:25:56 -04003298 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3299 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003300
Andy Adamson533eb462011-06-13 18:25:56 -04003301 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003302 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003303 memset(fhandle, 0, sizeof(struct nfs_fh));
3304out:
3305 if (page)
3306 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003307 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003308 return status;
3309}
3310
David Quigley1775fd32013-05-22 12:50:42 -04003311static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3312 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
3314 struct nfs4_getattr_arg args = {
3315 .fh = fhandle,
3316 .bitmask = server->attr_bitmask,
3317 };
3318 struct nfs4_getattr_res res = {
3319 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003320 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 .server = server,
3322 };
3323 struct rpc_message msg = {
3324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3325 .rpc_argp = &args,
3326 .rpc_resp = &res,
3327 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003328
3329 args.bitmask = nfs4_bitmask(server, label);
3330
Trond Myklebust0e574af2005-10-27 22:12:38 -04003331 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003332 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
David Quigley1775fd32013-05-22 12:50:42 -04003335static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3336 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
3338 struct nfs4_exception exception = { };
3339 int err;
3340 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003341 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3342 trace_nfs4_getattr(server, fhandle, fattr, err);
3343 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 &exception);
3345 } while (exception.retry);
3346 return err;
3347}
3348
3349/*
3350 * The file is not closed if it is opened due to the a request to change
3351 * the size of the file. The open call will not be needed once the
3352 * VFS layer lookup-intents are implemented.
3353 *
3354 * Close is called when the inode is destroyed.
3355 * If we haven't opened the file for O_WRONLY, we
3356 * need to in the size_change case to obtain a stateid.
3357 *
3358 * Got race?
3359 * Because OPEN is always done by name in nfsv4, it is
3360 * possible that we opened a different file by the same
3361 * name. We can recognize this race condition, but we
3362 * can't do anything about it besides returning an error.
3363 *
3364 * This will be fixed with VFS changes (lookup-intent).
3365 */
3366static int
3367nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3368 struct iattr *sattr)
3369{
David Howells2b0143b2015-03-17 22:25:59 +00003370 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003371 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003372 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003373 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 int status;
3375
Peng Tao88ac8152014-09-12 11:04:10 +08003376 if (pnfs_ld_layoutret_on_setattr(inode) &&
3377 sattr->ia_valid & ATTR_SIZE &&
3378 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003379 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003380
Trond Myklebust0e574af2005-10-27 22:12:38 -04003381 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Andy Adamson26699402012-05-30 16:12:24 -04003383 /* Deal with open(O_TRUNC) */
3384 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003385 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003386
3387 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003388 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003389 return 0;
3390
Trond Myklebustd5308382005-11-04 15:33:38 -05003391 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003392 if (sattr->ia_valid & ATTR_FILE) {
3393 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003394
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003395 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003396 if (ctx) {
3397 cred = ctx->cred;
3398 state = ctx->state;
3399 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003400 }
3401
David Quigley14c43f72013-05-22 12:50:43 -04003402 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3403 if (IS_ERR(label))
3404 return PTR_ERR(label);
3405
3406 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003407 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003408 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003409 nfs_setsecurity(inode, fattr, label);
3410 }
David Quigley14c43f72013-05-22 12:50:43 -04003411 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return status;
3413}
3414
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003415static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3416 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003417 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003418{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003419 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003420 int status;
3421 struct nfs4_lookup_arg args = {
3422 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003423 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003424 .name = name,
3425 };
3426 struct nfs4_lookup_res res = {
3427 .server = server,
3428 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003429 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003430 .fh = fhandle,
3431 };
3432 struct rpc_message msg = {
3433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3434 .rpc_argp = &args,
3435 .rpc_resp = &res,
3436 };
3437
David Quigleyaa9c2662013-05-22 12:50:44 -04003438 args.bitmask = nfs4_bitmask(server, label);
3439
David Howells2b3de442006-08-22 20:06:09 -04003440 nfs_fattr_init(fattr);
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003443 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 dprintk("NFS reply lookup: %d\n", status);
3445 return status;
3446}
3447
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003448static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003449{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003450 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003451 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003452 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3453 fattr->nlink = 2;
3454}
3455
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003456static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3457 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003458 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003459{
3460 struct nfs4_exception exception = { };
3461 struct rpc_clnt *client = *clnt;
3462 int err;
3463 do {
David Quigley1775fd32013-05-22 12:50:42 -04003464 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003465 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003466 switch (err) {
3467 case -NFS4ERR_BADNAME:
3468 err = -ENOENT;
3469 goto out;
3470 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003471 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003472 if (err == -NFS4ERR_MOVED)
3473 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003474 goto out;
3475 case -NFS4ERR_WRONGSEC:
3476 err = -EPERM;
3477 if (client != *clnt)
3478 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003479 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003480 if (IS_ERR(client))
3481 return PTR_ERR(client);
3482
3483 exception.retry = 1;
3484 break;
3485 default:
3486 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3487 }
3488 } while (exception.retry);
3489
3490out:
3491 if (err == 0)
3492 *clnt = client;
3493 else if (client != *clnt)
3494 rpc_shutdown_client(client);
3495
3496 return err;
3497}
3498
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003499static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003500 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3501 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003503 int status;
3504 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003505
David Quigley1775fd32013-05-22 12:50:42 -04003506 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003507 if (client != NFS_CLIENT(dir)) {
3508 rpc_shutdown_client(client);
3509 nfs_fixup_secinfo_attributes(fattr);
3510 }
3511 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003514struct rpc_clnt *
3515nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3516 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3517{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003518 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003519 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003520
David Quigley1775fd32013-05-22 12:50:42 -04003521 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003522 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003523 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003524 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003525}
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3528{
Trond Myklebust76b32992007-08-10 17:45:11 -04003529 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 struct nfs4_accessargs args = {
3531 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003532 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003534 struct nfs4_accessres res = {
3535 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003536 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 struct rpc_message msg = {
3538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3539 .rpc_argp = &args,
3540 .rpc_resp = &res,
3541 .rpc_cred = entry->cred,
3542 };
3543 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003544 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 /*
3547 * Determine which access bits we want to ask for...
3548 */
3549 if (mode & MAY_READ)
3550 args.access |= NFS4_ACCESS_READ;
3551 if (S_ISDIR(inode->i_mode)) {
3552 if (mode & MAY_WRITE)
3553 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3554 if (mode & MAY_EXEC)
3555 args.access |= NFS4_ACCESS_LOOKUP;
3556 } else {
3557 if (mode & MAY_WRITE)
3558 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3559 if (mode & MAY_EXEC)
3560 args.access |= NFS4_ACCESS_EXECUTE;
3561 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003562
3563 res.fattr = nfs_alloc_fattr();
3564 if (res.fattr == NULL)
3565 return -ENOMEM;
3566
Bryan Schumaker7c513052011-03-24 17:12:24 +00003567 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003569 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003570 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003572 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 return status;
3574}
3575
3576static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3577{
3578 struct nfs4_exception exception = { };
3579 int err;
3580 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003581 err = _nfs4_proc_access(inode, entry);
3582 trace_nfs4_access(inode, err);
3583 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 &exception);
3585 } while (exception.retry);
3586 return err;
3587}
3588
3589/*
3590 * TODO: For the time being, we don't try to get any attributes
3591 * along with any of the zero-copy operations READ, READDIR,
3592 * READLINK, WRITE.
3593 *
3594 * In the case of the first three, we want to put the GETATTR
3595 * after the read-type operation -- this is because it is hard
3596 * to predict the length of a GETATTR response in v4, and thus
3597 * align the READ data correctly. This means that the GETATTR
3598 * may end up partially falling into the page cache, and we should
3599 * shift it into the 'tail' of the xdr_buf before processing.
3600 * To do this efficiently, we need to know the total length
3601 * of data received, which doesn't seem to be available outside
3602 * of the RPC layer.
3603 *
3604 * In the case of WRITE, we also want to put the GETATTR after
3605 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003606 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 *
3608 * Both of these changes to the XDR layer would in fact be quite
3609 * minor, but I decided to leave them for a subsequent patch.
3610 */
3611static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3612 unsigned int pgbase, unsigned int pglen)
3613{
3614 struct nfs4_readlink args = {
3615 .fh = NFS_FH(inode),
3616 .pgbase = pgbase,
3617 .pglen = pglen,
3618 .pages = &page,
3619 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003620 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 struct rpc_message msg = {
3622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3623 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003624 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 };
3626
Bryan Schumaker7c513052011-03-24 17:12:24 +00003627 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 -07003628}
3629
3630static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3631 unsigned int pgbase, unsigned int pglen)
3632{
3633 struct nfs4_exception exception = { };
3634 int err;
3635 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003636 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3637 trace_nfs4_readlink(inode, err);
3638 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 &exception);
3640 } while (exception.retry);
3641 return err;
3642}
3643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003645 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647static int
3648nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003649 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
David Quigleyaa9c2662013-05-22 12:50:44 -04003651 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003652 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 int status = 0;
3655
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003656 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3657 if (IS_ERR(ctx))
3658 return PTR_ERR(ctx);
3659
David Quigleyaa9c2662013-05-22 12:50:44 -04003660 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3661
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003662 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003663 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 if (IS_ERR(state)) {
3665 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003666 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003669 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003670 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return status;
3672}
3673
3674static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3675{
Trond Myklebust16e42952005-10-27 22:12:44 -04003676 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003677 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003679 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003681 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003682 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003683 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3686 .rpc_argp = &args,
3687 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003689 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003690
Bryan Schumaker7c513052011-03-24 17:12:24 +00003691 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003692 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003693 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return status;
3695}
3696
3697static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3698{
3699 struct nfs4_exception exception = { };
3700 int err;
3701 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003702 err = _nfs4_proc_remove(dir, name);
3703 trace_nfs4_remove(dir, name, err);
3704 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 &exception);
3706 } while (exception.retry);
3707 return err;
3708}
3709
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003710static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003712 struct nfs_server *server = NFS_SERVER(dir);
3713 struct nfs_removeargs *args = msg->rpc_argp;
3714 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003716 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003718 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003719
3720 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
3722
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003723static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3724{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003725 nfs4_setup_sequence(NFS_SERVER(data->dir),
3726 &data->args.seq_args,
3727 &data->res.seq_res,
3728 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729}
3730
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003731static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003733 struct nfs_unlinkdata *data = task->tk_calldata;
3734 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003735
Trond Myklebust14516c32010-07-31 14:29:06 -04003736 if (!nfs4_sequence_done(task, &res->seq_res))
3737 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003738 if (nfs4_async_handle_error(task, res->server, NULL,
3739 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003740 return 0;
3741 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003742 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
3744
Jeff Laytond3d41522010-09-17 17:31:57 -04003745static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3746{
3747 struct nfs_server *server = NFS_SERVER(dir);
3748 struct nfs_renameargs *arg = msg->rpc_argp;
3749 struct nfs_renameres *res = msg->rpc_resp;
3750
3751 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003752 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003753 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003754}
3755
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003756static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3757{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003758 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3759 &data->args.seq_args,
3760 &data->res.seq_res,
3761 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003762}
3763
3764static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3765 struct inode *new_dir)
3766{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003767 struct nfs_renamedata *data = task->tk_calldata;
3768 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003769
3770 if (!nfs4_sequence_done(task, &res->seq_res))
3771 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003772 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003773 return 0;
3774
3775 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003776 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003777 return 1;
3778}
3779
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3781{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003782 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 struct nfs4_link_arg arg = {
3784 .fh = NFS_FH(inode),
3785 .dir_fh = NFS_FH(dir),
3786 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003787 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003789 struct nfs4_link_res res = {
3790 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003791 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003792 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 struct rpc_message msg = {
3794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3795 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003796 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003798 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Trond Myklebust136f2622010-04-16 16:22:49 -04003800 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003801 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003802 goto out;
3803
David Quigley14c43f72013-05-22 12:50:43 -04003804 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3805 if (IS_ERR(res.label)) {
3806 status = PTR_ERR(res.label);
3807 goto out;
3808 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003809 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003810
Bryan Schumaker7c513052011-03-24 17:12:24 +00003811 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003812 if (!status) {
3813 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003814 status = nfs_post_op_update_inode(inode, res.fattr);
3815 if (!status)
3816 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003817 }
David Quigley14c43f72013-05-22 12:50:43 -04003818
3819
3820 nfs4_label_free(res.label);
3821
Trond Myklebust136f2622010-04-16 16:22:49 -04003822out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003823 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return status;
3825}
3826
3827static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3828{
3829 struct nfs4_exception exception = { };
3830 int err;
3831 do {
3832 err = nfs4_handle_exception(NFS_SERVER(inode),
3833 _nfs4_proc_link(inode, dir, name),
3834 &exception);
3835 } while (exception.retry);
3836 return err;
3837}
3838
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003839struct nfs4_createdata {
3840 struct rpc_message msg;
3841 struct nfs4_create_arg arg;
3842 struct nfs4_create_res res;
3843 struct nfs_fh fh;
3844 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003845 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003846};
3847
3848static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3849 struct qstr *name, struct iattr *sattr, u32 ftype)
3850{
3851 struct nfs4_createdata *data;
3852
3853 data = kzalloc(sizeof(*data), GFP_KERNEL);
3854 if (data != NULL) {
3855 struct nfs_server *server = NFS_SERVER(dir);
3856
David Quigley14c43f72013-05-22 12:50:43 -04003857 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3858 if (IS_ERR(data->label))
3859 goto out_free;
3860
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003861 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3862 data->msg.rpc_argp = &data->arg;
3863 data->msg.rpc_resp = &data->res;
3864 data->arg.dir_fh = NFS_FH(dir);
3865 data->arg.server = server;
3866 data->arg.name = name;
3867 data->arg.attrs = sattr;
3868 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003869 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003870 data->res.server = server;
3871 data->res.fh = &data->fh;
3872 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003873 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003874 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003875 }
3876 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003877out_free:
3878 kfree(data);
3879 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003880}
3881
3882static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3883{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003884 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003885 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003886 if (status == 0) {
3887 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003888 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003889 }
3890 return status;
3891}
3892
3893static void nfs4_free_createdata(struct nfs4_createdata *data)
3894{
David Quigley14c43f72013-05-22 12:50:43 -04003895 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003896 kfree(data);
3897}
3898
Chuck Lever4f390c12006-08-22 20:06:22 -04003899static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003900 struct page *page, unsigned int len, struct iattr *sattr,
3901 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003903 struct nfs4_createdata *data;
3904 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Chuck Lever94a6d752006-08-22 20:06:23 -04003906 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003907 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003908
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003909 status = -ENOMEM;
3910 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3911 if (data == NULL)
3912 goto out;
3913
3914 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3915 data->arg.u.symlink.pages = &page;
3916 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003917 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003919 status = nfs4_do_create(dir, dentry, data);
3920
3921 nfs4_free_createdata(data);
3922out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 return status;
3924}
3925
Chuck Lever4f390c12006-08-22 20:06:22 -04003926static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003927 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
3929 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003930 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003932
3933 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003936 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3937 trace_nfs4_symlink(dir, &dentry->d_name, err);
3938 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 &exception);
3940 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003941
3942 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 return err;
3944}
3945
3946static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003947 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003949 struct nfs4_createdata *data;
3950 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003952 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3953 if (data == NULL)
3954 goto out;
3955
David Quigley1775fd32013-05-22 12:50:42 -04003956 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003957 status = nfs4_do_create(dir, dentry, data);
3958
3959 nfs4_free_createdata(data);
3960out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 return status;
3962}
3963
3964static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3965 struct iattr *sattr)
3966{
3967 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003968 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003970
David Quigleyaa9c2662013-05-22 12:50:44 -04003971 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3972
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003973 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003975 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3976 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3977 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 &exception);
3979 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003980 nfs4_label_release_security(label);
3981
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return err;
3983}
3984
3985static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003986 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
David Howells2b0143b2015-03-17 22:25:59 +00003988 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 struct nfs4_readdir_arg args = {
3990 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003991 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 .pgbase = 0,
3993 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00003994 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003995 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 };
3997 struct nfs4_readdir_res res;
3998 struct rpc_message msg = {
3999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4000 .rpc_argp = &args,
4001 .rpc_resp = &res,
4002 .rpc_cred = cred,
4003 };
4004 int status;
4005
Al Viro6de14722013-09-16 10:53:17 -04004006 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4007 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004008 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004009 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004011 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 -05004012 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004013 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004014 status += args.pgbase;
4015 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004016
4017 nfs_invalidate_atime(dir);
4018
Harvey Harrison3110ff82008-05-02 13:42:44 -07004019 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 return status;
4021}
4022
4023static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004024 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
4026 struct nfs4_exception exception = { };
4027 int err;
4028 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004029 err = _nfs4_proc_readdir(dentry, cred, cookie,
4030 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004031 trace_nfs4_readdir(d_inode(dentry), err);
4032 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 &exception);
4034 } while (exception.retry);
4035 return err;
4036}
4037
4038static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004039 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004041 struct nfs4_createdata *data;
4042 int mode = sattr->ia_mode;
4043 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004045 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4046 if (data == NULL)
4047 goto out;
4048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004050 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004052 data->arg.ftype = NF4BLK;
4053 data->arg.u.device.specdata1 = MAJOR(rdev);
4054 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 }
4056 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004057 data->arg.ftype = NF4CHR;
4058 data->arg.u.device.specdata1 = MAJOR(rdev);
4059 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004060 } else if (!S_ISSOCK(mode)) {
4061 status = -EINVAL;
4062 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 }
David Quigley1775fd32013-05-22 12:50:42 -04004064
David Quigleyaa9c2662013-05-22 12:50:44 -04004065 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004066 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004067out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004068 nfs4_free_createdata(data);
4069out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 return status;
4071}
4072
4073static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4074 struct iattr *sattr, dev_t rdev)
4075{
4076 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004077 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004079
David Quigleyaa9c2662013-05-22 12:50:44 -04004080 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4081
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004082 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004084 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4085 trace_nfs4_mknod(dir, &dentry->d_name, err);
4086 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 &exception);
4088 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004089
4090 nfs4_label_release_security(label);
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 return err;
4093}
4094
4095static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4096 struct nfs_fsstat *fsstat)
4097{
4098 struct nfs4_statfs_arg args = {
4099 .fh = fhandle,
4100 .bitmask = server->attr_bitmask,
4101 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004102 struct nfs4_statfs_res res = {
4103 .fsstat = fsstat,
4104 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 struct rpc_message msg = {
4106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4107 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004108 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 };
4110
Trond Myklebust0e574af2005-10-27 22:12:38 -04004111 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004112 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113}
4114
4115static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4116{
4117 struct nfs4_exception exception = { };
4118 int err;
4119 do {
4120 err = nfs4_handle_exception(server,
4121 _nfs4_proc_statfs(server, fhandle, fsstat),
4122 &exception);
4123 } while (exception.retry);
4124 return err;
4125}
4126
4127static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4128 struct nfs_fsinfo *fsinfo)
4129{
4130 struct nfs4_fsinfo_arg args = {
4131 .fh = fhandle,
4132 .bitmask = server->attr_bitmask,
4133 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004134 struct nfs4_fsinfo_res res = {
4135 .fsinfo = fsinfo,
4136 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 struct rpc_message msg = {
4138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4139 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004140 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 };
4142
Bryan Schumaker7c513052011-03-24 17:12:24 +00004143 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144}
4145
4146static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4147{
4148 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004149 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 int err;
4151
4152 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004153 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004154 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004155 if (err == 0) {
4156 struct nfs_client *clp = server->nfs_client;
4157
4158 spin_lock(&clp->cl_lock);
4159 clp->cl_lease_time = fsinfo->lease_time * HZ;
4160 clp->cl_last_renewal = now;
4161 spin_unlock(&clp->cl_lock);
4162 break;
4163 }
4164 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 } while (exception.retry);
4166 return err;
4167}
4168
4169static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4170{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004171 int error;
4172
Trond Myklebust0e574af2005-10-27 22:12:38 -04004173 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004174 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004175 if (error == 0) {
4176 /* block layout checks this! */
4177 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004178 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004179 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004180
4181 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
4183
4184static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4185 struct nfs_pathconf *pathconf)
4186{
4187 struct nfs4_pathconf_arg args = {
4188 .fh = fhandle,
4189 .bitmask = server->attr_bitmask,
4190 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004191 struct nfs4_pathconf_res res = {
4192 .pathconf = pathconf,
4193 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 struct rpc_message msg = {
4195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4196 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004197 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 };
4199
4200 /* None of the pathconf attributes are mandatory to implement */
4201 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4202 memset(pathconf, 0, sizeof(*pathconf));
4203 return 0;
4204 }
4205
Trond Myklebust0e574af2005-10-27 22:12:38 -04004206 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004207 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209
4210static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4211 struct nfs_pathconf *pathconf)
4212{
4213 struct nfs4_exception exception = { };
4214 int err;
4215
4216 do {
4217 err = nfs4_handle_exception(server,
4218 _nfs4_proc_pathconf(server, fhandle, pathconf),
4219 &exception);
4220 } while (exception.retry);
4221 return err;
4222}
4223
Trond Myklebust5521abf2013-03-16 20:54:34 -04004224int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004225 const struct nfs_open_context *ctx,
4226 const struct nfs_lock_context *l_ctx,
4227 fmode_t fmode)
4228{
4229 const struct nfs_lockowner *lockowner = NULL;
4230
4231 if (l_ctx != NULL)
4232 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004233 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004234}
4235EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4236
Trond Myklebust5521abf2013-03-16 20:54:34 -04004237static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4238 const struct nfs_open_context *ctx,
4239 const struct nfs_lock_context *l_ctx,
4240 fmode_t fmode)
4241{
4242 nfs4_stateid current_stateid;
4243
Trond Myklebuste1253be2014-03-05 08:44:23 -05004244 /* If the current stateid represents a lost lock, then exit */
4245 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4246 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004247 return nfs4_stateid_match(stateid, &current_stateid);
4248}
4249
4250static bool nfs4_error_stateid_expired(int err)
4251{
4252 switch (err) {
4253 case -NFS4ERR_DELEG_REVOKED:
4254 case -NFS4ERR_ADMIN_REVOKED:
4255 case -NFS4ERR_BAD_STATEID:
4256 case -NFS4ERR_STALE_STATEID:
4257 case -NFS4ERR_OLD_STATEID:
4258 case -NFS4ERR_OPENMODE:
4259 case -NFS4ERR_EXPIRED:
4260 return true;
4261 }
4262 return false;
4263}
4264
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004265void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004266{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004267 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004268}
4269
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004270static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004272 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004274 trace_nfs4_read(hdr, task->tk_status);
4275 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004276 hdr->args.context->state,
4277 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004278 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004279 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004281
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004282 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004284 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286}
4287
Trond Myklebust5521abf2013-03-16 20:54:34 -04004288static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004289 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004290{
4291
4292 if (!nfs4_error_stateid_expired(task->tk_status) ||
4293 nfs4_stateid_is_current(&args->stateid,
4294 args->context,
4295 args->lock_context,
4296 FMODE_READ))
4297 return false;
4298 rpc_restart_call_prepare(task);
4299 return true;
4300}
4301
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004302static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004303{
4304
4305 dprintk("--> %s\n", __func__);
4306
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004307 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004308 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004309 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004310 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004311 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4312 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004313}
4314
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004315static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4316 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004318 hdr->timestamp = jiffies;
4319 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004320 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004321 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322}
4323
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004324static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4325 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004326{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004327 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4328 &hdr->args.seq_args,
4329 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004330 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004331 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004332 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4333 hdr->args.lock_context,
4334 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004335 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004336 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004337 return -EIO;
4338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004341static int nfs4_write_done_cb(struct rpc_task *task,
4342 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004344 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004345
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004346 trace_nfs4_write(hdr, task->tk_status);
4347 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004348 hdr->args.context->state,
4349 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004350 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004351 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004353 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004354 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004355 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004356 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
Trond Myklebust5521abf2013-03-16 20:54:34 -04004360static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004361 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004362{
4363
4364 if (!nfs4_error_stateid_expired(task->tk_status) ||
4365 nfs4_stateid_is_current(&args->stateid,
4366 args->context,
4367 args->lock_context,
4368 FMODE_WRITE))
4369 return false;
4370 rpc_restart_call_prepare(task);
4371 return true;
4372}
4373
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004374static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004375{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004376 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004377 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004378 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004379 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004380 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4381 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004382}
4383
Trond Myklebust5a37f852012-04-28 14:55:16 -04004384static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004385bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004386{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004387 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004388 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004389 return false;
4390 /* Otherwise, request attributes if and only if we don't hold
4391 * a delegation
4392 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004393 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004394}
Fred Isamana69aef12011-03-03 15:13:47 +00004395
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004396static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4397 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004399 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004400
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004401 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4402 hdr->args.bitmask = NULL;
4403 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004404 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004405 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004406
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004407 if (!hdr->pgio_done_cb)
4408 hdr->pgio_done_cb = nfs4_write_done_cb;
4409 hdr->res.server = server;
4410 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004412 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004413 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414}
4415
Fred Isaman0b7c0152012-04-20 14:47:39 -04004416static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4417{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004418 nfs4_setup_sequence(NFS_SERVER(data->inode),
4419 &data->args.seq_args,
4420 &data->res.seq_res,
4421 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422}
4423
Fred Isaman0b7c0152012-04-20 14:47:39 -04004424static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004427
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004428 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004429 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4430 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004431 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004432 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
Fred Isaman0b7c0152012-04-20 14:47:39 -04004437static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004438{
4439 if (!nfs4_sequence_done(task, &data->res.seq_res))
4440 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004441 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004442}
4443
Fred Isaman0b7c0152012-04-20 14:47:39 -04004444static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004446 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004447
Fred Isaman0b7c0152012-04-20 14:47:39 -04004448 if (data->commit_done_cb == NULL)
4449 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004450 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004451 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004452 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
4454
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004455struct nfs4_renewdata {
4456 struct nfs_client *client;
4457 unsigned long timestamp;
4458};
4459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460/*
4461 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4462 * standalone procedure for queueing an asynchronous RENEW.
4463 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004464static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004465{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004466 struct nfs4_renewdata *data = calldata;
4467 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004468
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004469 if (atomic_read(&clp->cl_count) > 1)
4470 nfs4_schedule_state_renewal(clp);
4471 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004472 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004473}
4474
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004475static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004477 struct nfs4_renewdata *data = calldata;
4478 struct nfs_client *clp = data->client;
4479 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004481 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004482 switch (task->tk_status) {
4483 case 0:
4484 break;
4485 case -NFS4ERR_LEASE_MOVED:
4486 nfs4_schedule_lease_moved_recovery(clp);
4487 break;
4488 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004489 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004490 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4491 return;
4492 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004493 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004494 return;
4495 }
4496 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004498 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499}
4500
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004501static const struct rpc_call_ops nfs4_renew_ops = {
4502 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004503 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004504};
4505
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004506static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
4508 struct rpc_message msg = {
4509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4510 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004511 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004513 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004515 if (renew_flags == 0)
4516 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004517 if (!atomic_inc_not_zero(&clp->cl_count))
4518 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004519 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004520 if (data == NULL)
4521 return -ENOMEM;
4522 data->client = clp;
4523 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004524 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004525 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
4527
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004528static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529{
4530 struct rpc_message msg = {
4531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4532 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004533 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 };
4535 unsigned long now = jiffies;
4536 int status;
4537
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004538 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 if (status < 0)
4540 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004541 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 return 0;
4543}
4544
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004545static inline int nfs4_server_supports_acls(struct nfs_server *server)
4546{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004547 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004548}
4549
Trond Myklebust21f498c2012-08-24 10:59:25 -04004550/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4551 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004552 * the stack.
4553 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004554#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004555
Neil Hormane9e3d722011-03-04 19:26:03 -05004556static int buf_to_pages_noslab(const void *buf, size_t buflen,
4557 struct page **pages, unsigned int *pgbase)
4558{
4559 struct page *newpage, **spages;
4560 int rc = 0;
4561 size_t len;
4562 spages = pages;
4563
4564 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004565 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004566 newpage = alloc_page(GFP_KERNEL);
4567
4568 if (newpage == NULL)
4569 goto unwind;
4570 memcpy(page_address(newpage), buf, len);
4571 buf += len;
4572 buflen -= len;
4573 *pages++ = newpage;
4574 rc++;
4575 } while (buflen != 0);
4576
4577 return rc;
4578
4579unwind:
4580 for(; rc > 0; rc--)
4581 __free_page(spages[rc-1]);
4582 return -ENOMEM;
4583}
4584
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004585struct nfs4_cached_acl {
4586 int cached;
4587 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004588 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004589};
4590
4591static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004592{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004593 struct nfs_inode *nfsi = NFS_I(inode);
4594
4595 spin_lock(&inode->i_lock);
4596 kfree(nfsi->nfs4_acl);
4597 nfsi->nfs4_acl = acl;
4598 spin_unlock(&inode->i_lock);
4599}
4600
4601static void nfs4_zap_acl_attr(struct inode *inode)
4602{
4603 nfs4_set_cached_acl(inode, NULL);
4604}
4605
4606static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4607{
4608 struct nfs_inode *nfsi = NFS_I(inode);
4609 struct nfs4_cached_acl *acl;
4610 int ret = -ENOENT;
4611
4612 spin_lock(&inode->i_lock);
4613 acl = nfsi->nfs4_acl;
4614 if (acl == NULL)
4615 goto out;
4616 if (buf == NULL) /* user is just asking for length */
4617 goto out_len;
4618 if (acl->cached == 0)
4619 goto out;
4620 ret = -ERANGE; /* see getxattr(2) man page */
4621 if (acl->len > buflen)
4622 goto out;
4623 memcpy(buf, acl->data, acl->len);
4624out_len:
4625 ret = acl->len;
4626out:
4627 spin_unlock(&inode->i_lock);
4628 return ret;
4629}
4630
Sachin Prabhu5794d212012-04-17 14:36:40 +01004631static 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 +00004632{
4633 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004634 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004635
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004636 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004637 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004638 if (acl == NULL)
4639 goto out;
4640 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004641 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004642 } else {
4643 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4644 if (acl == NULL)
4645 goto out;
4646 acl->cached = 0;
4647 }
4648 acl->len = acl_len;
4649out:
4650 nfs4_set_cached_acl(inode, acl);
4651}
4652
Andy Adamsonbf118a32011-12-07 11:55:27 -05004653/*
4654 * The getxattr API returns the required buffer length when called with a
4655 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4656 * the required buf. On a NULL buf, we send a page of data to the server
4657 * guessing that the ACL request can be serviced by a page. If so, we cache
4658 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4659 * the cache. If not so, we throw away the page, and cache the required
4660 * length. The next getxattr call will then produce another round trip to
4661 * the server, this time with the input buf of the required size.
4662 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004663static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004664{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004665 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004666 struct nfs_getaclargs args = {
4667 .fh = NFS_FH(inode),
4668 .acl_pages = pages,
4669 .acl_len = buflen,
4670 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004671 struct nfs_getaclres res = {
4672 .acl_len = buflen,
4673 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004674 struct rpc_message msg = {
4675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4676 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004677 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004678 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004679 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4680 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004681
Andy Adamsonbf118a32011-12-07 11:55:27 -05004682 /* As long as we're doing a round trip to the server anyway,
4683 * let's be prepared for a page of acl data. */
4684 if (npages == 0)
4685 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004686 if (npages > ARRAY_SIZE(pages))
4687 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004688
Andy Adamsonbf118a32011-12-07 11:55:27 -05004689 for (i = 0; i < npages; i++) {
4690 pages[i] = alloc_page(GFP_KERNEL);
4691 if (!pages[i])
4692 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004693 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004694
4695 /* for decoding across pages */
4696 res.acl_scratch = alloc_page(GFP_KERNEL);
4697 if (!res.acl_scratch)
4698 goto out_free;
4699
Andy Adamsonbf118a32011-12-07 11:55:27 -05004700 args.acl_len = npages * PAGE_SIZE;
4701 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004702
Peng Taode040be2012-01-10 22:42:47 +08004703 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004704 __func__, buf, buflen, npages, args.acl_len);
4705 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4706 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004707 if (ret)
4708 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004709
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004710 /* Handle the case where the passed-in buffer is too short */
4711 if (res.acl_flags & NFS4_ACL_TRUNC) {
4712 /* Did the user only issue a request for the acl length? */
4713 if (buf == NULL)
4714 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004715 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004716 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004717 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004718 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004719 if (buf) {
4720 if (res.acl_len > buflen) {
4721 ret = -ERANGE;
4722 goto out_free;
4723 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004724 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004725 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004726out_ok:
4727 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004728out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004729 for (i = 0; i < npages; i++)
4730 if (pages[i])
4731 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004732 if (res.acl_scratch)
4733 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004734 return ret;
4735}
4736
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004737static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4738{
4739 struct nfs4_exception exception = { };
4740 ssize_t ret;
4741 do {
4742 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004743 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004744 if (ret >= 0)
4745 break;
4746 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4747 } while (exception.retry);
4748 return ret;
4749}
4750
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004751static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4752{
4753 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004754 int ret;
4755
4756 if (!nfs4_server_supports_acls(server))
4757 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004758 ret = nfs_revalidate_inode(server, inode);
4759 if (ret < 0)
4760 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004761 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4762 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004763 ret = nfs4_read_cached_acl(inode, buf, buflen);
4764 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004765 /* -ENOENT is returned if there is no ACL or if there is an ACL
4766 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004767 return ret;
4768 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004769}
4770
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004771static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004772{
4773 struct nfs_server *server = NFS_SERVER(inode);
4774 struct page *pages[NFS4ACL_MAXPAGES];
4775 struct nfs_setaclargs arg = {
4776 .fh = NFS_FH(inode),
4777 .acl_pages = pages,
4778 .acl_len = buflen,
4779 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004780 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004781 struct rpc_message msg = {
4782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4783 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004784 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004785 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004786 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004787 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004788
4789 if (!nfs4_server_supports_acls(server))
4790 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004791 if (npages > ARRAY_SIZE(pages))
4792 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004793 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4794 if (i < 0)
4795 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004796 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004797 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004798
4799 /*
4800 * Free each page after tx, so the only ref left is
4801 * held by the network stack
4802 */
4803 for (; i > 0; i--)
4804 put_page(pages[i-1]);
4805
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004806 /*
4807 * Acl update can result in inode attribute update.
4808 * so mark the attribute cache invalid.
4809 */
4810 spin_lock(&inode->i_lock);
4811 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4812 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004813 nfs_access_zap_cache(inode);
4814 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004815 return ret;
4816}
4817
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004818static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4819{
4820 struct nfs4_exception exception = { };
4821 int err;
4822 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004823 err = __nfs4_proc_set_acl(inode, buf, buflen);
4824 trace_nfs4_set_acl(inode, err);
4825 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004826 &exception);
4827 } while (exception.retry);
4828 return err;
4829}
4830
David Quigleyaa9c2662013-05-22 12:50:44 -04004831#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4832static int _nfs4_get_security_label(struct inode *inode, void *buf,
4833 size_t buflen)
4834{
4835 struct nfs_server *server = NFS_SERVER(inode);
4836 struct nfs_fattr fattr;
4837 struct nfs4_label label = {0, 0, buflen, buf};
4838
4839 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004840 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004841 .fh = NFS_FH(inode),
4842 .bitmask = bitmask,
4843 };
4844 struct nfs4_getattr_res res = {
4845 .fattr = &fattr,
4846 .label = &label,
4847 .server = server,
4848 };
4849 struct rpc_message msg = {
4850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004851 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004852 .rpc_resp = &res,
4853 };
4854 int ret;
4855
4856 nfs_fattr_init(&fattr);
4857
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004858 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004859 if (ret)
4860 return ret;
4861 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4862 return -ENOENT;
4863 if (buflen < label.len)
4864 return -ERANGE;
4865 return 0;
4866}
4867
4868static int nfs4_get_security_label(struct inode *inode, void *buf,
4869 size_t buflen)
4870{
4871 struct nfs4_exception exception = { };
4872 int err;
4873
4874 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4875 return -EOPNOTSUPP;
4876
4877 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004878 err = _nfs4_get_security_label(inode, buf, buflen);
4879 trace_nfs4_get_security_label(inode, err);
4880 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004881 &exception);
4882 } while (exception.retry);
4883 return err;
4884}
4885
4886static int _nfs4_do_set_security_label(struct inode *inode,
4887 struct nfs4_label *ilabel,
4888 struct nfs_fattr *fattr,
4889 struct nfs4_label *olabel)
4890{
4891
4892 struct iattr sattr = {0};
4893 struct nfs_server *server = NFS_SERVER(inode);
4894 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004895 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004896 .fh = NFS_FH(inode),
4897 .iap = &sattr,
4898 .server = server,
4899 .bitmask = bitmask,
4900 .label = ilabel,
4901 };
4902 struct nfs_setattrres res = {
4903 .fattr = fattr,
4904 .label = olabel,
4905 .server = server,
4906 };
4907 struct rpc_message msg = {
4908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004909 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004910 .rpc_resp = &res,
4911 };
4912 int status;
4913
Jeff Layton12207f62013-11-01 10:49:32 -04004914 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004915
Jeff Layton12207f62013-11-01 10:49:32 -04004916 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004917 if (status)
4918 dprintk("%s failed: %d\n", __func__, status);
4919
4920 return status;
4921}
4922
4923static int nfs4_do_set_security_label(struct inode *inode,
4924 struct nfs4_label *ilabel,
4925 struct nfs_fattr *fattr,
4926 struct nfs4_label *olabel)
4927{
4928 struct nfs4_exception exception = { };
4929 int err;
4930
4931 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004932 err = _nfs4_do_set_security_label(inode, ilabel,
4933 fattr, olabel);
4934 trace_nfs4_set_security_label(inode, err);
4935 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004936 &exception);
4937 } while (exception.retry);
4938 return err;
4939}
4940
4941static int
4942nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4943{
4944 struct nfs4_label ilabel, *olabel = NULL;
4945 struct nfs_fattr fattr;
4946 struct rpc_cred *cred;
David Howells2b0143b2015-03-17 22:25:59 +00004947 struct inode *inode = d_inode(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004948 int status;
4949
4950 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4951 return -EOPNOTSUPP;
4952
4953 nfs_fattr_init(&fattr);
4954
4955 ilabel.pi = 0;
4956 ilabel.lfs = 0;
4957 ilabel.label = (char *)buf;
4958 ilabel.len = buflen;
4959
4960 cred = rpc_lookup_cred();
4961 if (IS_ERR(cred))
4962 return PTR_ERR(cred);
4963
4964 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4965 if (IS_ERR(olabel)) {
4966 status = -PTR_ERR(olabel);
4967 goto out;
4968 }
4969
4970 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4971 if (status == 0)
4972 nfs_setsecurity(inode, &fattr, olabel);
4973
4974 nfs4_label_free(olabel);
4975out:
4976 put_rpccred(cred);
4977 return status;
4978}
4979#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4980
4981
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982static int
Trond Myklebust516285eb2015-09-20 16:15:24 -04004983nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004984 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004986 struct nfs_client *clp = server->nfs_client;
4987
4988 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 return 0;
4990 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004991 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004992 case -NFS4ERR_ADMIN_REVOKED:
4993 case -NFS4ERR_BAD_STATEID:
4994 case -NFS4ERR_OPENMODE:
4995 if (state == NULL)
4996 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004997 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004998 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004999 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005000 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04005001 if (state != NULL) {
5002 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04005003 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04005004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005006 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005007 nfs4_schedule_lease_recovery(clp);
5008 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04005009 case -NFS4ERR_MOVED:
5010 if (nfs4_schedule_migration_recovery(server) < 0)
5011 goto recovery_failed;
5012 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04005013 case -NFS4ERR_LEASE_MOVED:
5014 nfs4_schedule_lease_moved_recovery(clp);
5015 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04005016#if defined(CONFIG_NFS_V4_1)
5017 case -NFS4ERR_BADSESSION:
5018 case -NFS4ERR_BADSLOT:
5019 case -NFS4ERR_BAD_HIGH_SLOT:
5020 case -NFS4ERR_DEADSESSION:
5021 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5022 case -NFS4ERR_SEQ_FALSE_RETRY:
5023 case -NFS4ERR_SEQ_MISORDERED:
5024 dprintk("%s ERROR %d, Reset session\n", __func__,
5025 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04005026 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05005027 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04005028#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005030 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10005031 rpc_delay(task, nfs4_update_delay(timeout));
5032 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05005033 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005035 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04005037 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 }
5039 task->tk_status = nfs4_map_errors(task->tk_status);
5040 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04005041recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04005042 task->tk_status = -EIO;
5043 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005044wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005045 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005046 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
5047 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04005048 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
5049 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04005050restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005051 task->tk_status = 0;
5052 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053}
5054
Chuck Leverf0920752012-05-21 22:45:41 -04005055static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5056 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005057{
5058 __be32 verf[2];
5059
Chuck Lever2c820d92012-05-21 22:45:33 -04005060 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5061 /* An impossible timestamp guarantees this value
5062 * will never match a generated boot time. */
5063 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005064 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005065 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005066 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005067 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5068 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005069 }
Chuck Levercd937102012-03-02 17:14:31 -05005070 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5071}
5072
Jeff Laytona3192682015-06-09 19:43:59 -04005073static int
5074nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005075{
Jeff Laytona3192682015-06-09 19:43:59 -04005076 int result;
5077 size_t len;
5078 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005079
Trond Myklebustceb3a162015-01-03 15:16:04 -05005080 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005081 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005082
Jeff Laytona3192682015-06-09 19:43:59 -04005083 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005084 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005085 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5086 1 +
5087 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5088 1;
5089 rcu_read_unlock();
5090
5091 if (len > NFS4_OPAQUE_LIMIT + 1)
5092 return -EINVAL;
5093
5094 /*
5095 * Since this string is allocated at mount time, and held until the
5096 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5097 * about a memory-reclaim deadlock.
5098 */
5099 str = kmalloc(len, GFP_KERNEL);
5100 if (!str)
5101 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005102
Chuck Levere984a552012-09-14 17:24:21 -04005103 rcu_read_lock();
Jeff Laytona3192682015-06-09 19:43:59 -04005104 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5105 clp->cl_ipaddr,
5106 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5107 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005108 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005109
Jeff Laytona3192682015-06-09 19:43:59 -04005110 clp->cl_owner_id = str;
5111 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005112}
5113
Jeff Layton873e3852015-06-09 19:44:00 -04005114static int
5115nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005116{
Jeff Layton873e3852015-06-09 19:44:00 -04005117 int result;
5118 size_t len;
5119 char *str;
5120
5121 len = 10 + 10 + 1 + 10 + 1 +
5122 strlen(nfs4_client_id_uniquifier) + 1 +
5123 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5124
5125 if (len > NFS4_OPAQUE_LIMIT + 1)
5126 return -EINVAL;
5127
5128 /*
5129 * Since this string is allocated at mount time, and held until the
5130 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5131 * about a memory-reclaim deadlock.
5132 */
5133 str = kmalloc(len, GFP_KERNEL);
5134 if (!str)
5135 return -ENOMEM;
5136
5137 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5138 clp->rpc_ops->version, clp->cl_minorversion,
5139 nfs4_client_id_uniquifier,
5140 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005141 clp->cl_owner_id = str;
5142 return 0;
5143}
5144
5145static int
5146nfs4_init_uniform_client_string(struct nfs_client *clp)
5147{
5148 int result;
5149 size_t len;
5150 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005151
5152 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005153 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005154
5155 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005156 return nfs4_init_uniquifier_client_string(clp);
5157
5158 len = 10 + 10 + 1 + 10 + 1 +
5159 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5160
5161 if (len > NFS4_OPAQUE_LIMIT + 1)
5162 return -EINVAL;
5163
5164 /*
5165 * Since this string is allocated at mount time, and held until the
5166 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5167 * about a memory-reclaim deadlock.
5168 */
5169 str = kmalloc(len, GFP_KERNEL);
5170 if (!str)
5171 return -ENOMEM;
5172
5173 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5174 clp->rpc_ops->version, clp->cl_minorversion,
5175 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005176 clp->cl_owner_id = str;
5177 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005178}
5179
Chuck Lever706cb8d2014-03-12 12:51:47 -04005180/*
5181 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5182 * services. Advertise one based on the address family of the
5183 * clientaddr.
5184 */
5185static unsigned int
5186nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5187{
5188 if (strchr(clp->cl_ipaddr, ':') != NULL)
5189 return scnprintf(buf, len, "tcp6");
5190 else
5191 return scnprintf(buf, len, "tcp");
5192}
5193
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005194static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5195{
5196 struct nfs4_setclientid *sc = calldata;
5197
5198 if (task->tk_status == 0)
5199 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5200}
5201
5202static const struct rpc_call_ops nfs4_setclientid_ops = {
5203 .rpc_call_done = nfs4_setclientid_done,
5204};
5205
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005206/**
5207 * nfs4_proc_setclientid - Negotiate client ID
5208 * @clp: state data structure
5209 * @program: RPC program for NFSv4 callback service
5210 * @port: IP port number for NFS4 callback service
5211 * @cred: RPC credential to use for this call
5212 * @res: where to place the result
5213 *
5214 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5215 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005216int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5217 unsigned short port, struct rpc_cred *cred,
5218 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219{
5220 nfs4_verifier sc_verifier;
5221 struct nfs4_setclientid setclientid = {
5222 .sc_verifier = &sc_verifier,
5223 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005224 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 };
5226 struct rpc_message msg = {
5227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5228 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005229 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005230 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005232 struct rpc_task *task;
5233 struct rpc_task_setup task_setup_data = {
5234 .rpc_client = clp->cl_rpcclient,
5235 .rpc_message = &msg,
5236 .callback_ops = &nfs4_setclientid_ops,
5237 .callback_data = &setclientid,
5238 .flags = RPC_TASK_TIMEOUT,
5239 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005240 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241
Chuck Leverde734832012-07-11 16:30:50 -04005242 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005243 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005244
5245 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5246 status = nfs4_init_uniform_client_string(clp);
5247 else
Jeff Laytona3192682015-06-09 19:43:59 -04005248 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005249
5250 if (status)
5251 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005252
Chuck Leverde734832012-07-11 16:30:50 -04005253 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005254 setclientid.sc_netid_len =
5255 nfs4_init_callback_netid(clp,
5256 setclientid.sc_netid,
5257 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005258 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005259 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 clp->cl_ipaddr, port >> 8, port & 255);
5261
Jeff Layton3a6bb732015-06-09 19:43:57 -04005262 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005263 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005264 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005265 task = rpc_run_task(&task_setup_data);
5266 if (IS_ERR(task)) {
5267 status = PTR_ERR(task);
5268 goto out;
5269 }
5270 status = task->tk_status;
5271 if (setclientid.sc_cred) {
5272 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5273 put_rpccred(setclientid.sc_cred);
5274 }
5275 rpc_put_task(task);
5276out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005277 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005278 dprintk("NFS reply setclientid: %d\n", status);
5279 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280}
5281
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005282/**
5283 * nfs4_proc_setclientid_confirm - Confirm client ID
5284 * @clp: state data structure
5285 * @res: result of a previous SETCLIENTID
5286 * @cred: RPC credential to use for this call
5287 *
5288 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5289 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005290int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005291 struct nfs4_setclientid_res *arg,
5292 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 struct rpc_message msg = {
5295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005296 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005297 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 int status;
5300
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005301 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5302 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5303 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005304 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005305 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005306 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 return status;
5308}
5309
Trond Myklebustfe650402006-01-03 09:55:18 +01005310struct nfs4_delegreturndata {
5311 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005312 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005313 struct nfs_fh fh;
5314 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005315 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005316 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005317 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005318 struct inode *inode;
5319 bool roc;
5320 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005321};
5322
Trond Myklebustfe650402006-01-03 09:55:18 +01005323static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5324{
5325 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005326
Trond Myklebust14516c32010-07-31 14:29:06 -04005327 if (!nfs4_sequence_done(task, &data->res.seq_res))
5328 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005329
Trond Myklebustca8acf82013-08-13 10:36:56 -04005330 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005331 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005332 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005333 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005334 case -NFS4ERR_ADMIN_REVOKED:
5335 case -NFS4ERR_DELEG_REVOKED:
5336 case -NFS4ERR_BAD_STATEID:
5337 case -NFS4ERR_OLD_STATEID:
5338 case -NFS4ERR_STALE_STATEID:
5339 case -NFS4ERR_EXPIRED:
5340 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005341 if (data->roc)
5342 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005343 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005344 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005345 if (nfs4_async_handle_error(task, data->res.server,
5346 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005347 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005348 return;
5349 }
5350 }
5351 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005352}
5353
5354static void nfs4_delegreturn_release(void *calldata)
5355{
Peng Tao039b7562014-07-03 13:05:02 +08005356 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005357 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005358
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005359 if (inode) {
5360 if (data->roc)
5361 pnfs_roc_release(inode);
5362 nfs_iput_and_deactive(inode);
5363 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005364 kfree(calldata);
5365}
5366
Andy Adamson938e1012009-04-01 09:22:28 -04005367static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5368{
5369 struct nfs4_delegreturndata *d_data;
5370
5371 d_data = (struct nfs4_delegreturndata *)data;
5372
Peng Tao500d7012015-09-22 11:35:22 +08005373 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5374 return;
5375
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005376 if (d_data->roc)
5377 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005378
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005379 nfs4_setup_sequence(d_data->res.server,
5380 &d_data->args.seq_args,
5381 &d_data->res.seq_res,
5382 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005383}
Andy Adamson938e1012009-04-01 09:22:28 -04005384
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005385static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005386 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005387 .rpc_call_done = nfs4_delegreturn_done,
5388 .rpc_release = nfs4_delegreturn_release,
5389};
5390
Trond Myklebuste6f81072008-01-24 18:14:34 -05005391static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005392{
5393 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005394 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005395 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005396 struct rpc_message msg = {
5397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5398 .rpc_cred = cred,
5399 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005400 struct rpc_task_setup task_setup_data = {
5401 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005402 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005403 .callback_ops = &nfs4_delegreturn_ops,
5404 .flags = RPC_TASK_ASYNC,
5405 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005406 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005407
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005408 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005409 if (data == NULL)
5410 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005411 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005412 data->args.fhandle = &data->fh;
5413 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005414 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005415 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005416 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005417 data->res.fattr = &data->fattr;
5418 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005419 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005420 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005421 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005422 data->inode = nfs_igrab_and_active(inode);
5423 if (data->inode)
5424 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005425
Trond Myklebustc970aa82007-07-14 15:39:59 -04005426 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005427 msg.rpc_argp = &data->args;
5428 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005429 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005430 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005431 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005432 if (!issync)
5433 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005434 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005435 if (status != 0)
5436 goto out;
5437 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005438 if (status == 0)
5439 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5440 else
5441 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005442out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005443 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005444 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445}
5446
Trond Myklebuste6f81072008-01-24 18:14:34 -05005447int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448{
5449 struct nfs_server *server = NFS_SERVER(inode);
5450 struct nfs4_exception exception = { };
5451 int err;
5452 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005453 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005454 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 switch (err) {
5456 case -NFS4ERR_STALE_STATEID:
5457 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 case 0:
5459 return 0;
5460 }
5461 err = nfs4_handle_exception(server, err, &exception);
5462 } while (exception.retry);
5463 return err;
5464}
5465
5466#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5467#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5468
5469/*
5470 * sleep, with exponential backoff, and retry the LOCK operation.
5471 */
5472static unsigned long
5473nfs4_set_lock_task_retry(unsigned long timeout)
5474{
Colin Cross416ad3c2013-05-06 23:50:06 +00005475 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 timeout <<= 1;
5477 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5478 return NFS4_LOCK_MAXTIMEOUT;
5479 return timeout;
5480}
5481
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5483{
5484 struct inode *inode = state->inode;
5485 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005486 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005487 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005489 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005491 struct nfs_lockt_res res = {
5492 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 };
5494 struct rpc_message msg = {
5495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5496 .rpc_argp = &arg,
5497 .rpc_resp = &res,
5498 .rpc_cred = state->owner->so_cred,
5499 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 struct nfs4_lock_state *lsp;
5501 int status;
5502
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005503 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005504 status = nfs4_set_lock_state(state, request);
5505 if (status != 0)
5506 goto out;
5507 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005508 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005509 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005510 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005511 switch (status) {
5512 case 0:
5513 request->fl_type = F_UNLCK;
5514 break;
5515 case -NFS4ERR_DENIED:
5516 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005518 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005519 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005520out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 return status;
5522}
5523
5524static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5525{
5526 struct nfs4_exception exception = { };
5527 int err;
5528
5529 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005530 err = _nfs4_proc_getlk(state, cmd, request);
5531 trace_nfs4_get_lock(request, state, cmd, err);
5532 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 &exception);
5534 } while (exception.retry);
5535 return err;
5536}
5537
Jeff Layton83bfff22015-07-11 06:43:03 -04005538static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539{
5540 int res = 0;
5541 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5542 case FL_POSIX:
Jeff Layton83bfff22015-07-11 06:43:03 -04005543 res = posix_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 break;
5545 case FL_FLOCK:
Jeff Layton83bfff22015-07-11 06:43:03 -04005546 res = flock_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 break;
5548 default:
5549 BUG();
5550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 return res;
5552}
5553
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005554struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005555 struct nfs_locku_args arg;
5556 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005557 struct nfs4_lock_state *lsp;
5558 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005559 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005560 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005561 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005562};
5563
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005564static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5565 struct nfs_open_context *ctx,
5566 struct nfs4_lock_state *lsp,
5567 struct nfs_seqid *seqid)
5568{
5569 struct nfs4_unlockdata *p;
5570 struct inode *inode = lsp->ls_state->inode;
5571
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005572 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005573 if (p == NULL)
5574 return NULL;
5575 p->arg.fh = NFS_FH(inode);
5576 p->arg.fl = &p->fl;
5577 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005578 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005579 p->lsp = lsp;
5580 atomic_inc(&lsp->ls_count);
5581 /* Ensure we don't close file until we're done freeing locks! */
5582 p->ctx = get_nfs_open_context(ctx);
5583 memcpy(&p->fl, fl, sizeof(p->fl));
5584 p->server = NFS_SERVER(inode);
5585 return p;
5586}
5587
Trond Myklebust06f814a2006-01-03 09:55:07 +01005588static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005589{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005590 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005591 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005592 nfs4_put_lock_state(calldata->lsp);
5593 put_nfs_open_context(calldata->ctx);
5594 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005595}
5596
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005597static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005598{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005599 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005600
Trond Myklebust14516c32010-07-31 14:29:06 -04005601 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5602 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005603 switch (task->tk_status) {
5604 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005605 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005606 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005607 if (nfs4_update_lock_stateid(calldata->lsp,
5608 &calldata->res.stateid))
5609 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005610 case -NFS4ERR_BAD_STATEID:
5611 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005612 case -NFS4ERR_STALE_STATEID:
5613 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005614 if (!nfs4_stateid_match(&calldata->arg.stateid,
5615 &calldata->lsp->ls_stateid))
5616 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005617 break;
5618 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005619 if (nfs4_async_handle_error(task, calldata->server,
5620 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005621 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005622 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005623 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005624}
5625
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005626static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005627{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005628 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005630 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005631 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005632 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005633 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005634 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005635 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005636 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005637 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005638 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005639 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005640 &calldata->res.seq_res,
5641 task) != 0)
5642 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005643 return;
5644out_no_action:
5645 task->tk_action = NULL;
5646out_wait:
5647 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648}
5649
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005650static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005651 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005652 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005653 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005654};
5655
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005656static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5657 struct nfs_open_context *ctx,
5658 struct nfs4_lock_state *lsp,
5659 struct nfs_seqid *seqid)
5660{
5661 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005662 struct rpc_message msg = {
5663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5664 .rpc_cred = ctx->cred,
5665 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005666 struct rpc_task_setup task_setup_data = {
5667 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005668 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005669 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005670 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005671 .flags = RPC_TASK_ASYNC,
5672 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005673
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005674 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5675 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5676
Frank Filz137d6ac2007-07-09 15:32:29 -07005677 /* Ensure this is an unlock - when canceling a lock, the
5678 * canceled lock is passed in, and it won't be an unlock.
5679 */
5680 fl->fl_type = F_UNLCK;
5681
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005682 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5683 if (data == NULL) {
5684 nfs_free_seqid(seqid);
5685 return ERR_PTR(-ENOMEM);
5686 }
5687
Chuck Levera9c92d62013-08-09 12:48:18 -04005688 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005689 msg.rpc_argp = &data->arg;
5690 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005691 task_setup_data.callback_data = data;
5692 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005693}
5694
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5696{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005697 struct inode *inode = state->inode;
5698 struct nfs4_state_owner *sp = state->owner;
5699 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005700 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005701 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005702 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005703 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005704 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005705 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
Trond Myklebust9b073572006-06-29 16:38:34 -04005707 status = nfs4_set_lock_state(state, request);
5708 /* Unlock _before_ we do the RPC call */
5709 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005710 /* Exclude nfs_delegation_claim_locks() */
5711 mutex_lock(&sp->so_delegreturn_mutex);
5712 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005713 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005714 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005715 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005716 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005717 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005718 }
5719 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005720 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005721 if (status != 0)
5722 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005723 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005724 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005725 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5726 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005727 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5728 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005729 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005730 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005731 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005732 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005733 status = PTR_ERR(task);
5734 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005735 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005736 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005737 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005738out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005739 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005740 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005741 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742}
5743
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005744struct nfs4_lockdata {
5745 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005746 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005747 struct nfs4_lock_state *lsp;
5748 struct nfs_open_context *ctx;
5749 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005750 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005751 int rpc_status;
5752 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005753 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005754};
5755
5756static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005757 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5758 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005759{
5760 struct nfs4_lockdata *p;
5761 struct inode *inode = lsp->ls_state->inode;
5762 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005763 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005764
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005765 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005766 if (p == NULL)
5767 return NULL;
5768
5769 p->arg.fh = NFS_FH(inode);
5770 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005771 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005772 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005773 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005774 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5775 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005776 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005777 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005778 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005779 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005780 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005781 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005782 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005783 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005784 atomic_inc(&lsp->ls_count);
5785 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005786 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005787 memcpy(&p->fl, fl, sizeof(p->fl));
5788 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005789out_free_seqid:
5790 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005791out_free:
5792 kfree(p);
5793 return NULL;
5794}
5795
5796static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5797{
5798 struct nfs4_lockdata *data = calldata;
5799 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Harvey Harrison3110ff82008-05-02 13:42:44 -07005801 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005802 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005803 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005804 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005805 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005806 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005807 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005808 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005809 nfs4_stateid_copy(&data->arg.open_stateid,
5810 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005811 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005812 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005813 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005814 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005815 nfs4_stateid_copy(&data->arg.lock_stateid,
5816 &data->lsp->ls_stateid);
5817 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005818 if (!nfs4_valid_open_stateid(state)) {
5819 data->rpc_status = -EBADF;
5820 task->tk_action = NULL;
5821 goto out_release_open_seqid;
5822 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005823 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005824 if (nfs4_setup_sequence(data->server,
5825 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005826 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005827 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005828 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005829out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005830 nfs_release_seqid(data->arg.open_seqid);
5831out_release_lock_seqid:
5832 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005833out_wait:
5834 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005835 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005836}
5837
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005838static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5839{
5840 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005841 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005842
Harvey Harrison3110ff82008-05-02 13:42:44 -07005843 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005844
Trond Myklebust14516c32010-07-31 14:29:06 -04005845 if (!nfs4_sequence_done(task, &data->res.seq_res))
5846 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005847
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005848 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005849 switch (task->tk_status) {
5850 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005851 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005852 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005853 if (data->arg.new_lock) {
5854 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005855 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005856 rpc_restart_call_prepare(task);
5857 break;
5858 }
5859 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005860 if (data->arg.new_lock_owner != 0) {
5861 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5862 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5863 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5864 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5865 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005866 break;
5867 case -NFS4ERR_BAD_STATEID:
5868 case -NFS4ERR_OLD_STATEID:
5869 case -NFS4ERR_STALE_STATEID:
5870 case -NFS4ERR_EXPIRED:
5871 if (data->arg.new_lock_owner != 0) {
5872 if (!nfs4_stateid_match(&data->arg.open_stateid,
5873 &lsp->ls_state->open_stateid))
5874 rpc_restart_call_prepare(task);
5875 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5876 &lsp->ls_stateid))
5877 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005878 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005879 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005880}
5881
5882static void nfs4_lock_release(void *calldata)
5883{
5884 struct nfs4_lockdata *data = calldata;
5885
Harvey Harrison3110ff82008-05-02 13:42:44 -07005886 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005887 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005888 if (data->cancelled != 0) {
5889 struct rpc_task *task;
5890 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5891 data->arg.lock_seqid);
5892 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005893 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005894 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005895 } else
5896 nfs_free_seqid(data->arg.lock_seqid);
5897 nfs4_put_lock_state(data->lsp);
5898 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005899 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005900 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005901 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005902}
5903
5904static const struct rpc_call_ops nfs4_lock_ops = {
5905 .rpc_call_prepare = nfs4_lock_prepare,
5906 .rpc_call_done = nfs4_lock_done,
5907 .rpc_release = nfs4_lock_release,
5908};
5909
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005910static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5911{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005912 switch (error) {
5913 case -NFS4ERR_ADMIN_REVOKED:
5914 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005915 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005916 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005917 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005918 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005919 break;
5920 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005921 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005922 case -NFS4ERR_EXPIRED:
5923 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005924 };
5925}
5926
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005927static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005928{
5929 struct nfs4_lockdata *data;
5930 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005931 struct rpc_message msg = {
5932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5933 .rpc_cred = state->owner->so_cred,
5934 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005935 struct rpc_task_setup task_setup_data = {
5936 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005937 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005938 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005939 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005940 .flags = RPC_TASK_ASYNC,
5941 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005942 int ret;
5943
Harvey Harrison3110ff82008-05-02 13:42:44 -07005944 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005945 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005946 fl->fl_u.nfs4_fl.owner,
5947 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005948 if (data == NULL)
5949 return -ENOMEM;
5950 if (IS_SETLKW(cmd))
5951 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005952 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005953 msg.rpc_argp = &data->arg;
5954 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005955 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005956 if (recovery_type > NFS_LOCK_NEW) {
5957 if (recovery_type == NFS_LOCK_RECLAIM)
5958 data->arg.reclaim = NFS_LOCK_RECLAIM;
5959 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005960 } else
5961 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005962 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005963 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005964 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005965 ret = nfs4_wait_for_completion_rpc_task(task);
5966 if (ret == 0) {
5967 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005968 if (ret)
5969 nfs4_handle_setlk_error(data->server, data->lsp,
5970 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005971 } else
5972 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005973 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005974 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005975 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976}
5977
5978static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5979{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005980 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005981 struct nfs4_exception exception = {
5982 .inode = state->inode,
5983 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005984 int err;
5985
5986 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005987 /* Cache the lock if possible... */
5988 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5989 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005990 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005991 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005992 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005993 break;
5994 nfs4_handle_exception(server, err, &exception);
5995 } while (exception.retry);
5996 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997}
5998
5999static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6000{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006001 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006002 struct nfs4_exception exception = {
6003 .inode = state->inode,
6004 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006005 int err;
6006
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006007 err = nfs4_set_lock_state(state, request);
6008 if (err != 0)
6009 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006010 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006011 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6012 return 0;
6013 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006014 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006015 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6016 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006017 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006018 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006019 switch (err) {
6020 default:
6021 goto out;
6022 case -NFS4ERR_GRACE:
6023 case -NFS4ERR_DELAY:
6024 nfs4_handle_exception(server, err, &exception);
6025 err = 0;
6026 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006027 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006028out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006029 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030}
6031
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006032#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006033/**
6034 * nfs41_check_expired_locks - possibly free a lock stateid
6035 *
6036 * @state: NFSv4 state for an inode
6037 *
6038 * Returns NFS_OK if recovery for this stateid is now finished.
6039 * Otherwise a negative NFS4ERR value is returned.
6040 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006041static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006042{
Chuck Levereb64cf92012-07-11 16:30:05 -04006043 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006044 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006045 struct nfs_server *server = NFS_SERVER(state->inode);
6046
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006047 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006048 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006049 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6050
6051 status = nfs41_test_stateid(server,
6052 &lsp->ls_stateid,
6053 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006054 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006055 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006056 /* Free the stateid unless the server
6057 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006058 if (status != -NFS4ERR_BAD_STATEID)
6059 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006060 &lsp->ls_stateid,
6061 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006062 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006063 ret = status;
6064 }
6065 }
6066 };
6067
6068 return ret;
6069}
6070
6071static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6072{
6073 int status = NFS_OK;
6074
6075 if (test_bit(LK_STATE_IN_USE, &state->flags))
6076 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006077 if (status != NFS_OK)
6078 status = nfs4_lock_expired(state, request);
6079 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006080}
6081#endif
6082
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6084{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006085 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006086 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006087 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006089 if ((fl_flags & FL_POSIX) &&
6090 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6091 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006092 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006093 status = nfs4_set_lock_state(state, request);
6094 if (status != 0)
6095 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006096 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006097 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006098 if (status < 0)
6099 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006100 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006101 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006102 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006103 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006104 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006105 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006106 up_read(&nfsi->rwsem);
6107 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006108 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006109 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006110 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006111out:
6112 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 return status;
6114}
6115
6116static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6117{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006118 struct nfs4_exception exception = {
6119 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006120 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006121 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 int err;
6123
6124 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006125 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006126 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07006127 if (err == -NFS4ERR_DENIED)
6128 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006130 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 } while (exception.retry);
6132 return err;
6133}
6134
6135static int
6136nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6137{
6138 struct nfs_open_context *ctx;
6139 struct nfs4_state *state;
6140 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6141 int status;
6142
6143 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006144 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 state = ctx->state;
6146
6147 if (request->fl_start < 0 || request->fl_end < 0)
6148 return -EINVAL;
6149
Trond Myklebustd9531262009-07-21 19:22:38 -04006150 if (IS_GETLK(cmd)) {
6151 if (state != NULL)
6152 return nfs4_proc_getlk(state, F_GETLK, request);
6153 return 0;
6154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155
6156 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6157 return -EINVAL;
6158
Trond Myklebustd9531262009-07-21 19:22:38 -04006159 if (request->fl_type == F_UNLCK) {
6160 if (state != NULL)
6161 return nfs4_proc_unlck(state, cmd, request);
6162 return 0;
6163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164
Trond Myklebustd9531262009-07-21 19:22:38 -04006165 if (state == NULL)
6166 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006167 /*
6168 * Don't rely on the VFS having checked the file open mode,
6169 * since it won't do this for flock() locks.
6170 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006171 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006172 case F_RDLCK:
6173 if (!(filp->f_mode & FMODE_READ))
6174 return -EBADF;
6175 break;
6176 case F_WRLCK:
6177 if (!(filp->f_mode & FMODE_WRITE))
6178 return -EBADF;
6179 }
6180
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 do {
6182 status = nfs4_proc_setlk(state, cmd, request);
6183 if ((status != -EAGAIN) || IS_SETLK(cmd))
6184 break;
6185 timeout = nfs4_set_lock_task_retry(timeout);
6186 status = -ERESTARTSYS;
6187 if (signalled())
6188 break;
6189 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 return status;
6191}
6192
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006193int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006194{
6195 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006196 int err;
6197
6198 err = nfs4_set_lock_state(state, fl);
6199 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006200 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006201 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006202 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006203}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006204
Trond Myklebustcf470c32012-03-07 13:49:12 -05006205struct nfs_release_lockowner_data {
6206 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006207 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006208 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006209 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006210 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006211};
6212
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006213static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6214{
6215 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006216 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006217 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6218 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006219 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006220 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006221}
6222
6223static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6224{
6225 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006226 struct nfs_server *server = data->server;
6227
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006228 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006229
6230 switch (task->tk_status) {
6231 case 0:
6232 renew_lease(server, data->timestamp);
6233 break;
6234 case -NFS4ERR_STALE_CLIENTID:
6235 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006236 nfs4_schedule_lease_recovery(server->nfs_client);
6237 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006238 case -NFS4ERR_LEASE_MOVED:
6239 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006240 if (nfs4_async_handle_error(task, server,
6241 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006242 rpc_restart_call_prepare(task);
6243 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006244}
6245
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006246static void nfs4_release_lockowner_release(void *calldata)
6247{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006248 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006249 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006250 kfree(calldata);
6251}
6252
Trond Myklebust17280172012-03-11 13:11:00 -04006253static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006254 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6255 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006256 .rpc_release = nfs4_release_lockowner_release,
6257};
6258
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006259static void
6260nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006261{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006262 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006263 struct rpc_message msg = {
6264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6265 };
6266
6267 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006268 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006269
Trond Myklebustcf470c32012-03-07 13:49:12 -05006270 data = kmalloc(sizeof(*data), GFP_NOFS);
6271 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006272 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006273 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006274 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006275 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6276 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6277 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006278
Trond Myklebustcf470c32012-03-07 13:49:12 -05006279 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006280 msg.rpc_resp = &data->res;
6281 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006282 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006283}
6284
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006285#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6286
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006287static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6288 const void *buf, size_t buflen,
6289 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006290{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006291 if (strcmp(key, "") != 0)
6292 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006293
David Howells2b0143b2015-03-17 22:25:59 +00006294 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006295}
6296
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006297static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6298 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006299{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006300 if (strcmp(key, "") != 0)
6301 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006302
David Howells2b0143b2015-03-17 22:25:59 +00006303 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006304}
6305
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006306static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6307 size_t list_len, const char *name,
6308 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006309{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006310 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006311
David Howells2b0143b2015-03-17 22:25:59 +00006312 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
J. Bruce Fields096455a2006-03-20 23:23:42 -05006313 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006314
6315 if (list && len <= list_len)
6316 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006317 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006318}
6319
David Quigleyc9bccef2013-05-22 12:50:45 -04006320#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6321static inline int nfs4_server_supports_labels(struct nfs_server *server)
6322{
6323 return server->caps & NFS_CAP_SECURITY_LABEL;
6324}
6325
6326static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6327 const void *buf, size_t buflen,
6328 int flags, int type)
6329{
6330 if (security_ismaclabel(key))
6331 return nfs4_set_security_label(dentry, buf, buflen);
6332
6333 return -EOPNOTSUPP;
6334}
6335
6336static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6337 void *buf, size_t buflen, int type)
6338{
6339 if (security_ismaclabel(key))
David Howells2b0143b2015-03-17 22:25:59 +00006340 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006341 return -EOPNOTSUPP;
6342}
6343
6344static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6345 size_t list_len, const char *name,
6346 size_t name_len, int type)
6347{
6348 size_t len = 0;
6349
David Howells2b0143b2015-03-17 22:25:59 +00006350 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6351 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
David Quigleyc9bccef2013-05-22 12:50:45 -04006352 if (list && len <= list_len)
David Howells2b0143b2015-03-17 22:25:59 +00006353 security_inode_listsecurity(d_inode(dentry), list, len);
David Quigleyc9bccef2013-05-22 12:50:45 -04006354 }
6355 return len;
6356}
6357
6358static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6359 .prefix = XATTR_SECURITY_PREFIX,
6360 .list = nfs4_xattr_list_nfs4_label,
6361 .get = nfs4_xattr_get_nfs4_label,
6362 .set = nfs4_xattr_set_nfs4_label,
6363};
6364#endif
6365
6366
Andy Adamson533eb462011-06-13 18:25:56 -04006367/*
6368 * nfs_fhget will use either the mounted_on_fileid or the fileid
6369 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006370static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6371{
Andy Adamson533eb462011-06-13 18:25:56 -04006372 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6373 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6374 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006375 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006376 return;
6377
6378 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006379 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006380 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6381 fattr->nlink = 2;
6382}
6383
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006384static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6385 const struct qstr *name,
6386 struct nfs4_fs_locations *fs_locations,
6387 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006388{
6389 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006390 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006391 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006392 };
6393 struct nfs4_fs_locations_arg args = {
6394 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006395 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006396 .page = page,
6397 .bitmask = bitmask,
6398 };
Benny Halevy22958462009-04-01 09:22:02 -04006399 struct nfs4_fs_locations_res res = {
6400 .fs_locations = fs_locations,
6401 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006402 struct rpc_message msg = {
6403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6404 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006405 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006406 };
6407 int status;
6408
Harvey Harrison3110ff82008-05-02 13:42:44 -07006409 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006410
6411 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6412 * is not supported */
6413 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6414 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6415 else
6416 bitmask[0] |= FATTR4_WORD0_FILEID;
6417
Trond Myklebustc228fd32007-01-13 02:28:11 -05006418 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006419 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006420 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006421 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006422 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006423 return status;
6424}
6425
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006426int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6427 const struct qstr *name,
6428 struct nfs4_fs_locations *fs_locations,
6429 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006430{
6431 struct nfs4_exception exception = { };
6432 int err;
6433 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006434 err = _nfs4_proc_fs_locations(client, dir, name,
6435 fs_locations, page);
6436 trace_nfs4_get_fs_locations(dir, name, err);
6437 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006438 &exception);
6439 } while (exception.retry);
6440 return err;
6441}
6442
Chuck Leverb03d7352013-10-17 14:12:50 -04006443/*
6444 * This operation also signals the server that this client is
6445 * performing migration recovery. The server can stop returning
6446 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6447 * appended to this compound to identify the client ID which is
6448 * performing recovery.
6449 */
6450static int _nfs40_proc_get_locations(struct inode *inode,
6451 struct nfs4_fs_locations *locations,
6452 struct page *page, struct rpc_cred *cred)
6453{
6454 struct nfs_server *server = NFS_SERVER(inode);
6455 struct rpc_clnt *clnt = server->client;
6456 u32 bitmask[2] = {
6457 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6458 };
6459 struct nfs4_fs_locations_arg args = {
6460 .clientid = server->nfs_client->cl_clientid,
6461 .fh = NFS_FH(inode),
6462 .page = page,
6463 .bitmask = bitmask,
6464 .migration = 1, /* skip LOOKUP */
6465 .renew = 1, /* append RENEW */
6466 };
6467 struct nfs4_fs_locations_res res = {
6468 .fs_locations = locations,
6469 .migration = 1,
6470 .renew = 1,
6471 };
6472 struct rpc_message msg = {
6473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6474 .rpc_argp = &args,
6475 .rpc_resp = &res,
6476 .rpc_cred = cred,
6477 };
6478 unsigned long now = jiffies;
6479 int status;
6480
6481 nfs_fattr_init(&locations->fattr);
6482 locations->server = server;
6483 locations->nlocations = 0;
6484
6485 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6486 nfs4_set_sequence_privileged(&args.seq_args);
6487 status = nfs4_call_sync_sequence(clnt, server, &msg,
6488 &args.seq_args, &res.seq_res);
6489 if (status)
6490 return status;
6491
6492 renew_lease(server, now);
6493 return 0;
6494}
6495
6496#ifdef CONFIG_NFS_V4_1
6497
6498/*
6499 * This operation also signals the server that this client is
6500 * performing migration recovery. The server can stop asserting
6501 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6502 * performing this operation is identified in the SEQUENCE
6503 * operation in this compound.
6504 *
6505 * When the client supports GETATTR(fs_locations_info), it can
6506 * be plumbed in here.
6507 */
6508static int _nfs41_proc_get_locations(struct inode *inode,
6509 struct nfs4_fs_locations *locations,
6510 struct page *page, struct rpc_cred *cred)
6511{
6512 struct nfs_server *server = NFS_SERVER(inode);
6513 struct rpc_clnt *clnt = server->client;
6514 u32 bitmask[2] = {
6515 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6516 };
6517 struct nfs4_fs_locations_arg args = {
6518 .fh = NFS_FH(inode),
6519 .page = page,
6520 .bitmask = bitmask,
6521 .migration = 1, /* skip LOOKUP */
6522 };
6523 struct nfs4_fs_locations_res res = {
6524 .fs_locations = locations,
6525 .migration = 1,
6526 };
6527 struct rpc_message msg = {
6528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6529 .rpc_argp = &args,
6530 .rpc_resp = &res,
6531 .rpc_cred = cred,
6532 };
6533 int status;
6534
6535 nfs_fattr_init(&locations->fattr);
6536 locations->server = server;
6537 locations->nlocations = 0;
6538
6539 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6540 nfs4_set_sequence_privileged(&args.seq_args);
6541 status = nfs4_call_sync_sequence(clnt, server, &msg,
6542 &args.seq_args, &res.seq_res);
6543 if (status == NFS4_OK &&
6544 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6545 status = -NFS4ERR_LEASE_MOVED;
6546 return status;
6547}
6548
6549#endif /* CONFIG_NFS_V4_1 */
6550
6551/**
6552 * nfs4_proc_get_locations - discover locations for a migrated FSID
6553 * @inode: inode on FSID that is migrating
6554 * @locations: result of query
6555 * @page: buffer
6556 * @cred: credential to use for this operation
6557 *
6558 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6559 * operation failed, or a negative errno if a local error occurred.
6560 *
6561 * On success, "locations" is filled in, but if the server has
6562 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6563 * asserted.
6564 *
6565 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6566 * from this client that require migration recovery.
6567 */
6568int nfs4_proc_get_locations(struct inode *inode,
6569 struct nfs4_fs_locations *locations,
6570 struct page *page, struct rpc_cred *cred)
6571{
6572 struct nfs_server *server = NFS_SERVER(inode);
6573 struct nfs_client *clp = server->nfs_client;
6574 const struct nfs4_mig_recovery_ops *ops =
6575 clp->cl_mvops->mig_recovery_ops;
6576 struct nfs4_exception exception = { };
6577 int status;
6578
6579 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6580 (unsigned long long)server->fsid.major,
6581 (unsigned long long)server->fsid.minor,
6582 clp->cl_hostname);
6583 nfs_display_fhandle(NFS_FH(inode), __func__);
6584
6585 do {
6586 status = ops->get_locations(inode, locations, page, cred);
6587 if (status != -NFS4ERR_DELAY)
6588 break;
6589 nfs4_handle_exception(server, status, &exception);
6590 } while (exception.retry);
6591 return status;
6592}
6593
Chuck Lever44c99932013-10-17 14:13:30 -04006594/*
6595 * This operation also signals the server that this client is
6596 * performing "lease moved" recovery. The server can stop
6597 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6598 * is appended to this compound to identify the client ID which is
6599 * performing recovery.
6600 */
6601static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6602{
6603 struct nfs_server *server = NFS_SERVER(inode);
6604 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6605 struct rpc_clnt *clnt = server->client;
6606 struct nfs4_fsid_present_arg args = {
6607 .fh = NFS_FH(inode),
6608 .clientid = clp->cl_clientid,
6609 .renew = 1, /* append RENEW */
6610 };
6611 struct nfs4_fsid_present_res res = {
6612 .renew = 1,
6613 };
6614 struct rpc_message msg = {
6615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6616 .rpc_argp = &args,
6617 .rpc_resp = &res,
6618 .rpc_cred = cred,
6619 };
6620 unsigned long now = jiffies;
6621 int status;
6622
6623 res.fh = nfs_alloc_fhandle();
6624 if (res.fh == NULL)
6625 return -ENOMEM;
6626
6627 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6628 nfs4_set_sequence_privileged(&args.seq_args);
6629 status = nfs4_call_sync_sequence(clnt, server, &msg,
6630 &args.seq_args, &res.seq_res);
6631 nfs_free_fhandle(res.fh);
6632 if (status)
6633 return status;
6634
6635 do_renew_lease(clp, now);
6636 return 0;
6637}
6638
6639#ifdef CONFIG_NFS_V4_1
6640
6641/*
6642 * This operation also signals the server that this client is
6643 * performing "lease moved" recovery. The server can stop asserting
6644 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6645 * this operation is identified in the SEQUENCE operation in this
6646 * compound.
6647 */
6648static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6649{
6650 struct nfs_server *server = NFS_SERVER(inode);
6651 struct rpc_clnt *clnt = server->client;
6652 struct nfs4_fsid_present_arg args = {
6653 .fh = NFS_FH(inode),
6654 };
6655 struct nfs4_fsid_present_res res = {
6656 };
6657 struct rpc_message msg = {
6658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6659 .rpc_argp = &args,
6660 .rpc_resp = &res,
6661 .rpc_cred = cred,
6662 };
6663 int status;
6664
6665 res.fh = nfs_alloc_fhandle();
6666 if (res.fh == NULL)
6667 return -ENOMEM;
6668
6669 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6670 nfs4_set_sequence_privileged(&args.seq_args);
6671 status = nfs4_call_sync_sequence(clnt, server, &msg,
6672 &args.seq_args, &res.seq_res);
6673 nfs_free_fhandle(res.fh);
6674 if (status == NFS4_OK &&
6675 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6676 status = -NFS4ERR_LEASE_MOVED;
6677 return status;
6678}
6679
6680#endif /* CONFIG_NFS_V4_1 */
6681
6682/**
6683 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6684 * @inode: inode on FSID to check
6685 * @cred: credential to use for this operation
6686 *
6687 * Server indicates whether the FSID is present, moved, or not
6688 * recognized. This operation is necessary to clear a LEASE_MOVED
6689 * condition for this client ID.
6690 *
6691 * Returns NFS4_OK if the FSID is present on this server,
6692 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6693 * NFS4ERR code if some error occurred on the server, or a
6694 * negative errno if a local failure occurred.
6695 */
6696int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6697{
6698 struct nfs_server *server = NFS_SERVER(inode);
6699 struct nfs_client *clp = server->nfs_client;
6700 const struct nfs4_mig_recovery_ops *ops =
6701 clp->cl_mvops->mig_recovery_ops;
6702 struct nfs4_exception exception = { };
6703 int status;
6704
6705 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6706 (unsigned long long)server->fsid.major,
6707 (unsigned long long)server->fsid.minor,
6708 clp->cl_hostname);
6709 nfs_display_fhandle(NFS_FH(inode), __func__);
6710
6711 do {
6712 status = ops->fsid_present(inode, cred);
6713 if (status != -NFS4ERR_DELAY)
6714 break;
6715 nfs4_handle_exception(server, status, &exception);
6716 } while (exception.retry);
6717 return status;
6718}
6719
Andy Adamson5ec16a82013-08-08 10:57:55 -04006720/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006721 * If 'use_integrity' is true and the state managment nfs_client
6722 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6723 * and the machine credential as per RFC3530bis and RFC5661 Security
6724 * Considerations sections. Otherwise, just use the user cred with the
6725 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006726 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006727static 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 +00006728{
6729 int status;
6730 struct nfs4_secinfo_arg args = {
6731 .dir_fh = NFS_FH(dir),
6732 .name = name,
6733 };
6734 struct nfs4_secinfo_res res = {
6735 .flavors = flavors,
6736 };
6737 struct rpc_message msg = {
6738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6739 .rpc_argp = &args,
6740 .rpc_resp = &res,
6741 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006742 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006743 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006744
6745 if (use_integrity) {
6746 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006747 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6748 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006749 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006750
6751 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006752
6753 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6754 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6755
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006756 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6757 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006758 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006759
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006760 if (cred)
6761 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006762
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006763 return status;
6764}
6765
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006766int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6767 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006768{
6769 struct nfs4_exception exception = { };
6770 int err;
6771 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006772 err = -NFS4ERR_WRONGSEC;
6773
6774 /* try to use integrity protection with machine cred */
6775 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6776 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6777
6778 /*
6779 * if unable to use integrity protection, or SECINFO with
6780 * integrity protection returns NFS4ERR_WRONGSEC (which is
6781 * disallowed by spec, but exists in deployed servers) use
6782 * the current filesystem's rpc_client and the user cred.
6783 */
6784 if (err == -NFS4ERR_WRONGSEC)
6785 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6786
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006787 trace_nfs4_secinfo(dir, name, err);
6788 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006789 &exception);
6790 } while (exception.retry);
6791 return err;
6792}
6793
Andy Adamson557134a2009-04-01 09:21:53 -04006794#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006795/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006796 * Check the exchange flags returned by the server for invalid flags, having
6797 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6798 * DS flags set.
6799 */
6800static int nfs4_check_cl_exchange_flags(u32 flags)
6801{
6802 if (flags & ~EXCHGID4_FLAG_MASK_R)
6803 goto out_inval;
6804 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6805 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6806 goto out_inval;
6807 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6808 goto out_inval;
6809 return NFS_OK;
6810out_inval:
6811 return -NFS4ERR_INVAL;
6812}
6813
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006814static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006815nfs41_same_server_scope(struct nfs41_server_scope *a,
6816 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006817{
6818 if (a->server_scope_sz == b->server_scope_sz &&
6819 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6820 return true;
6821
6822 return false;
6823}
6824
Andy Adamson357f54d2010-12-14 10:11:57 -05006825/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006826 * nfs4_proc_bind_conn_to_session()
6827 *
6828 * The 4.1 client currently uses the same TCP connection for the
6829 * fore and backchannel.
6830 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006831int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006832{
6833 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006834 struct nfs41_bind_conn_to_session_args args = {
6835 .client = clp,
6836 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6837 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006838 struct nfs41_bind_conn_to_session_res res;
6839 struct rpc_message msg = {
6840 .rpc_proc =
6841 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006842 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006843 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006844 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006845 };
6846
6847 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006848
Trond Myklebust71a097c2015-02-18 09:27:18 -08006849 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6850 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6851 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006852
6853 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006854 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006855 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006856 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006857 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6858 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6859 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006860 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006861 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006862 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006863 dprintk("NFS: %s: Unexpected direction from server\n",
6864 __func__);
6865 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006866 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006867 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006868 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006869 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6870 __func__);
6871 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006872 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006873 }
6874 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006875out:
6876 dprintk("<-- %s status= %d\n", __func__, status);
6877 return status;
6878}
6879
6880/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006881 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6882 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006883 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006884static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6885 .how = SP4_MACH_CRED,
6886 .enforce.u.words = {
6887 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6888 1 << (OP_EXCHANGE_ID - 32) |
6889 1 << (OP_CREATE_SESSION - 32) |
6890 1 << (OP_DESTROY_SESSION - 32) |
6891 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006892 },
6893 .allow.u.words = {
6894 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006895 1 << (OP_LOCKU) |
6896 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006897 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006898 1 << (OP_SECINFO_NO_NAME - 32) |
6899 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006900 1 << (OP_FREE_STATEID - 32) |
6901 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006902 }
6903};
6904
6905/*
6906 * Select the state protection mode for client `clp' given the server results
6907 * from exchange_id in `sp'.
6908 *
6909 * Returns 0 on success, negative errno otherwise.
6910 */
6911static int nfs4_sp4_select_mode(struct nfs_client *clp,
6912 struct nfs41_state_protection *sp)
6913{
6914 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6915 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6916 1 << (OP_EXCHANGE_ID - 32) |
6917 1 << (OP_CREATE_SESSION - 32) |
6918 1 << (OP_DESTROY_SESSION - 32) |
6919 1 << (OP_DESTROY_CLIENTID - 32)
6920 };
6921 unsigned int i;
6922
6923 if (sp->how == SP4_MACH_CRED) {
6924 /* Print state protect result */
6925 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6926 for (i = 0; i <= LAST_NFS4_OP; i++) {
6927 if (test_bit(i, sp->enforce.u.longs))
6928 dfprintk(MOUNT, " enforce op %d\n", i);
6929 if (test_bit(i, sp->allow.u.longs))
6930 dfprintk(MOUNT, " allow op %d\n", i);
6931 }
6932
6933 /* make sure nothing is on enforce list that isn't supported */
6934 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6935 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6936 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6937 return -EINVAL;
6938 }
6939 }
6940
6941 /*
6942 * Minimal mode - state operations are allowed to use machine
6943 * credential. Note this already happens by default, so the
6944 * client doesn't have to do anything more than the negotiation.
6945 *
6946 * NOTE: we don't care if EXCHANGE_ID is in the list -
6947 * we're already using the machine cred for exchange_id
6948 * and will never use a different cred.
6949 */
6950 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6951 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6952 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6953 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6954 dfprintk(MOUNT, "sp4_mach_cred:\n");
6955 dfprintk(MOUNT, " minimal mode enabled\n");
6956 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6957 } else {
6958 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6959 return -EINVAL;
6960 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006961
6962 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6963 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6964 dfprintk(MOUNT, " cleanup mode enabled\n");
6965 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6966 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006967
6968 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6969 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6970 dfprintk(MOUNT, " secinfo mode enabled\n");
6971 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6972 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006973
6974 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6975 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6976 dfprintk(MOUNT, " stateid mode enabled\n");
6977 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6978 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006979
6980 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6981 dfprintk(MOUNT, " write mode enabled\n");
6982 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6983 }
6984
6985 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6986 dfprintk(MOUNT, " commit mode enabled\n");
6987 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6988 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006989 }
6990
6991 return 0;
6992}
6993
6994/*
6995 * _nfs4_proc_exchange_id()
6996 *
6997 * Wrapper for EXCHANGE_ID operation.
6998 */
6999static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7000 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007001{
7002 nfs4_verifier verifier;
7003 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05007004 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04007005 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04007006#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04007007 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04007008 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7009 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7010#else
7011 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7012 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7013#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04007014 };
7015 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04007016 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04007017 };
7018 int status;
7019 struct rpc_message msg = {
7020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7021 .rpc_argp = &args,
7022 .rpc_resp = &res,
7023 .rpc_cred = cred,
7024 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04007025
Chuck Leverf0920752012-05-21 22:45:41 -04007026 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007027
7028 status = nfs4_init_uniform_client_string(clp);
7029 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04007030 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007031
7032 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007033 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007034 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007035
Chuck Leveracdeb692012-05-21 22:46:16 -04007036 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007037 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04007038 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007039 status = -ENOMEM;
7040 goto out;
7041 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007042
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007043 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007044 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007045 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04007046 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04007047 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007048 }
7049
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007050 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007051 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007052 status = -ENOMEM;
7053 goto out_server_scope;
7054 }
7055
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007056 switch (sp4_how) {
7057 case SP4_NONE:
7058 args.state_protect.how = SP4_NONE;
7059 break;
7060
7061 case SP4_MACH_CRED:
7062 args.state_protect = nfs4_sp4_mach_cred_request;
7063 break;
7064
7065 default:
7066 /* unsupported! */
7067 WARN_ON_ONCE(1);
7068 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007069 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007070 }
7071
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007072 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007073 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04007074 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04007075 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007076
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007077 if (status == 0)
7078 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7079
Chuck Lever177313f2012-05-21 22:44:58 -04007080 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04007081 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007082 clp->cl_exchange_flags = res.flags;
7083 /* Client ID is not confirmed */
7084 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7085 clear_bit(NFS4_SESSION_ESTABLISHED,
7086 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007087 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007088 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007089
Chuck Leveracdeb692012-05-21 22:46:16 -04007090 kfree(clp->cl_serverowner);
7091 clp->cl_serverowner = res.server_owner;
7092 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007093
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007094 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007095 kfree(clp->cl_implid);
7096 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007097 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007098
Chuck Lever177313f2012-05-21 22:44:58 -04007099 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007100 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007101 res.server_scope)) {
7102 dprintk("%s: server_scope mismatch detected\n",
7103 __func__);
7104 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007105 kfree(clp->cl_serverscope);
7106 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007107 }
7108
Chuck Lever177313f2012-05-21 22:44:58 -04007109 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007110 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007111 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007112 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007113 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007114
Kinglong Mee6b559702015-07-01 11:54:53 +08007115out_impl_id:
7116 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007117out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007118 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007119out_server_owner:
7120 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007121out:
Chuck Lever177313f2012-05-21 22:44:58 -04007122 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007123 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007124 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007125 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007126 clp->cl_implid->date.seconds,
7127 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007128 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007129 return status;
7130}
7131
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007132/*
7133 * nfs4_proc_exchange_id()
7134 *
7135 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7136 *
7137 * Since the clientid has expired, all compounds using sessions
7138 * associated with the stale clientid will be returning
7139 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7140 * be in some phase of session reset.
7141 *
7142 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7143 */
7144int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7145{
7146 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7147 int status;
7148
7149 /* try SP4_MACH_CRED if krb5i/p */
7150 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7151 authflavor == RPC_AUTH_GSS_KRB5P) {
7152 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7153 if (!status)
7154 return 0;
7155 }
7156
7157 /* try SP4_NONE */
7158 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7159}
7160
Trond Myklebust66245532012-05-25 17:18:09 -04007161static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7162 struct rpc_cred *cred)
7163{
7164 struct rpc_message msg = {
7165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7166 .rpc_argp = clp,
7167 .rpc_cred = cred,
7168 };
7169 int status;
7170
7171 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007172 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007173 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007174 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007175 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7176 return status;
7177}
7178
7179static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7180 struct rpc_cred *cred)
7181{
7182 unsigned int loop;
7183 int ret;
7184
7185 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7186 ret = _nfs4_proc_destroy_clientid(clp, cred);
7187 switch (ret) {
7188 case -NFS4ERR_DELAY:
7189 case -NFS4ERR_CLIENTID_BUSY:
7190 ssleep(1);
7191 break;
7192 default:
7193 return ret;
7194 }
7195 }
7196 return 0;
7197}
7198
7199int nfs4_destroy_clientid(struct nfs_client *clp)
7200{
7201 struct rpc_cred *cred;
7202 int ret = 0;
7203
7204 if (clp->cl_mvops->minor_version < 1)
7205 goto out;
7206 if (clp->cl_exchange_flags == 0)
7207 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007208 if (clp->cl_preserve_clid)
7209 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007210 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007211 ret = nfs4_proc_destroy_clientid(clp, cred);
7212 if (cred)
7213 put_rpccred(cred);
7214 switch (ret) {
7215 case 0:
7216 case -NFS4ERR_STALE_CLIENTID:
7217 clp->cl_exchange_flags = 0;
7218 }
7219out:
7220 return ret;
7221}
7222
Andy Adamson2050f0c2009-04-01 09:22:30 -04007223struct nfs4_get_lease_time_data {
7224 struct nfs4_get_lease_time_args *args;
7225 struct nfs4_get_lease_time_res *res;
7226 struct nfs_client *clp;
7227};
7228
7229static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7230 void *calldata)
7231{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007232 struct nfs4_get_lease_time_data *data =
7233 (struct nfs4_get_lease_time_data *)calldata;
7234
7235 dprintk("--> %s\n", __func__);
7236 /* just setup sequence, do not trigger session recovery
7237 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007238 nfs41_setup_sequence(data->clp->cl_session,
7239 &data->args->la_seq_args,
7240 &data->res->lr_seq_res,
7241 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007242 dprintk("<-- %s\n", __func__);
7243}
7244
7245/*
7246 * Called from nfs4_state_manager thread for session setup, so don't recover
7247 * from sequence operation or clientid errors.
7248 */
7249static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7250{
7251 struct nfs4_get_lease_time_data *data =
7252 (struct nfs4_get_lease_time_data *)calldata;
7253
7254 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007255 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7256 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007257 switch (task->tk_status) {
7258 case -NFS4ERR_DELAY:
7259 case -NFS4ERR_GRACE:
7260 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7261 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7262 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007263 /* fall through */
7264 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007265 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007266 return;
7267 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007268 dprintk("<-- %s\n", __func__);
7269}
7270
Trond Myklebust17280172012-03-11 13:11:00 -04007271static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007272 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7273 .rpc_call_done = nfs4_get_lease_time_done,
7274};
7275
7276int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7277{
7278 struct rpc_task *task;
7279 struct nfs4_get_lease_time_args args;
7280 struct nfs4_get_lease_time_res res = {
7281 .lr_fsinfo = fsinfo,
7282 };
7283 struct nfs4_get_lease_time_data data = {
7284 .args = &args,
7285 .res = &res,
7286 .clp = clp,
7287 };
7288 struct rpc_message msg = {
7289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7290 .rpc_argp = &args,
7291 .rpc_resp = &res,
7292 };
7293 struct rpc_task_setup task_setup = {
7294 .rpc_client = clp->cl_rpcclient,
7295 .rpc_message = &msg,
7296 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007297 .callback_data = &data,
7298 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007299 };
7300 int status;
7301
Chuck Levera9c92d62013-08-09 12:48:18 -04007302 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007303 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007304 dprintk("--> %s\n", __func__);
7305 task = rpc_run_task(&task_setup);
7306
7307 if (IS_ERR(task))
7308 status = PTR_ERR(task);
7309 else {
7310 status = task->tk_status;
7311 rpc_put_task(task);
7312 }
7313 dprintk("<-- %s return %d\n", __func__, status);
7314
7315 return status;
7316}
7317
Andy Adamsonfc931582009-04-01 09:22:31 -04007318/*
7319 * Initialize the values to be used by the client in CREATE_SESSION
7320 * If nfs4_init_session set the fore channel request and response sizes,
7321 * use them.
7322 *
7323 * Set the back channel max_resp_sz_cached to zero to force the client to
7324 * always set csa_cachethis to FALSE because the current implementation
7325 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7326 */
7327static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7328{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007329 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007330
Andy Adamson18aad3d2013-06-26 12:21:49 -04007331 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7332 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7333
Andy Adamsonfc931582009-04-01 09:22:31 -04007334 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007335 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7336 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007337 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007338 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007339
7340 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007341 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007342 __func__,
7343 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007344 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007345
7346 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007347 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7348 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7349 args->bc_attrs.max_resp_sz_cached = 0;
7350 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7351 args->bc_attrs.max_reqs = 1;
7352
7353 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7354 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7355 __func__,
7356 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7357 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7358 args->bc_attrs.max_reqs);
7359}
7360
Trond Myklebust79969dd2015-02-18 11:30:18 -08007361static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7362 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007363{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007364 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007365 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007366
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007367 if (rcvd->max_resp_sz > sent->max_resp_sz)
7368 return -EINVAL;
7369 /*
7370 * Our requested max_ops is the minimum we need; we're not
7371 * prepared to break up compounds into smaller pieces than that.
7372 * So, no point even trying to continue if the server won't
7373 * cooperate:
7374 */
7375 if (rcvd->max_ops < sent->max_ops)
7376 return -EINVAL;
7377 if (rcvd->max_reqs == 0)
7378 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007379 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7380 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007381 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007382}
7383
Trond Myklebust79969dd2015-02-18 11:30:18 -08007384static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7385 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007386{
7387 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007388 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007389
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007390 if (!(res->flags & SESSION4_BACK_CHAN))
7391 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007392 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7393 return -EINVAL;
7394 if (rcvd->max_resp_sz < sent->max_resp_sz)
7395 return -EINVAL;
7396 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7397 return -EINVAL;
7398 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007399 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007400 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007401 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007402 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007403out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007404 return 0;
7405}
Andy Adamson8d353012009-04-01 09:22:32 -04007406
Andy Adamson8d353012009-04-01 09:22:32 -04007407static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007408 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007409{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007410 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007411
Trond Myklebust79969dd2015-02-18 11:30:18 -08007412 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007413 if (ret)
7414 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007415 return nfs4_verify_back_channel_attrs(args, res);
7416}
7417
7418static void nfs4_update_session(struct nfs4_session *session,
7419 struct nfs41_create_session_res *res)
7420{
7421 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007422 /* Mark client id and session as being confirmed */
7423 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7424 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007425 session->flags = res->flags;
7426 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007427 if (res->flags & SESSION4_BACK_CHAN)
7428 memcpy(&session->bc_attrs, &res->bc_attrs,
7429 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007430}
7431
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007432static int _nfs4_proc_create_session(struct nfs_client *clp,
7433 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007434{
7435 struct nfs4_session *session = clp->cl_session;
7436 struct nfs41_create_session_args args = {
7437 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007438 .clientid = clp->cl_clientid,
7439 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007440 .cb_program = NFS4_CALLBACK,
7441 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007442 struct nfs41_create_session_res res;
7443
Andy Adamsonfc931582009-04-01 09:22:31 -04007444 struct rpc_message msg = {
7445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7446 .rpc_argp = &args,
7447 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007448 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007449 };
7450 int status;
7451
7452 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007453 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007454
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007455 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007456 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007457
Trond Myklebust43095d32012-11-20 11:13:12 -05007458 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007459 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007460 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007461 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007462 if (clp->cl_seqid == res.seqid)
7463 clp->cl_seqid++;
7464 if (status)
7465 goto out;
7466 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007467 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007468out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007469 return status;
7470}
7471
7472/*
7473 * Issues a CREATE_SESSION operation to the server.
7474 * It is the responsibility of the caller to verify the session is
7475 * expired before calling this routine.
7476 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007477int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007478{
7479 int status;
7480 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007481 struct nfs4_session *session = clp->cl_session;
7482
7483 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7484
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007485 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007486 if (status)
7487 goto out;
7488
Andy Adamsonaacd5532011-11-09 13:58:21 -05007489 /* Init or reset the session slot tables */
7490 status = nfs4_setup_session_slot_tables(session);
7491 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007492 if (status)
7493 goto out;
7494
7495 ptr = (unsigned *)&session->sess_id.data[0];
7496 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7497 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007498out:
7499 dprintk("<-- %s\n", __func__);
7500 return status;
7501}
7502
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007503/*
7504 * Issue the over-the-wire RPC DESTROY_SESSION.
7505 * The caller must serialize access to this routine.
7506 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007507int nfs4_proc_destroy_session(struct nfs4_session *session,
7508 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007509{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007510 struct rpc_message msg = {
7511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7512 .rpc_argp = session,
7513 .rpc_cred = cred,
7514 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007515 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007516
7517 dprintk("--> nfs4_proc_destroy_session\n");
7518
7519 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007520 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7521 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007522
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007523 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007524 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007525
7526 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007527 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007528 "Session has been destroyed regardless...\n", status);
7529
7530 dprintk("<-- nfs4_proc_destroy_session\n");
7531 return status;
7532}
7533
Trond Myklebust7b38c362012-05-23 13:23:31 -04007534/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007535 * Renew the cl_session lease.
7536 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007537struct nfs4_sequence_data {
7538 struct nfs_client *clp;
7539 struct nfs4_sequence_args args;
7540 struct nfs4_sequence_res res;
7541};
7542
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007543static void nfs41_sequence_release(void *data)
7544{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007545 struct nfs4_sequence_data *calldata = data;
7546 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007547
Alexandros Batsakis71358402010-02-05 03:45:05 -08007548 if (atomic_read(&clp->cl_count) > 1)
7549 nfs4_schedule_state_renewal(clp);
7550 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007551 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007552}
7553
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007554static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7555{
7556 switch(task->tk_status) {
7557 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007558 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7559 return -EAGAIN;
7560 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007561 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007562 }
7563 return 0;
7564}
7565
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007566static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007567{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007568 struct nfs4_sequence_data *calldata = data;
7569 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007570
Trond Myklebust14516c32010-07-31 14:29:06 -04007571 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7572 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007573
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007574 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007575 if (task->tk_status < 0) {
7576 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007577 if (atomic_read(&clp->cl_count) == 1)
7578 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007579
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007580 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7581 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007582 return;
7583 }
7584 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007585 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007586out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007587 dprintk("<-- %s\n", __func__);
7588}
7589
7590static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7591{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007592 struct nfs4_sequence_data *calldata = data;
7593 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007594 struct nfs4_sequence_args *args;
7595 struct nfs4_sequence_res *res;
7596
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007597 args = task->tk_msg.rpc_argp;
7598 res = task->tk_msg.rpc_resp;
7599
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007600 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007601}
7602
7603static const struct rpc_call_ops nfs41_sequence_ops = {
7604 .rpc_call_done = nfs41_sequence_call_done,
7605 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007606 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007607};
7608
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007609static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7610 struct rpc_cred *cred,
7611 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007612{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007613 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007614 struct rpc_message msg = {
7615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7616 .rpc_cred = cred,
7617 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007618 struct rpc_task_setup task_setup_data = {
7619 .rpc_client = clp->cl_rpcclient,
7620 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007621 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007622 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007623 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007624
Alexandros Batsakis71358402010-02-05 03:45:05 -08007625 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007626 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007627 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007628 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007629 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007630 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007631 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007632 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007633 if (is_privileged)
7634 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007635 msg.rpc_argp = &calldata->args;
7636 msg.rpc_resp = &calldata->res;
7637 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007638 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007639
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007640 return rpc_run_task(&task_setup_data);
7641}
7642
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007643static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007644{
7645 struct rpc_task *task;
7646 int ret = 0;
7647
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007648 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007649 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007650 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007651 if (IS_ERR(task))
7652 ret = PTR_ERR(task);
7653 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007654 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007655 dprintk("<-- %s status=%d\n", __func__, ret);
7656 return ret;
7657}
7658
7659static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7660{
7661 struct rpc_task *task;
7662 int ret;
7663
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007664 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007665 if (IS_ERR(task)) {
7666 ret = PTR_ERR(task);
7667 goto out;
7668 }
7669 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007670 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007671 ret = task->tk_status;
7672 rpc_put_task(task);
7673out:
7674 dprintk("<-- %s status=%d\n", __func__, ret);
7675 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007676}
7677
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007678struct nfs4_reclaim_complete_data {
7679 struct nfs_client *clp;
7680 struct nfs41_reclaim_complete_args arg;
7681 struct nfs41_reclaim_complete_res res;
7682};
7683
7684static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7685{
7686 struct nfs4_reclaim_complete_data *calldata = data;
7687
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007688 nfs41_setup_sequence(calldata->clp->cl_session,
7689 &calldata->arg.seq_args,
7690 &calldata->res.seq_res,
7691 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007692}
7693
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007694static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7695{
7696 switch(task->tk_status) {
7697 case 0:
7698 case -NFS4ERR_COMPLETE_ALREADY:
7699 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7700 break;
7701 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007702 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007703 /* fall through */
7704 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007705 return -EAGAIN;
7706 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007707 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007708 }
7709 return 0;
7710}
7711
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007712static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7713{
7714 struct nfs4_reclaim_complete_data *calldata = data;
7715 struct nfs_client *clp = calldata->clp;
7716 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7717
7718 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007719 if (!nfs41_sequence_done(task, res))
7720 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007721
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007722 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007723 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7724 rpc_restart_call_prepare(task);
7725 return;
7726 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007727 dprintk("<-- %s\n", __func__);
7728}
7729
7730static void nfs4_free_reclaim_complete_data(void *data)
7731{
7732 struct nfs4_reclaim_complete_data *calldata = data;
7733
7734 kfree(calldata);
7735}
7736
7737static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7738 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7739 .rpc_call_done = nfs4_reclaim_complete_done,
7740 .rpc_release = nfs4_free_reclaim_complete_data,
7741};
7742
7743/*
7744 * Issue a global reclaim complete.
7745 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007746static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7747 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007748{
7749 struct nfs4_reclaim_complete_data *calldata;
7750 struct rpc_task *task;
7751 struct rpc_message msg = {
7752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007753 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007754 };
7755 struct rpc_task_setup task_setup_data = {
7756 .rpc_client = clp->cl_rpcclient,
7757 .rpc_message = &msg,
7758 .callback_ops = &nfs4_reclaim_complete_call_ops,
7759 .flags = RPC_TASK_ASYNC,
7760 };
7761 int status = -ENOMEM;
7762
7763 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007764 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007765 if (calldata == NULL)
7766 goto out;
7767 calldata->clp = clp;
7768 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007769
Chuck Levera9c92d62013-08-09 12:48:18 -04007770 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007771 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007772 msg.rpc_argp = &calldata->arg;
7773 msg.rpc_resp = &calldata->res;
7774 task_setup_data.callback_data = calldata;
7775 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007776 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007777 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007778 goto out;
7779 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007780 status = nfs4_wait_for_completion_rpc_task(task);
7781 if (status == 0)
7782 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007783 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007784 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007785out:
7786 dprintk("<-- %s status=%d\n", __func__, status);
7787 return status;
7788}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007789
7790static void
7791nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7792{
7793 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007794 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007795 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007796
7797 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007798 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7799 * right now covering the LAYOUTGET we are about to send.
7800 * However, that is not so catastrophic, and there seems
7801 * to be no way to prevent it completely.
7802 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007803 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007804 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007805 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007806 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7807 NFS_I(lgp->args.inode)->layout,
Peng Taoce6ab4f2014-09-06 00:53:24 +08007808 &lgp->args.range,
Fred Isamancf7d63f2011-01-06 11:36:25 +00007809 lgp->args.ctx->state)) {
7810 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007811 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007812}
7813
7814static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7815{
7816 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007817 struct inode *inode = lgp->args.inode;
7818 struct nfs_server *server = NFS_SERVER(inode);
7819 struct pnfs_layout_hdr *lo;
7820 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007821 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007822
Boaz Harroshed7e5422014-01-22 20:34:54 +02007823 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007824
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007825 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007826 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007827
7828 switch (task->tk_status) {
7829 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007830 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007831 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07007832 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7833 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7834 */
7835 case -NFS4ERR_BADLAYOUT:
7836 goto out_overflow;
7837 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02007838 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07007839 * (or clients) writing to the same RAID stripe except when
7840 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Boaz Harroshed7e5422014-01-22 20:34:54 +02007841 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007842 case -NFS4ERR_LAYOUTTRYLATER:
Trond Myklebust21b874c2015-08-31 01:19:22 -07007843 if (lgp->args.minlength == 0)
7844 goto out_overflow;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007845 /*
7846 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7847 * existing layout before getting a new one).
7848 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007849 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007850 timeo = rpc_get_timeout(task->tk_client);
7851 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007852 now = jiffies;
7853 if (time_after(giveup, now)) {
7854 unsigned long delay;
7855
7856 /* Delay for:
7857 * - Not less then NFS4_POLL_RETRY_MIN.
7858 * - One last time a jiffie before we give up
7859 * - exponential backoff (time_now minus start_attempt)
7860 */
7861 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7862 min((giveup - now - 1),
7863 now - lgp->args.timestamp));
7864
7865 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7866 __func__, delay);
7867 rpc_delay(task, delay);
Trond Myklebust2259f962015-09-20 13:30:30 -04007868 /* Do not call nfs4_async_handle_error() */
7869 goto out_restart;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007870 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007871 break;
7872 case -NFS4ERR_EXPIRED:
7873 case -NFS4ERR_BAD_STATEID:
7874 spin_lock(&inode->i_lock);
Trond Myklebust2259f962015-09-20 13:30:30 -04007875 if (nfs4_stateid_match(&lgp->args.stateid,
7876 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007877 spin_unlock(&inode->i_lock);
7878 /* If the open stateid was bad, then recover it. */
7879 state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04007880 break;
7881 }
7882 lo = NFS_I(inode)->layout;
7883 if (lo && nfs4_stateid_match(&lgp->args.stateid,
7884 &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007885 LIST_HEAD(head);
7886
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007887 /*
7888 * Mark the bad layout state as invalid, then retry
7889 * with the current stateid.
7890 */
Kinglong Mee8714d462015-09-20 23:04:22 +08007891 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Trond Myklebustee314c22012-10-01 17:25:48 -07007892 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7893 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007894 pnfs_free_lseg_list(&head);
Trond Myklebust2259f962015-09-20 13:30:30 -04007895 } else
7896 spin_unlock(&inode->i_lock);
7897 goto out_restart;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007898 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007899 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebust2259f962015-09-20 13:30:30 -04007900 goto out_restart;
Trond Myklebustee314c22012-10-01 17:25:48 -07007901out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007902 dprintk("<-- %s\n", __func__);
Trond Myklebust21b874c2015-08-31 01:19:22 -07007903 return;
Trond Myklebust2259f962015-09-20 13:30:30 -04007904out_restart:
7905 task->tk_status = 0;
7906 rpc_restart_call_prepare(task);
7907 return;
Trond Myklebust21b874c2015-08-31 01:19:22 -07007908out_overflow:
7909 task->tk_status = -EOVERFLOW;
7910 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007911}
7912
Idan Kedar85541162012-08-02 11:47:10 +03007913static size_t max_response_pages(struct nfs_server *server)
7914{
7915 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7916 return nfs_page_array_len(0, max_resp_sz);
7917}
7918
7919static void nfs4_free_pages(struct page **pages, size_t size)
7920{
7921 int i;
7922
7923 if (!pages)
7924 return;
7925
7926 for (i = 0; i < size; i++) {
7927 if (!pages[i])
7928 break;
7929 __free_page(pages[i]);
7930 }
7931 kfree(pages);
7932}
7933
7934static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7935{
7936 struct page **pages;
7937 int i;
7938
7939 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7940 if (!pages) {
7941 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7942 return NULL;
7943 }
7944
7945 for (i = 0; i < size; i++) {
7946 pages[i] = alloc_page(gfp_flags);
7947 if (!pages[i]) {
7948 dprintk("%s: failed to allocate page\n", __func__);
7949 nfs4_free_pages(pages, size);
7950 return NULL;
7951 }
7952 }
7953
7954 return pages;
7955}
7956
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007957static void nfs4_layoutget_release(void *calldata)
7958{
7959 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007960 struct inode *inode = lgp->args.inode;
7961 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007962 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007963
7964 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007965 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007966 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007967 put_nfs_open_context(lgp->args.ctx);
7968 kfree(calldata);
7969 dprintk("<-- %s\n", __func__);
7970}
7971
7972static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7973 .rpc_call_prepare = nfs4_layoutget_prepare,
7974 .rpc_call_done = nfs4_layoutget_done,
7975 .rpc_release = nfs4_layoutget_release,
7976};
7977
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007978struct pnfs_layout_segment *
7979nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007980{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007981 struct inode *inode = lgp->args.inode;
7982 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007983 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007984 struct rpc_task *task;
7985 struct rpc_message msg = {
7986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7987 .rpc_argp = &lgp->args,
7988 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007989 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007990 };
7991 struct rpc_task_setup task_setup_data = {
7992 .rpc_client = server->client,
7993 .rpc_message = &msg,
7994 .callback_ops = &nfs4_layoutget_call_ops,
7995 .callback_data = lgp,
7996 .flags = RPC_TASK_ASYNC,
7997 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007998 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007999 int status = 0;
8000
8001 dprintk("--> %s\n", __func__);
8002
Peng Tao4bd5a982014-11-17 11:05:17 +08008003 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8004 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8005
Idan Kedar85541162012-08-02 11:47:10 +03008006 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8007 if (!lgp->args.layout.pages) {
8008 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008009 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008010 }
8011 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05008012 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03008013
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008014 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008015 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008016 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008017
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008018 task = rpc_run_task(&task_setup_data);
8019 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008020 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008021 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00008022 if (status == 0)
8023 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008024 trace_nfs4_layoutget(lgp->args.ctx,
8025 &lgp->args.range,
8026 &lgp->res.range,
8027 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008028 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8029 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008030 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008031 rpc_put_task(task);
8032 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008033 if (status)
8034 return ERR_PTR(status);
8035 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008036}
8037
Benny Halevycbe82602011-05-22 19:52:37 +03008038static void
8039nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8040{
8041 struct nfs4_layoutreturn *lrp = calldata;
8042
8043 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008044 nfs41_setup_sequence(lrp->clp->cl_session,
8045 &lrp->args.seq_args,
8046 &lrp->res.seq_res,
8047 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008048}
8049
8050static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8051{
8052 struct nfs4_layoutreturn *lrp = calldata;
8053 struct nfs_server *server;
8054
8055 dprintk("--> %s\n", __func__);
8056
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008057 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008058 return;
8059
8060 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008061 switch (task->tk_status) {
8062 default:
8063 task->tk_status = 0;
8064 case 0:
8065 break;
8066 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008067 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008068 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008069 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008070 return;
8071 }
Benny Halevycbe82602011-05-22 19:52:37 +03008072 dprintk("<-- %s\n", __func__);
8073}
8074
8075static void nfs4_layoutreturn_release(void *calldata)
8076{
8077 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008078 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008079 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008080
8081 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008082 spin_lock(&lo->plh_inode->i_lock);
8083 if (lrp->res.lrs_present)
8084 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008085 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
Tom Haynesd67ae822014-12-11 17:02:04 -05008086 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008087 lo->plh_block_lgets--;
8088 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008089 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008090 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008091 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008092 kfree(calldata);
8093 dprintk("<-- %s\n", __func__);
8094}
8095
8096static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8097 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8098 .rpc_call_done = nfs4_layoutreturn_done,
8099 .rpc_release = nfs4_layoutreturn_release,
8100};
8101
Peng Tao6c166052014-11-17 09:30:40 +08008102int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008103{
8104 struct rpc_task *task;
8105 struct rpc_message msg = {
8106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8107 .rpc_argp = &lrp->args,
8108 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008109 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008110 };
8111 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008112 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008113 .rpc_message = &msg,
8114 .callback_ops = &nfs4_layoutreturn_call_ops,
8115 .callback_data = lrp,
8116 };
Peng Tao6c166052014-11-17 09:30:40 +08008117 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008118
8119 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008120 if (!sync) {
8121 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8122 if (!lrp->inode) {
8123 nfs4_layoutreturn_release(lrp);
8124 return -EAGAIN;
8125 }
8126 task_setup_data.flags |= RPC_TASK_ASYNC;
8127 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008128 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008129 task = rpc_run_task(&task_setup_data);
8130 if (IS_ERR(task))
8131 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008132 if (sync)
8133 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008134 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008135 dprintk("<-- %s status=%d\n", __func__, status);
8136 rpc_put_task(task);
8137 return status;
8138}
8139
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008140static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008141_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8142 struct pnfs_device *pdev,
8143 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008144{
8145 struct nfs4_getdeviceinfo_args args = {
8146 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008147 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8148 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008149 };
8150 struct nfs4_getdeviceinfo_res res = {
8151 .pdev = pdev,
8152 };
8153 struct rpc_message msg = {
8154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8155 .rpc_argp = &args,
8156 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008157 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008158 };
8159 int status;
8160
8161 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008162 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008163 if (res.notification & ~args.notify_types)
8164 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008165 if (res.notification != args.notify_types)
8166 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008167
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008168 dprintk("<-- %s status=%d\n", __func__, status);
8169
8170 return status;
8171}
8172
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008173int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8174 struct pnfs_device *pdev,
8175 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008176{
8177 struct nfs4_exception exception = { };
8178 int err;
8179
8180 do {
8181 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008182 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008183 &exception);
8184 } while (exception.retry);
8185 return err;
8186}
8187EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8188
Andy Adamson863a3c62011-03-23 13:27:54 +00008189static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8190{
8191 struct nfs4_layoutcommit_data *data = calldata;
8192 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008193 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008194
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008195 nfs41_setup_sequence(session,
8196 &data->args.seq_args,
8197 &data->res.seq_res,
8198 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008199}
8200
8201static void
8202nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8203{
8204 struct nfs4_layoutcommit_data *data = calldata;
8205 struct nfs_server *server = NFS_SERVER(data->args.inode);
8206
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008207 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008208 return;
8209
8210 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008211 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8212 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8213 case -NFS4ERR_BADLAYOUT: /* no layout */
8214 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008215 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008216 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008217 break;
8218 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008219 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008220 rpc_restart_call_prepare(task);
8221 return;
8222 }
8223 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008224}
8225
8226static void nfs4_layoutcommit_release(void *calldata)
8227{
8228 struct nfs4_layoutcommit_data *data = calldata;
8229
Andy Adamsondb29c082011-07-30 20:52:38 -04008230 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008231 nfs_post_op_update_inode_force_wcc(data->args.inode,
8232 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008233 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008234 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008235 kfree(data);
8236}
8237
8238static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8239 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8240 .rpc_call_done = nfs4_layoutcommit_done,
8241 .rpc_release = nfs4_layoutcommit_release,
8242};
8243
8244int
Andy Adamsonef311532011-03-12 02:58:10 -05008245nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008246{
8247 struct rpc_message msg = {
8248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8249 .rpc_argp = &data->args,
8250 .rpc_resp = &data->res,
8251 .rpc_cred = data->cred,
8252 };
8253 struct rpc_task_setup task_setup_data = {
8254 .task = &data->task,
8255 .rpc_client = NFS_CLIENT(data->args.inode),
8256 .rpc_message = &msg,
8257 .callback_ops = &nfs4_layoutcommit_ops,
8258 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008259 };
8260 struct rpc_task *task;
8261 int status = 0;
8262
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008263 dprintk("NFS: initiating layoutcommit call. sync %d "
8264 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008265 data->args.lastbytewritten,
8266 data->args.inode->i_ino);
8267
Trond Myklebust472e2592015-02-05 16:50:30 -05008268 if (!sync) {
8269 data->inode = nfs_igrab_and_active(data->args.inode);
8270 if (data->inode == NULL) {
8271 nfs4_layoutcommit_release(data);
8272 return -EAGAIN;
8273 }
8274 task_setup_data.flags = RPC_TASK_ASYNC;
8275 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008276 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008277 task = rpc_run_task(&task_setup_data);
8278 if (IS_ERR(task))
8279 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008280 if (sync)
8281 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008282 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008283 dprintk("%s: status %d\n", __func__, status);
8284 rpc_put_task(task);
8285 return status;
8286}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008287
Andy Adamson97431202013-08-08 10:57:56 -04008288/**
8289 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8290 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8291 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008292static int
8293_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008294 struct nfs_fsinfo *info,
8295 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008296{
8297 struct nfs41_secinfo_no_name_args args = {
8298 .style = SECINFO_STYLE_CURRENT_FH,
8299 };
8300 struct nfs4_secinfo_res res = {
8301 .flavors = flavors,
8302 };
8303 struct rpc_message msg = {
8304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8305 .rpc_argp = &args,
8306 .rpc_resp = &res,
8307 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008308 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008309 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008310 int status;
8311
8312 if (use_integrity) {
8313 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008314 cred = nfs4_get_clid_cred(server->nfs_client);
8315 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008316 }
8317
8318 dprintk("--> %s\n", __func__);
8319 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8320 &res.seq_res, 0);
8321 dprintk("<-- %s status=%d\n", __func__, status);
8322
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008323 if (cred)
8324 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008325
8326 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008327}
8328
8329static int
8330nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8331 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8332{
8333 struct nfs4_exception exception = { };
8334 int err;
8335 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008336 /* first try using integrity protection */
8337 err = -NFS4ERR_WRONGSEC;
8338
8339 /* try to use integrity protection with machine cred */
8340 if (_nfs4_is_integrity_protected(server->nfs_client))
8341 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8342 flavors, true);
8343
8344 /*
8345 * if unable to use integrity protection, or SECINFO with
8346 * integrity protection returns NFS4ERR_WRONGSEC (which is
8347 * disallowed by spec, but exists in deployed servers) use
8348 * the current filesystem's rpc_client and the user cred.
8349 */
8350 if (err == -NFS4ERR_WRONGSEC)
8351 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8352 flavors, false);
8353
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008354 switch (err) {
8355 case 0:
8356 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008357 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008358 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008359 default:
8360 err = nfs4_handle_exception(server, err, &exception);
8361 }
8362 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008363out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008364 return err;
8365}
8366
8367static int
8368nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8369 struct nfs_fsinfo *info)
8370{
8371 int err;
8372 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008373 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008374 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008375 struct nfs4_secinfo4 *secinfo;
8376 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008377
8378 page = alloc_page(GFP_KERNEL);
8379 if (!page) {
8380 err = -ENOMEM;
8381 goto out;
8382 }
8383
8384 flavors = page_address(page);
8385 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8386
8387 /*
8388 * Fall back on "guess and check" method if
8389 * the server doesn't support SECINFO_NO_NAME
8390 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008391 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008392 err = nfs4_find_root_sec(server, fhandle, info);
8393 goto out_freepage;
8394 }
8395 if (err)
8396 goto out_freepage;
8397
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008398 for (i = 0; i < flavors->num_flavors; i++) {
8399 secinfo = &flavors->flavors[i];
8400
8401 switch (secinfo->flavor) {
8402 case RPC_AUTH_NULL:
8403 case RPC_AUTH_UNIX:
8404 case RPC_AUTH_GSS:
8405 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8406 &secinfo->flavor_info);
8407 break;
8408 default:
8409 flavor = RPC_AUTH_MAXFLAVOR;
8410 break;
8411 }
8412
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008413 if (!nfs_auth_info_match(&server->auth_info, flavor))
8414 flavor = RPC_AUTH_MAXFLAVOR;
8415
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008416 if (flavor != RPC_AUTH_MAXFLAVOR) {
8417 err = nfs4_lookup_root_sec(server, fhandle,
8418 info, flavor);
8419 if (!err)
8420 break;
8421 }
8422 }
8423
8424 if (flavor == RPC_AUTH_MAXFLAVOR)
8425 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008426
8427out_freepage:
8428 put_page(page);
8429 if (err == -EACCES)
8430 return -EPERM;
8431out:
8432 return err;
8433}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008434
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008435static int _nfs41_test_stateid(struct nfs_server *server,
8436 nfs4_stateid *stateid,
8437 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008438{
8439 int status;
8440 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008441 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008442 };
8443 struct nfs41_test_stateid_res res;
8444 struct rpc_message msg = {
8445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8446 .rpc_argp = &args,
8447 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008448 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008449 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008450 struct rpc_clnt *rpc_client = server->client;
8451
8452 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8453 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008454
Chuck Lever38527b12012-07-11 16:30:23 -04008455 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008456 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008457 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008458 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008459 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008460 if (status != NFS_OK) {
8461 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008462 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008463 }
8464 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008465 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008466}
8467
Chuck Lever38527b12012-07-11 16:30:23 -04008468/**
8469 * nfs41_test_stateid - perform a TEST_STATEID operation
8470 *
8471 * @server: server / transport on which to perform the operation
8472 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008473 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008474 *
8475 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8476 * Otherwise a negative NFS4ERR value is returned if the operation
8477 * failed or the state ID is not currently valid.
8478 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008479static int nfs41_test_stateid(struct nfs_server *server,
8480 nfs4_stateid *stateid,
8481 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008482{
8483 struct nfs4_exception exception = { };
8484 int err;
8485 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008486 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008487 if (err != -NFS4ERR_DELAY)
8488 break;
8489 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008490 } while (exception.retry);
8491 return err;
8492}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008493
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008494struct nfs_free_stateid_data {
8495 struct nfs_server *server;
8496 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008497 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008498};
8499
8500static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8501{
8502 struct nfs_free_stateid_data *data = calldata;
8503 nfs41_setup_sequence(nfs4_get_session(data->server),
8504 &data->args.seq_args,
8505 &data->res.seq_res,
8506 task);
8507}
8508
8509static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8510{
8511 struct nfs_free_stateid_data *data = calldata;
8512
8513 nfs41_sequence_done(task, &data->res.seq_res);
8514
8515 switch (task->tk_status) {
8516 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008517 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008518 rpc_restart_call_prepare(task);
8519 }
8520}
8521
8522static void nfs41_free_stateid_release(void *calldata)
8523{
8524 kfree(calldata);
8525}
8526
Trond Myklebust17f26b12013-08-21 15:48:42 -04008527static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008528 .rpc_call_prepare = nfs41_free_stateid_prepare,
8529 .rpc_call_done = nfs41_free_stateid_done,
8530 .rpc_release = nfs41_free_stateid_release,
8531};
8532
8533static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8534 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008535 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008536 bool privileged)
8537{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008538 struct rpc_message msg = {
8539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008540 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008541 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008542 struct rpc_task_setup task_setup = {
8543 .rpc_client = server->client,
8544 .rpc_message = &msg,
8545 .callback_ops = &nfs41_free_stateid_ops,
8546 .flags = RPC_TASK_ASYNC,
8547 };
8548 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008549
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008550 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8551 &task_setup.rpc_client, &msg);
8552
Chuck Lever38527b12012-07-11 16:30:23 -04008553 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008554 data = kmalloc(sizeof(*data), GFP_NOFS);
8555 if (!data)
8556 return ERR_PTR(-ENOMEM);
8557 data->server = server;
8558 nfs4_stateid_copy(&data->args.stateid, stateid);
8559
8560 task_setup.callback_data = data;
8561
8562 msg.rpc_argp = &data->args;
8563 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008564 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008565 if (privileged)
8566 nfs4_set_sequence_privileged(&data->args.seq_args);
8567
8568 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008569}
8570
Chuck Lever38527b12012-07-11 16:30:23 -04008571/**
8572 * nfs41_free_stateid - perform a FREE_STATEID operation
8573 *
8574 * @server: server / transport on which to perform the operation
8575 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008576 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008577 *
8578 * Returns NFS_OK if the server freed "stateid". Otherwise a
8579 * negative NFS4ERR value is returned.
8580 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008581static int nfs41_free_stateid(struct nfs_server *server,
8582 nfs4_stateid *stateid,
8583 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008584{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008585 struct rpc_task *task;
8586 int ret;
8587
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008588 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008589 if (IS_ERR(task))
8590 return PTR_ERR(task);
8591 ret = rpc_wait_for_completion_task(task);
8592 if (!ret)
8593 ret = task->tk_status;
8594 rpc_put_task(task);
8595 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008596}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008597
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008598static void
8599nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008600{
8601 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008602 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008603
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008604 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008605 nfs4_free_lock_state(server, lsp);
8606 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008607 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008608 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008609}
8610
Trond Myklebust36281ca2012-03-04 18:13:56 -05008611static bool nfs41_match_stateid(const nfs4_stateid *s1,
8612 const nfs4_stateid *s2)
8613{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008614 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008615 return false;
8616
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008617 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008618 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008619 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008620 return true;
8621
8622 return false;
8623}
8624
Andy Adamson557134a2009-04-01 09:21:53 -04008625#endif /* CONFIG_NFS_V4_1 */
8626
Trond Myklebust36281ca2012-03-04 18:13:56 -05008627static bool nfs4_match_stateid(const nfs4_stateid *s1,
8628 const nfs4_stateid *s2)
8629{
Trond Myklebustf597c532012-03-04 18:13:56 -05008630 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008631}
8632
8633
Trond Myklebust17280172012-03-11 13:11:00 -04008634static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008635 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008636 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637 .recover_open = nfs4_open_reclaim,
8638 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008639 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008640 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641};
8642
Andy Adamson591d71c2009-04-01 09:22:47 -04008643#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008644static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008645 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8646 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8647 .recover_open = nfs4_open_reclaim,
8648 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008649 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008650 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008651 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008652};
8653#endif /* CONFIG_NFS_V4_1 */
8654
Trond Myklebust17280172012-03-11 13:11:00 -04008655static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008656 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008657 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008658 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008660 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008661};
8662
Andy Adamson591d71c2009-04-01 09:22:47 -04008663#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008664static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008665 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8666 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008667 .recover_open = nfs41_open_expired,
8668 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008669 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008670};
8671#endif /* CONFIG_NFS_V4_1 */
8672
Trond Myklebust17280172012-03-11 13:11:00 -04008673static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008674 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008675 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008676 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008677};
8678
8679#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008680static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008681 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008682 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008683 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008684};
8685#endif
8686
Chuck Leverec011fe2013-10-17 14:12:39 -04008687static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008688 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008689 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008690};
8691
8692#if defined(CONFIG_NFS_V4_1)
8693static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008694 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008695 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008696};
8697#endif /* CONFIG_NFS_V4_1 */
8698
Trond Myklebust97dc1352010-06-16 09:52:26 -04008699static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8700 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008701 .init_caps = NFS_CAP_READDIRPLUS
8702 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008703 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008704 .init_client = nfs40_init_client,
8705 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008706 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008707 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008708 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008709 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008710 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008711 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8712 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8713 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008714 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008715};
8716
8717#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008718static struct nfs_seqid *
8719nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8720{
8721 return NULL;
8722}
8723
Trond Myklebust97dc1352010-06-16 09:52:26 -04008724static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8725 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008726 .init_caps = NFS_CAP_READDIRPLUS
8727 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008728 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008729 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008730 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008731 .init_client = nfs41_init_client,
8732 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008733 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008734 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008735 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008736 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008737 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008738 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8739 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8740 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008741 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008742};
8743#endif
8744
Steve Dickson42c2c422013-05-22 12:50:38 -04008745#if defined(CONFIG_NFS_V4_2)
8746static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8747 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008748 .init_caps = NFS_CAP_READDIRPLUS
8749 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008750 | NFS_CAP_POSIX_LOCK
8751 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008752 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008753 | NFS_CAP_ALLOCATE
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008754 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008755 | NFS_CAP_SEEK
8756 | NFS_CAP_LAYOUTSTATS,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008757 .init_client = nfs41_init_client,
8758 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008759 .match_stateid = nfs41_match_stateid,
8760 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008761 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008762 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008763 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008764 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8765 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8766 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08008767 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008768};
8769#endif
8770
Trond Myklebust97dc1352010-06-16 09:52:26 -04008771const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8772 [0] = &nfs_v4_0_minor_ops,
8773#if defined(CONFIG_NFS_V4_1)
8774 [1] = &nfs_v4_1_minor_ops,
8775#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008776#if defined(CONFIG_NFS_V4_2)
8777 [2] = &nfs_v4_2_minor_ops,
8778#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008779};
8780
Trond Myklebust17f26b12013-08-21 15:48:42 -04008781static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008782 .create = nfs_create,
8783 .lookup = nfs_lookup,
8784 .atomic_open = nfs_atomic_open,
8785 .link = nfs_link,
8786 .unlink = nfs_unlink,
8787 .symlink = nfs_symlink,
8788 .mkdir = nfs_mkdir,
8789 .rmdir = nfs_rmdir,
8790 .mknod = nfs_mknod,
8791 .rename = nfs_rename,
8792 .permission = nfs_permission,
8793 .getattr = nfs_getattr,
8794 .setattr = nfs_setattr,
8795 .getxattr = generic_getxattr,
8796 .setxattr = generic_setxattr,
8797 .listxattr = generic_listxattr,
8798 .removexattr = generic_removexattr,
8799};
8800
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008801static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008802 .permission = nfs_permission,
8803 .getattr = nfs_getattr,
8804 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008805 .getxattr = generic_getxattr,
8806 .setxattr = generic_setxattr,
8807 .listxattr = generic_listxattr,
8808 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008809};
8810
David Howells509de812006-08-22 20:06:11 -04008811const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008812 .version = 4, /* protocol version */
8813 .dentry_ops = &nfs4_dentry_operations,
8814 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008815 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008816 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008817 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008818 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008819 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008820 .getattr = nfs4_proc_getattr,
8821 .setattr = nfs4_proc_setattr,
8822 .lookup = nfs4_proc_lookup,
8823 .access = nfs4_proc_access,
8824 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008825 .create = nfs4_proc_create,
8826 .remove = nfs4_proc_remove,
8827 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008828 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008829 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008830 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008831 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008832 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833 .link = nfs4_proc_link,
8834 .symlink = nfs4_proc_symlink,
8835 .mkdir = nfs4_proc_mkdir,
8836 .rmdir = nfs4_proc_remove,
8837 .readdir = nfs4_proc_readdir,
8838 .mknod = nfs4_proc_mknod,
8839 .statfs = nfs4_proc_statfs,
8840 .fsinfo = nfs4_proc_fsinfo,
8841 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008842 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008844 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008846 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008848 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008849 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008850 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008851 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008853 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008854 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008855 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008856 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008857 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008858 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008859 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008860 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008861 .create_server = nfs4_create_server,
8862 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863};
8864
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008865static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8866 .prefix = XATTR_NAME_NFSV4_ACL,
8867 .list = nfs4_xattr_list_nfs4_acl,
8868 .get = nfs4_xattr_get_nfs4_acl,
8869 .set = nfs4_xattr_set_nfs4_acl,
8870};
8871
8872const struct xattr_handler *nfs4_xattr_handlers[] = {
8873 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008874#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8875 &nfs4_xattr_nfs4_label_handler,
8876#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008877 NULL
8878};
8879
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880/*
8881 * Local variables:
8882 * c-basic-offset: 8
8883 * End:
8884 */