blob: 9264994ec9d3487c00e001faf917fd0f418ae977 [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 *);
NeilBrown8478eaa2014-09-18 16:09:27 +100081static int nfs4_async_handle_error(struct rpc_task *, const 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 */
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500347int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500348{
349 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500350 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500352 int ret = errorcode;
353
354 exception->retry = 0;
355 switch(errorcode) {
356 case 0:
357 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500358 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400359 case -NFS4ERR_DELEG_REVOKED:
360 case -NFS4ERR_ADMIN_REVOKED:
361 case -NFS4ERR_BAD_STATEID:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400362 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400363 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500364 exception->retry = 1;
365 return 0;
366 }
367 if (state == NULL)
368 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400369 ret = nfs4_schedule_stateid_recovery(server, state);
370 if (ret < 0)
371 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500372 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400373 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400374 if (state != NULL) {
375 ret = nfs4_schedule_stateid_recovery(server, state);
376 if (ret < 0)
377 break;
378 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500379 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500380 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500381 nfs4_schedule_lease_recovery(clp);
382 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400383 case -NFS4ERR_MOVED:
384 ret = nfs4_schedule_migration_recovery(server);
385 if (ret < 0)
386 break;
387 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400388 case -NFS4ERR_LEASE_MOVED:
389 nfs4_schedule_lease_moved_recovery(clp);
390 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500391#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400392 case -NFS4ERR_BADSESSION:
393 case -NFS4ERR_BADSLOT:
394 case -NFS4ERR_BAD_HIGH_SLOT:
395 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
396 case -NFS4ERR_DEADSESSION:
397 case -NFS4ERR_SEQ_FALSE_RETRY:
398 case -NFS4ERR_SEQ_MISORDERED:
399 dprintk("%s ERROR: %d Reset session\n", __func__,
400 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400401 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400402 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500403#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500405 if (exception->timeout > HZ) {
406 /* We have retried a decent amount, time to
407 * fail
408 */
409 ret = -EBUSY;
410 break;
411 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500412 case -NFS4ERR_GRACE:
413 case -NFS4ERR_DELAY:
414 ret = nfs4_delay(server->client, &exception->timeout);
415 if (ret != 0)
416 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400417 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500418 case -NFS4ERR_OLD_STATEID:
419 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800420 break;
421 case -NFS4ERR_BADOWNER:
422 /* The following works around a Linux server bug! */
423 case -NFS4ERR_BADNAME:
424 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
425 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
426 exception->retry = 1;
427 printk(KERN_WARNING "NFS: v4 server %s "
428 "does not accept raw "
429 "uid/gids. "
430 "Reenabling the idmapper.\n",
431 server->nfs_client->cl_hostname);
432 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500433 }
434 /* We failed to handle the error */
435 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500436wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500437 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400438 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
439 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500440 if (ret == 0)
441 exception->retry = 1;
442 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500443}
444
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400445/*
446 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
447 * or 'false' otherwise.
448 */
449static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
450{
451 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
452
453 if (flavor == RPC_AUTH_GSS_KRB5I ||
454 flavor == RPC_AUTH_GSS_KRB5P)
455 return true;
456
457 return false;
458}
Trond Myklebust65de8722008-12-23 15:21:44 -0500459
Trond Myklebust452e9352010-07-31 14:29:06 -0400460static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 spin_lock(&clp->cl_lock);
463 if (time_before(clp->cl_last_renewal,timestamp))
464 clp->cl_last_renewal = timestamp;
465 spin_unlock(&clp->cl_lock);
466}
467
Trond Myklebust452e9352010-07-31 14:29:06 -0400468static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
469{
Trond Myklebustbe824162015-07-05 14:50:46 -0400470 struct nfs_client *clp = server->nfs_client;
471
472 if (!nfs4_has_session(clp))
473 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400474}
475
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400476struct nfs4_call_sync_data {
477 const struct nfs_server *seq_server;
478 struct nfs4_sequence_args *seq_args;
479 struct nfs4_sequence_res *seq_res;
480};
481
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800482void nfs4_init_sequence(struct nfs4_sequence_args *args,
483 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400484{
485 args->sa_slot = NULL;
486 args->sa_cache_this = cache_reply;
487 args->sa_privileged = 0;
488
489 res->sr_slot = NULL;
490}
491
492static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
493{
494 args->sa_privileged = 1;
495}
496
Peng Taocb04ad22014-06-11 05:24:15 +0800497int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
498 struct nfs4_sequence_args *args,
499 struct nfs4_sequence_res *res,
500 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400501{
Chuck Lever3bd23842013-08-09 12:49:19 -0400502 struct nfs4_slot *slot;
503
504 /* slot already allocated? */
505 if (res->sr_slot != NULL)
506 goto out_start;
507
508 spin_lock(&tbl->slot_tbl_lock);
509 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
510 goto out_sleep;
511
512 slot = nfs4_alloc_slot(tbl);
513 if (IS_ERR(slot)) {
514 if (slot == ERR_PTR(-ENOMEM))
515 task->tk_timeout = HZ >> 2;
516 goto out_sleep;
517 }
518 spin_unlock(&tbl->slot_tbl_lock);
519
520 args->sa_slot = slot;
521 res->sr_slot = slot;
522
523out_start:
524 rpc_call_start(task);
525 return 0;
526
527out_sleep:
528 if (args->sa_privileged)
529 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
530 NULL, RPC_PRIORITY_PRIVILEGED);
531 else
532 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
533 spin_unlock(&tbl->slot_tbl_lock);
534 return -EAGAIN;
535}
Peng Taocb04ad22014-06-11 05:24:15 +0800536EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400537
538static int nfs40_sequence_done(struct rpc_task *task,
539 struct nfs4_sequence_res *res)
540{
541 struct nfs4_slot *slot = res->sr_slot;
542 struct nfs4_slot_table *tbl;
543
Trond Myklebustcab92c12014-01-29 12:12:15 -0500544 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400545 goto out;
546
547 tbl = slot->table;
548 spin_lock(&tbl->slot_tbl_lock);
549 if (!nfs41_wake_and_assign_slot(tbl, slot))
550 nfs4_free_slot(tbl, slot);
551 spin_unlock(&tbl->slot_tbl_lock);
552
553 res->sr_slot = NULL;
554out:
555 return 1;
556}
557
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400558#if defined(CONFIG_NFS_V4_1)
559
Trond Myklebustd185a332010-06-16 09:52:25 -0400560static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400561{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500562 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400563 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500564 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500565 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400566
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500567 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500568 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500569
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500570 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500571 /* Be nice to the server: try to ensure that the last transmitted
572 * value for highest_user_slotid <= target_highest_slotid
573 */
574 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
575 send_new_highest_used_slotid = true;
576
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500577 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500578 send_new_highest_used_slotid = false;
579 goto out_unlock;
580 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500581 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500582
583 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
584 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500585out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500586 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400587 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500588 if (send_new_highest_used_slotid)
589 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400590}
591
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500592int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400593{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500594 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500595 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400596 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500597 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500598 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400599
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500600 if (slot == NULL)
601 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400602 /* don't increment the sequence number if the task wasn't sent */
603 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400604 goto out;
605
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500606 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500607
Trond Myklebustac20d162012-12-15 15:36:07 -0500608 if (slot->interrupted) {
609 slot->interrupted = 0;
610 interrupted = true;
611 }
612
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400613 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500614 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400615 switch (res->sr_status) {
616 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400617 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500618 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500619 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500620 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500621 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400622 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500623 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400624 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500625 case 1:
626 /*
627 * sr_status remains 1 if an RPC level error occurred.
628 * The server may or may not have processed the sequence
629 * operation..
630 * Mark the slot as having hosted an interrupted RPC call.
631 */
632 slot->interrupted = 1;
633 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400634 case -NFS4ERR_DELAY:
635 /* The server detected a resend of the RPC call and
636 * returned NFS4ERR_DELAY as per Section 2.10.6.2
637 * of RFC5661.
638 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500639 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400640 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500641 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500642 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400643 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500644 case -NFS4ERR_BADSLOT:
645 /*
646 * The slot id we used was probably retired. Try again
647 * using a different slot id.
648 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500649 goto retry_nowait;
650 case -NFS4ERR_SEQ_MISORDERED:
651 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500652 * Was the last operation on this sequence interrupted?
653 * If so, retry after bumping the sequence number.
654 */
655 if (interrupted) {
656 ++slot->seq_nr;
657 goto retry_nowait;
658 }
659 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500660 * Could this slot have been previously retired?
661 * If so, then the server may be expecting seq_nr = 1!
662 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500663 if (slot->seq_nr != 1) {
664 slot->seq_nr = 1;
665 goto retry_nowait;
666 }
667 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500668 case -NFS4ERR_SEQ_FALSE_RETRY:
669 ++slot->seq_nr;
670 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400671 default:
672 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500673 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400674 }
675out:
676 /* The session may be reset by one of the error handlers. */
677 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400678 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500679out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500680 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500681retry_nowait:
682 if (rpc_restart_call_prepare(task)) {
683 task->tk_status = 0;
684 ret = 0;
685 }
686 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400687out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400688 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400689 goto out;
690 rpc_delay(task, NFS4_POLL_RETRY_MAX);
691 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400692}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500693EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400694
Peng Tao2c4b1312014-06-11 05:24:16 +0800695int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400696{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400698 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400699 if (!res->sr_slot->table->session)
700 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400701 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400702}
Peng Tao2c4b1312014-06-11 05:24:16 +0800703EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400704
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000705int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 struct nfs4_sequence_args *args,
707 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 struct rpc_task *task)
709{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400710 struct nfs4_slot *slot;
711 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400712
713 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400714 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400715 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400716 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400718 tbl = &session->fc_slot_table;
719
Trond Myklebust69d206b2012-11-22 13:21:02 -0500720 task->tk_timeout = 0;
721
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400722 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400723 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400724 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500725 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500726 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400727 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400728 }
729
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500730 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500731 if (IS_ERR(slot)) {
732 /* If out of memory, try again in 1/4 second */
733 if (slot == ERR_PTR(-ENOMEM))
734 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400735 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400736 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400737 }
738 spin_unlock(&tbl->slot_tbl_lock);
739
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500740 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400741
Chuck Levere8d92382013-08-09 12:47:51 -0400742 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500743 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400744
Benny Halevydfb4f3092010-09-24 09:17:01 -0400745 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500746 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400747 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400748 /*
749 * sr_status is only set in decode_sequence, and so will remain
750 * set to 1 if an rpc level failure occurs.
751 */
752 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400753 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400754out_success:
755 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400756 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400757out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400758 /* Privileged tasks are queued with top priority */
759 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400760 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
761 NULL, RPC_PRIORITY_PRIVILEGED);
762 else
763 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400764 spin_unlock(&tbl->slot_tbl_lock);
765 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400766}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000767EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400768
Chuck Lever5a580e02013-08-09 12:48:09 -0400769static int nfs4_setup_sequence(const struct nfs_server *server,
770 struct nfs4_sequence_args *args,
771 struct nfs4_sequence_res *res,
772 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400773{
Trond Myklebust035168a2010-06-16 09:52:26 -0400774 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400775 int ret = 0;
776
Chuck Lever3bd23842013-08-09 12:49:19 -0400777 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800778 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
779 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400780
Chuck Levere8d92382013-08-09 12:47:51 -0400781 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400782 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400783 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400784
Trond Myklebust9d12b212012-01-17 22:04:25 -0500785 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400786
Andy Adamsonce5039c2009-04-01 09:22:13 -0400787 dprintk("<-- %s status=%d\n", __func__, ret);
788 return ret;
789}
790
Andy Adamsonce5039c2009-04-01 09:22:13 -0400791static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
792{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400793 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400794 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400795
Trond Myklebust035168a2010-06-16 09:52:26 -0400796 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
797
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400798 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400799}
800
Andy Adamson69ab40c2009-04-01 09:22:19 -0400801static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
802{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400803 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804
Trond Myklebust14516c32010-07-31 14:29:06 -0400805 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400806}
807
Trond Myklebust17280172012-03-11 13:11:00 -0400808static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400810 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400811};
812
Chuck Lever3bd23842013-08-09 12:49:19 -0400813#else /* !CONFIG_NFS_V4_1 */
814
Chuck Lever5a580e02013-08-09 12:48:09 -0400815static int nfs4_setup_sequence(const struct nfs_server *server,
816 struct nfs4_sequence_args *args,
817 struct nfs4_sequence_res *res,
818 struct rpc_task *task)
819{
Peng Taocb04ad22014-06-11 05:24:15 +0800820 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
821 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400822}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400823
Peng Tao2c4b1312014-06-11 05:24:16 +0800824int nfs4_sequence_done(struct rpc_task *task,
825 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400826{
Chuck Lever3bd23842013-08-09 12:49:19 -0400827 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400828}
Peng Tao2c4b1312014-06-11 05:24:16 +0800829EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400830
831#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400832
Chuck Lever9915ea72013-08-09 12:48:27 -0400833static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
834{
835 struct nfs4_call_sync_data *data = calldata;
836 nfs4_setup_sequence(data->seq_server,
837 data->seq_args, data->seq_res, task);
838}
839
840static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
841{
842 struct nfs4_call_sync_data *data = calldata;
843 nfs4_sequence_done(task, data->seq_res);
844}
845
846static const struct rpc_call_ops nfs40_call_sync_ops = {
847 .rpc_call_prepare = nfs40_call_sync_prepare,
848 .rpc_call_done = nfs40_call_sync_done,
849};
850
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400851static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400852 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500853 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 struct nfs4_sequence_args *args,
855 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400856{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857 int ret;
858 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400859 struct nfs_client *clp = server->nfs_client;
860 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100861 .seq_server = server,
862 .seq_args = args,
863 .seq_res = res,
864 };
865 struct rpc_task_setup task_setup = {
866 .rpc_client = clnt,
867 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400868 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100869 .callback_data = &data
870 };
871
872 task = rpc_run_task(&task_setup);
873 if (IS_ERR(task))
874 ret = PTR_ERR(task);
875 else {
876 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400877 rpc_put_task(task);
878 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 return ret;
880}
881
Bryan Schumaker7c513052011-03-24 17:12:24 +0000882int nfs4_call_sync(struct rpc_clnt *clnt,
883 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000884 struct rpc_message *msg,
885 struct nfs4_sequence_args *args,
886 struct nfs4_sequence_res *res,
887 int cache_reply)
888{
Chuck Levera9c92d62013-08-09 12:48:18 -0400889 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400890 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000891}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
894{
895 struct nfs_inode *nfsi = NFS_I(dir);
896
897 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400898 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700899 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700901 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500902 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000903 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 spin_unlock(&dir->i_lock);
905}
906
907struct nfs4_opendata {
908 struct kref kref;
909 struct nfs_openargs o_arg;
910 struct nfs_openres o_res;
911 struct nfs_open_confirmargs c_arg;
912 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500913 struct nfs4_string owner_name;
914 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400916 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400918 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct nfs4_state_owner *owner;
920 struct nfs4_state *state;
921 struct iattr attrs;
922 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400923 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400924 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400925 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400926 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int cancelled;
928};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400929
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400930static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
931 int err, struct nfs4_exception *exception)
932{
933 if (err != -EINVAL)
934 return false;
935 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
936 return false;
937 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
938 exception->retry = 1;
939 return true;
940}
941
Trond Myklebust6ae37332015-01-30 14:21:14 -0500942static u32
943nfs4_map_atomic_open_share(struct nfs_server *server,
944 fmode_t fmode, int openflags)
945{
946 u32 res = 0;
947
948 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
949 case FMODE_READ:
950 res = NFS4_SHARE_ACCESS_READ;
951 break;
952 case FMODE_WRITE:
953 res = NFS4_SHARE_ACCESS_WRITE;
954 break;
955 case FMODE_READ|FMODE_WRITE:
956 res = NFS4_SHARE_ACCESS_BOTH;
957 }
958 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
959 goto out;
960 /* Want no delegation if we're using O_DIRECT */
961 if (openflags & O_DIRECT)
962 res |= NFS4_SHARE_WANT_NO_DELEG;
963out:
964 return res;
965}
966
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400967static enum open_claim_type4
968nfs4_map_atomic_open_claim(struct nfs_server *server,
969 enum open_claim_type4 claim)
970{
971 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
972 return claim;
973 switch (claim) {
974 default:
975 return claim;
976 case NFS4_OPEN_CLAIM_FH:
977 return NFS4_OPEN_CLAIM_NULL;
978 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
979 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
980 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
981 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
982 }
983}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986{
987 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400988 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100989 p->o_res.seqid = p->o_arg.seqid;
990 p->c_res.seqid = p->c_arg.seqid;
991 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400992 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100993 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500994 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100995}
996
Al Viro82a2c1b2011-06-22 18:30:55 -0400997static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100998 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400999 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001000 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001001 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001002 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003{
Al Viro82a2c1b2011-06-22 18:30:55 -04001004 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001005 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001007 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001008 struct nfs4_opendata *p;
1009
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001010 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001011 if (p == NULL)
1012 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001013
1014 p->f_label = nfs4_label_alloc(server, gfp_mask);
1015 if (IS_ERR(p->f_label))
1016 goto err_free_p;
1017
Trond Myklebust63f5f792015-01-23 19:19:25 -05001018 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1019 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001020 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001021 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001022 nfs_sb_active(dentry->d_sb);
1023 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001024 p->dir = parent;
1025 p->owner = sp;
1026 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 p->o_arg.open_flags = flags;
1028 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001029 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1030 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001031 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1032 * will return permission denied for all bits until close */
1033 if (!(flags & O_EXCL)) {
1034 /* ask server to check for all possible rights as results
1035 * are cached */
1036 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1037 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001038 }
David Howells7539bba2006-08-22 20:06:09 -04001039 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001040 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1041 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001042 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001044 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001045 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001046 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001047 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1048 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001049 case NFS4_OPEN_CLAIM_NULL:
1050 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1051 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1052 p->o_arg.fh = NFS_FH(dir);
1053 break;
1054 case NFS4_OPEN_CLAIM_PREVIOUS:
1055 case NFS4_OPEN_CLAIM_FH:
1056 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1057 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001058 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001059 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001060 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001061 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001062
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001063 p->o_arg.u.attrs = &p->attrs;
1064 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001065
1066 verf[0] = jiffies;
1067 verf[1] = current->pid;
1068 memcpy(p->o_arg.u.verifier.data, verf,
1069 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001070 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001071 p->c_arg.fh = &p->o_res.fh;
1072 p->c_arg.stateid = &p->o_res.stateid;
1073 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001074 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001075 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001076 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001077
1078err_free_label:
1079 nfs4_label_free(p->f_label);
1080err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001081 kfree(p);
1082err:
1083 dput(parent);
1084 return NULL;
1085}
1086
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001087static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001088{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001089 struct nfs4_opendata *p = container_of(kref,
1090 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001091 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001092
1093 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001094 if (p->state != NULL)
1095 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001096 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001097
1098 nfs4_label_free(p->f_label);
1099
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001100 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001101 dput(p->dentry);
1102 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001103 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001104 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001105 kfree(p);
1106}
1107
1108static void nfs4_opendata_put(struct nfs4_opendata *p)
1109{
1110 if (p != NULL)
1111 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112}
1113
Trond Myklebust06f814a2006-01-03 09:55:07 +01001114static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1115{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001116 int ret;
1117
Trond Myklebust06f814a2006-01-03 09:55:07 +01001118 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001119 return ret;
1120}
1121
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001122static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001123{
1124 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125
Trond Myklebust536e43d2012-01-17 22:04:26 -05001126 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127 goto out;
1128 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001129 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001130 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1131 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001132 break;
1133 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001134 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1135 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001136 break;
1137 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001138 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1139 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001140 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001142 return ret;
1143}
1144
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001145static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001146{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001147 if (delegation == NULL)
1148 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001149 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001150 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001151 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1152 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001153 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001154 return 1;
1155}
1156
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001158{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001159 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001160 case FMODE_WRITE:
1161 state->n_wronly++;
1162 break;
1163 case FMODE_READ:
1164 state->n_rdonly++;
1165 break;
1166 case FMODE_READ|FMODE_WRITE:
1167 state->n_rdwr++;
1168 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001169 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001170}
1171
Trond Myklebust4f14c192014-02-12 19:15:06 -05001172static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001173{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001174 struct nfs_client *clp = state->owner->so_server->nfs_client;
1175 bool need_recover = false;
1176
1177 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1178 need_recover = true;
1179 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1180 need_recover = true;
1181 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1182 need_recover = true;
1183 if (need_recover)
1184 nfs4_state_mark_reclaim_nograce(clp, state);
1185}
1186
Trond Myklebuste999e802014-02-10 18:20:47 -05001187static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1188 nfs4_stateid *stateid)
1189{
1190 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1191 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001192 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1193 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001194 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001195 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001196 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1197 return true;
1198 return false;
1199}
1200
Trond Myklebustf95549c2015-01-23 18:06:09 -05001201static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1202{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001203 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1204 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001205 if (state->n_wronly)
1206 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1207 if (state->n_rdonly)
1208 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1209 if (state->n_rdwr)
1210 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001211 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001212}
1213
Trond Myklebust226056c2014-02-11 10:41:07 -05001214static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1215 nfs4_stateid *stateid, fmode_t fmode)
1216{
1217 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1218 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1219 case FMODE_WRITE:
1220 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1221 break;
1222 case FMODE_READ:
1223 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1224 break;
1225 case 0:
1226 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1227 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1228 clear_bit(NFS_OPEN_STATE, &state->flags);
1229 }
1230 if (stateid == NULL)
1231 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001232 /* Handle races with OPEN */
1233 if (!nfs4_stateid_match_other(stateid, &state->open_stateid) ||
1234 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1235 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001236 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001237 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001238 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001239 nfs4_stateid_copy(&state->stateid, stateid);
1240 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001241}
1242
1243static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1244{
1245 write_seqlock(&state->seqlock);
1246 nfs_clear_open_stateid_locked(state, stateid, fmode);
1247 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001248 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1249 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001250}
1251
Trond Myklebust003707c2007-07-05 18:07:55 -04001252static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1253{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001254 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001255 case FMODE_READ:
1256 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1257 break;
1258 case FMODE_WRITE:
1259 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1260 break;
1261 case FMODE_READ|FMODE_WRITE:
1262 set_bit(NFS_O_RDWR_STATE, &state->flags);
1263 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001264 if (!nfs_need_update_open_stateid(state, stateid))
1265 return;
1266 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1267 nfs4_stateid_copy(&state->stateid, stateid);
1268 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001269}
1270
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001271static 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 -07001272{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001273 /*
1274 * Protect the call to nfs4_state_set_mode_locked and
1275 * serialise the stateid update
1276 */
1277 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001278 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001279 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001280 set_bit(NFS_DELEGATED_STATE, &state->flags);
1281 }
1282 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001283 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001284 write_sequnlock(&state->seqlock);
1285 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001286 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001287 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001290static 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 -05001291{
1292 struct nfs_inode *nfsi = NFS_I(state->inode);
1293 struct nfs_delegation *deleg_cur;
1294 int ret = 0;
1295
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001296 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001297
1298 rcu_read_lock();
1299 deleg_cur = rcu_dereference(nfsi->delegation);
1300 if (deleg_cur == NULL)
1301 goto no_delegation;
1302
1303 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001304 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001305 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001306 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001307 goto no_delegation_unlock;
1308
1309 if (delegation == NULL)
1310 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001311 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001312 goto no_delegation_unlock;
1313
Trond Myklebustb7391f42008-12-23 15:21:52 -05001314 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001315 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001316 ret = 1;
1317no_delegation_unlock:
1318 spin_unlock(&deleg_cur->lock);
1319no_delegation:
1320 rcu_read_unlock();
1321
1322 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001323 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001324 ret = 1;
1325 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001326 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1327 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001328
1329 return ret;
1330}
1331
Trond Myklebust39071e62015-01-24 15:07:56 -05001332static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1333 const nfs4_stateid *stateid)
1334{
1335 struct nfs4_state *state = lsp->ls_state;
1336 bool ret = false;
1337
1338 spin_lock(&state->state_lock);
1339 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1340 goto out_noupdate;
1341 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1342 goto out_noupdate;
1343 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1344 ret = true;
1345out_noupdate:
1346 spin_unlock(&state->state_lock);
1347 return ret;
1348}
Trond Myklebust34310432008-12-23 15:21:38 -05001349
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001350static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001351{
1352 struct nfs_delegation *delegation;
1353
1354 rcu_read_lock();
1355 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001356 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001357 rcu_read_unlock();
1358 return;
1359 }
1360 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001361 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001362}
1363
Trond Myklebust6ee41262007-07-08 14:11:36 -04001364static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001365{
1366 struct nfs4_state *state = opendata->state;
1367 struct nfs_inode *nfsi = NFS_I(state->inode);
1368 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001369 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001370 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001371 nfs4_stateid stateid;
1372 int ret = -EAGAIN;
1373
Trond Myklebustaac00a82007-07-05 19:02:21 -04001374 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001375 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001376 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001377 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001378 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001379 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001380 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001381 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001382 rcu_read_lock();
1383 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001384 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001385 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001386 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001387 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001388 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001389 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001390 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001391 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001392 if (!opendata->is_recover) {
1393 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1394 if (ret != 0)
1395 goto out;
1396 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001397 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001398
1399 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001400 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001401 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001402 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001403out:
1404 return ERR_PTR(ret);
1405out_return_state:
1406 atomic_inc(&state->count);
1407 return state;
1408}
1409
Andy Adamsone23008e2012-10-02 21:07:32 -04001410static void
1411nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1412{
1413 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1414 struct nfs_delegation *delegation;
1415 int delegation_flags = 0;
1416
1417 rcu_read_lock();
1418 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1419 if (delegation)
1420 delegation_flags = delegation->flags;
1421 rcu_read_unlock();
1422 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1423 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1424 "returning a delegation for "
1425 "OPEN(CLAIM_DELEGATE_CUR)\n",
1426 clp->cl_hostname);
1427 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1428 nfs_inode_set_delegation(state->inode,
1429 data->owner->so_cred,
1430 &data->o_res);
1431 else
1432 nfs_inode_reclaim_delegation(state->inode,
1433 data->owner->so_cred,
1434 &data->o_res);
1435}
1436
1437/*
1438 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1439 * and update the nfs4_state.
1440 */
1441static struct nfs4_state *
1442_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1443{
1444 struct inode *inode = data->state->inode;
1445 struct nfs4_state *state = data->state;
1446 int ret;
1447
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001448 if (!data->rpc_done) {
1449 if (data->rpc_status) {
1450 ret = data->rpc_status;
1451 goto err;
1452 }
1453 /* cached opens have already been processed */
1454 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001455 }
1456
Andy Adamsone23008e2012-10-02 21:07:32 -04001457 ret = nfs_refresh_inode(inode, &data->f_attr);
1458 if (ret)
1459 goto err;
1460
1461 if (data->o_res.delegation_type != 0)
1462 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001463update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001464 update_open_stateid(state, &data->o_res.stateid, NULL,
1465 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001466 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001467
1468 return state;
1469err:
1470 return ERR_PTR(ret);
1471
1472}
1473
1474static struct nfs4_state *
1475_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001476{
1477 struct inode *inode;
1478 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001479 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480
Trond Myklebustaac00a82007-07-05 19:02:21 -04001481 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001482 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001483 goto out;
1484 }
1485
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001486 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001488 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001489 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001490 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001491 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001492 goto err;
1493 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494 state = nfs4_get_open_state(inode, data->owner);
1495 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001496 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001497 if (data->o_res.delegation_type != 0)
1498 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001499 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001500 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001502out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001503 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001505err_put_inode:
1506 iput(inode);
1507err:
1508 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509}
1510
Andy Adamsone23008e2012-10-02 21:07:32 -04001511static struct nfs4_state *
1512nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1513{
1514 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1515 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1516 return _nfs4_opendata_to_nfs4_state(data);
1517}
1518
Trond Myklebust864472e2006-01-03 09:55:15 +01001519static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1520{
1521 struct nfs_inode *nfsi = NFS_I(state->inode);
1522 struct nfs_open_context *ctx;
1523
1524 spin_lock(&state->inode->i_lock);
1525 list_for_each_entry(ctx, &nfsi->open_files, list) {
1526 if (ctx->state != state)
1527 continue;
1528 get_nfs_open_context(ctx);
1529 spin_unlock(&state->inode->i_lock);
1530 return ctx;
1531 }
1532 spin_unlock(&state->inode->i_lock);
1533 return ERR_PTR(-ENOENT);
1534}
1535
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001536static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1537 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001538{
1539 struct nfs4_opendata *opendata;
1540
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001541 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001542 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001543 if (opendata == NULL)
1544 return ERR_PTR(-ENOMEM);
1545 opendata->state = state;
1546 atomic_inc(&state->count);
1547 return opendata;
1548}
1549
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001550static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001551{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001552 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001553 int ret;
1554
NeilBrown39f897f2015-06-29 14:28:54 +10001555 if ((opendata->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
1556 opendata->o_arg.claim == NFS4_OPEN_CLAIM_DELEG_CUR_FH) &&
1557 (opendata->o_arg.u.delegation_type & fmode) != fmode)
1558 /* This mode can't have been delegated, so we must have
1559 * a valid open_stateid to cover it - not need to reclaim.
1560 */
1561 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001562 opendata->o_arg.open_flags = 0;
1563 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001564 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1565 NFS_SB(opendata->dentry->d_sb),
1566 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001567 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1568 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1569 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001570 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001571 if (ret != 0)
1572 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001573 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001574 if (IS_ERR(newstate))
1575 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001576 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001577 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001578 return 0;
1579}
1580
1581static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1582{
Trond Myklebust864472e2006-01-03 09:55:15 +01001583 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001584 int ret;
1585
Trond Myklebust4f14c192014-02-12 19:15:06 -05001586 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1587 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1588 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1589 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001590 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001591 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001592 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001593 smp_rmb();
1594 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001595 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001596 if (ret != 0)
1597 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001598 if (newstate != state)
1599 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001600 }
1601 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001602 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001603 if (ret != 0)
1604 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001605 if (newstate != state)
1606 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001607 }
1608 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001609 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001610 if (ret != 0)
1611 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001612 if (newstate != state)
1613 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001614 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001615 /*
1616 * We may have performed cached opens for all three recoveries.
1617 * Check if we need to update the current stateid.
1618 */
1619 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001620 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001621 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001622 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001623 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001624 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001625 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001626 return 0;
1627}
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/*
1630 * OPEN_RECLAIM:
1631 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001633static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001635 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001636 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001637 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int status;
1639
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001640 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1641 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001642 if (IS_ERR(opendata))
1643 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001644 rcu_read_lock();
1645 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001646 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001647 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001648 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001649 opendata->o_arg.u.delegation_type = delegation_type;
1650 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001651 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return status;
1653}
1654
Trond Myklebust539cd032007-06-05 11:46:42 -04001655static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 struct nfs_server *server = NFS_SERVER(state->inode);
1658 struct nfs4_exception exception = { };
1659 int err;
1660 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001661 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001662 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001663 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1664 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001665 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001666 break;
1667 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 } while (exception.retry);
1669 return err;
1670}
1671
Trond Myklebust864472e2006-01-03 09:55:15 +01001672static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1673{
1674 struct nfs_open_context *ctx;
1675 int ret;
1676
1677 ctx = nfs4_state_find_open_context(state);
1678 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001679 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001680 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001681 put_nfs_open_context(ctx);
1682 return ret;
1683}
1684
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001685static 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 -07001686{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001687 switch (err) {
1688 default:
1689 printk(KERN_ERR "NFS: %s: unhandled error "
1690 "%d.\n", __func__, err);
1691 case 0:
1692 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001693 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001694 case -ESTALE:
1695 break;
1696 case -NFS4ERR_BADSESSION:
1697 case -NFS4ERR_BADSLOT:
1698 case -NFS4ERR_BAD_HIGH_SLOT:
1699 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1700 case -NFS4ERR_DEADSESSION:
1701 set_bit(NFS_DELEGATED_STATE, &state->flags);
1702 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1703 return -EAGAIN;
1704 case -NFS4ERR_STALE_CLIENTID:
1705 case -NFS4ERR_STALE_STATEID:
1706 set_bit(NFS_DELEGATED_STATE, &state->flags);
1707 case -NFS4ERR_EXPIRED:
1708 /* Don't recall a delegation if it was lost */
1709 nfs4_schedule_lease_recovery(server->nfs_client);
1710 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001711 case -NFS4ERR_MOVED:
1712 nfs4_schedule_migration_recovery(server);
1713 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001714 case -NFS4ERR_LEASE_MOVED:
1715 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1716 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001717 case -NFS4ERR_DELEG_REVOKED:
1718 case -NFS4ERR_ADMIN_REVOKED:
1719 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001720 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001721 nfs_inode_find_state_and_recover(state->inode,
1722 stateid);
1723 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001724 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001725 case -NFS4ERR_DELAY:
1726 case -NFS4ERR_GRACE:
1727 set_bit(NFS_DELEGATED_STATE, &state->flags);
1728 ssleep(1);
1729 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001730 case -ENOMEM:
1731 case -NFS4ERR_DENIED:
1732 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1733 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return err;
1736}
1737
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001738int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1739{
1740 struct nfs_server *server = NFS_SERVER(state->inode);
1741 struct nfs4_opendata *opendata;
1742 int err;
1743
1744 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1745 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1746 if (IS_ERR(opendata))
1747 return PTR_ERR(opendata);
1748 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1749 err = nfs4_open_recover(opendata, state);
1750 nfs4_opendata_put(opendata);
1751 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1752}
1753
Chuck Leverbe05c862013-08-09 12:49:47 -04001754static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1755{
1756 struct nfs4_opendata *data = calldata;
1757
Peng Taocb04ad22014-06-11 05:24:15 +08001758 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1759 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001760}
1761
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001762static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1763{
1764 struct nfs4_opendata *data = calldata;
1765
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001766 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001767
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001768 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001769 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001770 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001771 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001772 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001773 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001774 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001775}
1776
1777static void nfs4_open_confirm_release(void *calldata)
1778{
1779 struct nfs4_opendata *data = calldata;
1780 struct nfs4_state *state = NULL;
1781
1782 /* If this request hasn't been cancelled, do nothing */
1783 if (data->cancelled == 0)
1784 goto out_free;
1785 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001786 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001787 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001788 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001789 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001790 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001791out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001792 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001793}
1794
1795static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001796 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001797 .rpc_call_done = nfs4_open_confirm_done,
1798 .rpc_release = nfs4_open_confirm_release,
1799};
1800
1801/*
1802 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1803 */
1804static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1805{
David Howells2b0143b2015-03-17 22:25:59 +00001806 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001807 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001808 struct rpc_message msg = {
1809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1810 .rpc_argp = &data->c_arg,
1811 .rpc_resp = &data->c_res,
1812 .rpc_cred = data->owner->so_cred,
1813 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001814 struct rpc_task_setup task_setup_data = {
1815 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001816 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001817 .callback_ops = &nfs4_open_confirm_ops,
1818 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001819 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001820 .flags = RPC_TASK_ASYNC,
1821 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 int status;
1823
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001824 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001825 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001826 data->rpc_done = 0;
1827 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001828 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001829 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001830 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001831 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001832 status = nfs4_wait_for_completion_rpc_task(task);
1833 if (status != 0) {
1834 data->cancelled = 1;
1835 smp_wmb();
1836 } else
1837 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001838 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return status;
1840}
1841
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001842static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001844 struct nfs4_opendata *data = calldata;
1845 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001846 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001847
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001848 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001849 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001850 /*
1851 * Check if we still need to send an OPEN call, or if we can use
1852 * a delegation instead.
1853 */
1854 if (data->state != NULL) {
1855 struct nfs_delegation *delegation;
1856
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001857 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001858 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001859 rcu_read_lock();
1860 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001861 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001862 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001863 can_open_delegated(delegation, data->o_arg.fmode))
1864 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001865 rcu_read_unlock();
1866 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001867 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001868 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001869 switch (data->o_arg.claim) {
1870 case NFS4_OPEN_CLAIM_PREVIOUS:
1871 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1872 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001873 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001874 case NFS4_OPEN_CLAIM_FH:
1875 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001876 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1877 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001878 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001879 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001880 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001881 &data->o_res.seq_res,
1882 task) != 0)
1883 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001884
1885 /* Set the create mode (note dependency on the session type) */
1886 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1887 if (data->o_arg.open_flags & O_EXCL) {
1888 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1889 if (nfs4_has_persistent_session(clp))
1890 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1891 else if (clp->cl_mvops->minor_version > 0)
1892 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1893 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001894 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001895unlock_no_action:
1896 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001897out_no_action:
1898 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001899out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001900 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001901}
1902
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001903static void nfs4_open_done(struct rpc_task *task, void *calldata)
1904{
1905 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001907 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001908
Trond Myklebust14516c32010-07-31 14:29:06 -04001909 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1910 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001911
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001912 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001913 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1914 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001915 case S_IFREG:
1916 break;
1917 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001918 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001919 break;
1920 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001921 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001922 break;
1923 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001924 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001925 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001926 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001927 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001928 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1929 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001930 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001931 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001932}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001933
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001934static void nfs4_open_release(void *calldata)
1935{
1936 struct nfs4_opendata *data = calldata;
1937 struct nfs4_state *state = NULL;
1938
1939 /* If this request hasn't been cancelled, do nothing */
1940 if (data->cancelled == 0)
1941 goto out_free;
1942 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001943 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001944 goto out_free;
1945 /* In case we need an open_confirm, no cleanup! */
1946 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1947 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001948 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001949 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001950 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001951out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001952 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001953}
1954
1955static const struct rpc_call_ops nfs4_open_ops = {
1956 .rpc_call_prepare = nfs4_open_prepare,
1957 .rpc_call_done = nfs4_open_done,
1958 .rpc_release = nfs4_open_release,
1959};
1960
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001961static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001962{
David Howells2b0143b2015-03-17 22:25:59 +00001963 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001964 struct nfs_server *server = NFS_SERVER(dir);
1965 struct nfs_openargs *o_arg = &data->o_arg;
1966 struct nfs_openres *o_res = &data->o_res;
1967 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001968 struct rpc_message msg = {
1969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1970 .rpc_argp = o_arg,
1971 .rpc_resp = o_res,
1972 .rpc_cred = data->owner->so_cred,
1973 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001974 struct rpc_task_setup task_setup_data = {
1975 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001976 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001977 .callback_ops = &nfs4_open_ops,
1978 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001979 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001980 .flags = RPC_TASK_ASYNC,
1981 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001982 int status;
1983
Chuck Levera9c92d62013-08-09 12:48:18 -04001984 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001985 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001986 data->rpc_done = 0;
1987 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001988 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001989 data->is_recover = 0;
1990 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001991 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001992 data->is_recover = 1;
1993 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001994 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001995 if (IS_ERR(task))
1996 return PTR_ERR(task);
1997 status = nfs4_wait_for_completion_rpc_task(task);
1998 if (status != 0) {
1999 data->cancelled = 1;
2000 smp_wmb();
2001 } else
2002 status = data->rpc_status;
2003 rpc_put_task(task);
2004
2005 return status;
2006}
2007
2008static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2009{
David Howells2b0143b2015-03-17 22:25:59 +00002010 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002011 struct nfs_openres *o_res = &data->o_res;
2012 int status;
2013
2014 status = nfs4_run_open_task(data, 1);
2015 if (status != 0 || !data->rpc_done)
2016 return status;
2017
Trond Myklebust6926afd2012-01-07 13:22:46 -05002018 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2019
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002020 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2021 status = _nfs4_proc_open_confirm(data);
2022 if (status != 0)
2023 return status;
2024 }
2025
2026 return status;
2027}
2028
Trond Myklebustf3792d62014-07-10 08:54:32 -04002029/*
2030 * Additional permission checks in order to distinguish between an
2031 * open for read, and an open for execute. This works around the
2032 * fact that NFSv4 OPEN treats read and execute permissions as being
2033 * the same.
2034 * Note that in the non-execute case, we want to turn off permission
2035 * checking if we just created a new file (POSIX open() semantics).
2036 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002037static int nfs4_opendata_access(struct rpc_cred *cred,
2038 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002039 struct nfs4_state *state, fmode_t fmode,
2040 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002041{
2042 struct nfs_access_entry cache;
2043 u32 mask;
2044
2045 /* access call failed or for some reason the server doesn't
2046 * support any access modes -- defer access call until later */
2047 if (opendata->o_res.access_supported == 0)
2048 return 0;
2049
2050 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002051 /*
2052 * Use openflags to check for exec, because fmode won't
2053 * always have FMODE_EXEC set when file open for exec.
2054 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002055 if (openflags & __FMODE_EXEC) {
2056 /* ONLY check for exec rights */
2057 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002058 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002059 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002060
2061 cache.cred = cred;
2062 cache.jiffies = jiffies;
2063 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2064 nfs_access_add_cache(state->inode, &cache);
2065
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002066 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002067 return 0;
2068
2069 /* even though OPEN succeeded, access is denied. Close the file */
2070 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002071 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002072}
2073
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002074/*
2075 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2076 */
2077static int _nfs4_proc_open(struct nfs4_opendata *data)
2078{
David Howells2b0143b2015-03-17 22:25:59 +00002079 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002080 struct nfs_server *server = NFS_SERVER(dir);
2081 struct nfs_openargs *o_arg = &data->o_arg;
2082 struct nfs_openres *o_res = &data->o_res;
2083 int status;
2084
2085 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002086 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002087 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002088 if (status != 0) {
2089 if (status == -NFS4ERR_BADNAME &&
2090 !(o_arg->open_flags & O_CREAT))
2091 return -ENOENT;
2092 return status;
2093 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002094
Trond Myklebust6926afd2012-01-07 13:22:46 -05002095 nfs_fattr_map_and_free_names(server, &data->f_attr);
2096
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002097 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002098 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002099 if (o_arg->open_flags & O_EXCL)
2100 data->file_created = 1;
2101 else if (o_res->cinfo.before != o_res->cinfo.after)
2102 data->file_created = 1;
2103 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002104 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2105 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002107 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002109 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002112 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Andy Adamsond83217c2011-03-01 01:34:17 +00002116static int nfs4_recover_expired_lease(struct nfs_server *server)
2117{
2118 return nfs4_client_recover_expired_lease(server->nfs_client);
2119}
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121/*
2122 * OPEN_EXPIRED:
2123 * reclaim state on the server after a network partition.
2124 * Assumes caller holds the appropriate lock
2125 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002126static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002128 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002129 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002131 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002132 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002133 if (IS_ERR(opendata))
2134 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002135 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002136 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002137 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002138 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002139 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002142static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002143{
Trond Myklebust539cd032007-06-05 11:46:42 -04002144 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002145 struct nfs4_exception exception = { };
2146 int err;
2147
2148 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002149 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002150 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002151 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2152 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002153 switch (err) {
2154 default:
2155 goto out;
2156 case -NFS4ERR_GRACE:
2157 case -NFS4ERR_DELAY:
2158 nfs4_handle_exception(server, err, &exception);
2159 err = 0;
2160 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002161 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002162out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002163 return err;
2164}
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002169 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Trond Myklebust864472e2006-01-03 09:55:15 +01002171 ctx = nfs4_state_find_open_context(state);
2172 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002173 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002174 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002175 put_nfs_open_context(ctx);
2176 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002179static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2180{
2181 nfs_remove_bad_delegation(state->inode);
2182 write_seqlock(&state->seqlock);
2183 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2184 write_sequnlock(&state->seqlock);
2185 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2186}
2187
2188static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2189{
2190 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2191 nfs_finish_clear_delegation_stateid(state);
2192}
2193
2194static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2195{
2196 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2197 nfs40_clear_delegation_stateid(state);
2198 return nfs4_open_expired(sp, state);
2199}
2200
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002201#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002202static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002203{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002204 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002205 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002206 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002207 struct rpc_cred *cred;
2208 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002209
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002210 /* Get the delegation credential for use by test/free_stateid */
2211 rcu_read_lock();
2212 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002213 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002214 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002215 return;
2216 }
2217
2218 nfs4_stateid_copy(&stateid, &delegation->stateid);
2219 cred = get_rpccred(delegation->cred);
2220 rcu_read_unlock();
2221 status = nfs41_test_stateid(server, &stateid, cred);
2222 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002223
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002224 if (status != NFS_OK) {
2225 /* Free the stateid unless the server explicitly
2226 * informs us the stateid is unrecognized. */
2227 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002228 nfs41_free_stateid(server, &stateid, cred);
2229 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002230 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002231
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002232 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002233}
2234
2235/**
2236 * nfs41_check_open_stateid - possibly free an open stateid
2237 *
2238 * @state: NFSv4 state for an inode
2239 *
2240 * Returns NFS_OK if recovery for this stateid is now finished.
2241 * Otherwise a negative NFS4ERR value is returned.
2242 */
2243static int nfs41_check_open_stateid(struct nfs4_state *state)
2244{
2245 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002246 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002247 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002248 int status;
2249
2250 /* If a state reset has been done, test_stateid is unneeded */
2251 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2252 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2253 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2254 return -NFS4ERR_BAD_STATEID;
2255
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002256 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002257 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002258 if (status != NFS_OK) {
2259 /* Free the stateid unless the server explicitly
2260 * informs us the stateid is unrecognized. */
2261 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002262 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002263
2264 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2265 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2266 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002267 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002268 }
2269 return status;
2270}
2271
2272static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2273{
Chuck Levereb64cf92012-07-11 16:30:05 -04002274 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002275
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002276 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002277 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002278 if (status != NFS_OK)
2279 status = nfs4_open_expired(sp, state);
2280 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002281}
2282#endif
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002285 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2286 * fields corresponding to attributes that were used to store the verifier.
2287 * Make sure we clobber those fields in the later setattr call
2288 */
2289static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2290{
2291 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2292 !(sattr->ia_valid & ATTR_ATIME_SET))
2293 sattr->ia_valid |= ATTR_ATIME;
2294
2295 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2296 !(sattr->ia_valid & ATTR_MTIME_SET))
2297 sattr->ia_valid |= ATTR_MTIME;
2298}
2299
Trond Myklebustc21443c2013-02-07 14:26:21 -05002300static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2301 fmode_t fmode,
2302 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002303 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002304{
2305 struct nfs4_state_owner *sp = opendata->owner;
2306 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002307 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002308 struct nfs4_state *state;
2309 unsigned int seq;
2310 int ret;
2311
2312 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2313
2314 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002315 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002316 goto out;
2317
2318 state = nfs4_opendata_to_nfs4_state(opendata);
2319 ret = PTR_ERR(state);
2320 if (IS_ERR(state))
2321 goto out;
2322 if (server->caps & NFS_CAP_POSIX_LOCK)
2323 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2324
Trond Myklebust275bb302013-05-29 13:11:28 -04002325 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002326 if (d_really_is_negative(dentry)) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002327 /* FIXME: Is this d_drop() ever needed? */
2328 d_drop(dentry);
2329 dentry = d_add_unique(dentry, igrab(state->inode));
2330 if (dentry == NULL) {
2331 dentry = opendata->dentry;
2332 } else if (dentry != ctx->dentry) {
2333 dput(ctx->dentry);
2334 ctx->dentry = dget(dentry);
2335 }
2336 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002337 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002338 }
2339
Trond Myklebustc21443c2013-02-07 14:26:21 -05002340 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2341 if (ret != 0)
2342 goto out;
2343
Trond Myklebust3efb9722013-05-29 13:17:04 -04002344 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002345 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002346 nfs_inode_attach_open_context(ctx);
2347 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2348 nfs4_schedule_stateid_recovery(server, state);
2349 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002350out:
2351 return ret;
2352}
2353
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002354/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002355 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 */
Andy Adamson82be4172012-05-23 05:02:35 -04002357static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002358 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002359 int flags,
2360 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002361 struct nfs4_label *label,
2362 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
2364 struct nfs4_state_owner *sp;
2365 struct nfs4_state *state = NULL;
2366 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002367 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002368 struct dentry *dentry = ctx->dentry;
2369 struct rpc_cred *cred = ctx->cred;
2370 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2371 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002372 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002373 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002374 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002378 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2379 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2381 goto out_err;
2382 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002383 status = nfs4_recover_expired_lease(server);
2384 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002385 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002386 if (d_really_is_positive(dentry))
2387 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002388 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002389 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002390 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002391 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002392 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002393 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002394 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
David Quigley14c43f72013-05-22 12:50:43 -04002396 if (label) {
2397 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2398 if (IS_ERR(olabel)) {
2399 status = PTR_ERR(olabel);
2400 goto err_opendata_put;
2401 }
2402 }
2403
Trond Myklebuste911b812014-03-26 13:24:37 -07002404 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2405 if (!opendata->f_attr.mdsthreshold) {
2406 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2407 if (!opendata->f_attr.mdsthreshold)
2408 goto err_free_label;
2409 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002410 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002411 }
David Howells2b0143b2015-03-17 22:25:59 +00002412 if (d_really_is_positive(dentry))
2413 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002414
Trond Myklebust3efb9722013-05-29 13:17:04 -04002415 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002416 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002417 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002418 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002419
Trond Myklebust549b19c2013-04-16 18:42:34 -04002420 if ((opendata->o_arg.open_flags & O_EXCL) &&
2421 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002422 nfs4_exclusive_attrset(opendata, sattr);
2423
2424 nfs_fattr_init(opendata->o_res.f_attr);
2425 status = nfs4_do_setattr(state->inode, cred,
2426 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002427 state, label, olabel);
2428 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05002429 nfs_setattr_update_inode(state->inode, sattr,
2430 opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002431 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002432 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002433 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002434 if (opendata->file_created)
2435 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002436
Trond Myklebuste911b812014-03-26 13:24:37 -07002437 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002438 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002439 opendata->f_attr.mdsthreshold = NULL;
2440 }
Andy Adamson82be4172012-05-23 05:02:35 -04002441
David Quigley14c43f72013-05-22 12:50:43 -04002442 nfs4_label_free(olabel);
2443
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002444 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002447err_free_label:
2448 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002449err_opendata_put:
2450 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002451err_put_state_owner:
2452 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return status;
2455}
2456
2457
Andy Adamson82be4172012-05-23 05:02:35 -04002458static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002459 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002460 int flags,
2461 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002462 struct nfs4_label *label,
2463 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002465 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 struct nfs4_exception exception = { };
2467 struct nfs4_state *res;
2468 int status;
2469
2470 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002471 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002472 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002473 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (status == 0)
2475 break;
2476 /* NOTE: BAD_SEQID means the server and client disagree about the
2477 * book-keeping w.r.t. state-changing operations
2478 * (OPEN/CLOSE/LOCK/LOCKU...)
2479 * It is actually a sign of a bug on the client or on the server.
2480 *
2481 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002482 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 * have unhashed the old state_owner for us, and that we can
2484 * therefore safely retry using a new one. We should still warn
2485 * the user though...
2486 */
2487 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002488 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002489 " returned a bad sequence-id error!\n",
2490 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 exception.retry = 1;
2492 continue;
2493 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002494 /*
2495 * BAD_STATEID on OPEN means that the server cancelled our
2496 * state before it received the OPEN_CONFIRM.
2497 * Recover by retrying the request as per the discussion
2498 * on Page 181 of RFC3530.
2499 */
2500 if (status == -NFS4ERR_BAD_STATEID) {
2501 exception.retry = 1;
2502 continue;
2503 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002504 if (status == -EAGAIN) {
2505 /* We must have found a delegation */
2506 exception.retry = 1;
2507 continue;
2508 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002509 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2510 continue;
2511 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 status, &exception));
2513 } while (exception.retry);
2514 return res;
2515}
2516
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002517static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2518 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002519 struct nfs4_state *state, struct nfs4_label *ilabel,
2520 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002522 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002524 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 .iap = sattr,
2526 .server = server,
2527 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002528 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 };
2530 struct nfs_setattrres res = {
2531 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002532 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 .server = server,
2534 };
2535 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2537 .rpc_argp = &arg,
2538 .rpc_resp = &res,
2539 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002541 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002542 fmode_t fmode;
2543 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002544 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
David Quigleyaa9c2662013-05-22 12:50:44 -04002546 arg.bitmask = nfs4_bitmask(server, ilabel);
2547 if (ilabel)
2548 arg.bitmask = nfs4_bitmask(server, olabel);
2549
Trond Myklebust0e574af2005-10-27 22:12:38 -04002550 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Trond Myklebustee3ae842013-04-29 10:35:36 -04002552 /* Servers should only apply open mode checks for file size changes */
2553 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2554 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2555
2556 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2557 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002558 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002559 struct nfs_lockowner lockowner = {
2560 .l_owner = current->files,
2561 .l_pid = current->tgid,
2562 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002563 if (!nfs4_valid_open_stateid(state))
2564 return -EBADF;
2565 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2566 &lockowner) == -EIO)
2567 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002568 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002569 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Bryan Schumaker7c513052011-03-24 17:12:24 +00002571 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002572 if (status == 0 && state != NULL)
2573 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002574 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002577static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2578 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002579 struct nfs4_state *state, struct nfs4_label *ilabel,
2580 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002582 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002583 struct nfs4_exception exception = {
2584 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002585 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002586 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 int err;
2588 do {
David Quigley1775fd32013-05-22 12:50:42 -04002589 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002590 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002591 switch (err) {
2592 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002593 if (!(sattr->ia_valid & ATTR_SIZE)) {
2594 pr_warn_once("NFSv4: server %s is incorrectly "
2595 "applying open mode checks to "
2596 "a SETATTR that is not "
2597 "changing file size.\n",
2598 server->nfs_client->cl_hostname);
2599 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002600 if (state && !(state->state & FMODE_WRITE)) {
2601 err = -EBADF;
2602 if (sattr->ia_valid & ATTR_OPEN)
2603 err = -EACCES;
2604 goto out;
2605 }
2606 }
2607 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002609out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return err;
2611}
2612
2613struct nfs4_closedata {
2614 struct inode *inode;
2615 struct nfs4_state *state;
2616 struct nfs_closeargs arg;
2617 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002618 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002619 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002620 bool roc;
2621 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622};
2623
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002624static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002625{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002626 struct nfs4_closedata *calldata = data;
2627 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002628 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002629
Fred Isamanf7e89172011-01-06 11:36:32 +00002630 if (calldata->roc)
2631 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002632 nfs4_put_open_state(calldata->state);
2633 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002634 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002635 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002636 kfree(calldata);
2637}
2638
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002639static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002641 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002644 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Chuck Levera3ca5652012-03-01 17:00:40 -05002646 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002647 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2648 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002649 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 /* hmm. we are done with the inode, and in the process of freeing
2651 * the state_owner. we keep this around to process errors
2652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 switch (task->tk_status) {
2654 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002655 res_stateid = &calldata->res.stateid;
2656 if (calldata->arg.fmode == 0 && calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002657 pnfs_roc_set_barrier(state->inode,
2658 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002659 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002660 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002661 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002663 case -NFS4ERR_OLD_STATEID:
2664 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002666 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002667 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002668 rpc_restart_call_prepare(task);
2669 goto out_release;
2670 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002671 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002674 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002675 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002676 goto out_release;
2677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
Trond Myklebust412f6c42014-08-25 22:09:08 -04002679 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002680out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002681 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002682 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002683 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002686static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002688 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002689 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002690 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002691 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002692 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002693
Chuck Levera3ca5652012-03-01 17:00:40 -05002694 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002695 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002696 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002697
Trond Myklebust88069f72009-12-08 08:33:16 -05002698 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002699 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002700 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2701 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2702 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002703 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002704 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002705 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002706 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002707 if (state->n_rdonly == 0)
2708 call_close |= is_rdonly;
2709 else if (is_rdonly)
2710 calldata->arg.fmode |= FMODE_READ;
2711 if (state->n_wronly == 0)
2712 call_close |= is_wronly;
2713 else if (is_wronly)
2714 calldata->arg.fmode |= FMODE_WRITE;
2715 } else if (is_rdwr)
2716 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2717
2718 if (calldata->arg.fmode == 0)
2719 call_close |= is_rdwr;
2720
Trond Myklebust5d422302013-03-14 16:57:48 -04002721 if (!nfs4_valid_open_stateid(state))
2722 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002723 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002724
2725 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002726 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002727 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002728 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002729
Fred Isamanf7e89172011-01-06 11:36:32 +00002730 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002731 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002732 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002733 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2734 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002735 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002736 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002737 }
Trond Myklebust6ae37332015-01-30 14:21:14 -05002738 calldata->arg.share_access =
2739 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2740 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002741
Trond Myklebust516a6af2005-10-27 22:12:41 -04002742 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002743 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002744 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002745 &calldata->arg.seq_args,
2746 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002747 task) != 0)
2748 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002749 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002750 return;
2751out_no_action:
2752 task->tk_action = NULL;
2753out_wait:
2754 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002757static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002758 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002759 .rpc_call_done = nfs4_close_done,
2760 .rpc_release = nfs4_free_closedata,
2761};
2762
Peng Taofe08c542014-07-03 13:05:01 +08002763static bool nfs4_roc(struct inode *inode)
2764{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002765 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002766 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002767 return pnfs_roc(inode);
2768}
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770/*
2771 * It is possible for data to be read/written from a mem-mapped file
2772 * after the sys_close call (which hits the vfs layer as a flush).
2773 * This means that we can't safely call nfsv4 close on a file until
2774 * the inode is cleared. This in turn means that we are not good
2775 * NFSv4 citizens - we do not indicate to the server to update the file's
2776 * share state even when we are done with one of the three share
2777 * stateid's in the inode.
2778 *
2779 * NOTE: Caller must be holding the sp->so_owner semaphore!
2780 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002781int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002783 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002784 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002786 struct nfs4_state_owner *sp = state->owner;
2787 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002788 struct rpc_message msg = {
2789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2790 .rpc_cred = state->owner->so_cred,
2791 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002792 struct rpc_task_setup task_setup_data = {
2793 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002794 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002795 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002796 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002797 .flags = RPC_TASK_ASYNC,
2798 };
Trond Myklebust95121352005-10-18 14:20:12 -07002799 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002801 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2802 &task_setup_data.rpc_client, &msg);
2803
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002804 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002806 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002807 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002808 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002810 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002812 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2813 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002814 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002815 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002816 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002817 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002818 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002819 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002820 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002821 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002822 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002823
Trond Myklebust1174dd12010-12-21 10:52:24 -05002824 msg.rpc_argp = &calldata->arg;
2825 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002826 task_setup_data.callback_data = calldata;
2827 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002828 if (IS_ERR(task))
2829 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002830 status = 0;
2831 if (wait)
2832 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002833 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002834 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002835out_free_calldata:
2836 kfree(calldata);
2837out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002838 nfs4_put_open_state(state);
2839 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002840 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
Trond Myklebust2b484292010-09-17 10:56:51 -04002843static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002844nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2845 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002848 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2849
2850 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Trond Myklebust565277f2007-10-15 18:17:53 -04002852 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002853 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002854
2855 nfs4_label_release_security(label);
2856
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002857 if (IS_ERR(state))
2858 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002859 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
Trond Myklebust1185a552009-12-03 15:54:02 -05002862static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002863{
2864 if (ctx->state == NULL)
2865 return;
2866 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002867 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002868 else
Al Viro643168c2011-06-22 18:20:23 -04002869 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002870}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Trond Myklebustb944dba2013-11-04 15:20:20 -05002872#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2873#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002874#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2877{
Benny Halevy43652ad2009-04-01 09:21:54 -04002878 struct nfs4_server_caps_arg args = {
2879 .fhandle = fhandle,
2880 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 struct nfs4_server_caps_res res = {};
2882 struct rpc_message msg = {
2883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002884 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 .rpc_resp = &res,
2886 };
2887 int status;
2888
Bryan Schumaker7c513052011-03-24 17:12:24 +00002889 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002891 /* Sanity check the server answers */
2892 switch (server->nfs_client->cl_minorversion) {
2893 case 0:
2894 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2895 res.attr_bitmask[2] = 0;
2896 break;
2897 case 1:
2898 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2899 break;
2900 case 2:
2901 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002904 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2905 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2906 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2907 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002908 NFS_CAP_CTIME|NFS_CAP_MTIME|
2909 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002910 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2911 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 server->caps |= NFS_CAP_ACLS;
2913 if (res.has_links != 0)
2914 server->caps |= NFS_CAP_HARDLINKS;
2915 if (res.has_symlinks != 0)
2916 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002917 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2918 server->caps |= NFS_CAP_FILEID;
2919 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2920 server->caps |= NFS_CAP_MODE;
2921 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2922 server->caps |= NFS_CAP_NLINK;
2923 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2924 server->caps |= NFS_CAP_OWNER;
2925 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2926 server->caps |= NFS_CAP_OWNER_GROUP;
2927 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2928 server->caps |= NFS_CAP_ATIME;
2929 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2930 server->caps |= NFS_CAP_CTIME;
2931 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2932 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002933#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2934 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2935 server->caps |= NFS_CAP_SECURITY_LABEL;
2936#endif
2937 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2938 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002939 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002940
Trond Myklebusta65318b2009-03-11 14:10:28 -04002941 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2942 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2943 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002944 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002946 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return status;
2950}
2951
Trond Myklebust55a97592006-06-09 09:34:19 -04002952int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 struct nfs4_exception exception = { };
2955 int err;
2956 do {
2957 err = nfs4_handle_exception(server,
2958 _nfs4_server_capabilities(server, fhandle),
2959 &exception);
2960 } while (exception.retry);
2961 return err;
2962}
2963
2964static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2965 struct nfs_fsinfo *info)
2966{
David Quigleyaa9c2662013-05-22 12:50:44 -04002967 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002969 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 };
2971 struct nfs4_lookup_res res = {
2972 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002973 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 .fh = fhandle,
2975 };
2976 struct rpc_message msg = {
2977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2978 .rpc_argp = &args,
2979 .rpc_resp = &res,
2980 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002981
David Quigleyaa9c2662013-05-22 12:50:44 -04002982 bitmask[0] = nfs4_fattr_bitmap[0];
2983 bitmask[1] = nfs4_fattr_bitmap[1];
2984 /*
2985 * Process the label in the upcoming getfattr
2986 */
2987 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2988
Trond Myklebust0e574af2005-10-27 22:12:38 -04002989 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002990 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
2993static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2994 struct nfs_fsinfo *info)
2995{
2996 struct nfs4_exception exception = { };
2997 int err;
2998 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002999 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003000 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003001 switch (err) {
3002 case 0:
3003 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003004 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003005 default:
3006 err = nfs4_handle_exception(server, err, &exception);
3007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003009out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return err;
3011}
3012
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003013static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3014 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3015{
Trond Myklebustc2190662013-08-26 19:23:04 -04003016 struct rpc_auth_create_args auth_args = {
3017 .pseudoflavor = flavor,
3018 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003019 struct rpc_auth *auth;
3020 int ret;
3021
Trond Myklebustc2190662013-08-26 19:23:04 -04003022 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003023 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003024 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003025 goto out;
3026 }
3027 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003028out:
3029 return ret;
3030}
3031
Chuck Lever9a744ba2013-03-16 15:56:02 -04003032/*
3033 * Retry pseudoroot lookup with various security flavors. We do this when:
3034 *
3035 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3036 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3037 *
3038 * Returns zero on success, or a negative NFS4ERR value, or a
3039 * negative errno value.
3040 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003041static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003042 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003044 /* Per 3530bis 15.33.5 */
3045 static const rpc_authflavor_t flav_array[] = {
3046 RPC_AUTH_GSS_KRB5P,
3047 RPC_AUTH_GSS_KRB5I,
3048 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003049 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003050 RPC_AUTH_NULL,
3051 };
3052 int status = -EPERM;
3053 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003055 if (server->auth_info.flavor_len > 0) {
3056 /* try each flavor specified by user */
3057 for (i = 0; i < server->auth_info.flavor_len; i++) {
3058 status = nfs4_lookup_root_sec(server, fhandle, info,
3059 server->auth_info.flavors[i]);
3060 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3061 continue;
3062 break;
3063 }
3064 } else {
3065 /* no flavors specified by user, try default list */
3066 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3067 status = nfs4_lookup_root_sec(server, fhandle, info,
3068 flav_array[i]);
3069 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3070 continue;
3071 break;
3072 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003073 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003074
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003075 /*
3076 * -EACCESS could mean that the user doesn't have correct permissions
3077 * to access the mount. It could also mean that we tried to mount
3078 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3079 * existing mount programs don't handle -EACCES very well so it should
3080 * be mapped to -EPERM instead.
3081 */
3082 if (status == -EACCES)
3083 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003084 return status;
3085}
3086
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003087static int nfs4_do_find_root_sec(struct nfs_server *server,
3088 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3089{
3090 int mv = server->nfs_client->cl_minorversion;
3091 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3092}
3093
3094/**
3095 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3096 * @server: initialized nfs_server handle
3097 * @fhandle: we fill in the pseudo-fs root file handle
3098 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003099 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003100 *
3101 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003102 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003103int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003104 struct nfs_fsinfo *info,
3105 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003106{
Andre Przywarac7757072015-04-23 17:17:40 +01003107 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003108
Andre Przywarac7757072015-04-23 17:17:40 +01003109 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003110 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003111
3112 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003113 status = nfs4_do_find_root_sec(server, fhandle, info);
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 if (status == 0)
3116 status = nfs4_server_capabilities(server, fhandle);
3117 if (status == 0)
3118 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003119
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003120 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Bryan Schumakerbae36242012-05-10 15:07:31 -04003123static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3124 struct nfs_fsinfo *info)
3125{
3126 int error;
3127 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003128 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003129
3130 error = nfs4_server_capabilities(server, mntfh);
3131 if (error < 0) {
3132 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3133 return error;
3134 }
3135
David Quigley14c43f72013-05-22 12:50:43 -04003136 label = nfs4_label_alloc(server, GFP_KERNEL);
3137 if (IS_ERR(label))
3138 return PTR_ERR(label);
3139
David Quigley1775fd32013-05-22 12:50:42 -04003140 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003141 if (error < 0) {
3142 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003143 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003144 }
3145
3146 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3147 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3148 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3149
David Quigley14c43f72013-05-22 12:50:43 -04003150err_free_label:
3151 nfs4_label_free(label);
3152
Bryan Schumakerbae36242012-05-10 15:07:31 -04003153 return error;
3154}
3155
Manoj Naik6b97fd32006-06-09 09:34:29 -04003156/*
3157 * Get locations and (maybe) other attributes of a referral.
3158 * Note that we'll actually follow the referral later when
3159 * we detect fsid mismatch in inode revalidation
3160 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003161static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3162 const struct qstr *name, struct nfs_fattr *fattr,
3163 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003164{
3165 int status = -ENOMEM;
3166 struct page *page = NULL;
3167 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003168
3169 page = alloc_page(GFP_KERNEL);
3170 if (page == NULL)
3171 goto out;
3172 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3173 if (locations == NULL)
3174 goto out;
3175
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003176 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003177 if (status != 0)
3178 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003179
3180 /*
3181 * If the fsid didn't change, this is a migration event, not a
3182 * referral. Cause us to drop into the exception handler, which
3183 * will kick off migration recovery.
3184 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003185 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003186 dprintk("%s: server did not return a different fsid for"
3187 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003188 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003189 goto out;
3190 }
Andy Adamson533eb462011-06-13 18:25:56 -04003191 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3192 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003193
Andy Adamson533eb462011-06-13 18:25:56 -04003194 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003195 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003196 memset(fhandle, 0, sizeof(struct nfs_fh));
3197out:
3198 if (page)
3199 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003200 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003201 return status;
3202}
3203
David Quigley1775fd32013-05-22 12:50:42 -04003204static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3205 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
3207 struct nfs4_getattr_arg args = {
3208 .fh = fhandle,
3209 .bitmask = server->attr_bitmask,
3210 };
3211 struct nfs4_getattr_res res = {
3212 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003213 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 .server = server,
3215 };
3216 struct rpc_message msg = {
3217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3218 .rpc_argp = &args,
3219 .rpc_resp = &res,
3220 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003221
3222 args.bitmask = nfs4_bitmask(server, label);
3223
Trond Myklebust0e574af2005-10-27 22:12:38 -04003224 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003225 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
David Quigley1775fd32013-05-22 12:50:42 -04003228static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3229 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
3231 struct nfs4_exception exception = { };
3232 int err;
3233 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003234 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3235 trace_nfs4_getattr(server, fhandle, fattr, err);
3236 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 &exception);
3238 } while (exception.retry);
3239 return err;
3240}
3241
3242/*
3243 * The file is not closed if it is opened due to the a request to change
3244 * the size of the file. The open call will not be needed once the
3245 * VFS layer lookup-intents are implemented.
3246 *
3247 * Close is called when the inode is destroyed.
3248 * If we haven't opened the file for O_WRONLY, we
3249 * need to in the size_change case to obtain a stateid.
3250 *
3251 * Got race?
3252 * Because OPEN is always done by name in nfsv4, it is
3253 * possible that we opened a different file by the same
3254 * name. We can recognize this race condition, but we
3255 * can't do anything about it besides returning an error.
3256 *
3257 * This will be fixed with VFS changes (lookup-intent).
3258 */
3259static int
3260nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3261 struct iattr *sattr)
3262{
David Howells2b0143b2015-03-17 22:25:59 +00003263 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003264 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003265 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003266 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 int status;
3268
Peng Tao88ac8152014-09-12 11:04:10 +08003269 if (pnfs_ld_layoutret_on_setattr(inode) &&
3270 sattr->ia_valid & ATTR_SIZE &&
3271 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003272 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003273
Trond Myklebust0e574af2005-10-27 22:12:38 -04003274 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Andy Adamson26699402012-05-30 16:12:24 -04003276 /* Deal with open(O_TRUNC) */
3277 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003278 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003279
3280 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003281 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003282 return 0;
3283
Trond Myklebustd5308382005-11-04 15:33:38 -05003284 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003285 if (sattr->ia_valid & ATTR_FILE) {
3286 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003287
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003288 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003289 if (ctx) {
3290 cred = ctx->cred;
3291 state = ctx->state;
3292 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003293 }
3294
David Quigley14c43f72013-05-22 12:50:43 -04003295 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3296 if (IS_ERR(label))
3297 return PTR_ERR(label);
3298
3299 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003300 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003301 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003302 nfs_setsecurity(inode, fattr, label);
3303 }
David Quigley14c43f72013-05-22 12:50:43 -04003304 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return status;
3306}
3307
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003308static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3309 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003310 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003311{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003312 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003313 int status;
3314 struct nfs4_lookup_arg args = {
3315 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003316 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003317 .name = name,
3318 };
3319 struct nfs4_lookup_res res = {
3320 .server = server,
3321 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003322 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003323 .fh = fhandle,
3324 };
3325 struct rpc_message msg = {
3326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3327 .rpc_argp = &args,
3328 .rpc_resp = &res,
3329 };
3330
David Quigleyaa9c2662013-05-22 12:50:44 -04003331 args.bitmask = nfs4_bitmask(server, label);
3332
David Howells2b3de442006-08-22 20:06:09 -04003333 nfs_fattr_init(fattr);
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003336 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 dprintk("NFS reply lookup: %d\n", status);
3338 return status;
3339}
3340
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003341static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003342{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003343 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003344 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003345 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3346 fattr->nlink = 2;
3347}
3348
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003349static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3350 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003351 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003352{
3353 struct nfs4_exception exception = { };
3354 struct rpc_clnt *client = *clnt;
3355 int err;
3356 do {
David Quigley1775fd32013-05-22 12:50:42 -04003357 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003358 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003359 switch (err) {
3360 case -NFS4ERR_BADNAME:
3361 err = -ENOENT;
3362 goto out;
3363 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003364 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003365 if (err == -NFS4ERR_MOVED)
3366 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003367 goto out;
3368 case -NFS4ERR_WRONGSEC:
3369 err = -EPERM;
3370 if (client != *clnt)
3371 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003372 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003373 if (IS_ERR(client))
3374 return PTR_ERR(client);
3375
3376 exception.retry = 1;
3377 break;
3378 default:
3379 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3380 }
3381 } while (exception.retry);
3382
3383out:
3384 if (err == 0)
3385 *clnt = client;
3386 else if (client != *clnt)
3387 rpc_shutdown_client(client);
3388
3389 return err;
3390}
3391
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003392static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003393 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3394 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003396 int status;
3397 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003398
David Quigley1775fd32013-05-22 12:50:42 -04003399 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003400 if (client != NFS_CLIENT(dir)) {
3401 rpc_shutdown_client(client);
3402 nfs_fixup_secinfo_attributes(fattr);
3403 }
3404 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405}
3406
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003407struct rpc_clnt *
3408nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3409 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3410{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003411 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003412 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003413
David Quigley1775fd32013-05-22 12:50:42 -04003414 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003415 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003416 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003417 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003418}
3419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3421{
Trond Myklebust76b32992007-08-10 17:45:11 -04003422 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 struct nfs4_accessargs args = {
3424 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003425 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003427 struct nfs4_accessres res = {
3428 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003429 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 struct rpc_message msg = {
3431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3432 .rpc_argp = &args,
3433 .rpc_resp = &res,
3434 .rpc_cred = entry->cred,
3435 };
3436 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003437 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439 /*
3440 * Determine which access bits we want to ask for...
3441 */
3442 if (mode & MAY_READ)
3443 args.access |= NFS4_ACCESS_READ;
3444 if (S_ISDIR(inode->i_mode)) {
3445 if (mode & MAY_WRITE)
3446 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3447 if (mode & MAY_EXEC)
3448 args.access |= NFS4_ACCESS_LOOKUP;
3449 } else {
3450 if (mode & MAY_WRITE)
3451 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3452 if (mode & MAY_EXEC)
3453 args.access |= NFS4_ACCESS_EXECUTE;
3454 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003455
3456 res.fattr = nfs_alloc_fattr();
3457 if (res.fattr == NULL)
3458 return -ENOMEM;
3459
Bryan Schumaker7c513052011-03-24 17:12:24 +00003460 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003462 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003463 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003465 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return status;
3467}
3468
3469static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3470{
3471 struct nfs4_exception exception = { };
3472 int err;
3473 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003474 err = _nfs4_proc_access(inode, entry);
3475 trace_nfs4_access(inode, err);
3476 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 &exception);
3478 } while (exception.retry);
3479 return err;
3480}
3481
3482/*
3483 * TODO: For the time being, we don't try to get any attributes
3484 * along with any of the zero-copy operations READ, READDIR,
3485 * READLINK, WRITE.
3486 *
3487 * In the case of the first three, we want to put the GETATTR
3488 * after the read-type operation -- this is because it is hard
3489 * to predict the length of a GETATTR response in v4, and thus
3490 * align the READ data correctly. This means that the GETATTR
3491 * may end up partially falling into the page cache, and we should
3492 * shift it into the 'tail' of the xdr_buf before processing.
3493 * To do this efficiently, we need to know the total length
3494 * of data received, which doesn't seem to be available outside
3495 * of the RPC layer.
3496 *
3497 * In the case of WRITE, we also want to put the GETATTR after
3498 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003499 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 *
3501 * Both of these changes to the XDR layer would in fact be quite
3502 * minor, but I decided to leave them for a subsequent patch.
3503 */
3504static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3505 unsigned int pgbase, unsigned int pglen)
3506{
3507 struct nfs4_readlink args = {
3508 .fh = NFS_FH(inode),
3509 .pgbase = pgbase,
3510 .pglen = pglen,
3511 .pages = &page,
3512 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003513 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 struct rpc_message msg = {
3515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3516 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003517 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 };
3519
Bryan Schumaker7c513052011-03-24 17:12:24 +00003520 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 -07003521}
3522
3523static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3524 unsigned int pgbase, unsigned int pglen)
3525{
3526 struct nfs4_exception exception = { };
3527 int err;
3528 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003529 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3530 trace_nfs4_readlink(inode, err);
3531 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 &exception);
3533 } while (exception.retry);
3534 return err;
3535}
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003538 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540static int
3541nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003542 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
David Quigleyaa9c2662013-05-22 12:50:44 -04003544 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003545 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003547 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 int status = 0;
3549
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003550 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3551 if (IS_ERR(ctx))
3552 return PTR_ERR(ctx);
3553
David Quigleyaa9c2662013-05-22 12:50:44 -04003554 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3555
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003556 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003557 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 if (IS_ERR(state)) {
3559 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003560 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003563 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003564 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return status;
3566}
3567
3568static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3569{
Trond Myklebust16e42952005-10-27 22:12:44 -04003570 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003571 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003573 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003575 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003576 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003577 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3580 .rpc_argp = &args,
3581 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003583 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003584
Bryan Schumaker7c513052011-03-24 17:12:24 +00003585 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003586 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003587 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 return status;
3589}
3590
3591static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3592{
3593 struct nfs4_exception exception = { };
3594 int err;
3595 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003596 err = _nfs4_proc_remove(dir, name);
3597 trace_nfs4_remove(dir, name, err);
3598 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 &exception);
3600 } while (exception.retry);
3601 return err;
3602}
3603
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003604static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003606 struct nfs_server *server = NFS_SERVER(dir);
3607 struct nfs_removeargs *args = msg->rpc_argp;
3608 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003610 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003612 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003613
3614 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003617static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3618{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003619 nfs4_setup_sequence(NFS_SERVER(data->dir),
3620 &data->args.seq_args,
3621 &data->res.seq_res,
3622 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623}
3624
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003625static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003627 struct nfs_unlinkdata *data = task->tk_calldata;
3628 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003629
Trond Myklebust14516c32010-07-31 14:29:06 -04003630 if (!nfs4_sequence_done(task, &res->seq_res))
3631 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003632 if (nfs4_async_handle_error(task, res->server, NULL,
3633 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003634 return 0;
3635 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003636 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
3638
Jeff Laytond3d41522010-09-17 17:31:57 -04003639static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3640{
3641 struct nfs_server *server = NFS_SERVER(dir);
3642 struct nfs_renameargs *arg = msg->rpc_argp;
3643 struct nfs_renameres *res = msg->rpc_resp;
3644
3645 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003646 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003647 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003648}
3649
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003650static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3651{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003652 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3653 &data->args.seq_args,
3654 &data->res.seq_res,
3655 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003656}
3657
3658static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3659 struct inode *new_dir)
3660{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003661 struct nfs_renamedata *data = task->tk_calldata;
3662 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003663
3664 if (!nfs4_sequence_done(task, &res->seq_res))
3665 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003666 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003667 return 0;
3668
3669 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003670 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003671 return 1;
3672}
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3675{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003676 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 struct nfs4_link_arg arg = {
3678 .fh = NFS_FH(inode),
3679 .dir_fh = NFS_FH(dir),
3680 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003681 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003683 struct nfs4_link_res res = {
3684 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003685 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003686 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 struct rpc_message msg = {
3688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3689 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003690 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003692 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Trond Myklebust136f2622010-04-16 16:22:49 -04003694 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003695 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003696 goto out;
3697
David Quigley14c43f72013-05-22 12:50:43 -04003698 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3699 if (IS_ERR(res.label)) {
3700 status = PTR_ERR(res.label);
3701 goto out;
3702 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003703 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003704
Bryan Schumaker7c513052011-03-24 17:12:24 +00003705 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003706 if (!status) {
3707 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003708 status = nfs_post_op_update_inode(inode, res.fattr);
3709 if (!status)
3710 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003711 }
David Quigley14c43f72013-05-22 12:50:43 -04003712
3713
3714 nfs4_label_free(res.label);
3715
Trond Myklebust136f2622010-04-16 16:22:49 -04003716out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003717 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 return status;
3719}
3720
3721static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3722{
3723 struct nfs4_exception exception = { };
3724 int err;
3725 do {
3726 err = nfs4_handle_exception(NFS_SERVER(inode),
3727 _nfs4_proc_link(inode, dir, name),
3728 &exception);
3729 } while (exception.retry);
3730 return err;
3731}
3732
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003733struct nfs4_createdata {
3734 struct rpc_message msg;
3735 struct nfs4_create_arg arg;
3736 struct nfs4_create_res res;
3737 struct nfs_fh fh;
3738 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003739 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003740};
3741
3742static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3743 struct qstr *name, struct iattr *sattr, u32 ftype)
3744{
3745 struct nfs4_createdata *data;
3746
3747 data = kzalloc(sizeof(*data), GFP_KERNEL);
3748 if (data != NULL) {
3749 struct nfs_server *server = NFS_SERVER(dir);
3750
David Quigley14c43f72013-05-22 12:50:43 -04003751 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3752 if (IS_ERR(data->label))
3753 goto out_free;
3754
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003755 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3756 data->msg.rpc_argp = &data->arg;
3757 data->msg.rpc_resp = &data->res;
3758 data->arg.dir_fh = NFS_FH(dir);
3759 data->arg.server = server;
3760 data->arg.name = name;
3761 data->arg.attrs = sattr;
3762 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003763 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003764 data->res.server = server;
3765 data->res.fh = &data->fh;
3766 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003767 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003768 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003769 }
3770 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003771out_free:
3772 kfree(data);
3773 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003774}
3775
3776static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3777{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003778 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003779 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003780 if (status == 0) {
3781 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003782 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003783 }
3784 return status;
3785}
3786
3787static void nfs4_free_createdata(struct nfs4_createdata *data)
3788{
David Quigley14c43f72013-05-22 12:50:43 -04003789 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003790 kfree(data);
3791}
3792
Chuck Lever4f390c12006-08-22 20:06:22 -04003793static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003794 struct page *page, unsigned int len, struct iattr *sattr,
3795 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003797 struct nfs4_createdata *data;
3798 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Chuck Lever94a6d752006-08-22 20:06:23 -04003800 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003801 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003802
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003803 status = -ENOMEM;
3804 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3805 if (data == NULL)
3806 goto out;
3807
3808 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3809 data->arg.u.symlink.pages = &page;
3810 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003811 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003813 status = nfs4_do_create(dir, dentry, data);
3814
3815 nfs4_free_createdata(data);
3816out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 return status;
3818}
3819
Chuck Lever4f390c12006-08-22 20:06:22 -04003820static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003821 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822{
3823 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003824 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003826
3827 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003830 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3831 trace_nfs4_symlink(dir, &dentry->d_name, err);
3832 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 &exception);
3834 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003835
3836 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 return err;
3838}
3839
3840static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003841 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003843 struct nfs4_createdata *data;
3844 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003846 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3847 if (data == NULL)
3848 goto out;
3849
David Quigley1775fd32013-05-22 12:50:42 -04003850 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003851 status = nfs4_do_create(dir, dentry, data);
3852
3853 nfs4_free_createdata(data);
3854out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 return status;
3856}
3857
3858static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3859 struct iattr *sattr)
3860{
3861 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003862 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003864
David Quigleyaa9c2662013-05-22 12:50:44 -04003865 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3866
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003867 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003869 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3870 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3871 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 &exception);
3873 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003874 nfs4_label_release_security(label);
3875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 return err;
3877}
3878
3879static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003880 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
David Howells2b0143b2015-03-17 22:25:59 +00003882 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 struct nfs4_readdir_arg args = {
3884 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003885 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 .pgbase = 0,
3887 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00003888 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003889 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 };
3891 struct nfs4_readdir_res res;
3892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3894 .rpc_argp = &args,
3895 .rpc_resp = &res,
3896 .rpc_cred = cred,
3897 };
3898 int status;
3899
Al Viro6de14722013-09-16 10:53:17 -04003900 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3901 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003902 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003903 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003905 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 -05003906 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003907 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003908 status += args.pgbase;
3909 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003910
3911 nfs_invalidate_atime(dir);
3912
Harvey Harrison3110ff82008-05-02 13:42:44 -07003913 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 return status;
3915}
3916
3917static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003918 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919{
3920 struct nfs4_exception exception = { };
3921 int err;
3922 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003923 err = _nfs4_proc_readdir(dentry, cred, cookie,
3924 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00003925 trace_nfs4_readdir(d_inode(dentry), err);
3926 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 &exception);
3928 } while (exception.retry);
3929 return err;
3930}
3931
3932static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003933 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003935 struct nfs4_createdata *data;
3936 int mode = sattr->ia_mode;
3937 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003939 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3940 if (data == NULL)
3941 goto out;
3942
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003944 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003946 data->arg.ftype = NF4BLK;
3947 data->arg.u.device.specdata1 = MAJOR(rdev);
3948 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 }
3950 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003951 data->arg.ftype = NF4CHR;
3952 data->arg.u.device.specdata1 = MAJOR(rdev);
3953 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003954 } else if (!S_ISSOCK(mode)) {
3955 status = -EINVAL;
3956 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 }
David Quigley1775fd32013-05-22 12:50:42 -04003958
David Quigleyaa9c2662013-05-22 12:50:44 -04003959 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003960 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003961out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003962 nfs4_free_createdata(data);
3963out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 return status;
3965}
3966
3967static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3968 struct iattr *sattr, dev_t rdev)
3969{
3970 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003971 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003973
David Quigleyaa9c2662013-05-22 12:50:44 -04003974 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3975
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003976 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003978 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3979 trace_nfs4_mknod(dir, &dentry->d_name, err);
3980 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 &exception);
3982 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003983
3984 nfs4_label_release_security(label);
3985
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 return err;
3987}
3988
3989static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3990 struct nfs_fsstat *fsstat)
3991{
3992 struct nfs4_statfs_arg args = {
3993 .fh = fhandle,
3994 .bitmask = server->attr_bitmask,
3995 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003996 struct nfs4_statfs_res res = {
3997 .fsstat = fsstat,
3998 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 struct rpc_message msg = {
4000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4001 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004002 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 };
4004
Trond Myklebust0e574af2005-10-27 22:12:38 -04004005 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004006 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
4009static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4010{
4011 struct nfs4_exception exception = { };
4012 int err;
4013 do {
4014 err = nfs4_handle_exception(server,
4015 _nfs4_proc_statfs(server, fhandle, fsstat),
4016 &exception);
4017 } while (exception.retry);
4018 return err;
4019}
4020
4021static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4022 struct nfs_fsinfo *fsinfo)
4023{
4024 struct nfs4_fsinfo_arg args = {
4025 .fh = fhandle,
4026 .bitmask = server->attr_bitmask,
4027 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004028 struct nfs4_fsinfo_res res = {
4029 .fsinfo = fsinfo,
4030 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 struct rpc_message msg = {
4032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4033 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004034 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 };
4036
Bryan Schumaker7c513052011-03-24 17:12:24 +00004037 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
4040static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4041{
4042 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004043 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 int err;
4045
4046 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004047 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004048 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004049 if (err == 0) {
4050 struct nfs_client *clp = server->nfs_client;
4051
4052 spin_lock(&clp->cl_lock);
4053 clp->cl_lease_time = fsinfo->lease_time * HZ;
4054 clp->cl_last_renewal = now;
4055 spin_unlock(&clp->cl_lock);
4056 break;
4057 }
4058 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 } while (exception.retry);
4060 return err;
4061}
4062
4063static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4064{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004065 int error;
4066
Trond Myklebust0e574af2005-10-27 22:12:38 -04004067 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004068 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004069 if (error == 0) {
4070 /* block layout checks this! */
4071 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004072 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004073 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004074
4075 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076}
4077
4078static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4079 struct nfs_pathconf *pathconf)
4080{
4081 struct nfs4_pathconf_arg args = {
4082 .fh = fhandle,
4083 .bitmask = server->attr_bitmask,
4084 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004085 struct nfs4_pathconf_res res = {
4086 .pathconf = pathconf,
4087 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 struct rpc_message msg = {
4089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4090 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004091 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 };
4093
4094 /* None of the pathconf attributes are mandatory to implement */
4095 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4096 memset(pathconf, 0, sizeof(*pathconf));
4097 return 0;
4098 }
4099
Trond Myklebust0e574af2005-10-27 22:12:38 -04004100 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004101 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
4103
4104static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4105 struct nfs_pathconf *pathconf)
4106{
4107 struct nfs4_exception exception = { };
4108 int err;
4109
4110 do {
4111 err = nfs4_handle_exception(server,
4112 _nfs4_proc_pathconf(server, fhandle, pathconf),
4113 &exception);
4114 } while (exception.retry);
4115 return err;
4116}
4117
Trond Myklebust5521abf2013-03-16 20:54:34 -04004118int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004119 const struct nfs_open_context *ctx,
4120 const struct nfs_lock_context *l_ctx,
4121 fmode_t fmode)
4122{
4123 const struct nfs_lockowner *lockowner = NULL;
4124
4125 if (l_ctx != NULL)
4126 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004127 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004128}
4129EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4130
Trond Myklebust5521abf2013-03-16 20:54:34 -04004131static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4132 const struct nfs_open_context *ctx,
4133 const struct nfs_lock_context *l_ctx,
4134 fmode_t fmode)
4135{
4136 nfs4_stateid current_stateid;
4137
Trond Myklebuste1253be2014-03-05 08:44:23 -05004138 /* If the current stateid represents a lost lock, then exit */
4139 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4140 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004141 return nfs4_stateid_match(stateid, &current_stateid);
4142}
4143
4144static bool nfs4_error_stateid_expired(int err)
4145{
4146 switch (err) {
4147 case -NFS4ERR_DELEG_REVOKED:
4148 case -NFS4ERR_ADMIN_REVOKED:
4149 case -NFS4ERR_BAD_STATEID:
4150 case -NFS4ERR_STALE_STATEID:
4151 case -NFS4ERR_OLD_STATEID:
4152 case -NFS4ERR_OPENMODE:
4153 case -NFS4ERR_EXPIRED:
4154 return true;
4155 }
4156 return false;
4157}
4158
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004159void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004160{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004161 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004162}
4163
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004164static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004166 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004168 trace_nfs4_read(hdr, task->tk_status);
4169 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004170 hdr->args.context->state,
4171 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004172 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004173 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004175
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004176 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004178 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180}
4181
Trond Myklebust5521abf2013-03-16 20:54:34 -04004182static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004183 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004184{
4185
4186 if (!nfs4_error_stateid_expired(task->tk_status) ||
4187 nfs4_stateid_is_current(&args->stateid,
4188 args->context,
4189 args->lock_context,
4190 FMODE_READ))
4191 return false;
4192 rpc_restart_call_prepare(task);
4193 return true;
4194}
4195
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004196static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004197{
4198
4199 dprintk("--> %s\n", __func__);
4200
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004201 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004202 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004203 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004204 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004205 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4206 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004207}
4208
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004209static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4210 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004212 hdr->timestamp = jiffies;
4213 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004214 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004215 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
4217
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004218static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4219 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004220{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004221 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4222 &hdr->args.seq_args,
4223 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004224 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004225 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004226 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4227 hdr->args.lock_context,
4228 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004229 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004230 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004231 return -EIO;
4232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233}
4234
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004235static int nfs4_write_done_cb(struct rpc_task *task,
4236 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004238 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004239
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004240 trace_nfs4_write(hdr, task->tk_status);
4241 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004242 hdr->args.context->state,
4243 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004244 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004245 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004247 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004248 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004249 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004250 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
Trond Myklebust5521abf2013-03-16 20:54:34 -04004254static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004255 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004256{
4257
4258 if (!nfs4_error_stateid_expired(task->tk_status) ||
4259 nfs4_stateid_is_current(&args->stateid,
4260 args->context,
4261 args->lock_context,
4262 FMODE_WRITE))
4263 return false;
4264 rpc_restart_call_prepare(task);
4265 return true;
4266}
4267
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004268static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004269{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004270 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004271 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004272 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004273 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004274 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4275 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004276}
4277
Trond Myklebust5a37f852012-04-28 14:55:16 -04004278static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004279bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004280{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004281 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004282 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004283 return false;
4284 /* Otherwise, request attributes if and only if we don't hold
4285 * a delegation
4286 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004287 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004288}
Fred Isamana69aef12011-03-03 15:13:47 +00004289
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004290static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4291 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004293 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004294
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004295 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4296 hdr->args.bitmask = NULL;
4297 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004298 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004299 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004300
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004301 if (!hdr->pgio_done_cb)
4302 hdr->pgio_done_cb = nfs4_write_done_cb;
4303 hdr->res.server = server;
4304 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004306 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004307 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
Fred Isaman0b7c0152012-04-20 14:47:39 -04004310static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4311{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004312 nfs4_setup_sequence(NFS_SERVER(data->inode),
4313 &data->args.seq_args,
4314 &data->res.seq_res,
4315 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
Fred Isaman0b7c0152012-04-20 14:47:39 -04004318static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004321
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004322 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004323 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4324 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004325 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004326 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329}
4330
Fred Isaman0b7c0152012-04-20 14:47:39 -04004331static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004332{
4333 if (!nfs4_sequence_done(task, &data->res.seq_res))
4334 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004335 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004336}
4337
Fred Isaman0b7c0152012-04-20 14:47:39 -04004338static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004340 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004341
Fred Isaman0b7c0152012-04-20 14:47:39 -04004342 if (data->commit_done_cb == NULL)
4343 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004344 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004345 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004346 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347}
4348
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004349struct nfs4_renewdata {
4350 struct nfs_client *client;
4351 unsigned long timestamp;
4352};
4353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354/*
4355 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4356 * standalone procedure for queueing an asynchronous RENEW.
4357 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004358static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004359{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004360 struct nfs4_renewdata *data = calldata;
4361 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004362
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004363 if (atomic_read(&clp->cl_count) > 1)
4364 nfs4_schedule_state_renewal(clp);
4365 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004366 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004367}
4368
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004369static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004371 struct nfs4_renewdata *data = calldata;
4372 struct nfs_client *clp = data->client;
4373 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004375 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004376 switch (task->tk_status) {
4377 case 0:
4378 break;
4379 case -NFS4ERR_LEASE_MOVED:
4380 nfs4_schedule_lease_moved_recovery(clp);
4381 break;
4382 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004383 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004384 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4385 return;
4386 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004387 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004388 return;
4389 }
4390 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004392 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393}
4394
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004395static const struct rpc_call_ops nfs4_renew_ops = {
4396 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004397 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004398};
4399
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004400static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401{
4402 struct rpc_message msg = {
4403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4404 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004405 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004407 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004409 if (renew_flags == 0)
4410 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004411 if (!atomic_inc_not_zero(&clp->cl_count))
4412 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004413 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004414 if (data == NULL)
4415 return -ENOMEM;
4416 data->client = clp;
4417 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004418 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004419 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420}
4421
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004422static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
4424 struct rpc_message msg = {
4425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4426 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004427 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 };
4429 unsigned long now = jiffies;
4430 int status;
4431
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004432 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 if (status < 0)
4434 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004435 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return 0;
4437}
4438
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004439static inline int nfs4_server_supports_acls(struct nfs_server *server)
4440{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004441 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004442}
4443
Trond Myklebust21f498c2012-08-24 10:59:25 -04004444/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4445 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004446 * the stack.
4447 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004448#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004449
Neil Hormane9e3d722011-03-04 19:26:03 -05004450static int buf_to_pages_noslab(const void *buf, size_t buflen,
4451 struct page **pages, unsigned int *pgbase)
4452{
4453 struct page *newpage, **spages;
4454 int rc = 0;
4455 size_t len;
4456 spages = pages;
4457
4458 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004459 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004460 newpage = alloc_page(GFP_KERNEL);
4461
4462 if (newpage == NULL)
4463 goto unwind;
4464 memcpy(page_address(newpage), buf, len);
4465 buf += len;
4466 buflen -= len;
4467 *pages++ = newpage;
4468 rc++;
4469 } while (buflen != 0);
4470
4471 return rc;
4472
4473unwind:
4474 for(; rc > 0; rc--)
4475 __free_page(spages[rc-1]);
4476 return -ENOMEM;
4477}
4478
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004479struct nfs4_cached_acl {
4480 int cached;
4481 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004482 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004483};
4484
4485static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004486{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004487 struct nfs_inode *nfsi = NFS_I(inode);
4488
4489 spin_lock(&inode->i_lock);
4490 kfree(nfsi->nfs4_acl);
4491 nfsi->nfs4_acl = acl;
4492 spin_unlock(&inode->i_lock);
4493}
4494
4495static void nfs4_zap_acl_attr(struct inode *inode)
4496{
4497 nfs4_set_cached_acl(inode, NULL);
4498}
4499
4500static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4501{
4502 struct nfs_inode *nfsi = NFS_I(inode);
4503 struct nfs4_cached_acl *acl;
4504 int ret = -ENOENT;
4505
4506 spin_lock(&inode->i_lock);
4507 acl = nfsi->nfs4_acl;
4508 if (acl == NULL)
4509 goto out;
4510 if (buf == NULL) /* user is just asking for length */
4511 goto out_len;
4512 if (acl->cached == 0)
4513 goto out;
4514 ret = -ERANGE; /* see getxattr(2) man page */
4515 if (acl->len > buflen)
4516 goto out;
4517 memcpy(buf, acl->data, acl->len);
4518out_len:
4519 ret = acl->len;
4520out:
4521 spin_unlock(&inode->i_lock);
4522 return ret;
4523}
4524
Sachin Prabhu5794d212012-04-17 14:36:40 +01004525static 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 +00004526{
4527 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004528 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004529
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004530 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004531 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004532 if (acl == NULL)
4533 goto out;
4534 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004535 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004536 } else {
4537 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4538 if (acl == NULL)
4539 goto out;
4540 acl->cached = 0;
4541 }
4542 acl->len = acl_len;
4543out:
4544 nfs4_set_cached_acl(inode, acl);
4545}
4546
Andy Adamsonbf118a32011-12-07 11:55:27 -05004547/*
4548 * The getxattr API returns the required buffer length when called with a
4549 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4550 * the required buf. On a NULL buf, we send a page of data to the server
4551 * guessing that the ACL request can be serviced by a page. If so, we cache
4552 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4553 * the cache. If not so, we throw away the page, and cache the required
4554 * length. The next getxattr call will then produce another round trip to
4555 * the server, this time with the input buf of the required size.
4556 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004557static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004558{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004559 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004560 struct nfs_getaclargs args = {
4561 .fh = NFS_FH(inode),
4562 .acl_pages = pages,
4563 .acl_len = buflen,
4564 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004565 struct nfs_getaclres res = {
4566 .acl_len = buflen,
4567 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004568 struct rpc_message msg = {
4569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4570 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004571 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004572 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004573 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4574 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004575
Andy Adamsonbf118a32011-12-07 11:55:27 -05004576 /* As long as we're doing a round trip to the server anyway,
4577 * let's be prepared for a page of acl data. */
4578 if (npages == 0)
4579 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004580 if (npages > ARRAY_SIZE(pages))
4581 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004582
Andy Adamsonbf118a32011-12-07 11:55:27 -05004583 for (i = 0; i < npages; i++) {
4584 pages[i] = alloc_page(GFP_KERNEL);
4585 if (!pages[i])
4586 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004587 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004588
4589 /* for decoding across pages */
4590 res.acl_scratch = alloc_page(GFP_KERNEL);
4591 if (!res.acl_scratch)
4592 goto out_free;
4593
Andy Adamsonbf118a32011-12-07 11:55:27 -05004594 args.acl_len = npages * PAGE_SIZE;
4595 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004596
Peng Taode040be2012-01-10 22:42:47 +08004597 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004598 __func__, buf, buflen, npages, args.acl_len);
4599 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4600 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004601 if (ret)
4602 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004603
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004604 /* Handle the case where the passed-in buffer is too short */
4605 if (res.acl_flags & NFS4_ACL_TRUNC) {
4606 /* Did the user only issue a request for the acl length? */
4607 if (buf == NULL)
4608 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004609 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004610 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004611 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004612 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004613 if (buf) {
4614 if (res.acl_len > buflen) {
4615 ret = -ERANGE;
4616 goto out_free;
4617 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004618 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004619 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004620out_ok:
4621 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004622out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004623 for (i = 0; i < npages; i++)
4624 if (pages[i])
4625 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004626 if (res.acl_scratch)
4627 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004628 return ret;
4629}
4630
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004631static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4632{
4633 struct nfs4_exception exception = { };
4634 ssize_t ret;
4635 do {
4636 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004637 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004638 if (ret >= 0)
4639 break;
4640 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4641 } while (exception.retry);
4642 return ret;
4643}
4644
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004645static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4646{
4647 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004648 int ret;
4649
4650 if (!nfs4_server_supports_acls(server))
4651 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004652 ret = nfs_revalidate_inode(server, inode);
4653 if (ret < 0)
4654 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004655 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4656 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004657 ret = nfs4_read_cached_acl(inode, buf, buflen);
4658 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004659 /* -ENOENT is returned if there is no ACL or if there is an ACL
4660 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004661 return ret;
4662 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004663}
4664
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004665static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004666{
4667 struct nfs_server *server = NFS_SERVER(inode);
4668 struct page *pages[NFS4ACL_MAXPAGES];
4669 struct nfs_setaclargs arg = {
4670 .fh = NFS_FH(inode),
4671 .acl_pages = pages,
4672 .acl_len = buflen,
4673 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004674 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004675 struct rpc_message msg = {
4676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4677 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004678 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004679 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004680 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004681 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004682
4683 if (!nfs4_server_supports_acls(server))
4684 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004685 if (npages > ARRAY_SIZE(pages))
4686 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004687 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4688 if (i < 0)
4689 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004690 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004691 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004692
4693 /*
4694 * Free each page after tx, so the only ref left is
4695 * held by the network stack
4696 */
4697 for (; i > 0; i--)
4698 put_page(pages[i-1]);
4699
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004700 /*
4701 * Acl update can result in inode attribute update.
4702 * so mark the attribute cache invalid.
4703 */
4704 spin_lock(&inode->i_lock);
4705 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4706 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004707 nfs_access_zap_cache(inode);
4708 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004709 return ret;
4710}
4711
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004712static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4713{
4714 struct nfs4_exception exception = { };
4715 int err;
4716 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004717 err = __nfs4_proc_set_acl(inode, buf, buflen);
4718 trace_nfs4_set_acl(inode, err);
4719 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004720 &exception);
4721 } while (exception.retry);
4722 return err;
4723}
4724
David Quigleyaa9c2662013-05-22 12:50:44 -04004725#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4726static int _nfs4_get_security_label(struct inode *inode, void *buf,
4727 size_t buflen)
4728{
4729 struct nfs_server *server = NFS_SERVER(inode);
4730 struct nfs_fattr fattr;
4731 struct nfs4_label label = {0, 0, buflen, buf};
4732
4733 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004734 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004735 .fh = NFS_FH(inode),
4736 .bitmask = bitmask,
4737 };
4738 struct nfs4_getattr_res res = {
4739 .fattr = &fattr,
4740 .label = &label,
4741 .server = server,
4742 };
4743 struct rpc_message msg = {
4744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004745 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004746 .rpc_resp = &res,
4747 };
4748 int ret;
4749
4750 nfs_fattr_init(&fattr);
4751
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004752 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004753 if (ret)
4754 return ret;
4755 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4756 return -ENOENT;
4757 if (buflen < label.len)
4758 return -ERANGE;
4759 return 0;
4760}
4761
4762static int nfs4_get_security_label(struct inode *inode, void *buf,
4763 size_t buflen)
4764{
4765 struct nfs4_exception exception = { };
4766 int err;
4767
4768 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4769 return -EOPNOTSUPP;
4770
4771 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004772 err = _nfs4_get_security_label(inode, buf, buflen);
4773 trace_nfs4_get_security_label(inode, err);
4774 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004775 &exception);
4776 } while (exception.retry);
4777 return err;
4778}
4779
4780static int _nfs4_do_set_security_label(struct inode *inode,
4781 struct nfs4_label *ilabel,
4782 struct nfs_fattr *fattr,
4783 struct nfs4_label *olabel)
4784{
4785
4786 struct iattr sattr = {0};
4787 struct nfs_server *server = NFS_SERVER(inode);
4788 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004789 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004790 .fh = NFS_FH(inode),
4791 .iap = &sattr,
4792 .server = server,
4793 .bitmask = bitmask,
4794 .label = ilabel,
4795 };
4796 struct nfs_setattrres res = {
4797 .fattr = fattr,
4798 .label = olabel,
4799 .server = server,
4800 };
4801 struct rpc_message msg = {
4802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004803 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004804 .rpc_resp = &res,
4805 };
4806 int status;
4807
Jeff Layton12207f62013-11-01 10:49:32 -04004808 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004809
Jeff Layton12207f62013-11-01 10:49:32 -04004810 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004811 if (status)
4812 dprintk("%s failed: %d\n", __func__, status);
4813
4814 return status;
4815}
4816
4817static int nfs4_do_set_security_label(struct inode *inode,
4818 struct nfs4_label *ilabel,
4819 struct nfs_fattr *fattr,
4820 struct nfs4_label *olabel)
4821{
4822 struct nfs4_exception exception = { };
4823 int err;
4824
4825 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004826 err = _nfs4_do_set_security_label(inode, ilabel,
4827 fattr, olabel);
4828 trace_nfs4_set_security_label(inode, err);
4829 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004830 &exception);
4831 } while (exception.retry);
4832 return err;
4833}
4834
4835static int
4836nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4837{
4838 struct nfs4_label ilabel, *olabel = NULL;
4839 struct nfs_fattr fattr;
4840 struct rpc_cred *cred;
David Howells2b0143b2015-03-17 22:25:59 +00004841 struct inode *inode = d_inode(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004842 int status;
4843
4844 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4845 return -EOPNOTSUPP;
4846
4847 nfs_fattr_init(&fattr);
4848
4849 ilabel.pi = 0;
4850 ilabel.lfs = 0;
4851 ilabel.label = (char *)buf;
4852 ilabel.len = buflen;
4853
4854 cred = rpc_lookup_cred();
4855 if (IS_ERR(cred))
4856 return PTR_ERR(cred);
4857
4858 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4859 if (IS_ERR(olabel)) {
4860 status = -PTR_ERR(olabel);
4861 goto out;
4862 }
4863
4864 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4865 if (status == 0)
4866 nfs_setsecurity(inode, &fattr, olabel);
4867
4868 nfs4_label_free(olabel);
4869out:
4870 put_rpccred(cred);
4871 return status;
4872}
4873#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4874
4875
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004877nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4878 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004880 struct nfs_client *clp = server->nfs_client;
4881
4882 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 return 0;
4884 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004885 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004886 case -NFS4ERR_ADMIN_REVOKED:
4887 case -NFS4ERR_BAD_STATEID:
4888 case -NFS4ERR_OPENMODE:
4889 if (state == NULL)
4890 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004891 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004892 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004893 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004894 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004895 if (state != NULL) {
4896 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004897 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004900 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004901 nfs4_schedule_lease_recovery(clp);
4902 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004903 case -NFS4ERR_MOVED:
4904 if (nfs4_schedule_migration_recovery(server) < 0)
4905 goto recovery_failed;
4906 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004907 case -NFS4ERR_LEASE_MOVED:
4908 nfs4_schedule_lease_moved_recovery(clp);
4909 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004910#if defined(CONFIG_NFS_V4_1)
4911 case -NFS4ERR_BADSESSION:
4912 case -NFS4ERR_BADSLOT:
4913 case -NFS4ERR_BAD_HIGH_SLOT:
4914 case -NFS4ERR_DEADSESSION:
4915 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4916 case -NFS4ERR_SEQ_FALSE_RETRY:
4917 case -NFS4ERR_SEQ_MISORDERED:
4918 dprintk("%s ERROR %d, Reset session\n", __func__,
4919 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004920 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004921 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004922#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004924 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10004925 rpc_delay(task, nfs4_update_delay(timeout));
4926 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05004927 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004929 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004931 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 }
4933 task->tk_status = nfs4_map_errors(task->tk_status);
4934 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004935recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004936 task->tk_status = -EIO;
4937 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004938wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004939 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004940 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4941 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004942 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4943 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004944restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004945 task->tk_status = 0;
4946 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947}
4948
Chuck Leverf0920752012-05-21 22:45:41 -04004949static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4950 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004951{
4952 __be32 verf[2];
4953
Chuck Lever2c820d92012-05-21 22:45:33 -04004954 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4955 /* An impossible timestamp guarantees this value
4956 * will never match a generated boot time. */
4957 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004958 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004959 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004960 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004961 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4962 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004963 }
Chuck Levercd937102012-03-02 17:14:31 -05004964 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4965}
4966
Jeff Laytona3192682015-06-09 19:43:59 -04004967static int
4968nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04004969{
Jeff Laytona3192682015-06-09 19:43:59 -04004970 int result;
4971 size_t len;
4972 char *str;
4973 bool retried = false;
Chuck Levere984a552012-09-14 17:24:21 -04004974
Trond Myklebustceb3a162015-01-03 15:16:04 -05004975 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04004976 return 0;
4977retry:
4978 rcu_read_lock();
4979 len = 10 + strlen(clp->cl_ipaddr) + 1 +
4980 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
4981 1 +
4982 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
4983 1;
4984 rcu_read_unlock();
4985
4986 if (len > NFS4_OPAQUE_LIMIT + 1)
4987 return -EINVAL;
4988
4989 /*
4990 * Since this string is allocated at mount time, and held until the
4991 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
4992 * about a memory-reclaim deadlock.
4993 */
4994 str = kmalloc(len, GFP_KERNEL);
4995 if (!str)
4996 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05004997
Chuck Levere984a552012-09-14 17:24:21 -04004998 rcu_read_lock();
Jeff Laytona3192682015-06-09 19:43:59 -04004999 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5000 clp->cl_ipaddr,
5001 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5002 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005003 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005004
5005 /* Did something change? */
5006 if (result >= len) {
5007 kfree(str);
5008 if (retried)
5009 return -EINVAL;
5010 retried = true;
5011 goto retry;
5012 }
5013 clp->cl_owner_id = str;
5014 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005015}
5016
Jeff Layton873e3852015-06-09 19:44:00 -04005017static int
5018nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005019{
Jeff Layton873e3852015-06-09 19:44:00 -04005020 int result;
5021 size_t len;
5022 char *str;
5023
5024 len = 10 + 10 + 1 + 10 + 1 +
5025 strlen(nfs4_client_id_uniquifier) + 1 +
5026 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5027
5028 if (len > NFS4_OPAQUE_LIMIT + 1)
5029 return -EINVAL;
5030
5031 /*
5032 * Since this string is allocated at mount time, and held until the
5033 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5034 * about a memory-reclaim deadlock.
5035 */
5036 str = kmalloc(len, GFP_KERNEL);
5037 if (!str)
5038 return -ENOMEM;
5039
5040 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5041 clp->rpc_ops->version, clp->cl_minorversion,
5042 nfs4_client_id_uniquifier,
5043 clp->cl_rpcclient->cl_nodename);
5044 if (result >= len) {
5045 kfree(str);
5046 return -EINVAL;
5047 }
5048 clp->cl_owner_id = str;
5049 return 0;
5050}
5051
5052static int
5053nfs4_init_uniform_client_string(struct nfs_client *clp)
5054{
5055 int result;
5056 size_t len;
5057 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005058
5059 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005060 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005061
5062 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005063 return nfs4_init_uniquifier_client_string(clp);
5064
5065 len = 10 + 10 + 1 + 10 + 1 +
5066 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5067
5068 if (len > NFS4_OPAQUE_LIMIT + 1)
5069 return -EINVAL;
5070
5071 /*
5072 * Since this string is allocated at mount time, and held until the
5073 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5074 * about a memory-reclaim deadlock.
5075 */
5076 str = kmalloc(len, GFP_KERNEL);
5077 if (!str)
5078 return -ENOMEM;
5079
5080 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5081 clp->rpc_ops->version, clp->cl_minorversion,
5082 clp->cl_rpcclient->cl_nodename);
5083 if (result >= len) {
5084 kfree(str);
5085 return -EINVAL;
5086 }
5087 clp->cl_owner_id = str;
5088 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005089}
5090
Chuck Lever706cb8d2014-03-12 12:51:47 -04005091/*
5092 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5093 * services. Advertise one based on the address family of the
5094 * clientaddr.
5095 */
5096static unsigned int
5097nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5098{
5099 if (strchr(clp->cl_ipaddr, ':') != NULL)
5100 return scnprintf(buf, len, "tcp6");
5101 else
5102 return scnprintf(buf, len, "tcp");
5103}
5104
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005105static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5106{
5107 struct nfs4_setclientid *sc = calldata;
5108
5109 if (task->tk_status == 0)
5110 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5111}
5112
5113static const struct rpc_call_ops nfs4_setclientid_ops = {
5114 .rpc_call_done = nfs4_setclientid_done,
5115};
5116
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005117/**
5118 * nfs4_proc_setclientid - Negotiate client ID
5119 * @clp: state data structure
5120 * @program: RPC program for NFSv4 callback service
5121 * @port: IP port number for NFS4 callback service
5122 * @cred: RPC credential to use for this call
5123 * @res: where to place the result
5124 *
5125 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5126 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005127int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5128 unsigned short port, struct rpc_cred *cred,
5129 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130{
5131 nfs4_verifier sc_verifier;
5132 struct nfs4_setclientid setclientid = {
5133 .sc_verifier = &sc_verifier,
5134 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005135 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 };
5137 struct rpc_message msg = {
5138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5139 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005140 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005141 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005143 struct rpc_task *task;
5144 struct rpc_task_setup task_setup_data = {
5145 .rpc_client = clp->cl_rpcclient,
5146 .rpc_message = &msg,
5147 .callback_ops = &nfs4_setclientid_ops,
5148 .callback_data = &setclientid,
5149 .flags = RPC_TASK_TIMEOUT,
5150 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005151 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152
Chuck Leverde734832012-07-11 16:30:50 -04005153 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005154 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005155
5156 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5157 status = nfs4_init_uniform_client_string(clp);
5158 else
Jeff Laytona3192682015-06-09 19:43:59 -04005159 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005160
5161 if (status)
5162 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005163
Chuck Leverde734832012-07-11 16:30:50 -04005164 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005165 setclientid.sc_netid_len =
5166 nfs4_init_callback_netid(clp,
5167 setclientid.sc_netid,
5168 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005169 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005170 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 clp->cl_ipaddr, port >> 8, port & 255);
5172
Jeff Layton3a6bb732015-06-09 19:43:57 -04005173 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005174 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005175 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005176 task = rpc_run_task(&task_setup_data);
5177 if (IS_ERR(task)) {
5178 status = PTR_ERR(task);
5179 goto out;
5180 }
5181 status = task->tk_status;
5182 if (setclientid.sc_cred) {
5183 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5184 put_rpccred(setclientid.sc_cred);
5185 }
5186 rpc_put_task(task);
5187out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005188 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005189 dprintk("NFS reply setclientid: %d\n", status);
5190 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191}
5192
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005193/**
5194 * nfs4_proc_setclientid_confirm - Confirm client ID
5195 * @clp: state data structure
5196 * @res: result of a previous SETCLIENTID
5197 * @cred: RPC credential to use for this call
5198 *
5199 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5200 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005201int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005202 struct nfs4_setclientid_res *arg,
5203 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 struct rpc_message msg = {
5206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005207 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005208 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 int status;
5211
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005212 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5213 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5214 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005215 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005216 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005217 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 return status;
5219}
5220
Trond Myklebustfe650402006-01-03 09:55:18 +01005221struct nfs4_delegreturndata {
5222 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005223 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005224 struct nfs_fh fh;
5225 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005226 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005227 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005228 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005229 struct inode *inode;
5230 bool roc;
5231 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005232};
5233
Trond Myklebustfe650402006-01-03 09:55:18 +01005234static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5235{
5236 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005237
Trond Myklebust14516c32010-07-31 14:29:06 -04005238 if (!nfs4_sequence_done(task, &data->res.seq_res))
5239 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005240
Trond Myklebustca8acf82013-08-13 10:36:56 -04005241 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005242 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005243 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005244 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005245 case -NFS4ERR_ADMIN_REVOKED:
5246 case -NFS4ERR_DELEG_REVOKED:
5247 case -NFS4ERR_BAD_STATEID:
5248 case -NFS4ERR_OLD_STATEID:
5249 case -NFS4ERR_STALE_STATEID:
5250 case -NFS4ERR_EXPIRED:
5251 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005252 if (data->roc)
5253 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005254 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005255 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005256 if (nfs4_async_handle_error(task, data->res.server,
5257 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005258 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005259 return;
5260 }
5261 }
5262 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005263}
5264
5265static void nfs4_delegreturn_release(void *calldata)
5266{
Peng Tao039b7562014-07-03 13:05:02 +08005267 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005268 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005269
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005270 if (inode) {
5271 if (data->roc)
5272 pnfs_roc_release(inode);
5273 nfs_iput_and_deactive(inode);
5274 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005275 kfree(calldata);
5276}
5277
Andy Adamson938e1012009-04-01 09:22:28 -04005278static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5279{
5280 struct nfs4_delegreturndata *d_data;
5281
5282 d_data = (struct nfs4_delegreturndata *)data;
5283
Peng Tao039b7562014-07-03 13:05:02 +08005284 if (d_data->roc &&
5285 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5286 return;
5287
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005288 nfs4_setup_sequence(d_data->res.server,
5289 &d_data->args.seq_args,
5290 &d_data->res.seq_res,
5291 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005292}
Andy Adamson938e1012009-04-01 09:22:28 -04005293
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005294static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005295 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005296 .rpc_call_done = nfs4_delegreturn_done,
5297 .rpc_release = nfs4_delegreturn_release,
5298};
5299
Trond Myklebuste6f81072008-01-24 18:14:34 -05005300static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005301{
5302 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005303 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005304 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005305 struct rpc_message msg = {
5306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5307 .rpc_cred = cred,
5308 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005309 struct rpc_task_setup task_setup_data = {
5310 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005311 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005312 .callback_ops = &nfs4_delegreturn_ops,
5313 .flags = RPC_TASK_ASYNC,
5314 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005315 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005316
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005317 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005318 if (data == NULL)
5319 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005320 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005321 data->args.fhandle = &data->fh;
5322 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005323 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005324 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005325 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005326 data->res.fattr = &data->fattr;
5327 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005328 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005329 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005330 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005331 data->inode = nfs_igrab_and_active(inode);
5332 if (data->inode)
5333 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005334
Trond Myklebustc970aa82007-07-14 15:39:59 -04005335 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005336 msg.rpc_argp = &data->args;
5337 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005338 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005339 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005340 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005341 if (!issync)
5342 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005343 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005344 if (status != 0)
5345 goto out;
5346 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005347 if (status == 0)
5348 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5349 else
5350 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005351out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005352 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005353 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354}
5355
Trond Myklebuste6f81072008-01-24 18:14:34 -05005356int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357{
5358 struct nfs_server *server = NFS_SERVER(inode);
5359 struct nfs4_exception exception = { };
5360 int err;
5361 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005362 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005363 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 switch (err) {
5365 case -NFS4ERR_STALE_STATEID:
5366 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 case 0:
5368 return 0;
5369 }
5370 err = nfs4_handle_exception(server, err, &exception);
5371 } while (exception.retry);
5372 return err;
5373}
5374
5375#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5376#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5377
5378/*
5379 * sleep, with exponential backoff, and retry the LOCK operation.
5380 */
5381static unsigned long
5382nfs4_set_lock_task_retry(unsigned long timeout)
5383{
Colin Cross416ad3c2013-05-06 23:50:06 +00005384 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 timeout <<= 1;
5386 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5387 return NFS4_LOCK_MAXTIMEOUT;
5388 return timeout;
5389}
5390
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5392{
5393 struct inode *inode = state->inode;
5394 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005395 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005396 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005398 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005400 struct nfs_lockt_res res = {
5401 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 };
5403 struct rpc_message msg = {
5404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5405 .rpc_argp = &arg,
5406 .rpc_resp = &res,
5407 .rpc_cred = state->owner->so_cred,
5408 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 struct nfs4_lock_state *lsp;
5410 int status;
5411
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005412 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005413 status = nfs4_set_lock_state(state, request);
5414 if (status != 0)
5415 goto out;
5416 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005417 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005418 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005419 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005420 switch (status) {
5421 case 0:
5422 request->fl_type = F_UNLCK;
5423 break;
5424 case -NFS4ERR_DENIED:
5425 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005427 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005428 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005429out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 return status;
5431}
5432
5433static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5434{
5435 struct nfs4_exception exception = { };
5436 int err;
5437
5438 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005439 err = _nfs4_proc_getlk(state, cmd, request);
5440 trace_nfs4_get_lock(request, state, cmd, err);
5441 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 &exception);
5443 } while (exception.retry);
5444 return err;
5445}
5446
5447static int do_vfs_lock(struct file *file, struct file_lock *fl)
5448{
5449 int res = 0;
5450 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5451 case FL_POSIX:
5452 res = posix_lock_file_wait(file, fl);
5453 break;
5454 case FL_FLOCK:
5455 res = flock_lock_file_wait(file, fl);
5456 break;
5457 default:
5458 BUG();
5459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 return res;
5461}
5462
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005463struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005464 struct nfs_locku_args arg;
5465 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005466 struct nfs4_lock_state *lsp;
5467 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005468 struct file_lock fl;
5469 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005470 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005471};
5472
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005473static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5474 struct nfs_open_context *ctx,
5475 struct nfs4_lock_state *lsp,
5476 struct nfs_seqid *seqid)
5477{
5478 struct nfs4_unlockdata *p;
5479 struct inode *inode = lsp->ls_state->inode;
5480
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005481 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005482 if (p == NULL)
5483 return NULL;
5484 p->arg.fh = NFS_FH(inode);
5485 p->arg.fl = &p->fl;
5486 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005487 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005488 p->lsp = lsp;
5489 atomic_inc(&lsp->ls_count);
5490 /* Ensure we don't close file until we're done freeing locks! */
5491 p->ctx = get_nfs_open_context(ctx);
Jeff Laytondb2efec2015-06-30 14:12:30 -04005492 get_file(fl->fl_file);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005493 memcpy(&p->fl, fl, sizeof(p->fl));
5494 p->server = NFS_SERVER(inode);
5495 return p;
5496}
5497
Trond Myklebust06f814a2006-01-03 09:55:07 +01005498static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005499{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005500 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005501 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005502 nfs4_put_lock_state(calldata->lsp);
5503 put_nfs_open_context(calldata->ctx);
Jeff Laytondb2efec2015-06-30 14:12:30 -04005504 fput(calldata->fl.fl_file);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005505 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005506}
5507
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005508static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005509{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005510 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005511
Trond Myklebust14516c32010-07-31 14:29:06 -04005512 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5513 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005514 switch (task->tk_status) {
5515 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005516 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005517 do_vfs_lock(calldata->fl.fl_file, &calldata->fl);
5518 if (nfs4_update_lock_stateid(calldata->lsp,
5519 &calldata->res.stateid))
5520 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005521 case -NFS4ERR_BAD_STATEID:
5522 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005523 case -NFS4ERR_STALE_STATEID:
5524 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005525 if (!nfs4_stateid_match(&calldata->arg.stateid,
5526 &calldata->lsp->ls_stateid))
5527 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005528 break;
5529 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005530 if (nfs4_async_handle_error(task, calldata->server,
5531 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005532 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005533 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005534 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005535}
5536
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005537static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005538{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005539 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005541 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005542 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005543 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005544 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005545 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005546 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005547 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005548 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005549 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005550 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005551 &calldata->res.seq_res,
5552 task) != 0)
5553 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005554 return;
5555out_no_action:
5556 task->tk_action = NULL;
5557out_wait:
5558 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559}
5560
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005561static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005562 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005563 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005564 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005565};
5566
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005567static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5568 struct nfs_open_context *ctx,
5569 struct nfs4_lock_state *lsp,
5570 struct nfs_seqid *seqid)
5571{
5572 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005573 struct rpc_message msg = {
5574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5575 .rpc_cred = ctx->cred,
5576 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005577 struct rpc_task_setup task_setup_data = {
5578 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005579 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005580 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005581 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005582 .flags = RPC_TASK_ASYNC,
5583 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005584
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005585 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5586 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5587
Frank Filz137d6ac2007-07-09 15:32:29 -07005588 /* Ensure this is an unlock - when canceling a lock, the
5589 * canceled lock is passed in, and it won't be an unlock.
5590 */
5591 fl->fl_type = F_UNLCK;
5592
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005593 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5594 if (data == NULL) {
5595 nfs_free_seqid(seqid);
5596 return ERR_PTR(-ENOMEM);
5597 }
5598
Chuck Levera9c92d62013-08-09 12:48:18 -04005599 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005600 msg.rpc_argp = &data->arg;
5601 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005602 task_setup_data.callback_data = data;
5603 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005604}
5605
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5607{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005608 struct inode *inode = state->inode;
5609 struct nfs4_state_owner *sp = state->owner;
5610 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005611 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005612 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005613 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005614 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005615 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005616 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
Trond Myklebust9b073572006-06-29 16:38:34 -04005618 status = nfs4_set_lock_state(state, request);
5619 /* Unlock _before_ we do the RPC call */
5620 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005621 /* Exclude nfs_delegation_claim_locks() */
5622 mutex_lock(&sp->so_delegreturn_mutex);
5623 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005624 down_read(&nfsi->rwsem);
5625 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5626 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005627 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005628 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005629 }
5630 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005631 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005632 if (status != 0)
5633 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005634 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005635 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005636 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5637 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005638 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5639 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005640 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005641 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005642 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005643 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005644 status = PTR_ERR(task);
5645 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005646 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005647 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005648 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005649out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005650 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005651 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005652 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653}
5654
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005655struct nfs4_lockdata {
5656 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005657 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005658 struct nfs4_lock_state *lsp;
5659 struct nfs_open_context *ctx;
5660 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005661 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005662 int rpc_status;
5663 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005664 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005665};
5666
5667static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005668 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5669 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005670{
5671 struct nfs4_lockdata *p;
5672 struct inode *inode = lsp->ls_state->inode;
5673 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005674 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005675
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005676 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005677 if (p == NULL)
5678 return NULL;
5679
5680 p->arg.fh = NFS_FH(inode);
5681 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005682 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005683 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005684 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005685 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5686 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005687 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005688 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005689 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005690 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005691 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005692 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005693 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005694 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005695 atomic_inc(&lsp->ls_count);
5696 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005697 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005698 memcpy(&p->fl, fl, sizeof(p->fl));
5699 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005700out_free_seqid:
5701 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005702out_free:
5703 kfree(p);
5704 return NULL;
5705}
5706
5707static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5708{
5709 struct nfs4_lockdata *data = calldata;
5710 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711
Harvey Harrison3110ff82008-05-02 13:42:44 -07005712 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005713 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005714 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005715 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005716 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005717 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005718 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005719 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005720 nfs4_stateid_copy(&data->arg.open_stateid,
5721 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005722 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005723 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005724 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005725 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005726 nfs4_stateid_copy(&data->arg.lock_stateid,
5727 &data->lsp->ls_stateid);
5728 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005729 if (!nfs4_valid_open_stateid(state)) {
5730 data->rpc_status = -EBADF;
5731 task->tk_action = NULL;
5732 goto out_release_open_seqid;
5733 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005734 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005735 if (nfs4_setup_sequence(data->server,
5736 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005737 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005738 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005739 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005740out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005741 nfs_release_seqid(data->arg.open_seqid);
5742out_release_lock_seqid:
5743 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005744out_wait:
5745 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005746 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005747}
5748
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005749static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5750{
5751 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005752 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005753
Harvey Harrison3110ff82008-05-02 13:42:44 -07005754 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005755
Trond Myklebust14516c32010-07-31 14:29:06 -04005756 if (!nfs4_sequence_done(task, &data->res.seq_res))
5757 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005758
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005759 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005760 switch (task->tk_status) {
5761 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005762 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005763 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005764 if (data->arg.new_lock) {
5765 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5766 if (do_vfs_lock(data->fl.fl_file, &data->fl) < 0) {
5767 rpc_restart_call_prepare(task);
5768 break;
5769 }
5770 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005771 if (data->arg.new_lock_owner != 0) {
5772 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5773 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5774 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5775 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5776 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005777 break;
5778 case -NFS4ERR_BAD_STATEID:
5779 case -NFS4ERR_OLD_STATEID:
5780 case -NFS4ERR_STALE_STATEID:
5781 case -NFS4ERR_EXPIRED:
5782 if (data->arg.new_lock_owner != 0) {
5783 if (!nfs4_stateid_match(&data->arg.open_stateid,
5784 &lsp->ls_state->open_stateid))
5785 rpc_restart_call_prepare(task);
5786 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5787 &lsp->ls_stateid))
5788 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005789 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005790 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005791}
5792
5793static void nfs4_lock_release(void *calldata)
5794{
5795 struct nfs4_lockdata *data = calldata;
5796
Harvey Harrison3110ff82008-05-02 13:42:44 -07005797 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005798 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005799 if (data->cancelled != 0) {
5800 struct rpc_task *task;
5801 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5802 data->arg.lock_seqid);
5803 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005804 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005805 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005806 } else
5807 nfs_free_seqid(data->arg.lock_seqid);
5808 nfs4_put_lock_state(data->lsp);
5809 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005810 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005811 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005812 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005813}
5814
5815static const struct rpc_call_ops nfs4_lock_ops = {
5816 .rpc_call_prepare = nfs4_lock_prepare,
5817 .rpc_call_done = nfs4_lock_done,
5818 .rpc_release = nfs4_lock_release,
5819};
5820
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005821static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5822{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005823 switch (error) {
5824 case -NFS4ERR_ADMIN_REVOKED:
5825 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005826 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005827 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005828 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005829 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005830 break;
5831 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005832 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005833 case -NFS4ERR_EXPIRED:
5834 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005835 };
5836}
5837
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005838static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005839{
5840 struct nfs4_lockdata *data;
5841 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005842 struct rpc_message msg = {
5843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5844 .rpc_cred = state->owner->so_cred,
5845 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005846 struct rpc_task_setup task_setup_data = {
5847 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005848 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005849 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005850 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005851 .flags = RPC_TASK_ASYNC,
5852 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005853 int ret;
5854
Harvey Harrison3110ff82008-05-02 13:42:44 -07005855 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005856 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005857 fl->fl_u.nfs4_fl.owner,
5858 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005859 if (data == NULL)
5860 return -ENOMEM;
5861 if (IS_SETLKW(cmd))
5862 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005863 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005864 msg.rpc_argp = &data->arg;
5865 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005866 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005867 if (recovery_type > NFS_LOCK_NEW) {
5868 if (recovery_type == NFS_LOCK_RECLAIM)
5869 data->arg.reclaim = NFS_LOCK_RECLAIM;
5870 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005871 } else
5872 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005873 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005874 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005875 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005876 ret = nfs4_wait_for_completion_rpc_task(task);
5877 if (ret == 0) {
5878 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005879 if (ret)
5880 nfs4_handle_setlk_error(data->server, data->lsp,
5881 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005882 } else
5883 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005884 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005885 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005886 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887}
5888
5889static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5890{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005891 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005892 struct nfs4_exception exception = {
5893 .inode = state->inode,
5894 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005895 int err;
5896
5897 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005898 /* Cache the lock if possible... */
5899 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5900 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005901 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005902 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005903 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005904 break;
5905 nfs4_handle_exception(server, err, &exception);
5906 } while (exception.retry);
5907 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908}
5909
5910static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5911{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005912 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005913 struct nfs4_exception exception = {
5914 .inode = state->inode,
5915 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005916 int err;
5917
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005918 err = nfs4_set_lock_state(state, request);
5919 if (err != 0)
5920 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005921 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005922 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5923 return 0;
5924 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005925 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005926 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5927 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005928 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005929 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005930 switch (err) {
5931 default:
5932 goto out;
5933 case -NFS4ERR_GRACE:
5934 case -NFS4ERR_DELAY:
5935 nfs4_handle_exception(server, err, &exception);
5936 err = 0;
5937 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005938 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005939out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005940 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941}
5942
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005943#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005944/**
5945 * nfs41_check_expired_locks - possibly free a lock stateid
5946 *
5947 * @state: NFSv4 state for an inode
5948 *
5949 * Returns NFS_OK if recovery for this stateid is now finished.
5950 * Otherwise a negative NFS4ERR value is returned.
5951 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005952static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005953{
Chuck Levereb64cf92012-07-11 16:30:05 -04005954 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005955 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005956 struct nfs_server *server = NFS_SERVER(state->inode);
5957
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005958 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005959 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005960 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5961
5962 status = nfs41_test_stateid(server,
5963 &lsp->ls_stateid,
5964 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005965 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005966 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005967 /* Free the stateid unless the server
5968 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005969 if (status != -NFS4ERR_BAD_STATEID)
5970 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005971 &lsp->ls_stateid,
5972 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005973 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005974 ret = status;
5975 }
5976 }
5977 };
5978
5979 return ret;
5980}
5981
5982static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5983{
5984 int status = NFS_OK;
5985
5986 if (test_bit(LK_STATE_IN_USE, &state->flags))
5987 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005988 if (status != NFS_OK)
5989 status = nfs4_lock_expired(state, request);
5990 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005991}
5992#endif
5993
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5995{
Trond Myklebust19e03c52008-12-23 15:21:44 -05005996 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005997 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005998 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006000 if ((fl_flags & FL_POSIX) &&
6001 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6002 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006003 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006004 status = nfs4_set_lock_state(state, request);
6005 if (status != 0)
6006 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006007 request->fl_flags |= FL_ACCESS;
6008 status = do_vfs_lock(request->fl_file, request);
6009 if (status < 0)
6010 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006011 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006012 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006013 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006014 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006015 request->fl_flags = fl_flags & ~FL_SLEEP;
6016 status = do_vfs_lock(request->fl_file, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006017 up_read(&nfsi->rwsem);
6018 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006019 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006020 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006021 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006022out:
6023 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 return status;
6025}
6026
6027static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6028{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006029 struct nfs4_exception exception = {
6030 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006031 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006032 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 int err;
6034
6035 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006036 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006037 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07006038 if (err == -NFS4ERR_DENIED)
6039 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006041 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 } while (exception.retry);
6043 return err;
6044}
6045
6046static int
6047nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6048{
6049 struct nfs_open_context *ctx;
6050 struct nfs4_state *state;
6051 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6052 int status;
6053
6054 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006055 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 state = ctx->state;
6057
6058 if (request->fl_start < 0 || request->fl_end < 0)
6059 return -EINVAL;
6060
Trond Myklebustd9531262009-07-21 19:22:38 -04006061 if (IS_GETLK(cmd)) {
6062 if (state != NULL)
6063 return nfs4_proc_getlk(state, F_GETLK, request);
6064 return 0;
6065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
6067 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6068 return -EINVAL;
6069
Trond Myklebustd9531262009-07-21 19:22:38 -04006070 if (request->fl_type == F_UNLCK) {
6071 if (state != NULL)
6072 return nfs4_proc_unlck(state, cmd, request);
6073 return 0;
6074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
Trond Myklebustd9531262009-07-21 19:22:38 -04006076 if (state == NULL)
6077 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006078 /*
6079 * Don't rely on the VFS having checked the file open mode,
6080 * since it won't do this for flock() locks.
6081 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006082 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006083 case F_RDLCK:
6084 if (!(filp->f_mode & FMODE_READ))
6085 return -EBADF;
6086 break;
6087 case F_WRLCK:
6088 if (!(filp->f_mode & FMODE_WRITE))
6089 return -EBADF;
6090 }
6091
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 do {
6093 status = nfs4_proc_setlk(state, cmd, request);
6094 if ((status != -EAGAIN) || IS_SETLK(cmd))
6095 break;
6096 timeout = nfs4_set_lock_task_retry(timeout);
6097 status = -ERESTARTSYS;
6098 if (signalled())
6099 break;
6100 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 return status;
6102}
6103
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006104int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006105{
6106 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006107 int err;
6108
6109 err = nfs4_set_lock_state(state, fl);
6110 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006111 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006112 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006113 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006114}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006115
Trond Myklebustcf470c32012-03-07 13:49:12 -05006116struct nfs_release_lockowner_data {
6117 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006118 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006119 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006120 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006121 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006122};
6123
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006124static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6125{
6126 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006127 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006128 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6129 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006130 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006131 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006132}
6133
6134static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6135{
6136 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006137 struct nfs_server *server = data->server;
6138
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006139 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006140
6141 switch (task->tk_status) {
6142 case 0:
6143 renew_lease(server, data->timestamp);
6144 break;
6145 case -NFS4ERR_STALE_CLIENTID:
6146 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006147 nfs4_schedule_lease_recovery(server->nfs_client);
6148 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006149 case -NFS4ERR_LEASE_MOVED:
6150 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006151 if (nfs4_async_handle_error(task, server,
6152 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006153 rpc_restart_call_prepare(task);
6154 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006155}
6156
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006157static void nfs4_release_lockowner_release(void *calldata)
6158{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006159 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006160 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006161 kfree(calldata);
6162}
6163
Trond Myklebust17280172012-03-11 13:11:00 -04006164static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006165 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6166 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006167 .rpc_release = nfs4_release_lockowner_release,
6168};
6169
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006170static void
6171nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006172{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006173 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006174 struct rpc_message msg = {
6175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6176 };
6177
6178 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006179 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006180
Trond Myklebustcf470c32012-03-07 13:49:12 -05006181 data = kmalloc(sizeof(*data), GFP_NOFS);
6182 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006183 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006184 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006185 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006186 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6187 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6188 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006189
Trond Myklebustcf470c32012-03-07 13:49:12 -05006190 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006191 msg.rpc_resp = &data->res;
6192 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006193 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006194}
6195
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006196#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6197
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006198static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6199 const void *buf, size_t buflen,
6200 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006201{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006202 if (strcmp(key, "") != 0)
6203 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006204
David Howells2b0143b2015-03-17 22:25:59 +00006205 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006206}
6207
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006208static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6209 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006210{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006211 if (strcmp(key, "") != 0)
6212 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006213
David Howells2b0143b2015-03-17 22:25:59 +00006214 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006215}
6216
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006217static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6218 size_t list_len, const char *name,
6219 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006220{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006221 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006222
David Howells2b0143b2015-03-17 22:25:59 +00006223 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
J. Bruce Fields096455a2006-03-20 23:23:42 -05006224 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006225
6226 if (list && len <= list_len)
6227 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006228 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006229}
6230
David Quigleyc9bccef2013-05-22 12:50:45 -04006231#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6232static inline int nfs4_server_supports_labels(struct nfs_server *server)
6233{
6234 return server->caps & NFS_CAP_SECURITY_LABEL;
6235}
6236
6237static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6238 const void *buf, size_t buflen,
6239 int flags, int type)
6240{
6241 if (security_ismaclabel(key))
6242 return nfs4_set_security_label(dentry, buf, buflen);
6243
6244 return -EOPNOTSUPP;
6245}
6246
6247static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6248 void *buf, size_t buflen, int type)
6249{
6250 if (security_ismaclabel(key))
David Howells2b0143b2015-03-17 22:25:59 +00006251 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006252 return -EOPNOTSUPP;
6253}
6254
6255static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6256 size_t list_len, const char *name,
6257 size_t name_len, int type)
6258{
6259 size_t len = 0;
6260
David Howells2b0143b2015-03-17 22:25:59 +00006261 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6262 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
David Quigleyc9bccef2013-05-22 12:50:45 -04006263 if (list && len <= list_len)
David Howells2b0143b2015-03-17 22:25:59 +00006264 security_inode_listsecurity(d_inode(dentry), list, len);
David Quigleyc9bccef2013-05-22 12:50:45 -04006265 }
6266 return len;
6267}
6268
6269static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6270 .prefix = XATTR_SECURITY_PREFIX,
6271 .list = nfs4_xattr_list_nfs4_label,
6272 .get = nfs4_xattr_get_nfs4_label,
6273 .set = nfs4_xattr_set_nfs4_label,
6274};
6275#endif
6276
6277
Andy Adamson533eb462011-06-13 18:25:56 -04006278/*
6279 * nfs_fhget will use either the mounted_on_fileid or the fileid
6280 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006281static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6282{
Andy Adamson533eb462011-06-13 18:25:56 -04006283 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6284 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6285 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006286 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006287 return;
6288
6289 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006290 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006291 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6292 fattr->nlink = 2;
6293}
6294
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006295static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6296 const struct qstr *name,
6297 struct nfs4_fs_locations *fs_locations,
6298 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006299{
6300 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006301 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006302 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006303 };
6304 struct nfs4_fs_locations_arg args = {
6305 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006306 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006307 .page = page,
6308 .bitmask = bitmask,
6309 };
Benny Halevy22958462009-04-01 09:22:02 -04006310 struct nfs4_fs_locations_res res = {
6311 .fs_locations = fs_locations,
6312 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006313 struct rpc_message msg = {
6314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6315 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006316 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006317 };
6318 int status;
6319
Harvey Harrison3110ff82008-05-02 13:42:44 -07006320 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006321
6322 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6323 * is not supported */
6324 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6325 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6326 else
6327 bitmask[0] |= FATTR4_WORD0_FILEID;
6328
Trond Myklebustc228fd32007-01-13 02:28:11 -05006329 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006330 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006331 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006332 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006333 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006334 return status;
6335}
6336
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006337int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6338 const struct qstr *name,
6339 struct nfs4_fs_locations *fs_locations,
6340 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006341{
6342 struct nfs4_exception exception = { };
6343 int err;
6344 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006345 err = _nfs4_proc_fs_locations(client, dir, name,
6346 fs_locations, page);
6347 trace_nfs4_get_fs_locations(dir, name, err);
6348 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006349 &exception);
6350 } while (exception.retry);
6351 return err;
6352}
6353
Chuck Leverb03d7352013-10-17 14:12:50 -04006354/*
6355 * This operation also signals the server that this client is
6356 * performing migration recovery. The server can stop returning
6357 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6358 * appended to this compound to identify the client ID which is
6359 * performing recovery.
6360 */
6361static int _nfs40_proc_get_locations(struct inode *inode,
6362 struct nfs4_fs_locations *locations,
6363 struct page *page, struct rpc_cred *cred)
6364{
6365 struct nfs_server *server = NFS_SERVER(inode);
6366 struct rpc_clnt *clnt = server->client;
6367 u32 bitmask[2] = {
6368 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6369 };
6370 struct nfs4_fs_locations_arg args = {
6371 .clientid = server->nfs_client->cl_clientid,
6372 .fh = NFS_FH(inode),
6373 .page = page,
6374 .bitmask = bitmask,
6375 .migration = 1, /* skip LOOKUP */
6376 .renew = 1, /* append RENEW */
6377 };
6378 struct nfs4_fs_locations_res res = {
6379 .fs_locations = locations,
6380 .migration = 1,
6381 .renew = 1,
6382 };
6383 struct rpc_message msg = {
6384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6385 .rpc_argp = &args,
6386 .rpc_resp = &res,
6387 .rpc_cred = cred,
6388 };
6389 unsigned long now = jiffies;
6390 int status;
6391
6392 nfs_fattr_init(&locations->fattr);
6393 locations->server = server;
6394 locations->nlocations = 0;
6395
6396 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6397 nfs4_set_sequence_privileged(&args.seq_args);
6398 status = nfs4_call_sync_sequence(clnt, server, &msg,
6399 &args.seq_args, &res.seq_res);
6400 if (status)
6401 return status;
6402
6403 renew_lease(server, now);
6404 return 0;
6405}
6406
6407#ifdef CONFIG_NFS_V4_1
6408
6409/*
6410 * This operation also signals the server that this client is
6411 * performing migration recovery. The server can stop asserting
6412 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6413 * performing this operation is identified in the SEQUENCE
6414 * operation in this compound.
6415 *
6416 * When the client supports GETATTR(fs_locations_info), it can
6417 * be plumbed in here.
6418 */
6419static int _nfs41_proc_get_locations(struct inode *inode,
6420 struct nfs4_fs_locations *locations,
6421 struct page *page, struct rpc_cred *cred)
6422{
6423 struct nfs_server *server = NFS_SERVER(inode);
6424 struct rpc_clnt *clnt = server->client;
6425 u32 bitmask[2] = {
6426 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6427 };
6428 struct nfs4_fs_locations_arg args = {
6429 .fh = NFS_FH(inode),
6430 .page = page,
6431 .bitmask = bitmask,
6432 .migration = 1, /* skip LOOKUP */
6433 };
6434 struct nfs4_fs_locations_res res = {
6435 .fs_locations = locations,
6436 .migration = 1,
6437 };
6438 struct rpc_message msg = {
6439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6440 .rpc_argp = &args,
6441 .rpc_resp = &res,
6442 .rpc_cred = cred,
6443 };
6444 int status;
6445
6446 nfs_fattr_init(&locations->fattr);
6447 locations->server = server;
6448 locations->nlocations = 0;
6449
6450 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6451 nfs4_set_sequence_privileged(&args.seq_args);
6452 status = nfs4_call_sync_sequence(clnt, server, &msg,
6453 &args.seq_args, &res.seq_res);
6454 if (status == NFS4_OK &&
6455 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6456 status = -NFS4ERR_LEASE_MOVED;
6457 return status;
6458}
6459
6460#endif /* CONFIG_NFS_V4_1 */
6461
6462/**
6463 * nfs4_proc_get_locations - discover locations for a migrated FSID
6464 * @inode: inode on FSID that is migrating
6465 * @locations: result of query
6466 * @page: buffer
6467 * @cred: credential to use for this operation
6468 *
6469 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6470 * operation failed, or a negative errno if a local error occurred.
6471 *
6472 * On success, "locations" is filled in, but if the server has
6473 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6474 * asserted.
6475 *
6476 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6477 * from this client that require migration recovery.
6478 */
6479int nfs4_proc_get_locations(struct inode *inode,
6480 struct nfs4_fs_locations *locations,
6481 struct page *page, struct rpc_cred *cred)
6482{
6483 struct nfs_server *server = NFS_SERVER(inode);
6484 struct nfs_client *clp = server->nfs_client;
6485 const struct nfs4_mig_recovery_ops *ops =
6486 clp->cl_mvops->mig_recovery_ops;
6487 struct nfs4_exception exception = { };
6488 int status;
6489
6490 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6491 (unsigned long long)server->fsid.major,
6492 (unsigned long long)server->fsid.minor,
6493 clp->cl_hostname);
6494 nfs_display_fhandle(NFS_FH(inode), __func__);
6495
6496 do {
6497 status = ops->get_locations(inode, locations, page, cred);
6498 if (status != -NFS4ERR_DELAY)
6499 break;
6500 nfs4_handle_exception(server, status, &exception);
6501 } while (exception.retry);
6502 return status;
6503}
6504
Chuck Lever44c99932013-10-17 14:13:30 -04006505/*
6506 * This operation also signals the server that this client is
6507 * performing "lease moved" recovery. The server can stop
6508 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6509 * is appended to this compound to identify the client ID which is
6510 * performing recovery.
6511 */
6512static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6513{
6514 struct nfs_server *server = NFS_SERVER(inode);
6515 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6516 struct rpc_clnt *clnt = server->client;
6517 struct nfs4_fsid_present_arg args = {
6518 .fh = NFS_FH(inode),
6519 .clientid = clp->cl_clientid,
6520 .renew = 1, /* append RENEW */
6521 };
6522 struct nfs4_fsid_present_res res = {
6523 .renew = 1,
6524 };
6525 struct rpc_message msg = {
6526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6527 .rpc_argp = &args,
6528 .rpc_resp = &res,
6529 .rpc_cred = cred,
6530 };
6531 unsigned long now = jiffies;
6532 int status;
6533
6534 res.fh = nfs_alloc_fhandle();
6535 if (res.fh == NULL)
6536 return -ENOMEM;
6537
6538 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6539 nfs4_set_sequence_privileged(&args.seq_args);
6540 status = nfs4_call_sync_sequence(clnt, server, &msg,
6541 &args.seq_args, &res.seq_res);
6542 nfs_free_fhandle(res.fh);
6543 if (status)
6544 return status;
6545
6546 do_renew_lease(clp, now);
6547 return 0;
6548}
6549
6550#ifdef CONFIG_NFS_V4_1
6551
6552/*
6553 * This operation also signals the server that this client is
6554 * performing "lease moved" recovery. The server can stop asserting
6555 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6556 * this operation is identified in the SEQUENCE operation in this
6557 * compound.
6558 */
6559static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6560{
6561 struct nfs_server *server = NFS_SERVER(inode);
6562 struct rpc_clnt *clnt = server->client;
6563 struct nfs4_fsid_present_arg args = {
6564 .fh = NFS_FH(inode),
6565 };
6566 struct nfs4_fsid_present_res res = {
6567 };
6568 struct rpc_message msg = {
6569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6570 .rpc_argp = &args,
6571 .rpc_resp = &res,
6572 .rpc_cred = cred,
6573 };
6574 int status;
6575
6576 res.fh = nfs_alloc_fhandle();
6577 if (res.fh == NULL)
6578 return -ENOMEM;
6579
6580 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6581 nfs4_set_sequence_privileged(&args.seq_args);
6582 status = nfs4_call_sync_sequence(clnt, server, &msg,
6583 &args.seq_args, &res.seq_res);
6584 nfs_free_fhandle(res.fh);
6585 if (status == NFS4_OK &&
6586 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6587 status = -NFS4ERR_LEASE_MOVED;
6588 return status;
6589}
6590
6591#endif /* CONFIG_NFS_V4_1 */
6592
6593/**
6594 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6595 * @inode: inode on FSID to check
6596 * @cred: credential to use for this operation
6597 *
6598 * Server indicates whether the FSID is present, moved, or not
6599 * recognized. This operation is necessary to clear a LEASE_MOVED
6600 * condition for this client ID.
6601 *
6602 * Returns NFS4_OK if the FSID is present on this server,
6603 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6604 * NFS4ERR code if some error occurred on the server, or a
6605 * negative errno if a local failure occurred.
6606 */
6607int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6608{
6609 struct nfs_server *server = NFS_SERVER(inode);
6610 struct nfs_client *clp = server->nfs_client;
6611 const struct nfs4_mig_recovery_ops *ops =
6612 clp->cl_mvops->mig_recovery_ops;
6613 struct nfs4_exception exception = { };
6614 int status;
6615
6616 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6617 (unsigned long long)server->fsid.major,
6618 (unsigned long long)server->fsid.minor,
6619 clp->cl_hostname);
6620 nfs_display_fhandle(NFS_FH(inode), __func__);
6621
6622 do {
6623 status = ops->fsid_present(inode, cred);
6624 if (status != -NFS4ERR_DELAY)
6625 break;
6626 nfs4_handle_exception(server, status, &exception);
6627 } while (exception.retry);
6628 return status;
6629}
6630
Andy Adamson5ec16a82013-08-08 10:57:55 -04006631/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006632 * If 'use_integrity' is true and the state managment nfs_client
6633 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6634 * and the machine credential as per RFC3530bis and RFC5661 Security
6635 * Considerations sections. Otherwise, just use the user cred with the
6636 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006637 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006638static 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 +00006639{
6640 int status;
6641 struct nfs4_secinfo_arg args = {
6642 .dir_fh = NFS_FH(dir),
6643 .name = name,
6644 };
6645 struct nfs4_secinfo_res res = {
6646 .flavors = flavors,
6647 };
6648 struct rpc_message msg = {
6649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6650 .rpc_argp = &args,
6651 .rpc_resp = &res,
6652 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006653 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006654 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006655
6656 if (use_integrity) {
6657 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006658 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6659 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006660 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006661
6662 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006663
6664 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6665 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6666
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006667 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6668 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006669 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006670
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006671 if (cred)
6672 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006673
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006674 return status;
6675}
6676
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006677int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6678 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006679{
6680 struct nfs4_exception exception = { };
6681 int err;
6682 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006683 err = -NFS4ERR_WRONGSEC;
6684
6685 /* try to use integrity protection with machine cred */
6686 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6687 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6688
6689 /*
6690 * if unable to use integrity protection, or SECINFO with
6691 * integrity protection returns NFS4ERR_WRONGSEC (which is
6692 * disallowed by spec, but exists in deployed servers) use
6693 * the current filesystem's rpc_client and the user cred.
6694 */
6695 if (err == -NFS4ERR_WRONGSEC)
6696 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6697
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006698 trace_nfs4_secinfo(dir, name, err);
6699 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006700 &exception);
6701 } while (exception.retry);
6702 return err;
6703}
6704
Andy Adamson557134a2009-04-01 09:21:53 -04006705#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006706/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006707 * Check the exchange flags returned by the server for invalid flags, having
6708 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6709 * DS flags set.
6710 */
6711static int nfs4_check_cl_exchange_flags(u32 flags)
6712{
6713 if (flags & ~EXCHGID4_FLAG_MASK_R)
6714 goto out_inval;
6715 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6716 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6717 goto out_inval;
6718 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6719 goto out_inval;
6720 return NFS_OK;
6721out_inval:
6722 return -NFS4ERR_INVAL;
6723}
6724
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006725static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006726nfs41_same_server_scope(struct nfs41_server_scope *a,
6727 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006728{
6729 if (a->server_scope_sz == b->server_scope_sz &&
6730 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6731 return true;
6732
6733 return false;
6734}
6735
Andy Adamson357f54d2010-12-14 10:11:57 -05006736/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006737 * nfs4_proc_bind_conn_to_session()
6738 *
6739 * The 4.1 client currently uses the same TCP connection for the
6740 * fore and backchannel.
6741 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006742int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006743{
6744 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006745 struct nfs41_bind_conn_to_session_args args = {
6746 .client = clp,
6747 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6748 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006749 struct nfs41_bind_conn_to_session_res res;
6750 struct rpc_message msg = {
6751 .rpc_proc =
6752 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006753 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006754 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006755 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006756 };
6757
6758 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006759
Trond Myklebust71a097c2015-02-18 09:27:18 -08006760 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6761 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6762 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006763
6764 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006765 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006766 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006767 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006768 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6769 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6770 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006771 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006772 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006773 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006774 dprintk("NFS: %s: Unexpected direction from server\n",
6775 __func__);
6776 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006777 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006778 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006779 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006780 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6781 __func__);
6782 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006783 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006784 }
6785 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006786out:
6787 dprintk("<-- %s status= %d\n", __func__, status);
6788 return status;
6789}
6790
6791/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006792 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6793 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006794 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006795static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6796 .how = SP4_MACH_CRED,
6797 .enforce.u.words = {
6798 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6799 1 << (OP_EXCHANGE_ID - 32) |
6800 1 << (OP_CREATE_SESSION - 32) |
6801 1 << (OP_DESTROY_SESSION - 32) |
6802 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006803 },
6804 .allow.u.words = {
6805 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006806 1 << (OP_LOCKU) |
6807 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006808 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006809 1 << (OP_SECINFO_NO_NAME - 32) |
6810 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006811 1 << (OP_FREE_STATEID - 32) |
6812 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006813 }
6814};
6815
6816/*
6817 * Select the state protection mode for client `clp' given the server results
6818 * from exchange_id in `sp'.
6819 *
6820 * Returns 0 on success, negative errno otherwise.
6821 */
6822static int nfs4_sp4_select_mode(struct nfs_client *clp,
6823 struct nfs41_state_protection *sp)
6824{
6825 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6826 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6827 1 << (OP_EXCHANGE_ID - 32) |
6828 1 << (OP_CREATE_SESSION - 32) |
6829 1 << (OP_DESTROY_SESSION - 32) |
6830 1 << (OP_DESTROY_CLIENTID - 32)
6831 };
6832 unsigned int i;
6833
6834 if (sp->how == SP4_MACH_CRED) {
6835 /* Print state protect result */
6836 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6837 for (i = 0; i <= LAST_NFS4_OP; i++) {
6838 if (test_bit(i, sp->enforce.u.longs))
6839 dfprintk(MOUNT, " enforce op %d\n", i);
6840 if (test_bit(i, sp->allow.u.longs))
6841 dfprintk(MOUNT, " allow op %d\n", i);
6842 }
6843
6844 /* make sure nothing is on enforce list that isn't supported */
6845 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6846 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6847 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6848 return -EINVAL;
6849 }
6850 }
6851
6852 /*
6853 * Minimal mode - state operations are allowed to use machine
6854 * credential. Note this already happens by default, so the
6855 * client doesn't have to do anything more than the negotiation.
6856 *
6857 * NOTE: we don't care if EXCHANGE_ID is in the list -
6858 * we're already using the machine cred for exchange_id
6859 * and will never use a different cred.
6860 */
6861 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6862 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6863 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6864 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6865 dfprintk(MOUNT, "sp4_mach_cred:\n");
6866 dfprintk(MOUNT, " minimal mode enabled\n");
6867 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6868 } else {
6869 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6870 return -EINVAL;
6871 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006872
6873 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6874 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6875 dfprintk(MOUNT, " cleanup mode enabled\n");
6876 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6877 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006878
6879 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6880 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6881 dfprintk(MOUNT, " secinfo mode enabled\n");
6882 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6883 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006884
6885 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6886 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6887 dfprintk(MOUNT, " stateid mode enabled\n");
6888 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6889 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006890
6891 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6892 dfprintk(MOUNT, " write mode enabled\n");
6893 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6894 }
6895
6896 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6897 dfprintk(MOUNT, " commit mode enabled\n");
6898 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6899 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006900 }
6901
6902 return 0;
6903}
6904
6905/*
6906 * _nfs4_proc_exchange_id()
6907 *
6908 * Wrapper for EXCHANGE_ID operation.
6909 */
6910static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6911 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006912{
6913 nfs4_verifier verifier;
6914 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006915 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006916 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006917#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006918 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006919 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6920 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6921#else
6922 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6923 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6924#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006925 };
6926 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006927 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006928 };
6929 int status;
6930 struct rpc_message msg = {
6931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6932 .rpc_argp = &args,
6933 .rpc_resp = &res,
6934 .rpc_cred = cred,
6935 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006936
Chuck Leverf0920752012-05-21 22:45:41 -04006937 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006938
6939 status = nfs4_init_uniform_client_string(clp);
6940 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04006941 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006942
6943 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006944 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006945 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006946
Chuck Leveracdeb692012-05-21 22:46:16 -04006947 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006948 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006949 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006950 status = -ENOMEM;
6951 goto out;
6952 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006953
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006954 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006955 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006956 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006957 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006958 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006959 }
6960
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006961 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006962 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006963 status = -ENOMEM;
6964 goto out_server_scope;
6965 }
6966
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006967 switch (sp4_how) {
6968 case SP4_NONE:
6969 args.state_protect.how = SP4_NONE;
6970 break;
6971
6972 case SP4_MACH_CRED:
6973 args.state_protect = nfs4_sp4_mach_cred_request;
6974 break;
6975
6976 default:
6977 /* unsupported! */
6978 WARN_ON_ONCE(1);
6979 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08006980 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006981 }
6982
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006983 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006984 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006985 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006986 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006987
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006988 if (status == 0)
6989 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6990
Chuck Lever177313f2012-05-21 22:44:58 -04006991 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006992 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05006993 clp->cl_exchange_flags = res.flags;
6994 /* Client ID is not confirmed */
6995 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
6996 clear_bit(NFS4_SESSION_ESTABLISHED,
6997 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04006998 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05006999 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007000
Chuck Leveracdeb692012-05-21 22:46:16 -04007001 kfree(clp->cl_serverowner);
7002 clp->cl_serverowner = res.server_owner;
7003 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007004
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007005 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007006 kfree(clp->cl_implid);
7007 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007008 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007009
Chuck Lever177313f2012-05-21 22:44:58 -04007010 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007011 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007012 res.server_scope)) {
7013 dprintk("%s: server_scope mismatch detected\n",
7014 __func__);
7015 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007016 kfree(clp->cl_serverscope);
7017 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007018 }
7019
Chuck Lever177313f2012-05-21 22:44:58 -04007020 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007021 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007022 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007023 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007024 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007025
Kinglong Mee6b559702015-07-01 11:54:53 +08007026out_impl_id:
7027 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007028out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007029 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007030out_server_owner:
7031 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007032out:
Chuck Lever177313f2012-05-21 22:44:58 -04007033 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007034 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007035 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007036 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007037 clp->cl_implid->date.seconds,
7038 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007039 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007040 return status;
7041}
7042
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007043/*
7044 * nfs4_proc_exchange_id()
7045 *
7046 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7047 *
7048 * Since the clientid has expired, all compounds using sessions
7049 * associated with the stale clientid will be returning
7050 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7051 * be in some phase of session reset.
7052 *
7053 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7054 */
7055int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7056{
7057 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7058 int status;
7059
7060 /* try SP4_MACH_CRED if krb5i/p */
7061 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7062 authflavor == RPC_AUTH_GSS_KRB5P) {
7063 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7064 if (!status)
7065 return 0;
7066 }
7067
7068 /* try SP4_NONE */
7069 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7070}
7071
Trond Myklebust66245532012-05-25 17:18:09 -04007072static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7073 struct rpc_cred *cred)
7074{
7075 struct rpc_message msg = {
7076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7077 .rpc_argp = clp,
7078 .rpc_cred = cred,
7079 };
7080 int status;
7081
7082 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007083 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007084 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007085 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007086 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7087 return status;
7088}
7089
7090static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7091 struct rpc_cred *cred)
7092{
7093 unsigned int loop;
7094 int ret;
7095
7096 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7097 ret = _nfs4_proc_destroy_clientid(clp, cred);
7098 switch (ret) {
7099 case -NFS4ERR_DELAY:
7100 case -NFS4ERR_CLIENTID_BUSY:
7101 ssleep(1);
7102 break;
7103 default:
7104 return ret;
7105 }
7106 }
7107 return 0;
7108}
7109
7110int nfs4_destroy_clientid(struct nfs_client *clp)
7111{
7112 struct rpc_cred *cred;
7113 int ret = 0;
7114
7115 if (clp->cl_mvops->minor_version < 1)
7116 goto out;
7117 if (clp->cl_exchange_flags == 0)
7118 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007119 if (clp->cl_preserve_clid)
7120 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007121 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007122 ret = nfs4_proc_destroy_clientid(clp, cred);
7123 if (cred)
7124 put_rpccred(cred);
7125 switch (ret) {
7126 case 0:
7127 case -NFS4ERR_STALE_CLIENTID:
7128 clp->cl_exchange_flags = 0;
7129 }
7130out:
7131 return ret;
7132}
7133
Andy Adamson2050f0c2009-04-01 09:22:30 -04007134struct nfs4_get_lease_time_data {
7135 struct nfs4_get_lease_time_args *args;
7136 struct nfs4_get_lease_time_res *res;
7137 struct nfs_client *clp;
7138};
7139
7140static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7141 void *calldata)
7142{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007143 struct nfs4_get_lease_time_data *data =
7144 (struct nfs4_get_lease_time_data *)calldata;
7145
7146 dprintk("--> %s\n", __func__);
7147 /* just setup sequence, do not trigger session recovery
7148 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007149 nfs41_setup_sequence(data->clp->cl_session,
7150 &data->args->la_seq_args,
7151 &data->res->lr_seq_res,
7152 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007153 dprintk("<-- %s\n", __func__);
7154}
7155
7156/*
7157 * Called from nfs4_state_manager thread for session setup, so don't recover
7158 * from sequence operation or clientid errors.
7159 */
7160static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7161{
7162 struct nfs4_get_lease_time_data *data =
7163 (struct nfs4_get_lease_time_data *)calldata;
7164
7165 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007166 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7167 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007168 switch (task->tk_status) {
7169 case -NFS4ERR_DELAY:
7170 case -NFS4ERR_GRACE:
7171 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7172 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7173 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007174 /* fall through */
7175 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007176 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007177 return;
7178 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007179 dprintk("<-- %s\n", __func__);
7180}
7181
Trond Myklebust17280172012-03-11 13:11:00 -04007182static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007183 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7184 .rpc_call_done = nfs4_get_lease_time_done,
7185};
7186
7187int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7188{
7189 struct rpc_task *task;
7190 struct nfs4_get_lease_time_args args;
7191 struct nfs4_get_lease_time_res res = {
7192 .lr_fsinfo = fsinfo,
7193 };
7194 struct nfs4_get_lease_time_data data = {
7195 .args = &args,
7196 .res = &res,
7197 .clp = clp,
7198 };
7199 struct rpc_message msg = {
7200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7201 .rpc_argp = &args,
7202 .rpc_resp = &res,
7203 };
7204 struct rpc_task_setup task_setup = {
7205 .rpc_client = clp->cl_rpcclient,
7206 .rpc_message = &msg,
7207 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007208 .callback_data = &data,
7209 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007210 };
7211 int status;
7212
Chuck Levera9c92d62013-08-09 12:48:18 -04007213 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007214 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007215 dprintk("--> %s\n", __func__);
7216 task = rpc_run_task(&task_setup);
7217
7218 if (IS_ERR(task))
7219 status = PTR_ERR(task);
7220 else {
7221 status = task->tk_status;
7222 rpc_put_task(task);
7223 }
7224 dprintk("<-- %s return %d\n", __func__, status);
7225
7226 return status;
7227}
7228
Andy Adamsonfc931582009-04-01 09:22:31 -04007229/*
7230 * Initialize the values to be used by the client in CREATE_SESSION
7231 * If nfs4_init_session set the fore channel request and response sizes,
7232 * use them.
7233 *
7234 * Set the back channel max_resp_sz_cached to zero to force the client to
7235 * always set csa_cachethis to FALSE because the current implementation
7236 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7237 */
7238static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7239{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007240 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007241
Andy Adamson18aad3d2013-06-26 12:21:49 -04007242 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7243 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7244
Andy Adamsonfc931582009-04-01 09:22:31 -04007245 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007246 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7247 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007248 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007249 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007250
7251 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007252 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007253 __func__,
7254 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007255 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007256
7257 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007258 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7259 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7260 args->bc_attrs.max_resp_sz_cached = 0;
7261 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7262 args->bc_attrs.max_reqs = 1;
7263
7264 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7265 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7266 __func__,
7267 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7268 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7269 args->bc_attrs.max_reqs);
7270}
7271
Trond Myklebust79969dd2015-02-18 11:30:18 -08007272static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7273 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007274{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007275 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007276 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007277
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007278 if (rcvd->max_resp_sz > sent->max_resp_sz)
7279 return -EINVAL;
7280 /*
7281 * Our requested max_ops is the minimum we need; we're not
7282 * prepared to break up compounds into smaller pieces than that.
7283 * So, no point even trying to continue if the server won't
7284 * cooperate:
7285 */
7286 if (rcvd->max_ops < sent->max_ops)
7287 return -EINVAL;
7288 if (rcvd->max_reqs == 0)
7289 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007290 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7291 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007292 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007293}
7294
Trond Myklebust79969dd2015-02-18 11:30:18 -08007295static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7296 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007297{
7298 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007299 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007300
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007301 if (!(res->flags & SESSION4_BACK_CHAN))
7302 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007303 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7304 return -EINVAL;
7305 if (rcvd->max_resp_sz < sent->max_resp_sz)
7306 return -EINVAL;
7307 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7308 return -EINVAL;
7309 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007310 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007311 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007312 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007313 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007314out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007315 return 0;
7316}
Andy Adamson8d353012009-04-01 09:22:32 -04007317
Andy Adamson8d353012009-04-01 09:22:32 -04007318static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007319 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007320{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007321 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007322
Trond Myklebust79969dd2015-02-18 11:30:18 -08007323 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007324 if (ret)
7325 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007326 return nfs4_verify_back_channel_attrs(args, res);
7327}
7328
7329static void nfs4_update_session(struct nfs4_session *session,
7330 struct nfs41_create_session_res *res)
7331{
7332 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007333 /* Mark client id and session as being confirmed */
7334 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7335 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007336 session->flags = res->flags;
7337 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007338 if (res->flags & SESSION4_BACK_CHAN)
7339 memcpy(&session->bc_attrs, &res->bc_attrs,
7340 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007341}
7342
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007343static int _nfs4_proc_create_session(struct nfs_client *clp,
7344 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007345{
7346 struct nfs4_session *session = clp->cl_session;
7347 struct nfs41_create_session_args args = {
7348 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007349 .clientid = clp->cl_clientid,
7350 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007351 .cb_program = NFS4_CALLBACK,
7352 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007353 struct nfs41_create_session_res res;
7354
Andy Adamsonfc931582009-04-01 09:22:31 -04007355 struct rpc_message msg = {
7356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7357 .rpc_argp = &args,
7358 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007359 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007360 };
7361 int status;
7362
7363 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007364 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007365
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007366 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007367 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007368
Trond Myklebust43095d32012-11-20 11:13:12 -05007369 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007370 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007371 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007372 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007373 if (clp->cl_seqid == res.seqid)
7374 clp->cl_seqid++;
7375 if (status)
7376 goto out;
7377 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007378 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007379out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007380 return status;
7381}
7382
7383/*
7384 * Issues a CREATE_SESSION operation to the server.
7385 * It is the responsibility of the caller to verify the session is
7386 * expired before calling this routine.
7387 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007388int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007389{
7390 int status;
7391 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007392 struct nfs4_session *session = clp->cl_session;
7393
7394 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7395
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007396 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007397 if (status)
7398 goto out;
7399
Andy Adamsonaacd5532011-11-09 13:58:21 -05007400 /* Init or reset the session slot tables */
7401 status = nfs4_setup_session_slot_tables(session);
7402 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007403 if (status)
7404 goto out;
7405
7406 ptr = (unsigned *)&session->sess_id.data[0];
7407 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7408 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007409out:
7410 dprintk("<-- %s\n", __func__);
7411 return status;
7412}
7413
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007414/*
7415 * Issue the over-the-wire RPC DESTROY_SESSION.
7416 * The caller must serialize access to this routine.
7417 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007418int nfs4_proc_destroy_session(struct nfs4_session *session,
7419 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007420{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007421 struct rpc_message msg = {
7422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7423 .rpc_argp = session,
7424 .rpc_cred = cred,
7425 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007426 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007427
7428 dprintk("--> nfs4_proc_destroy_session\n");
7429
7430 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007431 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7432 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007433
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007434 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007435 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007436
7437 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007438 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007439 "Session has been destroyed regardless...\n", status);
7440
7441 dprintk("<-- nfs4_proc_destroy_session\n");
7442 return status;
7443}
7444
Trond Myklebust7b38c362012-05-23 13:23:31 -04007445/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007446 * Renew the cl_session lease.
7447 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007448struct nfs4_sequence_data {
7449 struct nfs_client *clp;
7450 struct nfs4_sequence_args args;
7451 struct nfs4_sequence_res res;
7452};
7453
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007454static void nfs41_sequence_release(void *data)
7455{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007456 struct nfs4_sequence_data *calldata = data;
7457 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007458
Alexandros Batsakis71358402010-02-05 03:45:05 -08007459 if (atomic_read(&clp->cl_count) > 1)
7460 nfs4_schedule_state_renewal(clp);
7461 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007462 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007463}
7464
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007465static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7466{
7467 switch(task->tk_status) {
7468 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007469 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7470 return -EAGAIN;
7471 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007472 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007473 }
7474 return 0;
7475}
7476
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007477static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007478{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007479 struct nfs4_sequence_data *calldata = data;
7480 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007481
Trond Myklebust14516c32010-07-31 14:29:06 -04007482 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7483 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007484
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007485 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007486 if (task->tk_status < 0) {
7487 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007488 if (atomic_read(&clp->cl_count) == 1)
7489 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007490
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007491 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7492 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007493 return;
7494 }
7495 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007496 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007497out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007498 dprintk("<-- %s\n", __func__);
7499}
7500
7501static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7502{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007503 struct nfs4_sequence_data *calldata = data;
7504 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007505 struct nfs4_sequence_args *args;
7506 struct nfs4_sequence_res *res;
7507
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007508 args = task->tk_msg.rpc_argp;
7509 res = task->tk_msg.rpc_resp;
7510
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007511 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007512}
7513
7514static const struct rpc_call_ops nfs41_sequence_ops = {
7515 .rpc_call_done = nfs41_sequence_call_done,
7516 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007517 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007518};
7519
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007520static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7521 struct rpc_cred *cred,
7522 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007523{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007524 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007525 struct rpc_message msg = {
7526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7527 .rpc_cred = cred,
7528 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007529 struct rpc_task_setup task_setup_data = {
7530 .rpc_client = clp->cl_rpcclient,
7531 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007532 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007533 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007534 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007535
Alexandros Batsakis71358402010-02-05 03:45:05 -08007536 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007537 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007538 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007539 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007540 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007541 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007542 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007543 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007544 if (is_privileged)
7545 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007546 msg.rpc_argp = &calldata->args;
7547 msg.rpc_resp = &calldata->res;
7548 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007549 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007550
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007551 return rpc_run_task(&task_setup_data);
7552}
7553
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007554static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007555{
7556 struct rpc_task *task;
7557 int ret = 0;
7558
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007559 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007560 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007561 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007562 if (IS_ERR(task))
7563 ret = PTR_ERR(task);
7564 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007565 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007566 dprintk("<-- %s status=%d\n", __func__, ret);
7567 return ret;
7568}
7569
7570static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7571{
7572 struct rpc_task *task;
7573 int ret;
7574
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007575 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007576 if (IS_ERR(task)) {
7577 ret = PTR_ERR(task);
7578 goto out;
7579 }
7580 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007581 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007582 ret = task->tk_status;
7583 rpc_put_task(task);
7584out:
7585 dprintk("<-- %s status=%d\n", __func__, ret);
7586 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007587}
7588
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007589struct nfs4_reclaim_complete_data {
7590 struct nfs_client *clp;
7591 struct nfs41_reclaim_complete_args arg;
7592 struct nfs41_reclaim_complete_res res;
7593};
7594
7595static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7596{
7597 struct nfs4_reclaim_complete_data *calldata = data;
7598
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007599 nfs41_setup_sequence(calldata->clp->cl_session,
7600 &calldata->arg.seq_args,
7601 &calldata->res.seq_res,
7602 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007603}
7604
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007605static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7606{
7607 switch(task->tk_status) {
7608 case 0:
7609 case -NFS4ERR_COMPLETE_ALREADY:
7610 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7611 break;
7612 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007613 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007614 /* fall through */
7615 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007616 return -EAGAIN;
7617 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007618 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007619 }
7620 return 0;
7621}
7622
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007623static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7624{
7625 struct nfs4_reclaim_complete_data *calldata = data;
7626 struct nfs_client *clp = calldata->clp;
7627 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7628
7629 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007630 if (!nfs41_sequence_done(task, res))
7631 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007632
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007633 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007634 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7635 rpc_restart_call_prepare(task);
7636 return;
7637 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007638 dprintk("<-- %s\n", __func__);
7639}
7640
7641static void nfs4_free_reclaim_complete_data(void *data)
7642{
7643 struct nfs4_reclaim_complete_data *calldata = data;
7644
7645 kfree(calldata);
7646}
7647
7648static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7649 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7650 .rpc_call_done = nfs4_reclaim_complete_done,
7651 .rpc_release = nfs4_free_reclaim_complete_data,
7652};
7653
7654/*
7655 * Issue a global reclaim complete.
7656 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007657static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7658 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007659{
7660 struct nfs4_reclaim_complete_data *calldata;
7661 struct rpc_task *task;
7662 struct rpc_message msg = {
7663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007664 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007665 };
7666 struct rpc_task_setup task_setup_data = {
7667 .rpc_client = clp->cl_rpcclient,
7668 .rpc_message = &msg,
7669 .callback_ops = &nfs4_reclaim_complete_call_ops,
7670 .flags = RPC_TASK_ASYNC,
7671 };
7672 int status = -ENOMEM;
7673
7674 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007675 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007676 if (calldata == NULL)
7677 goto out;
7678 calldata->clp = clp;
7679 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007680
Chuck Levera9c92d62013-08-09 12:48:18 -04007681 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007682 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007683 msg.rpc_argp = &calldata->arg;
7684 msg.rpc_resp = &calldata->res;
7685 task_setup_data.callback_data = calldata;
7686 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007687 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007688 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007689 goto out;
7690 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007691 status = nfs4_wait_for_completion_rpc_task(task);
7692 if (status == 0)
7693 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007694 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007695 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007696out:
7697 dprintk("<-- %s status=%d\n", __func__, status);
7698 return status;
7699}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007700
7701static void
7702nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7703{
7704 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007705 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007706 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007707
7708 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007709 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7710 * right now covering the LAYOUTGET we are about to send.
7711 * However, that is not so catastrophic, and there seems
7712 * to be no way to prevent it completely.
7713 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007714 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007715 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007716 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007717 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7718 NFS_I(lgp->args.inode)->layout,
Peng Taoce6ab4f2014-09-06 00:53:24 +08007719 &lgp->args.range,
Fred Isamancf7d63f2011-01-06 11:36:25 +00007720 lgp->args.ctx->state)) {
7721 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007722 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007723}
7724
7725static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7726{
7727 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007728 struct inode *inode = lgp->args.inode;
7729 struct nfs_server *server = NFS_SERVER(inode);
7730 struct pnfs_layout_hdr *lo;
7731 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007732 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007733
Boaz Harroshed7e5422014-01-22 20:34:54 +02007734 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007735
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007736 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007737 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007738
7739 switch (task->tk_status) {
7740 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007741 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007742 /*
7743 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7744 * (or clients) writing to the same RAID stripe
7745 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007746 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007747 /*
7748 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7749 * existing layout before getting a new one).
7750 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007751 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007752 timeo = rpc_get_timeout(task->tk_client);
7753 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007754 now = jiffies;
7755 if (time_after(giveup, now)) {
7756 unsigned long delay;
7757
7758 /* Delay for:
7759 * - Not less then NFS4_POLL_RETRY_MIN.
7760 * - One last time a jiffie before we give up
7761 * - exponential backoff (time_now minus start_attempt)
7762 */
7763 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7764 min((giveup - now - 1),
7765 now - lgp->args.timestamp));
7766
7767 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7768 __func__, delay);
7769 rpc_delay(task, delay);
7770 task->tk_status = 0;
7771 rpc_restart_call_prepare(task);
7772 goto out; /* Do not call nfs4_async_handle_error() */
7773 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007774 break;
7775 case -NFS4ERR_EXPIRED:
7776 case -NFS4ERR_BAD_STATEID:
7777 spin_lock(&inode->i_lock);
7778 lo = NFS_I(inode)->layout;
7779 if (!lo || list_empty(&lo->plh_segs)) {
7780 spin_unlock(&inode->i_lock);
7781 /* If the open stateid was bad, then recover it. */
7782 state = lgp->args.ctx->state;
7783 } else {
7784 LIST_HEAD(head);
7785
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007786 /*
7787 * Mark the bad layout state as invalid, then retry
7788 * with the current stateid.
7789 */
Trond Myklebustee314c22012-10-01 17:25:48 -07007790 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7791 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007792 pnfs_free_lseg_list(&head);
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007793
7794 task->tk_status = 0;
7795 rpc_restart_call_prepare(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007796 }
7797 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007798 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebustee314c22012-10-01 17:25:48 -07007799 rpc_restart_call_prepare(task);
7800out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007801 dprintk("<-- %s\n", __func__);
7802}
7803
Idan Kedar85541162012-08-02 11:47:10 +03007804static size_t max_response_pages(struct nfs_server *server)
7805{
7806 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7807 return nfs_page_array_len(0, max_resp_sz);
7808}
7809
7810static void nfs4_free_pages(struct page **pages, size_t size)
7811{
7812 int i;
7813
7814 if (!pages)
7815 return;
7816
7817 for (i = 0; i < size; i++) {
7818 if (!pages[i])
7819 break;
7820 __free_page(pages[i]);
7821 }
7822 kfree(pages);
7823}
7824
7825static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7826{
7827 struct page **pages;
7828 int i;
7829
7830 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7831 if (!pages) {
7832 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7833 return NULL;
7834 }
7835
7836 for (i = 0; i < size; i++) {
7837 pages[i] = alloc_page(gfp_flags);
7838 if (!pages[i]) {
7839 dprintk("%s: failed to allocate page\n", __func__);
7840 nfs4_free_pages(pages, size);
7841 return NULL;
7842 }
7843 }
7844
7845 return pages;
7846}
7847
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007848static void nfs4_layoutget_release(void *calldata)
7849{
7850 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007851 struct inode *inode = lgp->args.inode;
7852 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007853 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007854
7855 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007856 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007857 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007858 put_nfs_open_context(lgp->args.ctx);
7859 kfree(calldata);
7860 dprintk("<-- %s\n", __func__);
7861}
7862
7863static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7864 .rpc_call_prepare = nfs4_layoutget_prepare,
7865 .rpc_call_done = nfs4_layoutget_done,
7866 .rpc_release = nfs4_layoutget_release,
7867};
7868
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007869struct pnfs_layout_segment *
7870nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007871{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007872 struct inode *inode = lgp->args.inode;
7873 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007874 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007875 struct rpc_task *task;
7876 struct rpc_message msg = {
7877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7878 .rpc_argp = &lgp->args,
7879 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007880 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007881 };
7882 struct rpc_task_setup task_setup_data = {
7883 .rpc_client = server->client,
7884 .rpc_message = &msg,
7885 .callback_ops = &nfs4_layoutget_call_ops,
7886 .callback_data = lgp,
7887 .flags = RPC_TASK_ASYNC,
7888 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007889 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007890 int status = 0;
7891
7892 dprintk("--> %s\n", __func__);
7893
Peng Tao4bd5a982014-11-17 11:05:17 +08007894 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7895 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7896
Idan Kedar85541162012-08-02 11:47:10 +03007897 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7898 if (!lgp->args.layout.pages) {
7899 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007900 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007901 }
7902 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007903 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007904
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007905 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007906 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007907 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007908
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007909 task = rpc_run_task(&task_setup_data);
7910 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007911 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007912 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007913 if (status == 0)
7914 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007915 trace_nfs4_layoutget(lgp->args.ctx,
7916 &lgp->args.range,
7917 &lgp->res.range,
7918 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007919 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7920 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007921 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007922 rpc_put_task(task);
7923 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007924 if (status)
7925 return ERR_PTR(status);
7926 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007927}
7928
Benny Halevycbe82602011-05-22 19:52:37 +03007929static void
7930nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7931{
7932 struct nfs4_layoutreturn *lrp = calldata;
7933
7934 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007935 nfs41_setup_sequence(lrp->clp->cl_session,
7936 &lrp->args.seq_args,
7937 &lrp->res.seq_res,
7938 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007939}
7940
7941static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7942{
7943 struct nfs4_layoutreturn *lrp = calldata;
7944 struct nfs_server *server;
7945
7946 dprintk("--> %s\n", __func__);
7947
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007948 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007949 return;
7950
7951 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007952 switch (task->tk_status) {
7953 default:
7954 task->tk_status = 0;
7955 case 0:
7956 break;
7957 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007958 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007959 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007960 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007961 return;
7962 }
Benny Halevycbe82602011-05-22 19:52:37 +03007963 dprintk("<-- %s\n", __func__);
7964}
7965
7966static void nfs4_layoutreturn_release(void *calldata)
7967{
7968 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007969 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02007970 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03007971
7972 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007973 spin_lock(&lo->plh_inode->i_lock);
7974 if (lrp->res.lrs_present)
7975 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebustc5d73712015-07-09 17:58:39 +02007976 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
Tom Haynesd67ae822014-12-11 17:02:04 -05007977 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04007978 lo->plh_block_lgets--;
7979 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02007980 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007981 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05007982 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03007983 kfree(calldata);
7984 dprintk("<-- %s\n", __func__);
7985}
7986
7987static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7988 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7989 .rpc_call_done = nfs4_layoutreturn_done,
7990 .rpc_release = nfs4_layoutreturn_release,
7991};
7992
Peng Tao6c166052014-11-17 09:30:40 +08007993int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03007994{
7995 struct rpc_task *task;
7996 struct rpc_message msg = {
7997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7998 .rpc_argp = &lrp->args,
7999 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008000 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008001 };
8002 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008003 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008004 .rpc_message = &msg,
8005 .callback_ops = &nfs4_layoutreturn_call_ops,
8006 .callback_data = lrp,
8007 };
Peng Tao6c166052014-11-17 09:30:40 +08008008 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008009
8010 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008011 if (!sync) {
8012 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8013 if (!lrp->inode) {
8014 nfs4_layoutreturn_release(lrp);
8015 return -EAGAIN;
8016 }
8017 task_setup_data.flags |= RPC_TASK_ASYNC;
8018 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008019 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008020 task = rpc_run_task(&task_setup_data);
8021 if (IS_ERR(task))
8022 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008023 if (sync)
8024 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008025 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008026 dprintk("<-- %s status=%d\n", __func__, status);
8027 rpc_put_task(task);
8028 return status;
8029}
8030
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008031static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008032_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8033 struct pnfs_device *pdev,
8034 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008035{
8036 struct nfs4_getdeviceinfo_args args = {
8037 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008038 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8039 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008040 };
8041 struct nfs4_getdeviceinfo_res res = {
8042 .pdev = pdev,
8043 };
8044 struct rpc_message msg = {
8045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8046 .rpc_argp = &args,
8047 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008048 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008049 };
8050 int status;
8051
8052 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008053 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008054 if (res.notification & ~args.notify_types)
8055 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008056 if (res.notification != args.notify_types)
8057 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008058
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008059 dprintk("<-- %s status=%d\n", __func__, status);
8060
8061 return status;
8062}
8063
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008064int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8065 struct pnfs_device *pdev,
8066 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008067{
8068 struct nfs4_exception exception = { };
8069 int err;
8070
8071 do {
8072 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008073 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008074 &exception);
8075 } while (exception.retry);
8076 return err;
8077}
8078EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8079
Andy Adamson863a3c62011-03-23 13:27:54 +00008080static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8081{
8082 struct nfs4_layoutcommit_data *data = calldata;
8083 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008084 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008085
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008086 nfs41_setup_sequence(session,
8087 &data->args.seq_args,
8088 &data->res.seq_res,
8089 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008090}
8091
8092static void
8093nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8094{
8095 struct nfs4_layoutcommit_data *data = calldata;
8096 struct nfs_server *server = NFS_SERVER(data->args.inode);
8097
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008098 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008099 return;
8100
8101 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008102 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8103 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8104 case -NFS4ERR_BADLAYOUT: /* no layout */
8105 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008106 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008107 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008108 break;
8109 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008110 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008111 rpc_restart_call_prepare(task);
8112 return;
8113 }
8114 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008115}
8116
8117static void nfs4_layoutcommit_release(void *calldata)
8118{
8119 struct nfs4_layoutcommit_data *data = calldata;
8120
Andy Adamsondb29c082011-07-30 20:52:38 -04008121 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008122 nfs_post_op_update_inode_force_wcc(data->args.inode,
8123 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008124 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008125 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008126 kfree(data);
8127}
8128
8129static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8130 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8131 .rpc_call_done = nfs4_layoutcommit_done,
8132 .rpc_release = nfs4_layoutcommit_release,
8133};
8134
8135int
Andy Adamsonef311532011-03-12 02:58:10 -05008136nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008137{
8138 struct rpc_message msg = {
8139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8140 .rpc_argp = &data->args,
8141 .rpc_resp = &data->res,
8142 .rpc_cred = data->cred,
8143 };
8144 struct rpc_task_setup task_setup_data = {
8145 .task = &data->task,
8146 .rpc_client = NFS_CLIENT(data->args.inode),
8147 .rpc_message = &msg,
8148 .callback_ops = &nfs4_layoutcommit_ops,
8149 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008150 };
8151 struct rpc_task *task;
8152 int status = 0;
8153
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008154 dprintk("NFS: initiating layoutcommit call. sync %d "
8155 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008156 data->args.lastbytewritten,
8157 data->args.inode->i_ino);
8158
Trond Myklebust472e2592015-02-05 16:50:30 -05008159 if (!sync) {
8160 data->inode = nfs_igrab_and_active(data->args.inode);
8161 if (data->inode == NULL) {
8162 nfs4_layoutcommit_release(data);
8163 return -EAGAIN;
8164 }
8165 task_setup_data.flags = RPC_TASK_ASYNC;
8166 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008167 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008168 task = rpc_run_task(&task_setup_data);
8169 if (IS_ERR(task))
8170 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008171 if (sync)
8172 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008173 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008174 dprintk("%s: status %d\n", __func__, status);
8175 rpc_put_task(task);
8176 return status;
8177}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008178
Andy Adamson97431202013-08-08 10:57:56 -04008179/**
8180 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8181 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8182 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008183static int
8184_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008185 struct nfs_fsinfo *info,
8186 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008187{
8188 struct nfs41_secinfo_no_name_args args = {
8189 .style = SECINFO_STYLE_CURRENT_FH,
8190 };
8191 struct nfs4_secinfo_res res = {
8192 .flavors = flavors,
8193 };
8194 struct rpc_message msg = {
8195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8196 .rpc_argp = &args,
8197 .rpc_resp = &res,
8198 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008199 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008200 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008201 int status;
8202
8203 if (use_integrity) {
8204 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008205 cred = nfs4_get_clid_cred(server->nfs_client);
8206 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008207 }
8208
8209 dprintk("--> %s\n", __func__);
8210 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8211 &res.seq_res, 0);
8212 dprintk("<-- %s status=%d\n", __func__, status);
8213
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008214 if (cred)
8215 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008216
8217 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008218}
8219
8220static int
8221nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8222 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8223{
8224 struct nfs4_exception exception = { };
8225 int err;
8226 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008227 /* first try using integrity protection */
8228 err = -NFS4ERR_WRONGSEC;
8229
8230 /* try to use integrity protection with machine cred */
8231 if (_nfs4_is_integrity_protected(server->nfs_client))
8232 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8233 flavors, true);
8234
8235 /*
8236 * if unable to use integrity protection, or SECINFO with
8237 * integrity protection returns NFS4ERR_WRONGSEC (which is
8238 * disallowed by spec, but exists in deployed servers) use
8239 * the current filesystem's rpc_client and the user cred.
8240 */
8241 if (err == -NFS4ERR_WRONGSEC)
8242 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8243 flavors, false);
8244
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008245 switch (err) {
8246 case 0:
8247 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008248 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008249 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008250 default:
8251 err = nfs4_handle_exception(server, err, &exception);
8252 }
8253 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008254out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008255 return err;
8256}
8257
8258static int
8259nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8260 struct nfs_fsinfo *info)
8261{
8262 int err;
8263 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008264 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008265 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008266 struct nfs4_secinfo4 *secinfo;
8267 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008268
8269 page = alloc_page(GFP_KERNEL);
8270 if (!page) {
8271 err = -ENOMEM;
8272 goto out;
8273 }
8274
8275 flavors = page_address(page);
8276 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8277
8278 /*
8279 * Fall back on "guess and check" method if
8280 * the server doesn't support SECINFO_NO_NAME
8281 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008282 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008283 err = nfs4_find_root_sec(server, fhandle, info);
8284 goto out_freepage;
8285 }
8286 if (err)
8287 goto out_freepage;
8288
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008289 for (i = 0; i < flavors->num_flavors; i++) {
8290 secinfo = &flavors->flavors[i];
8291
8292 switch (secinfo->flavor) {
8293 case RPC_AUTH_NULL:
8294 case RPC_AUTH_UNIX:
8295 case RPC_AUTH_GSS:
8296 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8297 &secinfo->flavor_info);
8298 break;
8299 default:
8300 flavor = RPC_AUTH_MAXFLAVOR;
8301 break;
8302 }
8303
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008304 if (!nfs_auth_info_match(&server->auth_info, flavor))
8305 flavor = RPC_AUTH_MAXFLAVOR;
8306
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008307 if (flavor != RPC_AUTH_MAXFLAVOR) {
8308 err = nfs4_lookup_root_sec(server, fhandle,
8309 info, flavor);
8310 if (!err)
8311 break;
8312 }
8313 }
8314
8315 if (flavor == RPC_AUTH_MAXFLAVOR)
8316 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008317
8318out_freepage:
8319 put_page(page);
8320 if (err == -EACCES)
8321 return -EPERM;
8322out:
8323 return err;
8324}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008325
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008326static int _nfs41_test_stateid(struct nfs_server *server,
8327 nfs4_stateid *stateid,
8328 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008329{
8330 int status;
8331 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008332 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008333 };
8334 struct nfs41_test_stateid_res res;
8335 struct rpc_message msg = {
8336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8337 .rpc_argp = &args,
8338 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008339 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008340 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008341 struct rpc_clnt *rpc_client = server->client;
8342
8343 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8344 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008345
Chuck Lever38527b12012-07-11 16:30:23 -04008346 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008347 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008348 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008349 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008350 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008351 if (status != NFS_OK) {
8352 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008353 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008354 }
8355 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008356 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008357}
8358
Chuck Lever38527b12012-07-11 16:30:23 -04008359/**
8360 * nfs41_test_stateid - perform a TEST_STATEID operation
8361 *
8362 * @server: server / transport on which to perform the operation
8363 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008364 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008365 *
8366 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8367 * Otherwise a negative NFS4ERR value is returned if the operation
8368 * failed or the state ID is not currently valid.
8369 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008370static int nfs41_test_stateid(struct nfs_server *server,
8371 nfs4_stateid *stateid,
8372 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008373{
8374 struct nfs4_exception exception = { };
8375 int err;
8376 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008377 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008378 if (err != -NFS4ERR_DELAY)
8379 break;
8380 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008381 } while (exception.retry);
8382 return err;
8383}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008384
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008385struct nfs_free_stateid_data {
8386 struct nfs_server *server;
8387 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008388 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008389};
8390
8391static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8392{
8393 struct nfs_free_stateid_data *data = calldata;
8394 nfs41_setup_sequence(nfs4_get_session(data->server),
8395 &data->args.seq_args,
8396 &data->res.seq_res,
8397 task);
8398}
8399
8400static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8401{
8402 struct nfs_free_stateid_data *data = calldata;
8403
8404 nfs41_sequence_done(task, &data->res.seq_res);
8405
8406 switch (task->tk_status) {
8407 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008408 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008409 rpc_restart_call_prepare(task);
8410 }
8411}
8412
8413static void nfs41_free_stateid_release(void *calldata)
8414{
8415 kfree(calldata);
8416}
8417
Trond Myklebust17f26b12013-08-21 15:48:42 -04008418static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008419 .rpc_call_prepare = nfs41_free_stateid_prepare,
8420 .rpc_call_done = nfs41_free_stateid_done,
8421 .rpc_release = nfs41_free_stateid_release,
8422};
8423
8424static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8425 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008426 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008427 bool privileged)
8428{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008429 struct rpc_message msg = {
8430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008431 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008432 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008433 struct rpc_task_setup task_setup = {
8434 .rpc_client = server->client,
8435 .rpc_message = &msg,
8436 .callback_ops = &nfs41_free_stateid_ops,
8437 .flags = RPC_TASK_ASYNC,
8438 };
8439 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008440
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008441 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8442 &task_setup.rpc_client, &msg);
8443
Chuck Lever38527b12012-07-11 16:30:23 -04008444 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008445 data = kmalloc(sizeof(*data), GFP_NOFS);
8446 if (!data)
8447 return ERR_PTR(-ENOMEM);
8448 data->server = server;
8449 nfs4_stateid_copy(&data->args.stateid, stateid);
8450
8451 task_setup.callback_data = data;
8452
8453 msg.rpc_argp = &data->args;
8454 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008455 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008456 if (privileged)
8457 nfs4_set_sequence_privileged(&data->args.seq_args);
8458
8459 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008460}
8461
Chuck Lever38527b12012-07-11 16:30:23 -04008462/**
8463 * nfs41_free_stateid - perform a FREE_STATEID operation
8464 *
8465 * @server: server / transport on which to perform the operation
8466 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008467 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008468 *
8469 * Returns NFS_OK if the server freed "stateid". Otherwise a
8470 * negative NFS4ERR value is returned.
8471 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008472static int nfs41_free_stateid(struct nfs_server *server,
8473 nfs4_stateid *stateid,
8474 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008475{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008476 struct rpc_task *task;
8477 int ret;
8478
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008479 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008480 if (IS_ERR(task))
8481 return PTR_ERR(task);
8482 ret = rpc_wait_for_completion_task(task);
8483 if (!ret)
8484 ret = task->tk_status;
8485 rpc_put_task(task);
8486 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008487}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008488
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008489static void
8490nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008491{
8492 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008493 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008494
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008495 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008496 nfs4_free_lock_state(server, lsp);
8497 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008498 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008499 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008500}
8501
Trond Myklebust36281ca2012-03-04 18:13:56 -05008502static bool nfs41_match_stateid(const nfs4_stateid *s1,
8503 const nfs4_stateid *s2)
8504{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008505 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008506 return false;
8507
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008508 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008509 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008510 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008511 return true;
8512
8513 return false;
8514}
8515
Andy Adamson557134a2009-04-01 09:21:53 -04008516#endif /* CONFIG_NFS_V4_1 */
8517
Trond Myklebust36281ca2012-03-04 18:13:56 -05008518static bool nfs4_match_stateid(const nfs4_stateid *s1,
8519 const nfs4_stateid *s2)
8520{
Trond Myklebustf597c532012-03-04 18:13:56 -05008521 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008522}
8523
8524
Trond Myklebust17280172012-03-11 13:11:00 -04008525static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008526 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008527 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528 .recover_open = nfs4_open_reclaim,
8529 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008530 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008531 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532};
8533
Andy Adamson591d71c2009-04-01 09:22:47 -04008534#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008535static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008536 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8537 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8538 .recover_open = nfs4_open_reclaim,
8539 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008540 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008541 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008542 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008543};
8544#endif /* CONFIG_NFS_V4_1 */
8545
Trond Myklebust17280172012-03-11 13:11:00 -04008546static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008547 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008548 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008549 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008550 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008551 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552};
8553
Andy Adamson591d71c2009-04-01 09:22:47 -04008554#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008555static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008556 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8557 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008558 .recover_open = nfs41_open_expired,
8559 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008560 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008561};
8562#endif /* CONFIG_NFS_V4_1 */
8563
Trond Myklebust17280172012-03-11 13:11:00 -04008564static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008565 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008566 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008567 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008568};
8569
8570#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008571static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008572 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008573 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008574 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008575};
8576#endif
8577
Chuck Leverec011fe2013-10-17 14:12:39 -04008578static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008579 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008580 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008581};
8582
8583#if defined(CONFIG_NFS_V4_1)
8584static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008585 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008586 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008587};
8588#endif /* CONFIG_NFS_V4_1 */
8589
Trond Myklebust97dc1352010-06-16 09:52:26 -04008590static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8591 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008592 .init_caps = NFS_CAP_READDIRPLUS
8593 | NFS_CAP_ATOMIC_OPEN
8594 | NFS_CAP_CHANGE_ATTR
8595 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008596 .init_client = nfs40_init_client,
8597 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008598 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008599 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008600 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008601 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008602 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008603 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8604 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8605 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008606 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008607};
8608
8609#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008610static struct nfs_seqid *
8611nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8612{
8613 return NULL;
8614}
8615
Trond Myklebust97dc1352010-06-16 09:52:26 -04008616static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8617 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008618 .init_caps = NFS_CAP_READDIRPLUS
8619 | NFS_CAP_ATOMIC_OPEN
8620 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008621 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008622 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008623 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008624 .init_client = nfs41_init_client,
8625 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008626 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008627 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008628 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008629 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008630 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008631 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8632 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8633 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008634 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008635};
8636#endif
8637
Steve Dickson42c2c422013-05-22 12:50:38 -04008638#if defined(CONFIG_NFS_V4_2)
8639static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8640 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008641 .init_caps = NFS_CAP_READDIRPLUS
8642 | NFS_CAP_ATOMIC_OPEN
8643 | NFS_CAP_CHANGE_ATTR
8644 | NFS_CAP_POSIX_LOCK
8645 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008646 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008647 | NFS_CAP_ALLOCATE
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008648 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008649 | NFS_CAP_SEEK
8650 | NFS_CAP_LAYOUTSTATS,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008651 .init_client = nfs41_init_client,
8652 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008653 .match_stateid = nfs41_match_stateid,
8654 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008655 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008656 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008657 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008658 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8659 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8660 .state_renewal_ops = &nfs41_state_renewal_ops,
8661};
8662#endif
8663
Trond Myklebust97dc1352010-06-16 09:52:26 -04008664const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8665 [0] = &nfs_v4_0_minor_ops,
8666#if defined(CONFIG_NFS_V4_1)
8667 [1] = &nfs_v4_1_minor_ops,
8668#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008669#if defined(CONFIG_NFS_V4_2)
8670 [2] = &nfs_v4_2_minor_ops,
8671#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008672};
8673
Trond Myklebust17f26b12013-08-21 15:48:42 -04008674static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008675 .create = nfs_create,
8676 .lookup = nfs_lookup,
8677 .atomic_open = nfs_atomic_open,
8678 .link = nfs_link,
8679 .unlink = nfs_unlink,
8680 .symlink = nfs_symlink,
8681 .mkdir = nfs_mkdir,
8682 .rmdir = nfs_rmdir,
8683 .mknod = nfs_mknod,
8684 .rename = nfs_rename,
8685 .permission = nfs_permission,
8686 .getattr = nfs_getattr,
8687 .setattr = nfs_setattr,
8688 .getxattr = generic_getxattr,
8689 .setxattr = generic_setxattr,
8690 .listxattr = generic_listxattr,
8691 .removexattr = generic_removexattr,
8692};
8693
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008694static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008695 .permission = nfs_permission,
8696 .getattr = nfs_getattr,
8697 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008698 .getxattr = generic_getxattr,
8699 .setxattr = generic_setxattr,
8700 .listxattr = generic_listxattr,
8701 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008702};
8703
David Howells509de812006-08-22 20:06:11 -04008704const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705 .version = 4, /* protocol version */
8706 .dentry_ops = &nfs4_dentry_operations,
8707 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008708 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008709 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008711 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008712 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008713 .getattr = nfs4_proc_getattr,
8714 .setattr = nfs4_proc_setattr,
8715 .lookup = nfs4_proc_lookup,
8716 .access = nfs4_proc_access,
8717 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 .create = nfs4_proc_create,
8719 .remove = nfs4_proc_remove,
8720 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008721 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008722 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008723 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008724 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008725 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726 .link = nfs4_proc_link,
8727 .symlink = nfs4_proc_symlink,
8728 .mkdir = nfs4_proc_mkdir,
8729 .rmdir = nfs4_proc_remove,
8730 .readdir = nfs4_proc_readdir,
8731 .mknod = nfs4_proc_mknod,
8732 .statfs = nfs4_proc_statfs,
8733 .fsinfo = nfs4_proc_fsinfo,
8734 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008735 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008737 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008739 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008741 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008743 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008744 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008746 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008747 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008748 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008749 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008750 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008751 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008752 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008753 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008754 .create_server = nfs4_create_server,
8755 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008756};
8757
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008758static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8759 .prefix = XATTR_NAME_NFSV4_ACL,
8760 .list = nfs4_xattr_list_nfs4_acl,
8761 .get = nfs4_xattr_get_nfs4_acl,
8762 .set = nfs4_xattr_set_nfs4_acl,
8763};
8764
8765const struct xattr_handler *nfs4_xattr_handlers[] = {
8766 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008767#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8768 &nfs4_xattr_nfs4_label_handler,
8769#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008770 NULL
8771};
8772
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773/*
8774 * Local variables:
8775 * c-basic-offset: 8
8776 * End:
8777 */