blob: 605c203de556af3ceec72a1c371c4f17d1f6d0ea [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{
470 do_renew_lease(server->nfs_client, timestamp);
471}
472
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400473struct nfs4_call_sync_data {
474 const struct nfs_server *seq_server;
475 struct nfs4_sequence_args *seq_args;
476 struct nfs4_sequence_res *seq_res;
477};
478
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800479void nfs4_init_sequence(struct nfs4_sequence_args *args,
480 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400481{
482 args->sa_slot = NULL;
483 args->sa_cache_this = cache_reply;
484 args->sa_privileged = 0;
485
486 res->sr_slot = NULL;
487}
488
489static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
490{
491 args->sa_privileged = 1;
492}
493
Peng Taocb04ad22014-06-11 05:24:15 +0800494int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
495 struct nfs4_sequence_args *args,
496 struct nfs4_sequence_res *res,
497 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400498{
Chuck Lever3bd23842013-08-09 12:49:19 -0400499 struct nfs4_slot *slot;
500
501 /* slot already allocated? */
502 if (res->sr_slot != NULL)
503 goto out_start;
504
505 spin_lock(&tbl->slot_tbl_lock);
506 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
507 goto out_sleep;
508
509 slot = nfs4_alloc_slot(tbl);
510 if (IS_ERR(slot)) {
511 if (slot == ERR_PTR(-ENOMEM))
512 task->tk_timeout = HZ >> 2;
513 goto out_sleep;
514 }
515 spin_unlock(&tbl->slot_tbl_lock);
516
517 args->sa_slot = slot;
518 res->sr_slot = slot;
519
520out_start:
521 rpc_call_start(task);
522 return 0;
523
524out_sleep:
525 if (args->sa_privileged)
526 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
527 NULL, RPC_PRIORITY_PRIVILEGED);
528 else
529 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
530 spin_unlock(&tbl->slot_tbl_lock);
531 return -EAGAIN;
532}
Peng Taocb04ad22014-06-11 05:24:15 +0800533EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400534
535static int nfs40_sequence_done(struct rpc_task *task,
536 struct nfs4_sequence_res *res)
537{
538 struct nfs4_slot *slot = res->sr_slot;
539 struct nfs4_slot_table *tbl;
540
Trond Myklebustcab92c12014-01-29 12:12:15 -0500541 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400542 goto out;
543
544 tbl = slot->table;
545 spin_lock(&tbl->slot_tbl_lock);
546 if (!nfs41_wake_and_assign_slot(tbl, slot))
547 nfs4_free_slot(tbl, slot);
548 spin_unlock(&tbl->slot_tbl_lock);
549
550 res->sr_slot = NULL;
551out:
552 return 1;
553}
554
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400555#if defined(CONFIG_NFS_V4_1)
556
Trond Myklebustd185a332010-06-16 09:52:25 -0400557static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400558{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500559 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400560 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500561 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500562 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400563
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500564 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500565 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500566
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500567 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500568 /* Be nice to the server: try to ensure that the last transmitted
569 * value for highest_user_slotid <= target_highest_slotid
570 */
571 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
572 send_new_highest_used_slotid = true;
573
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500574 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500575 send_new_highest_used_slotid = false;
576 goto out_unlock;
577 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500578 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500579
580 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
581 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500582out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500583 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400584 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500585 if (send_new_highest_used_slotid)
586 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400587}
588
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500589int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400590{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500591 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500592 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400593 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500594 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500595 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400596
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500597 if (slot == NULL)
598 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400599 /* don't increment the sequence number if the task wasn't sent */
600 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400601 goto out;
602
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500603 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500604
Trond Myklebustac20d162012-12-15 15:36:07 -0500605 if (slot->interrupted) {
606 slot->interrupted = 0;
607 interrupted = true;
608 }
609
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400610 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500611 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400612 switch (res->sr_status) {
613 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400614 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500615 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500616 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500617 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500618 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500619 if (res->sr_status_flags != 0)
620 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500621 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400622 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500623 case 1:
624 /*
625 * sr_status remains 1 if an RPC level error occurred.
626 * The server may or may not have processed the sequence
627 * operation..
628 * Mark the slot as having hosted an interrupted RPC call.
629 */
630 slot->interrupted = 1;
631 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400632 case -NFS4ERR_DELAY:
633 /* The server detected a resend of the RPC call and
634 * returned NFS4ERR_DELAY as per Section 2.10.6.2
635 * of RFC5661.
636 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500637 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400638 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500639 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500640 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400641 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500642 case -NFS4ERR_BADSLOT:
643 /*
644 * The slot id we used was probably retired. Try again
645 * using a different slot id.
646 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500647 goto retry_nowait;
648 case -NFS4ERR_SEQ_MISORDERED:
649 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500650 * Was the last operation on this sequence interrupted?
651 * If so, retry after bumping the sequence number.
652 */
653 if (interrupted) {
654 ++slot->seq_nr;
655 goto retry_nowait;
656 }
657 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500658 * Could this slot have been previously retired?
659 * If so, then the server may be expecting seq_nr = 1!
660 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500661 if (slot->seq_nr != 1) {
662 slot->seq_nr = 1;
663 goto retry_nowait;
664 }
665 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500666 case -NFS4ERR_SEQ_FALSE_RETRY:
667 ++slot->seq_nr;
668 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400669 default:
670 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500671 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400672 }
673out:
674 /* The session may be reset by one of the error handlers. */
675 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400676 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500677out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500678 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500679retry_nowait:
680 if (rpc_restart_call_prepare(task)) {
681 task->tk_status = 0;
682 ret = 0;
683 }
684 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400685out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400686 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400687 goto out;
688 rpc_delay(task, NFS4_POLL_RETRY_MAX);
689 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400690}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500691EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400692
Peng Tao2c4b1312014-06-11 05:24:16 +0800693int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400694{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500695 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400696 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400697 if (!res->sr_slot->table->session)
698 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400699 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400700}
Peng Tao2c4b1312014-06-11 05:24:16 +0800701EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400702
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000703int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704 struct nfs4_sequence_args *args,
705 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 struct rpc_task *task)
707{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400708 struct nfs4_slot *slot;
709 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400710
711 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400712 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400713 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400714 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400715
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400716 tbl = &session->fc_slot_table;
717
Trond Myklebust69d206b2012-11-22 13:21:02 -0500718 task->tk_timeout = 0;
719
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400720 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400721 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400722 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500723 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500724 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400725 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400726 }
727
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500728 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500729 if (IS_ERR(slot)) {
730 /* If out of memory, try again in 1/4 second */
731 if (slot == ERR_PTR(-ENOMEM))
732 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400733 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400734 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400735 }
736 spin_unlock(&tbl->slot_tbl_lock);
737
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500738 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400739
Chuck Levere8d92382013-08-09 12:47:51 -0400740 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500741 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400742
Benny Halevydfb4f3092010-09-24 09:17:01 -0400743 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500744 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400745 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400746 /*
747 * sr_status is only set in decode_sequence, and so will remain
748 * set to 1 if an rpc level failure occurs.
749 */
750 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400751 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400752out_success:
753 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400754 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400755out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400756 /* Privileged tasks are queued with top priority */
757 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400758 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
759 NULL, RPC_PRIORITY_PRIVILEGED);
760 else
761 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400762 spin_unlock(&tbl->slot_tbl_lock);
763 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400764}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000765EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400766
Chuck Lever5a580e02013-08-09 12:48:09 -0400767static int nfs4_setup_sequence(const struct nfs_server *server,
768 struct nfs4_sequence_args *args,
769 struct nfs4_sequence_res *res,
770 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400771{
Trond Myklebust035168a2010-06-16 09:52:26 -0400772 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400773 int ret = 0;
774
Chuck Lever3bd23842013-08-09 12:49:19 -0400775 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800776 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
777 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400778
Chuck Levere8d92382013-08-09 12:47:51 -0400779 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400780 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400781 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400782
Trond Myklebust9d12b212012-01-17 22:04:25 -0500783 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400784
Andy Adamsonce5039c2009-04-01 09:22:13 -0400785 dprintk("<-- %s status=%d\n", __func__, ret);
786 return ret;
787}
788
Andy Adamsonce5039c2009-04-01 09:22:13 -0400789static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
790{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400791 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400792 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400793
Trond Myklebust035168a2010-06-16 09:52:26 -0400794 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
795
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400796 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400797}
798
Andy Adamson69ab40c2009-04-01 09:22:19 -0400799static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
800{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400801 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400802
Trond Myklebust14516c32010-07-31 14:29:06 -0400803 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804}
805
Trond Myklebust17280172012-03-11 13:11:00 -0400806static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400807 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400808 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809};
810
Chuck Lever3bd23842013-08-09 12:49:19 -0400811#else /* !CONFIG_NFS_V4_1 */
812
Chuck Lever5a580e02013-08-09 12:48:09 -0400813static int nfs4_setup_sequence(const struct nfs_server *server,
814 struct nfs4_sequence_args *args,
815 struct nfs4_sequence_res *res,
816 struct rpc_task *task)
817{
Peng Taocb04ad22014-06-11 05:24:15 +0800818 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
819 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400820}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400821
Peng Tao2c4b1312014-06-11 05:24:16 +0800822int nfs4_sequence_done(struct rpc_task *task,
823 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400824{
Chuck Lever3bd23842013-08-09 12:49:19 -0400825 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400826}
Peng Tao2c4b1312014-06-11 05:24:16 +0800827EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400828
829#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400830
Chuck Lever9915ea72013-08-09 12:48:27 -0400831static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
832{
833 struct nfs4_call_sync_data *data = calldata;
834 nfs4_setup_sequence(data->seq_server,
835 data->seq_args, data->seq_res, task);
836}
837
838static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
839{
840 struct nfs4_call_sync_data *data = calldata;
841 nfs4_sequence_done(task, data->seq_res);
842}
843
844static const struct rpc_call_ops nfs40_call_sync_ops = {
845 .rpc_call_prepare = nfs40_call_sync_prepare,
846 .rpc_call_done = nfs40_call_sync_done,
847};
848
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400849static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400850 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 struct nfs4_sequence_args *args,
853 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400854{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 int ret;
856 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400857 struct nfs_client *clp = server->nfs_client;
858 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 .seq_server = server,
860 .seq_args = args,
861 .seq_res = res,
862 };
863 struct rpc_task_setup task_setup = {
864 .rpc_client = clnt,
865 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400866 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867 .callback_data = &data
868 };
869
870 task = rpc_run_task(&task_setup);
871 if (IS_ERR(task))
872 ret = PTR_ERR(task);
873 else {
874 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400875 rpc_put_task(task);
876 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100877 return ret;
878}
879
Bryan Schumaker7c513052011-03-24 17:12:24 +0000880int nfs4_call_sync(struct rpc_clnt *clnt,
881 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000882 struct rpc_message *msg,
883 struct nfs4_sequence_args *args,
884 struct nfs4_sequence_res *res,
885 int cache_reply)
886{
Chuck Levera9c92d62013-08-09 12:48:18 -0400887 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400888 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000889}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
892{
893 struct nfs_inode *nfsi = NFS_I(dir);
894
895 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400896 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700897 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700899 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500900 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000901 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 spin_unlock(&dir->i_lock);
903}
904
905struct nfs4_opendata {
906 struct kref kref;
907 struct nfs_openargs o_arg;
908 struct nfs_openres o_res;
909 struct nfs_open_confirmargs c_arg;
910 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500911 struct nfs4_string owner_name;
912 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400914 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400916 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct nfs4_state_owner *owner;
918 struct nfs4_state *state;
919 struct iattr attrs;
920 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400921 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400922 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400923 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400924 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int cancelled;
926};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400927
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400928static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
929 int err, struct nfs4_exception *exception)
930{
931 if (err != -EINVAL)
932 return false;
933 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
934 return false;
935 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
936 exception->retry = 1;
937 return true;
938}
939
Trond Myklebust6ae37332015-01-30 14:21:14 -0500940static u32
941nfs4_map_atomic_open_share(struct nfs_server *server,
942 fmode_t fmode, int openflags)
943{
944 u32 res = 0;
945
946 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
947 case FMODE_READ:
948 res = NFS4_SHARE_ACCESS_READ;
949 break;
950 case FMODE_WRITE:
951 res = NFS4_SHARE_ACCESS_WRITE;
952 break;
953 case FMODE_READ|FMODE_WRITE:
954 res = NFS4_SHARE_ACCESS_BOTH;
955 }
956 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
957 goto out;
958 /* Want no delegation if we're using O_DIRECT */
959 if (openflags & O_DIRECT)
960 res |= NFS4_SHARE_WANT_NO_DELEG;
961out:
962 return res;
963}
964
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400965static enum open_claim_type4
966nfs4_map_atomic_open_claim(struct nfs_server *server,
967 enum open_claim_type4 claim)
968{
969 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
970 return claim;
971 switch (claim) {
972 default:
973 return claim;
974 case NFS4_OPEN_CLAIM_FH:
975 return NFS4_OPEN_CLAIM_NULL;
976 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
977 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
978 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
979 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
980 }
981}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100984{
985 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400986 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100987 p->o_res.seqid = p->o_arg.seqid;
988 p->c_res.seqid = p->c_arg.seqid;
989 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400990 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100991 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500992 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100993}
994
Al Viro82a2c1b2011-06-22 18:30:55 -0400995static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100996 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400997 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400998 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400999 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001000 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001001{
Al Viro82a2c1b2011-06-22 18:30:55 -04001002 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001003 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001004 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001005 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 struct nfs4_opendata *p;
1007
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001008 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001009 if (p == NULL)
1010 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001011
1012 p->f_label = nfs4_label_alloc(server, gfp_mask);
1013 if (IS_ERR(p->f_label))
1014 goto err_free_p;
1015
Trond Myklebust63f5f792015-01-23 19:19:25 -05001016 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1017 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001018 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001019 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001020 nfs_sb_active(dentry->d_sb);
1021 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001022 p->dir = parent;
1023 p->owner = sp;
1024 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 p->o_arg.open_flags = flags;
1026 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001027 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1028 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001029 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1030 * will return permission denied for all bits until close */
1031 if (!(flags & O_EXCL)) {
1032 /* ask server to check for all possible rights as results
1033 * are cached */
1034 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1035 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001036 }
David Howells7539bba2006-08-22 20:06:09 -04001037 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001038 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1039 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001040 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001041 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001042 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001043 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001044 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001045 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1046 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001047 case NFS4_OPEN_CLAIM_NULL:
1048 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1049 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1050 p->o_arg.fh = NFS_FH(dir);
1051 break;
1052 case NFS4_OPEN_CLAIM_PREVIOUS:
1053 case NFS4_OPEN_CLAIM_FH:
1054 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1055 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001056 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001057 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001058 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001059 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001060
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001061 p->o_arg.u.attrs = &p->attrs;
1062 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001063
1064 verf[0] = jiffies;
1065 verf[1] = current->pid;
1066 memcpy(p->o_arg.u.verifier.data, verf,
1067 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001068 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001069 p->c_arg.fh = &p->o_res.fh;
1070 p->c_arg.stateid = &p->o_res.stateid;
1071 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001072 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001073 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001074 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001075
1076err_free_label:
1077 nfs4_label_free(p->f_label);
1078err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001079 kfree(p);
1080err:
1081 dput(parent);
1082 return NULL;
1083}
1084
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001085static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001086{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001087 struct nfs4_opendata *p = container_of(kref,
1088 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001089 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001090
1091 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001092 if (p->state != NULL)
1093 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001094 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001095
1096 nfs4_label_free(p->f_label);
1097
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001098 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001099 dput(p->dentry);
1100 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001101 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001102 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001103 kfree(p);
1104}
1105
1106static void nfs4_opendata_put(struct nfs4_opendata *p)
1107{
1108 if (p != NULL)
1109 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110}
1111
Trond Myklebust06f814a2006-01-03 09:55:07 +01001112static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1113{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001114 int ret;
1115
Trond Myklebust06f814a2006-01-03 09:55:07 +01001116 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001117 return ret;
1118}
1119
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001121{
1122 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001123
Trond Myklebust536e43d2012-01-17 22:04:26 -05001124 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125 goto out;
1126 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001127 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001128 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1129 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001130 break;
1131 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001132 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1133 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001134 break;
1135 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001136 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1137 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001138 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001140 return ret;
1141}
1142
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001144{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001145 if (delegation == NULL)
1146 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001147 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001148 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001149 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1150 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001151 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001152 return 1;
1153}
1154
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001156{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001158 case FMODE_WRITE:
1159 state->n_wronly++;
1160 break;
1161 case FMODE_READ:
1162 state->n_rdonly++;
1163 break;
1164 case FMODE_READ|FMODE_WRITE:
1165 state->n_rdwr++;
1166 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001168}
1169
Trond Myklebust4f14c192014-02-12 19:15:06 -05001170static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001171{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001172 struct nfs_client *clp = state->owner->so_server->nfs_client;
1173 bool need_recover = false;
1174
1175 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1176 need_recover = true;
1177 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1178 need_recover = true;
1179 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1180 need_recover = true;
1181 if (need_recover)
1182 nfs4_state_mark_reclaim_nograce(clp, state);
1183}
1184
Trond Myklebuste999e802014-02-10 18:20:47 -05001185static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1186 nfs4_stateid *stateid)
1187{
1188 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1189 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001190 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1191 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001192 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001193 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001194 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1195 return true;
1196 return false;
1197}
1198
Trond Myklebustf95549c2015-01-23 18:06:09 -05001199static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1200{
1201 if (state->n_wronly)
1202 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1203 if (state->n_rdonly)
1204 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1205 if (state->n_rdwr)
1206 set_bit(NFS_O_RDWR_STATE, &state->flags);
1207}
1208
Trond Myklebust226056c2014-02-11 10:41:07 -05001209static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1210 nfs4_stateid *stateid, fmode_t fmode)
1211{
1212 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1213 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1214 case FMODE_WRITE:
1215 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1216 break;
1217 case FMODE_READ:
1218 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1219 break;
1220 case 0:
1221 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1222 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1223 clear_bit(NFS_OPEN_STATE, &state->flags);
1224 }
1225 if (stateid == NULL)
1226 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001227 /* Handle races with OPEN */
1228 if (!nfs4_stateid_match_other(stateid, &state->open_stateid) ||
1229 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1230 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001231 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001232 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001233 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001234 nfs4_stateid_copy(&state->stateid, stateid);
1235 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001236}
1237
1238static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1239{
1240 write_seqlock(&state->seqlock);
1241 nfs_clear_open_stateid_locked(state, stateid, fmode);
1242 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001243 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1244 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001245}
1246
Trond Myklebust003707c2007-07-05 18:07:55 -04001247static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1248{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001249 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001250 case FMODE_READ:
1251 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1252 break;
1253 case FMODE_WRITE:
1254 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1255 break;
1256 case FMODE_READ|FMODE_WRITE:
1257 set_bit(NFS_O_RDWR_STATE, &state->flags);
1258 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001259 if (!nfs_need_update_open_stateid(state, stateid))
1260 return;
1261 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1262 nfs4_stateid_copy(&state->stateid, stateid);
1263 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001264}
1265
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001266static 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 -07001267{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001268 /*
1269 * Protect the call to nfs4_state_set_mode_locked and
1270 * serialise the stateid update
1271 */
1272 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001273 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001274 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001275 set_bit(NFS_DELEGATED_STATE, &state->flags);
1276 }
1277 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001279 write_sequnlock(&state->seqlock);
1280 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001281 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001282 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001285static 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 -05001286{
1287 struct nfs_inode *nfsi = NFS_I(state->inode);
1288 struct nfs_delegation *deleg_cur;
1289 int ret = 0;
1290
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001292
1293 rcu_read_lock();
1294 deleg_cur = rcu_dereference(nfsi->delegation);
1295 if (deleg_cur == NULL)
1296 goto no_delegation;
1297
1298 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001299 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001300 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001301 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001302 goto no_delegation_unlock;
1303
1304 if (delegation == NULL)
1305 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001306 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001307 goto no_delegation_unlock;
1308
Trond Myklebustb7391f42008-12-23 15:21:52 -05001309 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001310 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001311 ret = 1;
1312no_delegation_unlock:
1313 spin_unlock(&deleg_cur->lock);
1314no_delegation:
1315 rcu_read_unlock();
1316
1317 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001318 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001319 ret = 1;
1320 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001321 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1322 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001323
1324 return ret;
1325}
1326
Trond Myklebust39071e62015-01-24 15:07:56 -05001327static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1328 const nfs4_stateid *stateid)
1329{
1330 struct nfs4_state *state = lsp->ls_state;
1331 bool ret = false;
1332
1333 spin_lock(&state->state_lock);
1334 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1335 goto out_noupdate;
1336 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1337 goto out_noupdate;
1338 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1339 ret = true;
1340out_noupdate:
1341 spin_unlock(&state->state_lock);
1342 return ret;
1343}
Trond Myklebust34310432008-12-23 15:21:38 -05001344
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001345static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001346{
1347 struct nfs_delegation *delegation;
1348
1349 rcu_read_lock();
1350 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001352 rcu_read_unlock();
1353 return;
1354 }
1355 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001356 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001357}
1358
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001360{
1361 struct nfs4_state *state = opendata->state;
1362 struct nfs_inode *nfsi = NFS_I(state->inode);
1363 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001364 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001365 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001366 nfs4_stateid stateid;
1367 int ret = -EAGAIN;
1368
Trond Myklebustaac00a82007-07-05 19:02:21 -04001369 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001370 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001371 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001372 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001373 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001374 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001375 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001376 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001377 rcu_read_lock();
1378 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001379 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001380 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001381 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001382 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001383 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001384 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001385 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001386 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001387 if (!opendata->is_recover) {
1388 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1389 if (ret != 0)
1390 goto out;
1391 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001392 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001393
1394 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001395 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001396 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001397 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001398out:
1399 return ERR_PTR(ret);
1400out_return_state:
1401 atomic_inc(&state->count);
1402 return state;
1403}
1404
Andy Adamsone23008e2012-10-02 21:07:32 -04001405static void
1406nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1407{
1408 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1409 struct nfs_delegation *delegation;
1410 int delegation_flags = 0;
1411
1412 rcu_read_lock();
1413 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1414 if (delegation)
1415 delegation_flags = delegation->flags;
1416 rcu_read_unlock();
1417 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1418 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1419 "returning a delegation for "
1420 "OPEN(CLAIM_DELEGATE_CUR)\n",
1421 clp->cl_hostname);
1422 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1423 nfs_inode_set_delegation(state->inode,
1424 data->owner->so_cred,
1425 &data->o_res);
1426 else
1427 nfs_inode_reclaim_delegation(state->inode,
1428 data->owner->so_cred,
1429 &data->o_res);
1430}
1431
1432/*
1433 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1434 * and update the nfs4_state.
1435 */
1436static struct nfs4_state *
1437_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1438{
1439 struct inode *inode = data->state->inode;
1440 struct nfs4_state *state = data->state;
1441 int ret;
1442
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001443 if (!data->rpc_done) {
1444 if (data->rpc_status) {
1445 ret = data->rpc_status;
1446 goto err;
1447 }
1448 /* cached opens have already been processed */
1449 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001450 }
1451
Andy Adamsone23008e2012-10-02 21:07:32 -04001452 ret = nfs_refresh_inode(inode, &data->f_attr);
1453 if (ret)
1454 goto err;
1455
1456 if (data->o_res.delegation_type != 0)
1457 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001458update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001459 update_open_stateid(state, &data->o_res.stateid, NULL,
1460 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001461 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001462
1463 return state;
1464err:
1465 return ERR_PTR(ret);
1466
1467}
1468
1469static struct nfs4_state *
1470_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471{
1472 struct inode *inode;
1473 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001474 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475
Trond Myklebustaac00a82007-07-05 19:02:21 -04001476 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001477 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001478 goto out;
1479 }
1480
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001481 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001483 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001484 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001485 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001486 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001487 goto err;
1488 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489 state = nfs4_get_open_state(inode, data->owner);
1490 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001491 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001492 if (data->o_res.delegation_type != 0)
1493 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001494 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001495 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001497out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001498 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001499 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001500err_put_inode:
1501 iput(inode);
1502err:
1503 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504}
1505
Andy Adamsone23008e2012-10-02 21:07:32 -04001506static struct nfs4_state *
1507nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1508{
1509 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1510 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1511 return _nfs4_opendata_to_nfs4_state(data);
1512}
1513
Trond Myklebust864472e2006-01-03 09:55:15 +01001514static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1515{
1516 struct nfs_inode *nfsi = NFS_I(state->inode);
1517 struct nfs_open_context *ctx;
1518
1519 spin_lock(&state->inode->i_lock);
1520 list_for_each_entry(ctx, &nfsi->open_files, list) {
1521 if (ctx->state != state)
1522 continue;
1523 get_nfs_open_context(ctx);
1524 spin_unlock(&state->inode->i_lock);
1525 return ctx;
1526 }
1527 spin_unlock(&state->inode->i_lock);
1528 return ERR_PTR(-ENOENT);
1529}
1530
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001531static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1532 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001533{
1534 struct nfs4_opendata *opendata;
1535
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001536 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001537 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001538 if (opendata == NULL)
1539 return ERR_PTR(-ENOMEM);
1540 opendata->state = state;
1541 atomic_inc(&state->count);
1542 return opendata;
1543}
1544
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001545static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001546{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001547 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001548 int ret;
1549
NeilBrown39f897f2015-06-29 14:28:54 +10001550 if ((opendata->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
1551 opendata->o_arg.claim == NFS4_OPEN_CLAIM_DELEG_CUR_FH) &&
1552 (opendata->o_arg.u.delegation_type & fmode) != fmode)
1553 /* This mode can't have been delegated, so we must have
1554 * a valid open_stateid to cover it - not need to reclaim.
1555 */
1556 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001557 opendata->o_arg.open_flags = 0;
1558 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001559 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1560 NFS_SB(opendata->dentry->d_sb),
1561 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001562 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1563 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1564 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001565 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001566 if (ret != 0)
1567 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001568 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001569 if (IS_ERR(newstate))
1570 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001571 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001572 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001573 return 0;
1574}
1575
1576static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1577{
Trond Myklebust864472e2006-01-03 09:55:15 +01001578 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001579 int ret;
1580
Trond Myklebust4f14c192014-02-12 19:15:06 -05001581 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1582 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1583 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1584 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001585 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001586 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001587 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001588 smp_rmb();
1589 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001590 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001591 if (ret != 0)
1592 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001593 if (newstate != state)
1594 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001595 }
1596 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001597 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001598 if (ret != 0)
1599 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001600 if (newstate != state)
1601 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001602 }
1603 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001604 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001605 if (ret != 0)
1606 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001607 if (newstate != state)
1608 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001609 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001610 /*
1611 * We may have performed cached opens for all three recoveries.
1612 * Check if we need to update the current stateid.
1613 */
1614 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001615 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001616 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001617 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001618 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001619 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001620 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001621 return 0;
1622}
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624/*
1625 * OPEN_RECLAIM:
1626 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001628static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001630 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001631 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001632 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 int status;
1634
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001635 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1636 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001637 if (IS_ERR(opendata))
1638 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001639 rcu_read_lock();
1640 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001641 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001642 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001643 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001644 opendata->o_arg.u.delegation_type = delegation_type;
1645 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001646 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return status;
1648}
1649
Trond Myklebust539cd032007-06-05 11:46:42 -04001650static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 struct nfs_server *server = NFS_SERVER(state->inode);
1653 struct nfs4_exception exception = { };
1654 int err;
1655 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001656 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001657 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001658 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1659 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001660 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001661 break;
1662 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 } while (exception.retry);
1664 return err;
1665}
1666
Trond Myklebust864472e2006-01-03 09:55:15 +01001667static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1668{
1669 struct nfs_open_context *ctx;
1670 int ret;
1671
1672 ctx = nfs4_state_find_open_context(state);
1673 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001674 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001675 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001676 put_nfs_open_context(ctx);
1677 return ret;
1678}
1679
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001680static 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 -07001681{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001682 switch (err) {
1683 default:
1684 printk(KERN_ERR "NFS: %s: unhandled error "
1685 "%d.\n", __func__, err);
1686 case 0:
1687 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001688 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001689 case -ESTALE:
1690 break;
1691 case -NFS4ERR_BADSESSION:
1692 case -NFS4ERR_BADSLOT:
1693 case -NFS4ERR_BAD_HIGH_SLOT:
1694 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1695 case -NFS4ERR_DEADSESSION:
1696 set_bit(NFS_DELEGATED_STATE, &state->flags);
1697 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1698 return -EAGAIN;
1699 case -NFS4ERR_STALE_CLIENTID:
1700 case -NFS4ERR_STALE_STATEID:
1701 set_bit(NFS_DELEGATED_STATE, &state->flags);
1702 case -NFS4ERR_EXPIRED:
1703 /* Don't recall a delegation if it was lost */
1704 nfs4_schedule_lease_recovery(server->nfs_client);
1705 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001706 case -NFS4ERR_MOVED:
1707 nfs4_schedule_migration_recovery(server);
1708 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001709 case -NFS4ERR_LEASE_MOVED:
1710 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1711 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001712 case -NFS4ERR_DELEG_REVOKED:
1713 case -NFS4ERR_ADMIN_REVOKED:
1714 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001715 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001716 nfs_inode_find_state_and_recover(state->inode,
1717 stateid);
1718 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001719 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001720 case -NFS4ERR_DELAY:
1721 case -NFS4ERR_GRACE:
1722 set_bit(NFS_DELEGATED_STATE, &state->flags);
1723 ssleep(1);
1724 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001725 case -ENOMEM:
1726 case -NFS4ERR_DENIED:
1727 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1728 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return err;
1731}
1732
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001733int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1734{
1735 struct nfs_server *server = NFS_SERVER(state->inode);
1736 struct nfs4_opendata *opendata;
1737 int err;
1738
1739 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1740 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1741 if (IS_ERR(opendata))
1742 return PTR_ERR(opendata);
1743 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1744 err = nfs4_open_recover(opendata, state);
1745 nfs4_opendata_put(opendata);
1746 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1747}
1748
Chuck Leverbe05c862013-08-09 12:49:47 -04001749static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1750{
1751 struct nfs4_opendata *data = calldata;
1752
Peng Taocb04ad22014-06-11 05:24:15 +08001753 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1754 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001755}
1756
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001757static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1758{
1759 struct nfs4_opendata *data = calldata;
1760
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001761 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001762
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001763 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001764 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001765 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001766 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001767 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001768 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001769 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001770}
1771
1772static void nfs4_open_confirm_release(void *calldata)
1773{
1774 struct nfs4_opendata *data = calldata;
1775 struct nfs4_state *state = NULL;
1776
1777 /* If this request hasn't been cancelled, do nothing */
1778 if (data->cancelled == 0)
1779 goto out_free;
1780 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001781 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001782 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001783 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001784 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001785 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001786out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001787 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001788}
1789
1790static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001791 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001792 .rpc_call_done = nfs4_open_confirm_done,
1793 .rpc_release = nfs4_open_confirm_release,
1794};
1795
1796/*
1797 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1798 */
1799static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1800{
David Howells2b0143b2015-03-17 22:25:59 +00001801 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001802 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001803 struct rpc_message msg = {
1804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1805 .rpc_argp = &data->c_arg,
1806 .rpc_resp = &data->c_res,
1807 .rpc_cred = data->owner->so_cred,
1808 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001809 struct rpc_task_setup task_setup_data = {
1810 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001811 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001812 .callback_ops = &nfs4_open_confirm_ops,
1813 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001814 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001815 .flags = RPC_TASK_ASYNC,
1816 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 int status;
1818
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001819 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001820 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001821 data->rpc_done = 0;
1822 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001823 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001824 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001825 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001826 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001827 status = nfs4_wait_for_completion_rpc_task(task);
1828 if (status != 0) {
1829 data->cancelled = 1;
1830 smp_wmb();
1831 } else
1832 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001833 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return status;
1835}
1836
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001837static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001839 struct nfs4_opendata *data = calldata;
1840 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001841 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001842
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001843 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001844 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001845 /*
1846 * Check if we still need to send an OPEN call, or if we can use
1847 * a delegation instead.
1848 */
1849 if (data->state != NULL) {
1850 struct nfs_delegation *delegation;
1851
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001852 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001853 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001854 rcu_read_lock();
1855 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001856 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001857 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001858 can_open_delegated(delegation, data->o_arg.fmode))
1859 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001860 rcu_read_unlock();
1861 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001862 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001863 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001864 switch (data->o_arg.claim) {
1865 case NFS4_OPEN_CLAIM_PREVIOUS:
1866 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1867 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001868 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001869 case NFS4_OPEN_CLAIM_FH:
1870 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001871 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1872 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001873 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001874 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001875 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001876 &data->o_res.seq_res,
1877 task) != 0)
1878 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001879
1880 /* Set the create mode (note dependency on the session type) */
1881 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1882 if (data->o_arg.open_flags & O_EXCL) {
1883 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1884 if (nfs4_has_persistent_session(clp))
1885 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1886 else if (clp->cl_mvops->minor_version > 0)
1887 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1888 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001889 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001890unlock_no_action:
1891 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001892out_no_action:
1893 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001894out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001895 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001896}
1897
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001898static void nfs4_open_done(struct rpc_task *task, void *calldata)
1899{
1900 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001902 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001903
Trond Myklebust14516c32010-07-31 14:29:06 -04001904 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1905 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001906
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001907 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001908 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1909 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001910 case S_IFREG:
1911 break;
1912 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001913 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001914 break;
1915 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001916 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001917 break;
1918 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001919 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001920 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001921 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001922 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001923 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1924 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001925 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001926 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001927}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001928
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001929static void nfs4_open_release(void *calldata)
1930{
1931 struct nfs4_opendata *data = calldata;
1932 struct nfs4_state *state = NULL;
1933
1934 /* If this request hasn't been cancelled, do nothing */
1935 if (data->cancelled == 0)
1936 goto out_free;
1937 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001938 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001939 goto out_free;
1940 /* In case we need an open_confirm, no cleanup! */
1941 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1942 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001943 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001944 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001945 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001946out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001947 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001948}
1949
1950static const struct rpc_call_ops nfs4_open_ops = {
1951 .rpc_call_prepare = nfs4_open_prepare,
1952 .rpc_call_done = nfs4_open_done,
1953 .rpc_release = nfs4_open_release,
1954};
1955
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001956static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001957{
David Howells2b0143b2015-03-17 22:25:59 +00001958 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001959 struct nfs_server *server = NFS_SERVER(dir);
1960 struct nfs_openargs *o_arg = &data->o_arg;
1961 struct nfs_openres *o_res = &data->o_res;
1962 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001963 struct rpc_message msg = {
1964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1965 .rpc_argp = o_arg,
1966 .rpc_resp = o_res,
1967 .rpc_cred = data->owner->so_cred,
1968 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001969 struct rpc_task_setup task_setup_data = {
1970 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001971 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001972 .callback_ops = &nfs4_open_ops,
1973 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001974 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001975 .flags = RPC_TASK_ASYNC,
1976 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001977 int status;
1978
Chuck Levera9c92d62013-08-09 12:48:18 -04001979 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001980 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001981 data->rpc_done = 0;
1982 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001983 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001984 data->is_recover = 0;
1985 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001986 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001987 data->is_recover = 1;
1988 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001989 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001990 if (IS_ERR(task))
1991 return PTR_ERR(task);
1992 status = nfs4_wait_for_completion_rpc_task(task);
1993 if (status != 0) {
1994 data->cancelled = 1;
1995 smp_wmb();
1996 } else
1997 status = data->rpc_status;
1998 rpc_put_task(task);
1999
2000 return status;
2001}
2002
2003static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2004{
David Howells2b0143b2015-03-17 22:25:59 +00002005 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002006 struct nfs_openres *o_res = &data->o_res;
2007 int status;
2008
2009 status = nfs4_run_open_task(data, 1);
2010 if (status != 0 || !data->rpc_done)
2011 return status;
2012
Trond Myklebust6926afd2012-01-07 13:22:46 -05002013 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2014
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002015 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2016 status = _nfs4_proc_open_confirm(data);
2017 if (status != 0)
2018 return status;
2019 }
2020
2021 return status;
2022}
2023
Trond Myklebustf3792d62014-07-10 08:54:32 -04002024/*
2025 * Additional permission checks in order to distinguish between an
2026 * open for read, and an open for execute. This works around the
2027 * fact that NFSv4 OPEN treats read and execute permissions as being
2028 * the same.
2029 * Note that in the non-execute case, we want to turn off permission
2030 * checking if we just created a new file (POSIX open() semantics).
2031 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002032static int nfs4_opendata_access(struct rpc_cred *cred,
2033 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002034 struct nfs4_state *state, fmode_t fmode,
2035 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002036{
2037 struct nfs_access_entry cache;
2038 u32 mask;
2039
2040 /* access call failed or for some reason the server doesn't
2041 * support any access modes -- defer access call until later */
2042 if (opendata->o_res.access_supported == 0)
2043 return 0;
2044
2045 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002046 /*
2047 * Use openflags to check for exec, because fmode won't
2048 * always have FMODE_EXEC set when file open for exec.
2049 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002050 if (openflags & __FMODE_EXEC) {
2051 /* ONLY check for exec rights */
2052 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002053 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002054 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002055
2056 cache.cred = cred;
2057 cache.jiffies = jiffies;
2058 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2059 nfs_access_add_cache(state->inode, &cache);
2060
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002061 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002062 return 0;
2063
2064 /* even though OPEN succeeded, access is denied. Close the file */
2065 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002066 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002067}
2068
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002069/*
2070 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2071 */
2072static int _nfs4_proc_open(struct nfs4_opendata *data)
2073{
David Howells2b0143b2015-03-17 22:25:59 +00002074 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002075 struct nfs_server *server = NFS_SERVER(dir);
2076 struct nfs_openargs *o_arg = &data->o_arg;
2077 struct nfs_openres *o_res = &data->o_res;
2078 int status;
2079
2080 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002081 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002082 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002083 if (status != 0) {
2084 if (status == -NFS4ERR_BADNAME &&
2085 !(o_arg->open_flags & O_CREAT))
2086 return -ENOENT;
2087 return status;
2088 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002089
Trond Myklebust6926afd2012-01-07 13:22:46 -05002090 nfs_fattr_map_and_free_names(server, &data->f_attr);
2091
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002092 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002093 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002094 if (o_arg->open_flags & O_EXCL)
2095 data->file_created = 1;
2096 else if (o_res->cinfo.before != o_res->cinfo.after)
2097 data->file_created = 1;
2098 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002099 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2100 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002102 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002104 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002107 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
Andy Adamsond83217c2011-03-01 01:34:17 +00002111static int nfs4_recover_expired_lease(struct nfs_server *server)
2112{
2113 return nfs4_client_recover_expired_lease(server->nfs_client);
2114}
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116/*
2117 * OPEN_EXPIRED:
2118 * reclaim state on the server after a network partition.
2119 * Assumes caller holds the appropriate lock
2120 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002121static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002123 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002124 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002126 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002127 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002128 if (IS_ERR(opendata))
2129 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002130 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002131 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002132 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002133 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002134 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002137static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002138{
Trond Myklebust539cd032007-06-05 11:46:42 -04002139 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002140 struct nfs4_exception exception = { };
2141 int err;
2142
2143 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002144 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002145 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002146 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2147 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002148 switch (err) {
2149 default:
2150 goto out;
2151 case -NFS4ERR_GRACE:
2152 case -NFS4ERR_DELAY:
2153 nfs4_handle_exception(server, err, &exception);
2154 err = 0;
2155 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002156 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002157out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002158 return err;
2159}
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002164 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Trond Myklebust864472e2006-01-03 09:55:15 +01002166 ctx = nfs4_state_find_open_context(state);
2167 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002168 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002169 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002170 put_nfs_open_context(ctx);
2171 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002174static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2175{
2176 nfs_remove_bad_delegation(state->inode);
2177 write_seqlock(&state->seqlock);
2178 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2179 write_sequnlock(&state->seqlock);
2180 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2181}
2182
2183static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2184{
2185 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2186 nfs_finish_clear_delegation_stateid(state);
2187}
2188
2189static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2190{
2191 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2192 nfs40_clear_delegation_stateid(state);
2193 return nfs4_open_expired(sp, state);
2194}
2195
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002196#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002197static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002198{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002199 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002200 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002201 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002202 struct rpc_cred *cred;
2203 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002204
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002205 /* Get the delegation credential for use by test/free_stateid */
2206 rcu_read_lock();
2207 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002208 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002209 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002210 return;
2211 }
2212
2213 nfs4_stateid_copy(&stateid, &delegation->stateid);
2214 cred = get_rpccred(delegation->cred);
2215 rcu_read_unlock();
2216 status = nfs41_test_stateid(server, &stateid, cred);
2217 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002218
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002219 if (status != NFS_OK) {
2220 /* Free the stateid unless the server explicitly
2221 * informs us the stateid is unrecognized. */
2222 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002223 nfs41_free_stateid(server, &stateid, cred);
2224 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002225 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002226
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002227 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002228}
2229
2230/**
2231 * nfs41_check_open_stateid - possibly free an open stateid
2232 *
2233 * @state: NFSv4 state for an inode
2234 *
2235 * Returns NFS_OK if recovery for this stateid is now finished.
2236 * Otherwise a negative NFS4ERR value is returned.
2237 */
2238static int nfs41_check_open_stateid(struct nfs4_state *state)
2239{
2240 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002241 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002242 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002243 int status;
2244
2245 /* If a state reset has been done, test_stateid is unneeded */
2246 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2247 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2248 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2249 return -NFS4ERR_BAD_STATEID;
2250
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002251 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002252 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002253 if (status != NFS_OK) {
2254 /* Free the stateid unless the server explicitly
2255 * informs us the stateid is unrecognized. */
2256 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002257 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002258
2259 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2260 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2261 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002262 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002263 }
2264 return status;
2265}
2266
2267static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2268{
Chuck Levereb64cf92012-07-11 16:30:05 -04002269 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002270
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002271 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002272 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002273 if (status != NFS_OK)
2274 status = nfs4_open_expired(sp, state);
2275 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002276}
2277#endif
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002280 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2281 * fields corresponding to attributes that were used to store the verifier.
2282 * Make sure we clobber those fields in the later setattr call
2283 */
2284static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2285{
2286 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2287 !(sattr->ia_valid & ATTR_ATIME_SET))
2288 sattr->ia_valid |= ATTR_ATIME;
2289
2290 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2291 !(sattr->ia_valid & ATTR_MTIME_SET))
2292 sattr->ia_valid |= ATTR_MTIME;
2293}
2294
Trond Myklebustc21443c2013-02-07 14:26:21 -05002295static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2296 fmode_t fmode,
2297 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002298 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002299{
2300 struct nfs4_state_owner *sp = opendata->owner;
2301 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002302 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002303 struct nfs4_state *state;
2304 unsigned int seq;
2305 int ret;
2306
2307 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2308
2309 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002310 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002311 goto out;
2312
2313 state = nfs4_opendata_to_nfs4_state(opendata);
2314 ret = PTR_ERR(state);
2315 if (IS_ERR(state))
2316 goto out;
2317 if (server->caps & NFS_CAP_POSIX_LOCK)
2318 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2319
Trond Myklebust275bb302013-05-29 13:11:28 -04002320 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002321 if (d_really_is_negative(dentry)) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002322 /* FIXME: Is this d_drop() ever needed? */
2323 d_drop(dentry);
2324 dentry = d_add_unique(dentry, igrab(state->inode));
2325 if (dentry == NULL) {
2326 dentry = opendata->dentry;
2327 } else if (dentry != ctx->dentry) {
2328 dput(ctx->dentry);
2329 ctx->dentry = dget(dentry);
2330 }
2331 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002332 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002333 }
2334
Trond Myklebustc21443c2013-02-07 14:26:21 -05002335 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2336 if (ret != 0)
2337 goto out;
2338
Trond Myklebust3efb9722013-05-29 13:17:04 -04002339 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002340 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002341 nfs_inode_attach_open_context(ctx);
2342 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2343 nfs4_schedule_stateid_recovery(server, state);
2344 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002345out:
2346 return ret;
2347}
2348
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002349/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002350 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 */
Andy Adamson82be4172012-05-23 05:02:35 -04002352static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002353 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002354 int flags,
2355 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002356 struct nfs4_label *label,
2357 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 struct nfs4_state_owner *sp;
2360 struct nfs4_state *state = NULL;
2361 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002362 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002363 struct dentry *dentry = ctx->dentry;
2364 struct rpc_cred *cred = ctx->cred;
2365 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2366 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002367 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002368 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002369 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002373 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2374 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2376 goto out_err;
2377 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002378 status = nfs4_recover_expired_lease(server);
2379 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002380 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002381 if (d_really_is_positive(dentry))
2382 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002383 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002384 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002385 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002386 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002387 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002388 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002389 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
David Quigley14c43f72013-05-22 12:50:43 -04002391 if (label) {
2392 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2393 if (IS_ERR(olabel)) {
2394 status = PTR_ERR(olabel);
2395 goto err_opendata_put;
2396 }
2397 }
2398
Trond Myklebuste911b812014-03-26 13:24:37 -07002399 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2400 if (!opendata->f_attr.mdsthreshold) {
2401 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2402 if (!opendata->f_attr.mdsthreshold)
2403 goto err_free_label;
2404 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002405 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002406 }
David Howells2b0143b2015-03-17 22:25:59 +00002407 if (d_really_is_positive(dentry))
2408 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002409
Trond Myklebust3efb9722013-05-29 13:17:04 -04002410 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002411 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002412 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002413 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002414
Trond Myklebust549b19c2013-04-16 18:42:34 -04002415 if ((opendata->o_arg.open_flags & O_EXCL) &&
2416 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002417 nfs4_exclusive_attrset(opendata, sattr);
2418
2419 nfs_fattr_init(opendata->o_res.f_attr);
2420 status = nfs4_do_setattr(state->inode, cred,
2421 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002422 state, label, olabel);
2423 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05002424 nfs_setattr_update_inode(state->inode, sattr,
2425 opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002426 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002427 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002428 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002429 if (opendata->file_created)
2430 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002431
Trond Myklebuste911b812014-03-26 13:24:37 -07002432 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002433 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002434 opendata->f_attr.mdsthreshold = NULL;
2435 }
Andy Adamson82be4172012-05-23 05:02:35 -04002436
David Quigley14c43f72013-05-22 12:50:43 -04002437 nfs4_label_free(olabel);
2438
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002439 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002442err_free_label:
2443 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002444err_opendata_put:
2445 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002446err_put_state_owner:
2447 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return status;
2450}
2451
2452
Andy Adamson82be4172012-05-23 05:02:35 -04002453static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002454 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002455 int flags,
2456 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002457 struct nfs4_label *label,
2458 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002460 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 struct nfs4_exception exception = { };
2462 struct nfs4_state *res;
2463 int status;
2464
2465 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002466 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002467 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002468 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (status == 0)
2470 break;
2471 /* NOTE: BAD_SEQID means the server and client disagree about the
2472 * book-keeping w.r.t. state-changing operations
2473 * (OPEN/CLOSE/LOCK/LOCKU...)
2474 * It is actually a sign of a bug on the client or on the server.
2475 *
2476 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002477 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 * have unhashed the old state_owner for us, and that we can
2479 * therefore safely retry using a new one. We should still warn
2480 * the user though...
2481 */
2482 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002483 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002484 " returned a bad sequence-id error!\n",
2485 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 exception.retry = 1;
2487 continue;
2488 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002489 /*
2490 * BAD_STATEID on OPEN means that the server cancelled our
2491 * state before it received the OPEN_CONFIRM.
2492 * Recover by retrying the request as per the discussion
2493 * on Page 181 of RFC3530.
2494 */
2495 if (status == -NFS4ERR_BAD_STATEID) {
2496 exception.retry = 1;
2497 continue;
2498 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002499 if (status == -EAGAIN) {
2500 /* We must have found a delegation */
2501 exception.retry = 1;
2502 continue;
2503 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002504 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2505 continue;
2506 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 status, &exception));
2508 } while (exception.retry);
2509 return res;
2510}
2511
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002512static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2513 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002514 struct nfs4_state *state, struct nfs4_label *ilabel,
2515 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002517 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002519 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 .iap = sattr,
2521 .server = server,
2522 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002523 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 };
2525 struct nfs_setattrres res = {
2526 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002527 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 .server = server,
2529 };
2530 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2532 .rpc_argp = &arg,
2533 .rpc_resp = &res,
2534 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002536 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002537 fmode_t fmode;
2538 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002539 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
David Quigleyaa9c2662013-05-22 12:50:44 -04002541 arg.bitmask = nfs4_bitmask(server, ilabel);
2542 if (ilabel)
2543 arg.bitmask = nfs4_bitmask(server, olabel);
2544
Trond Myklebust0e574af2005-10-27 22:12:38 -04002545 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Trond Myklebustee3ae842013-04-29 10:35:36 -04002547 /* Servers should only apply open mode checks for file size changes */
2548 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2549 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2550
2551 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2552 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002553 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002554 struct nfs_lockowner lockowner = {
2555 .l_owner = current->files,
2556 .l_pid = current->tgid,
2557 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002558 if (!nfs4_valid_open_stateid(state))
2559 return -EBADF;
2560 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2561 &lockowner) == -EIO)
2562 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002563 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002564 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Bryan Schumaker7c513052011-03-24 17:12:24 +00002566 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002567 if (status == 0 && state != NULL)
2568 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002569 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002572static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2573 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002574 struct nfs4_state *state, struct nfs4_label *ilabel,
2575 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002577 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002578 struct nfs4_exception exception = {
2579 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002580 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002581 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 int err;
2583 do {
David Quigley1775fd32013-05-22 12:50:42 -04002584 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002585 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002586 switch (err) {
2587 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002588 if (!(sattr->ia_valid & ATTR_SIZE)) {
2589 pr_warn_once("NFSv4: server %s is incorrectly "
2590 "applying open mode checks to "
2591 "a SETATTR that is not "
2592 "changing file size.\n",
2593 server->nfs_client->cl_hostname);
2594 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002595 if (state && !(state->state & FMODE_WRITE)) {
2596 err = -EBADF;
2597 if (sattr->ia_valid & ATTR_OPEN)
2598 err = -EACCES;
2599 goto out;
2600 }
2601 }
2602 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002604out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return err;
2606}
2607
2608struct nfs4_closedata {
2609 struct inode *inode;
2610 struct nfs4_state *state;
2611 struct nfs_closeargs arg;
2612 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002613 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002614 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002615 bool roc;
2616 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617};
2618
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002619static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002620{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002621 struct nfs4_closedata *calldata = data;
2622 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002623 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002624
Fred Isamanf7e89172011-01-06 11:36:32 +00002625 if (calldata->roc)
2626 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002627 nfs4_put_open_state(calldata->state);
2628 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002629 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002630 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002631 kfree(calldata);
2632}
2633
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002634static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002636 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002639 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Chuck Levera3ca5652012-03-01 17:00:40 -05002641 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002642 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2643 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002644 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 /* hmm. we are done with the inode, and in the process of freeing
2646 * the state_owner. we keep this around to process errors
2647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 switch (task->tk_status) {
2649 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002650 res_stateid = &calldata->res.stateid;
2651 if (calldata->arg.fmode == 0 && calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002652 pnfs_roc_set_barrier(state->inode,
2653 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002654 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002655 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002656 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002658 case -NFS4ERR_OLD_STATEID:
2659 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002661 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002662 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002663 rpc_restart_call_prepare(task);
2664 goto out_release;
2665 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002666 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002667 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002669 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002670 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002671 goto out_release;
2672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
Trond Myklebust412f6c42014-08-25 22:09:08 -04002674 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002675out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002676 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002677 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002678 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002681static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002683 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002684 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002685 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002686 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002687 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002688
Chuck Levera3ca5652012-03-01 17:00:40 -05002689 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002690 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002691 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002692
Trond Myklebust88069f72009-12-08 08:33:16 -05002693 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002694 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002695 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2696 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2697 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002698 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002699 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002700 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002701 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002702 if (state->n_rdonly == 0)
2703 call_close |= is_rdonly;
2704 else if (is_rdonly)
2705 calldata->arg.fmode |= FMODE_READ;
2706 if (state->n_wronly == 0)
2707 call_close |= is_wronly;
2708 else if (is_wronly)
2709 calldata->arg.fmode |= FMODE_WRITE;
2710 } else if (is_rdwr)
2711 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2712
2713 if (calldata->arg.fmode == 0)
2714 call_close |= is_rdwr;
2715
Trond Myklebust5d422302013-03-14 16:57:48 -04002716 if (!nfs4_valid_open_stateid(state))
2717 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002718 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002719
2720 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002721 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002722 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002723 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002724
Fred Isamanf7e89172011-01-06 11:36:32 +00002725 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002726 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002727 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002728 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2729 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002730 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002731 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002732 }
Trond Myklebust6ae37332015-01-30 14:21:14 -05002733 calldata->arg.share_access =
2734 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2735 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002736
Trond Myklebust516a6af2005-10-27 22:12:41 -04002737 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002738 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002739 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002740 &calldata->arg.seq_args,
2741 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002742 task) != 0)
2743 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002744 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002745 return;
2746out_no_action:
2747 task->tk_action = NULL;
2748out_wait:
2749 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002752static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002753 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002754 .rpc_call_done = nfs4_close_done,
2755 .rpc_release = nfs4_free_closedata,
2756};
2757
Peng Taofe08c542014-07-03 13:05:01 +08002758static bool nfs4_roc(struct inode *inode)
2759{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002760 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002761 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002762 return pnfs_roc(inode);
2763}
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765/*
2766 * It is possible for data to be read/written from a mem-mapped file
2767 * after the sys_close call (which hits the vfs layer as a flush).
2768 * This means that we can't safely call nfsv4 close on a file until
2769 * the inode is cleared. This in turn means that we are not good
2770 * NFSv4 citizens - we do not indicate to the server to update the file's
2771 * share state even when we are done with one of the three share
2772 * stateid's in the inode.
2773 *
2774 * NOTE: Caller must be holding the sp->so_owner semaphore!
2775 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002776int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002778 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002779 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002781 struct nfs4_state_owner *sp = state->owner;
2782 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002783 struct rpc_message msg = {
2784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2785 .rpc_cred = state->owner->so_cred,
2786 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002787 struct rpc_task_setup task_setup_data = {
2788 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002789 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002790 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002791 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002792 .flags = RPC_TASK_ASYNC,
2793 };
Trond Myklebust95121352005-10-18 14:20:12 -07002794 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002796 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2797 &task_setup_data.rpc_client, &msg);
2798
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002799 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002801 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002802 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002803 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002805 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002807 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2808 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002809 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002810 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002811 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002812 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002813 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002814 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002815 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002816 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002817 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002818
Trond Myklebust1174dd12010-12-21 10:52:24 -05002819 msg.rpc_argp = &calldata->arg;
2820 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002821 task_setup_data.callback_data = calldata;
2822 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002823 if (IS_ERR(task))
2824 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002825 status = 0;
2826 if (wait)
2827 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002828 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002829 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002830out_free_calldata:
2831 kfree(calldata);
2832out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002833 nfs4_put_open_state(state);
2834 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002835 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
Trond Myklebust2b484292010-09-17 10:56:51 -04002838static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002839nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2840 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002843 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2844
2845 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Trond Myklebust565277f2007-10-15 18:17:53 -04002847 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002848 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002849
2850 nfs4_label_release_security(label);
2851
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002852 if (IS_ERR(state))
2853 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002854 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856
Trond Myklebust1185a552009-12-03 15:54:02 -05002857static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002858{
2859 if (ctx->state == NULL)
2860 return;
2861 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002862 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002863 else
Al Viro643168c2011-06-22 18:20:23 -04002864 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002865}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Trond Myklebustb944dba2013-11-04 15:20:20 -05002867#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2868#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002869#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2872{
Benny Halevy43652ad2009-04-01 09:21:54 -04002873 struct nfs4_server_caps_arg args = {
2874 .fhandle = fhandle,
2875 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 struct nfs4_server_caps_res res = {};
2877 struct rpc_message msg = {
2878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002879 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 .rpc_resp = &res,
2881 };
2882 int status;
2883
Bryan Schumaker7c513052011-03-24 17:12:24 +00002884 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002886 /* Sanity check the server answers */
2887 switch (server->nfs_client->cl_minorversion) {
2888 case 0:
2889 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2890 res.attr_bitmask[2] = 0;
2891 break;
2892 case 1:
2893 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2894 break;
2895 case 2:
2896 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002899 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2900 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2901 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2902 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002903 NFS_CAP_CTIME|NFS_CAP_MTIME|
2904 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002905 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2906 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 server->caps |= NFS_CAP_ACLS;
2908 if (res.has_links != 0)
2909 server->caps |= NFS_CAP_HARDLINKS;
2910 if (res.has_symlinks != 0)
2911 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002912 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2913 server->caps |= NFS_CAP_FILEID;
2914 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2915 server->caps |= NFS_CAP_MODE;
2916 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2917 server->caps |= NFS_CAP_NLINK;
2918 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2919 server->caps |= NFS_CAP_OWNER;
2920 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2921 server->caps |= NFS_CAP_OWNER_GROUP;
2922 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2923 server->caps |= NFS_CAP_ATIME;
2924 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2925 server->caps |= NFS_CAP_CTIME;
2926 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2927 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002928#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2929 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2930 server->caps |= NFS_CAP_SECURITY_LABEL;
2931#endif
2932 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2933 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002934 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002935
Trond Myklebusta65318b2009-03-11 14:10:28 -04002936 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2937 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2938 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002939 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002941 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return status;
2945}
2946
Trond Myklebust55a97592006-06-09 09:34:19 -04002947int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 struct nfs4_exception exception = { };
2950 int err;
2951 do {
2952 err = nfs4_handle_exception(server,
2953 _nfs4_server_capabilities(server, fhandle),
2954 &exception);
2955 } while (exception.retry);
2956 return err;
2957}
2958
2959static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2960 struct nfs_fsinfo *info)
2961{
David Quigleyaa9c2662013-05-22 12:50:44 -04002962 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002964 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 };
2966 struct nfs4_lookup_res res = {
2967 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002968 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 .fh = fhandle,
2970 };
2971 struct rpc_message msg = {
2972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2973 .rpc_argp = &args,
2974 .rpc_resp = &res,
2975 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002976
David Quigleyaa9c2662013-05-22 12:50:44 -04002977 bitmask[0] = nfs4_fattr_bitmap[0];
2978 bitmask[1] = nfs4_fattr_bitmap[1];
2979 /*
2980 * Process the label in the upcoming getfattr
2981 */
2982 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2983
Trond Myklebust0e574af2005-10-27 22:12:38 -04002984 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002985 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987
2988static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2989 struct nfs_fsinfo *info)
2990{
2991 struct nfs4_exception exception = { };
2992 int err;
2993 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002994 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002995 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002996 switch (err) {
2997 case 0:
2998 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002999 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003000 default:
3001 err = nfs4_handle_exception(server, err, &exception);
3002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003004out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return err;
3006}
3007
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003008static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3009 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3010{
Trond Myklebustc2190662013-08-26 19:23:04 -04003011 struct rpc_auth_create_args auth_args = {
3012 .pseudoflavor = flavor,
3013 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003014 struct rpc_auth *auth;
3015 int ret;
3016
Trond Myklebustc2190662013-08-26 19:23:04 -04003017 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003018 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003019 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003020 goto out;
3021 }
3022 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003023out:
3024 return ret;
3025}
3026
Chuck Lever9a744ba2013-03-16 15:56:02 -04003027/*
3028 * Retry pseudoroot lookup with various security flavors. We do this when:
3029 *
3030 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3031 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3032 *
3033 * Returns zero on success, or a negative NFS4ERR value, or a
3034 * negative errno value.
3035 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003036static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003037 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003039 /* Per 3530bis 15.33.5 */
3040 static const rpc_authflavor_t flav_array[] = {
3041 RPC_AUTH_GSS_KRB5P,
3042 RPC_AUTH_GSS_KRB5I,
3043 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003044 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003045 RPC_AUTH_NULL,
3046 };
3047 int status = -EPERM;
3048 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003050 if (server->auth_info.flavor_len > 0) {
3051 /* try each flavor specified by user */
3052 for (i = 0; i < server->auth_info.flavor_len; i++) {
3053 status = nfs4_lookup_root_sec(server, fhandle, info,
3054 server->auth_info.flavors[i]);
3055 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3056 continue;
3057 break;
3058 }
3059 } else {
3060 /* no flavors specified by user, try default list */
3061 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3062 status = nfs4_lookup_root_sec(server, fhandle, info,
3063 flav_array[i]);
3064 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3065 continue;
3066 break;
3067 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003068 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003069
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003070 /*
3071 * -EACCESS could mean that the user doesn't have correct permissions
3072 * to access the mount. It could also mean that we tried to mount
3073 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3074 * existing mount programs don't handle -EACCES very well so it should
3075 * be mapped to -EPERM instead.
3076 */
3077 if (status == -EACCES)
3078 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003079 return status;
3080}
3081
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003082static int nfs4_do_find_root_sec(struct nfs_server *server,
3083 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3084{
3085 int mv = server->nfs_client->cl_minorversion;
3086 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3087}
3088
3089/**
3090 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3091 * @server: initialized nfs_server handle
3092 * @fhandle: we fill in the pseudo-fs root file handle
3093 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003094 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003095 *
3096 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003097 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003098int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003099 struct nfs_fsinfo *info,
3100 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003101{
Andre Przywarac7757072015-04-23 17:17:40 +01003102 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003103
Andre Przywarac7757072015-04-23 17:17:40 +01003104 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003105 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003106
3107 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003108 status = nfs4_do_find_root_sec(server, fhandle, info);
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (status == 0)
3111 status = nfs4_server_capabilities(server, fhandle);
3112 if (status == 0)
3113 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003114
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003115 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
Bryan Schumakerbae36242012-05-10 15:07:31 -04003118static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3119 struct nfs_fsinfo *info)
3120{
3121 int error;
3122 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003123 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003124
3125 error = nfs4_server_capabilities(server, mntfh);
3126 if (error < 0) {
3127 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3128 return error;
3129 }
3130
David Quigley14c43f72013-05-22 12:50:43 -04003131 label = nfs4_label_alloc(server, GFP_KERNEL);
3132 if (IS_ERR(label))
3133 return PTR_ERR(label);
3134
David Quigley1775fd32013-05-22 12:50:42 -04003135 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003136 if (error < 0) {
3137 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003138 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003139 }
3140
3141 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3142 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3143 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3144
David Quigley14c43f72013-05-22 12:50:43 -04003145err_free_label:
3146 nfs4_label_free(label);
3147
Bryan Schumakerbae36242012-05-10 15:07:31 -04003148 return error;
3149}
3150
Manoj Naik6b97fd32006-06-09 09:34:29 -04003151/*
3152 * Get locations and (maybe) other attributes of a referral.
3153 * Note that we'll actually follow the referral later when
3154 * we detect fsid mismatch in inode revalidation
3155 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003156static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3157 const struct qstr *name, struct nfs_fattr *fattr,
3158 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003159{
3160 int status = -ENOMEM;
3161 struct page *page = NULL;
3162 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003163
3164 page = alloc_page(GFP_KERNEL);
3165 if (page == NULL)
3166 goto out;
3167 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3168 if (locations == NULL)
3169 goto out;
3170
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003171 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003172 if (status != 0)
3173 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003174
3175 /*
3176 * If the fsid didn't change, this is a migration event, not a
3177 * referral. Cause us to drop into the exception handler, which
3178 * will kick off migration recovery.
3179 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003180 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003181 dprintk("%s: server did not return a different fsid for"
3182 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003183 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003184 goto out;
3185 }
Andy Adamson533eb462011-06-13 18:25:56 -04003186 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3187 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003188
Andy Adamson533eb462011-06-13 18:25:56 -04003189 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003190 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003191 memset(fhandle, 0, sizeof(struct nfs_fh));
3192out:
3193 if (page)
3194 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003195 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003196 return status;
3197}
3198
David Quigley1775fd32013-05-22 12:50:42 -04003199static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3200 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 struct nfs4_getattr_arg args = {
3203 .fh = fhandle,
3204 .bitmask = server->attr_bitmask,
3205 };
3206 struct nfs4_getattr_res res = {
3207 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003208 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 .server = server,
3210 };
3211 struct rpc_message msg = {
3212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3213 .rpc_argp = &args,
3214 .rpc_resp = &res,
3215 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003216
3217 args.bitmask = nfs4_bitmask(server, label);
3218
Trond Myklebust0e574af2005-10-27 22:12:38 -04003219 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003220 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
David Quigley1775fd32013-05-22 12:50:42 -04003223static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3224 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 struct nfs4_exception exception = { };
3227 int err;
3228 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003229 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3230 trace_nfs4_getattr(server, fhandle, fattr, err);
3231 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 &exception);
3233 } while (exception.retry);
3234 return err;
3235}
3236
3237/*
3238 * The file is not closed if it is opened due to the a request to change
3239 * the size of the file. The open call will not be needed once the
3240 * VFS layer lookup-intents are implemented.
3241 *
3242 * Close is called when the inode is destroyed.
3243 * If we haven't opened the file for O_WRONLY, we
3244 * need to in the size_change case to obtain a stateid.
3245 *
3246 * Got race?
3247 * Because OPEN is always done by name in nfsv4, it is
3248 * possible that we opened a different file by the same
3249 * name. We can recognize this race condition, but we
3250 * can't do anything about it besides returning an error.
3251 *
3252 * This will be fixed with VFS changes (lookup-intent).
3253 */
3254static int
3255nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3256 struct iattr *sattr)
3257{
David Howells2b0143b2015-03-17 22:25:59 +00003258 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003259 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003260 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003261 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 int status;
3263
Peng Tao88ac8152014-09-12 11:04:10 +08003264 if (pnfs_ld_layoutret_on_setattr(inode) &&
3265 sattr->ia_valid & ATTR_SIZE &&
3266 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003267 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003268
Trond Myklebust0e574af2005-10-27 22:12:38 -04003269 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Andy Adamson26699402012-05-30 16:12:24 -04003271 /* Deal with open(O_TRUNC) */
3272 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003273 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003274
3275 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003276 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003277 return 0;
3278
Trond Myklebustd5308382005-11-04 15:33:38 -05003279 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003280 if (sattr->ia_valid & ATTR_FILE) {
3281 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003282
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003283 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003284 if (ctx) {
3285 cred = ctx->cred;
3286 state = ctx->state;
3287 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003288 }
3289
David Quigley14c43f72013-05-22 12:50:43 -04003290 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3291 if (IS_ERR(label))
3292 return PTR_ERR(label);
3293
3294 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003295 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003296 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003297 nfs_setsecurity(inode, fattr, label);
3298 }
David Quigley14c43f72013-05-22 12:50:43 -04003299 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return status;
3301}
3302
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003303static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3304 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003305 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003306{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003307 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003308 int status;
3309 struct nfs4_lookup_arg args = {
3310 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003311 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003312 .name = name,
3313 };
3314 struct nfs4_lookup_res res = {
3315 .server = server,
3316 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003317 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003318 .fh = fhandle,
3319 };
3320 struct rpc_message msg = {
3321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3322 .rpc_argp = &args,
3323 .rpc_resp = &res,
3324 };
3325
David Quigleyaa9c2662013-05-22 12:50:44 -04003326 args.bitmask = nfs4_bitmask(server, label);
3327
David Howells2b3de442006-08-22 20:06:09 -04003328 nfs_fattr_init(fattr);
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003331 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 dprintk("NFS reply lookup: %d\n", status);
3333 return status;
3334}
3335
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003336static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003337{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003338 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003339 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003340 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3341 fattr->nlink = 2;
3342}
3343
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003344static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3345 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003346 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003347{
3348 struct nfs4_exception exception = { };
3349 struct rpc_clnt *client = *clnt;
3350 int err;
3351 do {
David Quigley1775fd32013-05-22 12:50:42 -04003352 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003353 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003354 switch (err) {
3355 case -NFS4ERR_BADNAME:
3356 err = -ENOENT;
3357 goto out;
3358 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003359 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003360 if (err == -NFS4ERR_MOVED)
3361 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003362 goto out;
3363 case -NFS4ERR_WRONGSEC:
3364 err = -EPERM;
3365 if (client != *clnt)
3366 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003367 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003368 if (IS_ERR(client))
3369 return PTR_ERR(client);
3370
3371 exception.retry = 1;
3372 break;
3373 default:
3374 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3375 }
3376 } while (exception.retry);
3377
3378out:
3379 if (err == 0)
3380 *clnt = client;
3381 else if (client != *clnt)
3382 rpc_shutdown_client(client);
3383
3384 return err;
3385}
3386
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003387static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003388 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3389 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003391 int status;
3392 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003393
David Quigley1775fd32013-05-22 12:50:42 -04003394 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003395 if (client != NFS_CLIENT(dir)) {
3396 rpc_shutdown_client(client);
3397 nfs_fixup_secinfo_attributes(fattr);
3398 }
3399 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003402struct rpc_clnt *
3403nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3404 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3405{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003406 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003407 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003408
David Quigley1775fd32013-05-22 12:50:42 -04003409 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003410 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003411 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003412 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003413}
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3416{
Trond Myklebust76b32992007-08-10 17:45:11 -04003417 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 struct nfs4_accessargs args = {
3419 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003420 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003422 struct nfs4_accessres res = {
3423 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003424 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 struct rpc_message msg = {
3426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3427 .rpc_argp = &args,
3428 .rpc_resp = &res,
3429 .rpc_cred = entry->cred,
3430 };
3431 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003432 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
3434 /*
3435 * Determine which access bits we want to ask for...
3436 */
3437 if (mode & MAY_READ)
3438 args.access |= NFS4_ACCESS_READ;
3439 if (S_ISDIR(inode->i_mode)) {
3440 if (mode & MAY_WRITE)
3441 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3442 if (mode & MAY_EXEC)
3443 args.access |= NFS4_ACCESS_LOOKUP;
3444 } else {
3445 if (mode & MAY_WRITE)
3446 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3447 if (mode & MAY_EXEC)
3448 args.access |= NFS4_ACCESS_EXECUTE;
3449 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003450
3451 res.fattr = nfs_alloc_fattr();
3452 if (res.fattr == NULL)
3453 return -ENOMEM;
3454
Bryan Schumaker7c513052011-03-24 17:12:24 +00003455 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003457 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003458 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003460 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return status;
3462}
3463
3464static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3465{
3466 struct nfs4_exception exception = { };
3467 int err;
3468 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003469 err = _nfs4_proc_access(inode, entry);
3470 trace_nfs4_access(inode, err);
3471 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 &exception);
3473 } while (exception.retry);
3474 return err;
3475}
3476
3477/*
3478 * TODO: For the time being, we don't try to get any attributes
3479 * along with any of the zero-copy operations READ, READDIR,
3480 * READLINK, WRITE.
3481 *
3482 * In the case of the first three, we want to put the GETATTR
3483 * after the read-type operation -- this is because it is hard
3484 * to predict the length of a GETATTR response in v4, and thus
3485 * align the READ data correctly. This means that the GETATTR
3486 * may end up partially falling into the page cache, and we should
3487 * shift it into the 'tail' of the xdr_buf before processing.
3488 * To do this efficiently, we need to know the total length
3489 * of data received, which doesn't seem to be available outside
3490 * of the RPC layer.
3491 *
3492 * In the case of WRITE, we also want to put the GETATTR after
3493 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003494 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 *
3496 * Both of these changes to the XDR layer would in fact be quite
3497 * minor, but I decided to leave them for a subsequent patch.
3498 */
3499static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3500 unsigned int pgbase, unsigned int pglen)
3501{
3502 struct nfs4_readlink args = {
3503 .fh = NFS_FH(inode),
3504 .pgbase = pgbase,
3505 .pglen = pglen,
3506 .pages = &page,
3507 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003508 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 struct rpc_message msg = {
3510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3511 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003512 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 };
3514
Bryan Schumaker7c513052011-03-24 17:12:24 +00003515 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 -07003516}
3517
3518static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3519 unsigned int pgbase, unsigned int pglen)
3520{
3521 struct nfs4_exception exception = { };
3522 int err;
3523 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003524 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3525 trace_nfs4_readlink(inode, err);
3526 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 &exception);
3528 } while (exception.retry);
3529 return err;
3530}
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003533 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535static int
3536nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003537 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
David Quigleyaa9c2662013-05-22 12:50:44 -04003539 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003540 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003542 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 int status = 0;
3544
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003545 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3546 if (IS_ERR(ctx))
3547 return PTR_ERR(ctx);
3548
David Quigleyaa9c2662013-05-22 12:50:44 -04003549 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3550
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003551 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003552 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 if (IS_ERR(state)) {
3554 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003555 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003558 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003559 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return status;
3561}
3562
3563static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3564{
Trond Myklebust16e42952005-10-27 22:12:44 -04003565 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003566 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003568 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003570 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003571 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003572 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3575 .rpc_argp = &args,
3576 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003578 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003579
Bryan Schumaker7c513052011-03-24 17:12:24 +00003580 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003581 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003582 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 return status;
3584}
3585
3586static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3587{
3588 struct nfs4_exception exception = { };
3589 int err;
3590 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003591 err = _nfs4_proc_remove(dir, name);
3592 trace_nfs4_remove(dir, name, err);
3593 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 &exception);
3595 } while (exception.retry);
3596 return err;
3597}
3598
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003599static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003601 struct nfs_server *server = NFS_SERVER(dir);
3602 struct nfs_removeargs *args = msg->rpc_argp;
3603 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003605 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003607 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003608
3609 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003612static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3613{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003614 nfs4_setup_sequence(NFS_SERVER(data->dir),
3615 &data->args.seq_args,
3616 &data->res.seq_res,
3617 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003620static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003622 struct nfs_unlinkdata *data = task->tk_calldata;
3623 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003624
Trond Myklebust14516c32010-07-31 14:29:06 -04003625 if (!nfs4_sequence_done(task, &res->seq_res))
3626 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003627 if (nfs4_async_handle_error(task, res->server, NULL,
3628 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003629 return 0;
3630 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003631 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
Jeff Laytond3d41522010-09-17 17:31:57 -04003634static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3635{
3636 struct nfs_server *server = NFS_SERVER(dir);
3637 struct nfs_renameargs *arg = msg->rpc_argp;
3638 struct nfs_renameres *res = msg->rpc_resp;
3639
3640 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003641 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003642 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003643}
3644
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003645static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3646{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003647 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3648 &data->args.seq_args,
3649 &data->res.seq_res,
3650 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003651}
3652
3653static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3654 struct inode *new_dir)
3655{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003656 struct nfs_renamedata *data = task->tk_calldata;
3657 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003658
3659 if (!nfs4_sequence_done(task, &res->seq_res))
3660 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003661 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003662 return 0;
3663
3664 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003665 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003666 return 1;
3667}
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3670{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003671 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 struct nfs4_link_arg arg = {
3673 .fh = NFS_FH(inode),
3674 .dir_fh = NFS_FH(dir),
3675 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003676 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003678 struct nfs4_link_res res = {
3679 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003680 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003681 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 struct rpc_message msg = {
3683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3684 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003685 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003687 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Trond Myklebust136f2622010-04-16 16:22:49 -04003689 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003690 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003691 goto out;
3692
David Quigley14c43f72013-05-22 12:50:43 -04003693 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3694 if (IS_ERR(res.label)) {
3695 status = PTR_ERR(res.label);
3696 goto out;
3697 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003698 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003699
Bryan Schumaker7c513052011-03-24 17:12:24 +00003700 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003701 if (!status) {
3702 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003703 status = nfs_post_op_update_inode(inode, res.fattr);
3704 if (!status)
3705 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003706 }
David Quigley14c43f72013-05-22 12:50:43 -04003707
3708
3709 nfs4_label_free(res.label);
3710
Trond Myklebust136f2622010-04-16 16:22:49 -04003711out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003712 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 return status;
3714}
3715
3716static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3717{
3718 struct nfs4_exception exception = { };
3719 int err;
3720 do {
3721 err = nfs4_handle_exception(NFS_SERVER(inode),
3722 _nfs4_proc_link(inode, dir, name),
3723 &exception);
3724 } while (exception.retry);
3725 return err;
3726}
3727
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003728struct nfs4_createdata {
3729 struct rpc_message msg;
3730 struct nfs4_create_arg arg;
3731 struct nfs4_create_res res;
3732 struct nfs_fh fh;
3733 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003734 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003735};
3736
3737static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3738 struct qstr *name, struct iattr *sattr, u32 ftype)
3739{
3740 struct nfs4_createdata *data;
3741
3742 data = kzalloc(sizeof(*data), GFP_KERNEL);
3743 if (data != NULL) {
3744 struct nfs_server *server = NFS_SERVER(dir);
3745
David Quigley14c43f72013-05-22 12:50:43 -04003746 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3747 if (IS_ERR(data->label))
3748 goto out_free;
3749
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003750 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3751 data->msg.rpc_argp = &data->arg;
3752 data->msg.rpc_resp = &data->res;
3753 data->arg.dir_fh = NFS_FH(dir);
3754 data->arg.server = server;
3755 data->arg.name = name;
3756 data->arg.attrs = sattr;
3757 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003758 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003759 data->res.server = server;
3760 data->res.fh = &data->fh;
3761 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003762 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003763 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003764 }
3765 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003766out_free:
3767 kfree(data);
3768 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003769}
3770
3771static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3772{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003773 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003774 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003775 if (status == 0) {
3776 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003777 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003778 }
3779 return status;
3780}
3781
3782static void nfs4_free_createdata(struct nfs4_createdata *data)
3783{
David Quigley14c43f72013-05-22 12:50:43 -04003784 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003785 kfree(data);
3786}
3787
Chuck Lever4f390c12006-08-22 20:06:22 -04003788static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003789 struct page *page, unsigned int len, struct iattr *sattr,
3790 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003792 struct nfs4_createdata *data;
3793 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Chuck Lever94a6d752006-08-22 20:06:23 -04003795 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003796 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003797
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003798 status = -ENOMEM;
3799 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3800 if (data == NULL)
3801 goto out;
3802
3803 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3804 data->arg.u.symlink.pages = &page;
3805 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003806 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003808 status = nfs4_do_create(dir, dentry, data);
3809
3810 nfs4_free_createdata(data);
3811out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 return status;
3813}
3814
Chuck Lever4f390c12006-08-22 20:06:22 -04003815static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003816 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817{
3818 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003819 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003821
3822 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3823
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003825 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3826 trace_nfs4_symlink(dir, &dentry->d_name, err);
3827 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 &exception);
3829 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003830
3831 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return err;
3833}
3834
3835static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003836 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003838 struct nfs4_createdata *data;
3839 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003841 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3842 if (data == NULL)
3843 goto out;
3844
David Quigley1775fd32013-05-22 12:50:42 -04003845 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003846 status = nfs4_do_create(dir, dentry, data);
3847
3848 nfs4_free_createdata(data);
3849out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 return status;
3851}
3852
3853static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3854 struct iattr *sattr)
3855{
3856 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003857 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003859
David Quigleyaa9c2662013-05-22 12:50:44 -04003860 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3861
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003862 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003864 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3865 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3866 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 &exception);
3868 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003869 nfs4_label_release_security(label);
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 return err;
3872}
3873
3874static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003875 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876{
David Howells2b0143b2015-03-17 22:25:59 +00003877 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 struct nfs4_readdir_arg args = {
3879 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003880 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 .pgbase = 0,
3882 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00003883 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003884 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 };
3886 struct nfs4_readdir_res res;
3887 struct rpc_message msg = {
3888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3889 .rpc_argp = &args,
3890 .rpc_resp = &res,
3891 .rpc_cred = cred,
3892 };
3893 int status;
3894
Al Viro6de14722013-09-16 10:53:17 -04003895 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3896 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003897 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003898 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003900 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 -05003901 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003902 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003903 status += args.pgbase;
3904 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003905
3906 nfs_invalidate_atime(dir);
3907
Harvey Harrison3110ff82008-05-02 13:42:44 -07003908 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 return status;
3910}
3911
3912static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003913 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
3915 struct nfs4_exception exception = { };
3916 int err;
3917 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003918 err = _nfs4_proc_readdir(dentry, cred, cookie,
3919 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00003920 trace_nfs4_readdir(d_inode(dentry), err);
3921 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 &exception);
3923 } while (exception.retry);
3924 return err;
3925}
3926
3927static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003928 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003930 struct nfs4_createdata *data;
3931 int mode = sattr->ia_mode;
3932 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003934 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3935 if (data == NULL)
3936 goto out;
3937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003939 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003941 data->arg.ftype = NF4BLK;
3942 data->arg.u.device.specdata1 = MAJOR(rdev);
3943 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 }
3945 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003946 data->arg.ftype = NF4CHR;
3947 data->arg.u.device.specdata1 = MAJOR(rdev);
3948 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003949 } else if (!S_ISSOCK(mode)) {
3950 status = -EINVAL;
3951 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
David Quigley1775fd32013-05-22 12:50:42 -04003953
David Quigleyaa9c2662013-05-22 12:50:44 -04003954 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003955 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003956out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003957 nfs4_free_createdata(data);
3958out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 return status;
3960}
3961
3962static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3963 struct iattr *sattr, dev_t rdev)
3964{
3965 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003966 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003968
David Quigleyaa9c2662013-05-22 12:50:44 -04003969 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3970
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003971 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003973 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3974 trace_nfs4_mknod(dir, &dentry->d_name, err);
3975 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 &exception);
3977 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003978
3979 nfs4_label_release_security(label);
3980
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 return err;
3982}
3983
3984static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3985 struct nfs_fsstat *fsstat)
3986{
3987 struct nfs4_statfs_arg args = {
3988 .fh = fhandle,
3989 .bitmask = server->attr_bitmask,
3990 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003991 struct nfs4_statfs_res res = {
3992 .fsstat = fsstat,
3993 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 struct rpc_message msg = {
3995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3996 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003997 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 };
3999
Trond Myklebust0e574af2005-10-27 22:12:38 -04004000 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004001 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
4004static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4005{
4006 struct nfs4_exception exception = { };
4007 int err;
4008 do {
4009 err = nfs4_handle_exception(server,
4010 _nfs4_proc_statfs(server, fhandle, fsstat),
4011 &exception);
4012 } while (exception.retry);
4013 return err;
4014}
4015
4016static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4017 struct nfs_fsinfo *fsinfo)
4018{
4019 struct nfs4_fsinfo_arg args = {
4020 .fh = fhandle,
4021 .bitmask = server->attr_bitmask,
4022 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004023 struct nfs4_fsinfo_res res = {
4024 .fsinfo = fsinfo,
4025 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 struct rpc_message msg = {
4027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4028 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004029 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 };
4031
Bryan Schumaker7c513052011-03-24 17:12:24 +00004032 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
4035static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4036{
4037 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004038 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 int err;
4040
4041 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004042 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004043 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004044 if (err == 0) {
4045 struct nfs_client *clp = server->nfs_client;
4046
4047 spin_lock(&clp->cl_lock);
4048 clp->cl_lease_time = fsinfo->lease_time * HZ;
4049 clp->cl_last_renewal = now;
4050 spin_unlock(&clp->cl_lock);
4051 break;
4052 }
4053 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 } while (exception.retry);
4055 return err;
4056}
4057
4058static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4059{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004060 int error;
4061
Trond Myklebust0e574af2005-10-27 22:12:38 -04004062 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004063 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004064 if (error == 0) {
4065 /* block layout checks this! */
4066 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004067 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004068 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004069
4070 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071}
4072
4073static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4074 struct nfs_pathconf *pathconf)
4075{
4076 struct nfs4_pathconf_arg args = {
4077 .fh = fhandle,
4078 .bitmask = server->attr_bitmask,
4079 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004080 struct nfs4_pathconf_res res = {
4081 .pathconf = pathconf,
4082 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 struct rpc_message msg = {
4084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4085 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004086 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 };
4088
4089 /* None of the pathconf attributes are mandatory to implement */
4090 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4091 memset(pathconf, 0, sizeof(*pathconf));
4092 return 0;
4093 }
4094
Trond Myklebust0e574af2005-10-27 22:12:38 -04004095 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004096 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097}
4098
4099static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4100 struct nfs_pathconf *pathconf)
4101{
4102 struct nfs4_exception exception = { };
4103 int err;
4104
4105 do {
4106 err = nfs4_handle_exception(server,
4107 _nfs4_proc_pathconf(server, fhandle, pathconf),
4108 &exception);
4109 } while (exception.retry);
4110 return err;
4111}
4112
Trond Myklebust5521abf2013-03-16 20:54:34 -04004113int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004114 const struct nfs_open_context *ctx,
4115 const struct nfs_lock_context *l_ctx,
4116 fmode_t fmode)
4117{
4118 const struct nfs_lockowner *lockowner = NULL;
4119
4120 if (l_ctx != NULL)
4121 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004122 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004123}
4124EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4125
Trond Myklebust5521abf2013-03-16 20:54:34 -04004126static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4127 const struct nfs_open_context *ctx,
4128 const struct nfs_lock_context *l_ctx,
4129 fmode_t fmode)
4130{
4131 nfs4_stateid current_stateid;
4132
Trond Myklebuste1253be2014-03-05 08:44:23 -05004133 /* If the current stateid represents a lost lock, then exit */
4134 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4135 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004136 return nfs4_stateid_match(stateid, &current_stateid);
4137}
4138
4139static bool nfs4_error_stateid_expired(int err)
4140{
4141 switch (err) {
4142 case -NFS4ERR_DELEG_REVOKED:
4143 case -NFS4ERR_ADMIN_REVOKED:
4144 case -NFS4ERR_BAD_STATEID:
4145 case -NFS4ERR_STALE_STATEID:
4146 case -NFS4ERR_OLD_STATEID:
4147 case -NFS4ERR_OPENMODE:
4148 case -NFS4ERR_EXPIRED:
4149 return true;
4150 }
4151 return false;
4152}
4153
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004154void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004155{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004156 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004157}
4158
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004159static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004161 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004163 trace_nfs4_read(hdr, task->tk_status);
4164 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004165 hdr->args.context->state,
4166 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004167 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004168 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004170
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004171 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004173 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175}
4176
Trond Myklebust5521abf2013-03-16 20:54:34 -04004177static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004178 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004179{
4180
4181 if (!nfs4_error_stateid_expired(task->tk_status) ||
4182 nfs4_stateid_is_current(&args->stateid,
4183 args->context,
4184 args->lock_context,
4185 FMODE_READ))
4186 return false;
4187 rpc_restart_call_prepare(task);
4188 return true;
4189}
4190
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004191static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004192{
4193
4194 dprintk("--> %s\n", __func__);
4195
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004196 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004197 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004198 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004199 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004200 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4201 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004202}
4203
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004204static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4205 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004207 hdr->timestamp = jiffies;
4208 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004209 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004210 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211}
4212
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004213static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4214 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004215{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004216 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4217 &hdr->args.seq_args,
4218 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004219 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004220 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004221 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4222 hdr->args.lock_context,
4223 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004224 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004225 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004226 return -EIO;
4227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004230static int nfs4_write_done_cb(struct rpc_task *task,
4231 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004233 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004234
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004235 trace_nfs4_write(hdr, task->tk_status);
4236 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004237 hdr->args.context->state,
4238 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004239 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004240 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004242 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004243 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004244 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004245 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
4248
Trond Myklebust5521abf2013-03-16 20:54:34 -04004249static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004250 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004251{
4252
4253 if (!nfs4_error_stateid_expired(task->tk_status) ||
4254 nfs4_stateid_is_current(&args->stateid,
4255 args->context,
4256 args->lock_context,
4257 FMODE_WRITE))
4258 return false;
4259 rpc_restart_call_prepare(task);
4260 return true;
4261}
4262
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004263static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004264{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004265 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004266 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004267 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004268 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004269 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4270 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004271}
4272
Trond Myklebust5a37f852012-04-28 14:55:16 -04004273static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004274bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004275{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004276 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004277 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004278 return false;
4279 /* Otherwise, request attributes if and only if we don't hold
4280 * a delegation
4281 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004282 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004283}
Fred Isamana69aef12011-03-03 15:13:47 +00004284
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004285static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4286 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004288 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004289
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004290 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4291 hdr->args.bitmask = NULL;
4292 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004293 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004294 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004295
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004296 if (!hdr->pgio_done_cb)
4297 hdr->pgio_done_cb = nfs4_write_done_cb;
4298 hdr->res.server = server;
4299 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004301 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004302 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304
Fred Isaman0b7c0152012-04-20 14:47:39 -04004305static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4306{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004307 nfs4_setup_sequence(NFS_SERVER(data->inode),
4308 &data->args.seq_args,
4309 &data->res.seq_res,
4310 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311}
4312
Fred Isaman0b7c0152012-04-20 14:47:39 -04004313static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004316
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004317 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004318 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4319 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004320 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004321 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324}
4325
Fred Isaman0b7c0152012-04-20 14:47:39 -04004326static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004327{
4328 if (!nfs4_sequence_done(task, &data->res.seq_res))
4329 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004330 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004331}
4332
Fred Isaman0b7c0152012-04-20 14:47:39 -04004333static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004335 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004336
Fred Isaman0b7c0152012-04-20 14:47:39 -04004337 if (data->commit_done_cb == NULL)
4338 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004339 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004340 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004341 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342}
4343
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004344struct nfs4_renewdata {
4345 struct nfs_client *client;
4346 unsigned long timestamp;
4347};
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349/*
4350 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4351 * standalone procedure for queueing an asynchronous RENEW.
4352 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004353static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004354{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004355 struct nfs4_renewdata *data = calldata;
4356 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004357
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004358 if (atomic_read(&clp->cl_count) > 1)
4359 nfs4_schedule_state_renewal(clp);
4360 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004361 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004362}
4363
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004364static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004366 struct nfs4_renewdata *data = calldata;
4367 struct nfs_client *clp = data->client;
4368 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004370 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004371 switch (task->tk_status) {
4372 case 0:
4373 break;
4374 case -NFS4ERR_LEASE_MOVED:
4375 nfs4_schedule_lease_moved_recovery(clp);
4376 break;
4377 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004378 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004379 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4380 return;
4381 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004382 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004383 return;
4384 }
4385 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004387 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388}
4389
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004390static const struct rpc_call_ops nfs4_renew_ops = {
4391 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004392 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004393};
4394
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004395static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396{
4397 struct rpc_message msg = {
4398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4399 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004400 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004402 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004404 if (renew_flags == 0)
4405 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004406 if (!atomic_inc_not_zero(&clp->cl_count))
4407 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004408 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004409 if (data == NULL)
4410 return -ENOMEM;
4411 data->client = clp;
4412 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004413 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004414 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415}
4416
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004417static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
4419 struct rpc_message msg = {
4420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4421 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004422 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 };
4424 unsigned long now = jiffies;
4425 int status;
4426
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004427 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 if (status < 0)
4429 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004430 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 return 0;
4432}
4433
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004434static inline int nfs4_server_supports_acls(struct nfs_server *server)
4435{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004436 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004437}
4438
Trond Myklebust21f498c2012-08-24 10:59:25 -04004439/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4440 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004441 * the stack.
4442 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004443#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004444
Neil Hormane9e3d722011-03-04 19:26:03 -05004445static int buf_to_pages_noslab(const void *buf, size_t buflen,
4446 struct page **pages, unsigned int *pgbase)
4447{
4448 struct page *newpage, **spages;
4449 int rc = 0;
4450 size_t len;
4451 spages = pages;
4452
4453 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004454 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004455 newpage = alloc_page(GFP_KERNEL);
4456
4457 if (newpage == NULL)
4458 goto unwind;
4459 memcpy(page_address(newpage), buf, len);
4460 buf += len;
4461 buflen -= len;
4462 *pages++ = newpage;
4463 rc++;
4464 } while (buflen != 0);
4465
4466 return rc;
4467
4468unwind:
4469 for(; rc > 0; rc--)
4470 __free_page(spages[rc-1]);
4471 return -ENOMEM;
4472}
4473
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004474struct nfs4_cached_acl {
4475 int cached;
4476 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004477 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004478};
4479
4480static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004481{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004482 struct nfs_inode *nfsi = NFS_I(inode);
4483
4484 spin_lock(&inode->i_lock);
4485 kfree(nfsi->nfs4_acl);
4486 nfsi->nfs4_acl = acl;
4487 spin_unlock(&inode->i_lock);
4488}
4489
4490static void nfs4_zap_acl_attr(struct inode *inode)
4491{
4492 nfs4_set_cached_acl(inode, NULL);
4493}
4494
4495static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4496{
4497 struct nfs_inode *nfsi = NFS_I(inode);
4498 struct nfs4_cached_acl *acl;
4499 int ret = -ENOENT;
4500
4501 spin_lock(&inode->i_lock);
4502 acl = nfsi->nfs4_acl;
4503 if (acl == NULL)
4504 goto out;
4505 if (buf == NULL) /* user is just asking for length */
4506 goto out_len;
4507 if (acl->cached == 0)
4508 goto out;
4509 ret = -ERANGE; /* see getxattr(2) man page */
4510 if (acl->len > buflen)
4511 goto out;
4512 memcpy(buf, acl->data, acl->len);
4513out_len:
4514 ret = acl->len;
4515out:
4516 spin_unlock(&inode->i_lock);
4517 return ret;
4518}
4519
Sachin Prabhu5794d212012-04-17 14:36:40 +01004520static 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 +00004521{
4522 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004523 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004524
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004525 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004526 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004527 if (acl == NULL)
4528 goto out;
4529 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004530 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004531 } else {
4532 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4533 if (acl == NULL)
4534 goto out;
4535 acl->cached = 0;
4536 }
4537 acl->len = acl_len;
4538out:
4539 nfs4_set_cached_acl(inode, acl);
4540}
4541
Andy Adamsonbf118a32011-12-07 11:55:27 -05004542/*
4543 * The getxattr API returns the required buffer length when called with a
4544 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4545 * the required buf. On a NULL buf, we send a page of data to the server
4546 * guessing that the ACL request can be serviced by a page. If so, we cache
4547 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4548 * the cache. If not so, we throw away the page, and cache the required
4549 * length. The next getxattr call will then produce another round trip to
4550 * the server, this time with the input buf of the required size.
4551 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004552static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004553{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004554 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004555 struct nfs_getaclargs args = {
4556 .fh = NFS_FH(inode),
4557 .acl_pages = pages,
4558 .acl_len = buflen,
4559 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004560 struct nfs_getaclres res = {
4561 .acl_len = buflen,
4562 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004563 struct rpc_message msg = {
4564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4565 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004566 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004567 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004568 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4569 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004570
Andy Adamsonbf118a32011-12-07 11:55:27 -05004571 /* As long as we're doing a round trip to the server anyway,
4572 * let's be prepared for a page of acl data. */
4573 if (npages == 0)
4574 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004575 if (npages > ARRAY_SIZE(pages))
4576 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004577
Andy Adamsonbf118a32011-12-07 11:55:27 -05004578 for (i = 0; i < npages; i++) {
4579 pages[i] = alloc_page(GFP_KERNEL);
4580 if (!pages[i])
4581 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004582 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004583
4584 /* for decoding across pages */
4585 res.acl_scratch = alloc_page(GFP_KERNEL);
4586 if (!res.acl_scratch)
4587 goto out_free;
4588
Andy Adamsonbf118a32011-12-07 11:55:27 -05004589 args.acl_len = npages * PAGE_SIZE;
4590 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004591
Peng Taode040be2012-01-10 22:42:47 +08004592 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004593 __func__, buf, buflen, npages, args.acl_len);
4594 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4595 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004596 if (ret)
4597 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004598
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004599 /* Handle the case where the passed-in buffer is too short */
4600 if (res.acl_flags & NFS4_ACL_TRUNC) {
4601 /* Did the user only issue a request for the acl length? */
4602 if (buf == NULL)
4603 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004604 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004605 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004606 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004607 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004608 if (buf) {
4609 if (res.acl_len > buflen) {
4610 ret = -ERANGE;
4611 goto out_free;
4612 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004613 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004614 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004615out_ok:
4616 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004617out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004618 for (i = 0; i < npages; i++)
4619 if (pages[i])
4620 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004621 if (res.acl_scratch)
4622 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004623 return ret;
4624}
4625
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004626static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4627{
4628 struct nfs4_exception exception = { };
4629 ssize_t ret;
4630 do {
4631 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004632 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004633 if (ret >= 0)
4634 break;
4635 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4636 } while (exception.retry);
4637 return ret;
4638}
4639
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004640static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4641{
4642 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004643 int ret;
4644
4645 if (!nfs4_server_supports_acls(server))
4646 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004647 ret = nfs_revalidate_inode(server, inode);
4648 if (ret < 0)
4649 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004650 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4651 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004652 ret = nfs4_read_cached_acl(inode, buf, buflen);
4653 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004654 /* -ENOENT is returned if there is no ACL or if there is an ACL
4655 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004656 return ret;
4657 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004658}
4659
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004660static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004661{
4662 struct nfs_server *server = NFS_SERVER(inode);
4663 struct page *pages[NFS4ACL_MAXPAGES];
4664 struct nfs_setaclargs arg = {
4665 .fh = NFS_FH(inode),
4666 .acl_pages = pages,
4667 .acl_len = buflen,
4668 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004669 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004670 struct rpc_message msg = {
4671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4672 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004673 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004674 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004675 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004676 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004677
4678 if (!nfs4_server_supports_acls(server))
4679 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004680 if (npages > ARRAY_SIZE(pages))
4681 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004682 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4683 if (i < 0)
4684 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004685 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004686 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004687
4688 /*
4689 * Free each page after tx, so the only ref left is
4690 * held by the network stack
4691 */
4692 for (; i > 0; i--)
4693 put_page(pages[i-1]);
4694
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004695 /*
4696 * Acl update can result in inode attribute update.
4697 * so mark the attribute cache invalid.
4698 */
4699 spin_lock(&inode->i_lock);
4700 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4701 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004702 nfs_access_zap_cache(inode);
4703 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004704 return ret;
4705}
4706
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004707static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4708{
4709 struct nfs4_exception exception = { };
4710 int err;
4711 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004712 err = __nfs4_proc_set_acl(inode, buf, buflen);
4713 trace_nfs4_set_acl(inode, err);
4714 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004715 &exception);
4716 } while (exception.retry);
4717 return err;
4718}
4719
David Quigleyaa9c2662013-05-22 12:50:44 -04004720#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4721static int _nfs4_get_security_label(struct inode *inode, void *buf,
4722 size_t buflen)
4723{
4724 struct nfs_server *server = NFS_SERVER(inode);
4725 struct nfs_fattr fattr;
4726 struct nfs4_label label = {0, 0, buflen, buf};
4727
4728 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004729 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004730 .fh = NFS_FH(inode),
4731 .bitmask = bitmask,
4732 };
4733 struct nfs4_getattr_res res = {
4734 .fattr = &fattr,
4735 .label = &label,
4736 .server = server,
4737 };
4738 struct rpc_message msg = {
4739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004740 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004741 .rpc_resp = &res,
4742 };
4743 int ret;
4744
4745 nfs_fattr_init(&fattr);
4746
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004747 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004748 if (ret)
4749 return ret;
4750 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4751 return -ENOENT;
4752 if (buflen < label.len)
4753 return -ERANGE;
4754 return 0;
4755}
4756
4757static int nfs4_get_security_label(struct inode *inode, void *buf,
4758 size_t buflen)
4759{
4760 struct nfs4_exception exception = { };
4761 int err;
4762
4763 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4764 return -EOPNOTSUPP;
4765
4766 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004767 err = _nfs4_get_security_label(inode, buf, buflen);
4768 trace_nfs4_get_security_label(inode, err);
4769 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004770 &exception);
4771 } while (exception.retry);
4772 return err;
4773}
4774
4775static int _nfs4_do_set_security_label(struct inode *inode,
4776 struct nfs4_label *ilabel,
4777 struct nfs_fattr *fattr,
4778 struct nfs4_label *olabel)
4779{
4780
4781 struct iattr sattr = {0};
4782 struct nfs_server *server = NFS_SERVER(inode);
4783 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004784 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004785 .fh = NFS_FH(inode),
4786 .iap = &sattr,
4787 .server = server,
4788 .bitmask = bitmask,
4789 .label = ilabel,
4790 };
4791 struct nfs_setattrres res = {
4792 .fattr = fattr,
4793 .label = olabel,
4794 .server = server,
4795 };
4796 struct rpc_message msg = {
4797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004798 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004799 .rpc_resp = &res,
4800 };
4801 int status;
4802
Jeff Layton12207f62013-11-01 10:49:32 -04004803 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004804
Jeff Layton12207f62013-11-01 10:49:32 -04004805 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004806 if (status)
4807 dprintk("%s failed: %d\n", __func__, status);
4808
4809 return status;
4810}
4811
4812static int nfs4_do_set_security_label(struct inode *inode,
4813 struct nfs4_label *ilabel,
4814 struct nfs_fattr *fattr,
4815 struct nfs4_label *olabel)
4816{
4817 struct nfs4_exception exception = { };
4818 int err;
4819
4820 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004821 err = _nfs4_do_set_security_label(inode, ilabel,
4822 fattr, olabel);
4823 trace_nfs4_set_security_label(inode, err);
4824 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004825 &exception);
4826 } while (exception.retry);
4827 return err;
4828}
4829
4830static int
4831nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4832{
4833 struct nfs4_label ilabel, *olabel = NULL;
4834 struct nfs_fattr fattr;
4835 struct rpc_cred *cred;
David Howells2b0143b2015-03-17 22:25:59 +00004836 struct inode *inode = d_inode(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004837 int status;
4838
4839 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4840 return -EOPNOTSUPP;
4841
4842 nfs_fattr_init(&fattr);
4843
4844 ilabel.pi = 0;
4845 ilabel.lfs = 0;
4846 ilabel.label = (char *)buf;
4847 ilabel.len = buflen;
4848
4849 cred = rpc_lookup_cred();
4850 if (IS_ERR(cred))
4851 return PTR_ERR(cred);
4852
4853 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4854 if (IS_ERR(olabel)) {
4855 status = -PTR_ERR(olabel);
4856 goto out;
4857 }
4858
4859 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4860 if (status == 0)
4861 nfs_setsecurity(inode, &fattr, olabel);
4862
4863 nfs4_label_free(olabel);
4864out:
4865 put_rpccred(cred);
4866 return status;
4867}
4868#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4869
4870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004872nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4873 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004875 struct nfs_client *clp = server->nfs_client;
4876
4877 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 return 0;
4879 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004880 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004881 case -NFS4ERR_ADMIN_REVOKED:
4882 case -NFS4ERR_BAD_STATEID:
4883 case -NFS4ERR_OPENMODE:
4884 if (state == NULL)
4885 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004886 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004887 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004888 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004889 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004890 if (state != NULL) {
4891 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004892 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004895 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004896 nfs4_schedule_lease_recovery(clp);
4897 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004898 case -NFS4ERR_MOVED:
4899 if (nfs4_schedule_migration_recovery(server) < 0)
4900 goto recovery_failed;
4901 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004902 case -NFS4ERR_LEASE_MOVED:
4903 nfs4_schedule_lease_moved_recovery(clp);
4904 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004905#if defined(CONFIG_NFS_V4_1)
4906 case -NFS4ERR_BADSESSION:
4907 case -NFS4ERR_BADSLOT:
4908 case -NFS4ERR_BAD_HIGH_SLOT:
4909 case -NFS4ERR_DEADSESSION:
4910 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4911 case -NFS4ERR_SEQ_FALSE_RETRY:
4912 case -NFS4ERR_SEQ_MISORDERED:
4913 dprintk("%s ERROR %d, Reset session\n", __func__,
4914 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004915 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004916 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004917#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004919 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10004920 rpc_delay(task, nfs4_update_delay(timeout));
4921 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05004922 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004924 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004926 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 }
4928 task->tk_status = nfs4_map_errors(task->tk_status);
4929 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004930recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004931 task->tk_status = -EIO;
4932 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004933wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004934 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004935 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4936 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004937 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4938 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004939restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004940 task->tk_status = 0;
4941 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942}
4943
Chuck Leverf0920752012-05-21 22:45:41 -04004944static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4945 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004946{
4947 __be32 verf[2];
4948
Chuck Lever2c820d92012-05-21 22:45:33 -04004949 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4950 /* An impossible timestamp guarantees this value
4951 * will never match a generated boot time. */
4952 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004953 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004954 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004955 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004956 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4957 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004958 }
Chuck Levercd937102012-03-02 17:14:31 -05004959 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4960}
4961
Jeff Laytona3192682015-06-09 19:43:59 -04004962static int
4963nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04004964{
Jeff Laytona3192682015-06-09 19:43:59 -04004965 int result;
4966 size_t len;
4967 char *str;
4968 bool retried = false;
Chuck Levere984a552012-09-14 17:24:21 -04004969
Trond Myklebustceb3a162015-01-03 15:16:04 -05004970 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04004971 return 0;
4972retry:
4973 rcu_read_lock();
4974 len = 10 + strlen(clp->cl_ipaddr) + 1 +
4975 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
4976 1 +
4977 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
4978 1;
4979 rcu_read_unlock();
4980
4981 if (len > NFS4_OPAQUE_LIMIT + 1)
4982 return -EINVAL;
4983
4984 /*
4985 * Since this string is allocated at mount time, and held until the
4986 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
4987 * about a memory-reclaim deadlock.
4988 */
4989 str = kmalloc(len, GFP_KERNEL);
4990 if (!str)
4991 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05004992
Chuck Levere984a552012-09-14 17:24:21 -04004993 rcu_read_lock();
Jeff Laytona3192682015-06-09 19:43:59 -04004994 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
4995 clp->cl_ipaddr,
4996 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
4997 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04004998 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04004999
5000 /* Did something change? */
5001 if (result >= len) {
5002 kfree(str);
5003 if (retried)
5004 return -EINVAL;
5005 retried = true;
5006 goto retry;
5007 }
5008 clp->cl_owner_id = str;
5009 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005010}
5011
Jeff Layton873e3852015-06-09 19:44:00 -04005012static int
5013nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005014{
Jeff Layton873e3852015-06-09 19:44:00 -04005015 int result;
5016 size_t len;
5017 char *str;
5018
5019 len = 10 + 10 + 1 + 10 + 1 +
5020 strlen(nfs4_client_id_uniquifier) + 1 +
5021 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5022
5023 if (len > NFS4_OPAQUE_LIMIT + 1)
5024 return -EINVAL;
5025
5026 /*
5027 * Since this string is allocated at mount time, and held until the
5028 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5029 * about a memory-reclaim deadlock.
5030 */
5031 str = kmalloc(len, GFP_KERNEL);
5032 if (!str)
5033 return -ENOMEM;
5034
5035 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5036 clp->rpc_ops->version, clp->cl_minorversion,
5037 nfs4_client_id_uniquifier,
5038 clp->cl_rpcclient->cl_nodename);
5039 if (result >= len) {
5040 kfree(str);
5041 return -EINVAL;
5042 }
5043 clp->cl_owner_id = str;
5044 return 0;
5045}
5046
5047static int
5048nfs4_init_uniform_client_string(struct nfs_client *clp)
5049{
5050 int result;
5051 size_t len;
5052 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005053
5054 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005055 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005056
5057 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005058 return nfs4_init_uniquifier_client_string(clp);
5059
5060 len = 10 + 10 + 1 + 10 + 1 +
5061 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5062
5063 if (len > NFS4_OPAQUE_LIMIT + 1)
5064 return -EINVAL;
5065
5066 /*
5067 * Since this string is allocated at mount time, and held until the
5068 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5069 * about a memory-reclaim deadlock.
5070 */
5071 str = kmalloc(len, GFP_KERNEL);
5072 if (!str)
5073 return -ENOMEM;
5074
5075 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5076 clp->rpc_ops->version, clp->cl_minorversion,
5077 clp->cl_rpcclient->cl_nodename);
5078 if (result >= len) {
5079 kfree(str);
5080 return -EINVAL;
5081 }
5082 clp->cl_owner_id = str;
5083 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005084}
5085
Chuck Lever706cb8d2014-03-12 12:51:47 -04005086/*
5087 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5088 * services. Advertise one based on the address family of the
5089 * clientaddr.
5090 */
5091static unsigned int
5092nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5093{
5094 if (strchr(clp->cl_ipaddr, ':') != NULL)
5095 return scnprintf(buf, len, "tcp6");
5096 else
5097 return scnprintf(buf, len, "tcp");
5098}
5099
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005100static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5101{
5102 struct nfs4_setclientid *sc = calldata;
5103
5104 if (task->tk_status == 0)
5105 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5106}
5107
5108static const struct rpc_call_ops nfs4_setclientid_ops = {
5109 .rpc_call_done = nfs4_setclientid_done,
5110};
5111
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005112/**
5113 * nfs4_proc_setclientid - Negotiate client ID
5114 * @clp: state data structure
5115 * @program: RPC program for NFSv4 callback service
5116 * @port: IP port number for NFS4 callback service
5117 * @cred: RPC credential to use for this call
5118 * @res: where to place the result
5119 *
5120 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5121 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005122int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5123 unsigned short port, struct rpc_cred *cred,
5124 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125{
5126 nfs4_verifier sc_verifier;
5127 struct nfs4_setclientid setclientid = {
5128 .sc_verifier = &sc_verifier,
5129 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005130 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 };
5132 struct rpc_message msg = {
5133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5134 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005135 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005136 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005138 struct rpc_task *task;
5139 struct rpc_task_setup task_setup_data = {
5140 .rpc_client = clp->cl_rpcclient,
5141 .rpc_message = &msg,
5142 .callback_ops = &nfs4_setclientid_ops,
5143 .callback_data = &setclientid,
5144 .flags = RPC_TASK_TIMEOUT,
5145 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005146 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Chuck Leverde734832012-07-11 16:30:50 -04005148 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005149 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005150
5151 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5152 status = nfs4_init_uniform_client_string(clp);
5153 else
Jeff Laytona3192682015-06-09 19:43:59 -04005154 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005155
5156 if (status)
5157 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005158
Chuck Leverde734832012-07-11 16:30:50 -04005159 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005160 setclientid.sc_netid_len =
5161 nfs4_init_callback_netid(clp,
5162 setclientid.sc_netid,
5163 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005164 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005165 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 clp->cl_ipaddr, port >> 8, port & 255);
5167
Jeff Layton3a6bb732015-06-09 19:43:57 -04005168 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005169 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005170 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005171 task = rpc_run_task(&task_setup_data);
5172 if (IS_ERR(task)) {
5173 status = PTR_ERR(task);
5174 goto out;
5175 }
5176 status = task->tk_status;
5177 if (setclientid.sc_cred) {
5178 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5179 put_rpccred(setclientid.sc_cred);
5180 }
5181 rpc_put_task(task);
5182out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005183 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005184 dprintk("NFS reply setclientid: %d\n", status);
5185 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186}
5187
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005188/**
5189 * nfs4_proc_setclientid_confirm - Confirm client ID
5190 * @clp: state data structure
5191 * @res: result of a previous SETCLIENTID
5192 * @cred: RPC credential to use for this call
5193 *
5194 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5195 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005196int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005197 struct nfs4_setclientid_res *arg,
5198 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 struct rpc_message msg = {
5201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005202 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005203 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 int status;
5206
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005207 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5208 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5209 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005210 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005211 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005212 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 return status;
5214}
5215
Trond Myklebustfe650402006-01-03 09:55:18 +01005216struct nfs4_delegreturndata {
5217 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005218 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005219 struct nfs_fh fh;
5220 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005221 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005222 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005223 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005224 struct inode *inode;
5225 bool roc;
5226 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005227};
5228
Trond Myklebustfe650402006-01-03 09:55:18 +01005229static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5230{
5231 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005232
Trond Myklebust14516c32010-07-31 14:29:06 -04005233 if (!nfs4_sequence_done(task, &data->res.seq_res))
5234 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005235
Trond Myklebustca8acf82013-08-13 10:36:56 -04005236 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005237 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005238 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005239 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005240 case -NFS4ERR_ADMIN_REVOKED:
5241 case -NFS4ERR_DELEG_REVOKED:
5242 case -NFS4ERR_BAD_STATEID:
5243 case -NFS4ERR_OLD_STATEID:
5244 case -NFS4ERR_STALE_STATEID:
5245 case -NFS4ERR_EXPIRED:
5246 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005247 if (data->roc)
5248 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005249 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005250 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005251 if (nfs4_async_handle_error(task, data->res.server,
5252 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005253 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005254 return;
5255 }
5256 }
5257 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005258}
5259
5260static void nfs4_delegreturn_release(void *calldata)
5261{
Peng Tao039b7562014-07-03 13:05:02 +08005262 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005263 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005264
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005265 if (inode) {
5266 if (data->roc)
5267 pnfs_roc_release(inode);
5268 nfs_iput_and_deactive(inode);
5269 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005270 kfree(calldata);
5271}
5272
Andy Adamson938e1012009-04-01 09:22:28 -04005273static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5274{
5275 struct nfs4_delegreturndata *d_data;
5276
5277 d_data = (struct nfs4_delegreturndata *)data;
5278
Peng Tao039b7562014-07-03 13:05:02 +08005279 if (d_data->roc &&
5280 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5281 return;
5282
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005283 nfs4_setup_sequence(d_data->res.server,
5284 &d_data->args.seq_args,
5285 &d_data->res.seq_res,
5286 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005287}
Andy Adamson938e1012009-04-01 09:22:28 -04005288
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005289static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005290 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005291 .rpc_call_done = nfs4_delegreturn_done,
5292 .rpc_release = nfs4_delegreturn_release,
5293};
5294
Trond Myklebuste6f81072008-01-24 18:14:34 -05005295static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005296{
5297 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005298 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005299 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005300 struct rpc_message msg = {
5301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5302 .rpc_cred = cred,
5303 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005304 struct rpc_task_setup task_setup_data = {
5305 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005306 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005307 .callback_ops = &nfs4_delegreturn_ops,
5308 .flags = RPC_TASK_ASYNC,
5309 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005310 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005311
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005312 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005313 if (data == NULL)
5314 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005315 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005316 data->args.fhandle = &data->fh;
5317 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005318 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005319 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005320 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005321 data->res.fattr = &data->fattr;
5322 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005323 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005324 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005325 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005326 data->inode = nfs_igrab_and_active(inode);
5327 if (data->inode)
5328 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005329
Trond Myklebustc970aa82007-07-14 15:39:59 -04005330 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005331 msg.rpc_argp = &data->args;
5332 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005333 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005334 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005335 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005336 if (!issync)
5337 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005338 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005339 if (status != 0)
5340 goto out;
5341 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005342 if (status == 0)
5343 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5344 else
5345 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005346out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005347 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005348 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349}
5350
Trond Myklebuste6f81072008-01-24 18:14:34 -05005351int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352{
5353 struct nfs_server *server = NFS_SERVER(inode);
5354 struct nfs4_exception exception = { };
5355 int err;
5356 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005357 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005358 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 switch (err) {
5360 case -NFS4ERR_STALE_STATEID:
5361 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 case 0:
5363 return 0;
5364 }
5365 err = nfs4_handle_exception(server, err, &exception);
5366 } while (exception.retry);
5367 return err;
5368}
5369
5370#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5371#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5372
5373/*
5374 * sleep, with exponential backoff, and retry the LOCK operation.
5375 */
5376static unsigned long
5377nfs4_set_lock_task_retry(unsigned long timeout)
5378{
Colin Cross416ad3c2013-05-06 23:50:06 +00005379 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 timeout <<= 1;
5381 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5382 return NFS4_LOCK_MAXTIMEOUT;
5383 return timeout;
5384}
5385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5387{
5388 struct inode *inode = state->inode;
5389 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005390 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005391 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005393 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005395 struct nfs_lockt_res res = {
5396 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 };
5398 struct rpc_message msg = {
5399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5400 .rpc_argp = &arg,
5401 .rpc_resp = &res,
5402 .rpc_cred = state->owner->so_cred,
5403 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 struct nfs4_lock_state *lsp;
5405 int status;
5406
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005407 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005408 status = nfs4_set_lock_state(state, request);
5409 if (status != 0)
5410 goto out;
5411 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005412 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005413 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005414 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005415 switch (status) {
5416 case 0:
5417 request->fl_type = F_UNLCK;
5418 break;
5419 case -NFS4ERR_DENIED:
5420 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005422 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005423 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005424out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 return status;
5426}
5427
5428static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5429{
5430 struct nfs4_exception exception = { };
5431 int err;
5432
5433 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005434 err = _nfs4_proc_getlk(state, cmd, request);
5435 trace_nfs4_get_lock(request, state, cmd, err);
5436 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 &exception);
5438 } while (exception.retry);
5439 return err;
5440}
5441
5442static int do_vfs_lock(struct file *file, struct file_lock *fl)
5443{
5444 int res = 0;
5445 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5446 case FL_POSIX:
5447 res = posix_lock_file_wait(file, fl);
5448 break;
5449 case FL_FLOCK:
5450 res = flock_lock_file_wait(file, fl);
5451 break;
5452 default:
5453 BUG();
5454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 return res;
5456}
5457
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005458struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005459 struct nfs_locku_args arg;
5460 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005461 struct nfs4_lock_state *lsp;
5462 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005463 struct file_lock fl;
5464 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005465 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005466};
5467
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005468static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5469 struct nfs_open_context *ctx,
5470 struct nfs4_lock_state *lsp,
5471 struct nfs_seqid *seqid)
5472{
5473 struct nfs4_unlockdata *p;
5474 struct inode *inode = lsp->ls_state->inode;
5475
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005476 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005477 if (p == NULL)
5478 return NULL;
5479 p->arg.fh = NFS_FH(inode);
5480 p->arg.fl = &p->fl;
5481 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005482 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005483 p->lsp = lsp;
5484 atomic_inc(&lsp->ls_count);
5485 /* Ensure we don't close file until we're done freeing locks! */
5486 p->ctx = get_nfs_open_context(ctx);
5487 memcpy(&p->fl, fl, sizeof(p->fl));
5488 p->server = NFS_SERVER(inode);
5489 return p;
5490}
5491
Trond Myklebust06f814a2006-01-03 09:55:07 +01005492static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005493{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005494 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005495 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005496 nfs4_put_lock_state(calldata->lsp);
5497 put_nfs_open_context(calldata->ctx);
5498 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005499}
5500
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005501static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005502{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005503 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005504
Trond Myklebust14516c32010-07-31 14:29:06 -04005505 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5506 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005507 switch (task->tk_status) {
5508 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005509 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005510 do_vfs_lock(calldata->fl.fl_file, &calldata->fl);
5511 if (nfs4_update_lock_stateid(calldata->lsp,
5512 &calldata->res.stateid))
5513 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005514 case -NFS4ERR_BAD_STATEID:
5515 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005516 case -NFS4ERR_STALE_STATEID:
5517 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005518 if (!nfs4_stateid_match(&calldata->arg.stateid,
5519 &calldata->lsp->ls_stateid))
5520 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005521 break;
5522 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005523 if (nfs4_async_handle_error(task, calldata->server,
5524 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005525 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005526 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005527 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005528}
5529
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005530static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005531{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005532 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005534 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005535 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005536 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005537 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005538 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005539 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005540 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005541 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005542 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005543 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005544 &calldata->res.seq_res,
5545 task) != 0)
5546 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005547 return;
5548out_no_action:
5549 task->tk_action = NULL;
5550out_wait:
5551 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552}
5553
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005554static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005555 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005556 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005557 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005558};
5559
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005560static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5561 struct nfs_open_context *ctx,
5562 struct nfs4_lock_state *lsp,
5563 struct nfs_seqid *seqid)
5564{
5565 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005566 struct rpc_message msg = {
5567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5568 .rpc_cred = ctx->cred,
5569 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005570 struct rpc_task_setup task_setup_data = {
5571 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005572 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005573 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005574 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005575 .flags = RPC_TASK_ASYNC,
5576 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005577
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005578 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5579 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5580
Frank Filz137d6ac2007-07-09 15:32:29 -07005581 /* Ensure this is an unlock - when canceling a lock, the
5582 * canceled lock is passed in, and it won't be an unlock.
5583 */
5584 fl->fl_type = F_UNLCK;
5585
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005586 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5587 if (data == NULL) {
5588 nfs_free_seqid(seqid);
5589 return ERR_PTR(-ENOMEM);
5590 }
5591
Chuck Levera9c92d62013-08-09 12:48:18 -04005592 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005593 msg.rpc_argp = &data->arg;
5594 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005595 task_setup_data.callback_data = data;
5596 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005597}
5598
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5600{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005601 struct inode *inode = state->inode;
5602 struct nfs4_state_owner *sp = state->owner;
5603 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005604 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005605 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005606 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005607 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005608 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005609 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Trond Myklebust9b073572006-06-29 16:38:34 -04005611 status = nfs4_set_lock_state(state, request);
5612 /* Unlock _before_ we do the RPC call */
5613 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005614 /* Exclude nfs_delegation_claim_locks() */
5615 mutex_lock(&sp->so_delegreturn_mutex);
5616 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005617 down_read(&nfsi->rwsem);
5618 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5619 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005620 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005621 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005622 }
5623 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005624 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005625 if (status != 0)
5626 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005627 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005628 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005629 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5630 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005631 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5632 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005633 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005634 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005635 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005636 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005637 status = PTR_ERR(task);
5638 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005639 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005640 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005641 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005642out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005643 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005644 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005645 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646}
5647
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005648struct nfs4_lockdata {
5649 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005650 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005651 struct nfs4_lock_state *lsp;
5652 struct nfs_open_context *ctx;
5653 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005654 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005655 int rpc_status;
5656 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005657 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005658};
5659
5660static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005661 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5662 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005663{
5664 struct nfs4_lockdata *p;
5665 struct inode *inode = lsp->ls_state->inode;
5666 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005667 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005668
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005669 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005670 if (p == NULL)
5671 return NULL;
5672
5673 p->arg.fh = NFS_FH(inode);
5674 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005675 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005676 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005677 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005678 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5679 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005680 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005681 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005682 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005683 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005684 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005685 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005686 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005687 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005688 atomic_inc(&lsp->ls_count);
5689 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005690 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005691 memcpy(&p->fl, fl, sizeof(p->fl));
5692 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005693out_free_seqid:
5694 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005695out_free:
5696 kfree(p);
5697 return NULL;
5698}
5699
5700static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5701{
5702 struct nfs4_lockdata *data = calldata;
5703 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
Harvey Harrison3110ff82008-05-02 13:42:44 -07005705 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005706 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005707 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005708 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005709 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005710 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005711 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005712 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005713 nfs4_stateid_copy(&data->arg.open_stateid,
5714 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005715 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005716 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005717 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005718 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005719 nfs4_stateid_copy(&data->arg.lock_stateid,
5720 &data->lsp->ls_stateid);
5721 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005722 if (!nfs4_valid_open_stateid(state)) {
5723 data->rpc_status = -EBADF;
5724 task->tk_action = NULL;
5725 goto out_release_open_seqid;
5726 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005727 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005728 if (nfs4_setup_sequence(data->server,
5729 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005730 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005731 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005732 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005733out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005734 nfs_release_seqid(data->arg.open_seqid);
5735out_release_lock_seqid:
5736 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005737out_wait:
5738 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005739 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005740}
5741
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005742static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5743{
5744 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005745 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005746
Harvey Harrison3110ff82008-05-02 13:42:44 -07005747 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005748
Trond Myklebust14516c32010-07-31 14:29:06 -04005749 if (!nfs4_sequence_done(task, &data->res.seq_res))
5750 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005751
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005752 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005753 switch (task->tk_status) {
5754 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005755 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005756 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005757 if (data->arg.new_lock) {
5758 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5759 if (do_vfs_lock(data->fl.fl_file, &data->fl) < 0) {
5760 rpc_restart_call_prepare(task);
5761 break;
5762 }
5763 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005764 if (data->arg.new_lock_owner != 0) {
5765 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5766 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5767 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5768 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5769 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005770 break;
5771 case -NFS4ERR_BAD_STATEID:
5772 case -NFS4ERR_OLD_STATEID:
5773 case -NFS4ERR_STALE_STATEID:
5774 case -NFS4ERR_EXPIRED:
5775 if (data->arg.new_lock_owner != 0) {
5776 if (!nfs4_stateid_match(&data->arg.open_stateid,
5777 &lsp->ls_state->open_stateid))
5778 rpc_restart_call_prepare(task);
5779 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5780 &lsp->ls_stateid))
5781 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005782 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005783 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005784}
5785
5786static void nfs4_lock_release(void *calldata)
5787{
5788 struct nfs4_lockdata *data = calldata;
5789
Harvey Harrison3110ff82008-05-02 13:42:44 -07005790 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005791 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005792 if (data->cancelled != 0) {
5793 struct rpc_task *task;
5794 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5795 data->arg.lock_seqid);
5796 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005797 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005798 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005799 } else
5800 nfs_free_seqid(data->arg.lock_seqid);
5801 nfs4_put_lock_state(data->lsp);
5802 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005803 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005804 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005805 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005806}
5807
5808static const struct rpc_call_ops nfs4_lock_ops = {
5809 .rpc_call_prepare = nfs4_lock_prepare,
5810 .rpc_call_done = nfs4_lock_done,
5811 .rpc_release = nfs4_lock_release,
5812};
5813
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005814static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5815{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005816 switch (error) {
5817 case -NFS4ERR_ADMIN_REVOKED:
5818 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005819 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005820 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005821 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005822 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005823 break;
5824 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005825 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005826 case -NFS4ERR_EXPIRED:
5827 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005828 };
5829}
5830
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005831static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005832{
5833 struct nfs4_lockdata *data;
5834 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005835 struct rpc_message msg = {
5836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5837 .rpc_cred = state->owner->so_cred,
5838 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005839 struct rpc_task_setup task_setup_data = {
5840 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005841 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005842 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005843 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005844 .flags = RPC_TASK_ASYNC,
5845 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005846 int ret;
5847
Harvey Harrison3110ff82008-05-02 13:42:44 -07005848 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005849 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005850 fl->fl_u.nfs4_fl.owner,
5851 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005852 if (data == NULL)
5853 return -ENOMEM;
5854 if (IS_SETLKW(cmd))
5855 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005856 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005857 msg.rpc_argp = &data->arg;
5858 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005859 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005860 if (recovery_type > NFS_LOCK_NEW) {
5861 if (recovery_type == NFS_LOCK_RECLAIM)
5862 data->arg.reclaim = NFS_LOCK_RECLAIM;
5863 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005864 } else
5865 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005866 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005867 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005868 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005869 ret = nfs4_wait_for_completion_rpc_task(task);
5870 if (ret == 0) {
5871 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005872 if (ret)
5873 nfs4_handle_setlk_error(data->server, data->lsp,
5874 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005875 } else
5876 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005877 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005878 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005879 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880}
5881
5882static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5883{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005884 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005885 struct nfs4_exception exception = {
5886 .inode = state->inode,
5887 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005888 int err;
5889
5890 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005891 /* Cache the lock if possible... */
5892 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5893 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005894 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005895 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005896 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005897 break;
5898 nfs4_handle_exception(server, err, &exception);
5899 } while (exception.retry);
5900 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901}
5902
5903static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5904{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005905 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005906 struct nfs4_exception exception = {
5907 .inode = state->inode,
5908 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005909 int err;
5910
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005911 err = nfs4_set_lock_state(state, request);
5912 if (err != 0)
5913 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005914 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005915 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5916 return 0;
5917 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005918 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005919 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5920 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005921 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005922 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005923 switch (err) {
5924 default:
5925 goto out;
5926 case -NFS4ERR_GRACE:
5927 case -NFS4ERR_DELAY:
5928 nfs4_handle_exception(server, err, &exception);
5929 err = 0;
5930 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005931 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005932out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005933 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934}
5935
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005936#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005937/**
5938 * nfs41_check_expired_locks - possibly free a lock stateid
5939 *
5940 * @state: NFSv4 state for an inode
5941 *
5942 * Returns NFS_OK if recovery for this stateid is now finished.
5943 * Otherwise a negative NFS4ERR value is returned.
5944 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005945static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005946{
Chuck Levereb64cf92012-07-11 16:30:05 -04005947 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005948 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005949 struct nfs_server *server = NFS_SERVER(state->inode);
5950
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005951 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005952 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005953 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5954
5955 status = nfs41_test_stateid(server,
5956 &lsp->ls_stateid,
5957 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005958 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005959 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005960 /* Free the stateid unless the server
5961 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005962 if (status != -NFS4ERR_BAD_STATEID)
5963 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005964 &lsp->ls_stateid,
5965 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005966 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005967 ret = status;
5968 }
5969 }
5970 };
5971
5972 return ret;
5973}
5974
5975static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5976{
5977 int status = NFS_OK;
5978
5979 if (test_bit(LK_STATE_IN_USE, &state->flags))
5980 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005981 if (status != NFS_OK)
5982 status = nfs4_lock_expired(state, request);
5983 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005984}
5985#endif
5986
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5988{
Trond Myklebust19e03c52008-12-23 15:21:44 -05005989 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005990 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005991 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005993 if ((fl_flags & FL_POSIX) &&
5994 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5995 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005996 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005997 status = nfs4_set_lock_state(state, request);
5998 if (status != 0)
5999 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006000 request->fl_flags |= FL_ACCESS;
6001 status = do_vfs_lock(request->fl_file, request);
6002 if (status < 0)
6003 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006004 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006005 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006006 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006007 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006008 request->fl_flags = fl_flags & ~FL_SLEEP;
6009 status = do_vfs_lock(request->fl_file, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006010 up_read(&nfsi->rwsem);
6011 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006012 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006013 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006014 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006015out:
6016 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 return status;
6018}
6019
6020static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6021{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006022 struct nfs4_exception exception = {
6023 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006024 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006025 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 int err;
6027
6028 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006029 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006030 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07006031 if (err == -NFS4ERR_DENIED)
6032 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006034 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 } while (exception.retry);
6036 return err;
6037}
6038
6039static int
6040nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6041{
6042 struct nfs_open_context *ctx;
6043 struct nfs4_state *state;
6044 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6045 int status;
6046
6047 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006048 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 state = ctx->state;
6050
6051 if (request->fl_start < 0 || request->fl_end < 0)
6052 return -EINVAL;
6053
Trond Myklebustd9531262009-07-21 19:22:38 -04006054 if (IS_GETLK(cmd)) {
6055 if (state != NULL)
6056 return nfs4_proc_getlk(state, F_GETLK, request);
6057 return 0;
6058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
6060 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6061 return -EINVAL;
6062
Trond Myklebustd9531262009-07-21 19:22:38 -04006063 if (request->fl_type == F_UNLCK) {
6064 if (state != NULL)
6065 return nfs4_proc_unlck(state, cmd, request);
6066 return 0;
6067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
Trond Myklebustd9531262009-07-21 19:22:38 -04006069 if (state == NULL)
6070 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006071 /*
6072 * Don't rely on the VFS having checked the file open mode,
6073 * since it won't do this for flock() locks.
6074 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006075 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006076 case F_RDLCK:
6077 if (!(filp->f_mode & FMODE_READ))
6078 return -EBADF;
6079 break;
6080 case F_WRLCK:
6081 if (!(filp->f_mode & FMODE_WRITE))
6082 return -EBADF;
6083 }
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 do {
6086 status = nfs4_proc_setlk(state, cmd, request);
6087 if ((status != -EAGAIN) || IS_SETLK(cmd))
6088 break;
6089 timeout = nfs4_set_lock_task_retry(timeout);
6090 status = -ERESTARTSYS;
6091 if (signalled())
6092 break;
6093 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 return status;
6095}
6096
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006097int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006098{
6099 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006100 int err;
6101
6102 err = nfs4_set_lock_state(state, fl);
6103 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006104 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006105 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006106 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006107}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006108
Trond Myklebustcf470c32012-03-07 13:49:12 -05006109struct nfs_release_lockowner_data {
6110 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006111 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006112 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006113 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006114 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006115};
6116
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006117static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6118{
6119 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006120 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006121 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6122 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006123 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006124 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006125}
6126
6127static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6128{
6129 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006130 struct nfs_server *server = data->server;
6131
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006132 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006133
6134 switch (task->tk_status) {
6135 case 0:
6136 renew_lease(server, data->timestamp);
6137 break;
6138 case -NFS4ERR_STALE_CLIENTID:
6139 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006140 nfs4_schedule_lease_recovery(server->nfs_client);
6141 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006142 case -NFS4ERR_LEASE_MOVED:
6143 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006144 if (nfs4_async_handle_error(task, server,
6145 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006146 rpc_restart_call_prepare(task);
6147 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006148}
6149
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006150static void nfs4_release_lockowner_release(void *calldata)
6151{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006152 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006153 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006154 kfree(calldata);
6155}
6156
Trond Myklebust17280172012-03-11 13:11:00 -04006157static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006158 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6159 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006160 .rpc_release = nfs4_release_lockowner_release,
6161};
6162
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006163static void
6164nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006165{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006166 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006167 struct rpc_message msg = {
6168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6169 };
6170
6171 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006172 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006173
Trond Myklebustcf470c32012-03-07 13:49:12 -05006174 data = kmalloc(sizeof(*data), GFP_NOFS);
6175 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006176 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006177 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006178 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006179 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6180 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6181 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006182
Trond Myklebustcf470c32012-03-07 13:49:12 -05006183 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006184 msg.rpc_resp = &data->res;
6185 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006186 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006187}
6188
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006189#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6190
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006191static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6192 const void *buf, size_t buflen,
6193 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006194{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006195 if (strcmp(key, "") != 0)
6196 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006197
David Howells2b0143b2015-03-17 22:25:59 +00006198 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006199}
6200
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006201static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6202 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006203{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006204 if (strcmp(key, "") != 0)
6205 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006206
David Howells2b0143b2015-03-17 22:25:59 +00006207 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006208}
6209
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006210static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6211 size_t list_len, const char *name,
6212 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006213{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006214 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006215
David Howells2b0143b2015-03-17 22:25:59 +00006216 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
J. Bruce Fields096455a2006-03-20 23:23:42 -05006217 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006218
6219 if (list && len <= list_len)
6220 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006221 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006222}
6223
David Quigleyc9bccef2013-05-22 12:50:45 -04006224#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6225static inline int nfs4_server_supports_labels(struct nfs_server *server)
6226{
6227 return server->caps & NFS_CAP_SECURITY_LABEL;
6228}
6229
6230static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6231 const void *buf, size_t buflen,
6232 int flags, int type)
6233{
6234 if (security_ismaclabel(key))
6235 return nfs4_set_security_label(dentry, buf, buflen);
6236
6237 return -EOPNOTSUPP;
6238}
6239
6240static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6241 void *buf, size_t buflen, int type)
6242{
6243 if (security_ismaclabel(key))
David Howells2b0143b2015-03-17 22:25:59 +00006244 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006245 return -EOPNOTSUPP;
6246}
6247
6248static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6249 size_t list_len, const char *name,
6250 size_t name_len, int type)
6251{
6252 size_t len = 0;
6253
David Howells2b0143b2015-03-17 22:25:59 +00006254 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6255 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
David Quigleyc9bccef2013-05-22 12:50:45 -04006256 if (list && len <= list_len)
David Howells2b0143b2015-03-17 22:25:59 +00006257 security_inode_listsecurity(d_inode(dentry), list, len);
David Quigleyc9bccef2013-05-22 12:50:45 -04006258 }
6259 return len;
6260}
6261
6262static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6263 .prefix = XATTR_SECURITY_PREFIX,
6264 .list = nfs4_xattr_list_nfs4_label,
6265 .get = nfs4_xattr_get_nfs4_label,
6266 .set = nfs4_xattr_set_nfs4_label,
6267};
6268#endif
6269
6270
Andy Adamson533eb462011-06-13 18:25:56 -04006271/*
6272 * nfs_fhget will use either the mounted_on_fileid or the fileid
6273 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006274static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6275{
Andy Adamson533eb462011-06-13 18:25:56 -04006276 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6277 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6278 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006279 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006280 return;
6281
6282 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006283 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006284 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6285 fattr->nlink = 2;
6286}
6287
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006288static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6289 const struct qstr *name,
6290 struct nfs4_fs_locations *fs_locations,
6291 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006292{
6293 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006294 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006295 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006296 };
6297 struct nfs4_fs_locations_arg args = {
6298 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006299 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006300 .page = page,
6301 .bitmask = bitmask,
6302 };
Benny Halevy22958462009-04-01 09:22:02 -04006303 struct nfs4_fs_locations_res res = {
6304 .fs_locations = fs_locations,
6305 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006306 struct rpc_message msg = {
6307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6308 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006309 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006310 };
6311 int status;
6312
Harvey Harrison3110ff82008-05-02 13:42:44 -07006313 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006314
6315 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6316 * is not supported */
6317 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6318 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6319 else
6320 bitmask[0] |= FATTR4_WORD0_FILEID;
6321
Trond Myklebustc228fd32007-01-13 02:28:11 -05006322 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006323 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006324 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006325 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006326 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006327 return status;
6328}
6329
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006330int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6331 const struct qstr *name,
6332 struct nfs4_fs_locations *fs_locations,
6333 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006334{
6335 struct nfs4_exception exception = { };
6336 int err;
6337 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006338 err = _nfs4_proc_fs_locations(client, dir, name,
6339 fs_locations, page);
6340 trace_nfs4_get_fs_locations(dir, name, err);
6341 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006342 &exception);
6343 } while (exception.retry);
6344 return err;
6345}
6346
Chuck Leverb03d7352013-10-17 14:12:50 -04006347/*
6348 * This operation also signals the server that this client is
6349 * performing migration recovery. The server can stop returning
6350 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6351 * appended to this compound to identify the client ID which is
6352 * performing recovery.
6353 */
6354static int _nfs40_proc_get_locations(struct inode *inode,
6355 struct nfs4_fs_locations *locations,
6356 struct page *page, struct rpc_cred *cred)
6357{
6358 struct nfs_server *server = NFS_SERVER(inode);
6359 struct rpc_clnt *clnt = server->client;
6360 u32 bitmask[2] = {
6361 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6362 };
6363 struct nfs4_fs_locations_arg args = {
6364 .clientid = server->nfs_client->cl_clientid,
6365 .fh = NFS_FH(inode),
6366 .page = page,
6367 .bitmask = bitmask,
6368 .migration = 1, /* skip LOOKUP */
6369 .renew = 1, /* append RENEW */
6370 };
6371 struct nfs4_fs_locations_res res = {
6372 .fs_locations = locations,
6373 .migration = 1,
6374 .renew = 1,
6375 };
6376 struct rpc_message msg = {
6377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6378 .rpc_argp = &args,
6379 .rpc_resp = &res,
6380 .rpc_cred = cred,
6381 };
6382 unsigned long now = jiffies;
6383 int status;
6384
6385 nfs_fattr_init(&locations->fattr);
6386 locations->server = server;
6387 locations->nlocations = 0;
6388
6389 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6390 nfs4_set_sequence_privileged(&args.seq_args);
6391 status = nfs4_call_sync_sequence(clnt, server, &msg,
6392 &args.seq_args, &res.seq_res);
6393 if (status)
6394 return status;
6395
6396 renew_lease(server, now);
6397 return 0;
6398}
6399
6400#ifdef CONFIG_NFS_V4_1
6401
6402/*
6403 * This operation also signals the server that this client is
6404 * performing migration recovery. The server can stop asserting
6405 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6406 * performing this operation is identified in the SEQUENCE
6407 * operation in this compound.
6408 *
6409 * When the client supports GETATTR(fs_locations_info), it can
6410 * be plumbed in here.
6411 */
6412static int _nfs41_proc_get_locations(struct inode *inode,
6413 struct nfs4_fs_locations *locations,
6414 struct page *page, struct rpc_cred *cred)
6415{
6416 struct nfs_server *server = NFS_SERVER(inode);
6417 struct rpc_clnt *clnt = server->client;
6418 u32 bitmask[2] = {
6419 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6420 };
6421 struct nfs4_fs_locations_arg args = {
6422 .fh = NFS_FH(inode),
6423 .page = page,
6424 .bitmask = bitmask,
6425 .migration = 1, /* skip LOOKUP */
6426 };
6427 struct nfs4_fs_locations_res res = {
6428 .fs_locations = locations,
6429 .migration = 1,
6430 };
6431 struct rpc_message msg = {
6432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6433 .rpc_argp = &args,
6434 .rpc_resp = &res,
6435 .rpc_cred = cred,
6436 };
6437 int status;
6438
6439 nfs_fattr_init(&locations->fattr);
6440 locations->server = server;
6441 locations->nlocations = 0;
6442
6443 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6444 nfs4_set_sequence_privileged(&args.seq_args);
6445 status = nfs4_call_sync_sequence(clnt, server, &msg,
6446 &args.seq_args, &res.seq_res);
6447 if (status == NFS4_OK &&
6448 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6449 status = -NFS4ERR_LEASE_MOVED;
6450 return status;
6451}
6452
6453#endif /* CONFIG_NFS_V4_1 */
6454
6455/**
6456 * nfs4_proc_get_locations - discover locations for a migrated FSID
6457 * @inode: inode on FSID that is migrating
6458 * @locations: result of query
6459 * @page: buffer
6460 * @cred: credential to use for this operation
6461 *
6462 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6463 * operation failed, or a negative errno if a local error occurred.
6464 *
6465 * On success, "locations" is filled in, but if the server has
6466 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6467 * asserted.
6468 *
6469 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6470 * from this client that require migration recovery.
6471 */
6472int nfs4_proc_get_locations(struct inode *inode,
6473 struct nfs4_fs_locations *locations,
6474 struct page *page, struct rpc_cred *cred)
6475{
6476 struct nfs_server *server = NFS_SERVER(inode);
6477 struct nfs_client *clp = server->nfs_client;
6478 const struct nfs4_mig_recovery_ops *ops =
6479 clp->cl_mvops->mig_recovery_ops;
6480 struct nfs4_exception exception = { };
6481 int status;
6482
6483 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6484 (unsigned long long)server->fsid.major,
6485 (unsigned long long)server->fsid.minor,
6486 clp->cl_hostname);
6487 nfs_display_fhandle(NFS_FH(inode), __func__);
6488
6489 do {
6490 status = ops->get_locations(inode, locations, page, cred);
6491 if (status != -NFS4ERR_DELAY)
6492 break;
6493 nfs4_handle_exception(server, status, &exception);
6494 } while (exception.retry);
6495 return status;
6496}
6497
Chuck Lever44c99932013-10-17 14:13:30 -04006498/*
6499 * This operation also signals the server that this client is
6500 * performing "lease moved" recovery. The server can stop
6501 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6502 * is appended to this compound to identify the client ID which is
6503 * performing recovery.
6504 */
6505static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6506{
6507 struct nfs_server *server = NFS_SERVER(inode);
6508 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6509 struct rpc_clnt *clnt = server->client;
6510 struct nfs4_fsid_present_arg args = {
6511 .fh = NFS_FH(inode),
6512 .clientid = clp->cl_clientid,
6513 .renew = 1, /* append RENEW */
6514 };
6515 struct nfs4_fsid_present_res res = {
6516 .renew = 1,
6517 };
6518 struct rpc_message msg = {
6519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6520 .rpc_argp = &args,
6521 .rpc_resp = &res,
6522 .rpc_cred = cred,
6523 };
6524 unsigned long now = jiffies;
6525 int status;
6526
6527 res.fh = nfs_alloc_fhandle();
6528 if (res.fh == NULL)
6529 return -ENOMEM;
6530
6531 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6532 nfs4_set_sequence_privileged(&args.seq_args);
6533 status = nfs4_call_sync_sequence(clnt, server, &msg,
6534 &args.seq_args, &res.seq_res);
6535 nfs_free_fhandle(res.fh);
6536 if (status)
6537 return status;
6538
6539 do_renew_lease(clp, now);
6540 return 0;
6541}
6542
6543#ifdef CONFIG_NFS_V4_1
6544
6545/*
6546 * This operation also signals the server that this client is
6547 * performing "lease moved" recovery. The server can stop asserting
6548 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6549 * this operation is identified in the SEQUENCE operation in this
6550 * compound.
6551 */
6552static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6553{
6554 struct nfs_server *server = NFS_SERVER(inode);
6555 struct rpc_clnt *clnt = server->client;
6556 struct nfs4_fsid_present_arg args = {
6557 .fh = NFS_FH(inode),
6558 };
6559 struct nfs4_fsid_present_res res = {
6560 };
6561 struct rpc_message msg = {
6562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6563 .rpc_argp = &args,
6564 .rpc_resp = &res,
6565 .rpc_cred = cred,
6566 };
6567 int status;
6568
6569 res.fh = nfs_alloc_fhandle();
6570 if (res.fh == NULL)
6571 return -ENOMEM;
6572
6573 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6574 nfs4_set_sequence_privileged(&args.seq_args);
6575 status = nfs4_call_sync_sequence(clnt, server, &msg,
6576 &args.seq_args, &res.seq_res);
6577 nfs_free_fhandle(res.fh);
6578 if (status == NFS4_OK &&
6579 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6580 status = -NFS4ERR_LEASE_MOVED;
6581 return status;
6582}
6583
6584#endif /* CONFIG_NFS_V4_1 */
6585
6586/**
6587 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6588 * @inode: inode on FSID to check
6589 * @cred: credential to use for this operation
6590 *
6591 * Server indicates whether the FSID is present, moved, or not
6592 * recognized. This operation is necessary to clear a LEASE_MOVED
6593 * condition for this client ID.
6594 *
6595 * Returns NFS4_OK if the FSID is present on this server,
6596 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6597 * NFS4ERR code if some error occurred on the server, or a
6598 * negative errno if a local failure occurred.
6599 */
6600int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6601{
6602 struct nfs_server *server = NFS_SERVER(inode);
6603 struct nfs_client *clp = server->nfs_client;
6604 const struct nfs4_mig_recovery_ops *ops =
6605 clp->cl_mvops->mig_recovery_ops;
6606 struct nfs4_exception exception = { };
6607 int status;
6608
6609 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6610 (unsigned long long)server->fsid.major,
6611 (unsigned long long)server->fsid.minor,
6612 clp->cl_hostname);
6613 nfs_display_fhandle(NFS_FH(inode), __func__);
6614
6615 do {
6616 status = ops->fsid_present(inode, cred);
6617 if (status != -NFS4ERR_DELAY)
6618 break;
6619 nfs4_handle_exception(server, status, &exception);
6620 } while (exception.retry);
6621 return status;
6622}
6623
Andy Adamson5ec16a82013-08-08 10:57:55 -04006624/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006625 * If 'use_integrity' is true and the state managment nfs_client
6626 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6627 * and the machine credential as per RFC3530bis and RFC5661 Security
6628 * Considerations sections. Otherwise, just use the user cred with the
6629 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006630 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006631static 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 +00006632{
6633 int status;
6634 struct nfs4_secinfo_arg args = {
6635 .dir_fh = NFS_FH(dir),
6636 .name = name,
6637 };
6638 struct nfs4_secinfo_res res = {
6639 .flavors = flavors,
6640 };
6641 struct rpc_message msg = {
6642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6643 .rpc_argp = &args,
6644 .rpc_resp = &res,
6645 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006646 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006647 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006648
6649 if (use_integrity) {
6650 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006651 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6652 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006653 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006654
6655 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006656
6657 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6658 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6659
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006660 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6661 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006662 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006663
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006664 if (cred)
6665 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006666
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006667 return status;
6668}
6669
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006670int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6671 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006672{
6673 struct nfs4_exception exception = { };
6674 int err;
6675 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006676 err = -NFS4ERR_WRONGSEC;
6677
6678 /* try to use integrity protection with machine cred */
6679 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6680 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6681
6682 /*
6683 * if unable to use integrity protection, or SECINFO with
6684 * integrity protection returns NFS4ERR_WRONGSEC (which is
6685 * disallowed by spec, but exists in deployed servers) use
6686 * the current filesystem's rpc_client and the user cred.
6687 */
6688 if (err == -NFS4ERR_WRONGSEC)
6689 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6690
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006691 trace_nfs4_secinfo(dir, name, err);
6692 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006693 &exception);
6694 } while (exception.retry);
6695 return err;
6696}
6697
Andy Adamson557134a2009-04-01 09:21:53 -04006698#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006699/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006700 * Check the exchange flags returned by the server for invalid flags, having
6701 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6702 * DS flags set.
6703 */
6704static int nfs4_check_cl_exchange_flags(u32 flags)
6705{
6706 if (flags & ~EXCHGID4_FLAG_MASK_R)
6707 goto out_inval;
6708 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6709 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6710 goto out_inval;
6711 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6712 goto out_inval;
6713 return NFS_OK;
6714out_inval:
6715 return -NFS4ERR_INVAL;
6716}
6717
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006718static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006719nfs41_same_server_scope(struct nfs41_server_scope *a,
6720 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006721{
6722 if (a->server_scope_sz == b->server_scope_sz &&
6723 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6724 return true;
6725
6726 return false;
6727}
6728
Andy Adamson357f54d2010-12-14 10:11:57 -05006729/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006730 * nfs4_proc_bind_conn_to_session()
6731 *
6732 * The 4.1 client currently uses the same TCP connection for the
6733 * fore and backchannel.
6734 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006735int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006736{
6737 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006738 struct nfs41_bind_conn_to_session_args args = {
6739 .client = clp,
6740 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6741 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006742 struct nfs41_bind_conn_to_session_res res;
6743 struct rpc_message msg = {
6744 .rpc_proc =
6745 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006746 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006747 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006748 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006749 };
6750
6751 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006752
Trond Myklebust71a097c2015-02-18 09:27:18 -08006753 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6754 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6755 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006756
6757 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006758 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006759 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006760 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006761 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6762 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6763 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006764 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006765 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006766 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006767 dprintk("NFS: %s: Unexpected direction from server\n",
6768 __func__);
6769 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006770 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006771 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006772 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006773 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6774 __func__);
6775 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006776 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006777 }
6778 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006779out:
6780 dprintk("<-- %s status= %d\n", __func__, status);
6781 return status;
6782}
6783
6784/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006785 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6786 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006787 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006788static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6789 .how = SP4_MACH_CRED,
6790 .enforce.u.words = {
6791 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6792 1 << (OP_EXCHANGE_ID - 32) |
6793 1 << (OP_CREATE_SESSION - 32) |
6794 1 << (OP_DESTROY_SESSION - 32) |
6795 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006796 },
6797 .allow.u.words = {
6798 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006799 1 << (OP_LOCKU) |
6800 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006801 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006802 1 << (OP_SECINFO_NO_NAME - 32) |
6803 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006804 1 << (OP_FREE_STATEID - 32) |
6805 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006806 }
6807};
6808
6809/*
6810 * Select the state protection mode for client `clp' given the server results
6811 * from exchange_id in `sp'.
6812 *
6813 * Returns 0 on success, negative errno otherwise.
6814 */
6815static int nfs4_sp4_select_mode(struct nfs_client *clp,
6816 struct nfs41_state_protection *sp)
6817{
6818 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6819 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6820 1 << (OP_EXCHANGE_ID - 32) |
6821 1 << (OP_CREATE_SESSION - 32) |
6822 1 << (OP_DESTROY_SESSION - 32) |
6823 1 << (OP_DESTROY_CLIENTID - 32)
6824 };
6825 unsigned int i;
6826
6827 if (sp->how == SP4_MACH_CRED) {
6828 /* Print state protect result */
6829 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6830 for (i = 0; i <= LAST_NFS4_OP; i++) {
6831 if (test_bit(i, sp->enforce.u.longs))
6832 dfprintk(MOUNT, " enforce op %d\n", i);
6833 if (test_bit(i, sp->allow.u.longs))
6834 dfprintk(MOUNT, " allow op %d\n", i);
6835 }
6836
6837 /* make sure nothing is on enforce list that isn't supported */
6838 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6839 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6840 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6841 return -EINVAL;
6842 }
6843 }
6844
6845 /*
6846 * Minimal mode - state operations are allowed to use machine
6847 * credential. Note this already happens by default, so the
6848 * client doesn't have to do anything more than the negotiation.
6849 *
6850 * NOTE: we don't care if EXCHANGE_ID is in the list -
6851 * we're already using the machine cred for exchange_id
6852 * and will never use a different cred.
6853 */
6854 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6855 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6856 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6857 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6858 dfprintk(MOUNT, "sp4_mach_cred:\n");
6859 dfprintk(MOUNT, " minimal mode enabled\n");
6860 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6861 } else {
6862 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6863 return -EINVAL;
6864 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006865
6866 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6867 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6868 dfprintk(MOUNT, " cleanup mode enabled\n");
6869 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6870 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006871
6872 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6873 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6874 dfprintk(MOUNT, " secinfo mode enabled\n");
6875 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6876 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006877
6878 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6879 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6880 dfprintk(MOUNT, " stateid mode enabled\n");
6881 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6882 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006883
6884 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6885 dfprintk(MOUNT, " write mode enabled\n");
6886 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6887 }
6888
6889 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6890 dfprintk(MOUNT, " commit mode enabled\n");
6891 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6892 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006893 }
6894
6895 return 0;
6896}
6897
6898/*
6899 * _nfs4_proc_exchange_id()
6900 *
6901 * Wrapper for EXCHANGE_ID operation.
6902 */
6903static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6904 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006905{
6906 nfs4_verifier verifier;
6907 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006908 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006909 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006910#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006911 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006912 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6913 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6914#else
6915 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6916 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6917#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006918 };
6919 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006920 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006921 };
6922 int status;
6923 struct rpc_message msg = {
6924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6925 .rpc_argp = &args,
6926 .rpc_resp = &res,
6927 .rpc_cred = cred,
6928 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006929
Chuck Leverf0920752012-05-21 22:45:41 -04006930 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006931
6932 status = nfs4_init_uniform_client_string(clp);
6933 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04006934 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006935
6936 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006937 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006938 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006939
Chuck Leveracdeb692012-05-21 22:46:16 -04006940 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006941 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006942 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006943 status = -ENOMEM;
6944 goto out;
6945 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006946
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006947 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006948 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006949 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006950 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006951 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006952 }
6953
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006954 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006955 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006956 status = -ENOMEM;
6957 goto out_server_scope;
6958 }
6959
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006960 switch (sp4_how) {
6961 case SP4_NONE:
6962 args.state_protect.how = SP4_NONE;
6963 break;
6964
6965 case SP4_MACH_CRED:
6966 args.state_protect = nfs4_sp4_mach_cred_request;
6967 break;
6968
6969 default:
6970 /* unsupported! */
6971 WARN_ON_ONCE(1);
6972 status = -EINVAL;
6973 goto out_server_scope;
6974 }
6975
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006976 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006977 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006978 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006979 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006980
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006981 if (status == 0)
6982 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6983
Chuck Lever177313f2012-05-21 22:44:58 -04006984 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006985 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05006986 clp->cl_exchange_flags = res.flags;
6987 /* Client ID is not confirmed */
6988 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
6989 clear_bit(NFS4_SESSION_ESTABLISHED,
6990 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04006991 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05006992 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006993
Chuck Leveracdeb692012-05-21 22:46:16 -04006994 kfree(clp->cl_serverowner);
6995 clp->cl_serverowner = res.server_owner;
6996 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006997
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006998 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006999 kfree(clp->cl_implid);
7000 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007001
Chuck Lever177313f2012-05-21 22:44:58 -04007002 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007003 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007004 res.server_scope)) {
7005 dprintk("%s: server_scope mismatch detected\n",
7006 __func__);
7007 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007008 kfree(clp->cl_serverscope);
7009 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007010 }
7011
Chuck Lever177313f2012-05-21 22:44:58 -04007012 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007013 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007014 goto out;
7015 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007016 } else
7017 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007018
Chuck Leveracdeb692012-05-21 22:46:16 -04007019out_server_owner:
7020 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007021out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007022 kfree(res.server_scope);
7023out:
Chuck Lever177313f2012-05-21 22:44:58 -04007024 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007025 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007026 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007027 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007028 clp->cl_implid->date.seconds,
7029 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007030 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007031 return status;
7032}
7033
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007034/*
7035 * nfs4_proc_exchange_id()
7036 *
7037 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7038 *
7039 * Since the clientid has expired, all compounds using sessions
7040 * associated with the stale clientid will be returning
7041 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7042 * be in some phase of session reset.
7043 *
7044 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7045 */
7046int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7047{
7048 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7049 int status;
7050
7051 /* try SP4_MACH_CRED if krb5i/p */
7052 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7053 authflavor == RPC_AUTH_GSS_KRB5P) {
7054 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7055 if (!status)
7056 return 0;
7057 }
7058
7059 /* try SP4_NONE */
7060 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7061}
7062
Trond Myklebust66245532012-05-25 17:18:09 -04007063static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7064 struct rpc_cred *cred)
7065{
7066 struct rpc_message msg = {
7067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7068 .rpc_argp = clp,
7069 .rpc_cred = cred,
7070 };
7071 int status;
7072
7073 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007074 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007075 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007076 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007077 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7078 return status;
7079}
7080
7081static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7082 struct rpc_cred *cred)
7083{
7084 unsigned int loop;
7085 int ret;
7086
7087 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7088 ret = _nfs4_proc_destroy_clientid(clp, cred);
7089 switch (ret) {
7090 case -NFS4ERR_DELAY:
7091 case -NFS4ERR_CLIENTID_BUSY:
7092 ssleep(1);
7093 break;
7094 default:
7095 return ret;
7096 }
7097 }
7098 return 0;
7099}
7100
7101int nfs4_destroy_clientid(struct nfs_client *clp)
7102{
7103 struct rpc_cred *cred;
7104 int ret = 0;
7105
7106 if (clp->cl_mvops->minor_version < 1)
7107 goto out;
7108 if (clp->cl_exchange_flags == 0)
7109 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007110 if (clp->cl_preserve_clid)
7111 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007112 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007113 ret = nfs4_proc_destroy_clientid(clp, cred);
7114 if (cred)
7115 put_rpccred(cred);
7116 switch (ret) {
7117 case 0:
7118 case -NFS4ERR_STALE_CLIENTID:
7119 clp->cl_exchange_flags = 0;
7120 }
7121out:
7122 return ret;
7123}
7124
Andy Adamson2050f0c2009-04-01 09:22:30 -04007125struct nfs4_get_lease_time_data {
7126 struct nfs4_get_lease_time_args *args;
7127 struct nfs4_get_lease_time_res *res;
7128 struct nfs_client *clp;
7129};
7130
7131static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7132 void *calldata)
7133{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007134 struct nfs4_get_lease_time_data *data =
7135 (struct nfs4_get_lease_time_data *)calldata;
7136
7137 dprintk("--> %s\n", __func__);
7138 /* just setup sequence, do not trigger session recovery
7139 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007140 nfs41_setup_sequence(data->clp->cl_session,
7141 &data->args->la_seq_args,
7142 &data->res->lr_seq_res,
7143 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007144 dprintk("<-- %s\n", __func__);
7145}
7146
7147/*
7148 * Called from nfs4_state_manager thread for session setup, so don't recover
7149 * from sequence operation or clientid errors.
7150 */
7151static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7152{
7153 struct nfs4_get_lease_time_data *data =
7154 (struct nfs4_get_lease_time_data *)calldata;
7155
7156 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007157 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7158 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007159 switch (task->tk_status) {
7160 case -NFS4ERR_DELAY:
7161 case -NFS4ERR_GRACE:
7162 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7163 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7164 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007165 /* fall through */
7166 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007167 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007168 return;
7169 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007170 dprintk("<-- %s\n", __func__);
7171}
7172
Trond Myklebust17280172012-03-11 13:11:00 -04007173static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007174 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7175 .rpc_call_done = nfs4_get_lease_time_done,
7176};
7177
7178int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7179{
7180 struct rpc_task *task;
7181 struct nfs4_get_lease_time_args args;
7182 struct nfs4_get_lease_time_res res = {
7183 .lr_fsinfo = fsinfo,
7184 };
7185 struct nfs4_get_lease_time_data data = {
7186 .args = &args,
7187 .res = &res,
7188 .clp = clp,
7189 };
7190 struct rpc_message msg = {
7191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7192 .rpc_argp = &args,
7193 .rpc_resp = &res,
7194 };
7195 struct rpc_task_setup task_setup = {
7196 .rpc_client = clp->cl_rpcclient,
7197 .rpc_message = &msg,
7198 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007199 .callback_data = &data,
7200 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007201 };
7202 int status;
7203
Chuck Levera9c92d62013-08-09 12:48:18 -04007204 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007205 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007206 dprintk("--> %s\n", __func__);
7207 task = rpc_run_task(&task_setup);
7208
7209 if (IS_ERR(task))
7210 status = PTR_ERR(task);
7211 else {
7212 status = task->tk_status;
7213 rpc_put_task(task);
7214 }
7215 dprintk("<-- %s return %d\n", __func__, status);
7216
7217 return status;
7218}
7219
Andy Adamsonfc931582009-04-01 09:22:31 -04007220/*
7221 * Initialize the values to be used by the client in CREATE_SESSION
7222 * If nfs4_init_session set the fore channel request and response sizes,
7223 * use them.
7224 *
7225 * Set the back channel max_resp_sz_cached to zero to force the client to
7226 * always set csa_cachethis to FALSE because the current implementation
7227 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7228 */
7229static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7230{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007231 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007232
Andy Adamson18aad3d2013-06-26 12:21:49 -04007233 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7234 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7235
Andy Adamsonfc931582009-04-01 09:22:31 -04007236 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007237 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7238 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007239 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007240 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007241
7242 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007243 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007244 __func__,
7245 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007246 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007247
7248 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007249 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7250 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7251 args->bc_attrs.max_resp_sz_cached = 0;
7252 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7253 args->bc_attrs.max_reqs = 1;
7254
7255 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7256 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7257 __func__,
7258 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7259 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7260 args->bc_attrs.max_reqs);
7261}
7262
Trond Myklebust79969dd2015-02-18 11:30:18 -08007263static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7264 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007265{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007266 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007267 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007268
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007269 if (rcvd->max_resp_sz > sent->max_resp_sz)
7270 return -EINVAL;
7271 /*
7272 * Our requested max_ops is the minimum we need; we're not
7273 * prepared to break up compounds into smaller pieces than that.
7274 * So, no point even trying to continue if the server won't
7275 * cooperate:
7276 */
7277 if (rcvd->max_ops < sent->max_ops)
7278 return -EINVAL;
7279 if (rcvd->max_reqs == 0)
7280 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007281 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7282 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007283 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007284}
7285
Trond Myklebust79969dd2015-02-18 11:30:18 -08007286static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7287 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007288{
7289 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007290 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007291
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007292 if (!(res->flags & SESSION4_BACK_CHAN))
7293 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007294 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7295 return -EINVAL;
7296 if (rcvd->max_resp_sz < sent->max_resp_sz)
7297 return -EINVAL;
7298 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7299 return -EINVAL;
7300 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007301 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007302 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007303 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007304 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007305out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007306 return 0;
7307}
Andy Adamson8d353012009-04-01 09:22:32 -04007308
Andy Adamson8d353012009-04-01 09:22:32 -04007309static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007310 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007311{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007312 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007313
Trond Myklebust79969dd2015-02-18 11:30:18 -08007314 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007315 if (ret)
7316 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007317 return nfs4_verify_back_channel_attrs(args, res);
7318}
7319
7320static void nfs4_update_session(struct nfs4_session *session,
7321 struct nfs41_create_session_res *res)
7322{
7323 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007324 /* Mark client id and session as being confirmed */
7325 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7326 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007327 session->flags = res->flags;
7328 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007329 if (res->flags & SESSION4_BACK_CHAN)
7330 memcpy(&session->bc_attrs, &res->bc_attrs,
7331 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007332}
7333
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007334static int _nfs4_proc_create_session(struct nfs_client *clp,
7335 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007336{
7337 struct nfs4_session *session = clp->cl_session;
7338 struct nfs41_create_session_args args = {
7339 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007340 .clientid = clp->cl_clientid,
7341 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007342 .cb_program = NFS4_CALLBACK,
7343 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007344 struct nfs41_create_session_res res;
7345
Andy Adamsonfc931582009-04-01 09:22:31 -04007346 struct rpc_message msg = {
7347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7348 .rpc_argp = &args,
7349 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007350 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007351 };
7352 int status;
7353
7354 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007355 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007356
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007357 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007358 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007359
Trond Myklebust43095d32012-11-20 11:13:12 -05007360 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007361 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007362 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007363 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007364 if (clp->cl_seqid == res.seqid)
7365 clp->cl_seqid++;
7366 if (status)
7367 goto out;
7368 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007369 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007370out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007371 return status;
7372}
7373
7374/*
7375 * Issues a CREATE_SESSION operation to the server.
7376 * It is the responsibility of the caller to verify the session is
7377 * expired before calling this routine.
7378 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007379int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007380{
7381 int status;
7382 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007383 struct nfs4_session *session = clp->cl_session;
7384
7385 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7386
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007387 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007388 if (status)
7389 goto out;
7390
Andy Adamsonaacd5532011-11-09 13:58:21 -05007391 /* Init or reset the session slot tables */
7392 status = nfs4_setup_session_slot_tables(session);
7393 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007394 if (status)
7395 goto out;
7396
7397 ptr = (unsigned *)&session->sess_id.data[0];
7398 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7399 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007400out:
7401 dprintk("<-- %s\n", __func__);
7402 return status;
7403}
7404
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007405/*
7406 * Issue the over-the-wire RPC DESTROY_SESSION.
7407 * The caller must serialize access to this routine.
7408 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007409int nfs4_proc_destroy_session(struct nfs4_session *session,
7410 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007411{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007412 struct rpc_message msg = {
7413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7414 .rpc_argp = session,
7415 .rpc_cred = cred,
7416 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007417 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007418
7419 dprintk("--> nfs4_proc_destroy_session\n");
7420
7421 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007422 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7423 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007424
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007425 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007426 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007427
7428 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007429 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007430 "Session has been destroyed regardless...\n", status);
7431
7432 dprintk("<-- nfs4_proc_destroy_session\n");
7433 return status;
7434}
7435
Trond Myklebust7b38c362012-05-23 13:23:31 -04007436/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007437 * Renew the cl_session lease.
7438 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007439struct nfs4_sequence_data {
7440 struct nfs_client *clp;
7441 struct nfs4_sequence_args args;
7442 struct nfs4_sequence_res res;
7443};
7444
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007445static void nfs41_sequence_release(void *data)
7446{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007447 struct nfs4_sequence_data *calldata = data;
7448 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007449
Alexandros Batsakis71358402010-02-05 03:45:05 -08007450 if (atomic_read(&clp->cl_count) > 1)
7451 nfs4_schedule_state_renewal(clp);
7452 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007453 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007454}
7455
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007456static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7457{
7458 switch(task->tk_status) {
7459 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007460 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7461 return -EAGAIN;
7462 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007463 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007464 }
7465 return 0;
7466}
7467
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007468static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007469{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007470 struct nfs4_sequence_data *calldata = data;
7471 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007472
Trond Myklebust14516c32010-07-31 14:29:06 -04007473 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7474 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007475
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007476 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007477 if (task->tk_status < 0) {
7478 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007479 if (atomic_read(&clp->cl_count) == 1)
7480 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007481
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007482 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7483 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007484 return;
7485 }
7486 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007487 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007488out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007489 dprintk("<-- %s\n", __func__);
7490}
7491
7492static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7493{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007494 struct nfs4_sequence_data *calldata = data;
7495 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007496 struct nfs4_sequence_args *args;
7497 struct nfs4_sequence_res *res;
7498
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007499 args = task->tk_msg.rpc_argp;
7500 res = task->tk_msg.rpc_resp;
7501
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007502 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007503}
7504
7505static const struct rpc_call_ops nfs41_sequence_ops = {
7506 .rpc_call_done = nfs41_sequence_call_done,
7507 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007508 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007509};
7510
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007511static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7512 struct rpc_cred *cred,
7513 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007514{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007515 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007516 struct rpc_message msg = {
7517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7518 .rpc_cred = cred,
7519 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007520 struct rpc_task_setup task_setup_data = {
7521 .rpc_client = clp->cl_rpcclient,
7522 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007523 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007524 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007525 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007526
Alexandros Batsakis71358402010-02-05 03:45:05 -08007527 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007528 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007529 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007530 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007531 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007532 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007533 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007534 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007535 if (is_privileged)
7536 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007537 msg.rpc_argp = &calldata->args;
7538 msg.rpc_resp = &calldata->res;
7539 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007540 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007541
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007542 return rpc_run_task(&task_setup_data);
7543}
7544
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007545static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007546{
7547 struct rpc_task *task;
7548 int ret = 0;
7549
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007550 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007551 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007552 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007553 if (IS_ERR(task))
7554 ret = PTR_ERR(task);
7555 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007556 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007557 dprintk("<-- %s status=%d\n", __func__, ret);
7558 return ret;
7559}
7560
7561static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7562{
7563 struct rpc_task *task;
7564 int ret;
7565
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007566 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007567 if (IS_ERR(task)) {
7568 ret = PTR_ERR(task);
7569 goto out;
7570 }
7571 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007572 if (!ret) {
7573 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7574
7575 if (task->tk_status == 0)
7576 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007577 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007578 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007579 rpc_put_task(task);
7580out:
7581 dprintk("<-- %s status=%d\n", __func__, ret);
7582 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007583}
7584
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007585struct nfs4_reclaim_complete_data {
7586 struct nfs_client *clp;
7587 struct nfs41_reclaim_complete_args arg;
7588 struct nfs41_reclaim_complete_res res;
7589};
7590
7591static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7592{
7593 struct nfs4_reclaim_complete_data *calldata = data;
7594
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007595 nfs41_setup_sequence(calldata->clp->cl_session,
7596 &calldata->arg.seq_args,
7597 &calldata->res.seq_res,
7598 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007599}
7600
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007601static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7602{
7603 switch(task->tk_status) {
7604 case 0:
7605 case -NFS4ERR_COMPLETE_ALREADY:
7606 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7607 break;
7608 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007609 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007610 /* fall through */
7611 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007612 return -EAGAIN;
7613 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007614 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007615 }
7616 return 0;
7617}
7618
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007619static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7620{
7621 struct nfs4_reclaim_complete_data *calldata = data;
7622 struct nfs_client *clp = calldata->clp;
7623 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7624
7625 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007626 if (!nfs41_sequence_done(task, res))
7627 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007628
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007629 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007630 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7631 rpc_restart_call_prepare(task);
7632 return;
7633 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007634 dprintk("<-- %s\n", __func__);
7635}
7636
7637static void nfs4_free_reclaim_complete_data(void *data)
7638{
7639 struct nfs4_reclaim_complete_data *calldata = data;
7640
7641 kfree(calldata);
7642}
7643
7644static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7645 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7646 .rpc_call_done = nfs4_reclaim_complete_done,
7647 .rpc_release = nfs4_free_reclaim_complete_data,
7648};
7649
7650/*
7651 * Issue a global reclaim complete.
7652 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007653static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7654 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007655{
7656 struct nfs4_reclaim_complete_data *calldata;
7657 struct rpc_task *task;
7658 struct rpc_message msg = {
7659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007660 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007661 };
7662 struct rpc_task_setup task_setup_data = {
7663 .rpc_client = clp->cl_rpcclient,
7664 .rpc_message = &msg,
7665 .callback_ops = &nfs4_reclaim_complete_call_ops,
7666 .flags = RPC_TASK_ASYNC,
7667 };
7668 int status = -ENOMEM;
7669
7670 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007671 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007672 if (calldata == NULL)
7673 goto out;
7674 calldata->clp = clp;
7675 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007676
Chuck Levera9c92d62013-08-09 12:48:18 -04007677 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007678 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007679 msg.rpc_argp = &calldata->arg;
7680 msg.rpc_resp = &calldata->res;
7681 task_setup_data.callback_data = calldata;
7682 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007683 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007684 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007685 goto out;
7686 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007687 status = nfs4_wait_for_completion_rpc_task(task);
7688 if (status == 0)
7689 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007690 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007691 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007692out:
7693 dprintk("<-- %s status=%d\n", __func__, status);
7694 return status;
7695}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007696
7697static void
7698nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7699{
7700 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007701 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007702 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007703
7704 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007705 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7706 * right now covering the LAYOUTGET we are about to send.
7707 * However, that is not so catastrophic, and there seems
7708 * to be no way to prevent it completely.
7709 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007710 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007711 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007712 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007713 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7714 NFS_I(lgp->args.inode)->layout,
Peng Taoce6ab4f2014-09-06 00:53:24 +08007715 &lgp->args.range,
Fred Isamancf7d63f2011-01-06 11:36:25 +00007716 lgp->args.ctx->state)) {
7717 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007718 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007719}
7720
7721static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7722{
7723 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007724 struct inode *inode = lgp->args.inode;
7725 struct nfs_server *server = NFS_SERVER(inode);
7726 struct pnfs_layout_hdr *lo;
7727 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007728 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007729
Boaz Harroshed7e5422014-01-22 20:34:54 +02007730 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007731
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007732 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007733 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007734
7735 switch (task->tk_status) {
7736 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007737 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007738 /*
7739 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7740 * (or clients) writing to the same RAID stripe
7741 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007742 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007743 /*
7744 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7745 * existing layout before getting a new one).
7746 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007747 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007748 timeo = rpc_get_timeout(task->tk_client);
7749 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007750 now = jiffies;
7751 if (time_after(giveup, now)) {
7752 unsigned long delay;
7753
7754 /* Delay for:
7755 * - Not less then NFS4_POLL_RETRY_MIN.
7756 * - One last time a jiffie before we give up
7757 * - exponential backoff (time_now minus start_attempt)
7758 */
7759 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7760 min((giveup - now - 1),
7761 now - lgp->args.timestamp));
7762
7763 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7764 __func__, delay);
7765 rpc_delay(task, delay);
7766 task->tk_status = 0;
7767 rpc_restart_call_prepare(task);
7768 goto out; /* Do not call nfs4_async_handle_error() */
7769 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007770 break;
7771 case -NFS4ERR_EXPIRED:
7772 case -NFS4ERR_BAD_STATEID:
7773 spin_lock(&inode->i_lock);
7774 lo = NFS_I(inode)->layout;
7775 if (!lo || list_empty(&lo->plh_segs)) {
7776 spin_unlock(&inode->i_lock);
7777 /* If the open stateid was bad, then recover it. */
7778 state = lgp->args.ctx->state;
7779 } else {
7780 LIST_HEAD(head);
7781
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007782 /*
7783 * Mark the bad layout state as invalid, then retry
7784 * with the current stateid.
7785 */
Trond Myklebustee314c22012-10-01 17:25:48 -07007786 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7787 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007788 pnfs_free_lseg_list(&head);
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007789
7790 task->tk_status = 0;
7791 rpc_restart_call_prepare(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007792 }
7793 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007794 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebustee314c22012-10-01 17:25:48 -07007795 rpc_restart_call_prepare(task);
7796out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007797 dprintk("<-- %s\n", __func__);
7798}
7799
Idan Kedar85541162012-08-02 11:47:10 +03007800static size_t max_response_pages(struct nfs_server *server)
7801{
7802 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7803 return nfs_page_array_len(0, max_resp_sz);
7804}
7805
7806static void nfs4_free_pages(struct page **pages, size_t size)
7807{
7808 int i;
7809
7810 if (!pages)
7811 return;
7812
7813 for (i = 0; i < size; i++) {
7814 if (!pages[i])
7815 break;
7816 __free_page(pages[i]);
7817 }
7818 kfree(pages);
7819}
7820
7821static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7822{
7823 struct page **pages;
7824 int i;
7825
7826 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7827 if (!pages) {
7828 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7829 return NULL;
7830 }
7831
7832 for (i = 0; i < size; i++) {
7833 pages[i] = alloc_page(gfp_flags);
7834 if (!pages[i]) {
7835 dprintk("%s: failed to allocate page\n", __func__);
7836 nfs4_free_pages(pages, size);
7837 return NULL;
7838 }
7839 }
7840
7841 return pages;
7842}
7843
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007844static void nfs4_layoutget_release(void *calldata)
7845{
7846 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007847 struct inode *inode = lgp->args.inode;
7848 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007849 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007850
7851 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007852 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007853 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007854 put_nfs_open_context(lgp->args.ctx);
7855 kfree(calldata);
7856 dprintk("<-- %s\n", __func__);
7857}
7858
7859static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7860 .rpc_call_prepare = nfs4_layoutget_prepare,
7861 .rpc_call_done = nfs4_layoutget_done,
7862 .rpc_release = nfs4_layoutget_release,
7863};
7864
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007865struct pnfs_layout_segment *
7866nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007867{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007868 struct inode *inode = lgp->args.inode;
7869 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007870 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007871 struct rpc_task *task;
7872 struct rpc_message msg = {
7873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7874 .rpc_argp = &lgp->args,
7875 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007876 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007877 };
7878 struct rpc_task_setup task_setup_data = {
7879 .rpc_client = server->client,
7880 .rpc_message = &msg,
7881 .callback_ops = &nfs4_layoutget_call_ops,
7882 .callback_data = lgp,
7883 .flags = RPC_TASK_ASYNC,
7884 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007885 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007886 int status = 0;
7887
7888 dprintk("--> %s\n", __func__);
7889
Peng Tao4bd5a982014-11-17 11:05:17 +08007890 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7891 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7892
Idan Kedar85541162012-08-02 11:47:10 +03007893 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7894 if (!lgp->args.layout.pages) {
7895 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007896 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007897 }
7898 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007899 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007900
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007901 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007902 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007903 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007904
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007905 task = rpc_run_task(&task_setup_data);
7906 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007907 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007908 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007909 if (status == 0)
7910 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007911 trace_nfs4_layoutget(lgp->args.ctx,
7912 &lgp->args.range,
7913 &lgp->res.range,
7914 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007915 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7916 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007917 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007918 rpc_put_task(task);
7919 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007920 if (status)
7921 return ERR_PTR(status);
7922 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007923}
7924
Benny Halevycbe82602011-05-22 19:52:37 +03007925static void
7926nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7927{
7928 struct nfs4_layoutreturn *lrp = calldata;
7929
7930 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007931 nfs41_setup_sequence(lrp->clp->cl_session,
7932 &lrp->args.seq_args,
7933 &lrp->res.seq_res,
7934 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007935}
7936
7937static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7938{
7939 struct nfs4_layoutreturn *lrp = calldata;
7940 struct nfs_server *server;
7941
7942 dprintk("--> %s\n", __func__);
7943
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007944 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007945 return;
7946
7947 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007948 switch (task->tk_status) {
7949 default:
7950 task->tk_status = 0;
7951 case 0:
7952 break;
7953 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007954 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007955 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007956 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007957 return;
7958 }
Benny Halevycbe82602011-05-22 19:52:37 +03007959 dprintk("<-- %s\n", __func__);
7960}
7961
7962static void nfs4_layoutreturn_release(void *calldata)
7963{
7964 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007965 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007966
7967 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007968 spin_lock(&lo->plh_inode->i_lock);
7969 if (lrp->res.lrs_present)
7970 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Tom Haynesd67ae822014-12-11 17:02:04 -05007971 pnfs_clear_layoutreturn_waitbit(lo);
Peng Tao193e3aa2014-11-17 09:30:41 +08007972 clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE, &lo->plh_flags);
7973 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
Trond Myklebust849b2862012-09-24 14:18:39 -04007974 lo->plh_block_lgets--;
7975 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007976 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05007977 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03007978 kfree(calldata);
7979 dprintk("<-- %s\n", __func__);
7980}
7981
7982static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7983 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7984 .rpc_call_done = nfs4_layoutreturn_done,
7985 .rpc_release = nfs4_layoutreturn_release,
7986};
7987
Peng Tao6c166052014-11-17 09:30:40 +08007988int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03007989{
7990 struct rpc_task *task;
7991 struct rpc_message msg = {
7992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7993 .rpc_argp = &lrp->args,
7994 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007995 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007996 };
7997 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007998 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007999 .rpc_message = &msg,
8000 .callback_ops = &nfs4_layoutreturn_call_ops,
8001 .callback_data = lrp,
8002 };
Peng Tao6c166052014-11-17 09:30:40 +08008003 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008004
8005 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008006 if (!sync) {
8007 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8008 if (!lrp->inode) {
8009 nfs4_layoutreturn_release(lrp);
8010 return -EAGAIN;
8011 }
8012 task_setup_data.flags |= RPC_TASK_ASYNC;
8013 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008014 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008015 task = rpc_run_task(&task_setup_data);
8016 if (IS_ERR(task))
8017 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008018 if (sync)
8019 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008020 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008021 dprintk("<-- %s status=%d\n", __func__, status);
8022 rpc_put_task(task);
8023 return status;
8024}
8025
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008026static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008027_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8028 struct pnfs_device *pdev,
8029 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008030{
8031 struct nfs4_getdeviceinfo_args args = {
8032 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008033 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8034 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008035 };
8036 struct nfs4_getdeviceinfo_res res = {
8037 .pdev = pdev,
8038 };
8039 struct rpc_message msg = {
8040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8041 .rpc_argp = &args,
8042 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008043 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008044 };
8045 int status;
8046
8047 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008048 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008049 if (res.notification & ~args.notify_types)
8050 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008051 if (res.notification != args.notify_types)
8052 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008053
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008054 dprintk("<-- %s status=%d\n", __func__, status);
8055
8056 return status;
8057}
8058
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008059int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8060 struct pnfs_device *pdev,
8061 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008062{
8063 struct nfs4_exception exception = { };
8064 int err;
8065
8066 do {
8067 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008068 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008069 &exception);
8070 } while (exception.retry);
8071 return err;
8072}
8073EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8074
Andy Adamson863a3c62011-03-23 13:27:54 +00008075static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8076{
8077 struct nfs4_layoutcommit_data *data = calldata;
8078 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008079 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008080
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008081 nfs41_setup_sequence(session,
8082 &data->args.seq_args,
8083 &data->res.seq_res,
8084 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008085}
8086
8087static void
8088nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8089{
8090 struct nfs4_layoutcommit_data *data = calldata;
8091 struct nfs_server *server = NFS_SERVER(data->args.inode);
8092
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008093 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008094 return;
8095
8096 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008097 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8098 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8099 case -NFS4ERR_BADLAYOUT: /* no layout */
8100 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008101 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008102 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008103 break;
8104 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008105 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008106 rpc_restart_call_prepare(task);
8107 return;
8108 }
8109 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008110}
8111
8112static void nfs4_layoutcommit_release(void *calldata)
8113{
8114 struct nfs4_layoutcommit_data *data = calldata;
8115
Andy Adamsondb29c082011-07-30 20:52:38 -04008116 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008117 nfs_post_op_update_inode_force_wcc(data->args.inode,
8118 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008119 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008120 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008121 kfree(data);
8122}
8123
8124static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8125 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8126 .rpc_call_done = nfs4_layoutcommit_done,
8127 .rpc_release = nfs4_layoutcommit_release,
8128};
8129
8130int
Andy Adamsonef311532011-03-12 02:58:10 -05008131nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008132{
8133 struct rpc_message msg = {
8134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8135 .rpc_argp = &data->args,
8136 .rpc_resp = &data->res,
8137 .rpc_cred = data->cred,
8138 };
8139 struct rpc_task_setup task_setup_data = {
8140 .task = &data->task,
8141 .rpc_client = NFS_CLIENT(data->args.inode),
8142 .rpc_message = &msg,
8143 .callback_ops = &nfs4_layoutcommit_ops,
8144 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008145 };
8146 struct rpc_task *task;
8147 int status = 0;
8148
8149 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
8150 "lbw: %llu inode %lu\n",
8151 data->task.tk_pid, sync,
8152 data->args.lastbytewritten,
8153 data->args.inode->i_ino);
8154
Trond Myklebust472e2592015-02-05 16:50:30 -05008155 if (!sync) {
8156 data->inode = nfs_igrab_and_active(data->args.inode);
8157 if (data->inode == NULL) {
8158 nfs4_layoutcommit_release(data);
8159 return -EAGAIN;
8160 }
8161 task_setup_data.flags = RPC_TASK_ASYNC;
8162 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008163 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008164 task = rpc_run_task(&task_setup_data);
8165 if (IS_ERR(task))
8166 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008167 if (sync)
8168 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008169 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008170 dprintk("%s: status %d\n", __func__, status);
8171 rpc_put_task(task);
8172 return status;
8173}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008174
Andy Adamson97431202013-08-08 10:57:56 -04008175/**
8176 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8177 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8178 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008179static int
8180_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008181 struct nfs_fsinfo *info,
8182 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008183{
8184 struct nfs41_secinfo_no_name_args args = {
8185 .style = SECINFO_STYLE_CURRENT_FH,
8186 };
8187 struct nfs4_secinfo_res res = {
8188 .flavors = flavors,
8189 };
8190 struct rpc_message msg = {
8191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8192 .rpc_argp = &args,
8193 .rpc_resp = &res,
8194 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008195 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008196 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008197 int status;
8198
8199 if (use_integrity) {
8200 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008201 cred = nfs4_get_clid_cred(server->nfs_client);
8202 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008203 }
8204
8205 dprintk("--> %s\n", __func__);
8206 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8207 &res.seq_res, 0);
8208 dprintk("<-- %s status=%d\n", __func__, status);
8209
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008210 if (cred)
8211 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008212
8213 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008214}
8215
8216static int
8217nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8218 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8219{
8220 struct nfs4_exception exception = { };
8221 int err;
8222 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008223 /* first try using integrity protection */
8224 err = -NFS4ERR_WRONGSEC;
8225
8226 /* try to use integrity protection with machine cred */
8227 if (_nfs4_is_integrity_protected(server->nfs_client))
8228 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8229 flavors, true);
8230
8231 /*
8232 * if unable to use integrity protection, or SECINFO with
8233 * integrity protection returns NFS4ERR_WRONGSEC (which is
8234 * disallowed by spec, but exists in deployed servers) use
8235 * the current filesystem's rpc_client and the user cred.
8236 */
8237 if (err == -NFS4ERR_WRONGSEC)
8238 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8239 flavors, false);
8240
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008241 switch (err) {
8242 case 0:
8243 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008244 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008245 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008246 default:
8247 err = nfs4_handle_exception(server, err, &exception);
8248 }
8249 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008250out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008251 return err;
8252}
8253
8254static int
8255nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8256 struct nfs_fsinfo *info)
8257{
8258 int err;
8259 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008260 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008261 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008262 struct nfs4_secinfo4 *secinfo;
8263 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008264
8265 page = alloc_page(GFP_KERNEL);
8266 if (!page) {
8267 err = -ENOMEM;
8268 goto out;
8269 }
8270
8271 flavors = page_address(page);
8272 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8273
8274 /*
8275 * Fall back on "guess and check" method if
8276 * the server doesn't support SECINFO_NO_NAME
8277 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008278 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008279 err = nfs4_find_root_sec(server, fhandle, info);
8280 goto out_freepage;
8281 }
8282 if (err)
8283 goto out_freepage;
8284
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008285 for (i = 0; i < flavors->num_flavors; i++) {
8286 secinfo = &flavors->flavors[i];
8287
8288 switch (secinfo->flavor) {
8289 case RPC_AUTH_NULL:
8290 case RPC_AUTH_UNIX:
8291 case RPC_AUTH_GSS:
8292 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8293 &secinfo->flavor_info);
8294 break;
8295 default:
8296 flavor = RPC_AUTH_MAXFLAVOR;
8297 break;
8298 }
8299
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008300 if (!nfs_auth_info_match(&server->auth_info, flavor))
8301 flavor = RPC_AUTH_MAXFLAVOR;
8302
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008303 if (flavor != RPC_AUTH_MAXFLAVOR) {
8304 err = nfs4_lookup_root_sec(server, fhandle,
8305 info, flavor);
8306 if (!err)
8307 break;
8308 }
8309 }
8310
8311 if (flavor == RPC_AUTH_MAXFLAVOR)
8312 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008313
8314out_freepage:
8315 put_page(page);
8316 if (err == -EACCES)
8317 return -EPERM;
8318out:
8319 return err;
8320}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008321
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008322static int _nfs41_test_stateid(struct nfs_server *server,
8323 nfs4_stateid *stateid,
8324 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008325{
8326 int status;
8327 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008328 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008329 };
8330 struct nfs41_test_stateid_res res;
8331 struct rpc_message msg = {
8332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8333 .rpc_argp = &args,
8334 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008335 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008336 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008337 struct rpc_clnt *rpc_client = server->client;
8338
8339 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8340 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008341
Chuck Lever38527b12012-07-11 16:30:23 -04008342 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008343 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008344 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008345 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008346 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008347 if (status != NFS_OK) {
8348 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008349 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008350 }
8351 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008352 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008353}
8354
Chuck Lever38527b12012-07-11 16:30:23 -04008355/**
8356 * nfs41_test_stateid - perform a TEST_STATEID operation
8357 *
8358 * @server: server / transport on which to perform the operation
8359 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008360 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008361 *
8362 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8363 * Otherwise a negative NFS4ERR value is returned if the operation
8364 * failed or the state ID is not currently valid.
8365 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008366static int nfs41_test_stateid(struct nfs_server *server,
8367 nfs4_stateid *stateid,
8368 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008369{
8370 struct nfs4_exception exception = { };
8371 int err;
8372 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008373 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008374 if (err != -NFS4ERR_DELAY)
8375 break;
8376 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008377 } while (exception.retry);
8378 return err;
8379}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008380
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008381struct nfs_free_stateid_data {
8382 struct nfs_server *server;
8383 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008384 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008385};
8386
8387static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8388{
8389 struct nfs_free_stateid_data *data = calldata;
8390 nfs41_setup_sequence(nfs4_get_session(data->server),
8391 &data->args.seq_args,
8392 &data->res.seq_res,
8393 task);
8394}
8395
8396static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8397{
8398 struct nfs_free_stateid_data *data = calldata;
8399
8400 nfs41_sequence_done(task, &data->res.seq_res);
8401
8402 switch (task->tk_status) {
8403 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008404 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008405 rpc_restart_call_prepare(task);
8406 }
8407}
8408
8409static void nfs41_free_stateid_release(void *calldata)
8410{
8411 kfree(calldata);
8412}
8413
Trond Myklebust17f26b12013-08-21 15:48:42 -04008414static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008415 .rpc_call_prepare = nfs41_free_stateid_prepare,
8416 .rpc_call_done = nfs41_free_stateid_done,
8417 .rpc_release = nfs41_free_stateid_release,
8418};
8419
8420static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8421 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008422 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008423 bool privileged)
8424{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008425 struct rpc_message msg = {
8426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008427 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008428 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008429 struct rpc_task_setup task_setup = {
8430 .rpc_client = server->client,
8431 .rpc_message = &msg,
8432 .callback_ops = &nfs41_free_stateid_ops,
8433 .flags = RPC_TASK_ASYNC,
8434 };
8435 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008436
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008437 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8438 &task_setup.rpc_client, &msg);
8439
Chuck Lever38527b12012-07-11 16:30:23 -04008440 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008441 data = kmalloc(sizeof(*data), GFP_NOFS);
8442 if (!data)
8443 return ERR_PTR(-ENOMEM);
8444 data->server = server;
8445 nfs4_stateid_copy(&data->args.stateid, stateid);
8446
8447 task_setup.callback_data = data;
8448
8449 msg.rpc_argp = &data->args;
8450 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008451 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008452 if (privileged)
8453 nfs4_set_sequence_privileged(&data->args.seq_args);
8454
8455 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008456}
8457
Chuck Lever38527b12012-07-11 16:30:23 -04008458/**
8459 * nfs41_free_stateid - perform a FREE_STATEID operation
8460 *
8461 * @server: server / transport on which to perform the operation
8462 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008463 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008464 *
8465 * Returns NFS_OK if the server freed "stateid". Otherwise a
8466 * negative NFS4ERR value is returned.
8467 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008468static int nfs41_free_stateid(struct nfs_server *server,
8469 nfs4_stateid *stateid,
8470 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008471{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008472 struct rpc_task *task;
8473 int ret;
8474
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008475 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008476 if (IS_ERR(task))
8477 return PTR_ERR(task);
8478 ret = rpc_wait_for_completion_task(task);
8479 if (!ret)
8480 ret = task->tk_status;
8481 rpc_put_task(task);
8482 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008483}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008484
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008485static void
8486nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008487{
8488 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008489 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008490
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008491 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008492 nfs4_free_lock_state(server, lsp);
8493 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008494 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008495 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008496}
8497
Trond Myklebust36281ca2012-03-04 18:13:56 -05008498static bool nfs41_match_stateid(const nfs4_stateid *s1,
8499 const nfs4_stateid *s2)
8500{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008501 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008502 return false;
8503
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008504 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008505 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008506 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008507 return true;
8508
8509 return false;
8510}
8511
Andy Adamson557134a2009-04-01 09:21:53 -04008512#endif /* CONFIG_NFS_V4_1 */
8513
Trond Myklebust36281ca2012-03-04 18:13:56 -05008514static bool nfs4_match_stateid(const nfs4_stateid *s1,
8515 const nfs4_stateid *s2)
8516{
Trond Myklebustf597c532012-03-04 18:13:56 -05008517 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008518}
8519
8520
Trond Myklebust17280172012-03-11 13:11:00 -04008521static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008522 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008523 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524 .recover_open = nfs4_open_reclaim,
8525 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008526 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008527 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528};
8529
Andy Adamson591d71c2009-04-01 09:22:47 -04008530#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008531static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008532 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8533 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8534 .recover_open = nfs4_open_reclaim,
8535 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008536 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008537 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008538 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008539};
8540#endif /* CONFIG_NFS_V4_1 */
8541
Trond Myklebust17280172012-03-11 13:11:00 -04008542static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008543 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008544 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008545 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008547 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548};
8549
Andy Adamson591d71c2009-04-01 09:22:47 -04008550#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008551static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008552 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8553 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008554 .recover_open = nfs41_open_expired,
8555 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008556 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008557};
8558#endif /* CONFIG_NFS_V4_1 */
8559
Trond Myklebust17280172012-03-11 13:11:00 -04008560static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008561 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008562 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008563 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008564};
8565
8566#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008567static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008568 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008569 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008570 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008571};
8572#endif
8573
Chuck Leverec011fe2013-10-17 14:12:39 -04008574static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008575 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008576 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008577};
8578
8579#if defined(CONFIG_NFS_V4_1)
8580static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008581 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008582 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008583};
8584#endif /* CONFIG_NFS_V4_1 */
8585
Trond Myklebust97dc1352010-06-16 09:52:26 -04008586static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8587 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008588 .init_caps = NFS_CAP_READDIRPLUS
8589 | NFS_CAP_ATOMIC_OPEN
8590 | NFS_CAP_CHANGE_ATTR
8591 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008592 .init_client = nfs40_init_client,
8593 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008594 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008595 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008596 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008597 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008598 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008599 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8600 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8601 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008602 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008603};
8604
8605#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008606static struct nfs_seqid *
8607nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8608{
8609 return NULL;
8610}
8611
Trond Myklebust97dc1352010-06-16 09:52:26 -04008612static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8613 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008614 .init_caps = NFS_CAP_READDIRPLUS
8615 | NFS_CAP_ATOMIC_OPEN
8616 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008617 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008618 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008619 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008620 .init_client = nfs41_init_client,
8621 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008622 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008623 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008624 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008625 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008626 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008627 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8628 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8629 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008630 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008631};
8632#endif
8633
Steve Dickson42c2c422013-05-22 12:50:38 -04008634#if defined(CONFIG_NFS_V4_2)
8635static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8636 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008637 .init_caps = NFS_CAP_READDIRPLUS
8638 | NFS_CAP_ATOMIC_OPEN
8639 | NFS_CAP_CHANGE_ATTR
8640 | NFS_CAP_POSIX_LOCK
8641 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008642 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008643 | NFS_CAP_ALLOCATE
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008644 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008645 | NFS_CAP_SEEK
8646 | NFS_CAP_LAYOUTSTATS,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008647 .init_client = nfs41_init_client,
8648 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008649 .match_stateid = nfs41_match_stateid,
8650 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008651 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008652 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008653 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008654 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8655 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8656 .state_renewal_ops = &nfs41_state_renewal_ops,
8657};
8658#endif
8659
Trond Myklebust97dc1352010-06-16 09:52:26 -04008660const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8661 [0] = &nfs_v4_0_minor_ops,
8662#if defined(CONFIG_NFS_V4_1)
8663 [1] = &nfs_v4_1_minor_ops,
8664#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008665#if defined(CONFIG_NFS_V4_2)
8666 [2] = &nfs_v4_2_minor_ops,
8667#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008668};
8669
Trond Myklebust17f26b12013-08-21 15:48:42 -04008670static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008671 .create = nfs_create,
8672 .lookup = nfs_lookup,
8673 .atomic_open = nfs_atomic_open,
8674 .link = nfs_link,
8675 .unlink = nfs_unlink,
8676 .symlink = nfs_symlink,
8677 .mkdir = nfs_mkdir,
8678 .rmdir = nfs_rmdir,
8679 .mknod = nfs_mknod,
8680 .rename = nfs_rename,
8681 .permission = nfs_permission,
8682 .getattr = nfs_getattr,
8683 .setattr = nfs_setattr,
8684 .getxattr = generic_getxattr,
8685 .setxattr = generic_setxattr,
8686 .listxattr = generic_listxattr,
8687 .removexattr = generic_removexattr,
8688};
8689
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008690static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008691 .permission = nfs_permission,
8692 .getattr = nfs_getattr,
8693 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008694 .getxattr = generic_getxattr,
8695 .setxattr = generic_setxattr,
8696 .listxattr = generic_listxattr,
8697 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008698};
8699
David Howells509de812006-08-22 20:06:11 -04008700const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008701 .version = 4, /* protocol version */
8702 .dentry_ops = &nfs4_dentry_operations,
8703 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008704 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008705 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008707 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008708 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709 .getattr = nfs4_proc_getattr,
8710 .setattr = nfs4_proc_setattr,
8711 .lookup = nfs4_proc_lookup,
8712 .access = nfs4_proc_access,
8713 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008714 .create = nfs4_proc_create,
8715 .remove = nfs4_proc_remove,
8716 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008717 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008719 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008720 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008721 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008722 .link = nfs4_proc_link,
8723 .symlink = nfs4_proc_symlink,
8724 .mkdir = nfs4_proc_mkdir,
8725 .rmdir = nfs4_proc_remove,
8726 .readdir = nfs4_proc_readdir,
8727 .mknod = nfs4_proc_mknod,
8728 .statfs = nfs4_proc_statfs,
8729 .fsinfo = nfs4_proc_fsinfo,
8730 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008731 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008733 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008735 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008737 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008739 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008740 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008742 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008743 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008744 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008745 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008746 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008747 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008748 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008749 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008750 .create_server = nfs4_create_server,
8751 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752};
8753
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008754static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8755 .prefix = XATTR_NAME_NFSV4_ACL,
8756 .list = nfs4_xattr_list_nfs4_acl,
8757 .get = nfs4_xattr_get_nfs4_acl,
8758 .set = nfs4_xattr_set_nfs4_acl,
8759};
8760
8761const struct xattr_handler *nfs4_xattr_handlers[] = {
8762 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008763#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8764 &nfs4_xattr_nfs4_label_handler,
8765#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008766 NULL
8767};
8768
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769/*
8770 * Local variables:
8771 * c-basic-offset: 8
8772 * End:
8773 */