blob: 671498ca36d761a3c2f2de7e22fad98d0ca2c1b7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
NeilBrown8478eaa2014-09-18 16:09:27 +100081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *, long *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040083static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
84static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040085static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040087 struct nfs4_state *state, struct nfs4_label *ilabel,
88 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040090static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 struct rpc_cred *);
92static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
93 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040094#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040095
96#ifdef CONFIG_NFS_V4_SECURITY_LABEL
97static inline struct nfs4_label *
98nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
99 struct iattr *sattr, struct nfs4_label *label)
100{
101 int err;
102
103 if (label == NULL)
104 return NULL;
105
106 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
107 return NULL;
108
David Quigleyaa9c2662013-05-22 12:50:44 -0400109 err = security_dentry_init_security(dentry, sattr->ia_mode,
110 &dentry->d_name, (void **)&label->label, &label->len);
111 if (err == 0)
112 return label;
113
114 return NULL;
115}
116static inline void
117nfs4_label_release_security(struct nfs4_label *label)
118{
119 if (label)
120 security_release_secctx(label->label, label->len);
121}
122static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
123{
124 if (label)
125 return server->attr_bitmask;
126
127 return server->attr_bitmask_nl;
128}
129#else
130static inline struct nfs4_label *
131nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
132 struct iattr *sattr, struct nfs4_label *l)
133{ return NULL; }
134static inline void
135nfs4_label_release_security(struct nfs4_label *label)
136{ return; }
137static inline u32 *
138nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139{ return server->attr_bitmask; }
140#endif
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500143static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Trond Myklebust52567b02009-10-23 14:46:42 -0400145 if (err >= -1000)
146 return err;
147 switch (err) {
148 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500149 case -NFS4ERR_LAYOUTTRYLATER:
150 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400151 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000152 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400153 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800155 case -NFS4ERR_BADOWNER:
156 case -NFS4ERR_BADNAME:
157 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400158 case -NFS4ERR_SHARE_DENIED:
159 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400160 case -NFS4ERR_MINOR_VERS_MISMATCH:
161 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400162 case -NFS4ERR_FILE_OPEN:
163 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400164 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700166 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400169 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * This is our standard bitmap for GETATTR requests.
174 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400175const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 FATTR4_WORD0_TYPE
177 | FATTR4_WORD0_CHANGE
178 | FATTR4_WORD0_SIZE
179 | FATTR4_WORD0_FSID
180 | FATTR4_WORD0_FILEID,
181 FATTR4_WORD1_MODE
182 | FATTR4_WORD1_NUMLINKS
183 | FATTR4_WORD1_OWNER
184 | FATTR4_WORD1_OWNER_GROUP
185 | FATTR4_WORD1_RAWDEV
186 | FATTR4_WORD1_SPACE_USED
187 | FATTR4_WORD1_TIME_ACCESS
188 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400189 | FATTR4_WORD1_TIME_MODIFY
190 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400191#ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Trond Myklebust1549210f2012-06-05 09:16:47 -0400196static const u32 nfs4_pnfs_open_bitmap[3] = {
197 FATTR4_WORD0_TYPE
198 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_SIZE
200 | FATTR4_WORD0_FSID
201 | FATTR4_WORD0_FILEID,
202 FATTR4_WORD1_MODE
203 | FATTR4_WORD1_NUMLINKS
204 | FATTR4_WORD1_OWNER
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
212};
213
Andy Adamsone23008e2012-10-02 21:07:32 -0400214static const u32 nfs4_open_noattr_bitmap[3] = {
215 FATTR4_WORD0_TYPE
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
218};
219
David Quigleya09df2c2013-05-22 12:50:41 -0400220const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
227};
228
David Quigleya09df2c2013-05-22 12:50:41 -0400229const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 FATTR4_WORD0_MAXLINK
231 | FATTR4_WORD0_MAXNAME,
232 0
233};
234
Fred Isamandae100c2011-07-30 20:52:37 -0400235const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700239 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400245 FATTR4_WORD0_TYPE
246 | FATTR4_WORD0_CHANGE
247 | FATTR4_WORD0_SIZE
248 | FATTR4_WORD0_FSID
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
251 FATTR4_WORD1_MODE
252 | FATTR4_WORD1_NUMLINKS
253 | FATTR4_WORD1_OWNER
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400261};
262
Al Virobc4785c2006-10-19 23:28:51 -0700263static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct nfs4_readdir_arg *readdir)
265{
Al Viro0dbb4c62006-10-19 23:28:49 -0700266 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000269 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
271 return;
272 }
273
274 readdir->cookie = 0;
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
276 if (cookie == 2)
277 return;
278
279 /*
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
284 * instead of 1 or 2.
285 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800286 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (cookie == 0) {
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
294 p++;
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000298 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800314 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
NeilBrown8478eaa2014-09-18 16:09:27 +1000317static long nfs4_update_delay(long *timeout)
318{
319 long ret;
320 if (!timeout)
321 return NFS4_POLL_RETRY_MAX;
322 if (*timeout <= 0)
323 *timeout = NFS4_POLL_RETRY_MIN;
324 if (*timeout > NFS4_POLL_RETRY_MAX)
325 *timeout = NFS4_POLL_RETRY_MAX;
326 ret = *timeout;
327 *timeout <<= 1;
328 return ret;
329}
330
Trond Myklebust65de8722008-12-23 15:21:44 -0500331static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
332{
333 int res = 0;
334
335 might_sleep();
336
NeilBrown8478eaa2014-09-18 16:09:27 +1000337 freezable_schedule_timeout_killable_unsafe(
338 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 if (fatal_signal_pending(current))
340 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 return res;
342}
343
344/* This is the error handling routine for processes that are allowed
345 * to sleep.
346 */
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500347int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500348{
349 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500350 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500352 int ret = errorcode;
353
354 exception->retry = 0;
355 switch(errorcode) {
356 case 0:
357 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500358 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400359 case -NFS4ERR_DELEG_REVOKED:
360 case -NFS4ERR_ADMIN_REVOKED:
361 case -NFS4ERR_BAD_STATEID:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400362 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400363 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500364 exception->retry = 1;
365 return 0;
366 }
367 if (state == NULL)
368 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400369 ret = nfs4_schedule_stateid_recovery(server, state);
370 if (ret < 0)
371 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500372 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400373 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400374 if (state != NULL) {
375 ret = nfs4_schedule_stateid_recovery(server, state);
376 if (ret < 0)
377 break;
378 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500379 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500380 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500381 nfs4_schedule_lease_recovery(clp);
382 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400383 case -NFS4ERR_MOVED:
384 ret = nfs4_schedule_migration_recovery(server);
385 if (ret < 0)
386 break;
387 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400388 case -NFS4ERR_LEASE_MOVED:
389 nfs4_schedule_lease_moved_recovery(clp);
390 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500391#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400392 case -NFS4ERR_BADSESSION:
393 case -NFS4ERR_BADSLOT:
394 case -NFS4ERR_BAD_HIGH_SLOT:
395 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
396 case -NFS4ERR_DEADSESSION:
397 case -NFS4ERR_SEQ_FALSE_RETRY:
398 case -NFS4ERR_SEQ_MISORDERED:
399 dprintk("%s ERROR: %d Reset session\n", __func__,
400 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400401 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400402 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500403#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500405 if (exception->timeout > HZ) {
406 /* We have retried a decent amount, time to
407 * fail
408 */
409 ret = -EBUSY;
410 break;
411 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500412 case -NFS4ERR_GRACE:
413 case -NFS4ERR_DELAY:
414 ret = nfs4_delay(server->client, &exception->timeout);
415 if (ret != 0)
416 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400417 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500418 case -NFS4ERR_OLD_STATEID:
419 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800420 break;
421 case -NFS4ERR_BADOWNER:
422 /* The following works around a Linux server bug! */
423 case -NFS4ERR_BADNAME:
424 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
425 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
426 exception->retry = 1;
427 printk(KERN_WARNING "NFS: v4 server %s "
428 "does not accept raw "
429 "uid/gids. "
430 "Reenabling the idmapper.\n",
431 server->nfs_client->cl_hostname);
432 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500433 }
434 /* We failed to handle the error */
435 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500436wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500437 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400438 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
439 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500440 if (ret == 0)
441 exception->retry = 1;
442 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500443}
444
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400445/*
446 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
447 * or 'false' otherwise.
448 */
449static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
450{
451 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
452
453 if (flavor == RPC_AUTH_GSS_KRB5I ||
454 flavor == RPC_AUTH_GSS_KRB5P)
455 return true;
456
457 return false;
458}
Trond Myklebust65de8722008-12-23 15:21:44 -0500459
Trond Myklebust452e9352010-07-31 14:29:06 -0400460static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 spin_lock(&clp->cl_lock);
463 if (time_before(clp->cl_last_renewal,timestamp))
464 clp->cl_last_renewal = timestamp;
465 spin_unlock(&clp->cl_lock);
466}
467
Trond Myklebust452e9352010-07-31 14:29:06 -0400468static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
469{
Trond Myklebustbe824162015-07-05 14:50:46 -0400470 struct nfs_client *clp = server->nfs_client;
471
472 if (!nfs4_has_session(clp))
473 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400474}
475
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400476struct nfs4_call_sync_data {
477 const struct nfs_server *seq_server;
478 struct nfs4_sequence_args *seq_args;
479 struct nfs4_sequence_res *seq_res;
480};
481
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800482void nfs4_init_sequence(struct nfs4_sequence_args *args,
483 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400484{
485 args->sa_slot = NULL;
486 args->sa_cache_this = cache_reply;
487 args->sa_privileged = 0;
488
489 res->sr_slot = NULL;
490}
491
492static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
493{
494 args->sa_privileged = 1;
495}
496
Peng Taocb04ad22014-06-11 05:24:15 +0800497int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
498 struct nfs4_sequence_args *args,
499 struct nfs4_sequence_res *res,
500 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400501{
Chuck Lever3bd23842013-08-09 12:49:19 -0400502 struct nfs4_slot *slot;
503
504 /* slot already allocated? */
505 if (res->sr_slot != NULL)
506 goto out_start;
507
508 spin_lock(&tbl->slot_tbl_lock);
509 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
510 goto out_sleep;
511
512 slot = nfs4_alloc_slot(tbl);
513 if (IS_ERR(slot)) {
514 if (slot == ERR_PTR(-ENOMEM))
515 task->tk_timeout = HZ >> 2;
516 goto out_sleep;
517 }
518 spin_unlock(&tbl->slot_tbl_lock);
519
520 args->sa_slot = slot;
521 res->sr_slot = slot;
522
523out_start:
524 rpc_call_start(task);
525 return 0;
526
527out_sleep:
528 if (args->sa_privileged)
529 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
530 NULL, RPC_PRIORITY_PRIVILEGED);
531 else
532 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
533 spin_unlock(&tbl->slot_tbl_lock);
534 return -EAGAIN;
535}
Peng Taocb04ad22014-06-11 05:24:15 +0800536EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400537
538static int nfs40_sequence_done(struct rpc_task *task,
539 struct nfs4_sequence_res *res)
540{
541 struct nfs4_slot *slot = res->sr_slot;
542 struct nfs4_slot_table *tbl;
543
Trond Myklebustcab92c12014-01-29 12:12:15 -0500544 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400545 goto out;
546
547 tbl = slot->table;
548 spin_lock(&tbl->slot_tbl_lock);
549 if (!nfs41_wake_and_assign_slot(tbl, slot))
550 nfs4_free_slot(tbl, slot);
551 spin_unlock(&tbl->slot_tbl_lock);
552
553 res->sr_slot = NULL;
554out:
555 return 1;
556}
557
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400558#if defined(CONFIG_NFS_V4_1)
559
Trond Myklebustd185a332010-06-16 09:52:25 -0400560static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400561{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500562 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400563 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500564 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500565 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400566
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500567 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500568 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500569
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500570 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500571 /* Be nice to the server: try to ensure that the last transmitted
572 * value for highest_user_slotid <= target_highest_slotid
573 */
574 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
575 send_new_highest_used_slotid = true;
576
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500577 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500578 send_new_highest_used_slotid = false;
579 goto out_unlock;
580 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500581 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500582
583 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
584 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500585out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500586 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400587 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500588 if (send_new_highest_used_slotid)
589 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400590}
591
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500592int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400593{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500594 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500595 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400596 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500597 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500598 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400599
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500600 if (slot == NULL)
601 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400602 /* don't increment the sequence number if the task wasn't sent */
603 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400604 goto out;
605
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500606 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500607
Trond Myklebustac20d162012-12-15 15:36:07 -0500608 if (slot->interrupted) {
609 slot->interrupted = 0;
610 interrupted = true;
611 }
612
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400613 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500614 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400615 switch (res->sr_status) {
616 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400617 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500618 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500619 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500620 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500621 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400622 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500623 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400624 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500625 case 1:
626 /*
627 * sr_status remains 1 if an RPC level error occurred.
628 * The server may or may not have processed the sequence
629 * operation..
630 * Mark the slot as having hosted an interrupted RPC call.
631 */
632 slot->interrupted = 1;
633 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400634 case -NFS4ERR_DELAY:
635 /* The server detected a resend of the RPC call and
636 * returned NFS4ERR_DELAY as per Section 2.10.6.2
637 * of RFC5661.
638 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500639 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400640 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500641 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500642 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400643 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500644 case -NFS4ERR_BADSLOT:
645 /*
646 * The slot id we used was probably retired. Try again
647 * using a different slot id.
648 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500649 goto retry_nowait;
650 case -NFS4ERR_SEQ_MISORDERED:
651 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500652 * Was the last operation on this sequence interrupted?
653 * If so, retry after bumping the sequence number.
654 */
655 if (interrupted) {
656 ++slot->seq_nr;
657 goto retry_nowait;
658 }
659 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500660 * Could this slot have been previously retired?
661 * If so, then the server may be expecting seq_nr = 1!
662 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500663 if (slot->seq_nr != 1) {
664 slot->seq_nr = 1;
665 goto retry_nowait;
666 }
667 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500668 case -NFS4ERR_SEQ_FALSE_RETRY:
669 ++slot->seq_nr;
670 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400671 default:
672 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500673 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400674 }
675out:
676 /* The session may be reset by one of the error handlers. */
677 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400678 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500679out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500680 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500681retry_nowait:
682 if (rpc_restart_call_prepare(task)) {
683 task->tk_status = 0;
684 ret = 0;
685 }
686 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400687out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400688 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400689 goto out;
690 rpc_delay(task, NFS4_POLL_RETRY_MAX);
691 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400692}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500693EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400694
Peng Tao2c4b1312014-06-11 05:24:16 +0800695int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400696{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400698 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400699 if (!res->sr_slot->table->session)
700 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400701 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400702}
Peng Tao2c4b1312014-06-11 05:24:16 +0800703EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400704
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000705int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 struct nfs4_sequence_args *args,
707 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 struct rpc_task *task)
709{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400710 struct nfs4_slot *slot;
711 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400712
713 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400714 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400715 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400716 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400718 tbl = &session->fc_slot_table;
719
Trond Myklebust69d206b2012-11-22 13:21:02 -0500720 task->tk_timeout = 0;
721
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400722 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400723 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400724 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500725 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500726 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400727 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400728 }
729
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500730 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500731 if (IS_ERR(slot)) {
732 /* If out of memory, try again in 1/4 second */
733 if (slot == ERR_PTR(-ENOMEM))
734 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400735 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400736 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400737 }
738 spin_unlock(&tbl->slot_tbl_lock);
739
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500740 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400741
Chuck Levere8d92382013-08-09 12:47:51 -0400742 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500743 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400744
Benny Halevydfb4f3092010-09-24 09:17:01 -0400745 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500746 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400747 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400748 /*
749 * sr_status is only set in decode_sequence, and so will remain
750 * set to 1 if an rpc level failure occurs.
751 */
752 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400753 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400754out_success:
755 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400756 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400757out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400758 /* Privileged tasks are queued with top priority */
759 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400760 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
761 NULL, RPC_PRIORITY_PRIVILEGED);
762 else
763 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400764 spin_unlock(&tbl->slot_tbl_lock);
765 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400766}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000767EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400768
Chuck Lever5a580e02013-08-09 12:48:09 -0400769static int nfs4_setup_sequence(const struct nfs_server *server,
770 struct nfs4_sequence_args *args,
771 struct nfs4_sequence_res *res,
772 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400773{
Trond Myklebust035168a2010-06-16 09:52:26 -0400774 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400775 int ret = 0;
776
Chuck Lever3bd23842013-08-09 12:49:19 -0400777 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800778 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
779 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400780
Chuck Levere8d92382013-08-09 12:47:51 -0400781 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400782 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400783 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400784
Trond Myklebust9d12b212012-01-17 22:04:25 -0500785 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400786
Andy Adamsonce5039c2009-04-01 09:22:13 -0400787 dprintk("<-- %s status=%d\n", __func__, ret);
788 return ret;
789}
790
Andy Adamsonce5039c2009-04-01 09:22:13 -0400791static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
792{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400793 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400794 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400795
Trond Myklebust035168a2010-06-16 09:52:26 -0400796 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
797
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400798 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400799}
800
Andy Adamson69ab40c2009-04-01 09:22:19 -0400801static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
802{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400803 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804
Trond Myklebust14516c32010-07-31 14:29:06 -0400805 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400806}
807
Trond Myklebust17280172012-03-11 13:11:00 -0400808static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400810 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400811};
812
Chuck Lever3bd23842013-08-09 12:49:19 -0400813#else /* !CONFIG_NFS_V4_1 */
814
Chuck Lever5a580e02013-08-09 12:48:09 -0400815static int nfs4_setup_sequence(const struct nfs_server *server,
816 struct nfs4_sequence_args *args,
817 struct nfs4_sequence_res *res,
818 struct rpc_task *task)
819{
Peng Taocb04ad22014-06-11 05:24:15 +0800820 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
821 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400822}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400823
Peng Tao2c4b1312014-06-11 05:24:16 +0800824int nfs4_sequence_done(struct rpc_task *task,
825 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400826{
Chuck Lever3bd23842013-08-09 12:49:19 -0400827 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400828}
Peng Tao2c4b1312014-06-11 05:24:16 +0800829EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400830
831#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400832
Chuck Lever9915ea72013-08-09 12:48:27 -0400833static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
834{
835 struct nfs4_call_sync_data *data = calldata;
836 nfs4_setup_sequence(data->seq_server,
837 data->seq_args, data->seq_res, task);
838}
839
840static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
841{
842 struct nfs4_call_sync_data *data = calldata;
843 nfs4_sequence_done(task, data->seq_res);
844}
845
846static const struct rpc_call_ops nfs40_call_sync_ops = {
847 .rpc_call_prepare = nfs40_call_sync_prepare,
848 .rpc_call_done = nfs40_call_sync_done,
849};
850
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400851static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400852 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500853 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 struct nfs4_sequence_args *args,
855 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400856{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857 int ret;
858 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400859 struct nfs_client *clp = server->nfs_client;
860 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100861 .seq_server = server,
862 .seq_args = args,
863 .seq_res = res,
864 };
865 struct rpc_task_setup task_setup = {
866 .rpc_client = clnt,
867 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400868 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100869 .callback_data = &data
870 };
871
872 task = rpc_run_task(&task_setup);
873 if (IS_ERR(task))
874 ret = PTR_ERR(task);
875 else {
876 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400877 rpc_put_task(task);
878 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 return ret;
880}
881
Bryan Schumaker7c513052011-03-24 17:12:24 +0000882int nfs4_call_sync(struct rpc_clnt *clnt,
883 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000884 struct rpc_message *msg,
885 struct nfs4_sequence_args *args,
886 struct nfs4_sequence_res *res,
887 int cache_reply)
888{
Chuck Levera9c92d62013-08-09 12:48:18 -0400889 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400890 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000891}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
894{
895 struct nfs_inode *nfsi = NFS_I(dir);
896
897 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400898 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700899 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700901 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500902 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000903 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 spin_unlock(&dir->i_lock);
905}
906
907struct nfs4_opendata {
908 struct kref kref;
909 struct nfs_openargs o_arg;
910 struct nfs_openres o_res;
911 struct nfs_open_confirmargs c_arg;
912 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500913 struct nfs4_string owner_name;
914 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400916 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400918 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct nfs4_state_owner *owner;
920 struct nfs4_state *state;
921 struct iattr attrs;
922 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400923 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400924 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400925 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400926 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int cancelled;
928};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400929
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400930static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
931 int err, struct nfs4_exception *exception)
932{
933 if (err != -EINVAL)
934 return false;
935 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
936 return false;
937 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
938 exception->retry = 1;
939 return true;
940}
941
Trond Myklebust6ae37332015-01-30 14:21:14 -0500942static u32
943nfs4_map_atomic_open_share(struct nfs_server *server,
944 fmode_t fmode, int openflags)
945{
946 u32 res = 0;
947
948 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
949 case FMODE_READ:
950 res = NFS4_SHARE_ACCESS_READ;
951 break;
952 case FMODE_WRITE:
953 res = NFS4_SHARE_ACCESS_WRITE;
954 break;
955 case FMODE_READ|FMODE_WRITE:
956 res = NFS4_SHARE_ACCESS_BOTH;
957 }
958 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
959 goto out;
960 /* Want no delegation if we're using O_DIRECT */
961 if (openflags & O_DIRECT)
962 res |= NFS4_SHARE_WANT_NO_DELEG;
963out:
964 return res;
965}
966
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400967static enum open_claim_type4
968nfs4_map_atomic_open_claim(struct nfs_server *server,
969 enum open_claim_type4 claim)
970{
971 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
972 return claim;
973 switch (claim) {
974 default:
975 return claim;
976 case NFS4_OPEN_CLAIM_FH:
977 return NFS4_OPEN_CLAIM_NULL;
978 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
979 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
980 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
981 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
982 }
983}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986{
987 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400988 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100989 p->o_res.seqid = p->o_arg.seqid;
990 p->c_res.seqid = p->c_arg.seqid;
991 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400992 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100993 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500994 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100995}
996
Al Viro82a2c1b2011-06-22 18:30:55 -0400997static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100998 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400999 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001000 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001001 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001002 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003{
Al Viro82a2c1b2011-06-22 18:30:55 -04001004 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001005 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001007 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001008 struct nfs4_opendata *p;
1009
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001010 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001011 if (p == NULL)
1012 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001013
1014 p->f_label = nfs4_label_alloc(server, gfp_mask);
1015 if (IS_ERR(p->f_label))
1016 goto err_free_p;
1017
Trond Myklebust63f5f792015-01-23 19:19:25 -05001018 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1019 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001020 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001021 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001022 nfs_sb_active(dentry->d_sb);
1023 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001024 p->dir = parent;
1025 p->owner = sp;
1026 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 p->o_arg.open_flags = flags;
1028 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001029 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1030 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001031 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1032 * will return permission denied for all bits until close */
1033 if (!(flags & O_EXCL)) {
1034 /* ask server to check for all possible rights as results
1035 * are cached */
1036 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1037 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001038 }
David Howells7539bba2006-08-22 20:06:09 -04001039 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001040 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1041 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001042 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001044 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001045 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001046 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001047 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1048 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001049 case NFS4_OPEN_CLAIM_NULL:
1050 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1051 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1052 p->o_arg.fh = NFS_FH(dir);
1053 break;
1054 case NFS4_OPEN_CLAIM_PREVIOUS:
1055 case NFS4_OPEN_CLAIM_FH:
1056 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1057 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001058 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001059 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001060 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001061 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001062
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001063 p->o_arg.u.attrs = &p->attrs;
1064 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001065
1066 verf[0] = jiffies;
1067 verf[1] = current->pid;
1068 memcpy(p->o_arg.u.verifier.data, verf,
1069 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001070 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001071 p->c_arg.fh = &p->o_res.fh;
1072 p->c_arg.stateid = &p->o_res.stateid;
1073 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001074 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001075 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001076 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001077
1078err_free_label:
1079 nfs4_label_free(p->f_label);
1080err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001081 kfree(p);
1082err:
1083 dput(parent);
1084 return NULL;
1085}
1086
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001087static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001088{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001089 struct nfs4_opendata *p = container_of(kref,
1090 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001091 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001092
1093 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001094 if (p->state != NULL)
1095 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001096 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001097
1098 nfs4_label_free(p->f_label);
1099
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001100 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001101 dput(p->dentry);
1102 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001103 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001104 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001105 kfree(p);
1106}
1107
1108static void nfs4_opendata_put(struct nfs4_opendata *p)
1109{
1110 if (p != NULL)
1111 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112}
1113
Trond Myklebust06f814a2006-01-03 09:55:07 +01001114static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1115{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001116 int ret;
1117
Trond Myklebust06f814a2006-01-03 09:55:07 +01001118 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001119 return ret;
1120}
1121
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001122static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001123{
1124 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125
Trond Myklebust536e43d2012-01-17 22:04:26 -05001126 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127 goto out;
1128 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001129 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001130 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1131 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001132 break;
1133 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001134 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1135 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001136 break;
1137 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001138 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1139 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001140 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001142 return ret;
1143}
1144
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001145static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001146{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001147 if (delegation == NULL)
1148 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001149 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001150 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001151 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1152 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001153 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001154 return 1;
1155}
1156
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001158{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001159 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001160 case FMODE_WRITE:
1161 state->n_wronly++;
1162 break;
1163 case FMODE_READ:
1164 state->n_rdonly++;
1165 break;
1166 case FMODE_READ|FMODE_WRITE:
1167 state->n_rdwr++;
1168 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001169 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001170}
1171
Trond Myklebust4f14c192014-02-12 19:15:06 -05001172static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001173{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001174 struct nfs_client *clp = state->owner->so_server->nfs_client;
1175 bool need_recover = false;
1176
1177 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1178 need_recover = true;
1179 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1180 need_recover = true;
1181 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1182 need_recover = true;
1183 if (need_recover)
1184 nfs4_state_mark_reclaim_nograce(clp, state);
1185}
1186
Trond Myklebuste999e802014-02-10 18:20:47 -05001187static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1188 nfs4_stateid *stateid)
1189{
1190 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1191 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001192 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1193 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001194 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001195 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001196 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1197 return true;
1198 return false;
1199}
1200
Trond Myklebustf95549c2015-01-23 18:06:09 -05001201static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1202{
1203 if (state->n_wronly)
1204 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1205 if (state->n_rdonly)
1206 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1207 if (state->n_rdwr)
1208 set_bit(NFS_O_RDWR_STATE, &state->flags);
1209}
1210
Trond Myklebust226056c2014-02-11 10:41:07 -05001211static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1212 nfs4_stateid *stateid, fmode_t fmode)
1213{
1214 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1215 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1216 case FMODE_WRITE:
1217 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1218 break;
1219 case FMODE_READ:
1220 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1221 break;
1222 case 0:
1223 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1224 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1225 clear_bit(NFS_OPEN_STATE, &state->flags);
1226 }
1227 if (stateid == NULL)
1228 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001229 /* Handle races with OPEN */
1230 if (!nfs4_stateid_match_other(stateid, &state->open_stateid) ||
1231 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1232 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001233 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001234 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001235 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001236 nfs4_stateid_copy(&state->stateid, stateid);
1237 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001238}
1239
1240static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1241{
1242 write_seqlock(&state->seqlock);
1243 nfs_clear_open_stateid_locked(state, stateid, fmode);
1244 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001245 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1246 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001247}
1248
Trond Myklebust003707c2007-07-05 18:07:55 -04001249static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1250{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001251 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001252 case FMODE_READ:
1253 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1254 break;
1255 case FMODE_WRITE:
1256 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1257 break;
1258 case FMODE_READ|FMODE_WRITE:
1259 set_bit(NFS_O_RDWR_STATE, &state->flags);
1260 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001261 if (!nfs_need_update_open_stateid(state, stateid))
1262 return;
1263 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1264 nfs4_stateid_copy(&state->stateid, stateid);
1265 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001266}
1267
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001268static 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 -07001269{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001270 /*
1271 * Protect the call to nfs4_state_set_mode_locked and
1272 * serialise the stateid update
1273 */
1274 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001275 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001276 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001277 set_bit(NFS_DELEGATED_STATE, &state->flags);
1278 }
1279 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001280 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001281 write_sequnlock(&state->seqlock);
1282 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001283 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001284 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001287static 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 -05001288{
1289 struct nfs_inode *nfsi = NFS_I(state->inode);
1290 struct nfs_delegation *deleg_cur;
1291 int ret = 0;
1292
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001293 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001294
1295 rcu_read_lock();
1296 deleg_cur = rcu_dereference(nfsi->delegation);
1297 if (deleg_cur == NULL)
1298 goto no_delegation;
1299
1300 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001301 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001302 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001303 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001304 goto no_delegation_unlock;
1305
1306 if (delegation == NULL)
1307 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001308 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001309 goto no_delegation_unlock;
1310
Trond Myklebustb7391f42008-12-23 15:21:52 -05001311 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001312 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001313 ret = 1;
1314no_delegation_unlock:
1315 spin_unlock(&deleg_cur->lock);
1316no_delegation:
1317 rcu_read_unlock();
1318
1319 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001320 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001321 ret = 1;
1322 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001323 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1324 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001325
1326 return ret;
1327}
1328
Trond Myklebust39071e62015-01-24 15:07:56 -05001329static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1330 const nfs4_stateid *stateid)
1331{
1332 struct nfs4_state *state = lsp->ls_state;
1333 bool ret = false;
1334
1335 spin_lock(&state->state_lock);
1336 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1337 goto out_noupdate;
1338 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1339 goto out_noupdate;
1340 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1341 ret = true;
1342out_noupdate:
1343 spin_unlock(&state->state_lock);
1344 return ret;
1345}
Trond Myklebust34310432008-12-23 15:21:38 -05001346
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001347static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001348{
1349 struct nfs_delegation *delegation;
1350
1351 rcu_read_lock();
1352 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001354 rcu_read_unlock();
1355 return;
1356 }
1357 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001358 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001359}
1360
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001362{
1363 struct nfs4_state *state = opendata->state;
1364 struct nfs_inode *nfsi = NFS_I(state->inode);
1365 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001366 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001367 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001368 nfs4_stateid stateid;
1369 int ret = -EAGAIN;
1370
Trond Myklebustaac00a82007-07-05 19:02:21 -04001371 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001372 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001373 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001374 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001375 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001376 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001377 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001378 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001379 rcu_read_lock();
1380 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001381 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001382 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001383 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001384 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001385 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001386 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001387 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001388 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001389 if (!opendata->is_recover) {
1390 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1391 if (ret != 0)
1392 goto out;
1393 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001394 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001395
1396 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001397 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001398 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001399 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001400out:
1401 return ERR_PTR(ret);
1402out_return_state:
1403 atomic_inc(&state->count);
1404 return state;
1405}
1406
Andy Adamsone23008e2012-10-02 21:07:32 -04001407static void
1408nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1409{
1410 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1411 struct nfs_delegation *delegation;
1412 int delegation_flags = 0;
1413
1414 rcu_read_lock();
1415 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1416 if (delegation)
1417 delegation_flags = delegation->flags;
1418 rcu_read_unlock();
1419 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1420 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1421 "returning a delegation for "
1422 "OPEN(CLAIM_DELEGATE_CUR)\n",
1423 clp->cl_hostname);
1424 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1425 nfs_inode_set_delegation(state->inode,
1426 data->owner->so_cred,
1427 &data->o_res);
1428 else
1429 nfs_inode_reclaim_delegation(state->inode,
1430 data->owner->so_cred,
1431 &data->o_res);
1432}
1433
1434/*
1435 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1436 * and update the nfs4_state.
1437 */
1438static struct nfs4_state *
1439_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1440{
1441 struct inode *inode = data->state->inode;
1442 struct nfs4_state *state = data->state;
1443 int ret;
1444
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001445 if (!data->rpc_done) {
1446 if (data->rpc_status) {
1447 ret = data->rpc_status;
1448 goto err;
1449 }
1450 /* cached opens have already been processed */
1451 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001452 }
1453
Andy Adamsone23008e2012-10-02 21:07:32 -04001454 ret = nfs_refresh_inode(inode, &data->f_attr);
1455 if (ret)
1456 goto err;
1457
1458 if (data->o_res.delegation_type != 0)
1459 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001460update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001461 update_open_stateid(state, &data->o_res.stateid, NULL,
1462 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001463 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001464
1465 return state;
1466err:
1467 return ERR_PTR(ret);
1468
1469}
1470
1471static struct nfs4_state *
1472_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001473{
1474 struct inode *inode;
1475 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001476 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477
Trond Myklebustaac00a82007-07-05 19:02:21 -04001478 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001479 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001480 goto out;
1481 }
1482
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001483 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001484 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001485 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001486 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001487 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001488 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001489 goto err;
1490 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491 state = nfs4_get_open_state(inode, data->owner);
1492 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001493 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001494 if (data->o_res.delegation_type != 0)
1495 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001496 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001497 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001499out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001500 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001502err_put_inode:
1503 iput(inode);
1504err:
1505 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506}
1507
Andy Adamsone23008e2012-10-02 21:07:32 -04001508static struct nfs4_state *
1509nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1510{
1511 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1512 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1513 return _nfs4_opendata_to_nfs4_state(data);
1514}
1515
Trond Myklebust864472e2006-01-03 09:55:15 +01001516static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1517{
1518 struct nfs_inode *nfsi = NFS_I(state->inode);
1519 struct nfs_open_context *ctx;
1520
1521 spin_lock(&state->inode->i_lock);
1522 list_for_each_entry(ctx, &nfsi->open_files, list) {
1523 if (ctx->state != state)
1524 continue;
1525 get_nfs_open_context(ctx);
1526 spin_unlock(&state->inode->i_lock);
1527 return ctx;
1528 }
1529 spin_unlock(&state->inode->i_lock);
1530 return ERR_PTR(-ENOENT);
1531}
1532
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001533static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1534 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001535{
1536 struct nfs4_opendata *opendata;
1537
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001538 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001539 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001540 if (opendata == NULL)
1541 return ERR_PTR(-ENOMEM);
1542 opendata->state = state;
1543 atomic_inc(&state->count);
1544 return opendata;
1545}
1546
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001547static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001548{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001549 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001550 int ret;
1551
NeilBrown39f897f2015-06-29 14:28:54 +10001552 if ((opendata->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
1553 opendata->o_arg.claim == NFS4_OPEN_CLAIM_DELEG_CUR_FH) &&
1554 (opendata->o_arg.u.delegation_type & fmode) != fmode)
1555 /* This mode can't have been delegated, so we must have
1556 * a valid open_stateid to cover it - not need to reclaim.
1557 */
1558 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001559 opendata->o_arg.open_flags = 0;
1560 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001561 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1562 NFS_SB(opendata->dentry->d_sb),
1563 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001564 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1565 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1566 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001567 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001568 if (ret != 0)
1569 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001570 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001571 if (IS_ERR(newstate))
1572 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001573 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001574 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001575 return 0;
1576}
1577
1578static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1579{
Trond Myklebust864472e2006-01-03 09:55:15 +01001580 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001581 int ret;
1582
Trond Myklebust4f14c192014-02-12 19:15:06 -05001583 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1584 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1585 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1586 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001587 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001588 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001589 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001590 smp_rmb();
1591 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001592 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001593 if (ret != 0)
1594 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001595 if (newstate != state)
1596 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001597 }
1598 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001599 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001600 if (ret != 0)
1601 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001602 if (newstate != state)
1603 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001604 }
1605 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001606 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001607 if (ret != 0)
1608 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001609 if (newstate != state)
1610 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001611 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001612 /*
1613 * We may have performed cached opens for all three recoveries.
1614 * Check if we need to update the current stateid.
1615 */
1616 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001617 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001618 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001619 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001620 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001621 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001622 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001623 return 0;
1624}
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626/*
1627 * OPEN_RECLAIM:
1628 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001630static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001632 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001633 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001634 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 int status;
1636
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001637 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1638 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001639 if (IS_ERR(opendata))
1640 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001641 rcu_read_lock();
1642 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001643 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001644 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001645 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001646 opendata->o_arg.u.delegation_type = delegation_type;
1647 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001648 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return status;
1650}
1651
Trond Myklebust539cd032007-06-05 11:46:42 -04001652static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 struct nfs_server *server = NFS_SERVER(state->inode);
1655 struct nfs4_exception exception = { };
1656 int err;
1657 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001658 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001659 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001660 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1661 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001662 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001663 break;
1664 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 } while (exception.retry);
1666 return err;
1667}
1668
Trond Myklebust864472e2006-01-03 09:55:15 +01001669static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1670{
1671 struct nfs_open_context *ctx;
1672 int ret;
1673
1674 ctx = nfs4_state_find_open_context(state);
1675 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001676 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001677 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001678 put_nfs_open_context(ctx);
1679 return ret;
1680}
1681
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001682static 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 -07001683{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001684 switch (err) {
1685 default:
1686 printk(KERN_ERR "NFS: %s: unhandled error "
1687 "%d.\n", __func__, err);
1688 case 0:
1689 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001690 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001691 case -ESTALE:
1692 break;
1693 case -NFS4ERR_BADSESSION:
1694 case -NFS4ERR_BADSLOT:
1695 case -NFS4ERR_BAD_HIGH_SLOT:
1696 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1697 case -NFS4ERR_DEADSESSION:
1698 set_bit(NFS_DELEGATED_STATE, &state->flags);
1699 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1700 return -EAGAIN;
1701 case -NFS4ERR_STALE_CLIENTID:
1702 case -NFS4ERR_STALE_STATEID:
1703 set_bit(NFS_DELEGATED_STATE, &state->flags);
1704 case -NFS4ERR_EXPIRED:
1705 /* Don't recall a delegation if it was lost */
1706 nfs4_schedule_lease_recovery(server->nfs_client);
1707 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001708 case -NFS4ERR_MOVED:
1709 nfs4_schedule_migration_recovery(server);
1710 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001711 case -NFS4ERR_LEASE_MOVED:
1712 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1713 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001714 case -NFS4ERR_DELEG_REVOKED:
1715 case -NFS4ERR_ADMIN_REVOKED:
1716 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001717 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001718 nfs_inode_find_state_and_recover(state->inode,
1719 stateid);
1720 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001721 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001722 case -NFS4ERR_DELAY:
1723 case -NFS4ERR_GRACE:
1724 set_bit(NFS_DELEGATED_STATE, &state->flags);
1725 ssleep(1);
1726 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001727 case -ENOMEM:
1728 case -NFS4ERR_DENIED:
1729 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1730 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return err;
1733}
1734
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001735int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1736{
1737 struct nfs_server *server = NFS_SERVER(state->inode);
1738 struct nfs4_opendata *opendata;
1739 int err;
1740
1741 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1742 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1743 if (IS_ERR(opendata))
1744 return PTR_ERR(opendata);
1745 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1746 err = nfs4_open_recover(opendata, state);
1747 nfs4_opendata_put(opendata);
1748 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1749}
1750
Chuck Leverbe05c862013-08-09 12:49:47 -04001751static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1752{
1753 struct nfs4_opendata *data = calldata;
1754
Peng Taocb04ad22014-06-11 05:24:15 +08001755 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1756 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001757}
1758
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001759static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1760{
1761 struct nfs4_opendata *data = calldata;
1762
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001763 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001764
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001765 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001766 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001767 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001768 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001769 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001770 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001771 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001772}
1773
1774static void nfs4_open_confirm_release(void *calldata)
1775{
1776 struct nfs4_opendata *data = calldata;
1777 struct nfs4_state *state = NULL;
1778
1779 /* If this request hasn't been cancelled, do nothing */
1780 if (data->cancelled == 0)
1781 goto out_free;
1782 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001783 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001784 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001785 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001786 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001787 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001788out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001789 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001790}
1791
1792static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001793 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001794 .rpc_call_done = nfs4_open_confirm_done,
1795 .rpc_release = nfs4_open_confirm_release,
1796};
1797
1798/*
1799 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1800 */
1801static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1802{
David Howells2b0143b2015-03-17 22:25:59 +00001803 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001804 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001805 struct rpc_message msg = {
1806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1807 .rpc_argp = &data->c_arg,
1808 .rpc_resp = &data->c_res,
1809 .rpc_cred = data->owner->so_cred,
1810 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001811 struct rpc_task_setup task_setup_data = {
1812 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001813 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001814 .callback_ops = &nfs4_open_confirm_ops,
1815 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001816 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001817 .flags = RPC_TASK_ASYNC,
1818 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 int status;
1820
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001821 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001822 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001823 data->rpc_done = 0;
1824 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001825 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001826 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001827 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001828 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001829 status = nfs4_wait_for_completion_rpc_task(task);
1830 if (status != 0) {
1831 data->cancelled = 1;
1832 smp_wmb();
1833 } else
1834 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001835 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return status;
1837}
1838
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001839static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001841 struct nfs4_opendata *data = calldata;
1842 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001843 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001844
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001846 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001847 /*
1848 * Check if we still need to send an OPEN call, or if we can use
1849 * a delegation instead.
1850 */
1851 if (data->state != NULL) {
1852 struct nfs_delegation *delegation;
1853
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001854 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001855 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856 rcu_read_lock();
1857 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001858 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001859 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001860 can_open_delegated(delegation, data->o_arg.fmode))
1861 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001862 rcu_read_unlock();
1863 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001864 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001865 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001866 switch (data->o_arg.claim) {
1867 case NFS4_OPEN_CLAIM_PREVIOUS:
1868 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1869 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001870 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001871 case NFS4_OPEN_CLAIM_FH:
1872 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001873 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1874 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001875 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001876 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001877 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001878 &data->o_res.seq_res,
1879 task) != 0)
1880 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001881
1882 /* Set the create mode (note dependency on the session type) */
1883 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1884 if (data->o_arg.open_flags & O_EXCL) {
1885 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1886 if (nfs4_has_persistent_session(clp))
1887 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1888 else if (clp->cl_mvops->minor_version > 0)
1889 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1890 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001891 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001892unlock_no_action:
1893 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001894out_no_action:
1895 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001896out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001897 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001898}
1899
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001900static void nfs4_open_done(struct rpc_task *task, void *calldata)
1901{
1902 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001904 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001905
Trond Myklebust14516c32010-07-31 14:29:06 -04001906 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1907 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001908
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001909 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001910 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1911 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001912 case S_IFREG:
1913 break;
1914 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001915 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001916 break;
1917 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001918 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001919 break;
1920 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001921 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001922 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001923 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001924 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001925 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1926 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001927 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001928 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001929}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001930
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001931static void nfs4_open_release(void *calldata)
1932{
1933 struct nfs4_opendata *data = calldata;
1934 struct nfs4_state *state = NULL;
1935
1936 /* If this request hasn't been cancelled, do nothing */
1937 if (data->cancelled == 0)
1938 goto out_free;
1939 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001940 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001941 goto out_free;
1942 /* In case we need an open_confirm, no cleanup! */
1943 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1944 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001945 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001946 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001947 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001948out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001949 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001950}
1951
1952static const struct rpc_call_ops nfs4_open_ops = {
1953 .rpc_call_prepare = nfs4_open_prepare,
1954 .rpc_call_done = nfs4_open_done,
1955 .rpc_release = nfs4_open_release,
1956};
1957
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001958static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001959{
David Howells2b0143b2015-03-17 22:25:59 +00001960 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001961 struct nfs_server *server = NFS_SERVER(dir);
1962 struct nfs_openargs *o_arg = &data->o_arg;
1963 struct nfs_openres *o_res = &data->o_res;
1964 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001965 struct rpc_message msg = {
1966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1967 .rpc_argp = o_arg,
1968 .rpc_resp = o_res,
1969 .rpc_cred = data->owner->so_cred,
1970 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001971 struct rpc_task_setup task_setup_data = {
1972 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001973 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001974 .callback_ops = &nfs4_open_ops,
1975 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001976 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001977 .flags = RPC_TASK_ASYNC,
1978 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001979 int status;
1980
Chuck Levera9c92d62013-08-09 12:48:18 -04001981 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001982 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001983 data->rpc_done = 0;
1984 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001985 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001986 data->is_recover = 0;
1987 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001988 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001989 data->is_recover = 1;
1990 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001991 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001992 if (IS_ERR(task))
1993 return PTR_ERR(task);
1994 status = nfs4_wait_for_completion_rpc_task(task);
1995 if (status != 0) {
1996 data->cancelled = 1;
1997 smp_wmb();
1998 } else
1999 status = data->rpc_status;
2000 rpc_put_task(task);
2001
2002 return status;
2003}
2004
2005static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2006{
David Howells2b0143b2015-03-17 22:25:59 +00002007 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002008 struct nfs_openres *o_res = &data->o_res;
2009 int status;
2010
2011 status = nfs4_run_open_task(data, 1);
2012 if (status != 0 || !data->rpc_done)
2013 return status;
2014
Trond Myklebust6926afd2012-01-07 13:22:46 -05002015 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2016
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002017 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2018 status = _nfs4_proc_open_confirm(data);
2019 if (status != 0)
2020 return status;
2021 }
2022
2023 return status;
2024}
2025
Trond Myklebustf3792d62014-07-10 08:54:32 -04002026/*
2027 * Additional permission checks in order to distinguish between an
2028 * open for read, and an open for execute. This works around the
2029 * fact that NFSv4 OPEN treats read and execute permissions as being
2030 * the same.
2031 * Note that in the non-execute case, we want to turn off permission
2032 * checking if we just created a new file (POSIX open() semantics).
2033 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002034static int nfs4_opendata_access(struct rpc_cred *cred,
2035 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002036 struct nfs4_state *state, fmode_t fmode,
2037 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002038{
2039 struct nfs_access_entry cache;
2040 u32 mask;
2041
2042 /* access call failed or for some reason the server doesn't
2043 * support any access modes -- defer access call until later */
2044 if (opendata->o_res.access_supported == 0)
2045 return 0;
2046
2047 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002048 /*
2049 * Use openflags to check for exec, because fmode won't
2050 * always have FMODE_EXEC set when file open for exec.
2051 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002052 if (openflags & __FMODE_EXEC) {
2053 /* ONLY check for exec rights */
2054 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002055 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002056 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002057
2058 cache.cred = cred;
2059 cache.jiffies = jiffies;
2060 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2061 nfs_access_add_cache(state->inode, &cache);
2062
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002063 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002064 return 0;
2065
2066 /* even though OPEN succeeded, access is denied. Close the file */
2067 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002068 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002069}
2070
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002071/*
2072 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2073 */
2074static int _nfs4_proc_open(struct nfs4_opendata *data)
2075{
David Howells2b0143b2015-03-17 22:25:59 +00002076 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002077 struct nfs_server *server = NFS_SERVER(dir);
2078 struct nfs_openargs *o_arg = &data->o_arg;
2079 struct nfs_openres *o_res = &data->o_res;
2080 int status;
2081
2082 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002083 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002084 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002085 if (status != 0) {
2086 if (status == -NFS4ERR_BADNAME &&
2087 !(o_arg->open_flags & O_CREAT))
2088 return -ENOENT;
2089 return status;
2090 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002091
Trond Myklebust6926afd2012-01-07 13:22:46 -05002092 nfs_fattr_map_and_free_names(server, &data->f_attr);
2093
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002094 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002095 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002096 if (o_arg->open_flags & O_EXCL)
2097 data->file_created = 1;
2098 else if (o_res->cinfo.before != o_res->cinfo.after)
2099 data->file_created = 1;
2100 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002101 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2102 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002104 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002106 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002109 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Andy Adamsond83217c2011-03-01 01:34:17 +00002113static int nfs4_recover_expired_lease(struct nfs_server *server)
2114{
2115 return nfs4_client_recover_expired_lease(server->nfs_client);
2116}
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118/*
2119 * OPEN_EXPIRED:
2120 * reclaim state on the server after a network partition.
2121 * Assumes caller holds the appropriate lock
2122 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002123static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002125 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002126 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002128 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002129 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002130 if (IS_ERR(opendata))
2131 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002132 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002133 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002134 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002135 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002136 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002139static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002140{
Trond Myklebust539cd032007-06-05 11:46:42 -04002141 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002142 struct nfs4_exception exception = { };
2143 int err;
2144
2145 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002146 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002147 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002148 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2149 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002150 switch (err) {
2151 default:
2152 goto out;
2153 case -NFS4ERR_GRACE:
2154 case -NFS4ERR_DELAY:
2155 nfs4_handle_exception(server, err, &exception);
2156 err = 0;
2157 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002158 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002159out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002160 return err;
2161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002166 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Trond Myklebust864472e2006-01-03 09:55:15 +01002168 ctx = nfs4_state_find_open_context(state);
2169 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002170 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002171 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002172 put_nfs_open_context(ctx);
2173 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002176static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2177{
2178 nfs_remove_bad_delegation(state->inode);
2179 write_seqlock(&state->seqlock);
2180 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2181 write_sequnlock(&state->seqlock);
2182 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2183}
2184
2185static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2186{
2187 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2188 nfs_finish_clear_delegation_stateid(state);
2189}
2190
2191static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2192{
2193 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2194 nfs40_clear_delegation_stateid(state);
2195 return nfs4_open_expired(sp, state);
2196}
2197
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002198#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002199static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002200{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002201 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002202 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002203 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002204 struct rpc_cred *cred;
2205 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002206
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002207 /* Get the delegation credential for use by test/free_stateid */
2208 rcu_read_lock();
2209 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002210 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002211 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002212 return;
2213 }
2214
2215 nfs4_stateid_copy(&stateid, &delegation->stateid);
2216 cred = get_rpccred(delegation->cred);
2217 rcu_read_unlock();
2218 status = nfs41_test_stateid(server, &stateid, cred);
2219 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002220
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002221 if (status != NFS_OK) {
2222 /* Free the stateid unless the server explicitly
2223 * informs us the stateid is unrecognized. */
2224 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002225 nfs41_free_stateid(server, &stateid, cred);
2226 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002227 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002228
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002229 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002230}
2231
2232/**
2233 * nfs41_check_open_stateid - possibly free an open stateid
2234 *
2235 * @state: NFSv4 state for an inode
2236 *
2237 * Returns NFS_OK if recovery for this stateid is now finished.
2238 * Otherwise a negative NFS4ERR value is returned.
2239 */
2240static int nfs41_check_open_stateid(struct nfs4_state *state)
2241{
2242 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002243 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002244 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002245 int status;
2246
2247 /* If a state reset has been done, test_stateid is unneeded */
2248 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2249 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2250 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2251 return -NFS4ERR_BAD_STATEID;
2252
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002253 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002254 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002255 if (status != NFS_OK) {
2256 /* Free the stateid unless the server explicitly
2257 * informs us the stateid is unrecognized. */
2258 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002259 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002260
2261 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2262 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2263 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002264 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002265 }
2266 return status;
2267}
2268
2269static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2270{
Chuck Levereb64cf92012-07-11 16:30:05 -04002271 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002272
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002273 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002274 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002275 if (status != NFS_OK)
2276 status = nfs4_open_expired(sp, state);
2277 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002278}
2279#endif
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002282 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2283 * fields corresponding to attributes that were used to store the verifier.
2284 * Make sure we clobber those fields in the later setattr call
2285 */
2286static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2287{
2288 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2289 !(sattr->ia_valid & ATTR_ATIME_SET))
2290 sattr->ia_valid |= ATTR_ATIME;
2291
2292 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2293 !(sattr->ia_valid & ATTR_MTIME_SET))
2294 sattr->ia_valid |= ATTR_MTIME;
2295}
2296
Trond Myklebustc21443c2013-02-07 14:26:21 -05002297static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2298 fmode_t fmode,
2299 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002300 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002301{
2302 struct nfs4_state_owner *sp = opendata->owner;
2303 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002304 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002305 struct nfs4_state *state;
2306 unsigned int seq;
2307 int ret;
2308
2309 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2310
2311 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002312 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002313 goto out;
2314
2315 state = nfs4_opendata_to_nfs4_state(opendata);
2316 ret = PTR_ERR(state);
2317 if (IS_ERR(state))
2318 goto out;
2319 if (server->caps & NFS_CAP_POSIX_LOCK)
2320 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2321
Trond Myklebust275bb302013-05-29 13:11:28 -04002322 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002323 if (d_really_is_negative(dentry)) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002324 /* FIXME: Is this d_drop() ever needed? */
2325 d_drop(dentry);
2326 dentry = d_add_unique(dentry, igrab(state->inode));
2327 if (dentry == NULL) {
2328 dentry = opendata->dentry;
2329 } else if (dentry != ctx->dentry) {
2330 dput(ctx->dentry);
2331 ctx->dentry = dget(dentry);
2332 }
2333 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002334 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002335 }
2336
Trond Myklebustc21443c2013-02-07 14:26:21 -05002337 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2338 if (ret != 0)
2339 goto out;
2340
Trond Myklebust3efb9722013-05-29 13:17:04 -04002341 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002342 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002343 nfs_inode_attach_open_context(ctx);
2344 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2345 nfs4_schedule_stateid_recovery(server, state);
2346 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002347out:
2348 return ret;
2349}
2350
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002351/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002352 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 */
Andy Adamson82be4172012-05-23 05:02:35 -04002354static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002355 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002356 int flags,
2357 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002358 struct nfs4_label *label,
2359 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 struct nfs4_state_owner *sp;
2362 struct nfs4_state *state = NULL;
2363 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002364 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002365 struct dentry *dentry = ctx->dentry;
2366 struct rpc_cred *cred = ctx->cred;
2367 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2368 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002369 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002370 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002371 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002375 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2376 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2378 goto out_err;
2379 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002380 status = nfs4_recover_expired_lease(server);
2381 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002382 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002383 if (d_really_is_positive(dentry))
2384 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002385 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002386 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002387 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002388 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002389 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002390 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002391 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
David Quigley14c43f72013-05-22 12:50:43 -04002393 if (label) {
2394 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2395 if (IS_ERR(olabel)) {
2396 status = PTR_ERR(olabel);
2397 goto err_opendata_put;
2398 }
2399 }
2400
Trond Myklebuste911b812014-03-26 13:24:37 -07002401 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2402 if (!opendata->f_attr.mdsthreshold) {
2403 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2404 if (!opendata->f_attr.mdsthreshold)
2405 goto err_free_label;
2406 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002407 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002408 }
David Howells2b0143b2015-03-17 22:25:59 +00002409 if (d_really_is_positive(dentry))
2410 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002411
Trond Myklebust3efb9722013-05-29 13:17:04 -04002412 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002413 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002414 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002415 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002416
Trond Myklebust549b19c2013-04-16 18:42:34 -04002417 if ((opendata->o_arg.open_flags & O_EXCL) &&
2418 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002419 nfs4_exclusive_attrset(opendata, sattr);
2420
2421 nfs_fattr_init(opendata->o_res.f_attr);
2422 status = nfs4_do_setattr(state->inode, cred,
2423 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002424 state, label, olabel);
2425 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05002426 nfs_setattr_update_inode(state->inode, sattr,
2427 opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002428 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002429 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002430 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002431 if (opendata->file_created)
2432 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002433
Trond Myklebuste911b812014-03-26 13:24:37 -07002434 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002435 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002436 opendata->f_attr.mdsthreshold = NULL;
2437 }
Andy Adamson82be4172012-05-23 05:02:35 -04002438
David Quigley14c43f72013-05-22 12:50:43 -04002439 nfs4_label_free(olabel);
2440
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002441 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002444err_free_label:
2445 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002446err_opendata_put:
2447 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002448err_put_state_owner:
2449 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return status;
2452}
2453
2454
Andy Adamson82be4172012-05-23 05:02:35 -04002455static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002456 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002457 int flags,
2458 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002459 struct nfs4_label *label,
2460 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002462 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 struct nfs4_exception exception = { };
2464 struct nfs4_state *res;
2465 int status;
2466
2467 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002468 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002469 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002470 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (status == 0)
2472 break;
2473 /* NOTE: BAD_SEQID means the server and client disagree about the
2474 * book-keeping w.r.t. state-changing operations
2475 * (OPEN/CLOSE/LOCK/LOCKU...)
2476 * It is actually a sign of a bug on the client or on the server.
2477 *
2478 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002479 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 * have unhashed the old state_owner for us, and that we can
2481 * therefore safely retry using a new one. We should still warn
2482 * the user though...
2483 */
2484 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002485 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002486 " returned a bad sequence-id error!\n",
2487 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 exception.retry = 1;
2489 continue;
2490 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002491 /*
2492 * BAD_STATEID on OPEN means that the server cancelled our
2493 * state before it received the OPEN_CONFIRM.
2494 * Recover by retrying the request as per the discussion
2495 * on Page 181 of RFC3530.
2496 */
2497 if (status == -NFS4ERR_BAD_STATEID) {
2498 exception.retry = 1;
2499 continue;
2500 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002501 if (status == -EAGAIN) {
2502 /* We must have found a delegation */
2503 exception.retry = 1;
2504 continue;
2505 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002506 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2507 continue;
2508 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 status, &exception));
2510 } while (exception.retry);
2511 return res;
2512}
2513
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002514static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2515 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002516 struct nfs4_state *state, struct nfs4_label *ilabel,
2517 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002519 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002521 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 .iap = sattr,
2523 .server = server,
2524 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002525 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 };
2527 struct nfs_setattrres res = {
2528 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002529 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 .server = server,
2531 };
2532 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2534 .rpc_argp = &arg,
2535 .rpc_resp = &res,
2536 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002538 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002539 fmode_t fmode;
2540 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002541 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
David Quigleyaa9c2662013-05-22 12:50:44 -04002543 arg.bitmask = nfs4_bitmask(server, ilabel);
2544 if (ilabel)
2545 arg.bitmask = nfs4_bitmask(server, olabel);
2546
Trond Myklebust0e574af2005-10-27 22:12:38 -04002547 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Trond Myklebustee3ae842013-04-29 10:35:36 -04002549 /* Servers should only apply open mode checks for file size changes */
2550 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2551 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2552
2553 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2554 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002555 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002556 struct nfs_lockowner lockowner = {
2557 .l_owner = current->files,
2558 .l_pid = current->tgid,
2559 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002560 if (!nfs4_valid_open_stateid(state))
2561 return -EBADF;
2562 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2563 &lockowner) == -EIO)
2564 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002565 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002566 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Bryan Schumaker7c513052011-03-24 17:12:24 +00002568 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002569 if (status == 0 && state != NULL)
2570 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002571 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002574static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2575 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002576 struct nfs4_state *state, struct nfs4_label *ilabel,
2577 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002579 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002580 struct nfs4_exception exception = {
2581 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002582 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002583 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 int err;
2585 do {
David Quigley1775fd32013-05-22 12:50:42 -04002586 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002587 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002588 switch (err) {
2589 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002590 if (!(sattr->ia_valid & ATTR_SIZE)) {
2591 pr_warn_once("NFSv4: server %s is incorrectly "
2592 "applying open mode checks to "
2593 "a SETATTR that is not "
2594 "changing file size.\n",
2595 server->nfs_client->cl_hostname);
2596 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002597 if (state && !(state->state & FMODE_WRITE)) {
2598 err = -EBADF;
2599 if (sattr->ia_valid & ATTR_OPEN)
2600 err = -EACCES;
2601 goto out;
2602 }
2603 }
2604 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002606out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return err;
2608}
2609
2610struct nfs4_closedata {
2611 struct inode *inode;
2612 struct nfs4_state *state;
2613 struct nfs_closeargs arg;
2614 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002615 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002616 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002617 bool roc;
2618 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619};
2620
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002621static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002622{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002623 struct nfs4_closedata *calldata = data;
2624 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002625 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002626
Fred Isamanf7e89172011-01-06 11:36:32 +00002627 if (calldata->roc)
2628 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002629 nfs4_put_open_state(calldata->state);
2630 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002631 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002632 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002633 kfree(calldata);
2634}
2635
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002636static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002638 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002641 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Chuck Levera3ca5652012-03-01 17:00:40 -05002643 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002644 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2645 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002646 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 /* hmm. we are done with the inode, and in the process of freeing
2648 * the state_owner. we keep this around to process errors
2649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 switch (task->tk_status) {
2651 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002652 res_stateid = &calldata->res.stateid;
2653 if (calldata->arg.fmode == 0 && calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002654 pnfs_roc_set_barrier(state->inode,
2655 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002656 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002657 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002658 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002660 case -NFS4ERR_OLD_STATEID:
2661 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002663 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002664 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002665 rpc_restart_call_prepare(task);
2666 goto out_release;
2667 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002668 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002669 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002671 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002672 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002673 goto out_release;
2674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
Trond Myklebust412f6c42014-08-25 22:09:08 -04002676 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002677out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002678 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002679 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002680 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002683static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002685 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002686 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002687 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002688 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002689 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002690
Chuck Levera3ca5652012-03-01 17:00:40 -05002691 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002692 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002693 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002694
Trond Myklebust88069f72009-12-08 08:33:16 -05002695 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002696 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002697 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2698 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2699 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002700 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002701 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002702 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002703 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002704 if (state->n_rdonly == 0)
2705 call_close |= is_rdonly;
2706 else if (is_rdonly)
2707 calldata->arg.fmode |= FMODE_READ;
2708 if (state->n_wronly == 0)
2709 call_close |= is_wronly;
2710 else if (is_wronly)
2711 calldata->arg.fmode |= FMODE_WRITE;
2712 } else if (is_rdwr)
2713 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2714
2715 if (calldata->arg.fmode == 0)
2716 call_close |= is_rdwr;
2717
Trond Myklebust5d422302013-03-14 16:57:48 -04002718 if (!nfs4_valid_open_stateid(state))
2719 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002720 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002721
2722 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002723 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002724 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002725 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002726
Fred Isamanf7e89172011-01-06 11:36:32 +00002727 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002728 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002729 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002730 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2731 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002732 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002733 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002734 }
Trond Myklebust6ae37332015-01-30 14:21:14 -05002735 calldata->arg.share_access =
2736 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2737 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002738
Trond Myklebust516a6af2005-10-27 22:12:41 -04002739 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002740 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002741 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002742 &calldata->arg.seq_args,
2743 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002744 task) != 0)
2745 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002746 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002747 return;
2748out_no_action:
2749 task->tk_action = NULL;
2750out_wait:
2751 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
2753
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002754static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002755 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002756 .rpc_call_done = nfs4_close_done,
2757 .rpc_release = nfs4_free_closedata,
2758};
2759
Peng Taofe08c542014-07-03 13:05:01 +08002760static bool nfs4_roc(struct inode *inode)
2761{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002762 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002763 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002764 return pnfs_roc(inode);
2765}
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767/*
2768 * It is possible for data to be read/written from a mem-mapped file
2769 * after the sys_close call (which hits the vfs layer as a flush).
2770 * This means that we can't safely call nfsv4 close on a file until
2771 * the inode is cleared. This in turn means that we are not good
2772 * NFSv4 citizens - we do not indicate to the server to update the file's
2773 * share state even when we are done with one of the three share
2774 * stateid's in the inode.
2775 *
2776 * NOTE: Caller must be holding the sp->so_owner semaphore!
2777 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002778int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002780 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002781 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002783 struct nfs4_state_owner *sp = state->owner;
2784 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002785 struct rpc_message msg = {
2786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2787 .rpc_cred = state->owner->so_cred,
2788 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002789 struct rpc_task_setup task_setup_data = {
2790 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002791 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002792 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002793 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002794 .flags = RPC_TASK_ASYNC,
2795 };
Trond Myklebust95121352005-10-18 14:20:12 -07002796 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002798 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2799 &task_setup_data.rpc_client, &msg);
2800
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002801 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002803 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002804 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002805 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002807 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002809 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2810 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002811 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002812 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002813 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002814 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002815 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002816 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002817 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002818 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002819 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002820
Trond Myklebust1174dd12010-12-21 10:52:24 -05002821 msg.rpc_argp = &calldata->arg;
2822 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002823 task_setup_data.callback_data = calldata;
2824 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002825 if (IS_ERR(task))
2826 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002827 status = 0;
2828 if (wait)
2829 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002830 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002831 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002832out_free_calldata:
2833 kfree(calldata);
2834out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002835 nfs4_put_open_state(state);
2836 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002837 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
Trond Myklebust2b484292010-09-17 10:56:51 -04002840static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002841nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2842 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002845 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2846
2847 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Trond Myklebust565277f2007-10-15 18:17:53 -04002849 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002850 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002851
2852 nfs4_label_release_security(label);
2853
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002854 if (IS_ERR(state))
2855 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002856 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
Trond Myklebust1185a552009-12-03 15:54:02 -05002859static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002860{
2861 if (ctx->state == NULL)
2862 return;
2863 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002864 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002865 else
Al Viro643168c2011-06-22 18:20:23 -04002866 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002867}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Trond Myklebustb944dba2013-11-04 15:20:20 -05002869#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2870#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002871#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2874{
Benny Halevy43652ad2009-04-01 09:21:54 -04002875 struct nfs4_server_caps_arg args = {
2876 .fhandle = fhandle,
2877 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 struct nfs4_server_caps_res res = {};
2879 struct rpc_message msg = {
2880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002881 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 .rpc_resp = &res,
2883 };
2884 int status;
2885
Bryan Schumaker7c513052011-03-24 17:12:24 +00002886 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002888 /* Sanity check the server answers */
2889 switch (server->nfs_client->cl_minorversion) {
2890 case 0:
2891 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2892 res.attr_bitmask[2] = 0;
2893 break;
2894 case 1:
2895 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2896 break;
2897 case 2:
2898 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002901 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2902 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2903 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2904 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002905 NFS_CAP_CTIME|NFS_CAP_MTIME|
2906 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002907 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2908 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 server->caps |= NFS_CAP_ACLS;
2910 if (res.has_links != 0)
2911 server->caps |= NFS_CAP_HARDLINKS;
2912 if (res.has_symlinks != 0)
2913 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002914 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2915 server->caps |= NFS_CAP_FILEID;
2916 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2917 server->caps |= NFS_CAP_MODE;
2918 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2919 server->caps |= NFS_CAP_NLINK;
2920 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2921 server->caps |= NFS_CAP_OWNER;
2922 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2923 server->caps |= NFS_CAP_OWNER_GROUP;
2924 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2925 server->caps |= NFS_CAP_ATIME;
2926 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2927 server->caps |= NFS_CAP_CTIME;
2928 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2929 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002930#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2931 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2932 server->caps |= NFS_CAP_SECURITY_LABEL;
2933#endif
2934 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2935 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002936 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002937
Trond Myklebusta65318b2009-03-11 14:10:28 -04002938 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2939 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2940 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002941 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002943 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return status;
2947}
2948
Trond Myklebust55a97592006-06-09 09:34:19 -04002949int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
2951 struct nfs4_exception exception = { };
2952 int err;
2953 do {
2954 err = nfs4_handle_exception(server,
2955 _nfs4_server_capabilities(server, fhandle),
2956 &exception);
2957 } while (exception.retry);
2958 return err;
2959}
2960
2961static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2962 struct nfs_fsinfo *info)
2963{
David Quigleyaa9c2662013-05-22 12:50:44 -04002964 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002966 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 };
2968 struct nfs4_lookup_res res = {
2969 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002970 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 .fh = fhandle,
2972 };
2973 struct rpc_message msg = {
2974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2975 .rpc_argp = &args,
2976 .rpc_resp = &res,
2977 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002978
David Quigleyaa9c2662013-05-22 12:50:44 -04002979 bitmask[0] = nfs4_fattr_bitmap[0];
2980 bitmask[1] = nfs4_fattr_bitmap[1];
2981 /*
2982 * Process the label in the upcoming getfattr
2983 */
2984 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2985
Trond Myklebust0e574af2005-10-27 22:12:38 -04002986 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002987 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
2989
2990static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2991 struct nfs_fsinfo *info)
2992{
2993 struct nfs4_exception exception = { };
2994 int err;
2995 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002996 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002997 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002998 switch (err) {
2999 case 0:
3000 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003001 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003002 default:
3003 err = nfs4_handle_exception(server, err, &exception);
3004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003006out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return err;
3008}
3009
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003010static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3011 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3012{
Trond Myklebustc2190662013-08-26 19:23:04 -04003013 struct rpc_auth_create_args auth_args = {
3014 .pseudoflavor = flavor,
3015 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003016 struct rpc_auth *auth;
3017 int ret;
3018
Trond Myklebustc2190662013-08-26 19:23:04 -04003019 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003020 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003021 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003022 goto out;
3023 }
3024 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003025out:
3026 return ret;
3027}
3028
Chuck Lever9a744ba2013-03-16 15:56:02 -04003029/*
3030 * Retry pseudoroot lookup with various security flavors. We do this when:
3031 *
3032 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3033 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3034 *
3035 * Returns zero on success, or a negative NFS4ERR value, or a
3036 * negative errno value.
3037 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003038static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003039 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003041 /* Per 3530bis 15.33.5 */
3042 static const rpc_authflavor_t flav_array[] = {
3043 RPC_AUTH_GSS_KRB5P,
3044 RPC_AUTH_GSS_KRB5I,
3045 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003046 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003047 RPC_AUTH_NULL,
3048 };
3049 int status = -EPERM;
3050 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003052 if (server->auth_info.flavor_len > 0) {
3053 /* try each flavor specified by user */
3054 for (i = 0; i < server->auth_info.flavor_len; i++) {
3055 status = nfs4_lookup_root_sec(server, fhandle, info,
3056 server->auth_info.flavors[i]);
3057 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3058 continue;
3059 break;
3060 }
3061 } else {
3062 /* no flavors specified by user, try default list */
3063 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3064 status = nfs4_lookup_root_sec(server, fhandle, info,
3065 flav_array[i]);
3066 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3067 continue;
3068 break;
3069 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003070 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003071
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003072 /*
3073 * -EACCESS could mean that the user doesn't have correct permissions
3074 * to access the mount. It could also mean that we tried to mount
3075 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3076 * existing mount programs don't handle -EACCES very well so it should
3077 * be mapped to -EPERM instead.
3078 */
3079 if (status == -EACCES)
3080 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003081 return status;
3082}
3083
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003084static int nfs4_do_find_root_sec(struct nfs_server *server,
3085 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3086{
3087 int mv = server->nfs_client->cl_minorversion;
3088 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3089}
3090
3091/**
3092 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3093 * @server: initialized nfs_server handle
3094 * @fhandle: we fill in the pseudo-fs root file handle
3095 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003096 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003097 *
3098 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003099 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003100int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003101 struct nfs_fsinfo *info,
3102 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003103{
Andre Przywarac7757072015-04-23 17:17:40 +01003104 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003105
Andre Przywarac7757072015-04-23 17:17:40 +01003106 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003107 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003108
3109 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003110 status = nfs4_do_find_root_sec(server, fhandle, info);
3111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 if (status == 0)
3113 status = nfs4_server_capabilities(server, fhandle);
3114 if (status == 0)
3115 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003116
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003117 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
Bryan Schumakerbae36242012-05-10 15:07:31 -04003120static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3121 struct nfs_fsinfo *info)
3122{
3123 int error;
3124 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003125 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003126
3127 error = nfs4_server_capabilities(server, mntfh);
3128 if (error < 0) {
3129 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3130 return error;
3131 }
3132
David Quigley14c43f72013-05-22 12:50:43 -04003133 label = nfs4_label_alloc(server, GFP_KERNEL);
3134 if (IS_ERR(label))
3135 return PTR_ERR(label);
3136
David Quigley1775fd32013-05-22 12:50:42 -04003137 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003138 if (error < 0) {
3139 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003140 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003141 }
3142
3143 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3144 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3145 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3146
David Quigley14c43f72013-05-22 12:50:43 -04003147err_free_label:
3148 nfs4_label_free(label);
3149
Bryan Schumakerbae36242012-05-10 15:07:31 -04003150 return error;
3151}
3152
Manoj Naik6b97fd32006-06-09 09:34:29 -04003153/*
3154 * Get locations and (maybe) other attributes of a referral.
3155 * Note that we'll actually follow the referral later when
3156 * we detect fsid mismatch in inode revalidation
3157 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003158static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3159 const struct qstr *name, struct nfs_fattr *fattr,
3160 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003161{
3162 int status = -ENOMEM;
3163 struct page *page = NULL;
3164 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003165
3166 page = alloc_page(GFP_KERNEL);
3167 if (page == NULL)
3168 goto out;
3169 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3170 if (locations == NULL)
3171 goto out;
3172
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003173 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003174 if (status != 0)
3175 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003176
3177 /*
3178 * If the fsid didn't change, this is a migration event, not a
3179 * referral. Cause us to drop into the exception handler, which
3180 * will kick off migration recovery.
3181 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003182 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003183 dprintk("%s: server did not return a different fsid for"
3184 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003185 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003186 goto out;
3187 }
Andy Adamson533eb462011-06-13 18:25:56 -04003188 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3189 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003190
Andy Adamson533eb462011-06-13 18:25:56 -04003191 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003192 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003193 memset(fhandle, 0, sizeof(struct nfs_fh));
3194out:
3195 if (page)
3196 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003197 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003198 return status;
3199}
3200
David Quigley1775fd32013-05-22 12:50:42 -04003201static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3202 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
3204 struct nfs4_getattr_arg args = {
3205 .fh = fhandle,
3206 .bitmask = server->attr_bitmask,
3207 };
3208 struct nfs4_getattr_res res = {
3209 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003210 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 .server = server,
3212 };
3213 struct rpc_message msg = {
3214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3215 .rpc_argp = &args,
3216 .rpc_resp = &res,
3217 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003218
3219 args.bitmask = nfs4_bitmask(server, label);
3220
Trond Myklebust0e574af2005-10-27 22:12:38 -04003221 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003222 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223}
3224
David Quigley1775fd32013-05-22 12:50:42 -04003225static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3226 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
3228 struct nfs4_exception exception = { };
3229 int err;
3230 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003231 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3232 trace_nfs4_getattr(server, fhandle, fattr, err);
3233 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 &exception);
3235 } while (exception.retry);
3236 return err;
3237}
3238
3239/*
3240 * The file is not closed if it is opened due to the a request to change
3241 * the size of the file. The open call will not be needed once the
3242 * VFS layer lookup-intents are implemented.
3243 *
3244 * Close is called when the inode is destroyed.
3245 * If we haven't opened the file for O_WRONLY, we
3246 * need to in the size_change case to obtain a stateid.
3247 *
3248 * Got race?
3249 * Because OPEN is always done by name in nfsv4, it is
3250 * possible that we opened a different file by the same
3251 * name. We can recognize this race condition, but we
3252 * can't do anything about it besides returning an error.
3253 *
3254 * This will be fixed with VFS changes (lookup-intent).
3255 */
3256static int
3257nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3258 struct iattr *sattr)
3259{
David Howells2b0143b2015-03-17 22:25:59 +00003260 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003261 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003262 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003263 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 int status;
3265
Peng Tao88ac8152014-09-12 11:04:10 +08003266 if (pnfs_ld_layoutret_on_setattr(inode) &&
3267 sattr->ia_valid & ATTR_SIZE &&
3268 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003269 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003270
Trond Myklebust0e574af2005-10-27 22:12:38 -04003271 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Andy Adamson26699402012-05-30 16:12:24 -04003273 /* Deal with open(O_TRUNC) */
3274 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003275 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003276
3277 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003278 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003279 return 0;
3280
Trond Myklebustd5308382005-11-04 15:33:38 -05003281 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003282 if (sattr->ia_valid & ATTR_FILE) {
3283 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003284
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003285 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003286 if (ctx) {
3287 cred = ctx->cred;
3288 state = ctx->state;
3289 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003290 }
3291
David Quigley14c43f72013-05-22 12:50:43 -04003292 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3293 if (IS_ERR(label))
3294 return PTR_ERR(label);
3295
3296 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003297 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003298 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003299 nfs_setsecurity(inode, fattr, label);
3300 }
David Quigley14c43f72013-05-22 12:50:43 -04003301 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return status;
3303}
3304
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003305static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3306 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003307 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003308{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003309 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003310 int status;
3311 struct nfs4_lookup_arg args = {
3312 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003313 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003314 .name = name,
3315 };
3316 struct nfs4_lookup_res res = {
3317 .server = server,
3318 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003319 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003320 .fh = fhandle,
3321 };
3322 struct rpc_message msg = {
3323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3324 .rpc_argp = &args,
3325 .rpc_resp = &res,
3326 };
3327
David Quigleyaa9c2662013-05-22 12:50:44 -04003328 args.bitmask = nfs4_bitmask(server, label);
3329
David Howells2b3de442006-08-22 20:06:09 -04003330 nfs_fattr_init(fattr);
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003333 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 dprintk("NFS reply lookup: %d\n", status);
3335 return status;
3336}
3337
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003338static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003339{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003340 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003341 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003342 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3343 fattr->nlink = 2;
3344}
3345
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003346static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3347 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003348 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003349{
3350 struct nfs4_exception exception = { };
3351 struct rpc_clnt *client = *clnt;
3352 int err;
3353 do {
David Quigley1775fd32013-05-22 12:50:42 -04003354 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003355 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003356 switch (err) {
3357 case -NFS4ERR_BADNAME:
3358 err = -ENOENT;
3359 goto out;
3360 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003361 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003362 if (err == -NFS4ERR_MOVED)
3363 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003364 goto out;
3365 case -NFS4ERR_WRONGSEC:
3366 err = -EPERM;
3367 if (client != *clnt)
3368 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003369 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003370 if (IS_ERR(client))
3371 return PTR_ERR(client);
3372
3373 exception.retry = 1;
3374 break;
3375 default:
3376 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3377 }
3378 } while (exception.retry);
3379
3380out:
3381 if (err == 0)
3382 *clnt = client;
3383 else if (client != *clnt)
3384 rpc_shutdown_client(client);
3385
3386 return err;
3387}
3388
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003389static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003390 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3391 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003393 int status;
3394 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003395
David Quigley1775fd32013-05-22 12:50:42 -04003396 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003397 if (client != NFS_CLIENT(dir)) {
3398 rpc_shutdown_client(client);
3399 nfs_fixup_secinfo_attributes(fattr);
3400 }
3401 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003404struct rpc_clnt *
3405nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3406 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3407{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003408 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003409 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003410
David Quigley1775fd32013-05-22 12:50:42 -04003411 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003412 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003413 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003414 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003415}
3416
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3418{
Trond Myklebust76b32992007-08-10 17:45:11 -04003419 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 struct nfs4_accessargs args = {
3421 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003422 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003424 struct nfs4_accessres res = {
3425 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 struct rpc_message msg = {
3428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3429 .rpc_argp = &args,
3430 .rpc_resp = &res,
3431 .rpc_cred = entry->cred,
3432 };
3433 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003434 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
3436 /*
3437 * Determine which access bits we want to ask for...
3438 */
3439 if (mode & MAY_READ)
3440 args.access |= NFS4_ACCESS_READ;
3441 if (S_ISDIR(inode->i_mode)) {
3442 if (mode & MAY_WRITE)
3443 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3444 if (mode & MAY_EXEC)
3445 args.access |= NFS4_ACCESS_LOOKUP;
3446 } else {
3447 if (mode & MAY_WRITE)
3448 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3449 if (mode & MAY_EXEC)
3450 args.access |= NFS4_ACCESS_EXECUTE;
3451 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003452
3453 res.fattr = nfs_alloc_fattr();
3454 if (res.fattr == NULL)
3455 return -ENOMEM;
3456
Bryan Schumaker7c513052011-03-24 17:12:24 +00003457 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003459 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003460 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003462 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return status;
3464}
3465
3466static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3467{
3468 struct nfs4_exception exception = { };
3469 int err;
3470 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003471 err = _nfs4_proc_access(inode, entry);
3472 trace_nfs4_access(inode, err);
3473 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 &exception);
3475 } while (exception.retry);
3476 return err;
3477}
3478
3479/*
3480 * TODO: For the time being, we don't try to get any attributes
3481 * along with any of the zero-copy operations READ, READDIR,
3482 * READLINK, WRITE.
3483 *
3484 * In the case of the first three, we want to put the GETATTR
3485 * after the read-type operation -- this is because it is hard
3486 * to predict the length of a GETATTR response in v4, and thus
3487 * align the READ data correctly. This means that the GETATTR
3488 * may end up partially falling into the page cache, and we should
3489 * shift it into the 'tail' of the xdr_buf before processing.
3490 * To do this efficiently, we need to know the total length
3491 * of data received, which doesn't seem to be available outside
3492 * of the RPC layer.
3493 *
3494 * In the case of WRITE, we also want to put the GETATTR after
3495 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003496 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 *
3498 * Both of these changes to the XDR layer would in fact be quite
3499 * minor, but I decided to leave them for a subsequent patch.
3500 */
3501static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3502 unsigned int pgbase, unsigned int pglen)
3503{
3504 struct nfs4_readlink args = {
3505 .fh = NFS_FH(inode),
3506 .pgbase = pgbase,
3507 .pglen = pglen,
3508 .pages = &page,
3509 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003510 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 struct rpc_message msg = {
3512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3513 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003514 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 };
3516
Bryan Schumaker7c513052011-03-24 17:12:24 +00003517 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 -07003518}
3519
3520static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3521 unsigned int pgbase, unsigned int pglen)
3522{
3523 struct nfs4_exception exception = { };
3524 int err;
3525 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003526 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3527 trace_nfs4_readlink(inode, err);
3528 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 &exception);
3530 } while (exception.retry);
3531 return err;
3532}
3533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003535 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537static int
3538nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003539 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
David Quigleyaa9c2662013-05-22 12:50:44 -04003541 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003542 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003544 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 int status = 0;
3546
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003547 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3548 if (IS_ERR(ctx))
3549 return PTR_ERR(ctx);
3550
David Quigleyaa9c2662013-05-22 12:50:44 -04003551 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3552
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003553 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003554 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 if (IS_ERR(state)) {
3556 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003560 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003561 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 return status;
3563}
3564
3565static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3566{
Trond Myklebust16e42952005-10-27 22:12:44 -04003567 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003568 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003570 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003572 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003573 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003574 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3577 .rpc_argp = &args,
3578 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003580 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003581
Bryan Schumaker7c513052011-03-24 17:12:24 +00003582 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003583 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003584 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return status;
3586}
3587
3588static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3589{
3590 struct nfs4_exception exception = { };
3591 int err;
3592 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003593 err = _nfs4_proc_remove(dir, name);
3594 trace_nfs4_remove(dir, name, err);
3595 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 &exception);
3597 } while (exception.retry);
3598 return err;
3599}
3600
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003601static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003603 struct nfs_server *server = NFS_SERVER(dir);
3604 struct nfs_removeargs *args = msg->rpc_argp;
3605 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003607 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003609 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003610
3611 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612}
3613
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003614static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3615{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003616 nfs4_setup_sequence(NFS_SERVER(data->dir),
3617 &data->args.seq_args,
3618 &data->res.seq_res,
3619 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
3621
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003622static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003624 struct nfs_unlinkdata *data = task->tk_calldata;
3625 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003626
Trond Myklebust14516c32010-07-31 14:29:06 -04003627 if (!nfs4_sequence_done(task, &res->seq_res))
3628 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003629 if (nfs4_async_handle_error(task, res->server, NULL,
3630 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003631 return 0;
3632 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003633 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634}
3635
Jeff Laytond3d41522010-09-17 17:31:57 -04003636static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3637{
3638 struct nfs_server *server = NFS_SERVER(dir);
3639 struct nfs_renameargs *arg = msg->rpc_argp;
3640 struct nfs_renameres *res = msg->rpc_resp;
3641
3642 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003643 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003644 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003645}
3646
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003647static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3648{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003649 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3650 &data->args.seq_args,
3651 &data->res.seq_res,
3652 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003653}
3654
3655static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3656 struct inode *new_dir)
3657{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003658 struct nfs_renamedata *data = task->tk_calldata;
3659 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003660
3661 if (!nfs4_sequence_done(task, &res->seq_res))
3662 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003663 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003664 return 0;
3665
3666 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003667 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003668 return 1;
3669}
3670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3672{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003673 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 struct nfs4_link_arg arg = {
3675 .fh = NFS_FH(inode),
3676 .dir_fh = NFS_FH(dir),
3677 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003678 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003680 struct nfs4_link_res res = {
3681 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003682 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003683 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 struct rpc_message msg = {
3685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3686 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003687 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003689 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
Trond Myklebust136f2622010-04-16 16:22:49 -04003691 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003692 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003693 goto out;
3694
David Quigley14c43f72013-05-22 12:50:43 -04003695 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3696 if (IS_ERR(res.label)) {
3697 status = PTR_ERR(res.label);
3698 goto out;
3699 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003700 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003701
Bryan Schumaker7c513052011-03-24 17:12:24 +00003702 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003703 if (!status) {
3704 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003705 status = nfs_post_op_update_inode(inode, res.fattr);
3706 if (!status)
3707 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003708 }
David Quigley14c43f72013-05-22 12:50:43 -04003709
3710
3711 nfs4_label_free(res.label);
3712
Trond Myklebust136f2622010-04-16 16:22:49 -04003713out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003714 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return status;
3716}
3717
3718static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3719{
3720 struct nfs4_exception exception = { };
3721 int err;
3722 do {
3723 err = nfs4_handle_exception(NFS_SERVER(inode),
3724 _nfs4_proc_link(inode, dir, name),
3725 &exception);
3726 } while (exception.retry);
3727 return err;
3728}
3729
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003730struct nfs4_createdata {
3731 struct rpc_message msg;
3732 struct nfs4_create_arg arg;
3733 struct nfs4_create_res res;
3734 struct nfs_fh fh;
3735 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003736 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003737};
3738
3739static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3740 struct qstr *name, struct iattr *sattr, u32 ftype)
3741{
3742 struct nfs4_createdata *data;
3743
3744 data = kzalloc(sizeof(*data), GFP_KERNEL);
3745 if (data != NULL) {
3746 struct nfs_server *server = NFS_SERVER(dir);
3747
David Quigley14c43f72013-05-22 12:50:43 -04003748 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3749 if (IS_ERR(data->label))
3750 goto out_free;
3751
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003752 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3753 data->msg.rpc_argp = &data->arg;
3754 data->msg.rpc_resp = &data->res;
3755 data->arg.dir_fh = NFS_FH(dir);
3756 data->arg.server = server;
3757 data->arg.name = name;
3758 data->arg.attrs = sattr;
3759 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003760 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003761 data->res.server = server;
3762 data->res.fh = &data->fh;
3763 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003764 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003765 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003766 }
3767 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003768out_free:
3769 kfree(data);
3770 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003771}
3772
3773static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3774{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003775 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003776 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003777 if (status == 0) {
3778 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003779 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003780 }
3781 return status;
3782}
3783
3784static void nfs4_free_createdata(struct nfs4_createdata *data)
3785{
David Quigley14c43f72013-05-22 12:50:43 -04003786 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003787 kfree(data);
3788}
3789
Chuck Lever4f390c12006-08-22 20:06:22 -04003790static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003791 struct page *page, unsigned int len, struct iattr *sattr,
3792 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003794 struct nfs4_createdata *data;
3795 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Chuck Lever94a6d752006-08-22 20:06:23 -04003797 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003798 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003799
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003800 status = -ENOMEM;
3801 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3802 if (data == NULL)
3803 goto out;
3804
3805 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3806 data->arg.u.symlink.pages = &page;
3807 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003808 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003810 status = nfs4_do_create(dir, dentry, data);
3811
3812 nfs4_free_createdata(data);
3813out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 return status;
3815}
3816
Chuck Lever4f390c12006-08-22 20:06:22 -04003817static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003818 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819{
3820 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003821 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003823
3824 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003827 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3828 trace_nfs4_symlink(dir, &dentry->d_name, err);
3829 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 &exception);
3831 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003832
3833 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 return err;
3835}
3836
3837static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003838 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003840 struct nfs4_createdata *data;
3841 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003843 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3844 if (data == NULL)
3845 goto out;
3846
David Quigley1775fd32013-05-22 12:50:42 -04003847 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003848 status = nfs4_do_create(dir, dentry, data);
3849
3850 nfs4_free_createdata(data);
3851out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 return status;
3853}
3854
3855static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3856 struct iattr *sattr)
3857{
3858 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003859 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003861
David Quigleyaa9c2662013-05-22 12:50:44 -04003862 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3863
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003864 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003866 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3867 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3868 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 &exception);
3870 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003871 nfs4_label_release_security(label);
3872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return err;
3874}
3875
3876static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003877 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878{
David Howells2b0143b2015-03-17 22:25:59 +00003879 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 struct nfs4_readdir_arg args = {
3881 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003882 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 .pgbase = 0,
3884 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00003885 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003886 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 };
3888 struct nfs4_readdir_res res;
3889 struct rpc_message msg = {
3890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3891 .rpc_argp = &args,
3892 .rpc_resp = &res,
3893 .rpc_cred = cred,
3894 };
3895 int status;
3896
Al Viro6de14722013-09-16 10:53:17 -04003897 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3898 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003899 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003900 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003902 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 -05003903 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003904 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003905 status += args.pgbase;
3906 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003907
3908 nfs_invalidate_atime(dir);
3909
Harvey Harrison3110ff82008-05-02 13:42:44 -07003910 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 return status;
3912}
3913
3914static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003915 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
3917 struct nfs4_exception exception = { };
3918 int err;
3919 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003920 err = _nfs4_proc_readdir(dentry, cred, cookie,
3921 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00003922 trace_nfs4_readdir(d_inode(dentry), err);
3923 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 &exception);
3925 } while (exception.retry);
3926 return err;
3927}
3928
3929static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003930 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003932 struct nfs4_createdata *data;
3933 int mode = sattr->ia_mode;
3934 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003936 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3937 if (data == NULL)
3938 goto out;
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003941 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003943 data->arg.ftype = NF4BLK;
3944 data->arg.u.device.specdata1 = MAJOR(rdev);
3945 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 }
3947 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003948 data->arg.ftype = NF4CHR;
3949 data->arg.u.device.specdata1 = MAJOR(rdev);
3950 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003951 } else if (!S_ISSOCK(mode)) {
3952 status = -EINVAL;
3953 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 }
David Quigley1775fd32013-05-22 12:50:42 -04003955
David Quigleyaa9c2662013-05-22 12:50:44 -04003956 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003957 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003958out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003959 nfs4_free_createdata(data);
3960out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 return status;
3962}
3963
3964static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3965 struct iattr *sattr, dev_t rdev)
3966{
3967 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003968 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003970
David Quigleyaa9c2662013-05-22 12:50:44 -04003971 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3972
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003973 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003975 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3976 trace_nfs4_mknod(dir, &dentry->d_name, err);
3977 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 &exception);
3979 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003980
3981 nfs4_label_release_security(label);
3982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return err;
3984}
3985
3986static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3987 struct nfs_fsstat *fsstat)
3988{
3989 struct nfs4_statfs_arg args = {
3990 .fh = fhandle,
3991 .bitmask = server->attr_bitmask,
3992 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003993 struct nfs4_statfs_res res = {
3994 .fsstat = fsstat,
3995 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 struct rpc_message msg = {
3997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3998 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003999 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 };
4001
Trond Myklebust0e574af2005-10-27 22:12:38 -04004002 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004003 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004}
4005
4006static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4007{
4008 struct nfs4_exception exception = { };
4009 int err;
4010 do {
4011 err = nfs4_handle_exception(server,
4012 _nfs4_proc_statfs(server, fhandle, fsstat),
4013 &exception);
4014 } while (exception.retry);
4015 return err;
4016}
4017
4018static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4019 struct nfs_fsinfo *fsinfo)
4020{
4021 struct nfs4_fsinfo_arg args = {
4022 .fh = fhandle,
4023 .bitmask = server->attr_bitmask,
4024 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004025 struct nfs4_fsinfo_res res = {
4026 .fsinfo = fsinfo,
4027 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 struct rpc_message msg = {
4029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4030 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004031 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 };
4033
Bryan Schumaker7c513052011-03-24 17:12:24 +00004034 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035}
4036
4037static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4038{
4039 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004040 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 int err;
4042
4043 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004044 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004045 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004046 if (err == 0) {
4047 struct nfs_client *clp = server->nfs_client;
4048
4049 spin_lock(&clp->cl_lock);
4050 clp->cl_lease_time = fsinfo->lease_time * HZ;
4051 clp->cl_last_renewal = now;
4052 spin_unlock(&clp->cl_lock);
4053 break;
4054 }
4055 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 } while (exception.retry);
4057 return err;
4058}
4059
4060static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4061{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004062 int error;
4063
Trond Myklebust0e574af2005-10-27 22:12:38 -04004064 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004065 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004066 if (error == 0) {
4067 /* block layout checks this! */
4068 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004069 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004070 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004071
4072 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073}
4074
4075static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4076 struct nfs_pathconf *pathconf)
4077{
4078 struct nfs4_pathconf_arg args = {
4079 .fh = fhandle,
4080 .bitmask = server->attr_bitmask,
4081 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004082 struct nfs4_pathconf_res res = {
4083 .pathconf = pathconf,
4084 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 struct rpc_message msg = {
4086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4087 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004088 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 };
4090
4091 /* None of the pathconf attributes are mandatory to implement */
4092 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4093 memset(pathconf, 0, sizeof(*pathconf));
4094 return 0;
4095 }
4096
Trond Myklebust0e574af2005-10-27 22:12:38 -04004097 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004098 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099}
4100
4101static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4102 struct nfs_pathconf *pathconf)
4103{
4104 struct nfs4_exception exception = { };
4105 int err;
4106
4107 do {
4108 err = nfs4_handle_exception(server,
4109 _nfs4_proc_pathconf(server, fhandle, pathconf),
4110 &exception);
4111 } while (exception.retry);
4112 return err;
4113}
4114
Trond Myklebust5521abf2013-03-16 20:54:34 -04004115int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004116 const struct nfs_open_context *ctx,
4117 const struct nfs_lock_context *l_ctx,
4118 fmode_t fmode)
4119{
4120 const struct nfs_lockowner *lockowner = NULL;
4121
4122 if (l_ctx != NULL)
4123 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004124 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004125}
4126EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4127
Trond Myklebust5521abf2013-03-16 20:54:34 -04004128static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4129 const struct nfs_open_context *ctx,
4130 const struct nfs_lock_context *l_ctx,
4131 fmode_t fmode)
4132{
4133 nfs4_stateid current_stateid;
4134
Trond Myklebuste1253be2014-03-05 08:44:23 -05004135 /* If the current stateid represents a lost lock, then exit */
4136 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4137 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004138 return nfs4_stateid_match(stateid, &current_stateid);
4139}
4140
4141static bool nfs4_error_stateid_expired(int err)
4142{
4143 switch (err) {
4144 case -NFS4ERR_DELEG_REVOKED:
4145 case -NFS4ERR_ADMIN_REVOKED:
4146 case -NFS4ERR_BAD_STATEID:
4147 case -NFS4ERR_STALE_STATEID:
4148 case -NFS4ERR_OLD_STATEID:
4149 case -NFS4ERR_OPENMODE:
4150 case -NFS4ERR_EXPIRED:
4151 return true;
4152 }
4153 return false;
4154}
4155
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004156void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004157{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004158 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004159}
4160
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004161static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004163 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004165 trace_nfs4_read(hdr, task->tk_status);
4166 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004167 hdr->args.context->state,
4168 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004169 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004170 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004172
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004173 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004175 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
4178
Trond Myklebust5521abf2013-03-16 20:54:34 -04004179static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004180 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004181{
4182
4183 if (!nfs4_error_stateid_expired(task->tk_status) ||
4184 nfs4_stateid_is_current(&args->stateid,
4185 args->context,
4186 args->lock_context,
4187 FMODE_READ))
4188 return false;
4189 rpc_restart_call_prepare(task);
4190 return true;
4191}
4192
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004193static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004194{
4195
4196 dprintk("--> %s\n", __func__);
4197
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004198 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004199 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004200 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004201 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004202 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4203 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004204}
4205
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004206static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4207 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004209 hdr->timestamp = jiffies;
4210 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004211 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004212 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004215static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4216 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004217{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004218 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4219 &hdr->args.seq_args,
4220 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004221 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004222 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004223 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4224 hdr->args.lock_context,
4225 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004226 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004227 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004228 return -EIO;
4229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230}
4231
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004232static int nfs4_write_done_cb(struct rpc_task *task,
4233 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004235 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004236
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004237 trace_nfs4_write(hdr, task->tk_status);
4238 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004239 hdr->args.context->state,
4240 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004241 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004242 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004244 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004245 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004246 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004247 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
Trond Myklebust5521abf2013-03-16 20:54:34 -04004251static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004252 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004253{
4254
4255 if (!nfs4_error_stateid_expired(task->tk_status) ||
4256 nfs4_stateid_is_current(&args->stateid,
4257 args->context,
4258 args->lock_context,
4259 FMODE_WRITE))
4260 return false;
4261 rpc_restart_call_prepare(task);
4262 return true;
4263}
4264
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004265static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004266{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004267 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004268 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004269 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004270 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004271 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4272 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004273}
4274
Trond Myklebust5a37f852012-04-28 14:55:16 -04004275static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004276bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004277{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004278 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004279 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004280 return false;
4281 /* Otherwise, request attributes if and only if we don't hold
4282 * a delegation
4283 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004284 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004285}
Fred Isamana69aef12011-03-03 15:13:47 +00004286
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004287static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4288 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004290 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004291
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004292 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4293 hdr->args.bitmask = NULL;
4294 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004295 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004296 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004297
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004298 if (!hdr->pgio_done_cb)
4299 hdr->pgio_done_cb = nfs4_write_done_cb;
4300 hdr->res.server = server;
4301 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004303 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004304 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
4306
Fred Isaman0b7c0152012-04-20 14:47:39 -04004307static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4308{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004309 nfs4_setup_sequence(NFS_SERVER(data->inode),
4310 &data->args.seq_args,
4311 &data->res.seq_res,
4312 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313}
4314
Fred Isaman0b7c0152012-04-20 14:47:39 -04004315static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004318
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004319 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004320 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4321 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004322 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004323 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326}
4327
Fred Isaman0b7c0152012-04-20 14:47:39 -04004328static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004329{
4330 if (!nfs4_sequence_done(task, &data->res.seq_res))
4331 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004332 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004333}
4334
Fred Isaman0b7c0152012-04-20 14:47:39 -04004335static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004337 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004338
Fred Isaman0b7c0152012-04-20 14:47:39 -04004339 if (data->commit_done_cb == NULL)
4340 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004341 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004342 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004343 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344}
4345
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004346struct nfs4_renewdata {
4347 struct nfs_client *client;
4348 unsigned long timestamp;
4349};
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351/*
4352 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4353 * standalone procedure for queueing an asynchronous RENEW.
4354 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004355static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004356{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004357 struct nfs4_renewdata *data = calldata;
4358 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004359
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004360 if (atomic_read(&clp->cl_count) > 1)
4361 nfs4_schedule_state_renewal(clp);
4362 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004363 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004364}
4365
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004366static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004368 struct nfs4_renewdata *data = calldata;
4369 struct nfs_client *clp = data->client;
4370 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004372 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004373 switch (task->tk_status) {
4374 case 0:
4375 break;
4376 case -NFS4ERR_LEASE_MOVED:
4377 nfs4_schedule_lease_moved_recovery(clp);
4378 break;
4379 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004380 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004381 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4382 return;
4383 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004384 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004385 return;
4386 }
4387 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004389 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390}
4391
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004392static const struct rpc_call_ops nfs4_renew_ops = {
4393 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004394 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004395};
4396
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004397static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
4399 struct rpc_message msg = {
4400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4401 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004402 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004404 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004406 if (renew_flags == 0)
4407 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004408 if (!atomic_inc_not_zero(&clp->cl_count))
4409 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004410 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004411 if (data == NULL)
4412 return -ENOMEM;
4413 data->client = clp;
4414 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004415 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004416 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004419static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420{
4421 struct rpc_message msg = {
4422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4423 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004424 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 };
4426 unsigned long now = jiffies;
4427 int status;
4428
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004429 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 if (status < 0)
4431 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004432 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 return 0;
4434}
4435
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004436static inline int nfs4_server_supports_acls(struct nfs_server *server)
4437{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004438 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004439}
4440
Trond Myklebust21f498c2012-08-24 10:59:25 -04004441/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4442 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004443 * the stack.
4444 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004445#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004446
Neil Hormane9e3d722011-03-04 19:26:03 -05004447static int buf_to_pages_noslab(const void *buf, size_t buflen,
4448 struct page **pages, unsigned int *pgbase)
4449{
4450 struct page *newpage, **spages;
4451 int rc = 0;
4452 size_t len;
4453 spages = pages;
4454
4455 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004456 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004457 newpage = alloc_page(GFP_KERNEL);
4458
4459 if (newpage == NULL)
4460 goto unwind;
4461 memcpy(page_address(newpage), buf, len);
4462 buf += len;
4463 buflen -= len;
4464 *pages++ = newpage;
4465 rc++;
4466 } while (buflen != 0);
4467
4468 return rc;
4469
4470unwind:
4471 for(; rc > 0; rc--)
4472 __free_page(spages[rc-1]);
4473 return -ENOMEM;
4474}
4475
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004476struct nfs4_cached_acl {
4477 int cached;
4478 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004479 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004480};
4481
4482static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004483{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004484 struct nfs_inode *nfsi = NFS_I(inode);
4485
4486 spin_lock(&inode->i_lock);
4487 kfree(nfsi->nfs4_acl);
4488 nfsi->nfs4_acl = acl;
4489 spin_unlock(&inode->i_lock);
4490}
4491
4492static void nfs4_zap_acl_attr(struct inode *inode)
4493{
4494 nfs4_set_cached_acl(inode, NULL);
4495}
4496
4497static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4498{
4499 struct nfs_inode *nfsi = NFS_I(inode);
4500 struct nfs4_cached_acl *acl;
4501 int ret = -ENOENT;
4502
4503 spin_lock(&inode->i_lock);
4504 acl = nfsi->nfs4_acl;
4505 if (acl == NULL)
4506 goto out;
4507 if (buf == NULL) /* user is just asking for length */
4508 goto out_len;
4509 if (acl->cached == 0)
4510 goto out;
4511 ret = -ERANGE; /* see getxattr(2) man page */
4512 if (acl->len > buflen)
4513 goto out;
4514 memcpy(buf, acl->data, acl->len);
4515out_len:
4516 ret = acl->len;
4517out:
4518 spin_unlock(&inode->i_lock);
4519 return ret;
4520}
4521
Sachin Prabhu5794d212012-04-17 14:36:40 +01004522static 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 +00004523{
4524 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004525 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004526
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004527 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004528 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004529 if (acl == NULL)
4530 goto out;
4531 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004532 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004533 } else {
4534 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4535 if (acl == NULL)
4536 goto out;
4537 acl->cached = 0;
4538 }
4539 acl->len = acl_len;
4540out:
4541 nfs4_set_cached_acl(inode, acl);
4542}
4543
Andy Adamsonbf118a32011-12-07 11:55:27 -05004544/*
4545 * The getxattr API returns the required buffer length when called with a
4546 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4547 * the required buf. On a NULL buf, we send a page of data to the server
4548 * guessing that the ACL request can be serviced by a page. If so, we cache
4549 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4550 * the cache. If not so, we throw away the page, and cache the required
4551 * length. The next getxattr call will then produce another round trip to
4552 * the server, this time with the input buf of the required size.
4553 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004554static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004555{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004556 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004557 struct nfs_getaclargs args = {
4558 .fh = NFS_FH(inode),
4559 .acl_pages = pages,
4560 .acl_len = buflen,
4561 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004562 struct nfs_getaclres res = {
4563 .acl_len = buflen,
4564 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004565 struct rpc_message msg = {
4566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4567 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004568 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004569 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004570 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4571 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004572
Andy Adamsonbf118a32011-12-07 11:55:27 -05004573 /* As long as we're doing a round trip to the server anyway,
4574 * let's be prepared for a page of acl data. */
4575 if (npages == 0)
4576 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004577 if (npages > ARRAY_SIZE(pages))
4578 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004579
Andy Adamsonbf118a32011-12-07 11:55:27 -05004580 for (i = 0; i < npages; i++) {
4581 pages[i] = alloc_page(GFP_KERNEL);
4582 if (!pages[i])
4583 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004584 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004585
4586 /* for decoding across pages */
4587 res.acl_scratch = alloc_page(GFP_KERNEL);
4588 if (!res.acl_scratch)
4589 goto out_free;
4590
Andy Adamsonbf118a32011-12-07 11:55:27 -05004591 args.acl_len = npages * PAGE_SIZE;
4592 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004593
Peng Taode040be2012-01-10 22:42:47 +08004594 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004595 __func__, buf, buflen, npages, args.acl_len);
4596 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4597 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004598 if (ret)
4599 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004600
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004601 /* Handle the case where the passed-in buffer is too short */
4602 if (res.acl_flags & NFS4_ACL_TRUNC) {
4603 /* Did the user only issue a request for the acl length? */
4604 if (buf == NULL)
4605 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004606 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004607 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004608 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004609 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004610 if (buf) {
4611 if (res.acl_len > buflen) {
4612 ret = -ERANGE;
4613 goto out_free;
4614 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004615 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004616 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004617out_ok:
4618 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004619out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004620 for (i = 0; i < npages; i++)
4621 if (pages[i])
4622 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004623 if (res.acl_scratch)
4624 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004625 return ret;
4626}
4627
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004628static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4629{
4630 struct nfs4_exception exception = { };
4631 ssize_t ret;
4632 do {
4633 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004634 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004635 if (ret >= 0)
4636 break;
4637 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4638 } while (exception.retry);
4639 return ret;
4640}
4641
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004642static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4643{
4644 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004645 int ret;
4646
4647 if (!nfs4_server_supports_acls(server))
4648 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004649 ret = nfs_revalidate_inode(server, inode);
4650 if (ret < 0)
4651 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004652 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4653 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004654 ret = nfs4_read_cached_acl(inode, buf, buflen);
4655 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004656 /* -ENOENT is returned if there is no ACL or if there is an ACL
4657 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004658 return ret;
4659 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004660}
4661
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004662static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004663{
4664 struct nfs_server *server = NFS_SERVER(inode);
4665 struct page *pages[NFS4ACL_MAXPAGES];
4666 struct nfs_setaclargs arg = {
4667 .fh = NFS_FH(inode),
4668 .acl_pages = pages,
4669 .acl_len = buflen,
4670 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004671 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004672 struct rpc_message msg = {
4673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4674 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004675 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004676 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004677 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004678 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004679
4680 if (!nfs4_server_supports_acls(server))
4681 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004682 if (npages > ARRAY_SIZE(pages))
4683 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004684 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4685 if (i < 0)
4686 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004687 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004688 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004689
4690 /*
4691 * Free each page after tx, so the only ref left is
4692 * held by the network stack
4693 */
4694 for (; i > 0; i--)
4695 put_page(pages[i-1]);
4696
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004697 /*
4698 * Acl update can result in inode attribute update.
4699 * so mark the attribute cache invalid.
4700 */
4701 spin_lock(&inode->i_lock);
4702 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4703 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004704 nfs_access_zap_cache(inode);
4705 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004706 return ret;
4707}
4708
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004709static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4710{
4711 struct nfs4_exception exception = { };
4712 int err;
4713 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004714 err = __nfs4_proc_set_acl(inode, buf, buflen);
4715 trace_nfs4_set_acl(inode, err);
4716 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004717 &exception);
4718 } while (exception.retry);
4719 return err;
4720}
4721
David Quigleyaa9c2662013-05-22 12:50:44 -04004722#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4723static int _nfs4_get_security_label(struct inode *inode, void *buf,
4724 size_t buflen)
4725{
4726 struct nfs_server *server = NFS_SERVER(inode);
4727 struct nfs_fattr fattr;
4728 struct nfs4_label label = {0, 0, buflen, buf};
4729
4730 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004731 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004732 .fh = NFS_FH(inode),
4733 .bitmask = bitmask,
4734 };
4735 struct nfs4_getattr_res res = {
4736 .fattr = &fattr,
4737 .label = &label,
4738 .server = server,
4739 };
4740 struct rpc_message msg = {
4741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004742 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004743 .rpc_resp = &res,
4744 };
4745 int ret;
4746
4747 nfs_fattr_init(&fattr);
4748
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004749 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004750 if (ret)
4751 return ret;
4752 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4753 return -ENOENT;
4754 if (buflen < label.len)
4755 return -ERANGE;
4756 return 0;
4757}
4758
4759static int nfs4_get_security_label(struct inode *inode, void *buf,
4760 size_t buflen)
4761{
4762 struct nfs4_exception exception = { };
4763 int err;
4764
4765 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4766 return -EOPNOTSUPP;
4767
4768 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004769 err = _nfs4_get_security_label(inode, buf, buflen);
4770 trace_nfs4_get_security_label(inode, err);
4771 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004772 &exception);
4773 } while (exception.retry);
4774 return err;
4775}
4776
4777static int _nfs4_do_set_security_label(struct inode *inode,
4778 struct nfs4_label *ilabel,
4779 struct nfs_fattr *fattr,
4780 struct nfs4_label *olabel)
4781{
4782
4783 struct iattr sattr = {0};
4784 struct nfs_server *server = NFS_SERVER(inode);
4785 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004786 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004787 .fh = NFS_FH(inode),
4788 .iap = &sattr,
4789 .server = server,
4790 .bitmask = bitmask,
4791 .label = ilabel,
4792 };
4793 struct nfs_setattrres res = {
4794 .fattr = fattr,
4795 .label = olabel,
4796 .server = server,
4797 };
4798 struct rpc_message msg = {
4799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004800 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004801 .rpc_resp = &res,
4802 };
4803 int status;
4804
Jeff Layton12207f62013-11-01 10:49:32 -04004805 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004806
Jeff Layton12207f62013-11-01 10:49:32 -04004807 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004808 if (status)
4809 dprintk("%s failed: %d\n", __func__, status);
4810
4811 return status;
4812}
4813
4814static int nfs4_do_set_security_label(struct inode *inode,
4815 struct nfs4_label *ilabel,
4816 struct nfs_fattr *fattr,
4817 struct nfs4_label *olabel)
4818{
4819 struct nfs4_exception exception = { };
4820 int err;
4821
4822 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004823 err = _nfs4_do_set_security_label(inode, ilabel,
4824 fattr, olabel);
4825 trace_nfs4_set_security_label(inode, err);
4826 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004827 &exception);
4828 } while (exception.retry);
4829 return err;
4830}
4831
4832static int
4833nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4834{
4835 struct nfs4_label ilabel, *olabel = NULL;
4836 struct nfs_fattr fattr;
4837 struct rpc_cred *cred;
David Howells2b0143b2015-03-17 22:25:59 +00004838 struct inode *inode = d_inode(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004839 int status;
4840
4841 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4842 return -EOPNOTSUPP;
4843
4844 nfs_fattr_init(&fattr);
4845
4846 ilabel.pi = 0;
4847 ilabel.lfs = 0;
4848 ilabel.label = (char *)buf;
4849 ilabel.len = buflen;
4850
4851 cred = rpc_lookup_cred();
4852 if (IS_ERR(cred))
4853 return PTR_ERR(cred);
4854
4855 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4856 if (IS_ERR(olabel)) {
4857 status = -PTR_ERR(olabel);
4858 goto out;
4859 }
4860
4861 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4862 if (status == 0)
4863 nfs_setsecurity(inode, &fattr, olabel);
4864
4865 nfs4_label_free(olabel);
4866out:
4867 put_rpccred(cred);
4868 return status;
4869}
4870#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4871
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004874nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4875 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004877 struct nfs_client *clp = server->nfs_client;
4878
4879 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 return 0;
4881 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004882 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004883 case -NFS4ERR_ADMIN_REVOKED:
4884 case -NFS4ERR_BAD_STATEID:
4885 case -NFS4ERR_OPENMODE:
4886 if (state == NULL)
4887 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004888 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004889 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004890 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004891 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004892 if (state != NULL) {
4893 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004894 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004897 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004898 nfs4_schedule_lease_recovery(clp);
4899 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004900 case -NFS4ERR_MOVED:
4901 if (nfs4_schedule_migration_recovery(server) < 0)
4902 goto recovery_failed;
4903 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004904 case -NFS4ERR_LEASE_MOVED:
4905 nfs4_schedule_lease_moved_recovery(clp);
4906 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004907#if defined(CONFIG_NFS_V4_1)
4908 case -NFS4ERR_BADSESSION:
4909 case -NFS4ERR_BADSLOT:
4910 case -NFS4ERR_BAD_HIGH_SLOT:
4911 case -NFS4ERR_DEADSESSION:
4912 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4913 case -NFS4ERR_SEQ_FALSE_RETRY:
4914 case -NFS4ERR_SEQ_MISORDERED:
4915 dprintk("%s ERROR %d, Reset session\n", __func__,
4916 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004917 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004918 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004919#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004921 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10004922 rpc_delay(task, nfs4_update_delay(timeout));
4923 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05004924 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004926 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004928 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 }
4930 task->tk_status = nfs4_map_errors(task->tk_status);
4931 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004932recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004933 task->tk_status = -EIO;
4934 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004935wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004936 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004937 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4938 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004939 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4940 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004941restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004942 task->tk_status = 0;
4943 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945
Chuck Leverf0920752012-05-21 22:45:41 -04004946static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4947 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004948{
4949 __be32 verf[2];
4950
Chuck Lever2c820d92012-05-21 22:45:33 -04004951 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4952 /* An impossible timestamp guarantees this value
4953 * will never match a generated boot time. */
4954 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004955 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004956 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004957 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004958 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4959 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004960 }
Chuck Levercd937102012-03-02 17:14:31 -05004961 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4962}
4963
Jeff Laytona3192682015-06-09 19:43:59 -04004964static int
4965nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04004966{
Jeff Laytona3192682015-06-09 19:43:59 -04004967 int result;
4968 size_t len;
4969 char *str;
4970 bool retried = false;
Chuck Levere984a552012-09-14 17:24:21 -04004971
Trond Myklebustceb3a162015-01-03 15:16:04 -05004972 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04004973 return 0;
4974retry:
4975 rcu_read_lock();
4976 len = 10 + strlen(clp->cl_ipaddr) + 1 +
4977 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
4978 1 +
4979 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
4980 1;
4981 rcu_read_unlock();
4982
4983 if (len > NFS4_OPAQUE_LIMIT + 1)
4984 return -EINVAL;
4985
4986 /*
4987 * Since this string is allocated at mount time, and held until the
4988 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
4989 * about a memory-reclaim deadlock.
4990 */
4991 str = kmalloc(len, GFP_KERNEL);
4992 if (!str)
4993 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05004994
Chuck Levere984a552012-09-14 17:24:21 -04004995 rcu_read_lock();
Jeff Laytona3192682015-06-09 19:43:59 -04004996 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
4997 clp->cl_ipaddr,
4998 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
4999 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005000 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005001
5002 /* Did something change? */
5003 if (result >= len) {
5004 kfree(str);
5005 if (retried)
5006 return -EINVAL;
5007 retried = true;
5008 goto retry;
5009 }
5010 clp->cl_owner_id = str;
5011 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005012}
5013
Jeff Layton873e3852015-06-09 19:44:00 -04005014static int
5015nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005016{
Jeff Layton873e3852015-06-09 19:44:00 -04005017 int result;
5018 size_t len;
5019 char *str;
5020
5021 len = 10 + 10 + 1 + 10 + 1 +
5022 strlen(nfs4_client_id_uniquifier) + 1 +
5023 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5024
5025 if (len > NFS4_OPAQUE_LIMIT + 1)
5026 return -EINVAL;
5027
5028 /*
5029 * Since this string is allocated at mount time, and held until the
5030 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5031 * about a memory-reclaim deadlock.
5032 */
5033 str = kmalloc(len, GFP_KERNEL);
5034 if (!str)
5035 return -ENOMEM;
5036
5037 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5038 clp->rpc_ops->version, clp->cl_minorversion,
5039 nfs4_client_id_uniquifier,
5040 clp->cl_rpcclient->cl_nodename);
5041 if (result >= len) {
5042 kfree(str);
5043 return -EINVAL;
5044 }
5045 clp->cl_owner_id = str;
5046 return 0;
5047}
5048
5049static int
5050nfs4_init_uniform_client_string(struct nfs_client *clp)
5051{
5052 int result;
5053 size_t len;
5054 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005055
5056 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005057 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005058
5059 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005060 return nfs4_init_uniquifier_client_string(clp);
5061
5062 len = 10 + 10 + 1 + 10 + 1 +
5063 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5064
5065 if (len > NFS4_OPAQUE_LIMIT + 1)
5066 return -EINVAL;
5067
5068 /*
5069 * Since this string is allocated at mount time, and held until the
5070 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5071 * about a memory-reclaim deadlock.
5072 */
5073 str = kmalloc(len, GFP_KERNEL);
5074 if (!str)
5075 return -ENOMEM;
5076
5077 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5078 clp->rpc_ops->version, clp->cl_minorversion,
5079 clp->cl_rpcclient->cl_nodename);
5080 if (result >= len) {
5081 kfree(str);
5082 return -EINVAL;
5083 }
5084 clp->cl_owner_id = str;
5085 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005086}
5087
Chuck Lever706cb8d2014-03-12 12:51:47 -04005088/*
5089 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5090 * services. Advertise one based on the address family of the
5091 * clientaddr.
5092 */
5093static unsigned int
5094nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5095{
5096 if (strchr(clp->cl_ipaddr, ':') != NULL)
5097 return scnprintf(buf, len, "tcp6");
5098 else
5099 return scnprintf(buf, len, "tcp");
5100}
5101
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005102static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5103{
5104 struct nfs4_setclientid *sc = calldata;
5105
5106 if (task->tk_status == 0)
5107 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5108}
5109
5110static const struct rpc_call_ops nfs4_setclientid_ops = {
5111 .rpc_call_done = nfs4_setclientid_done,
5112};
5113
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005114/**
5115 * nfs4_proc_setclientid - Negotiate client ID
5116 * @clp: state data structure
5117 * @program: RPC program for NFSv4 callback service
5118 * @port: IP port number for NFS4 callback service
5119 * @cred: RPC credential to use for this call
5120 * @res: where to place the result
5121 *
5122 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5123 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005124int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5125 unsigned short port, struct rpc_cred *cred,
5126 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127{
5128 nfs4_verifier sc_verifier;
5129 struct nfs4_setclientid setclientid = {
5130 .sc_verifier = &sc_verifier,
5131 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005132 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 };
5134 struct rpc_message msg = {
5135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5136 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005137 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005138 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005140 struct rpc_task *task;
5141 struct rpc_task_setup task_setup_data = {
5142 .rpc_client = clp->cl_rpcclient,
5143 .rpc_message = &msg,
5144 .callback_ops = &nfs4_setclientid_ops,
5145 .callback_data = &setclientid,
5146 .flags = RPC_TASK_TIMEOUT,
5147 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005148 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
Chuck Leverde734832012-07-11 16:30:50 -04005150 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005151 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005152
5153 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5154 status = nfs4_init_uniform_client_string(clp);
5155 else
Jeff Laytona3192682015-06-09 19:43:59 -04005156 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005157
5158 if (status)
5159 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005160
Chuck Leverde734832012-07-11 16:30:50 -04005161 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005162 setclientid.sc_netid_len =
5163 nfs4_init_callback_netid(clp,
5164 setclientid.sc_netid,
5165 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005166 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005167 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 clp->cl_ipaddr, port >> 8, port & 255);
5169
Jeff Layton3a6bb732015-06-09 19:43:57 -04005170 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005171 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005172 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005173 task = rpc_run_task(&task_setup_data);
5174 if (IS_ERR(task)) {
5175 status = PTR_ERR(task);
5176 goto out;
5177 }
5178 status = task->tk_status;
5179 if (setclientid.sc_cred) {
5180 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5181 put_rpccred(setclientid.sc_cred);
5182 }
5183 rpc_put_task(task);
5184out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005185 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005186 dprintk("NFS reply setclientid: %d\n", status);
5187 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188}
5189
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005190/**
5191 * nfs4_proc_setclientid_confirm - Confirm client ID
5192 * @clp: state data structure
5193 * @res: result of a previous SETCLIENTID
5194 * @cred: RPC credential to use for this call
5195 *
5196 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5197 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005198int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005199 struct nfs4_setclientid_res *arg,
5200 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 struct rpc_message msg = {
5203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005204 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005205 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 int status;
5208
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005209 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5210 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5211 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005212 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005213 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005214 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 return status;
5216}
5217
Trond Myklebustfe650402006-01-03 09:55:18 +01005218struct nfs4_delegreturndata {
5219 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005220 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005221 struct nfs_fh fh;
5222 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005223 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005224 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005225 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005226 struct inode *inode;
5227 bool roc;
5228 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005229};
5230
Trond Myklebustfe650402006-01-03 09:55:18 +01005231static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5232{
5233 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005234
Trond Myklebust14516c32010-07-31 14:29:06 -04005235 if (!nfs4_sequence_done(task, &data->res.seq_res))
5236 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005237
Trond Myklebustca8acf82013-08-13 10:36:56 -04005238 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005239 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005240 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005241 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005242 case -NFS4ERR_ADMIN_REVOKED:
5243 case -NFS4ERR_DELEG_REVOKED:
5244 case -NFS4ERR_BAD_STATEID:
5245 case -NFS4ERR_OLD_STATEID:
5246 case -NFS4ERR_STALE_STATEID:
5247 case -NFS4ERR_EXPIRED:
5248 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005249 if (data->roc)
5250 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005251 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005252 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005253 if (nfs4_async_handle_error(task, data->res.server,
5254 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005255 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005256 return;
5257 }
5258 }
5259 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005260}
5261
5262static void nfs4_delegreturn_release(void *calldata)
5263{
Peng Tao039b7562014-07-03 13:05:02 +08005264 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005265 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005266
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005267 if (inode) {
5268 if (data->roc)
5269 pnfs_roc_release(inode);
5270 nfs_iput_and_deactive(inode);
5271 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005272 kfree(calldata);
5273}
5274
Andy Adamson938e1012009-04-01 09:22:28 -04005275static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5276{
5277 struct nfs4_delegreturndata *d_data;
5278
5279 d_data = (struct nfs4_delegreturndata *)data;
5280
Peng Tao039b7562014-07-03 13:05:02 +08005281 if (d_data->roc &&
5282 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5283 return;
5284
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005285 nfs4_setup_sequence(d_data->res.server,
5286 &d_data->args.seq_args,
5287 &d_data->res.seq_res,
5288 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005289}
Andy Adamson938e1012009-04-01 09:22:28 -04005290
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005291static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005292 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005293 .rpc_call_done = nfs4_delegreturn_done,
5294 .rpc_release = nfs4_delegreturn_release,
5295};
5296
Trond Myklebuste6f81072008-01-24 18:14:34 -05005297static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005298{
5299 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005300 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005301 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005302 struct rpc_message msg = {
5303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5304 .rpc_cred = cred,
5305 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005306 struct rpc_task_setup task_setup_data = {
5307 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005308 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005309 .callback_ops = &nfs4_delegreturn_ops,
5310 .flags = RPC_TASK_ASYNC,
5311 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005312 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005313
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005314 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005315 if (data == NULL)
5316 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005317 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005318 data->args.fhandle = &data->fh;
5319 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005320 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005321 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005322 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005323 data->res.fattr = &data->fattr;
5324 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005325 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005326 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005327 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005328 data->inode = nfs_igrab_and_active(inode);
5329 if (data->inode)
5330 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005331
Trond Myklebustc970aa82007-07-14 15:39:59 -04005332 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005333 msg.rpc_argp = &data->args;
5334 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005335 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005336 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005337 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005338 if (!issync)
5339 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005340 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005341 if (status != 0)
5342 goto out;
5343 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005344 if (status == 0)
5345 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5346 else
5347 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005348out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005349 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005350 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351}
5352
Trond Myklebuste6f81072008-01-24 18:14:34 -05005353int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354{
5355 struct nfs_server *server = NFS_SERVER(inode);
5356 struct nfs4_exception exception = { };
5357 int err;
5358 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005359 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005360 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 switch (err) {
5362 case -NFS4ERR_STALE_STATEID:
5363 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 case 0:
5365 return 0;
5366 }
5367 err = nfs4_handle_exception(server, err, &exception);
5368 } while (exception.retry);
5369 return err;
5370}
5371
5372#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5373#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5374
5375/*
5376 * sleep, with exponential backoff, and retry the LOCK operation.
5377 */
5378static unsigned long
5379nfs4_set_lock_task_retry(unsigned long timeout)
5380{
Colin Cross416ad3c2013-05-06 23:50:06 +00005381 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 timeout <<= 1;
5383 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5384 return NFS4_LOCK_MAXTIMEOUT;
5385 return timeout;
5386}
5387
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5389{
5390 struct inode *inode = state->inode;
5391 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005392 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005393 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005395 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005397 struct nfs_lockt_res res = {
5398 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 };
5400 struct rpc_message msg = {
5401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5402 .rpc_argp = &arg,
5403 .rpc_resp = &res,
5404 .rpc_cred = state->owner->so_cred,
5405 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 struct nfs4_lock_state *lsp;
5407 int status;
5408
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005409 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005410 status = nfs4_set_lock_state(state, request);
5411 if (status != 0)
5412 goto out;
5413 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005414 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005415 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005416 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005417 switch (status) {
5418 case 0:
5419 request->fl_type = F_UNLCK;
5420 break;
5421 case -NFS4ERR_DENIED:
5422 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005424 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005425 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005426out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 return status;
5428}
5429
5430static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5431{
5432 struct nfs4_exception exception = { };
5433 int err;
5434
5435 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005436 err = _nfs4_proc_getlk(state, cmd, request);
5437 trace_nfs4_get_lock(request, state, cmd, err);
5438 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 &exception);
5440 } while (exception.retry);
5441 return err;
5442}
5443
5444static int do_vfs_lock(struct file *file, struct file_lock *fl)
5445{
5446 int res = 0;
5447 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5448 case FL_POSIX:
5449 res = posix_lock_file_wait(file, fl);
5450 break;
5451 case FL_FLOCK:
5452 res = flock_lock_file_wait(file, fl);
5453 break;
5454 default:
5455 BUG();
5456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 return res;
5458}
5459
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005460struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005461 struct nfs_locku_args arg;
5462 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005463 struct nfs4_lock_state *lsp;
5464 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005465 struct file_lock fl;
5466 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005467 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005468};
5469
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005470static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5471 struct nfs_open_context *ctx,
5472 struct nfs4_lock_state *lsp,
5473 struct nfs_seqid *seqid)
5474{
5475 struct nfs4_unlockdata *p;
5476 struct inode *inode = lsp->ls_state->inode;
5477
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005478 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005479 if (p == NULL)
5480 return NULL;
5481 p->arg.fh = NFS_FH(inode);
5482 p->arg.fl = &p->fl;
5483 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005484 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005485 p->lsp = lsp;
5486 atomic_inc(&lsp->ls_count);
5487 /* Ensure we don't close file until we're done freeing locks! */
5488 p->ctx = get_nfs_open_context(ctx);
Jeff Laytondb2efec2015-06-30 14:12:30 -04005489 get_file(fl->fl_file);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005490 memcpy(&p->fl, fl, sizeof(p->fl));
5491 p->server = NFS_SERVER(inode);
5492 return p;
5493}
5494
Trond Myklebust06f814a2006-01-03 09:55:07 +01005495static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005496{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005497 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005498 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005499 nfs4_put_lock_state(calldata->lsp);
5500 put_nfs_open_context(calldata->ctx);
Jeff Laytondb2efec2015-06-30 14:12:30 -04005501 fput(calldata->fl.fl_file);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005502 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005503}
5504
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005505static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005506{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005507 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005508
Trond Myklebust14516c32010-07-31 14:29:06 -04005509 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5510 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005511 switch (task->tk_status) {
5512 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005513 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005514 do_vfs_lock(calldata->fl.fl_file, &calldata->fl);
5515 if (nfs4_update_lock_stateid(calldata->lsp,
5516 &calldata->res.stateid))
5517 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005518 case -NFS4ERR_BAD_STATEID:
5519 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005520 case -NFS4ERR_STALE_STATEID:
5521 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005522 if (!nfs4_stateid_match(&calldata->arg.stateid,
5523 &calldata->lsp->ls_stateid))
5524 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005525 break;
5526 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005527 if (nfs4_async_handle_error(task, calldata->server,
5528 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005529 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005530 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005531 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005532}
5533
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005534static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005535{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005536 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005538 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005539 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005540 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005541 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005542 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005543 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005544 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005545 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005546 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005547 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005548 &calldata->res.seq_res,
5549 task) != 0)
5550 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005551 return;
5552out_no_action:
5553 task->tk_action = NULL;
5554out_wait:
5555 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556}
5557
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005558static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005559 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005560 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005561 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005562};
5563
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005564static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5565 struct nfs_open_context *ctx,
5566 struct nfs4_lock_state *lsp,
5567 struct nfs_seqid *seqid)
5568{
5569 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005570 struct rpc_message msg = {
5571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5572 .rpc_cred = ctx->cred,
5573 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005574 struct rpc_task_setup task_setup_data = {
5575 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005576 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005577 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005578 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005579 .flags = RPC_TASK_ASYNC,
5580 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005581
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005582 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5583 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5584
Frank Filz137d6ac2007-07-09 15:32:29 -07005585 /* Ensure this is an unlock - when canceling a lock, the
5586 * canceled lock is passed in, and it won't be an unlock.
5587 */
5588 fl->fl_type = F_UNLCK;
5589
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005590 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5591 if (data == NULL) {
5592 nfs_free_seqid(seqid);
5593 return ERR_PTR(-ENOMEM);
5594 }
5595
Chuck Levera9c92d62013-08-09 12:48:18 -04005596 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005597 msg.rpc_argp = &data->arg;
5598 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005599 task_setup_data.callback_data = data;
5600 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005601}
5602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5604{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005605 struct inode *inode = state->inode;
5606 struct nfs4_state_owner *sp = state->owner;
5607 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005608 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005609 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005610 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005611 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005612 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005613 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
Trond Myklebust9b073572006-06-29 16:38:34 -04005615 status = nfs4_set_lock_state(state, request);
5616 /* Unlock _before_ we do the RPC call */
5617 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005618 /* Exclude nfs_delegation_claim_locks() */
5619 mutex_lock(&sp->so_delegreturn_mutex);
5620 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005621 down_read(&nfsi->rwsem);
5622 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
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 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005626 }
5627 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005628 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005629 if (status != 0)
5630 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005631 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005632 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005633 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5634 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005635 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5636 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005637 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005638 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005639 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005640 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005641 status = PTR_ERR(task);
5642 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005643 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005644 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005645 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005646out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005647 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005648 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005649 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650}
5651
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005652struct nfs4_lockdata {
5653 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005654 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005655 struct nfs4_lock_state *lsp;
5656 struct nfs_open_context *ctx;
5657 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005658 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005659 int rpc_status;
5660 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005661 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005662};
5663
5664static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005665 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5666 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005667{
5668 struct nfs4_lockdata *p;
5669 struct inode *inode = lsp->ls_state->inode;
5670 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005671 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005672
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005673 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005674 if (p == NULL)
5675 return NULL;
5676
5677 p->arg.fh = NFS_FH(inode);
5678 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005679 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005680 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005681 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005682 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5683 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005684 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005685 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005686 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005687 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005688 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005689 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005690 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005691 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005692 atomic_inc(&lsp->ls_count);
5693 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005694 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005695 memcpy(&p->fl, fl, sizeof(p->fl));
5696 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005697out_free_seqid:
5698 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005699out_free:
5700 kfree(p);
5701 return NULL;
5702}
5703
5704static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5705{
5706 struct nfs4_lockdata *data = calldata;
5707 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
Harvey Harrison3110ff82008-05-02 13:42:44 -07005709 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005710 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005711 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005712 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005713 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005714 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005715 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005716 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005717 nfs4_stateid_copy(&data->arg.open_stateid,
5718 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005719 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005720 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005721 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005722 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005723 nfs4_stateid_copy(&data->arg.lock_stateid,
5724 &data->lsp->ls_stateid);
5725 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005726 if (!nfs4_valid_open_stateid(state)) {
5727 data->rpc_status = -EBADF;
5728 task->tk_action = NULL;
5729 goto out_release_open_seqid;
5730 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005731 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005732 if (nfs4_setup_sequence(data->server,
5733 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005734 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005735 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005736 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005737out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005738 nfs_release_seqid(data->arg.open_seqid);
5739out_release_lock_seqid:
5740 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005741out_wait:
5742 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005743 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005744}
5745
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005746static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5747{
5748 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005749 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005750
Harvey Harrison3110ff82008-05-02 13:42:44 -07005751 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005752
Trond Myklebust14516c32010-07-31 14:29:06 -04005753 if (!nfs4_sequence_done(task, &data->res.seq_res))
5754 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005755
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005756 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005757 switch (task->tk_status) {
5758 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005759 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005760 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005761 if (data->arg.new_lock) {
5762 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5763 if (do_vfs_lock(data->fl.fl_file, &data->fl) < 0) {
5764 rpc_restart_call_prepare(task);
5765 break;
5766 }
5767 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005768 if (data->arg.new_lock_owner != 0) {
5769 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5770 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5771 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5772 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5773 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005774 break;
5775 case -NFS4ERR_BAD_STATEID:
5776 case -NFS4ERR_OLD_STATEID:
5777 case -NFS4ERR_STALE_STATEID:
5778 case -NFS4ERR_EXPIRED:
5779 if (data->arg.new_lock_owner != 0) {
5780 if (!nfs4_stateid_match(&data->arg.open_stateid,
5781 &lsp->ls_state->open_stateid))
5782 rpc_restart_call_prepare(task);
5783 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5784 &lsp->ls_stateid))
5785 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005786 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005787 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005788}
5789
5790static void nfs4_lock_release(void *calldata)
5791{
5792 struct nfs4_lockdata *data = calldata;
5793
Harvey Harrison3110ff82008-05-02 13:42:44 -07005794 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005795 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005796 if (data->cancelled != 0) {
5797 struct rpc_task *task;
5798 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5799 data->arg.lock_seqid);
5800 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005801 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005802 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005803 } else
5804 nfs_free_seqid(data->arg.lock_seqid);
5805 nfs4_put_lock_state(data->lsp);
5806 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005807 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005808 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005809 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005810}
5811
5812static const struct rpc_call_ops nfs4_lock_ops = {
5813 .rpc_call_prepare = nfs4_lock_prepare,
5814 .rpc_call_done = nfs4_lock_done,
5815 .rpc_release = nfs4_lock_release,
5816};
5817
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005818static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5819{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005820 switch (error) {
5821 case -NFS4ERR_ADMIN_REVOKED:
5822 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005823 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005824 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005825 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005826 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005827 break;
5828 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005829 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005830 case -NFS4ERR_EXPIRED:
5831 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005832 };
5833}
5834
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005835static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005836{
5837 struct nfs4_lockdata *data;
5838 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005839 struct rpc_message msg = {
5840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5841 .rpc_cred = state->owner->so_cred,
5842 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005843 struct rpc_task_setup task_setup_data = {
5844 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005845 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005846 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005847 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005848 .flags = RPC_TASK_ASYNC,
5849 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005850 int ret;
5851
Harvey Harrison3110ff82008-05-02 13:42:44 -07005852 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005853 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005854 fl->fl_u.nfs4_fl.owner,
5855 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005856 if (data == NULL)
5857 return -ENOMEM;
5858 if (IS_SETLKW(cmd))
5859 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005860 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005861 msg.rpc_argp = &data->arg;
5862 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005863 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005864 if (recovery_type > NFS_LOCK_NEW) {
5865 if (recovery_type == NFS_LOCK_RECLAIM)
5866 data->arg.reclaim = NFS_LOCK_RECLAIM;
5867 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005868 } else
5869 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005870 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005871 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005872 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005873 ret = nfs4_wait_for_completion_rpc_task(task);
5874 if (ret == 0) {
5875 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005876 if (ret)
5877 nfs4_handle_setlk_error(data->server, data->lsp,
5878 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005879 } else
5880 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005881 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005882 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884}
5885
5886static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5887{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005888 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005889 struct nfs4_exception exception = {
5890 .inode = state->inode,
5891 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005892 int err;
5893
5894 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005895 /* Cache the lock if possible... */
5896 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5897 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005898 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005899 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005900 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005901 break;
5902 nfs4_handle_exception(server, err, &exception);
5903 } while (exception.retry);
5904 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905}
5906
5907static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5908{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005909 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005910 struct nfs4_exception exception = {
5911 .inode = state->inode,
5912 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005913 int err;
5914
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005915 err = nfs4_set_lock_state(state, request);
5916 if (err != 0)
5917 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005918 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005919 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5920 return 0;
5921 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005922 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005923 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5924 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005925 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005926 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005927 switch (err) {
5928 default:
5929 goto out;
5930 case -NFS4ERR_GRACE:
5931 case -NFS4ERR_DELAY:
5932 nfs4_handle_exception(server, err, &exception);
5933 err = 0;
5934 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005935 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005936out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005937 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938}
5939
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005940#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005941/**
5942 * nfs41_check_expired_locks - possibly free a lock stateid
5943 *
5944 * @state: NFSv4 state for an inode
5945 *
5946 * Returns NFS_OK if recovery for this stateid is now finished.
5947 * Otherwise a negative NFS4ERR value is returned.
5948 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005949static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005950{
Chuck Levereb64cf92012-07-11 16:30:05 -04005951 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005952 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005953 struct nfs_server *server = NFS_SERVER(state->inode);
5954
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005955 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005956 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005957 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5958
5959 status = nfs41_test_stateid(server,
5960 &lsp->ls_stateid,
5961 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005962 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005963 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005964 /* Free the stateid unless the server
5965 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005966 if (status != -NFS4ERR_BAD_STATEID)
5967 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005968 &lsp->ls_stateid,
5969 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005970 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005971 ret = status;
5972 }
5973 }
5974 };
5975
5976 return ret;
5977}
5978
5979static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5980{
5981 int status = NFS_OK;
5982
5983 if (test_bit(LK_STATE_IN_USE, &state->flags))
5984 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005985 if (status != NFS_OK)
5986 status = nfs4_lock_expired(state, request);
5987 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005988}
5989#endif
5990
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5992{
Trond Myklebust19e03c52008-12-23 15:21:44 -05005993 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005994 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005995 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005997 if ((fl_flags & FL_POSIX) &&
5998 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5999 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006000 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006001 status = nfs4_set_lock_state(state, request);
6002 if (status != 0)
6003 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006004 request->fl_flags |= FL_ACCESS;
6005 status = do_vfs_lock(request->fl_file, request);
6006 if (status < 0)
6007 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006008 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006009 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006010 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006011 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006012 request->fl_flags = fl_flags & ~FL_SLEEP;
6013 status = do_vfs_lock(request->fl_file, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006014 up_read(&nfsi->rwsem);
6015 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006016 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006017 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006018 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006019out:
6020 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 return status;
6022}
6023
6024static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6025{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006026 struct nfs4_exception exception = {
6027 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006028 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006029 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 int err;
6031
6032 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006033 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006034 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07006035 if (err == -NFS4ERR_DENIED)
6036 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006038 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 } while (exception.retry);
6040 return err;
6041}
6042
6043static int
6044nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6045{
6046 struct nfs_open_context *ctx;
6047 struct nfs4_state *state;
6048 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6049 int status;
6050
6051 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006052 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 state = ctx->state;
6054
6055 if (request->fl_start < 0 || request->fl_end < 0)
6056 return -EINVAL;
6057
Trond Myklebustd9531262009-07-21 19:22:38 -04006058 if (IS_GETLK(cmd)) {
6059 if (state != NULL)
6060 return nfs4_proc_getlk(state, F_GETLK, request);
6061 return 0;
6062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
6064 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6065 return -EINVAL;
6066
Trond Myklebustd9531262009-07-21 19:22:38 -04006067 if (request->fl_type == F_UNLCK) {
6068 if (state != NULL)
6069 return nfs4_proc_unlck(state, cmd, request);
6070 return 0;
6071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
Trond Myklebustd9531262009-07-21 19:22:38 -04006073 if (state == NULL)
6074 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006075 /*
6076 * Don't rely on the VFS having checked the file open mode,
6077 * since it won't do this for flock() locks.
6078 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006079 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006080 case F_RDLCK:
6081 if (!(filp->f_mode & FMODE_READ))
6082 return -EBADF;
6083 break;
6084 case F_WRLCK:
6085 if (!(filp->f_mode & FMODE_WRITE))
6086 return -EBADF;
6087 }
6088
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 do {
6090 status = nfs4_proc_setlk(state, cmd, request);
6091 if ((status != -EAGAIN) || IS_SETLK(cmd))
6092 break;
6093 timeout = nfs4_set_lock_task_retry(timeout);
6094 status = -ERESTARTSYS;
6095 if (signalled())
6096 break;
6097 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 return status;
6099}
6100
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006101int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006102{
6103 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006104 int err;
6105
6106 err = nfs4_set_lock_state(state, fl);
6107 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006108 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006109 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006110 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006111}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006112
Trond Myklebustcf470c32012-03-07 13:49:12 -05006113struct nfs_release_lockowner_data {
6114 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006115 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006116 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006117 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006118 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006119};
6120
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006121static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6122{
6123 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006124 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006125 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6126 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006127 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006128 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006129}
6130
6131static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6132{
6133 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006134 struct nfs_server *server = data->server;
6135
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006136 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006137
6138 switch (task->tk_status) {
6139 case 0:
6140 renew_lease(server, data->timestamp);
6141 break;
6142 case -NFS4ERR_STALE_CLIENTID:
6143 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006144 nfs4_schedule_lease_recovery(server->nfs_client);
6145 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006146 case -NFS4ERR_LEASE_MOVED:
6147 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006148 if (nfs4_async_handle_error(task, server,
6149 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006150 rpc_restart_call_prepare(task);
6151 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006152}
6153
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006154static void nfs4_release_lockowner_release(void *calldata)
6155{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006156 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006157 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006158 kfree(calldata);
6159}
6160
Trond Myklebust17280172012-03-11 13:11:00 -04006161static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006162 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6163 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006164 .rpc_release = nfs4_release_lockowner_release,
6165};
6166
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006167static void
6168nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006169{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006170 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006171 struct rpc_message msg = {
6172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6173 };
6174
6175 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006176 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006177
Trond Myklebustcf470c32012-03-07 13:49:12 -05006178 data = kmalloc(sizeof(*data), GFP_NOFS);
6179 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006180 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006181 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006182 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006183 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6184 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6185 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006186
Trond Myklebustcf470c32012-03-07 13:49:12 -05006187 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006188 msg.rpc_resp = &data->res;
6189 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006190 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006191}
6192
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006193#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6194
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006195static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6196 const void *buf, size_t buflen,
6197 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006198{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006199 if (strcmp(key, "") != 0)
6200 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006201
David Howells2b0143b2015-03-17 22:25:59 +00006202 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006203}
6204
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006205static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6206 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006207{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006208 if (strcmp(key, "") != 0)
6209 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006210
David Howells2b0143b2015-03-17 22:25:59 +00006211 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006212}
6213
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006214static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6215 size_t list_len, const char *name,
6216 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006217{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006218 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006219
David Howells2b0143b2015-03-17 22:25:59 +00006220 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
J. Bruce Fields096455a2006-03-20 23:23:42 -05006221 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006222
6223 if (list && len <= list_len)
6224 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006225 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006226}
6227
David Quigleyc9bccef2013-05-22 12:50:45 -04006228#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6229static inline int nfs4_server_supports_labels(struct nfs_server *server)
6230{
6231 return server->caps & NFS_CAP_SECURITY_LABEL;
6232}
6233
6234static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6235 const void *buf, size_t buflen,
6236 int flags, int type)
6237{
6238 if (security_ismaclabel(key))
6239 return nfs4_set_security_label(dentry, buf, buflen);
6240
6241 return -EOPNOTSUPP;
6242}
6243
6244static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6245 void *buf, size_t buflen, int type)
6246{
6247 if (security_ismaclabel(key))
David Howells2b0143b2015-03-17 22:25:59 +00006248 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006249 return -EOPNOTSUPP;
6250}
6251
6252static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6253 size_t list_len, const char *name,
6254 size_t name_len, int type)
6255{
6256 size_t len = 0;
6257
David Howells2b0143b2015-03-17 22:25:59 +00006258 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6259 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
David Quigleyc9bccef2013-05-22 12:50:45 -04006260 if (list && len <= list_len)
David Howells2b0143b2015-03-17 22:25:59 +00006261 security_inode_listsecurity(d_inode(dentry), list, len);
David Quigleyc9bccef2013-05-22 12:50:45 -04006262 }
6263 return len;
6264}
6265
6266static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6267 .prefix = XATTR_SECURITY_PREFIX,
6268 .list = nfs4_xattr_list_nfs4_label,
6269 .get = nfs4_xattr_get_nfs4_label,
6270 .set = nfs4_xattr_set_nfs4_label,
6271};
6272#endif
6273
6274
Andy Adamson533eb462011-06-13 18:25:56 -04006275/*
6276 * nfs_fhget will use either the mounted_on_fileid or the fileid
6277 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006278static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6279{
Andy Adamson533eb462011-06-13 18:25:56 -04006280 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6281 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6282 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006283 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006284 return;
6285
6286 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006287 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006288 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6289 fattr->nlink = 2;
6290}
6291
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006292static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6293 const struct qstr *name,
6294 struct nfs4_fs_locations *fs_locations,
6295 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006296{
6297 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006298 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006299 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006300 };
6301 struct nfs4_fs_locations_arg args = {
6302 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006303 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006304 .page = page,
6305 .bitmask = bitmask,
6306 };
Benny Halevy22958462009-04-01 09:22:02 -04006307 struct nfs4_fs_locations_res res = {
6308 .fs_locations = fs_locations,
6309 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006310 struct rpc_message msg = {
6311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6312 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006313 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006314 };
6315 int status;
6316
Harvey Harrison3110ff82008-05-02 13:42:44 -07006317 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006318
6319 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6320 * is not supported */
6321 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6322 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6323 else
6324 bitmask[0] |= FATTR4_WORD0_FILEID;
6325
Trond Myklebustc228fd32007-01-13 02:28:11 -05006326 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006327 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006328 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006329 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006330 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006331 return status;
6332}
6333
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006334int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6335 const struct qstr *name,
6336 struct nfs4_fs_locations *fs_locations,
6337 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006338{
6339 struct nfs4_exception exception = { };
6340 int err;
6341 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006342 err = _nfs4_proc_fs_locations(client, dir, name,
6343 fs_locations, page);
6344 trace_nfs4_get_fs_locations(dir, name, err);
6345 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006346 &exception);
6347 } while (exception.retry);
6348 return err;
6349}
6350
Chuck Leverb03d7352013-10-17 14:12:50 -04006351/*
6352 * This operation also signals the server that this client is
6353 * performing migration recovery. The server can stop returning
6354 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6355 * appended to this compound to identify the client ID which is
6356 * performing recovery.
6357 */
6358static int _nfs40_proc_get_locations(struct inode *inode,
6359 struct nfs4_fs_locations *locations,
6360 struct page *page, struct rpc_cred *cred)
6361{
6362 struct nfs_server *server = NFS_SERVER(inode);
6363 struct rpc_clnt *clnt = server->client;
6364 u32 bitmask[2] = {
6365 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6366 };
6367 struct nfs4_fs_locations_arg args = {
6368 .clientid = server->nfs_client->cl_clientid,
6369 .fh = NFS_FH(inode),
6370 .page = page,
6371 .bitmask = bitmask,
6372 .migration = 1, /* skip LOOKUP */
6373 .renew = 1, /* append RENEW */
6374 };
6375 struct nfs4_fs_locations_res res = {
6376 .fs_locations = locations,
6377 .migration = 1,
6378 .renew = 1,
6379 };
6380 struct rpc_message msg = {
6381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6382 .rpc_argp = &args,
6383 .rpc_resp = &res,
6384 .rpc_cred = cred,
6385 };
6386 unsigned long now = jiffies;
6387 int status;
6388
6389 nfs_fattr_init(&locations->fattr);
6390 locations->server = server;
6391 locations->nlocations = 0;
6392
6393 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6394 nfs4_set_sequence_privileged(&args.seq_args);
6395 status = nfs4_call_sync_sequence(clnt, server, &msg,
6396 &args.seq_args, &res.seq_res);
6397 if (status)
6398 return status;
6399
6400 renew_lease(server, now);
6401 return 0;
6402}
6403
6404#ifdef CONFIG_NFS_V4_1
6405
6406/*
6407 * This operation also signals the server that this client is
6408 * performing migration recovery. The server can stop asserting
6409 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6410 * performing this operation is identified in the SEQUENCE
6411 * operation in this compound.
6412 *
6413 * When the client supports GETATTR(fs_locations_info), it can
6414 * be plumbed in here.
6415 */
6416static int _nfs41_proc_get_locations(struct inode *inode,
6417 struct nfs4_fs_locations *locations,
6418 struct page *page, struct rpc_cred *cred)
6419{
6420 struct nfs_server *server = NFS_SERVER(inode);
6421 struct rpc_clnt *clnt = server->client;
6422 u32 bitmask[2] = {
6423 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6424 };
6425 struct nfs4_fs_locations_arg args = {
6426 .fh = NFS_FH(inode),
6427 .page = page,
6428 .bitmask = bitmask,
6429 .migration = 1, /* skip LOOKUP */
6430 };
6431 struct nfs4_fs_locations_res res = {
6432 .fs_locations = locations,
6433 .migration = 1,
6434 };
6435 struct rpc_message msg = {
6436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6437 .rpc_argp = &args,
6438 .rpc_resp = &res,
6439 .rpc_cred = cred,
6440 };
6441 int status;
6442
6443 nfs_fattr_init(&locations->fattr);
6444 locations->server = server;
6445 locations->nlocations = 0;
6446
6447 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6448 nfs4_set_sequence_privileged(&args.seq_args);
6449 status = nfs4_call_sync_sequence(clnt, server, &msg,
6450 &args.seq_args, &res.seq_res);
6451 if (status == NFS4_OK &&
6452 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6453 status = -NFS4ERR_LEASE_MOVED;
6454 return status;
6455}
6456
6457#endif /* CONFIG_NFS_V4_1 */
6458
6459/**
6460 * nfs4_proc_get_locations - discover locations for a migrated FSID
6461 * @inode: inode on FSID that is migrating
6462 * @locations: result of query
6463 * @page: buffer
6464 * @cred: credential to use for this operation
6465 *
6466 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6467 * operation failed, or a negative errno if a local error occurred.
6468 *
6469 * On success, "locations" is filled in, but if the server has
6470 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6471 * asserted.
6472 *
6473 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6474 * from this client that require migration recovery.
6475 */
6476int nfs4_proc_get_locations(struct inode *inode,
6477 struct nfs4_fs_locations *locations,
6478 struct page *page, struct rpc_cred *cred)
6479{
6480 struct nfs_server *server = NFS_SERVER(inode);
6481 struct nfs_client *clp = server->nfs_client;
6482 const struct nfs4_mig_recovery_ops *ops =
6483 clp->cl_mvops->mig_recovery_ops;
6484 struct nfs4_exception exception = { };
6485 int status;
6486
6487 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6488 (unsigned long long)server->fsid.major,
6489 (unsigned long long)server->fsid.minor,
6490 clp->cl_hostname);
6491 nfs_display_fhandle(NFS_FH(inode), __func__);
6492
6493 do {
6494 status = ops->get_locations(inode, locations, page, cred);
6495 if (status != -NFS4ERR_DELAY)
6496 break;
6497 nfs4_handle_exception(server, status, &exception);
6498 } while (exception.retry);
6499 return status;
6500}
6501
Chuck Lever44c99932013-10-17 14:13:30 -04006502/*
6503 * This operation also signals the server that this client is
6504 * performing "lease moved" recovery. The server can stop
6505 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6506 * is appended to this compound to identify the client ID which is
6507 * performing recovery.
6508 */
6509static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6510{
6511 struct nfs_server *server = NFS_SERVER(inode);
6512 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6513 struct rpc_clnt *clnt = server->client;
6514 struct nfs4_fsid_present_arg args = {
6515 .fh = NFS_FH(inode),
6516 .clientid = clp->cl_clientid,
6517 .renew = 1, /* append RENEW */
6518 };
6519 struct nfs4_fsid_present_res res = {
6520 .renew = 1,
6521 };
6522 struct rpc_message msg = {
6523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6524 .rpc_argp = &args,
6525 .rpc_resp = &res,
6526 .rpc_cred = cred,
6527 };
6528 unsigned long now = jiffies;
6529 int status;
6530
6531 res.fh = nfs_alloc_fhandle();
6532 if (res.fh == NULL)
6533 return -ENOMEM;
6534
6535 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6536 nfs4_set_sequence_privileged(&args.seq_args);
6537 status = nfs4_call_sync_sequence(clnt, server, &msg,
6538 &args.seq_args, &res.seq_res);
6539 nfs_free_fhandle(res.fh);
6540 if (status)
6541 return status;
6542
6543 do_renew_lease(clp, now);
6544 return 0;
6545}
6546
6547#ifdef CONFIG_NFS_V4_1
6548
6549/*
6550 * This operation also signals the server that this client is
6551 * performing "lease moved" recovery. The server can stop asserting
6552 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6553 * this operation is identified in the SEQUENCE operation in this
6554 * compound.
6555 */
6556static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6557{
6558 struct nfs_server *server = NFS_SERVER(inode);
6559 struct rpc_clnt *clnt = server->client;
6560 struct nfs4_fsid_present_arg args = {
6561 .fh = NFS_FH(inode),
6562 };
6563 struct nfs4_fsid_present_res res = {
6564 };
6565 struct rpc_message msg = {
6566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6567 .rpc_argp = &args,
6568 .rpc_resp = &res,
6569 .rpc_cred = cred,
6570 };
6571 int status;
6572
6573 res.fh = nfs_alloc_fhandle();
6574 if (res.fh == NULL)
6575 return -ENOMEM;
6576
6577 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6578 nfs4_set_sequence_privileged(&args.seq_args);
6579 status = nfs4_call_sync_sequence(clnt, server, &msg,
6580 &args.seq_args, &res.seq_res);
6581 nfs_free_fhandle(res.fh);
6582 if (status == NFS4_OK &&
6583 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6584 status = -NFS4ERR_LEASE_MOVED;
6585 return status;
6586}
6587
6588#endif /* CONFIG_NFS_V4_1 */
6589
6590/**
6591 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6592 * @inode: inode on FSID to check
6593 * @cred: credential to use for this operation
6594 *
6595 * Server indicates whether the FSID is present, moved, or not
6596 * recognized. This operation is necessary to clear a LEASE_MOVED
6597 * condition for this client ID.
6598 *
6599 * Returns NFS4_OK if the FSID is present on this server,
6600 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6601 * NFS4ERR code if some error occurred on the server, or a
6602 * negative errno if a local failure occurred.
6603 */
6604int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6605{
6606 struct nfs_server *server = NFS_SERVER(inode);
6607 struct nfs_client *clp = server->nfs_client;
6608 const struct nfs4_mig_recovery_ops *ops =
6609 clp->cl_mvops->mig_recovery_ops;
6610 struct nfs4_exception exception = { };
6611 int status;
6612
6613 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6614 (unsigned long long)server->fsid.major,
6615 (unsigned long long)server->fsid.minor,
6616 clp->cl_hostname);
6617 nfs_display_fhandle(NFS_FH(inode), __func__);
6618
6619 do {
6620 status = ops->fsid_present(inode, cred);
6621 if (status != -NFS4ERR_DELAY)
6622 break;
6623 nfs4_handle_exception(server, status, &exception);
6624 } while (exception.retry);
6625 return status;
6626}
6627
Andy Adamson5ec16a82013-08-08 10:57:55 -04006628/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006629 * If 'use_integrity' is true and the state managment nfs_client
6630 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6631 * and the machine credential as per RFC3530bis and RFC5661 Security
6632 * Considerations sections. Otherwise, just use the user cred with the
6633 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006634 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006635static 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 +00006636{
6637 int status;
6638 struct nfs4_secinfo_arg args = {
6639 .dir_fh = NFS_FH(dir),
6640 .name = name,
6641 };
6642 struct nfs4_secinfo_res res = {
6643 .flavors = flavors,
6644 };
6645 struct rpc_message msg = {
6646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6647 .rpc_argp = &args,
6648 .rpc_resp = &res,
6649 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006650 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006651 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006652
6653 if (use_integrity) {
6654 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006655 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6656 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006657 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006658
6659 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006660
6661 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6662 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6663
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006664 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6665 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006666 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006667
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006668 if (cred)
6669 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006670
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006671 return status;
6672}
6673
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006674int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6675 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006676{
6677 struct nfs4_exception exception = { };
6678 int err;
6679 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006680 err = -NFS4ERR_WRONGSEC;
6681
6682 /* try to use integrity protection with machine cred */
6683 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6684 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6685
6686 /*
6687 * if unable to use integrity protection, or SECINFO with
6688 * integrity protection returns NFS4ERR_WRONGSEC (which is
6689 * disallowed by spec, but exists in deployed servers) use
6690 * the current filesystem's rpc_client and the user cred.
6691 */
6692 if (err == -NFS4ERR_WRONGSEC)
6693 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6694
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006695 trace_nfs4_secinfo(dir, name, err);
6696 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006697 &exception);
6698 } while (exception.retry);
6699 return err;
6700}
6701
Andy Adamson557134a2009-04-01 09:21:53 -04006702#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006703/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006704 * Check the exchange flags returned by the server for invalid flags, having
6705 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6706 * DS flags set.
6707 */
6708static int nfs4_check_cl_exchange_flags(u32 flags)
6709{
6710 if (flags & ~EXCHGID4_FLAG_MASK_R)
6711 goto out_inval;
6712 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6713 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6714 goto out_inval;
6715 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6716 goto out_inval;
6717 return NFS_OK;
6718out_inval:
6719 return -NFS4ERR_INVAL;
6720}
6721
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006722static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006723nfs41_same_server_scope(struct nfs41_server_scope *a,
6724 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006725{
6726 if (a->server_scope_sz == b->server_scope_sz &&
6727 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6728 return true;
6729
6730 return false;
6731}
6732
Andy Adamson357f54d2010-12-14 10:11:57 -05006733/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006734 * nfs4_proc_bind_conn_to_session()
6735 *
6736 * The 4.1 client currently uses the same TCP connection for the
6737 * fore and backchannel.
6738 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006739int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006740{
6741 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006742 struct nfs41_bind_conn_to_session_args args = {
6743 .client = clp,
6744 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6745 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006746 struct nfs41_bind_conn_to_session_res res;
6747 struct rpc_message msg = {
6748 .rpc_proc =
6749 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006750 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006751 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006752 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006753 };
6754
6755 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006756
Trond Myklebust71a097c2015-02-18 09:27:18 -08006757 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6758 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6759 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006760
6761 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006762 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006763 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006764 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006765 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6766 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6767 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006768 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006769 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006770 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006771 dprintk("NFS: %s: Unexpected direction from server\n",
6772 __func__);
6773 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006774 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006775 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006776 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006777 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6778 __func__);
6779 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006780 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006781 }
6782 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006783out:
6784 dprintk("<-- %s status= %d\n", __func__, status);
6785 return status;
6786}
6787
6788/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006789 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6790 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006791 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006792static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6793 .how = SP4_MACH_CRED,
6794 .enforce.u.words = {
6795 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6796 1 << (OP_EXCHANGE_ID - 32) |
6797 1 << (OP_CREATE_SESSION - 32) |
6798 1 << (OP_DESTROY_SESSION - 32) |
6799 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006800 },
6801 .allow.u.words = {
6802 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006803 1 << (OP_LOCKU) |
6804 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006805 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006806 1 << (OP_SECINFO_NO_NAME - 32) |
6807 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006808 1 << (OP_FREE_STATEID - 32) |
6809 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006810 }
6811};
6812
6813/*
6814 * Select the state protection mode for client `clp' given the server results
6815 * from exchange_id in `sp'.
6816 *
6817 * Returns 0 on success, negative errno otherwise.
6818 */
6819static int nfs4_sp4_select_mode(struct nfs_client *clp,
6820 struct nfs41_state_protection *sp)
6821{
6822 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6823 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6824 1 << (OP_EXCHANGE_ID - 32) |
6825 1 << (OP_CREATE_SESSION - 32) |
6826 1 << (OP_DESTROY_SESSION - 32) |
6827 1 << (OP_DESTROY_CLIENTID - 32)
6828 };
6829 unsigned int i;
6830
6831 if (sp->how == SP4_MACH_CRED) {
6832 /* Print state protect result */
6833 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6834 for (i = 0; i <= LAST_NFS4_OP; i++) {
6835 if (test_bit(i, sp->enforce.u.longs))
6836 dfprintk(MOUNT, " enforce op %d\n", i);
6837 if (test_bit(i, sp->allow.u.longs))
6838 dfprintk(MOUNT, " allow op %d\n", i);
6839 }
6840
6841 /* make sure nothing is on enforce list that isn't supported */
6842 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6843 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6844 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6845 return -EINVAL;
6846 }
6847 }
6848
6849 /*
6850 * Minimal mode - state operations are allowed to use machine
6851 * credential. Note this already happens by default, so the
6852 * client doesn't have to do anything more than the negotiation.
6853 *
6854 * NOTE: we don't care if EXCHANGE_ID is in the list -
6855 * we're already using the machine cred for exchange_id
6856 * and will never use a different cred.
6857 */
6858 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6859 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6860 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6861 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6862 dfprintk(MOUNT, "sp4_mach_cred:\n");
6863 dfprintk(MOUNT, " minimal mode enabled\n");
6864 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6865 } else {
6866 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6867 return -EINVAL;
6868 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006869
6870 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6871 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6872 dfprintk(MOUNT, " cleanup mode enabled\n");
6873 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6874 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006875
6876 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6877 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6878 dfprintk(MOUNT, " secinfo mode enabled\n");
6879 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6880 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006881
6882 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6883 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6884 dfprintk(MOUNT, " stateid mode enabled\n");
6885 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6886 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006887
6888 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6889 dfprintk(MOUNT, " write mode enabled\n");
6890 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6891 }
6892
6893 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6894 dfprintk(MOUNT, " commit mode enabled\n");
6895 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6896 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006897 }
6898
6899 return 0;
6900}
6901
6902/*
6903 * _nfs4_proc_exchange_id()
6904 *
6905 * Wrapper for EXCHANGE_ID operation.
6906 */
6907static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6908 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006909{
6910 nfs4_verifier verifier;
6911 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006912 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006913 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006914#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006915 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006916 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6917 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6918#else
6919 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6920 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6921#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006922 };
6923 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006924 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006925 };
6926 int status;
6927 struct rpc_message msg = {
6928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6929 .rpc_argp = &args,
6930 .rpc_resp = &res,
6931 .rpc_cred = cred,
6932 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006933
Chuck Leverf0920752012-05-21 22:45:41 -04006934 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006935
6936 status = nfs4_init_uniform_client_string(clp);
6937 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04006938 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006939
6940 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006941 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006942 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006943
Chuck Leveracdeb692012-05-21 22:46:16 -04006944 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006945 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006946 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006947 status = -ENOMEM;
6948 goto out;
6949 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006950
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006951 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006952 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006953 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006954 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006955 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006956 }
6957
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006958 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006959 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006960 status = -ENOMEM;
6961 goto out_server_scope;
6962 }
6963
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006964 switch (sp4_how) {
6965 case SP4_NONE:
6966 args.state_protect.how = SP4_NONE;
6967 break;
6968
6969 case SP4_MACH_CRED:
6970 args.state_protect = nfs4_sp4_mach_cred_request;
6971 break;
6972
6973 default:
6974 /* unsupported! */
6975 WARN_ON_ONCE(1);
6976 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08006977 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006978 }
6979
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006980 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006981 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006982 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006983 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006984
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006985 if (status == 0)
6986 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6987
Chuck Lever177313f2012-05-21 22:44:58 -04006988 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006989 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05006990 clp->cl_exchange_flags = res.flags;
6991 /* Client ID is not confirmed */
6992 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
6993 clear_bit(NFS4_SESSION_ESTABLISHED,
6994 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04006995 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05006996 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006997
Chuck Leveracdeb692012-05-21 22:46:16 -04006998 kfree(clp->cl_serverowner);
6999 clp->cl_serverowner = res.server_owner;
7000 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007001
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007002 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007003 kfree(clp->cl_implid);
7004 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007005 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007006
Chuck Lever177313f2012-05-21 22:44:58 -04007007 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007008 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007009 res.server_scope)) {
7010 dprintk("%s: server_scope mismatch detected\n",
7011 __func__);
7012 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007013 kfree(clp->cl_serverscope);
7014 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007015 }
7016
Chuck Lever177313f2012-05-21 22:44:58 -04007017 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007018 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007019 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007020 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007021 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007022
Kinglong Mee6b559702015-07-01 11:54:53 +08007023out_impl_id:
7024 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007025out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007026 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007027out_server_owner:
7028 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007029out:
Chuck Lever177313f2012-05-21 22:44:58 -04007030 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007031 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007032 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007033 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007034 clp->cl_implid->date.seconds,
7035 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007036 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007037 return status;
7038}
7039
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007040/*
7041 * nfs4_proc_exchange_id()
7042 *
7043 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7044 *
7045 * Since the clientid has expired, all compounds using sessions
7046 * associated with the stale clientid will be returning
7047 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7048 * be in some phase of session reset.
7049 *
7050 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7051 */
7052int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7053{
7054 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7055 int status;
7056
7057 /* try SP4_MACH_CRED if krb5i/p */
7058 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7059 authflavor == RPC_AUTH_GSS_KRB5P) {
7060 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7061 if (!status)
7062 return 0;
7063 }
7064
7065 /* try SP4_NONE */
7066 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7067}
7068
Trond Myklebust66245532012-05-25 17:18:09 -04007069static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7070 struct rpc_cred *cred)
7071{
7072 struct rpc_message msg = {
7073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7074 .rpc_argp = clp,
7075 .rpc_cred = cred,
7076 };
7077 int status;
7078
7079 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007080 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007081 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007082 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007083 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7084 return status;
7085}
7086
7087static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7088 struct rpc_cred *cred)
7089{
7090 unsigned int loop;
7091 int ret;
7092
7093 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7094 ret = _nfs4_proc_destroy_clientid(clp, cred);
7095 switch (ret) {
7096 case -NFS4ERR_DELAY:
7097 case -NFS4ERR_CLIENTID_BUSY:
7098 ssleep(1);
7099 break;
7100 default:
7101 return ret;
7102 }
7103 }
7104 return 0;
7105}
7106
7107int nfs4_destroy_clientid(struct nfs_client *clp)
7108{
7109 struct rpc_cred *cred;
7110 int ret = 0;
7111
7112 if (clp->cl_mvops->minor_version < 1)
7113 goto out;
7114 if (clp->cl_exchange_flags == 0)
7115 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007116 if (clp->cl_preserve_clid)
7117 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007118 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007119 ret = nfs4_proc_destroy_clientid(clp, cred);
7120 if (cred)
7121 put_rpccred(cred);
7122 switch (ret) {
7123 case 0:
7124 case -NFS4ERR_STALE_CLIENTID:
7125 clp->cl_exchange_flags = 0;
7126 }
7127out:
7128 return ret;
7129}
7130
Andy Adamson2050f0c2009-04-01 09:22:30 -04007131struct nfs4_get_lease_time_data {
7132 struct nfs4_get_lease_time_args *args;
7133 struct nfs4_get_lease_time_res *res;
7134 struct nfs_client *clp;
7135};
7136
7137static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7138 void *calldata)
7139{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007140 struct nfs4_get_lease_time_data *data =
7141 (struct nfs4_get_lease_time_data *)calldata;
7142
7143 dprintk("--> %s\n", __func__);
7144 /* just setup sequence, do not trigger session recovery
7145 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007146 nfs41_setup_sequence(data->clp->cl_session,
7147 &data->args->la_seq_args,
7148 &data->res->lr_seq_res,
7149 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007150 dprintk("<-- %s\n", __func__);
7151}
7152
7153/*
7154 * Called from nfs4_state_manager thread for session setup, so don't recover
7155 * from sequence operation or clientid errors.
7156 */
7157static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7158{
7159 struct nfs4_get_lease_time_data *data =
7160 (struct nfs4_get_lease_time_data *)calldata;
7161
7162 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007163 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7164 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007165 switch (task->tk_status) {
7166 case -NFS4ERR_DELAY:
7167 case -NFS4ERR_GRACE:
7168 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7169 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7170 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007171 /* fall through */
7172 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007173 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007174 return;
7175 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007176 dprintk("<-- %s\n", __func__);
7177}
7178
Trond Myklebust17280172012-03-11 13:11:00 -04007179static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007180 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7181 .rpc_call_done = nfs4_get_lease_time_done,
7182};
7183
7184int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7185{
7186 struct rpc_task *task;
7187 struct nfs4_get_lease_time_args args;
7188 struct nfs4_get_lease_time_res res = {
7189 .lr_fsinfo = fsinfo,
7190 };
7191 struct nfs4_get_lease_time_data data = {
7192 .args = &args,
7193 .res = &res,
7194 .clp = clp,
7195 };
7196 struct rpc_message msg = {
7197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7198 .rpc_argp = &args,
7199 .rpc_resp = &res,
7200 };
7201 struct rpc_task_setup task_setup = {
7202 .rpc_client = clp->cl_rpcclient,
7203 .rpc_message = &msg,
7204 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007205 .callback_data = &data,
7206 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007207 };
7208 int status;
7209
Chuck Levera9c92d62013-08-09 12:48:18 -04007210 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007211 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007212 dprintk("--> %s\n", __func__);
7213 task = rpc_run_task(&task_setup);
7214
7215 if (IS_ERR(task))
7216 status = PTR_ERR(task);
7217 else {
7218 status = task->tk_status;
7219 rpc_put_task(task);
7220 }
7221 dprintk("<-- %s return %d\n", __func__, status);
7222
7223 return status;
7224}
7225
Andy Adamsonfc931582009-04-01 09:22:31 -04007226/*
7227 * Initialize the values to be used by the client in CREATE_SESSION
7228 * If nfs4_init_session set the fore channel request and response sizes,
7229 * use them.
7230 *
7231 * Set the back channel max_resp_sz_cached to zero to force the client to
7232 * always set csa_cachethis to FALSE because the current implementation
7233 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7234 */
7235static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7236{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007237 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007238
Andy Adamson18aad3d2013-06-26 12:21:49 -04007239 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7240 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7241
Andy Adamsonfc931582009-04-01 09:22:31 -04007242 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007243 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7244 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007245 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007246 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007247
7248 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007249 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007250 __func__,
7251 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007252 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007253
7254 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007255 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7256 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7257 args->bc_attrs.max_resp_sz_cached = 0;
7258 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7259 args->bc_attrs.max_reqs = 1;
7260
7261 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7262 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7263 __func__,
7264 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7265 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7266 args->bc_attrs.max_reqs);
7267}
7268
Trond Myklebust79969dd2015-02-18 11:30:18 -08007269static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7270 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007271{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007272 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007273 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007274
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007275 if (rcvd->max_resp_sz > sent->max_resp_sz)
7276 return -EINVAL;
7277 /*
7278 * Our requested max_ops is the minimum we need; we're not
7279 * prepared to break up compounds into smaller pieces than that.
7280 * So, no point even trying to continue if the server won't
7281 * cooperate:
7282 */
7283 if (rcvd->max_ops < sent->max_ops)
7284 return -EINVAL;
7285 if (rcvd->max_reqs == 0)
7286 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007287 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7288 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007289 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007290}
7291
Trond Myklebust79969dd2015-02-18 11:30:18 -08007292static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7293 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007294{
7295 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007296 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007297
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007298 if (!(res->flags & SESSION4_BACK_CHAN))
7299 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007300 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7301 return -EINVAL;
7302 if (rcvd->max_resp_sz < sent->max_resp_sz)
7303 return -EINVAL;
7304 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7305 return -EINVAL;
7306 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007307 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007308 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007309 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007310 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007311out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007312 return 0;
7313}
Andy Adamson8d353012009-04-01 09:22:32 -04007314
Andy Adamson8d353012009-04-01 09:22:32 -04007315static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007316 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007317{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007318 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007319
Trond Myklebust79969dd2015-02-18 11:30:18 -08007320 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007321 if (ret)
7322 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007323 return nfs4_verify_back_channel_attrs(args, res);
7324}
7325
7326static void nfs4_update_session(struct nfs4_session *session,
7327 struct nfs41_create_session_res *res)
7328{
7329 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007330 /* Mark client id and session as being confirmed */
7331 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7332 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007333 session->flags = res->flags;
7334 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007335 if (res->flags & SESSION4_BACK_CHAN)
7336 memcpy(&session->bc_attrs, &res->bc_attrs,
7337 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007338}
7339
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007340static int _nfs4_proc_create_session(struct nfs_client *clp,
7341 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007342{
7343 struct nfs4_session *session = clp->cl_session;
7344 struct nfs41_create_session_args args = {
7345 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007346 .clientid = clp->cl_clientid,
7347 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007348 .cb_program = NFS4_CALLBACK,
7349 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007350 struct nfs41_create_session_res res;
7351
Andy Adamsonfc931582009-04-01 09:22:31 -04007352 struct rpc_message msg = {
7353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7354 .rpc_argp = &args,
7355 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007356 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007357 };
7358 int status;
7359
7360 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007361 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007362
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007363 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007364 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007365
Trond Myklebust43095d32012-11-20 11:13:12 -05007366 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007367 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007368 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007369 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007370 if (clp->cl_seqid == res.seqid)
7371 clp->cl_seqid++;
7372 if (status)
7373 goto out;
7374 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007375 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007376out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007377 return status;
7378}
7379
7380/*
7381 * Issues a CREATE_SESSION operation to the server.
7382 * It is the responsibility of the caller to verify the session is
7383 * expired before calling this routine.
7384 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007385int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007386{
7387 int status;
7388 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007389 struct nfs4_session *session = clp->cl_session;
7390
7391 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7392
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007393 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007394 if (status)
7395 goto out;
7396
Andy Adamsonaacd5532011-11-09 13:58:21 -05007397 /* Init or reset the session slot tables */
7398 status = nfs4_setup_session_slot_tables(session);
7399 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007400 if (status)
7401 goto out;
7402
7403 ptr = (unsigned *)&session->sess_id.data[0];
7404 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7405 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007406out:
7407 dprintk("<-- %s\n", __func__);
7408 return status;
7409}
7410
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007411/*
7412 * Issue the over-the-wire RPC DESTROY_SESSION.
7413 * The caller must serialize access to this routine.
7414 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007415int nfs4_proc_destroy_session(struct nfs4_session *session,
7416 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007417{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007418 struct rpc_message msg = {
7419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7420 .rpc_argp = session,
7421 .rpc_cred = cred,
7422 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007423 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007424
7425 dprintk("--> nfs4_proc_destroy_session\n");
7426
7427 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007428 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7429 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007430
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007431 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007432 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007433
7434 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007435 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007436 "Session has been destroyed regardless...\n", status);
7437
7438 dprintk("<-- nfs4_proc_destroy_session\n");
7439 return status;
7440}
7441
Trond Myklebust7b38c362012-05-23 13:23:31 -04007442/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007443 * Renew the cl_session lease.
7444 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007445struct nfs4_sequence_data {
7446 struct nfs_client *clp;
7447 struct nfs4_sequence_args args;
7448 struct nfs4_sequence_res res;
7449};
7450
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007451static void nfs41_sequence_release(void *data)
7452{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007453 struct nfs4_sequence_data *calldata = data;
7454 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007455
Alexandros Batsakis71358402010-02-05 03:45:05 -08007456 if (atomic_read(&clp->cl_count) > 1)
7457 nfs4_schedule_state_renewal(clp);
7458 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007459 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007460}
7461
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007462static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7463{
7464 switch(task->tk_status) {
7465 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007466 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7467 return -EAGAIN;
7468 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007469 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007470 }
7471 return 0;
7472}
7473
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007474static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007475{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007476 struct nfs4_sequence_data *calldata = data;
7477 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007478
Trond Myklebust14516c32010-07-31 14:29:06 -04007479 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7480 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007481
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007482 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007483 if (task->tk_status < 0) {
7484 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007485 if (atomic_read(&clp->cl_count) == 1)
7486 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007487
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007488 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7489 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007490 return;
7491 }
7492 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007493 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007494out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007495 dprintk("<-- %s\n", __func__);
7496}
7497
7498static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7499{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007500 struct nfs4_sequence_data *calldata = data;
7501 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007502 struct nfs4_sequence_args *args;
7503 struct nfs4_sequence_res *res;
7504
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007505 args = task->tk_msg.rpc_argp;
7506 res = task->tk_msg.rpc_resp;
7507
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007508 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007509}
7510
7511static const struct rpc_call_ops nfs41_sequence_ops = {
7512 .rpc_call_done = nfs41_sequence_call_done,
7513 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007514 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007515};
7516
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007517static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7518 struct rpc_cred *cred,
7519 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007520{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007521 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007522 struct rpc_message msg = {
7523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7524 .rpc_cred = cred,
7525 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007526 struct rpc_task_setup task_setup_data = {
7527 .rpc_client = clp->cl_rpcclient,
7528 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007529 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007530 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007531 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007532
Alexandros Batsakis71358402010-02-05 03:45:05 -08007533 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007534 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007535 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007536 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007537 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007538 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007539 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007540 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007541 if (is_privileged)
7542 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007543 msg.rpc_argp = &calldata->args;
7544 msg.rpc_resp = &calldata->res;
7545 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007546 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007547
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007548 return rpc_run_task(&task_setup_data);
7549}
7550
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007551static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007552{
7553 struct rpc_task *task;
7554 int ret = 0;
7555
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007556 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007557 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007558 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007559 if (IS_ERR(task))
7560 ret = PTR_ERR(task);
7561 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007562 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007563 dprintk("<-- %s status=%d\n", __func__, ret);
7564 return ret;
7565}
7566
7567static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7568{
7569 struct rpc_task *task;
7570 int ret;
7571
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007572 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007573 if (IS_ERR(task)) {
7574 ret = PTR_ERR(task);
7575 goto out;
7576 }
7577 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007578 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007579 ret = task->tk_status;
7580 rpc_put_task(task);
7581out:
7582 dprintk("<-- %s status=%d\n", __func__, ret);
7583 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007584}
7585
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007586struct nfs4_reclaim_complete_data {
7587 struct nfs_client *clp;
7588 struct nfs41_reclaim_complete_args arg;
7589 struct nfs41_reclaim_complete_res res;
7590};
7591
7592static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7593{
7594 struct nfs4_reclaim_complete_data *calldata = data;
7595
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007596 nfs41_setup_sequence(calldata->clp->cl_session,
7597 &calldata->arg.seq_args,
7598 &calldata->res.seq_res,
7599 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007600}
7601
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007602static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7603{
7604 switch(task->tk_status) {
7605 case 0:
7606 case -NFS4ERR_COMPLETE_ALREADY:
7607 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7608 break;
7609 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007610 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007611 /* fall through */
7612 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007613 return -EAGAIN;
7614 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007615 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007616 }
7617 return 0;
7618}
7619
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007620static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7621{
7622 struct nfs4_reclaim_complete_data *calldata = data;
7623 struct nfs_client *clp = calldata->clp;
7624 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7625
7626 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007627 if (!nfs41_sequence_done(task, res))
7628 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007629
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007630 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007631 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7632 rpc_restart_call_prepare(task);
7633 return;
7634 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007635 dprintk("<-- %s\n", __func__);
7636}
7637
7638static void nfs4_free_reclaim_complete_data(void *data)
7639{
7640 struct nfs4_reclaim_complete_data *calldata = data;
7641
7642 kfree(calldata);
7643}
7644
7645static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7646 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7647 .rpc_call_done = nfs4_reclaim_complete_done,
7648 .rpc_release = nfs4_free_reclaim_complete_data,
7649};
7650
7651/*
7652 * Issue a global reclaim complete.
7653 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007654static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7655 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007656{
7657 struct nfs4_reclaim_complete_data *calldata;
7658 struct rpc_task *task;
7659 struct rpc_message msg = {
7660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007661 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007662 };
7663 struct rpc_task_setup task_setup_data = {
7664 .rpc_client = clp->cl_rpcclient,
7665 .rpc_message = &msg,
7666 .callback_ops = &nfs4_reclaim_complete_call_ops,
7667 .flags = RPC_TASK_ASYNC,
7668 };
7669 int status = -ENOMEM;
7670
7671 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007672 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007673 if (calldata == NULL)
7674 goto out;
7675 calldata->clp = clp;
7676 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007677
Chuck Levera9c92d62013-08-09 12:48:18 -04007678 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007679 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007680 msg.rpc_argp = &calldata->arg;
7681 msg.rpc_resp = &calldata->res;
7682 task_setup_data.callback_data = calldata;
7683 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007684 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007685 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007686 goto out;
7687 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007688 status = nfs4_wait_for_completion_rpc_task(task);
7689 if (status == 0)
7690 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007691 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007692 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007693out:
7694 dprintk("<-- %s status=%d\n", __func__, status);
7695 return status;
7696}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007697
7698static void
7699nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7700{
7701 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007702 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007703 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007704
7705 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007706 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7707 * right now covering the LAYOUTGET we are about to send.
7708 * However, that is not so catastrophic, and there seems
7709 * to be no way to prevent it completely.
7710 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007711 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007712 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007713 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007714 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7715 NFS_I(lgp->args.inode)->layout,
Peng Taoce6ab4f2014-09-06 00:53:24 +08007716 &lgp->args.range,
Fred Isamancf7d63f2011-01-06 11:36:25 +00007717 lgp->args.ctx->state)) {
7718 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007719 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007720}
7721
7722static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7723{
7724 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007725 struct inode *inode = lgp->args.inode;
7726 struct nfs_server *server = NFS_SERVER(inode);
7727 struct pnfs_layout_hdr *lo;
7728 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007729 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007730
Boaz Harroshed7e5422014-01-22 20:34:54 +02007731 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007732
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007733 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007734 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007735
7736 switch (task->tk_status) {
7737 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007738 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007739 /*
7740 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7741 * (or clients) writing to the same RAID stripe
7742 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007743 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007744 /*
7745 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7746 * existing layout before getting a new one).
7747 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007748 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007749 timeo = rpc_get_timeout(task->tk_client);
7750 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007751 now = jiffies;
7752 if (time_after(giveup, now)) {
7753 unsigned long delay;
7754
7755 /* Delay for:
7756 * - Not less then NFS4_POLL_RETRY_MIN.
7757 * - One last time a jiffie before we give up
7758 * - exponential backoff (time_now minus start_attempt)
7759 */
7760 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7761 min((giveup - now - 1),
7762 now - lgp->args.timestamp));
7763
7764 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7765 __func__, delay);
7766 rpc_delay(task, delay);
7767 task->tk_status = 0;
7768 rpc_restart_call_prepare(task);
7769 goto out; /* Do not call nfs4_async_handle_error() */
7770 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007771 break;
7772 case -NFS4ERR_EXPIRED:
7773 case -NFS4ERR_BAD_STATEID:
7774 spin_lock(&inode->i_lock);
7775 lo = NFS_I(inode)->layout;
7776 if (!lo || list_empty(&lo->plh_segs)) {
7777 spin_unlock(&inode->i_lock);
7778 /* If the open stateid was bad, then recover it. */
7779 state = lgp->args.ctx->state;
7780 } else {
7781 LIST_HEAD(head);
7782
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007783 /*
7784 * Mark the bad layout state as invalid, then retry
7785 * with the current stateid.
7786 */
Trond Myklebustee314c22012-10-01 17:25:48 -07007787 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7788 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007789 pnfs_free_lseg_list(&head);
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007790
7791 task->tk_status = 0;
7792 rpc_restart_call_prepare(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007793 }
7794 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007795 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebustee314c22012-10-01 17:25:48 -07007796 rpc_restart_call_prepare(task);
7797out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007798 dprintk("<-- %s\n", __func__);
7799}
7800
Idan Kedar85541162012-08-02 11:47:10 +03007801static size_t max_response_pages(struct nfs_server *server)
7802{
7803 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7804 return nfs_page_array_len(0, max_resp_sz);
7805}
7806
7807static void nfs4_free_pages(struct page **pages, size_t size)
7808{
7809 int i;
7810
7811 if (!pages)
7812 return;
7813
7814 for (i = 0; i < size; i++) {
7815 if (!pages[i])
7816 break;
7817 __free_page(pages[i]);
7818 }
7819 kfree(pages);
7820}
7821
7822static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7823{
7824 struct page **pages;
7825 int i;
7826
7827 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7828 if (!pages) {
7829 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7830 return NULL;
7831 }
7832
7833 for (i = 0; i < size; i++) {
7834 pages[i] = alloc_page(gfp_flags);
7835 if (!pages[i]) {
7836 dprintk("%s: failed to allocate page\n", __func__);
7837 nfs4_free_pages(pages, size);
7838 return NULL;
7839 }
7840 }
7841
7842 return pages;
7843}
7844
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007845static void nfs4_layoutget_release(void *calldata)
7846{
7847 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007848 struct inode *inode = lgp->args.inode;
7849 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007850 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007851
7852 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007853 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007854 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007855 put_nfs_open_context(lgp->args.ctx);
7856 kfree(calldata);
7857 dprintk("<-- %s\n", __func__);
7858}
7859
7860static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7861 .rpc_call_prepare = nfs4_layoutget_prepare,
7862 .rpc_call_done = nfs4_layoutget_done,
7863 .rpc_release = nfs4_layoutget_release,
7864};
7865
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007866struct pnfs_layout_segment *
7867nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007868{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007869 struct inode *inode = lgp->args.inode;
7870 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007871 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007872 struct rpc_task *task;
7873 struct rpc_message msg = {
7874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7875 .rpc_argp = &lgp->args,
7876 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007877 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007878 };
7879 struct rpc_task_setup task_setup_data = {
7880 .rpc_client = server->client,
7881 .rpc_message = &msg,
7882 .callback_ops = &nfs4_layoutget_call_ops,
7883 .callback_data = lgp,
7884 .flags = RPC_TASK_ASYNC,
7885 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007886 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007887 int status = 0;
7888
7889 dprintk("--> %s\n", __func__);
7890
Peng Tao4bd5a982014-11-17 11:05:17 +08007891 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7892 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7893
Idan Kedar85541162012-08-02 11:47:10 +03007894 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7895 if (!lgp->args.layout.pages) {
7896 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007897 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007898 }
7899 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007900 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007901
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007902 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007903 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007904 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007905
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007906 task = rpc_run_task(&task_setup_data);
7907 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007908 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007909 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007910 if (status == 0)
7911 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007912 trace_nfs4_layoutget(lgp->args.ctx,
7913 &lgp->args.range,
7914 &lgp->res.range,
7915 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007916 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7917 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007918 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007919 rpc_put_task(task);
7920 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007921 if (status)
7922 return ERR_PTR(status);
7923 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007924}
7925
Benny Halevycbe82602011-05-22 19:52:37 +03007926static void
7927nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7928{
7929 struct nfs4_layoutreturn *lrp = calldata;
7930
7931 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007932 nfs41_setup_sequence(lrp->clp->cl_session,
7933 &lrp->args.seq_args,
7934 &lrp->res.seq_res,
7935 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007936}
7937
7938static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7939{
7940 struct nfs4_layoutreturn *lrp = calldata;
7941 struct nfs_server *server;
7942
7943 dprintk("--> %s\n", __func__);
7944
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007945 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007946 return;
7947
7948 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007949 switch (task->tk_status) {
7950 default:
7951 task->tk_status = 0;
7952 case 0:
7953 break;
7954 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007955 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007956 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007957 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007958 return;
7959 }
Benny Halevycbe82602011-05-22 19:52:37 +03007960 dprintk("<-- %s\n", __func__);
7961}
7962
7963static void nfs4_layoutreturn_release(void *calldata)
7964{
7965 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007966 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02007967 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03007968
7969 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007970 spin_lock(&lo->plh_inode->i_lock);
7971 if (lrp->res.lrs_present)
7972 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebustc5d73712015-07-09 17:58:39 +02007973 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
Tom Haynesd67ae822014-12-11 17:02:04 -05007974 pnfs_clear_layoutreturn_waitbit(lo);
Peng Tao193e3aa2014-11-17 09:30:41 +08007975 clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE, &lo->plh_flags);
7976 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
Trond Myklebust849b2862012-09-24 14:18:39 -04007977 lo->plh_block_lgets--;
7978 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02007979 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007980 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05007981 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03007982 kfree(calldata);
7983 dprintk("<-- %s\n", __func__);
7984}
7985
7986static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7987 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7988 .rpc_call_done = nfs4_layoutreturn_done,
7989 .rpc_release = nfs4_layoutreturn_release,
7990};
7991
Peng Tao6c166052014-11-17 09:30:40 +08007992int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03007993{
7994 struct rpc_task *task;
7995 struct rpc_message msg = {
7996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7997 .rpc_argp = &lrp->args,
7998 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007999 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008000 };
8001 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008002 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008003 .rpc_message = &msg,
8004 .callback_ops = &nfs4_layoutreturn_call_ops,
8005 .callback_data = lrp,
8006 };
Peng Tao6c166052014-11-17 09:30:40 +08008007 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008008
8009 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008010 if (!sync) {
8011 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8012 if (!lrp->inode) {
8013 nfs4_layoutreturn_release(lrp);
8014 return -EAGAIN;
8015 }
8016 task_setup_data.flags |= RPC_TASK_ASYNC;
8017 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008018 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008019 task = rpc_run_task(&task_setup_data);
8020 if (IS_ERR(task))
8021 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008022 if (sync)
8023 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008024 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008025 dprintk("<-- %s status=%d\n", __func__, status);
8026 rpc_put_task(task);
8027 return status;
8028}
8029
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008030static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008031_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8032 struct pnfs_device *pdev,
8033 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008034{
8035 struct nfs4_getdeviceinfo_args args = {
8036 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008037 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8038 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008039 };
8040 struct nfs4_getdeviceinfo_res res = {
8041 .pdev = pdev,
8042 };
8043 struct rpc_message msg = {
8044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8045 .rpc_argp = &args,
8046 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008047 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008048 };
8049 int status;
8050
8051 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008052 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008053 if (res.notification & ~args.notify_types)
8054 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008055 if (res.notification != args.notify_types)
8056 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008057
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008058 dprintk("<-- %s status=%d\n", __func__, status);
8059
8060 return status;
8061}
8062
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008063int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8064 struct pnfs_device *pdev,
8065 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008066{
8067 struct nfs4_exception exception = { };
8068 int err;
8069
8070 do {
8071 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008072 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008073 &exception);
8074 } while (exception.retry);
8075 return err;
8076}
8077EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8078
Andy Adamson863a3c62011-03-23 13:27:54 +00008079static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8080{
8081 struct nfs4_layoutcommit_data *data = calldata;
8082 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008083 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008084
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008085 nfs41_setup_sequence(session,
8086 &data->args.seq_args,
8087 &data->res.seq_res,
8088 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008089}
8090
8091static void
8092nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8093{
8094 struct nfs4_layoutcommit_data *data = calldata;
8095 struct nfs_server *server = NFS_SERVER(data->args.inode);
8096
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008097 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008098 return;
8099
8100 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008101 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8102 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8103 case -NFS4ERR_BADLAYOUT: /* no layout */
8104 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008105 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008106 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008107 break;
8108 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008109 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008110 rpc_restart_call_prepare(task);
8111 return;
8112 }
8113 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008114}
8115
8116static void nfs4_layoutcommit_release(void *calldata)
8117{
8118 struct nfs4_layoutcommit_data *data = calldata;
8119
Andy Adamsondb29c082011-07-30 20:52:38 -04008120 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008121 nfs_post_op_update_inode_force_wcc(data->args.inode,
8122 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008123 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008124 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008125 kfree(data);
8126}
8127
8128static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8129 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8130 .rpc_call_done = nfs4_layoutcommit_done,
8131 .rpc_release = nfs4_layoutcommit_release,
8132};
8133
8134int
Andy Adamsonef311532011-03-12 02:58:10 -05008135nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008136{
8137 struct rpc_message msg = {
8138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8139 .rpc_argp = &data->args,
8140 .rpc_resp = &data->res,
8141 .rpc_cred = data->cred,
8142 };
8143 struct rpc_task_setup task_setup_data = {
8144 .task = &data->task,
8145 .rpc_client = NFS_CLIENT(data->args.inode),
8146 .rpc_message = &msg,
8147 .callback_ops = &nfs4_layoutcommit_ops,
8148 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008149 };
8150 struct rpc_task *task;
8151 int status = 0;
8152
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008153 dprintk("NFS: initiating layoutcommit call. sync %d "
8154 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008155 data->args.lastbytewritten,
8156 data->args.inode->i_ino);
8157
Trond Myklebust472e2592015-02-05 16:50:30 -05008158 if (!sync) {
8159 data->inode = nfs_igrab_and_active(data->args.inode);
8160 if (data->inode == NULL) {
8161 nfs4_layoutcommit_release(data);
8162 return -EAGAIN;
8163 }
8164 task_setup_data.flags = RPC_TASK_ASYNC;
8165 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008166 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008167 task = rpc_run_task(&task_setup_data);
8168 if (IS_ERR(task))
8169 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008170 if (sync)
8171 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008172 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008173 dprintk("%s: status %d\n", __func__, status);
8174 rpc_put_task(task);
8175 return status;
8176}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008177
Andy Adamson97431202013-08-08 10:57:56 -04008178/**
8179 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8180 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8181 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008182static int
8183_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008184 struct nfs_fsinfo *info,
8185 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008186{
8187 struct nfs41_secinfo_no_name_args args = {
8188 .style = SECINFO_STYLE_CURRENT_FH,
8189 };
8190 struct nfs4_secinfo_res res = {
8191 .flavors = flavors,
8192 };
8193 struct rpc_message msg = {
8194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8195 .rpc_argp = &args,
8196 .rpc_resp = &res,
8197 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008198 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008199 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008200 int status;
8201
8202 if (use_integrity) {
8203 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008204 cred = nfs4_get_clid_cred(server->nfs_client);
8205 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008206 }
8207
8208 dprintk("--> %s\n", __func__);
8209 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8210 &res.seq_res, 0);
8211 dprintk("<-- %s status=%d\n", __func__, status);
8212
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008213 if (cred)
8214 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008215
8216 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008217}
8218
8219static int
8220nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8221 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8222{
8223 struct nfs4_exception exception = { };
8224 int err;
8225 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008226 /* first try using integrity protection */
8227 err = -NFS4ERR_WRONGSEC;
8228
8229 /* try to use integrity protection with machine cred */
8230 if (_nfs4_is_integrity_protected(server->nfs_client))
8231 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8232 flavors, true);
8233
8234 /*
8235 * if unable to use integrity protection, or SECINFO with
8236 * integrity protection returns NFS4ERR_WRONGSEC (which is
8237 * disallowed by spec, but exists in deployed servers) use
8238 * the current filesystem's rpc_client and the user cred.
8239 */
8240 if (err == -NFS4ERR_WRONGSEC)
8241 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8242 flavors, false);
8243
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008244 switch (err) {
8245 case 0:
8246 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008247 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008248 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008249 default:
8250 err = nfs4_handle_exception(server, err, &exception);
8251 }
8252 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008253out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008254 return err;
8255}
8256
8257static int
8258nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8259 struct nfs_fsinfo *info)
8260{
8261 int err;
8262 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008263 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008264 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008265 struct nfs4_secinfo4 *secinfo;
8266 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008267
8268 page = alloc_page(GFP_KERNEL);
8269 if (!page) {
8270 err = -ENOMEM;
8271 goto out;
8272 }
8273
8274 flavors = page_address(page);
8275 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8276
8277 /*
8278 * Fall back on "guess and check" method if
8279 * the server doesn't support SECINFO_NO_NAME
8280 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008281 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008282 err = nfs4_find_root_sec(server, fhandle, info);
8283 goto out_freepage;
8284 }
8285 if (err)
8286 goto out_freepage;
8287
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008288 for (i = 0; i < flavors->num_flavors; i++) {
8289 secinfo = &flavors->flavors[i];
8290
8291 switch (secinfo->flavor) {
8292 case RPC_AUTH_NULL:
8293 case RPC_AUTH_UNIX:
8294 case RPC_AUTH_GSS:
8295 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8296 &secinfo->flavor_info);
8297 break;
8298 default:
8299 flavor = RPC_AUTH_MAXFLAVOR;
8300 break;
8301 }
8302
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008303 if (!nfs_auth_info_match(&server->auth_info, flavor))
8304 flavor = RPC_AUTH_MAXFLAVOR;
8305
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008306 if (flavor != RPC_AUTH_MAXFLAVOR) {
8307 err = nfs4_lookup_root_sec(server, fhandle,
8308 info, flavor);
8309 if (!err)
8310 break;
8311 }
8312 }
8313
8314 if (flavor == RPC_AUTH_MAXFLAVOR)
8315 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008316
8317out_freepage:
8318 put_page(page);
8319 if (err == -EACCES)
8320 return -EPERM;
8321out:
8322 return err;
8323}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008324
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008325static int _nfs41_test_stateid(struct nfs_server *server,
8326 nfs4_stateid *stateid,
8327 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008328{
8329 int status;
8330 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008331 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008332 };
8333 struct nfs41_test_stateid_res res;
8334 struct rpc_message msg = {
8335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8336 .rpc_argp = &args,
8337 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008338 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008339 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008340 struct rpc_clnt *rpc_client = server->client;
8341
8342 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8343 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008344
Chuck Lever38527b12012-07-11 16:30:23 -04008345 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008346 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008347 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008348 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008349 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008350 if (status != NFS_OK) {
8351 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008352 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008353 }
8354 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008355 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008356}
8357
Chuck Lever38527b12012-07-11 16:30:23 -04008358/**
8359 * nfs41_test_stateid - perform a TEST_STATEID operation
8360 *
8361 * @server: server / transport on which to perform the operation
8362 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008363 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008364 *
8365 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8366 * Otherwise a negative NFS4ERR value is returned if the operation
8367 * failed or the state ID is not currently valid.
8368 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008369static int nfs41_test_stateid(struct nfs_server *server,
8370 nfs4_stateid *stateid,
8371 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008372{
8373 struct nfs4_exception exception = { };
8374 int err;
8375 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008376 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008377 if (err != -NFS4ERR_DELAY)
8378 break;
8379 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008380 } while (exception.retry);
8381 return err;
8382}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008383
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008384struct nfs_free_stateid_data {
8385 struct nfs_server *server;
8386 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008387 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008388};
8389
8390static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8391{
8392 struct nfs_free_stateid_data *data = calldata;
8393 nfs41_setup_sequence(nfs4_get_session(data->server),
8394 &data->args.seq_args,
8395 &data->res.seq_res,
8396 task);
8397}
8398
8399static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8400{
8401 struct nfs_free_stateid_data *data = calldata;
8402
8403 nfs41_sequence_done(task, &data->res.seq_res);
8404
8405 switch (task->tk_status) {
8406 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008407 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008408 rpc_restart_call_prepare(task);
8409 }
8410}
8411
8412static void nfs41_free_stateid_release(void *calldata)
8413{
8414 kfree(calldata);
8415}
8416
Trond Myklebust17f26b12013-08-21 15:48:42 -04008417static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008418 .rpc_call_prepare = nfs41_free_stateid_prepare,
8419 .rpc_call_done = nfs41_free_stateid_done,
8420 .rpc_release = nfs41_free_stateid_release,
8421};
8422
8423static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8424 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008425 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008426 bool privileged)
8427{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008428 struct rpc_message msg = {
8429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008430 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008431 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008432 struct rpc_task_setup task_setup = {
8433 .rpc_client = server->client,
8434 .rpc_message = &msg,
8435 .callback_ops = &nfs41_free_stateid_ops,
8436 .flags = RPC_TASK_ASYNC,
8437 };
8438 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008439
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008440 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8441 &task_setup.rpc_client, &msg);
8442
Chuck Lever38527b12012-07-11 16:30:23 -04008443 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008444 data = kmalloc(sizeof(*data), GFP_NOFS);
8445 if (!data)
8446 return ERR_PTR(-ENOMEM);
8447 data->server = server;
8448 nfs4_stateid_copy(&data->args.stateid, stateid);
8449
8450 task_setup.callback_data = data;
8451
8452 msg.rpc_argp = &data->args;
8453 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008454 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008455 if (privileged)
8456 nfs4_set_sequence_privileged(&data->args.seq_args);
8457
8458 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008459}
8460
Chuck Lever38527b12012-07-11 16:30:23 -04008461/**
8462 * nfs41_free_stateid - perform a FREE_STATEID operation
8463 *
8464 * @server: server / transport on which to perform the operation
8465 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008466 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008467 *
8468 * Returns NFS_OK if the server freed "stateid". Otherwise a
8469 * negative NFS4ERR value is returned.
8470 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008471static int nfs41_free_stateid(struct nfs_server *server,
8472 nfs4_stateid *stateid,
8473 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008474{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008475 struct rpc_task *task;
8476 int ret;
8477
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008478 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008479 if (IS_ERR(task))
8480 return PTR_ERR(task);
8481 ret = rpc_wait_for_completion_task(task);
8482 if (!ret)
8483 ret = task->tk_status;
8484 rpc_put_task(task);
8485 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008486}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008487
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008488static void
8489nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008490{
8491 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008492 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008493
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008494 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008495 nfs4_free_lock_state(server, lsp);
8496 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008497 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008498 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008499}
8500
Trond Myklebust36281ca2012-03-04 18:13:56 -05008501static bool nfs41_match_stateid(const nfs4_stateid *s1,
8502 const nfs4_stateid *s2)
8503{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008504 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008505 return false;
8506
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008507 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008508 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008509 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008510 return true;
8511
8512 return false;
8513}
8514
Andy Adamson557134a2009-04-01 09:21:53 -04008515#endif /* CONFIG_NFS_V4_1 */
8516
Trond Myklebust36281ca2012-03-04 18:13:56 -05008517static bool nfs4_match_stateid(const nfs4_stateid *s1,
8518 const nfs4_stateid *s2)
8519{
Trond Myklebustf597c532012-03-04 18:13:56 -05008520 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008521}
8522
8523
Trond Myklebust17280172012-03-11 13:11:00 -04008524static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008525 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008526 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 .recover_open = nfs4_open_reclaim,
8528 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008529 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008530 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531};
8532
Andy Adamson591d71c2009-04-01 09:22:47 -04008533#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008534static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008535 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8536 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8537 .recover_open = nfs4_open_reclaim,
8538 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008539 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008540 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008541 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008542};
8543#endif /* CONFIG_NFS_V4_1 */
8544
Trond Myklebust17280172012-03-11 13:11:00 -04008545static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008546 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008547 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008548 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008550 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551};
8552
Andy Adamson591d71c2009-04-01 09:22:47 -04008553#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008554static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008555 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8556 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008557 .recover_open = nfs41_open_expired,
8558 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008559 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008560};
8561#endif /* CONFIG_NFS_V4_1 */
8562
Trond Myklebust17280172012-03-11 13:11:00 -04008563static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008564 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008565 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008566 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008567};
8568
8569#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008570static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008571 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008572 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008573 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008574};
8575#endif
8576
Chuck Leverec011fe2013-10-17 14:12:39 -04008577static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008578 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008579 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008580};
8581
8582#if defined(CONFIG_NFS_V4_1)
8583static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008584 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008585 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008586};
8587#endif /* CONFIG_NFS_V4_1 */
8588
Trond Myklebust97dc1352010-06-16 09:52:26 -04008589static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8590 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008591 .init_caps = NFS_CAP_READDIRPLUS
8592 | NFS_CAP_ATOMIC_OPEN
8593 | NFS_CAP_CHANGE_ATTR
8594 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008595 .init_client = nfs40_init_client,
8596 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008597 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008598 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008599 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008600 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008601 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008602 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8603 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8604 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008605 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008606};
8607
8608#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008609static struct nfs_seqid *
8610nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8611{
8612 return NULL;
8613}
8614
Trond Myklebust97dc1352010-06-16 09:52:26 -04008615static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8616 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008617 .init_caps = NFS_CAP_READDIRPLUS
8618 | NFS_CAP_ATOMIC_OPEN
8619 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008620 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008621 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008622 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008623 .init_client = nfs41_init_client,
8624 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008625 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008626 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008627 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008628 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008629 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008630 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8631 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8632 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008633 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008634};
8635#endif
8636
Steve Dickson42c2c422013-05-22 12:50:38 -04008637#if defined(CONFIG_NFS_V4_2)
8638static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8639 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008640 .init_caps = NFS_CAP_READDIRPLUS
8641 | NFS_CAP_ATOMIC_OPEN
8642 | NFS_CAP_CHANGE_ATTR
8643 | NFS_CAP_POSIX_LOCK
8644 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008645 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008646 | NFS_CAP_ALLOCATE
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008647 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008648 | NFS_CAP_SEEK
8649 | NFS_CAP_LAYOUTSTATS,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008650 .init_client = nfs41_init_client,
8651 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008652 .match_stateid = nfs41_match_stateid,
8653 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008654 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008655 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008656 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008657 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8658 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8659 .state_renewal_ops = &nfs41_state_renewal_ops,
8660};
8661#endif
8662
Trond Myklebust97dc1352010-06-16 09:52:26 -04008663const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8664 [0] = &nfs_v4_0_minor_ops,
8665#if defined(CONFIG_NFS_V4_1)
8666 [1] = &nfs_v4_1_minor_ops,
8667#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008668#if defined(CONFIG_NFS_V4_2)
8669 [2] = &nfs_v4_2_minor_ops,
8670#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008671};
8672
Trond Myklebust17f26b12013-08-21 15:48:42 -04008673static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008674 .create = nfs_create,
8675 .lookup = nfs_lookup,
8676 .atomic_open = nfs_atomic_open,
8677 .link = nfs_link,
8678 .unlink = nfs_unlink,
8679 .symlink = nfs_symlink,
8680 .mkdir = nfs_mkdir,
8681 .rmdir = nfs_rmdir,
8682 .mknod = nfs_mknod,
8683 .rename = nfs_rename,
8684 .permission = nfs_permission,
8685 .getattr = nfs_getattr,
8686 .setattr = nfs_setattr,
8687 .getxattr = generic_getxattr,
8688 .setxattr = generic_setxattr,
8689 .listxattr = generic_listxattr,
8690 .removexattr = generic_removexattr,
8691};
8692
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008693static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008694 .permission = nfs_permission,
8695 .getattr = nfs_getattr,
8696 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008697 .getxattr = generic_getxattr,
8698 .setxattr = generic_setxattr,
8699 .listxattr = generic_listxattr,
8700 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008701};
8702
David Howells509de812006-08-22 20:06:11 -04008703const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704 .version = 4, /* protocol version */
8705 .dentry_ops = &nfs4_dentry_operations,
8706 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008707 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008708 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008710 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008711 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712 .getattr = nfs4_proc_getattr,
8713 .setattr = nfs4_proc_setattr,
8714 .lookup = nfs4_proc_lookup,
8715 .access = nfs4_proc_access,
8716 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008717 .create = nfs4_proc_create,
8718 .remove = nfs4_proc_remove,
8719 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008720 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008722 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008723 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008724 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 .link = nfs4_proc_link,
8726 .symlink = nfs4_proc_symlink,
8727 .mkdir = nfs4_proc_mkdir,
8728 .rmdir = nfs4_proc_remove,
8729 .readdir = nfs4_proc_readdir,
8730 .mknod = nfs4_proc_mknod,
8731 .statfs = nfs4_proc_statfs,
8732 .fsinfo = nfs4_proc_fsinfo,
8733 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008734 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008735 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008736 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008738 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008740 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008742 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008743 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008745 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008746 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008747 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008748 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008749 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008750 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008751 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008752 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008753 .create_server = nfs4_create_server,
8754 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755};
8756
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008757static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8758 .prefix = XATTR_NAME_NFSV4_ACL,
8759 .list = nfs4_xattr_list_nfs4_acl,
8760 .get = nfs4_xattr_get_nfs4_acl,
8761 .set = nfs4_xattr_set_nfs4_acl,
8762};
8763
8764const struct xattr_handler *nfs4_xattr_handlers[] = {
8765 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008766#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8767 &nfs4_xattr_nfs4_label_handler,
8768#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008769 NULL
8770};
8771
Linus Torvalds1da177e2005-04-16 15:20:36 -07008772/*
8773 * Local variables:
8774 * c-basic-offset: 8
8775 * End:
8776 */