blob: 0fc1b0cdda98eb341acc568791a99363e9ad6fc7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
NeilBrown8478eaa2014-09-18 16:09:27 +100081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *, long *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040083static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
84static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040085static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040087 struct nfs4_state *state, struct nfs4_label *ilabel,
88 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040090static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 struct rpc_cred *);
92static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
93 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040094#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040095
96#ifdef CONFIG_NFS_V4_SECURITY_LABEL
97static inline struct nfs4_label *
98nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
99 struct iattr *sattr, struct nfs4_label *label)
100{
101 int err;
102
103 if (label == NULL)
104 return NULL;
105
106 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
107 return NULL;
108
David Quigleyaa9c2662013-05-22 12:50:44 -0400109 err = security_dentry_init_security(dentry, sattr->ia_mode,
110 &dentry->d_name, (void **)&label->label, &label->len);
111 if (err == 0)
112 return label;
113
114 return NULL;
115}
116static inline void
117nfs4_label_release_security(struct nfs4_label *label)
118{
119 if (label)
120 security_release_secctx(label->label, label->len);
121}
122static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
123{
124 if (label)
125 return server->attr_bitmask;
126
127 return server->attr_bitmask_nl;
128}
129#else
130static inline struct nfs4_label *
131nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
132 struct iattr *sattr, struct nfs4_label *l)
133{ return NULL; }
134static inline void
135nfs4_label_release_security(struct nfs4_label *label)
136{ return; }
137static inline u32 *
138nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139{ return server->attr_bitmask; }
140#endif
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500143static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Trond Myklebust52567b02009-10-23 14:46:42 -0400145 if (err >= -1000)
146 return err;
147 switch (err) {
148 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500149 case -NFS4ERR_LAYOUTTRYLATER:
150 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400151 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000152 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400153 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800155 case -NFS4ERR_BADOWNER:
156 case -NFS4ERR_BADNAME:
157 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400158 case -NFS4ERR_SHARE_DENIED:
159 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400160 case -NFS4ERR_MINOR_VERS_MISMATCH:
161 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400162 case -NFS4ERR_FILE_OPEN:
163 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400164 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700166 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400169 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * This is our standard bitmap for GETATTR requests.
174 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400175const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 FATTR4_WORD0_TYPE
177 | FATTR4_WORD0_CHANGE
178 | FATTR4_WORD0_SIZE
179 | FATTR4_WORD0_FSID
180 | FATTR4_WORD0_FILEID,
181 FATTR4_WORD1_MODE
182 | FATTR4_WORD1_NUMLINKS
183 | FATTR4_WORD1_OWNER
184 | FATTR4_WORD1_OWNER_GROUP
185 | FATTR4_WORD1_RAWDEV
186 | FATTR4_WORD1_SPACE_USED
187 | FATTR4_WORD1_TIME_ACCESS
188 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400189 | FATTR4_WORD1_TIME_MODIFY
190 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400191#ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Trond Myklebust1549210f2012-06-05 09:16:47 -0400196static const u32 nfs4_pnfs_open_bitmap[3] = {
197 FATTR4_WORD0_TYPE
198 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_SIZE
200 | FATTR4_WORD0_FSID
201 | FATTR4_WORD0_FILEID,
202 FATTR4_WORD1_MODE
203 | FATTR4_WORD1_NUMLINKS
204 | FATTR4_WORD1_OWNER
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
212};
213
Andy Adamsone23008e2012-10-02 21:07:32 -0400214static const u32 nfs4_open_noattr_bitmap[3] = {
215 FATTR4_WORD0_TYPE
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
218};
219
David Quigleya09df2c2013-05-22 12:50:41 -0400220const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
227};
228
David Quigleya09df2c2013-05-22 12:50:41 -0400229const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 FATTR4_WORD0_MAXLINK
231 | FATTR4_WORD0_MAXNAME,
232 0
233};
234
Fred Isamandae100c2011-07-30 20:52:37 -0400235const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700239 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400245 FATTR4_WORD0_TYPE
246 | FATTR4_WORD0_CHANGE
247 | FATTR4_WORD0_SIZE
248 | FATTR4_WORD0_FSID
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
251 FATTR4_WORD1_MODE
252 | FATTR4_WORD1_NUMLINKS
253 | FATTR4_WORD1_OWNER
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400261};
262
Al Virobc4785c2006-10-19 23:28:51 -0700263static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct nfs4_readdir_arg *readdir)
265{
Al Viro0dbb4c62006-10-19 23:28:49 -0700266 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000269 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
271 return;
272 }
273
274 readdir->cookie = 0;
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
276 if (cookie == 2)
277 return;
278
279 /*
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
284 * instead of 1 or 2.
285 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800286 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (cookie == 0) {
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
294 p++;
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000298 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800314 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
NeilBrown8478eaa2014-09-18 16:09:27 +1000317static long nfs4_update_delay(long *timeout)
318{
319 long ret;
320 if (!timeout)
321 return NFS4_POLL_RETRY_MAX;
322 if (*timeout <= 0)
323 *timeout = NFS4_POLL_RETRY_MIN;
324 if (*timeout > NFS4_POLL_RETRY_MAX)
325 *timeout = NFS4_POLL_RETRY_MAX;
326 ret = *timeout;
327 *timeout <<= 1;
328 return ret;
329}
330
Trond Myklebust65de8722008-12-23 15:21:44 -0500331static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
332{
333 int res = 0;
334
335 might_sleep();
336
NeilBrown8478eaa2014-09-18 16:09:27 +1000337 freezable_schedule_timeout_killable_unsafe(
338 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 if (fatal_signal_pending(current))
340 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 return res;
342}
343
344/* This is the error handling routine for processes that are allowed
345 * to sleep.
346 */
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500347int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500348{
349 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500350 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500352 int ret = errorcode;
353
354 exception->retry = 0;
355 switch(errorcode) {
356 case 0:
357 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500358 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400359 case -NFS4ERR_DELEG_REVOKED:
360 case -NFS4ERR_ADMIN_REVOKED:
361 case -NFS4ERR_BAD_STATEID:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400362 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400363 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500364 exception->retry = 1;
365 return 0;
366 }
367 if (state == NULL)
368 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400369 ret = nfs4_schedule_stateid_recovery(server, state);
370 if (ret < 0)
371 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500372 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400373 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400374 if (state != NULL) {
375 ret = nfs4_schedule_stateid_recovery(server, state);
376 if (ret < 0)
377 break;
378 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500379 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500380 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500381 nfs4_schedule_lease_recovery(clp);
382 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400383 case -NFS4ERR_MOVED:
384 ret = nfs4_schedule_migration_recovery(server);
385 if (ret < 0)
386 break;
387 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400388 case -NFS4ERR_LEASE_MOVED:
389 nfs4_schedule_lease_moved_recovery(clp);
390 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500391#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400392 case -NFS4ERR_BADSESSION:
393 case -NFS4ERR_BADSLOT:
394 case -NFS4ERR_BAD_HIGH_SLOT:
395 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
396 case -NFS4ERR_DEADSESSION:
397 case -NFS4ERR_SEQ_FALSE_RETRY:
398 case -NFS4ERR_SEQ_MISORDERED:
399 dprintk("%s ERROR: %d Reset session\n", __func__,
400 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400401 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400402 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500403#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500405 if (exception->timeout > HZ) {
406 /* We have retried a decent amount, time to
407 * fail
408 */
409 ret = -EBUSY;
410 break;
411 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500412 case -NFS4ERR_GRACE:
413 case -NFS4ERR_DELAY:
414 ret = nfs4_delay(server->client, &exception->timeout);
415 if (ret != 0)
416 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400417 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500418 case -NFS4ERR_OLD_STATEID:
419 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800420 break;
421 case -NFS4ERR_BADOWNER:
422 /* The following works around a Linux server bug! */
423 case -NFS4ERR_BADNAME:
424 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
425 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
426 exception->retry = 1;
427 printk(KERN_WARNING "NFS: v4 server %s "
428 "does not accept raw "
429 "uid/gids. "
430 "Reenabling the idmapper.\n",
431 server->nfs_client->cl_hostname);
432 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500433 }
434 /* We failed to handle the error */
435 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500436wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500437 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400438 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
439 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500440 if (ret == 0)
441 exception->retry = 1;
442 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500443}
444
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400445/*
446 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
447 * or 'false' otherwise.
448 */
449static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
450{
451 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
452
453 if (flavor == RPC_AUTH_GSS_KRB5I ||
454 flavor == RPC_AUTH_GSS_KRB5P)
455 return true;
456
457 return false;
458}
Trond Myklebust65de8722008-12-23 15:21:44 -0500459
Trond Myklebust452e9352010-07-31 14:29:06 -0400460static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 spin_lock(&clp->cl_lock);
463 if (time_before(clp->cl_last_renewal,timestamp))
464 clp->cl_last_renewal = timestamp;
465 spin_unlock(&clp->cl_lock);
466}
467
Trond Myklebust452e9352010-07-31 14:29:06 -0400468static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
469{
470 do_renew_lease(server->nfs_client, timestamp);
471}
472
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400473struct nfs4_call_sync_data {
474 const struct nfs_server *seq_server;
475 struct nfs4_sequence_args *seq_args;
476 struct nfs4_sequence_res *seq_res;
477};
478
Chuck Levera9c92d62013-08-09 12:48:18 -0400479static void nfs4_init_sequence(struct nfs4_sequence_args *args,
480 struct nfs4_sequence_res *res, int cache_reply)
481{
482 args->sa_slot = NULL;
483 args->sa_cache_this = cache_reply;
484 args->sa_privileged = 0;
485
486 res->sr_slot = NULL;
487}
488
489static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
490{
491 args->sa_privileged = 1;
492}
493
Peng Taocb04ad22014-06-11 05:24:15 +0800494int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
495 struct nfs4_sequence_args *args,
496 struct nfs4_sequence_res *res,
497 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400498{
Chuck Lever3bd23842013-08-09 12:49:19 -0400499 struct nfs4_slot *slot;
500
501 /* slot already allocated? */
502 if (res->sr_slot != NULL)
503 goto out_start;
504
505 spin_lock(&tbl->slot_tbl_lock);
506 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
507 goto out_sleep;
508
509 slot = nfs4_alloc_slot(tbl);
510 if (IS_ERR(slot)) {
511 if (slot == ERR_PTR(-ENOMEM))
512 task->tk_timeout = HZ >> 2;
513 goto out_sleep;
514 }
515 spin_unlock(&tbl->slot_tbl_lock);
516
517 args->sa_slot = slot;
518 res->sr_slot = slot;
519
520out_start:
521 rpc_call_start(task);
522 return 0;
523
524out_sleep:
525 if (args->sa_privileged)
526 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
527 NULL, RPC_PRIORITY_PRIVILEGED);
528 else
529 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
530 spin_unlock(&tbl->slot_tbl_lock);
531 return -EAGAIN;
532}
Peng Taocb04ad22014-06-11 05:24:15 +0800533EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400534
535static int nfs40_sequence_done(struct rpc_task *task,
536 struct nfs4_sequence_res *res)
537{
538 struct nfs4_slot *slot = res->sr_slot;
539 struct nfs4_slot_table *tbl;
540
Trond Myklebustcab92c12014-01-29 12:12:15 -0500541 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400542 goto out;
543
544 tbl = slot->table;
545 spin_lock(&tbl->slot_tbl_lock);
546 if (!nfs41_wake_and_assign_slot(tbl, slot))
547 nfs4_free_slot(tbl, slot);
548 spin_unlock(&tbl->slot_tbl_lock);
549
550 res->sr_slot = NULL;
551out:
552 return 1;
553}
554
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400555#if defined(CONFIG_NFS_V4_1)
556
Trond Myklebustd185a332010-06-16 09:52:25 -0400557static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400558{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500559 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400560 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500561 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500562 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400563
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500564 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500565 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500566
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500567 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500568 /* Be nice to the server: try to ensure that the last transmitted
569 * value for highest_user_slotid <= target_highest_slotid
570 */
571 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
572 send_new_highest_used_slotid = true;
573
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500574 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500575 send_new_highest_used_slotid = false;
576 goto out_unlock;
577 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500578 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500579
580 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
581 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500582out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500583 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400584 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500585 if (send_new_highest_used_slotid)
586 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400587}
588
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500589int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400590{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500591 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500592 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400593 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500594 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500595 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400596
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500597 if (slot == NULL)
598 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400599 /* don't increment the sequence number if the task wasn't sent */
600 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400601 goto out;
602
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500603 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500604
Trond Myklebustac20d162012-12-15 15:36:07 -0500605 if (slot->interrupted) {
606 slot->interrupted = 0;
607 interrupted = true;
608 }
609
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400610 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500611 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400612 switch (res->sr_status) {
613 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400614 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500615 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500616 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500617 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500618 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500619 if (res->sr_status_flags != 0)
620 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500621 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400622 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500623 case 1:
624 /*
625 * sr_status remains 1 if an RPC level error occurred.
626 * The server may or may not have processed the sequence
627 * operation..
628 * Mark the slot as having hosted an interrupted RPC call.
629 */
630 slot->interrupted = 1;
631 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400632 case -NFS4ERR_DELAY:
633 /* The server detected a resend of the RPC call and
634 * returned NFS4ERR_DELAY as per Section 2.10.6.2
635 * of RFC5661.
636 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500637 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400638 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500639 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500640 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400641 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500642 case -NFS4ERR_BADSLOT:
643 /*
644 * The slot id we used was probably retired. Try again
645 * using a different slot id.
646 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500647 goto retry_nowait;
648 case -NFS4ERR_SEQ_MISORDERED:
649 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500650 * Was the last operation on this sequence interrupted?
651 * If so, retry after bumping the sequence number.
652 */
653 if (interrupted) {
654 ++slot->seq_nr;
655 goto retry_nowait;
656 }
657 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500658 * Could this slot have been previously retired?
659 * If so, then the server may be expecting seq_nr = 1!
660 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500661 if (slot->seq_nr != 1) {
662 slot->seq_nr = 1;
663 goto retry_nowait;
664 }
665 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500666 case -NFS4ERR_SEQ_FALSE_RETRY:
667 ++slot->seq_nr;
668 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400669 default:
670 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500671 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400672 }
673out:
674 /* The session may be reset by one of the error handlers. */
675 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400676 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500677out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500678 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500679retry_nowait:
680 if (rpc_restart_call_prepare(task)) {
681 task->tk_status = 0;
682 ret = 0;
683 }
684 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400685out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400686 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400687 goto out;
688 rpc_delay(task, NFS4_POLL_RETRY_MAX);
689 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400690}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500691EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400692
Peng Tao2c4b1312014-06-11 05:24:16 +0800693int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400694{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500695 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400696 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400697 if (!res->sr_slot->table->session)
698 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400699 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400700}
Peng Tao2c4b1312014-06-11 05:24:16 +0800701EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400702
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000703int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704 struct nfs4_sequence_args *args,
705 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 struct rpc_task *task)
707{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400708 struct nfs4_slot *slot;
709 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400710
711 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400712 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400713 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400714 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400715
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400716 tbl = &session->fc_slot_table;
717
Trond Myklebust69d206b2012-11-22 13:21:02 -0500718 task->tk_timeout = 0;
719
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400720 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400721 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400722 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500723 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500724 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400725 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400726 }
727
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500728 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500729 if (IS_ERR(slot)) {
730 /* If out of memory, try again in 1/4 second */
731 if (slot == ERR_PTR(-ENOMEM))
732 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400733 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400734 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400735 }
736 spin_unlock(&tbl->slot_tbl_lock);
737
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500738 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400739
Chuck Levere8d92382013-08-09 12:47:51 -0400740 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500741 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400742
Benny Halevydfb4f3092010-09-24 09:17:01 -0400743 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500744 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400745 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400746 /*
747 * sr_status is only set in decode_sequence, and so will remain
748 * set to 1 if an rpc level failure occurs.
749 */
750 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400751 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400752out_success:
753 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400754 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400755out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400756 /* Privileged tasks are queued with top priority */
757 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400758 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
759 NULL, RPC_PRIORITY_PRIVILEGED);
760 else
761 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400762 spin_unlock(&tbl->slot_tbl_lock);
763 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400764}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000765EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400766
Chuck Lever5a580e02013-08-09 12:48:09 -0400767static int nfs4_setup_sequence(const struct nfs_server *server,
768 struct nfs4_sequence_args *args,
769 struct nfs4_sequence_res *res,
770 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400771{
Trond Myklebust035168a2010-06-16 09:52:26 -0400772 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400773 int ret = 0;
774
Chuck Lever3bd23842013-08-09 12:49:19 -0400775 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800776 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
777 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400778
Chuck Levere8d92382013-08-09 12:47:51 -0400779 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400780 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400781 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400782
Trond Myklebust9d12b212012-01-17 22:04:25 -0500783 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400784
Andy Adamsonce5039c2009-04-01 09:22:13 -0400785 dprintk("<-- %s status=%d\n", __func__, ret);
786 return ret;
787}
788
Andy Adamsonce5039c2009-04-01 09:22:13 -0400789static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
790{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400791 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400792 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400793
Trond Myklebust035168a2010-06-16 09:52:26 -0400794 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
795
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400796 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400797}
798
Andy Adamson69ab40c2009-04-01 09:22:19 -0400799static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
800{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400801 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400802
Trond Myklebust14516c32010-07-31 14:29:06 -0400803 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804}
805
Trond Myklebust17280172012-03-11 13:11:00 -0400806static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400807 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400808 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809};
810
Chuck Lever3bd23842013-08-09 12:49:19 -0400811#else /* !CONFIG_NFS_V4_1 */
812
Chuck Lever5a580e02013-08-09 12:48:09 -0400813static int nfs4_setup_sequence(const struct nfs_server *server,
814 struct nfs4_sequence_args *args,
815 struct nfs4_sequence_res *res,
816 struct rpc_task *task)
817{
Peng Taocb04ad22014-06-11 05:24:15 +0800818 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
819 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400820}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400821
Peng Tao2c4b1312014-06-11 05:24:16 +0800822int nfs4_sequence_done(struct rpc_task *task,
823 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400824{
Chuck Lever3bd23842013-08-09 12:49:19 -0400825 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400826}
Peng Tao2c4b1312014-06-11 05:24:16 +0800827EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400828
829#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400830
Chuck Lever9915ea72013-08-09 12:48:27 -0400831static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
832{
833 struct nfs4_call_sync_data *data = calldata;
834 nfs4_setup_sequence(data->seq_server,
835 data->seq_args, data->seq_res, task);
836}
837
838static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
839{
840 struct nfs4_call_sync_data *data = calldata;
841 nfs4_sequence_done(task, data->seq_res);
842}
843
844static const struct rpc_call_ops nfs40_call_sync_ops = {
845 .rpc_call_prepare = nfs40_call_sync_prepare,
846 .rpc_call_done = nfs40_call_sync_done,
847};
848
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400849static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400850 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 struct nfs4_sequence_args *args,
853 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400854{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 int ret;
856 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400857 struct nfs_client *clp = server->nfs_client;
858 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 .seq_server = server,
860 .seq_args = args,
861 .seq_res = res,
862 };
863 struct rpc_task_setup task_setup = {
864 .rpc_client = clnt,
865 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400866 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867 .callback_data = &data
868 };
869
870 task = rpc_run_task(&task_setup);
871 if (IS_ERR(task))
872 ret = PTR_ERR(task);
873 else {
874 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400875 rpc_put_task(task);
876 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100877 return ret;
878}
879
Bryan Schumaker7c513052011-03-24 17:12:24 +0000880int nfs4_call_sync(struct rpc_clnt *clnt,
881 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000882 struct rpc_message *msg,
883 struct nfs4_sequence_args *args,
884 struct nfs4_sequence_res *res,
885 int cache_reply)
886{
Chuck Levera9c92d62013-08-09 12:48:18 -0400887 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400888 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000889}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
892{
893 struct nfs_inode *nfsi = NFS_I(dir);
894
895 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400896 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700897 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700899 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500900 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000901 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 spin_unlock(&dir->i_lock);
903}
904
905struct nfs4_opendata {
906 struct kref kref;
907 struct nfs_openargs o_arg;
908 struct nfs_openres o_res;
909 struct nfs_open_confirmargs c_arg;
910 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500911 struct nfs4_string owner_name;
912 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400914 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400916 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct nfs4_state_owner *owner;
918 struct nfs4_state *state;
919 struct iattr attrs;
920 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400921 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400922 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400923 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400924 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int cancelled;
926};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400927
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400928static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
929 int err, struct nfs4_exception *exception)
930{
931 if (err != -EINVAL)
932 return false;
933 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
934 return false;
935 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
936 exception->retry = 1;
937 return true;
938}
939
Trond Myklebust6ae37332015-01-30 14:21:14 -0500940static u32
941nfs4_map_atomic_open_share(struct nfs_server *server,
942 fmode_t fmode, int openflags)
943{
944 u32 res = 0;
945
946 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
947 case FMODE_READ:
948 res = NFS4_SHARE_ACCESS_READ;
949 break;
950 case FMODE_WRITE:
951 res = NFS4_SHARE_ACCESS_WRITE;
952 break;
953 case FMODE_READ|FMODE_WRITE:
954 res = NFS4_SHARE_ACCESS_BOTH;
955 }
956 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
957 goto out;
958 /* Want no delegation if we're using O_DIRECT */
959 if (openflags & O_DIRECT)
960 res |= NFS4_SHARE_WANT_NO_DELEG;
961out:
962 return res;
963}
964
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400965static enum open_claim_type4
966nfs4_map_atomic_open_claim(struct nfs_server *server,
967 enum open_claim_type4 claim)
968{
969 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
970 return claim;
971 switch (claim) {
972 default:
973 return claim;
974 case NFS4_OPEN_CLAIM_FH:
975 return NFS4_OPEN_CLAIM_NULL;
976 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
977 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
978 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
979 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
980 }
981}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100984{
985 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400986 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100987 p->o_res.seqid = p->o_arg.seqid;
988 p->c_res.seqid = p->c_arg.seqid;
989 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400990 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100991 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500992 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100993}
994
Al Viro82a2c1b2011-06-22 18:30:55 -0400995static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100996 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400997 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400998 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400999 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001000 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001001{
Al Viro82a2c1b2011-06-22 18:30:55 -04001002 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001003 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001004 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001005 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 struct nfs4_opendata *p;
1007
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001008 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001009 if (p == NULL)
1010 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001011
1012 p->f_label = nfs4_label_alloc(server, gfp_mask);
1013 if (IS_ERR(p->f_label))
1014 goto err_free_p;
1015
Trond Myklebust63f5f792015-01-23 19:19:25 -05001016 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1017 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001018 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001019 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001020 nfs_sb_active(dentry->d_sb);
1021 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001022 p->dir = parent;
1023 p->owner = sp;
1024 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 p->o_arg.open_flags = flags;
1026 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001027 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1028 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001029 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1030 * will return permission denied for all bits until close */
1031 if (!(flags & O_EXCL)) {
1032 /* ask server to check for all possible rights as results
1033 * are cached */
1034 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1035 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001036 }
David Howells7539bba2006-08-22 20:06:09 -04001037 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001038 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1039 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001040 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001041 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001042 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001043 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001044 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001045 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1046 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001047 case NFS4_OPEN_CLAIM_NULL:
1048 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1049 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1050 p->o_arg.fh = NFS_FH(dir);
1051 break;
1052 case NFS4_OPEN_CLAIM_PREVIOUS:
1053 case NFS4_OPEN_CLAIM_FH:
1054 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1055 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001056 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001057 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001058 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001059 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001060
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001061 p->o_arg.u.attrs = &p->attrs;
1062 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001063
1064 verf[0] = jiffies;
1065 verf[1] = current->pid;
1066 memcpy(p->o_arg.u.verifier.data, verf,
1067 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001068 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001069 p->c_arg.fh = &p->o_res.fh;
1070 p->c_arg.stateid = &p->o_res.stateid;
1071 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001072 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001073 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001074 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001075
1076err_free_label:
1077 nfs4_label_free(p->f_label);
1078err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001079 kfree(p);
1080err:
1081 dput(parent);
1082 return NULL;
1083}
1084
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001085static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001086{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001087 struct nfs4_opendata *p = container_of(kref,
1088 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001089 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001090
1091 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001092 if (p->state != NULL)
1093 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001094 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001095
1096 nfs4_label_free(p->f_label);
1097
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001098 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001099 dput(p->dentry);
1100 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001101 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001102 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001103 kfree(p);
1104}
1105
1106static void nfs4_opendata_put(struct nfs4_opendata *p)
1107{
1108 if (p != NULL)
1109 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110}
1111
Trond Myklebust06f814a2006-01-03 09:55:07 +01001112static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1113{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001114 int ret;
1115
Trond Myklebust06f814a2006-01-03 09:55:07 +01001116 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001117 return ret;
1118}
1119
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001121{
1122 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001123
Trond Myklebust536e43d2012-01-17 22:04:26 -05001124 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125 goto out;
1126 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001127 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001128 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1129 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001130 break;
1131 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001132 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1133 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001134 break;
1135 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001136 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1137 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001138 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001140 return ret;
1141}
1142
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001144{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001145 if (delegation == NULL)
1146 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001147 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001148 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001149 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1150 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001151 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001152 return 1;
1153}
1154
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001156{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001158 case FMODE_WRITE:
1159 state->n_wronly++;
1160 break;
1161 case FMODE_READ:
1162 state->n_rdonly++;
1163 break;
1164 case FMODE_READ|FMODE_WRITE:
1165 state->n_rdwr++;
1166 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001168}
1169
Trond Myklebust4f14c192014-02-12 19:15:06 -05001170static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001171{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001172 struct nfs_client *clp = state->owner->so_server->nfs_client;
1173 bool need_recover = false;
1174
1175 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1176 need_recover = true;
1177 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1178 need_recover = true;
1179 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1180 need_recover = true;
1181 if (need_recover)
1182 nfs4_state_mark_reclaim_nograce(clp, state);
1183}
1184
Trond Myklebuste999e802014-02-10 18:20:47 -05001185static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1186 nfs4_stateid *stateid)
1187{
1188 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1189 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001190 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1191 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001192 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001193 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001194 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1195 return true;
1196 return false;
1197}
1198
Trond Myklebustf95549c2015-01-23 18:06:09 -05001199static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1200{
1201 if (state->n_wronly)
1202 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1203 if (state->n_rdonly)
1204 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1205 if (state->n_rdwr)
1206 set_bit(NFS_O_RDWR_STATE, &state->flags);
1207}
1208
Trond Myklebust226056c2014-02-11 10:41:07 -05001209static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1210 nfs4_stateid *stateid, fmode_t fmode)
1211{
1212 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1213 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1214 case FMODE_WRITE:
1215 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1216 break;
1217 case FMODE_READ:
1218 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1219 break;
1220 case 0:
1221 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1222 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1223 clear_bit(NFS_OPEN_STATE, &state->flags);
1224 }
1225 if (stateid == NULL)
1226 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001227 /* Handle races with OPEN */
1228 if (!nfs4_stateid_match_other(stateid, &state->open_stateid) ||
1229 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1230 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001231 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001232 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001233 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001234 nfs4_stateid_copy(&state->stateid, stateid);
1235 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001236}
1237
1238static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1239{
1240 write_seqlock(&state->seqlock);
1241 nfs_clear_open_stateid_locked(state, stateid, fmode);
1242 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001243 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1244 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001245}
1246
Trond Myklebust003707c2007-07-05 18:07:55 -04001247static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1248{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001249 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001250 case FMODE_READ:
1251 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1252 break;
1253 case FMODE_WRITE:
1254 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1255 break;
1256 case FMODE_READ|FMODE_WRITE:
1257 set_bit(NFS_O_RDWR_STATE, &state->flags);
1258 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001259 if (!nfs_need_update_open_stateid(state, stateid))
1260 return;
1261 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1262 nfs4_stateid_copy(&state->stateid, stateid);
1263 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001264}
1265
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001266static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001268 /*
1269 * Protect the call to nfs4_state_set_mode_locked and
1270 * serialise the stateid update
1271 */
1272 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001273 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001274 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001275 set_bit(NFS_DELEGATED_STATE, &state->flags);
1276 }
1277 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001279 write_sequnlock(&state->seqlock);
1280 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001281 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001282 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001285static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001286{
1287 struct nfs_inode *nfsi = NFS_I(state->inode);
1288 struct nfs_delegation *deleg_cur;
1289 int ret = 0;
1290
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001292
1293 rcu_read_lock();
1294 deleg_cur = rcu_dereference(nfsi->delegation);
1295 if (deleg_cur == NULL)
1296 goto no_delegation;
1297
1298 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001299 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001300 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001301 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001302 goto no_delegation_unlock;
1303
1304 if (delegation == NULL)
1305 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001306 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001307 goto no_delegation_unlock;
1308
Trond Myklebustb7391f42008-12-23 15:21:52 -05001309 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001310 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001311 ret = 1;
1312no_delegation_unlock:
1313 spin_unlock(&deleg_cur->lock);
1314no_delegation:
1315 rcu_read_unlock();
1316
1317 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001318 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001319 ret = 1;
1320 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001321 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1322 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001323
1324 return ret;
1325}
1326
Trond Myklebust39071e62015-01-24 15:07:56 -05001327static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1328 const nfs4_stateid *stateid)
1329{
1330 struct nfs4_state *state = lsp->ls_state;
1331 bool ret = false;
1332
1333 spin_lock(&state->state_lock);
1334 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1335 goto out_noupdate;
1336 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1337 goto out_noupdate;
1338 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1339 ret = true;
1340out_noupdate:
1341 spin_unlock(&state->state_lock);
1342 return ret;
1343}
Trond Myklebust34310432008-12-23 15:21:38 -05001344
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001345static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001346{
1347 struct nfs_delegation *delegation;
1348
1349 rcu_read_lock();
1350 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001352 rcu_read_unlock();
1353 return;
1354 }
1355 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001356 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001357}
1358
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001360{
1361 struct nfs4_state *state = opendata->state;
1362 struct nfs_inode *nfsi = NFS_I(state->inode);
1363 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001364 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001365 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001366 nfs4_stateid stateid;
1367 int ret = -EAGAIN;
1368
Trond Myklebustaac00a82007-07-05 19:02:21 -04001369 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001370 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001371 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001372 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001373 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001374 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001375 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001376 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001377 rcu_read_lock();
1378 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001379 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001380 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001381 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001382 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001383 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001384 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001385 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001386 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001387 if (!opendata->is_recover) {
1388 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1389 if (ret != 0)
1390 goto out;
1391 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001392 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001393
1394 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001395 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001396 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001397 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001398out:
1399 return ERR_PTR(ret);
1400out_return_state:
1401 atomic_inc(&state->count);
1402 return state;
1403}
1404
Andy Adamsone23008e2012-10-02 21:07:32 -04001405static void
1406nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1407{
1408 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1409 struct nfs_delegation *delegation;
1410 int delegation_flags = 0;
1411
1412 rcu_read_lock();
1413 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1414 if (delegation)
1415 delegation_flags = delegation->flags;
1416 rcu_read_unlock();
1417 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1418 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1419 "returning a delegation for "
1420 "OPEN(CLAIM_DELEGATE_CUR)\n",
1421 clp->cl_hostname);
1422 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1423 nfs_inode_set_delegation(state->inode,
1424 data->owner->so_cred,
1425 &data->o_res);
1426 else
1427 nfs_inode_reclaim_delegation(state->inode,
1428 data->owner->so_cred,
1429 &data->o_res);
1430}
1431
1432/*
1433 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1434 * and update the nfs4_state.
1435 */
1436static struct nfs4_state *
1437_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1438{
1439 struct inode *inode = data->state->inode;
1440 struct nfs4_state *state = data->state;
1441 int ret;
1442
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001443 if (!data->rpc_done) {
1444 if (data->rpc_status) {
1445 ret = data->rpc_status;
1446 goto err;
1447 }
1448 /* cached opens have already been processed */
1449 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001450 }
1451
Andy Adamsone23008e2012-10-02 21:07:32 -04001452 ret = nfs_refresh_inode(inode, &data->f_attr);
1453 if (ret)
1454 goto err;
1455
1456 if (data->o_res.delegation_type != 0)
1457 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001458update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001459 update_open_stateid(state, &data->o_res.stateid, NULL,
1460 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001461 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001462
1463 return state;
1464err:
1465 return ERR_PTR(ret);
1466
1467}
1468
1469static struct nfs4_state *
1470_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471{
1472 struct inode *inode;
1473 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001474 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475
Trond Myklebustaac00a82007-07-05 19:02:21 -04001476 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001477 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001478 goto out;
1479 }
1480
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001481 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001483 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001484 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001485 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001486 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001487 goto err;
1488 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489 state = nfs4_get_open_state(inode, data->owner);
1490 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001491 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001492 if (data->o_res.delegation_type != 0)
1493 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001494 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001495 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001497out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001498 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001499 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001500err_put_inode:
1501 iput(inode);
1502err:
1503 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504}
1505
Andy Adamsone23008e2012-10-02 21:07:32 -04001506static struct nfs4_state *
1507nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1508{
1509 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1510 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1511 return _nfs4_opendata_to_nfs4_state(data);
1512}
1513
Trond Myklebust864472e2006-01-03 09:55:15 +01001514static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1515{
1516 struct nfs_inode *nfsi = NFS_I(state->inode);
1517 struct nfs_open_context *ctx;
1518
1519 spin_lock(&state->inode->i_lock);
1520 list_for_each_entry(ctx, &nfsi->open_files, list) {
1521 if (ctx->state != state)
1522 continue;
1523 get_nfs_open_context(ctx);
1524 spin_unlock(&state->inode->i_lock);
1525 return ctx;
1526 }
1527 spin_unlock(&state->inode->i_lock);
1528 return ERR_PTR(-ENOENT);
1529}
1530
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001531static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1532 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001533{
1534 struct nfs4_opendata *opendata;
1535
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001536 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001537 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001538 if (opendata == NULL)
1539 return ERR_PTR(-ENOMEM);
1540 opendata->state = state;
1541 atomic_inc(&state->count);
1542 return opendata;
1543}
1544
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001545static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001546{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001547 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001548 int ret;
1549
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001550 opendata->o_arg.open_flags = 0;
1551 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001552 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1553 NFS_SB(opendata->dentry->d_sb),
1554 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001555 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1556 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1557 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001558 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001559 if (ret != 0)
1560 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001561 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001562 if (IS_ERR(newstate))
1563 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001564 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001565 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001566 return 0;
1567}
1568
1569static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1570{
Trond Myklebust864472e2006-01-03 09:55:15 +01001571 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001572 int ret;
1573
Trond Myklebust4f14c192014-02-12 19:15:06 -05001574 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1575 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1576 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1577 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001578 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001579 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001580 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001581 smp_rmb();
1582 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001583 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001584 if (ret != 0)
1585 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001586 if (newstate != state)
1587 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001588 }
1589 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001590 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001591 if (ret != 0)
1592 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001593 if (newstate != state)
1594 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001595 }
1596 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001597 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001598 if (ret != 0)
1599 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001600 if (newstate != state)
1601 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001602 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001603 /*
1604 * We may have performed cached opens for all three recoveries.
1605 * Check if we need to update the current stateid.
1606 */
1607 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001608 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001609 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001610 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001611 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001612 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001613 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001614 return 0;
1615}
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617/*
1618 * OPEN_RECLAIM:
1619 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001621static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001623 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001624 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001625 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int status;
1627
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001628 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1629 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001630 if (IS_ERR(opendata))
1631 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001632 rcu_read_lock();
1633 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001634 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001635 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001636 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001637 opendata->o_arg.u.delegation_type = delegation_type;
1638 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001639 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return status;
1641}
1642
Trond Myklebust539cd032007-06-05 11:46:42 -04001643static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 struct nfs_server *server = NFS_SERVER(state->inode);
1646 struct nfs4_exception exception = { };
1647 int err;
1648 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001649 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001650 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001651 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1652 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001653 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001654 break;
1655 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 } while (exception.retry);
1657 return err;
1658}
1659
Trond Myklebust864472e2006-01-03 09:55:15 +01001660static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1661{
1662 struct nfs_open_context *ctx;
1663 int ret;
1664
1665 ctx = nfs4_state_find_open_context(state);
1666 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001667 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001668 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001669 put_nfs_open_context(ctx);
1670 return ret;
1671}
1672
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001673static 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 -07001674{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001675 switch (err) {
1676 default:
1677 printk(KERN_ERR "NFS: %s: unhandled error "
1678 "%d.\n", __func__, err);
1679 case 0:
1680 case -ENOENT:
1681 case -ESTALE:
1682 break;
1683 case -NFS4ERR_BADSESSION:
1684 case -NFS4ERR_BADSLOT:
1685 case -NFS4ERR_BAD_HIGH_SLOT:
1686 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1687 case -NFS4ERR_DEADSESSION:
1688 set_bit(NFS_DELEGATED_STATE, &state->flags);
1689 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1690 return -EAGAIN;
1691 case -NFS4ERR_STALE_CLIENTID:
1692 case -NFS4ERR_STALE_STATEID:
1693 set_bit(NFS_DELEGATED_STATE, &state->flags);
1694 case -NFS4ERR_EXPIRED:
1695 /* Don't recall a delegation if it was lost */
1696 nfs4_schedule_lease_recovery(server->nfs_client);
1697 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001698 case -NFS4ERR_MOVED:
1699 nfs4_schedule_migration_recovery(server);
1700 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001701 case -NFS4ERR_LEASE_MOVED:
1702 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1703 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001704 case -NFS4ERR_DELEG_REVOKED:
1705 case -NFS4ERR_ADMIN_REVOKED:
1706 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001707 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001708 nfs_inode_find_state_and_recover(state->inode,
1709 stateid);
1710 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001711 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001712 case -NFS4ERR_DELAY:
1713 case -NFS4ERR_GRACE:
1714 set_bit(NFS_DELEGATED_STATE, &state->flags);
1715 ssleep(1);
1716 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001717 case -ENOMEM:
1718 case -NFS4ERR_DENIED:
1719 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1720 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return err;
1723}
1724
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001725int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1726{
1727 struct nfs_server *server = NFS_SERVER(state->inode);
1728 struct nfs4_opendata *opendata;
1729 int err;
1730
1731 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1732 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1733 if (IS_ERR(opendata))
1734 return PTR_ERR(opendata);
1735 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1736 err = nfs4_open_recover(opendata, state);
1737 nfs4_opendata_put(opendata);
1738 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1739}
1740
Chuck Leverbe05c862013-08-09 12:49:47 -04001741static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1742{
1743 struct nfs4_opendata *data = calldata;
1744
Peng Taocb04ad22014-06-11 05:24:15 +08001745 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1746 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001747}
1748
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001749static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1750{
1751 struct nfs4_opendata *data = calldata;
1752
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001753 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001754
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001755 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001756 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001757 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001758 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001759 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001760 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001761 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001762}
1763
1764static void nfs4_open_confirm_release(void *calldata)
1765{
1766 struct nfs4_opendata *data = calldata;
1767 struct nfs4_state *state = NULL;
1768
1769 /* If this request hasn't been cancelled, do nothing */
1770 if (data->cancelled == 0)
1771 goto out_free;
1772 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001773 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001774 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001775 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001776 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001777 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001778out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001779 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001780}
1781
1782static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001783 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001784 .rpc_call_done = nfs4_open_confirm_done,
1785 .rpc_release = nfs4_open_confirm_release,
1786};
1787
1788/*
1789 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1790 */
1791static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1792{
David Howells2b0143b2015-03-17 22:25:59 +00001793 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001794 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001795 struct rpc_message msg = {
1796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1797 .rpc_argp = &data->c_arg,
1798 .rpc_resp = &data->c_res,
1799 .rpc_cred = data->owner->so_cred,
1800 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001801 struct rpc_task_setup task_setup_data = {
1802 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001803 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001804 .callback_ops = &nfs4_open_confirm_ops,
1805 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001806 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001807 .flags = RPC_TASK_ASYNC,
1808 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 int status;
1810
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001811 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001812 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001813 data->rpc_done = 0;
1814 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001815 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001816 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001817 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001818 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001819 status = nfs4_wait_for_completion_rpc_task(task);
1820 if (status != 0) {
1821 data->cancelled = 1;
1822 smp_wmb();
1823 } else
1824 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001825 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return status;
1827}
1828
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001829static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001831 struct nfs4_opendata *data = calldata;
1832 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001833 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001834
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001835 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001836 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001837 /*
1838 * Check if we still need to send an OPEN call, or if we can use
1839 * a delegation instead.
1840 */
1841 if (data->state != NULL) {
1842 struct nfs_delegation *delegation;
1843
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001844 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001845 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001846 rcu_read_lock();
1847 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001848 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001849 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001850 can_open_delegated(delegation, data->o_arg.fmode))
1851 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001852 rcu_read_unlock();
1853 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001854 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001855 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001856 switch (data->o_arg.claim) {
1857 case NFS4_OPEN_CLAIM_PREVIOUS:
1858 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1859 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001860 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001861 case NFS4_OPEN_CLAIM_FH:
1862 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001863 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1864 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001865 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001866 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001867 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001868 &data->o_res.seq_res,
1869 task) != 0)
1870 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001871
1872 /* Set the create mode (note dependency on the session type) */
1873 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1874 if (data->o_arg.open_flags & O_EXCL) {
1875 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1876 if (nfs4_has_persistent_session(clp))
1877 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1878 else if (clp->cl_mvops->minor_version > 0)
1879 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1880 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001881 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001882unlock_no_action:
1883 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001884out_no_action:
1885 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001886out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001887 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001888}
1889
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001890static void nfs4_open_done(struct rpc_task *task, void *calldata)
1891{
1892 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001894 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001895
Trond Myklebust14516c32010-07-31 14:29:06 -04001896 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1897 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001898
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001899 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001900 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1901 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001902 case S_IFREG:
1903 break;
1904 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001905 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001906 break;
1907 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001908 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001909 break;
1910 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001911 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001912 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001913 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001914 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001915 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1916 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001917 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001918 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001919}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001920
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001921static void nfs4_open_release(void *calldata)
1922{
1923 struct nfs4_opendata *data = calldata;
1924 struct nfs4_state *state = NULL;
1925
1926 /* If this request hasn't been cancelled, do nothing */
1927 if (data->cancelled == 0)
1928 goto out_free;
1929 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001930 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001931 goto out_free;
1932 /* In case we need an open_confirm, no cleanup! */
1933 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1934 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001935 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001936 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001937 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001938out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001939 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001940}
1941
1942static const struct rpc_call_ops nfs4_open_ops = {
1943 .rpc_call_prepare = nfs4_open_prepare,
1944 .rpc_call_done = nfs4_open_done,
1945 .rpc_release = nfs4_open_release,
1946};
1947
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001948static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001949{
David Howells2b0143b2015-03-17 22:25:59 +00001950 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001951 struct nfs_server *server = NFS_SERVER(dir);
1952 struct nfs_openargs *o_arg = &data->o_arg;
1953 struct nfs_openres *o_res = &data->o_res;
1954 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001955 struct rpc_message msg = {
1956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1957 .rpc_argp = o_arg,
1958 .rpc_resp = o_res,
1959 .rpc_cred = data->owner->so_cred,
1960 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001961 struct rpc_task_setup task_setup_data = {
1962 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001963 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001964 .callback_ops = &nfs4_open_ops,
1965 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001966 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001967 .flags = RPC_TASK_ASYNC,
1968 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001969 int status;
1970
Chuck Levera9c92d62013-08-09 12:48:18 -04001971 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001972 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001973 data->rpc_done = 0;
1974 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001975 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001976 data->is_recover = 0;
1977 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001978 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001979 data->is_recover = 1;
1980 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001981 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001982 if (IS_ERR(task))
1983 return PTR_ERR(task);
1984 status = nfs4_wait_for_completion_rpc_task(task);
1985 if (status != 0) {
1986 data->cancelled = 1;
1987 smp_wmb();
1988 } else
1989 status = data->rpc_status;
1990 rpc_put_task(task);
1991
1992 return status;
1993}
1994
1995static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1996{
David Howells2b0143b2015-03-17 22:25:59 +00001997 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001998 struct nfs_openres *o_res = &data->o_res;
1999 int status;
2000
2001 status = nfs4_run_open_task(data, 1);
2002 if (status != 0 || !data->rpc_done)
2003 return status;
2004
Trond Myklebust6926afd2012-01-07 13:22:46 -05002005 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2006
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002007 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2008 status = _nfs4_proc_open_confirm(data);
2009 if (status != 0)
2010 return status;
2011 }
2012
2013 return status;
2014}
2015
Trond Myklebustf3792d62014-07-10 08:54:32 -04002016/*
2017 * Additional permission checks in order to distinguish between an
2018 * open for read, and an open for execute. This works around the
2019 * fact that NFSv4 OPEN treats read and execute permissions as being
2020 * the same.
2021 * Note that in the non-execute case, we want to turn off permission
2022 * checking if we just created a new file (POSIX open() semantics).
2023 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002024static int nfs4_opendata_access(struct rpc_cred *cred,
2025 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002026 struct nfs4_state *state, fmode_t fmode,
2027 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002028{
2029 struct nfs_access_entry cache;
2030 u32 mask;
2031
2032 /* access call failed or for some reason the server doesn't
2033 * support any access modes -- defer access call until later */
2034 if (opendata->o_res.access_supported == 0)
2035 return 0;
2036
2037 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002038 /*
2039 * Use openflags to check for exec, because fmode won't
2040 * always have FMODE_EXEC set when file open for exec.
2041 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002042 if (openflags & __FMODE_EXEC) {
2043 /* ONLY check for exec rights */
2044 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002045 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002046 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002047
2048 cache.cred = cred;
2049 cache.jiffies = jiffies;
2050 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2051 nfs_access_add_cache(state->inode, &cache);
2052
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002053 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002054 return 0;
2055
2056 /* even though OPEN succeeded, access is denied. Close the file */
2057 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002058 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002059}
2060
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002061/*
2062 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2063 */
2064static int _nfs4_proc_open(struct nfs4_opendata *data)
2065{
David Howells2b0143b2015-03-17 22:25:59 +00002066 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002067 struct nfs_server *server = NFS_SERVER(dir);
2068 struct nfs_openargs *o_arg = &data->o_arg;
2069 struct nfs_openres *o_res = &data->o_res;
2070 int status;
2071
2072 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002073 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002074 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002075 if (status != 0) {
2076 if (status == -NFS4ERR_BADNAME &&
2077 !(o_arg->open_flags & O_CREAT))
2078 return -ENOENT;
2079 return status;
2080 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002081
Trond Myklebust6926afd2012-01-07 13:22:46 -05002082 nfs_fattr_map_and_free_names(server, &data->f_attr);
2083
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002084 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002085 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002086 if (o_arg->open_flags & O_EXCL)
2087 data->file_created = 1;
2088 else if (o_res->cinfo.before != o_res->cinfo.after)
2089 data->file_created = 1;
2090 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002091 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2092 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002094 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002096 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002099 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Andy Adamsond83217c2011-03-01 01:34:17 +00002103static int nfs4_recover_expired_lease(struct nfs_server *server)
2104{
2105 return nfs4_client_recover_expired_lease(server->nfs_client);
2106}
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108/*
2109 * OPEN_EXPIRED:
2110 * reclaim state on the server after a network partition.
2111 * Assumes caller holds the appropriate lock
2112 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002113static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002115 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002116 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002118 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002119 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002120 if (IS_ERR(opendata))
2121 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002122 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002123 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002124 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002125 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002129static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002130{
Trond Myklebust539cd032007-06-05 11:46:42 -04002131 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002132 struct nfs4_exception exception = { };
2133 int err;
2134
2135 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002136 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002137 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002138 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2139 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002140 switch (err) {
2141 default:
2142 goto out;
2143 case -NFS4ERR_GRACE:
2144 case -NFS4ERR_DELAY:
2145 nfs4_handle_exception(server, err, &exception);
2146 err = 0;
2147 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002148 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002149out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002150 return err;
2151}
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002156 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Trond Myklebust864472e2006-01-03 09:55:15 +01002158 ctx = nfs4_state_find_open_context(state);
2159 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002160 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002161 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002162 put_nfs_open_context(ctx);
2163 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002166static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2167{
2168 nfs_remove_bad_delegation(state->inode);
2169 write_seqlock(&state->seqlock);
2170 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2171 write_sequnlock(&state->seqlock);
2172 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2173}
2174
2175static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2176{
2177 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2178 nfs_finish_clear_delegation_stateid(state);
2179}
2180
2181static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2182{
2183 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2184 nfs40_clear_delegation_stateid(state);
2185 return nfs4_open_expired(sp, state);
2186}
2187
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002188#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002189static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002190{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002191 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002192 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002193 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002194 struct rpc_cred *cred;
2195 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002196
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002197 /* Get the delegation credential for use by test/free_stateid */
2198 rcu_read_lock();
2199 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002200 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002201 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002202 return;
2203 }
2204
2205 nfs4_stateid_copy(&stateid, &delegation->stateid);
2206 cred = get_rpccred(delegation->cred);
2207 rcu_read_unlock();
2208 status = nfs41_test_stateid(server, &stateid, cred);
2209 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002210
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002211 if (status != NFS_OK) {
2212 /* Free the stateid unless the server explicitly
2213 * informs us the stateid is unrecognized. */
2214 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002215 nfs41_free_stateid(server, &stateid, cred);
2216 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002217 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002218
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002219 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002220}
2221
2222/**
2223 * nfs41_check_open_stateid - possibly free an open stateid
2224 *
2225 * @state: NFSv4 state for an inode
2226 *
2227 * Returns NFS_OK if recovery for this stateid is now finished.
2228 * Otherwise a negative NFS4ERR value is returned.
2229 */
2230static int nfs41_check_open_stateid(struct nfs4_state *state)
2231{
2232 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002233 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002234 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002235 int status;
2236
2237 /* If a state reset has been done, test_stateid is unneeded */
2238 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2239 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2240 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2241 return -NFS4ERR_BAD_STATEID;
2242
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002243 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002244 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002245 if (status != NFS_OK) {
2246 /* Free the stateid unless the server explicitly
2247 * informs us the stateid is unrecognized. */
2248 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002249 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002250
2251 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2252 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2253 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002254 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002255 }
2256 return status;
2257}
2258
2259static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2260{
Chuck Levereb64cf92012-07-11 16:30:05 -04002261 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002262
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002263 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002264 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002265 if (status != NFS_OK)
2266 status = nfs4_open_expired(sp, state);
2267 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002268}
2269#endif
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002272 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2273 * fields corresponding to attributes that were used to store the verifier.
2274 * Make sure we clobber those fields in the later setattr call
2275 */
2276static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2277{
2278 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2279 !(sattr->ia_valid & ATTR_ATIME_SET))
2280 sattr->ia_valid |= ATTR_ATIME;
2281
2282 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2283 !(sattr->ia_valid & ATTR_MTIME_SET))
2284 sattr->ia_valid |= ATTR_MTIME;
2285}
2286
Trond Myklebustc21443c2013-02-07 14:26:21 -05002287static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2288 fmode_t fmode,
2289 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002290 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002291{
2292 struct nfs4_state_owner *sp = opendata->owner;
2293 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002294 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002295 struct nfs4_state *state;
2296 unsigned int seq;
2297 int ret;
2298
2299 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2300
2301 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002302 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002303 goto out;
2304
2305 state = nfs4_opendata_to_nfs4_state(opendata);
2306 ret = PTR_ERR(state);
2307 if (IS_ERR(state))
2308 goto out;
2309 if (server->caps & NFS_CAP_POSIX_LOCK)
2310 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2311
Trond Myklebust275bb302013-05-29 13:11:28 -04002312 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002313 if (d_really_is_negative(dentry)) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002314 /* FIXME: Is this d_drop() ever needed? */
2315 d_drop(dentry);
2316 dentry = d_add_unique(dentry, igrab(state->inode));
2317 if (dentry == NULL) {
2318 dentry = opendata->dentry;
2319 } else if (dentry != ctx->dentry) {
2320 dput(ctx->dentry);
2321 ctx->dentry = dget(dentry);
2322 }
2323 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002324 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002325 }
2326
Trond Myklebustc21443c2013-02-07 14:26:21 -05002327 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2328 if (ret != 0)
2329 goto out;
2330
Trond Myklebust3efb9722013-05-29 13:17:04 -04002331 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002332 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002333 nfs_inode_attach_open_context(ctx);
2334 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2335 nfs4_schedule_stateid_recovery(server, state);
2336 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002337out:
2338 return ret;
2339}
2340
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002341/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002342 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 */
Andy Adamson82be4172012-05-23 05:02:35 -04002344static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002345 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002346 int flags,
2347 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002348 struct nfs4_label *label,
2349 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 struct nfs4_state_owner *sp;
2352 struct nfs4_state *state = NULL;
2353 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002354 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002355 struct dentry *dentry = ctx->dentry;
2356 struct rpc_cred *cred = ctx->cred;
2357 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2358 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002359 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002360 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002361 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002365 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2366 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2368 goto out_err;
2369 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002370 status = nfs4_recover_expired_lease(server);
2371 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002372 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002373 if (d_really_is_positive(dentry))
2374 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002375 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002376 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002377 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002378 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002379 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002380 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002381 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
David Quigley14c43f72013-05-22 12:50:43 -04002383 if (label) {
2384 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2385 if (IS_ERR(olabel)) {
2386 status = PTR_ERR(olabel);
2387 goto err_opendata_put;
2388 }
2389 }
2390
Trond Myklebuste911b812014-03-26 13:24:37 -07002391 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2392 if (!opendata->f_attr.mdsthreshold) {
2393 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2394 if (!opendata->f_attr.mdsthreshold)
2395 goto err_free_label;
2396 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002397 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002398 }
David Howells2b0143b2015-03-17 22:25:59 +00002399 if (d_really_is_positive(dentry))
2400 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002401
Trond Myklebust3efb9722013-05-29 13:17:04 -04002402 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002403 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002404 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002405 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002406
Trond Myklebust549b19c2013-04-16 18:42:34 -04002407 if ((opendata->o_arg.open_flags & O_EXCL) &&
2408 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002409 nfs4_exclusive_attrset(opendata, sattr);
2410
2411 nfs_fattr_init(opendata->o_res.f_attr);
2412 status = nfs4_do_setattr(state->inode, cred,
2413 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002414 state, label, olabel);
2415 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05002416 nfs_setattr_update_inode(state->inode, sattr,
2417 opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002418 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002419 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002420 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002421 if (opendata->file_created)
2422 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002423
Trond Myklebuste911b812014-03-26 13:24:37 -07002424 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002425 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002426 opendata->f_attr.mdsthreshold = NULL;
2427 }
Andy Adamson82be4172012-05-23 05:02:35 -04002428
David Quigley14c43f72013-05-22 12:50:43 -04002429 nfs4_label_free(olabel);
2430
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002431 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002434err_free_label:
2435 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002436err_opendata_put:
2437 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002438err_put_state_owner:
2439 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return status;
2442}
2443
2444
Andy Adamson82be4172012-05-23 05:02:35 -04002445static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002446 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002447 int flags,
2448 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002449 struct nfs4_label *label,
2450 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002452 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 struct nfs4_exception exception = { };
2454 struct nfs4_state *res;
2455 int status;
2456
2457 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002458 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002459 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002460 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (status == 0)
2462 break;
2463 /* NOTE: BAD_SEQID means the server and client disagree about the
2464 * book-keeping w.r.t. state-changing operations
2465 * (OPEN/CLOSE/LOCK/LOCKU...)
2466 * It is actually a sign of a bug on the client or on the server.
2467 *
2468 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002469 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 * have unhashed the old state_owner for us, and that we can
2471 * therefore safely retry using a new one. We should still warn
2472 * the user though...
2473 */
2474 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002475 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002476 " returned a bad sequence-id error!\n",
2477 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 exception.retry = 1;
2479 continue;
2480 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002481 /*
2482 * BAD_STATEID on OPEN means that the server cancelled our
2483 * state before it received the OPEN_CONFIRM.
2484 * Recover by retrying the request as per the discussion
2485 * on Page 181 of RFC3530.
2486 */
2487 if (status == -NFS4ERR_BAD_STATEID) {
2488 exception.retry = 1;
2489 continue;
2490 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002491 if (status == -EAGAIN) {
2492 /* We must have found a delegation */
2493 exception.retry = 1;
2494 continue;
2495 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002496 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2497 continue;
2498 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 status, &exception));
2500 } while (exception.retry);
2501 return res;
2502}
2503
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002504static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2505 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002506 struct nfs4_state *state, struct nfs4_label *ilabel,
2507 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002509 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002511 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 .iap = sattr,
2513 .server = server,
2514 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002515 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 };
2517 struct nfs_setattrres res = {
2518 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002519 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 .server = server,
2521 };
2522 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2524 .rpc_argp = &arg,
2525 .rpc_resp = &res,
2526 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002528 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002529 fmode_t fmode;
2530 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002531 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
David Quigleyaa9c2662013-05-22 12:50:44 -04002533 arg.bitmask = nfs4_bitmask(server, ilabel);
2534 if (ilabel)
2535 arg.bitmask = nfs4_bitmask(server, olabel);
2536
Trond Myklebust0e574af2005-10-27 22:12:38 -04002537 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Trond Myklebustee3ae842013-04-29 10:35:36 -04002539 /* Servers should only apply open mode checks for file size changes */
2540 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2541 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2542
2543 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2544 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002545 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002546 struct nfs_lockowner lockowner = {
2547 .l_owner = current->files,
2548 .l_pid = current->tgid,
2549 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002550 if (!nfs4_valid_open_stateid(state))
2551 return -EBADF;
2552 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2553 &lockowner) == -EIO)
2554 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002555 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002556 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Bryan Schumaker7c513052011-03-24 17:12:24 +00002558 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002559 if (status == 0 && state != NULL)
2560 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002561 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002564static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2565 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002566 struct nfs4_state *state, struct nfs4_label *ilabel,
2567 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002569 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002570 struct nfs4_exception exception = {
2571 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002572 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002573 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 int err;
2575 do {
David Quigley1775fd32013-05-22 12:50:42 -04002576 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002577 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002578 switch (err) {
2579 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002580 if (!(sattr->ia_valid & ATTR_SIZE)) {
2581 pr_warn_once("NFSv4: server %s is incorrectly "
2582 "applying open mode checks to "
2583 "a SETATTR that is not "
2584 "changing file size.\n",
2585 server->nfs_client->cl_hostname);
2586 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002587 if (state && !(state->state & FMODE_WRITE)) {
2588 err = -EBADF;
2589 if (sattr->ia_valid & ATTR_OPEN)
2590 err = -EACCES;
2591 goto out;
2592 }
2593 }
2594 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002596out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return err;
2598}
2599
2600struct nfs4_closedata {
2601 struct inode *inode;
2602 struct nfs4_state *state;
2603 struct nfs_closeargs arg;
2604 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002605 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002606 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002607 bool roc;
2608 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609};
2610
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002611static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002612{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002613 struct nfs4_closedata *calldata = data;
2614 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002615 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002616
Fred Isamanf7e89172011-01-06 11:36:32 +00002617 if (calldata->roc)
2618 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002619 nfs4_put_open_state(calldata->state);
2620 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002621 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002622 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002623 kfree(calldata);
2624}
2625
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002626static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002628 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002631 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Chuck Levera3ca5652012-03-01 17:00:40 -05002633 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002634 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2635 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002636 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 /* hmm. we are done with the inode, and in the process of freeing
2638 * the state_owner. we keep this around to process errors
2639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 switch (task->tk_status) {
2641 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002642 res_stateid = &calldata->res.stateid;
2643 if (calldata->arg.fmode == 0 && calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002644 pnfs_roc_set_barrier(state->inode,
2645 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002646 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002647 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002648 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002650 case -NFS4ERR_OLD_STATEID:
2651 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002653 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002654 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002655 rpc_restart_call_prepare(task);
2656 goto out_release;
2657 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002658 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002661 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002662 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002663 goto out_release;
2664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
Trond Myklebust412f6c42014-08-25 22:09:08 -04002666 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002667out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002668 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002669 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002670 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002673static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002675 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002676 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002677 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002678 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002679 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002680
Chuck Levera3ca5652012-03-01 17:00:40 -05002681 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002682 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002683 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002684
Trond Myklebust88069f72009-12-08 08:33:16 -05002685 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002686 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002687 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2688 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2689 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002690 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002691 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002692 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002693 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002694 if (state->n_rdonly == 0)
2695 call_close |= is_rdonly;
2696 else if (is_rdonly)
2697 calldata->arg.fmode |= FMODE_READ;
2698 if (state->n_wronly == 0)
2699 call_close |= is_wronly;
2700 else if (is_wronly)
2701 calldata->arg.fmode |= FMODE_WRITE;
2702 } else if (is_rdwr)
2703 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2704
2705 if (calldata->arg.fmode == 0)
2706 call_close |= is_rdwr;
2707
Trond Myklebust5d422302013-03-14 16:57:48 -04002708 if (!nfs4_valid_open_stateid(state))
2709 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002710 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002711
2712 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002713 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002714 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002715 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002716
Fred Isamanf7e89172011-01-06 11:36:32 +00002717 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002718 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002719 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002720 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2721 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002722 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002723 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002724 }
Trond Myklebust6ae37332015-01-30 14:21:14 -05002725 calldata->arg.share_access =
2726 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2727 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002728
Trond Myklebust516a6af2005-10-27 22:12:41 -04002729 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002730 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002731 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002732 &calldata->arg.seq_args,
2733 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002734 task) != 0)
2735 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002736 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002737 return;
2738out_no_action:
2739 task->tk_action = NULL;
2740out_wait:
2741 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002744static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002745 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002746 .rpc_call_done = nfs4_close_done,
2747 .rpc_release = nfs4_free_closedata,
2748};
2749
Peng Taofe08c542014-07-03 13:05:01 +08002750static bool nfs4_roc(struct inode *inode)
2751{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002752 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002753 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002754 return pnfs_roc(inode);
2755}
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757/*
2758 * It is possible for data to be read/written from a mem-mapped file
2759 * after the sys_close call (which hits the vfs layer as a flush).
2760 * This means that we can't safely call nfsv4 close on a file until
2761 * the inode is cleared. This in turn means that we are not good
2762 * NFSv4 citizens - we do not indicate to the server to update the file's
2763 * share state even when we are done with one of the three share
2764 * stateid's in the inode.
2765 *
2766 * NOTE: Caller must be holding the sp->so_owner semaphore!
2767 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002768int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002770 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002771 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002773 struct nfs4_state_owner *sp = state->owner;
2774 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002775 struct rpc_message msg = {
2776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2777 .rpc_cred = state->owner->so_cred,
2778 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002779 struct rpc_task_setup task_setup_data = {
2780 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002781 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002782 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002783 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002784 .flags = RPC_TASK_ASYNC,
2785 };
Trond Myklebust95121352005-10-18 14:20:12 -07002786 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002788 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2789 &task_setup_data.rpc_client, &msg);
2790
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002791 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002793 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002794 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002795 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002797 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002799 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2800 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002801 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002802 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002803 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002804 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002805 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002806 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002807 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002808 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002809 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002810
Trond Myklebust1174dd12010-12-21 10:52:24 -05002811 msg.rpc_argp = &calldata->arg;
2812 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002813 task_setup_data.callback_data = calldata;
2814 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002815 if (IS_ERR(task))
2816 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002817 status = 0;
2818 if (wait)
2819 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002820 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002821 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002822out_free_calldata:
2823 kfree(calldata);
2824out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002825 nfs4_put_open_state(state);
2826 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002827 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
Trond Myklebust2b484292010-09-17 10:56:51 -04002830static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002831nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2832 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002835 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2836
2837 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Trond Myklebust565277f2007-10-15 18:17:53 -04002839 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002840 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002841
2842 nfs4_label_release_security(label);
2843
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002844 if (IS_ERR(state))
2845 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002846 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
Trond Myklebust1185a552009-12-03 15:54:02 -05002849static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002850{
2851 if (ctx->state == NULL)
2852 return;
2853 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002854 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002855 else
Al Viro643168c2011-06-22 18:20:23 -04002856 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002857}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Trond Myklebustb944dba2013-11-04 15:20:20 -05002859#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2860#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002861#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2864{
Benny Halevy43652ad2009-04-01 09:21:54 -04002865 struct nfs4_server_caps_arg args = {
2866 .fhandle = fhandle,
2867 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 struct nfs4_server_caps_res res = {};
2869 struct rpc_message msg = {
2870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002871 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 .rpc_resp = &res,
2873 };
2874 int status;
2875
Bryan Schumaker7c513052011-03-24 17:12:24 +00002876 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002878 /* Sanity check the server answers */
2879 switch (server->nfs_client->cl_minorversion) {
2880 case 0:
2881 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2882 res.attr_bitmask[2] = 0;
2883 break;
2884 case 1:
2885 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2886 break;
2887 case 2:
2888 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002891 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2892 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2893 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2894 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002895 NFS_CAP_CTIME|NFS_CAP_MTIME|
2896 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002897 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2898 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 server->caps |= NFS_CAP_ACLS;
2900 if (res.has_links != 0)
2901 server->caps |= NFS_CAP_HARDLINKS;
2902 if (res.has_symlinks != 0)
2903 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002904 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2905 server->caps |= NFS_CAP_FILEID;
2906 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2907 server->caps |= NFS_CAP_MODE;
2908 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2909 server->caps |= NFS_CAP_NLINK;
2910 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2911 server->caps |= NFS_CAP_OWNER;
2912 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2913 server->caps |= NFS_CAP_OWNER_GROUP;
2914 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2915 server->caps |= NFS_CAP_ATIME;
2916 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2917 server->caps |= NFS_CAP_CTIME;
2918 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2919 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002920#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2921 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2922 server->caps |= NFS_CAP_SECURITY_LABEL;
2923#endif
2924 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2925 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002926 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002927
Trond Myklebusta65318b2009-03-11 14:10:28 -04002928 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2929 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2930 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002931 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002933 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return status;
2937}
2938
Trond Myklebust55a97592006-06-09 09:34:19 -04002939int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 struct nfs4_exception exception = { };
2942 int err;
2943 do {
2944 err = nfs4_handle_exception(server,
2945 _nfs4_server_capabilities(server, fhandle),
2946 &exception);
2947 } while (exception.retry);
2948 return err;
2949}
2950
2951static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2952 struct nfs_fsinfo *info)
2953{
David Quigleyaa9c2662013-05-22 12:50:44 -04002954 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002956 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 };
2958 struct nfs4_lookup_res res = {
2959 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002960 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 .fh = fhandle,
2962 };
2963 struct rpc_message msg = {
2964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2965 .rpc_argp = &args,
2966 .rpc_resp = &res,
2967 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002968
David Quigleyaa9c2662013-05-22 12:50:44 -04002969 bitmask[0] = nfs4_fattr_bitmap[0];
2970 bitmask[1] = nfs4_fattr_bitmap[1];
2971 /*
2972 * Process the label in the upcoming getfattr
2973 */
2974 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2975
Trond Myklebust0e574af2005-10-27 22:12:38 -04002976 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002977 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
2980static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2981 struct nfs_fsinfo *info)
2982{
2983 struct nfs4_exception exception = { };
2984 int err;
2985 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002986 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002987 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002988 switch (err) {
2989 case 0:
2990 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002991 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002992 default:
2993 err = nfs4_handle_exception(server, err, &exception);
2994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002996out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return err;
2998}
2999
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003000static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3001 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3002{
Trond Myklebustc2190662013-08-26 19:23:04 -04003003 struct rpc_auth_create_args auth_args = {
3004 .pseudoflavor = flavor,
3005 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003006 struct rpc_auth *auth;
3007 int ret;
3008
Trond Myklebustc2190662013-08-26 19:23:04 -04003009 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003010 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003011 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003012 goto out;
3013 }
3014 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003015out:
3016 return ret;
3017}
3018
Chuck Lever9a744ba2013-03-16 15:56:02 -04003019/*
3020 * Retry pseudoroot lookup with various security flavors. We do this when:
3021 *
3022 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3023 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3024 *
3025 * Returns zero on success, or a negative NFS4ERR value, or a
3026 * negative errno value.
3027 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003028static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003029 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003031 /* Per 3530bis 15.33.5 */
3032 static const rpc_authflavor_t flav_array[] = {
3033 RPC_AUTH_GSS_KRB5P,
3034 RPC_AUTH_GSS_KRB5I,
3035 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003036 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003037 RPC_AUTH_NULL,
3038 };
3039 int status = -EPERM;
3040 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003042 if (server->auth_info.flavor_len > 0) {
3043 /* try each flavor specified by user */
3044 for (i = 0; i < server->auth_info.flavor_len; i++) {
3045 status = nfs4_lookup_root_sec(server, fhandle, info,
3046 server->auth_info.flavors[i]);
3047 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3048 continue;
3049 break;
3050 }
3051 } else {
3052 /* no flavors specified by user, try default list */
3053 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3054 status = nfs4_lookup_root_sec(server, fhandle, info,
3055 flav_array[i]);
3056 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3057 continue;
3058 break;
3059 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003060 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003061
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003062 /*
3063 * -EACCESS could mean that the user doesn't have correct permissions
3064 * to access the mount. It could also mean that we tried to mount
3065 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3066 * existing mount programs don't handle -EACCES very well so it should
3067 * be mapped to -EPERM instead.
3068 */
3069 if (status == -EACCES)
3070 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003071 return status;
3072}
3073
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003074static int nfs4_do_find_root_sec(struct nfs_server *server,
3075 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3076{
3077 int mv = server->nfs_client->cl_minorversion;
3078 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3079}
3080
3081/**
3082 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3083 * @server: initialized nfs_server handle
3084 * @fhandle: we fill in the pseudo-fs root file handle
3085 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003086 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003087 *
3088 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003089 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003090int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003091 struct nfs_fsinfo *info,
3092 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003093{
Andre Przywarac7757072015-04-23 17:17:40 +01003094 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003095
Andre Przywarac7757072015-04-23 17:17:40 +01003096 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003097 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003098
3099 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003100 status = nfs4_do_find_root_sec(server, fhandle, info);
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (status == 0)
3103 status = nfs4_server_capabilities(server, fhandle);
3104 if (status == 0)
3105 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003106
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003107 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Bryan Schumakerbae36242012-05-10 15:07:31 -04003110static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3111 struct nfs_fsinfo *info)
3112{
3113 int error;
3114 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003115 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003116
3117 error = nfs4_server_capabilities(server, mntfh);
3118 if (error < 0) {
3119 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3120 return error;
3121 }
3122
David Quigley14c43f72013-05-22 12:50:43 -04003123 label = nfs4_label_alloc(server, GFP_KERNEL);
3124 if (IS_ERR(label))
3125 return PTR_ERR(label);
3126
David Quigley1775fd32013-05-22 12:50:42 -04003127 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003128 if (error < 0) {
3129 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003130 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003131 }
3132
3133 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3134 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3135 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3136
David Quigley14c43f72013-05-22 12:50:43 -04003137err_free_label:
3138 nfs4_label_free(label);
3139
Bryan Schumakerbae36242012-05-10 15:07:31 -04003140 return error;
3141}
3142
Manoj Naik6b97fd32006-06-09 09:34:29 -04003143/*
3144 * Get locations and (maybe) other attributes of a referral.
3145 * Note that we'll actually follow the referral later when
3146 * we detect fsid mismatch in inode revalidation
3147 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003148static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3149 const struct qstr *name, struct nfs_fattr *fattr,
3150 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003151{
3152 int status = -ENOMEM;
3153 struct page *page = NULL;
3154 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003155
3156 page = alloc_page(GFP_KERNEL);
3157 if (page == NULL)
3158 goto out;
3159 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3160 if (locations == NULL)
3161 goto out;
3162
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003163 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003164 if (status != 0)
3165 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003166
3167 /*
3168 * If the fsid didn't change, this is a migration event, not a
3169 * referral. Cause us to drop into the exception handler, which
3170 * will kick off migration recovery.
3171 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003172 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003173 dprintk("%s: server did not return a different fsid for"
3174 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003175 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003176 goto out;
3177 }
Andy Adamson533eb462011-06-13 18:25:56 -04003178 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3179 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003180
Andy Adamson533eb462011-06-13 18:25:56 -04003181 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003182 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003183 memset(fhandle, 0, sizeof(struct nfs_fh));
3184out:
3185 if (page)
3186 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003187 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003188 return status;
3189}
3190
David Quigley1775fd32013-05-22 12:50:42 -04003191static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3192 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 struct nfs4_getattr_arg args = {
3195 .fh = fhandle,
3196 .bitmask = server->attr_bitmask,
3197 };
3198 struct nfs4_getattr_res res = {
3199 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003200 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 .server = server,
3202 };
3203 struct rpc_message msg = {
3204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3205 .rpc_argp = &args,
3206 .rpc_resp = &res,
3207 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003208
3209 args.bitmask = nfs4_bitmask(server, label);
3210
Trond Myklebust0e574af2005-10-27 22:12:38 -04003211 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003212 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213}
3214
David Quigley1775fd32013-05-22 12:50:42 -04003215static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3216 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
3218 struct nfs4_exception exception = { };
3219 int err;
3220 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003221 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3222 trace_nfs4_getattr(server, fhandle, fattr, err);
3223 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 &exception);
3225 } while (exception.retry);
3226 return err;
3227}
3228
3229/*
3230 * The file is not closed if it is opened due to the a request to change
3231 * the size of the file. The open call will not be needed once the
3232 * VFS layer lookup-intents are implemented.
3233 *
3234 * Close is called when the inode is destroyed.
3235 * If we haven't opened the file for O_WRONLY, we
3236 * need to in the size_change case to obtain a stateid.
3237 *
3238 * Got race?
3239 * Because OPEN is always done by name in nfsv4, it is
3240 * possible that we opened a different file by the same
3241 * name. We can recognize this race condition, but we
3242 * can't do anything about it besides returning an error.
3243 *
3244 * This will be fixed with VFS changes (lookup-intent).
3245 */
3246static int
3247nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3248 struct iattr *sattr)
3249{
David Howells2b0143b2015-03-17 22:25:59 +00003250 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003251 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003252 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003253 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 int status;
3255
Peng Tao88ac8152014-09-12 11:04:10 +08003256 if (pnfs_ld_layoutret_on_setattr(inode) &&
3257 sattr->ia_valid & ATTR_SIZE &&
3258 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003259 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003260
Trond Myklebust0e574af2005-10-27 22:12:38 -04003261 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Andy Adamson26699402012-05-30 16:12:24 -04003263 /* Deal with open(O_TRUNC) */
3264 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003265 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003266
3267 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003268 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003269 return 0;
3270
Trond Myklebustd5308382005-11-04 15:33:38 -05003271 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003272 if (sattr->ia_valid & ATTR_FILE) {
3273 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003274
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003275 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003276 if (ctx) {
3277 cred = ctx->cred;
3278 state = ctx->state;
3279 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003280 }
3281
David Quigley14c43f72013-05-22 12:50:43 -04003282 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3283 if (IS_ERR(label))
3284 return PTR_ERR(label);
3285
3286 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003287 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003288 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003289 nfs_setsecurity(inode, fattr, label);
3290 }
David Quigley14c43f72013-05-22 12:50:43 -04003291 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return status;
3293}
3294
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003295static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3296 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003297 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003298{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003299 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003300 int status;
3301 struct nfs4_lookup_arg args = {
3302 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003303 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003304 .name = name,
3305 };
3306 struct nfs4_lookup_res res = {
3307 .server = server,
3308 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003309 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003310 .fh = fhandle,
3311 };
3312 struct rpc_message msg = {
3313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3314 .rpc_argp = &args,
3315 .rpc_resp = &res,
3316 };
3317
David Quigleyaa9c2662013-05-22 12:50:44 -04003318 args.bitmask = nfs4_bitmask(server, label);
3319
David Howells2b3de442006-08-22 20:06:09 -04003320 nfs_fattr_init(fattr);
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003323 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 dprintk("NFS reply lookup: %d\n", status);
3325 return status;
3326}
3327
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003328static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003329{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003330 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003331 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003332 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3333 fattr->nlink = 2;
3334}
3335
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003336static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3337 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003338 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003339{
3340 struct nfs4_exception exception = { };
3341 struct rpc_clnt *client = *clnt;
3342 int err;
3343 do {
David Quigley1775fd32013-05-22 12:50:42 -04003344 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003345 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003346 switch (err) {
3347 case -NFS4ERR_BADNAME:
3348 err = -ENOENT;
3349 goto out;
3350 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003351 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003352 if (err == -NFS4ERR_MOVED)
3353 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003354 goto out;
3355 case -NFS4ERR_WRONGSEC:
3356 err = -EPERM;
3357 if (client != *clnt)
3358 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003359 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003360 if (IS_ERR(client))
3361 return PTR_ERR(client);
3362
3363 exception.retry = 1;
3364 break;
3365 default:
3366 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3367 }
3368 } while (exception.retry);
3369
3370out:
3371 if (err == 0)
3372 *clnt = client;
3373 else if (client != *clnt)
3374 rpc_shutdown_client(client);
3375
3376 return err;
3377}
3378
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003379static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003380 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3381 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003383 int status;
3384 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003385
David Quigley1775fd32013-05-22 12:50:42 -04003386 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003387 if (client != NFS_CLIENT(dir)) {
3388 rpc_shutdown_client(client);
3389 nfs_fixup_secinfo_attributes(fattr);
3390 }
3391 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
3393
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003394struct rpc_clnt *
3395nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3396 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3397{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003398 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003399 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003400
David Quigley1775fd32013-05-22 12:50:42 -04003401 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003402 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003403 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003404 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003405}
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3408{
Trond Myklebust76b32992007-08-10 17:45:11 -04003409 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 struct nfs4_accessargs args = {
3411 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003412 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003414 struct nfs4_accessres res = {
3415 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003416 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 struct rpc_message msg = {
3418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3419 .rpc_argp = &args,
3420 .rpc_resp = &res,
3421 .rpc_cred = entry->cred,
3422 };
3423 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003424 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 /*
3427 * Determine which access bits we want to ask for...
3428 */
3429 if (mode & MAY_READ)
3430 args.access |= NFS4_ACCESS_READ;
3431 if (S_ISDIR(inode->i_mode)) {
3432 if (mode & MAY_WRITE)
3433 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3434 if (mode & MAY_EXEC)
3435 args.access |= NFS4_ACCESS_LOOKUP;
3436 } else {
3437 if (mode & MAY_WRITE)
3438 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3439 if (mode & MAY_EXEC)
3440 args.access |= NFS4_ACCESS_EXECUTE;
3441 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003442
3443 res.fattr = nfs_alloc_fattr();
3444 if (res.fattr == NULL)
3445 return -ENOMEM;
3446
Bryan Schumaker7c513052011-03-24 17:12:24 +00003447 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003449 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003450 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003452 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 return status;
3454}
3455
3456static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3457{
3458 struct nfs4_exception exception = { };
3459 int err;
3460 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003461 err = _nfs4_proc_access(inode, entry);
3462 trace_nfs4_access(inode, err);
3463 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 &exception);
3465 } while (exception.retry);
3466 return err;
3467}
3468
3469/*
3470 * TODO: For the time being, we don't try to get any attributes
3471 * along with any of the zero-copy operations READ, READDIR,
3472 * READLINK, WRITE.
3473 *
3474 * In the case of the first three, we want to put the GETATTR
3475 * after the read-type operation -- this is because it is hard
3476 * to predict the length of a GETATTR response in v4, and thus
3477 * align the READ data correctly. This means that the GETATTR
3478 * may end up partially falling into the page cache, and we should
3479 * shift it into the 'tail' of the xdr_buf before processing.
3480 * To do this efficiently, we need to know the total length
3481 * of data received, which doesn't seem to be available outside
3482 * of the RPC layer.
3483 *
3484 * In the case of WRITE, we also want to put the GETATTR after
3485 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003486 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 *
3488 * Both of these changes to the XDR layer would in fact be quite
3489 * minor, but I decided to leave them for a subsequent patch.
3490 */
3491static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3492 unsigned int pgbase, unsigned int pglen)
3493{
3494 struct nfs4_readlink args = {
3495 .fh = NFS_FH(inode),
3496 .pgbase = pgbase,
3497 .pglen = pglen,
3498 .pages = &page,
3499 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003500 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 struct rpc_message msg = {
3502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3503 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003504 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 };
3506
Bryan Schumaker7c513052011-03-24 17:12:24 +00003507 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 -07003508}
3509
3510static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3511 unsigned int pgbase, unsigned int pglen)
3512{
3513 struct nfs4_exception exception = { };
3514 int err;
3515 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003516 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3517 trace_nfs4_readlink(inode, err);
3518 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 &exception);
3520 } while (exception.retry);
3521 return err;
3522}
3523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003525 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527static int
3528nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003529 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
David Quigleyaa9c2662013-05-22 12:50:44 -04003531 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003532 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003534 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 int status = 0;
3536
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003537 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3538 if (IS_ERR(ctx))
3539 return PTR_ERR(ctx);
3540
David Quigleyaa9c2662013-05-22 12:50:44 -04003541 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3542
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003543 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003544 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 if (IS_ERR(state)) {
3546 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003547 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003550 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003551 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return status;
3553}
3554
3555static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3556{
Trond Myklebust16e42952005-10-27 22:12:44 -04003557 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003558 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003560 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003562 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003563 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003564 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3567 .rpc_argp = &args,
3568 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003570 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003571
Bryan Schumaker7c513052011-03-24 17:12:24 +00003572 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003573 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003574 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return status;
3576}
3577
3578static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3579{
3580 struct nfs4_exception exception = { };
3581 int err;
3582 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003583 err = _nfs4_proc_remove(dir, name);
3584 trace_nfs4_remove(dir, name, err);
3585 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 &exception);
3587 } while (exception.retry);
3588 return err;
3589}
3590
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003591static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003593 struct nfs_server *server = NFS_SERVER(dir);
3594 struct nfs_removeargs *args = msg->rpc_argp;
3595 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003597 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003599 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003600
3601 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003604static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3605{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003606 nfs4_setup_sequence(NFS_SERVER(data->dir),
3607 &data->args.seq_args,
3608 &data->res.seq_res,
3609 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003612static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003614 struct nfs_unlinkdata *data = task->tk_calldata;
3615 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003616
Trond Myklebust14516c32010-07-31 14:29:06 -04003617 if (!nfs4_sequence_done(task, &res->seq_res))
3618 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003619 if (nfs4_async_handle_error(task, res->server, NULL,
3620 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003621 return 0;
3622 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003623 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
Jeff Laytond3d41522010-09-17 17:31:57 -04003626static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3627{
3628 struct nfs_server *server = NFS_SERVER(dir);
3629 struct nfs_renameargs *arg = msg->rpc_argp;
3630 struct nfs_renameres *res = msg->rpc_resp;
3631
3632 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003633 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003634 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003635}
3636
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003637static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3638{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003639 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3640 &data->args.seq_args,
3641 &data->res.seq_res,
3642 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003643}
3644
3645static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3646 struct inode *new_dir)
3647{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003648 struct nfs_renamedata *data = task->tk_calldata;
3649 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003650
3651 if (!nfs4_sequence_done(task, &res->seq_res))
3652 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003653 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003654 return 0;
3655
3656 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003657 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003658 return 1;
3659}
3660
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3662{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003663 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 struct nfs4_link_arg arg = {
3665 .fh = NFS_FH(inode),
3666 .dir_fh = NFS_FH(dir),
3667 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003668 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003670 struct nfs4_link_res res = {
3671 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003672 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003673 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 struct rpc_message msg = {
3675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3676 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003677 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003679 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Trond Myklebust136f2622010-04-16 16:22:49 -04003681 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003682 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003683 goto out;
3684
David Quigley14c43f72013-05-22 12:50:43 -04003685 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3686 if (IS_ERR(res.label)) {
3687 status = PTR_ERR(res.label);
3688 goto out;
3689 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003690 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003691
Bryan Schumaker7c513052011-03-24 17:12:24 +00003692 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003693 if (!status) {
3694 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003695 status = nfs_post_op_update_inode(inode, res.fattr);
3696 if (!status)
3697 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003698 }
David Quigley14c43f72013-05-22 12:50:43 -04003699
3700
3701 nfs4_label_free(res.label);
3702
Trond Myklebust136f2622010-04-16 16:22:49 -04003703out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003704 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 return status;
3706}
3707
3708static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3709{
3710 struct nfs4_exception exception = { };
3711 int err;
3712 do {
3713 err = nfs4_handle_exception(NFS_SERVER(inode),
3714 _nfs4_proc_link(inode, dir, name),
3715 &exception);
3716 } while (exception.retry);
3717 return err;
3718}
3719
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003720struct nfs4_createdata {
3721 struct rpc_message msg;
3722 struct nfs4_create_arg arg;
3723 struct nfs4_create_res res;
3724 struct nfs_fh fh;
3725 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003726 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003727};
3728
3729static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3730 struct qstr *name, struct iattr *sattr, u32 ftype)
3731{
3732 struct nfs4_createdata *data;
3733
3734 data = kzalloc(sizeof(*data), GFP_KERNEL);
3735 if (data != NULL) {
3736 struct nfs_server *server = NFS_SERVER(dir);
3737
David Quigley14c43f72013-05-22 12:50:43 -04003738 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3739 if (IS_ERR(data->label))
3740 goto out_free;
3741
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003742 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3743 data->msg.rpc_argp = &data->arg;
3744 data->msg.rpc_resp = &data->res;
3745 data->arg.dir_fh = NFS_FH(dir);
3746 data->arg.server = server;
3747 data->arg.name = name;
3748 data->arg.attrs = sattr;
3749 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003750 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003751 data->res.server = server;
3752 data->res.fh = &data->fh;
3753 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003754 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003755 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003756 }
3757 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003758out_free:
3759 kfree(data);
3760 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003761}
3762
3763static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3764{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003765 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003766 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003767 if (status == 0) {
3768 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003769 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003770 }
3771 return status;
3772}
3773
3774static void nfs4_free_createdata(struct nfs4_createdata *data)
3775{
David Quigley14c43f72013-05-22 12:50:43 -04003776 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003777 kfree(data);
3778}
3779
Chuck Lever4f390c12006-08-22 20:06:22 -04003780static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003781 struct page *page, unsigned int len, struct iattr *sattr,
3782 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003784 struct nfs4_createdata *data;
3785 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
Chuck Lever94a6d752006-08-22 20:06:23 -04003787 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003788 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003789
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003790 status = -ENOMEM;
3791 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3792 if (data == NULL)
3793 goto out;
3794
3795 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3796 data->arg.u.symlink.pages = &page;
3797 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003798 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003800 status = nfs4_do_create(dir, dentry, data);
3801
3802 nfs4_free_createdata(data);
3803out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return status;
3805}
3806
Chuck Lever4f390c12006-08-22 20:06:22 -04003807static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003808 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809{
3810 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003811 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003813
3814 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003817 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3818 trace_nfs4_symlink(dir, &dentry->d_name, err);
3819 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 &exception);
3821 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003822
3823 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return err;
3825}
3826
3827static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003828 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003830 struct nfs4_createdata *data;
3831 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003833 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3834 if (data == NULL)
3835 goto out;
3836
David Quigley1775fd32013-05-22 12:50:42 -04003837 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003838 status = nfs4_do_create(dir, dentry, data);
3839
3840 nfs4_free_createdata(data);
3841out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 return status;
3843}
3844
3845static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3846 struct iattr *sattr)
3847{
3848 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003849 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003851
David Quigleyaa9c2662013-05-22 12:50:44 -04003852 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3853
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003854 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003856 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3857 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3858 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 &exception);
3860 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003861 nfs4_label_release_security(label);
3862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 return err;
3864}
3865
3866static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003867 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868{
David Howells2b0143b2015-03-17 22:25:59 +00003869 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 struct nfs4_readdir_arg args = {
3871 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003872 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 .pgbase = 0,
3874 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00003875 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003876 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 };
3878 struct nfs4_readdir_res res;
3879 struct rpc_message msg = {
3880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3881 .rpc_argp = &args,
3882 .rpc_resp = &res,
3883 .rpc_cred = cred,
3884 };
3885 int status;
3886
Al Viro6de14722013-09-16 10:53:17 -04003887 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3888 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003889 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003890 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003892 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 -05003893 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003894 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003895 status += args.pgbase;
3896 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003897
3898 nfs_invalidate_atime(dir);
3899
Harvey Harrison3110ff82008-05-02 13:42:44 -07003900 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return status;
3902}
3903
3904static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003905 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
3907 struct nfs4_exception exception = { };
3908 int err;
3909 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003910 err = _nfs4_proc_readdir(dentry, cred, cookie,
3911 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00003912 trace_nfs4_readdir(d_inode(dentry), err);
3913 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 &exception);
3915 } while (exception.retry);
3916 return err;
3917}
3918
3919static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003920 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003922 struct nfs4_createdata *data;
3923 int mode = sattr->ia_mode;
3924 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003926 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3927 if (data == NULL)
3928 goto out;
3929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003931 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003933 data->arg.ftype = NF4BLK;
3934 data->arg.u.device.specdata1 = MAJOR(rdev);
3935 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 }
3937 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003938 data->arg.ftype = NF4CHR;
3939 data->arg.u.device.specdata1 = MAJOR(rdev);
3940 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003941 } else if (!S_ISSOCK(mode)) {
3942 status = -EINVAL;
3943 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 }
David Quigley1775fd32013-05-22 12:50:42 -04003945
David Quigleyaa9c2662013-05-22 12:50:44 -04003946 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003947 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003948out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003949 nfs4_free_createdata(data);
3950out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 return status;
3952}
3953
3954static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3955 struct iattr *sattr, dev_t rdev)
3956{
3957 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003958 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003960
David Quigleyaa9c2662013-05-22 12:50:44 -04003961 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3962
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003963 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003965 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3966 trace_nfs4_mknod(dir, &dentry->d_name, err);
3967 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 &exception);
3969 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003970
3971 nfs4_label_release_security(label);
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return err;
3974}
3975
3976static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3977 struct nfs_fsstat *fsstat)
3978{
3979 struct nfs4_statfs_arg args = {
3980 .fh = fhandle,
3981 .bitmask = server->attr_bitmask,
3982 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003983 struct nfs4_statfs_res res = {
3984 .fsstat = fsstat,
3985 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 struct rpc_message msg = {
3987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3988 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003989 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 };
3991
Trond Myklebust0e574af2005-10-27 22:12:38 -04003992 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003993 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
3995
3996static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3997{
3998 struct nfs4_exception exception = { };
3999 int err;
4000 do {
4001 err = nfs4_handle_exception(server,
4002 _nfs4_proc_statfs(server, fhandle, fsstat),
4003 &exception);
4004 } while (exception.retry);
4005 return err;
4006}
4007
4008static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4009 struct nfs_fsinfo *fsinfo)
4010{
4011 struct nfs4_fsinfo_arg args = {
4012 .fh = fhandle,
4013 .bitmask = server->attr_bitmask,
4014 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004015 struct nfs4_fsinfo_res res = {
4016 .fsinfo = fsinfo,
4017 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 struct rpc_message msg = {
4019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4020 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004021 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 };
4023
Bryan Schumaker7c513052011-03-24 17:12:24 +00004024 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
4026
4027static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4028{
4029 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004030 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 int err;
4032
4033 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004034 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004035 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004036 if (err == 0) {
4037 struct nfs_client *clp = server->nfs_client;
4038
4039 spin_lock(&clp->cl_lock);
4040 clp->cl_lease_time = fsinfo->lease_time * HZ;
4041 clp->cl_last_renewal = now;
4042 spin_unlock(&clp->cl_lock);
4043 break;
4044 }
4045 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 } while (exception.retry);
4047 return err;
4048}
4049
4050static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4051{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004052 int error;
4053
Trond Myklebust0e574af2005-10-27 22:12:38 -04004054 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004055 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004056 if (error == 0) {
4057 /* block layout checks this! */
4058 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004059 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004060 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004061
4062 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063}
4064
4065static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4066 struct nfs_pathconf *pathconf)
4067{
4068 struct nfs4_pathconf_arg args = {
4069 .fh = fhandle,
4070 .bitmask = server->attr_bitmask,
4071 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004072 struct nfs4_pathconf_res res = {
4073 .pathconf = pathconf,
4074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 struct rpc_message msg = {
4076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4077 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004078 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 };
4080
4081 /* None of the pathconf attributes are mandatory to implement */
4082 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4083 memset(pathconf, 0, sizeof(*pathconf));
4084 return 0;
4085 }
4086
Trond Myklebust0e574af2005-10-27 22:12:38 -04004087 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004088 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
4090
4091static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4092 struct nfs_pathconf *pathconf)
4093{
4094 struct nfs4_exception exception = { };
4095 int err;
4096
4097 do {
4098 err = nfs4_handle_exception(server,
4099 _nfs4_proc_pathconf(server, fhandle, pathconf),
4100 &exception);
4101 } while (exception.retry);
4102 return err;
4103}
4104
Trond Myklebust5521abf2013-03-16 20:54:34 -04004105int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004106 const struct nfs_open_context *ctx,
4107 const struct nfs_lock_context *l_ctx,
4108 fmode_t fmode)
4109{
4110 const struct nfs_lockowner *lockowner = NULL;
4111
4112 if (l_ctx != NULL)
4113 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004114 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004115}
4116EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4117
Trond Myklebust5521abf2013-03-16 20:54:34 -04004118static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4119 const struct nfs_open_context *ctx,
4120 const struct nfs_lock_context *l_ctx,
4121 fmode_t fmode)
4122{
4123 nfs4_stateid current_stateid;
4124
Trond Myklebuste1253be2014-03-05 08:44:23 -05004125 /* If the current stateid represents a lost lock, then exit */
4126 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4127 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004128 return nfs4_stateid_match(stateid, &current_stateid);
4129}
4130
4131static bool nfs4_error_stateid_expired(int err)
4132{
4133 switch (err) {
4134 case -NFS4ERR_DELEG_REVOKED:
4135 case -NFS4ERR_ADMIN_REVOKED:
4136 case -NFS4ERR_BAD_STATEID:
4137 case -NFS4ERR_STALE_STATEID:
4138 case -NFS4ERR_OLD_STATEID:
4139 case -NFS4ERR_OPENMODE:
4140 case -NFS4ERR_EXPIRED:
4141 return true;
4142 }
4143 return false;
4144}
4145
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004146void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004147{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004148 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004149}
4150
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004151static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004153 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004155 trace_nfs4_read(hdr, task->tk_status);
4156 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004157 hdr->args.context->state,
4158 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004159 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004160 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004162
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004163 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004165 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167}
4168
Trond Myklebust5521abf2013-03-16 20:54:34 -04004169static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004170 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004171{
4172
4173 if (!nfs4_error_stateid_expired(task->tk_status) ||
4174 nfs4_stateid_is_current(&args->stateid,
4175 args->context,
4176 args->lock_context,
4177 FMODE_READ))
4178 return false;
4179 rpc_restart_call_prepare(task);
4180 return true;
4181}
4182
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004183static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004184{
4185
4186 dprintk("--> %s\n", __func__);
4187
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004188 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004189 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004190 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004191 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004192 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4193 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004194}
4195
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004196static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4197 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004199 hdr->timestamp = jiffies;
4200 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004201 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004202 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203}
4204
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004205static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4206 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004207{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004208 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4209 &hdr->args.seq_args,
4210 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004211 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004212 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004213 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4214 hdr->args.lock_context,
4215 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004216 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004217 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004218 return -EIO;
4219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004222static int nfs4_write_done_cb(struct rpc_task *task,
4223 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004225 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004226
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004227 trace_nfs4_write(hdr, task->tk_status);
4228 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004229 hdr->args.context->state,
4230 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004231 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004232 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004234 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004235 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004236 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004237 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239}
4240
Trond Myklebust5521abf2013-03-16 20:54:34 -04004241static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004242 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004243{
4244
4245 if (!nfs4_error_stateid_expired(task->tk_status) ||
4246 nfs4_stateid_is_current(&args->stateid,
4247 args->context,
4248 args->lock_context,
4249 FMODE_WRITE))
4250 return false;
4251 rpc_restart_call_prepare(task);
4252 return true;
4253}
4254
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004255static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004256{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004257 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004258 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004259 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004260 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004261 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4262 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004263}
4264
Trond Myklebust5a37f852012-04-28 14:55:16 -04004265static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004266bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004267{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004268 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004269 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004270 return false;
4271 /* Otherwise, request attributes if and only if we don't hold
4272 * a delegation
4273 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004274 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004275}
Fred Isamana69aef12011-03-03 15:13:47 +00004276
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004277static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4278 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004280 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004281
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004282 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4283 hdr->args.bitmask = NULL;
4284 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004285 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004286 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004287
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004288 if (!hdr->pgio_done_cb)
4289 hdr->pgio_done_cb = nfs4_write_done_cb;
4290 hdr->res.server = server;
4291 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004293 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004294 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295}
4296
Fred Isaman0b7c0152012-04-20 14:47:39 -04004297static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4298{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004299 nfs4_setup_sequence(NFS_SERVER(data->inode),
4300 &data->args.seq_args,
4301 &data->res.seq_res,
4302 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304
Fred Isaman0b7c0152012-04-20 14:47:39 -04004305static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004308
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004309 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004310 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4311 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004312 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004313 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
Fred Isaman0b7c0152012-04-20 14:47:39 -04004318static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004319{
4320 if (!nfs4_sequence_done(task, &data->res.seq_res))
4321 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004322 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004323}
4324
Fred Isaman0b7c0152012-04-20 14:47:39 -04004325static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004327 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004328
Fred Isaman0b7c0152012-04-20 14:47:39 -04004329 if (data->commit_done_cb == NULL)
4330 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004331 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004332 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004333 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334}
4335
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004336struct nfs4_renewdata {
4337 struct nfs_client *client;
4338 unsigned long timestamp;
4339};
4340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341/*
4342 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4343 * standalone procedure for queueing an asynchronous RENEW.
4344 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004345static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004346{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004347 struct nfs4_renewdata *data = calldata;
4348 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004349
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004350 if (atomic_read(&clp->cl_count) > 1)
4351 nfs4_schedule_state_renewal(clp);
4352 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004353 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004354}
4355
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004356static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004358 struct nfs4_renewdata *data = calldata;
4359 struct nfs_client *clp = data->client;
4360 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004362 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004363 switch (task->tk_status) {
4364 case 0:
4365 break;
4366 case -NFS4ERR_LEASE_MOVED:
4367 nfs4_schedule_lease_moved_recovery(clp);
4368 break;
4369 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004370 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004371 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4372 return;
4373 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004374 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004375 return;
4376 }
4377 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004379 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380}
4381
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004382static const struct rpc_call_ops nfs4_renew_ops = {
4383 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004384 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004385};
4386
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004387static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
4389 struct rpc_message msg = {
4390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4391 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004392 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004394 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004396 if (renew_flags == 0)
4397 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004398 if (!atomic_inc_not_zero(&clp->cl_count))
4399 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004400 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004401 if (data == NULL)
4402 return -ENOMEM;
4403 data->client = clp;
4404 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004405 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004406 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004409static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
4411 struct rpc_message msg = {
4412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4413 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004414 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 };
4416 unsigned long now = jiffies;
4417 int status;
4418
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004419 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 if (status < 0)
4421 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004422 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return 0;
4424}
4425
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004426static inline int nfs4_server_supports_acls(struct nfs_server *server)
4427{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004428 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004429}
4430
Trond Myklebust21f498c2012-08-24 10:59:25 -04004431/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4432 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004433 * the stack.
4434 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004435#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004436
Neil Hormane9e3d722011-03-04 19:26:03 -05004437static int buf_to_pages_noslab(const void *buf, size_t buflen,
4438 struct page **pages, unsigned int *pgbase)
4439{
4440 struct page *newpage, **spages;
4441 int rc = 0;
4442 size_t len;
4443 spages = pages;
4444
4445 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004446 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004447 newpage = alloc_page(GFP_KERNEL);
4448
4449 if (newpage == NULL)
4450 goto unwind;
4451 memcpy(page_address(newpage), buf, len);
4452 buf += len;
4453 buflen -= len;
4454 *pages++ = newpage;
4455 rc++;
4456 } while (buflen != 0);
4457
4458 return rc;
4459
4460unwind:
4461 for(; rc > 0; rc--)
4462 __free_page(spages[rc-1]);
4463 return -ENOMEM;
4464}
4465
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004466struct nfs4_cached_acl {
4467 int cached;
4468 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004469 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004470};
4471
4472static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004473{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004474 struct nfs_inode *nfsi = NFS_I(inode);
4475
4476 spin_lock(&inode->i_lock);
4477 kfree(nfsi->nfs4_acl);
4478 nfsi->nfs4_acl = acl;
4479 spin_unlock(&inode->i_lock);
4480}
4481
4482static void nfs4_zap_acl_attr(struct inode *inode)
4483{
4484 nfs4_set_cached_acl(inode, NULL);
4485}
4486
4487static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4488{
4489 struct nfs_inode *nfsi = NFS_I(inode);
4490 struct nfs4_cached_acl *acl;
4491 int ret = -ENOENT;
4492
4493 spin_lock(&inode->i_lock);
4494 acl = nfsi->nfs4_acl;
4495 if (acl == NULL)
4496 goto out;
4497 if (buf == NULL) /* user is just asking for length */
4498 goto out_len;
4499 if (acl->cached == 0)
4500 goto out;
4501 ret = -ERANGE; /* see getxattr(2) man page */
4502 if (acl->len > buflen)
4503 goto out;
4504 memcpy(buf, acl->data, acl->len);
4505out_len:
4506 ret = acl->len;
4507out:
4508 spin_unlock(&inode->i_lock);
4509 return ret;
4510}
4511
Sachin Prabhu5794d212012-04-17 14:36:40 +01004512static 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 +00004513{
4514 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004515 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004516
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004517 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004518 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004519 if (acl == NULL)
4520 goto out;
4521 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004522 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004523 } else {
4524 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4525 if (acl == NULL)
4526 goto out;
4527 acl->cached = 0;
4528 }
4529 acl->len = acl_len;
4530out:
4531 nfs4_set_cached_acl(inode, acl);
4532}
4533
Andy Adamsonbf118a32011-12-07 11:55:27 -05004534/*
4535 * The getxattr API returns the required buffer length when called with a
4536 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4537 * the required buf. On a NULL buf, we send a page of data to the server
4538 * guessing that the ACL request can be serviced by a page. If so, we cache
4539 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4540 * the cache. If not so, we throw away the page, and cache the required
4541 * length. The next getxattr call will then produce another round trip to
4542 * the server, this time with the input buf of the required size.
4543 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004544static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004545{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004546 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004547 struct nfs_getaclargs args = {
4548 .fh = NFS_FH(inode),
4549 .acl_pages = pages,
4550 .acl_len = buflen,
4551 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004552 struct nfs_getaclres res = {
4553 .acl_len = buflen,
4554 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004555 struct rpc_message msg = {
4556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4557 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004558 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004559 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004560 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4561 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004562
Andy Adamsonbf118a32011-12-07 11:55:27 -05004563 /* As long as we're doing a round trip to the server anyway,
4564 * let's be prepared for a page of acl data. */
4565 if (npages == 0)
4566 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004567 if (npages > ARRAY_SIZE(pages))
4568 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004569
Andy Adamsonbf118a32011-12-07 11:55:27 -05004570 for (i = 0; i < npages; i++) {
4571 pages[i] = alloc_page(GFP_KERNEL);
4572 if (!pages[i])
4573 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004574 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004575
4576 /* for decoding across pages */
4577 res.acl_scratch = alloc_page(GFP_KERNEL);
4578 if (!res.acl_scratch)
4579 goto out_free;
4580
Andy Adamsonbf118a32011-12-07 11:55:27 -05004581 args.acl_len = npages * PAGE_SIZE;
4582 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004583
Peng Taode040be2012-01-10 22:42:47 +08004584 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004585 __func__, buf, buflen, npages, args.acl_len);
4586 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4587 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004588 if (ret)
4589 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004590
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004591 /* Handle the case where the passed-in buffer is too short */
4592 if (res.acl_flags & NFS4_ACL_TRUNC) {
4593 /* Did the user only issue a request for the acl length? */
4594 if (buf == NULL)
4595 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004596 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004597 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004598 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004599 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004600 if (buf) {
4601 if (res.acl_len > buflen) {
4602 ret = -ERANGE;
4603 goto out_free;
4604 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004605 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004606 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004607out_ok:
4608 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004609out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004610 for (i = 0; i < npages; i++)
4611 if (pages[i])
4612 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004613 if (res.acl_scratch)
4614 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004615 return ret;
4616}
4617
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004618static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4619{
4620 struct nfs4_exception exception = { };
4621 ssize_t ret;
4622 do {
4623 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004624 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004625 if (ret >= 0)
4626 break;
4627 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4628 } while (exception.retry);
4629 return ret;
4630}
4631
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004632static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4633{
4634 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004635 int ret;
4636
4637 if (!nfs4_server_supports_acls(server))
4638 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004639 ret = nfs_revalidate_inode(server, inode);
4640 if (ret < 0)
4641 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004642 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4643 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004644 ret = nfs4_read_cached_acl(inode, buf, buflen);
4645 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004646 /* -ENOENT is returned if there is no ACL or if there is an ACL
4647 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004648 return ret;
4649 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004650}
4651
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004652static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004653{
4654 struct nfs_server *server = NFS_SERVER(inode);
4655 struct page *pages[NFS4ACL_MAXPAGES];
4656 struct nfs_setaclargs arg = {
4657 .fh = NFS_FH(inode),
4658 .acl_pages = pages,
4659 .acl_len = buflen,
4660 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004661 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004662 struct rpc_message msg = {
4663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4664 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004665 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004666 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004667 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004668 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004669
4670 if (!nfs4_server_supports_acls(server))
4671 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004672 if (npages > ARRAY_SIZE(pages))
4673 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004674 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4675 if (i < 0)
4676 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004677 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004678 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004679
4680 /*
4681 * Free each page after tx, so the only ref left is
4682 * held by the network stack
4683 */
4684 for (; i > 0; i--)
4685 put_page(pages[i-1]);
4686
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004687 /*
4688 * Acl update can result in inode attribute update.
4689 * so mark the attribute cache invalid.
4690 */
4691 spin_lock(&inode->i_lock);
4692 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4693 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004694 nfs_access_zap_cache(inode);
4695 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004696 return ret;
4697}
4698
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004699static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4700{
4701 struct nfs4_exception exception = { };
4702 int err;
4703 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004704 err = __nfs4_proc_set_acl(inode, buf, buflen);
4705 trace_nfs4_set_acl(inode, err);
4706 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004707 &exception);
4708 } while (exception.retry);
4709 return err;
4710}
4711
David Quigleyaa9c2662013-05-22 12:50:44 -04004712#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4713static int _nfs4_get_security_label(struct inode *inode, void *buf,
4714 size_t buflen)
4715{
4716 struct nfs_server *server = NFS_SERVER(inode);
4717 struct nfs_fattr fattr;
4718 struct nfs4_label label = {0, 0, buflen, buf};
4719
4720 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004721 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004722 .fh = NFS_FH(inode),
4723 .bitmask = bitmask,
4724 };
4725 struct nfs4_getattr_res res = {
4726 .fattr = &fattr,
4727 .label = &label,
4728 .server = server,
4729 };
4730 struct rpc_message msg = {
4731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004732 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004733 .rpc_resp = &res,
4734 };
4735 int ret;
4736
4737 nfs_fattr_init(&fattr);
4738
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004739 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004740 if (ret)
4741 return ret;
4742 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4743 return -ENOENT;
4744 if (buflen < label.len)
4745 return -ERANGE;
4746 return 0;
4747}
4748
4749static int nfs4_get_security_label(struct inode *inode, void *buf,
4750 size_t buflen)
4751{
4752 struct nfs4_exception exception = { };
4753 int err;
4754
4755 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4756 return -EOPNOTSUPP;
4757
4758 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004759 err = _nfs4_get_security_label(inode, buf, buflen);
4760 trace_nfs4_get_security_label(inode, err);
4761 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004762 &exception);
4763 } while (exception.retry);
4764 return err;
4765}
4766
4767static int _nfs4_do_set_security_label(struct inode *inode,
4768 struct nfs4_label *ilabel,
4769 struct nfs_fattr *fattr,
4770 struct nfs4_label *olabel)
4771{
4772
4773 struct iattr sattr = {0};
4774 struct nfs_server *server = NFS_SERVER(inode);
4775 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004776 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004777 .fh = NFS_FH(inode),
4778 .iap = &sattr,
4779 .server = server,
4780 .bitmask = bitmask,
4781 .label = ilabel,
4782 };
4783 struct nfs_setattrres res = {
4784 .fattr = fattr,
4785 .label = olabel,
4786 .server = server,
4787 };
4788 struct rpc_message msg = {
4789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004790 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004791 .rpc_resp = &res,
4792 };
4793 int status;
4794
Jeff Layton12207f62013-11-01 10:49:32 -04004795 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004796
Jeff Layton12207f62013-11-01 10:49:32 -04004797 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004798 if (status)
4799 dprintk("%s failed: %d\n", __func__, status);
4800
4801 return status;
4802}
4803
4804static int nfs4_do_set_security_label(struct inode *inode,
4805 struct nfs4_label *ilabel,
4806 struct nfs_fattr *fattr,
4807 struct nfs4_label *olabel)
4808{
4809 struct nfs4_exception exception = { };
4810 int err;
4811
4812 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004813 err = _nfs4_do_set_security_label(inode, ilabel,
4814 fattr, olabel);
4815 trace_nfs4_set_security_label(inode, err);
4816 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004817 &exception);
4818 } while (exception.retry);
4819 return err;
4820}
4821
4822static int
4823nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4824{
4825 struct nfs4_label ilabel, *olabel = NULL;
4826 struct nfs_fattr fattr;
4827 struct rpc_cred *cred;
David Howells2b0143b2015-03-17 22:25:59 +00004828 struct inode *inode = d_inode(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004829 int status;
4830
4831 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4832 return -EOPNOTSUPP;
4833
4834 nfs_fattr_init(&fattr);
4835
4836 ilabel.pi = 0;
4837 ilabel.lfs = 0;
4838 ilabel.label = (char *)buf;
4839 ilabel.len = buflen;
4840
4841 cred = rpc_lookup_cred();
4842 if (IS_ERR(cred))
4843 return PTR_ERR(cred);
4844
4845 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4846 if (IS_ERR(olabel)) {
4847 status = -PTR_ERR(olabel);
4848 goto out;
4849 }
4850
4851 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4852 if (status == 0)
4853 nfs_setsecurity(inode, &fattr, olabel);
4854
4855 nfs4_label_free(olabel);
4856out:
4857 put_rpccred(cred);
4858 return status;
4859}
4860#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4861
4862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004864nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4865 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004867 struct nfs_client *clp = server->nfs_client;
4868
4869 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 return 0;
4871 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004872 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004873 case -NFS4ERR_ADMIN_REVOKED:
4874 case -NFS4ERR_BAD_STATEID:
4875 case -NFS4ERR_OPENMODE:
4876 if (state == NULL)
4877 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004878 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004879 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004880 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004881 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004882 if (state != NULL) {
4883 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004884 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004887 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004888 nfs4_schedule_lease_recovery(clp);
4889 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004890 case -NFS4ERR_MOVED:
4891 if (nfs4_schedule_migration_recovery(server) < 0)
4892 goto recovery_failed;
4893 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004894 case -NFS4ERR_LEASE_MOVED:
4895 nfs4_schedule_lease_moved_recovery(clp);
4896 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004897#if defined(CONFIG_NFS_V4_1)
4898 case -NFS4ERR_BADSESSION:
4899 case -NFS4ERR_BADSLOT:
4900 case -NFS4ERR_BAD_HIGH_SLOT:
4901 case -NFS4ERR_DEADSESSION:
4902 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4903 case -NFS4ERR_SEQ_FALSE_RETRY:
4904 case -NFS4ERR_SEQ_MISORDERED:
4905 dprintk("%s ERROR %d, Reset session\n", __func__,
4906 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004907 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004908 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004909#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004911 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10004912 rpc_delay(task, nfs4_update_delay(timeout));
4913 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05004914 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004916 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004918 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
4920 task->tk_status = nfs4_map_errors(task->tk_status);
4921 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004922recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004923 task->tk_status = -EIO;
4924 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004925wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004926 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004927 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4928 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004929 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4930 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004931restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004932 task->tk_status = 0;
4933 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934}
4935
Chuck Leverf0920752012-05-21 22:45:41 -04004936static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4937 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004938{
4939 __be32 verf[2];
4940
Chuck Lever2c820d92012-05-21 22:45:33 -04004941 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4942 /* An impossible timestamp guarantees this value
4943 * will never match a generated boot time. */
4944 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004945 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004946 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004947 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004948 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4949 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004950 }
Chuck Levercd937102012-03-02 17:14:31 -05004951 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4952}
4953
Jeff Laytona3192682015-06-09 19:43:59 -04004954static int
4955nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04004956{
Jeff Laytona3192682015-06-09 19:43:59 -04004957 int result;
4958 size_t len;
4959 char *str;
4960 bool retried = false;
Chuck Levere984a552012-09-14 17:24:21 -04004961
Trond Myklebustceb3a162015-01-03 15:16:04 -05004962 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04004963 return 0;
4964retry:
4965 rcu_read_lock();
4966 len = 10 + strlen(clp->cl_ipaddr) + 1 +
4967 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
4968 1 +
4969 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
4970 1;
4971 rcu_read_unlock();
4972
4973 if (len > NFS4_OPAQUE_LIMIT + 1)
4974 return -EINVAL;
4975
4976 /*
4977 * Since this string is allocated at mount time, and held until the
4978 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
4979 * about a memory-reclaim deadlock.
4980 */
4981 str = kmalloc(len, GFP_KERNEL);
4982 if (!str)
4983 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05004984
Chuck Levere984a552012-09-14 17:24:21 -04004985 rcu_read_lock();
Jeff Laytona3192682015-06-09 19:43:59 -04004986 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
4987 clp->cl_ipaddr,
4988 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
4989 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04004990 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04004991
4992 /* Did something change? */
4993 if (result >= len) {
4994 kfree(str);
4995 if (retried)
4996 return -EINVAL;
4997 retried = true;
4998 goto retry;
4999 }
5000 clp->cl_owner_id = str;
5001 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005002}
5003
Jeff Layton873e3852015-06-09 19:44:00 -04005004static int
5005nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005006{
Jeff Layton873e3852015-06-09 19:44:00 -04005007 int result;
5008 size_t len;
5009 char *str;
5010
5011 len = 10 + 10 + 1 + 10 + 1 +
5012 strlen(nfs4_client_id_uniquifier) + 1 +
5013 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5014
5015 if (len > NFS4_OPAQUE_LIMIT + 1)
5016 return -EINVAL;
5017
5018 /*
5019 * Since this string is allocated at mount time, and held until the
5020 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5021 * about a memory-reclaim deadlock.
5022 */
5023 str = kmalloc(len, GFP_KERNEL);
5024 if (!str)
5025 return -ENOMEM;
5026
5027 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5028 clp->rpc_ops->version, clp->cl_minorversion,
5029 nfs4_client_id_uniquifier,
5030 clp->cl_rpcclient->cl_nodename);
5031 if (result >= len) {
5032 kfree(str);
5033 return -EINVAL;
5034 }
5035 clp->cl_owner_id = str;
5036 return 0;
5037}
5038
5039static int
5040nfs4_init_uniform_client_string(struct nfs_client *clp)
5041{
5042 int result;
5043 size_t len;
5044 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005045
5046 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005047 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005048
5049 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005050 return nfs4_init_uniquifier_client_string(clp);
5051
5052 len = 10 + 10 + 1 + 10 + 1 +
5053 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5054
5055 if (len > NFS4_OPAQUE_LIMIT + 1)
5056 return -EINVAL;
5057
5058 /*
5059 * Since this string is allocated at mount time, and held until the
5060 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5061 * about a memory-reclaim deadlock.
5062 */
5063 str = kmalloc(len, GFP_KERNEL);
5064 if (!str)
5065 return -ENOMEM;
5066
5067 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5068 clp->rpc_ops->version, clp->cl_minorversion,
5069 clp->cl_rpcclient->cl_nodename);
5070 if (result >= len) {
5071 kfree(str);
5072 return -EINVAL;
5073 }
5074 clp->cl_owner_id = str;
5075 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005076}
5077
Chuck Lever706cb8d2014-03-12 12:51:47 -04005078/*
5079 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5080 * services. Advertise one based on the address family of the
5081 * clientaddr.
5082 */
5083static unsigned int
5084nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5085{
5086 if (strchr(clp->cl_ipaddr, ':') != NULL)
5087 return scnprintf(buf, len, "tcp6");
5088 else
5089 return scnprintf(buf, len, "tcp");
5090}
5091
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005092static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5093{
5094 struct nfs4_setclientid *sc = calldata;
5095
5096 if (task->tk_status == 0)
5097 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5098}
5099
5100static const struct rpc_call_ops nfs4_setclientid_ops = {
5101 .rpc_call_done = nfs4_setclientid_done,
5102};
5103
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005104/**
5105 * nfs4_proc_setclientid - Negotiate client ID
5106 * @clp: state data structure
5107 * @program: RPC program for NFSv4 callback service
5108 * @port: IP port number for NFS4 callback service
5109 * @cred: RPC credential to use for this call
5110 * @res: where to place the result
5111 *
5112 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5113 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005114int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5115 unsigned short port, struct rpc_cred *cred,
5116 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117{
5118 nfs4_verifier sc_verifier;
5119 struct nfs4_setclientid setclientid = {
5120 .sc_verifier = &sc_verifier,
5121 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005122 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 };
5124 struct rpc_message msg = {
5125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5126 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005127 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005128 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005130 struct rpc_task *task;
5131 struct rpc_task_setup task_setup_data = {
5132 .rpc_client = clp->cl_rpcclient,
5133 .rpc_message = &msg,
5134 .callback_ops = &nfs4_setclientid_ops,
5135 .callback_data = &setclientid,
5136 .flags = RPC_TASK_TIMEOUT,
5137 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005138 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139
Chuck Leverde734832012-07-11 16:30:50 -04005140 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005141 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005142
5143 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5144 status = nfs4_init_uniform_client_string(clp);
5145 else
Jeff Laytona3192682015-06-09 19:43:59 -04005146 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005147
5148 if (status)
5149 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005150
Chuck Leverde734832012-07-11 16:30:50 -04005151 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005152 setclientid.sc_netid_len =
5153 nfs4_init_callback_netid(clp,
5154 setclientid.sc_netid,
5155 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005156 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005157 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 clp->cl_ipaddr, port >> 8, port & 255);
5159
Jeff Layton3a6bb732015-06-09 19:43:57 -04005160 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005161 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005162 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005163 task = rpc_run_task(&task_setup_data);
5164 if (IS_ERR(task)) {
5165 status = PTR_ERR(task);
5166 goto out;
5167 }
5168 status = task->tk_status;
5169 if (setclientid.sc_cred) {
5170 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5171 put_rpccred(setclientid.sc_cred);
5172 }
5173 rpc_put_task(task);
5174out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005175 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005176 dprintk("NFS reply setclientid: %d\n", status);
5177 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178}
5179
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005180/**
5181 * nfs4_proc_setclientid_confirm - Confirm client ID
5182 * @clp: state data structure
5183 * @res: result of a previous SETCLIENTID
5184 * @cred: RPC credential to use for this call
5185 *
5186 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5187 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005188int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005189 struct nfs4_setclientid_res *arg,
5190 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 struct rpc_message msg = {
5193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005194 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005195 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 int status;
5198
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005199 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5200 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5201 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005202 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005203 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005204 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 return status;
5206}
5207
Trond Myklebustfe650402006-01-03 09:55:18 +01005208struct nfs4_delegreturndata {
5209 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005210 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005211 struct nfs_fh fh;
5212 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005213 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005214 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005215 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005216 struct inode *inode;
5217 bool roc;
5218 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005219};
5220
Trond Myklebustfe650402006-01-03 09:55:18 +01005221static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5222{
5223 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005224
Trond Myklebust14516c32010-07-31 14:29:06 -04005225 if (!nfs4_sequence_done(task, &data->res.seq_res))
5226 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005227
Trond Myklebustca8acf82013-08-13 10:36:56 -04005228 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005229 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005230 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005231 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005232 case -NFS4ERR_ADMIN_REVOKED:
5233 case -NFS4ERR_DELEG_REVOKED:
5234 case -NFS4ERR_BAD_STATEID:
5235 case -NFS4ERR_OLD_STATEID:
5236 case -NFS4ERR_STALE_STATEID:
5237 case -NFS4ERR_EXPIRED:
5238 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005239 if (data->roc)
5240 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005241 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005242 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005243 if (nfs4_async_handle_error(task, data->res.server,
5244 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005245 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005246 return;
5247 }
5248 }
5249 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005250}
5251
5252static void nfs4_delegreturn_release(void *calldata)
5253{
Peng Tao039b7562014-07-03 13:05:02 +08005254 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005255 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005256
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005257 if (inode) {
5258 if (data->roc)
5259 pnfs_roc_release(inode);
5260 nfs_iput_and_deactive(inode);
5261 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005262 kfree(calldata);
5263}
5264
Andy Adamson938e1012009-04-01 09:22:28 -04005265static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5266{
5267 struct nfs4_delegreturndata *d_data;
5268
5269 d_data = (struct nfs4_delegreturndata *)data;
5270
Peng Tao039b7562014-07-03 13:05:02 +08005271 if (d_data->roc &&
5272 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5273 return;
5274
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005275 nfs4_setup_sequence(d_data->res.server,
5276 &d_data->args.seq_args,
5277 &d_data->res.seq_res,
5278 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005279}
Andy Adamson938e1012009-04-01 09:22:28 -04005280
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005281static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005282 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005283 .rpc_call_done = nfs4_delegreturn_done,
5284 .rpc_release = nfs4_delegreturn_release,
5285};
5286
Trond Myklebuste6f81072008-01-24 18:14:34 -05005287static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005288{
5289 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005290 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005291 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005292 struct rpc_message msg = {
5293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5294 .rpc_cred = cred,
5295 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005296 struct rpc_task_setup task_setup_data = {
5297 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005298 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005299 .callback_ops = &nfs4_delegreturn_ops,
5300 .flags = RPC_TASK_ASYNC,
5301 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005302 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005303
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005304 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005305 if (data == NULL)
5306 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005307 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005308 data->args.fhandle = &data->fh;
5309 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005310 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005311 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005312 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005313 data->res.fattr = &data->fattr;
5314 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005315 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005316 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005317 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005318 data->inode = nfs_igrab_and_active(inode);
5319 if (data->inode)
5320 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005321
Trond Myklebustc970aa82007-07-14 15:39:59 -04005322 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005323 msg.rpc_argp = &data->args;
5324 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005325 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005326 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005327 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005328 if (!issync)
5329 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005330 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005331 if (status != 0)
5332 goto out;
5333 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005334 if (status == 0)
5335 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5336 else
5337 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005338out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005339 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005340 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341}
5342
Trond Myklebuste6f81072008-01-24 18:14:34 -05005343int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344{
5345 struct nfs_server *server = NFS_SERVER(inode);
5346 struct nfs4_exception exception = { };
5347 int err;
5348 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005349 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005350 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 switch (err) {
5352 case -NFS4ERR_STALE_STATEID:
5353 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 case 0:
5355 return 0;
5356 }
5357 err = nfs4_handle_exception(server, err, &exception);
5358 } while (exception.retry);
5359 return err;
5360}
5361
5362#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5363#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5364
5365/*
5366 * sleep, with exponential backoff, and retry the LOCK operation.
5367 */
5368static unsigned long
5369nfs4_set_lock_task_retry(unsigned long timeout)
5370{
Colin Cross416ad3c2013-05-06 23:50:06 +00005371 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 timeout <<= 1;
5373 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5374 return NFS4_LOCK_MAXTIMEOUT;
5375 return timeout;
5376}
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5379{
5380 struct inode *inode = state->inode;
5381 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005382 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005383 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005385 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005387 struct nfs_lockt_res res = {
5388 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 };
5390 struct rpc_message msg = {
5391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5392 .rpc_argp = &arg,
5393 .rpc_resp = &res,
5394 .rpc_cred = state->owner->so_cred,
5395 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 struct nfs4_lock_state *lsp;
5397 int status;
5398
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005399 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005400 status = nfs4_set_lock_state(state, request);
5401 if (status != 0)
5402 goto out;
5403 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005404 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005405 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005406 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005407 switch (status) {
5408 case 0:
5409 request->fl_type = F_UNLCK;
5410 break;
5411 case -NFS4ERR_DENIED:
5412 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005414 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005415 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005416out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 return status;
5418}
5419
5420static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5421{
5422 struct nfs4_exception exception = { };
5423 int err;
5424
5425 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005426 err = _nfs4_proc_getlk(state, cmd, request);
5427 trace_nfs4_get_lock(request, state, cmd, err);
5428 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 &exception);
5430 } while (exception.retry);
5431 return err;
5432}
5433
5434static int do_vfs_lock(struct file *file, struct file_lock *fl)
5435{
5436 int res = 0;
5437 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5438 case FL_POSIX:
5439 res = posix_lock_file_wait(file, fl);
5440 break;
5441 case FL_FLOCK:
5442 res = flock_lock_file_wait(file, fl);
5443 break;
5444 default:
5445 BUG();
5446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 return res;
5448}
5449
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005450struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005451 struct nfs_locku_args arg;
5452 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005453 struct nfs4_lock_state *lsp;
5454 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005455 struct file_lock fl;
5456 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005457 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005458};
5459
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005460static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5461 struct nfs_open_context *ctx,
5462 struct nfs4_lock_state *lsp,
5463 struct nfs_seqid *seqid)
5464{
5465 struct nfs4_unlockdata *p;
5466 struct inode *inode = lsp->ls_state->inode;
5467
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005468 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005469 if (p == NULL)
5470 return NULL;
5471 p->arg.fh = NFS_FH(inode);
5472 p->arg.fl = &p->fl;
5473 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005474 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005475 p->lsp = lsp;
5476 atomic_inc(&lsp->ls_count);
5477 /* Ensure we don't close file until we're done freeing locks! */
5478 p->ctx = get_nfs_open_context(ctx);
5479 memcpy(&p->fl, fl, sizeof(p->fl));
5480 p->server = NFS_SERVER(inode);
5481 return p;
5482}
5483
Trond Myklebust06f814a2006-01-03 09:55:07 +01005484static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005485{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005486 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005487 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005488 nfs4_put_lock_state(calldata->lsp);
5489 put_nfs_open_context(calldata->ctx);
5490 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005491}
5492
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005493static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005494{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005495 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005496
Trond Myklebust14516c32010-07-31 14:29:06 -04005497 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5498 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005499 switch (task->tk_status) {
5500 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005501 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005502 do_vfs_lock(calldata->fl.fl_file, &calldata->fl);
5503 if (nfs4_update_lock_stateid(calldata->lsp,
5504 &calldata->res.stateid))
5505 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005506 case -NFS4ERR_BAD_STATEID:
5507 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005508 case -NFS4ERR_STALE_STATEID:
5509 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005510 if (!nfs4_stateid_match(&calldata->arg.stateid,
5511 &calldata->lsp->ls_stateid))
5512 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005513 break;
5514 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005515 if (nfs4_async_handle_error(task, calldata->server,
5516 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005517 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005518 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005519 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005520}
5521
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005522static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005523{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005524 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005526 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005527 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005528 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005529 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005530 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005531 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005532 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005533 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005534 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005535 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005536 &calldata->res.seq_res,
5537 task) != 0)
5538 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005539 return;
5540out_no_action:
5541 task->tk_action = NULL;
5542out_wait:
5543 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544}
5545
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005546static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005547 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005548 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005549 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005550};
5551
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005552static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5553 struct nfs_open_context *ctx,
5554 struct nfs4_lock_state *lsp,
5555 struct nfs_seqid *seqid)
5556{
5557 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005558 struct rpc_message msg = {
5559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5560 .rpc_cred = ctx->cred,
5561 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005562 struct rpc_task_setup task_setup_data = {
5563 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005564 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005565 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005566 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005567 .flags = RPC_TASK_ASYNC,
5568 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005569
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005570 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5571 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5572
Frank Filz137d6ac2007-07-09 15:32:29 -07005573 /* Ensure this is an unlock - when canceling a lock, the
5574 * canceled lock is passed in, and it won't be an unlock.
5575 */
5576 fl->fl_type = F_UNLCK;
5577
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005578 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5579 if (data == NULL) {
5580 nfs_free_seqid(seqid);
5581 return ERR_PTR(-ENOMEM);
5582 }
5583
Chuck Levera9c92d62013-08-09 12:48:18 -04005584 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005585 msg.rpc_argp = &data->arg;
5586 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005587 task_setup_data.callback_data = data;
5588 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005589}
5590
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5592{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005593 struct inode *inode = state->inode;
5594 struct nfs4_state_owner *sp = state->owner;
5595 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005596 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005597 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005598 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005599 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005600 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005601 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
Trond Myklebust9b073572006-06-29 16:38:34 -04005603 status = nfs4_set_lock_state(state, request);
5604 /* Unlock _before_ we do the RPC call */
5605 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005606 /* Exclude nfs_delegation_claim_locks() */
5607 mutex_lock(&sp->so_delegreturn_mutex);
5608 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005609 down_read(&nfsi->rwsem);
5610 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5611 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005612 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005613 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005614 }
5615 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005616 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005617 if (status != 0)
5618 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005619 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005620 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005621 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5622 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005623 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5624 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005625 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005626 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005627 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005628 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005629 status = PTR_ERR(task);
5630 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005631 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005632 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005633 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005634out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005635 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005636 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005637 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638}
5639
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005640struct nfs4_lockdata {
5641 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005642 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005643 struct nfs4_lock_state *lsp;
5644 struct nfs_open_context *ctx;
5645 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005646 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005647 int rpc_status;
5648 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005649 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005650};
5651
5652static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005653 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5654 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005655{
5656 struct nfs4_lockdata *p;
5657 struct inode *inode = lsp->ls_state->inode;
5658 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005659 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005660
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005661 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005662 if (p == NULL)
5663 return NULL;
5664
5665 p->arg.fh = NFS_FH(inode);
5666 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005667 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005668 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005669 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005670 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5671 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005672 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005673 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005674 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005675 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005676 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005677 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005678 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005679 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005680 atomic_inc(&lsp->ls_count);
5681 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005682 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005683 memcpy(&p->fl, fl, sizeof(p->fl));
5684 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005685out_free_seqid:
5686 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005687out_free:
5688 kfree(p);
5689 return NULL;
5690}
5691
5692static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5693{
5694 struct nfs4_lockdata *data = calldata;
5695 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
Harvey Harrison3110ff82008-05-02 13:42:44 -07005697 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005698 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005699 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005700 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005701 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005702 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005703 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005704 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005705 nfs4_stateid_copy(&data->arg.open_stateid,
5706 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005707 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005708 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005709 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005710 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005711 nfs4_stateid_copy(&data->arg.lock_stateid,
5712 &data->lsp->ls_stateid);
5713 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005714 if (!nfs4_valid_open_stateid(state)) {
5715 data->rpc_status = -EBADF;
5716 task->tk_action = NULL;
5717 goto out_release_open_seqid;
5718 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005719 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005720 if (nfs4_setup_sequence(data->server,
5721 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005722 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005723 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005724 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005725out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005726 nfs_release_seqid(data->arg.open_seqid);
5727out_release_lock_seqid:
5728 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005729out_wait:
5730 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005731 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005732}
5733
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005734static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5735{
5736 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005737 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005738
Harvey Harrison3110ff82008-05-02 13:42:44 -07005739 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005740
Trond Myklebust14516c32010-07-31 14:29:06 -04005741 if (!nfs4_sequence_done(task, &data->res.seq_res))
5742 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005743
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005744 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005745 switch (task->tk_status) {
5746 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005747 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005748 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005749 if (data->arg.new_lock) {
5750 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
5751 if (do_vfs_lock(data->fl.fl_file, &data->fl) < 0) {
5752 rpc_restart_call_prepare(task);
5753 break;
5754 }
5755 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005756 if (data->arg.new_lock_owner != 0) {
5757 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5758 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5759 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5760 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5761 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005762 break;
5763 case -NFS4ERR_BAD_STATEID:
5764 case -NFS4ERR_OLD_STATEID:
5765 case -NFS4ERR_STALE_STATEID:
5766 case -NFS4ERR_EXPIRED:
5767 if (data->arg.new_lock_owner != 0) {
5768 if (!nfs4_stateid_match(&data->arg.open_stateid,
5769 &lsp->ls_state->open_stateid))
5770 rpc_restart_call_prepare(task);
5771 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5772 &lsp->ls_stateid))
5773 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005774 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005775 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005776}
5777
5778static void nfs4_lock_release(void *calldata)
5779{
5780 struct nfs4_lockdata *data = calldata;
5781
Harvey Harrison3110ff82008-05-02 13:42:44 -07005782 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005783 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005784 if (data->cancelled != 0) {
5785 struct rpc_task *task;
5786 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5787 data->arg.lock_seqid);
5788 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005789 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005790 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005791 } else
5792 nfs_free_seqid(data->arg.lock_seqid);
5793 nfs4_put_lock_state(data->lsp);
5794 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005795 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005796 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005797 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005798}
5799
5800static const struct rpc_call_ops nfs4_lock_ops = {
5801 .rpc_call_prepare = nfs4_lock_prepare,
5802 .rpc_call_done = nfs4_lock_done,
5803 .rpc_release = nfs4_lock_release,
5804};
5805
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005806static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5807{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005808 switch (error) {
5809 case -NFS4ERR_ADMIN_REVOKED:
5810 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005811 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005812 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005813 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005814 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005815 break;
5816 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005817 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005818 case -NFS4ERR_EXPIRED:
5819 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005820 };
5821}
5822
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005823static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005824{
5825 struct nfs4_lockdata *data;
5826 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005827 struct rpc_message msg = {
5828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5829 .rpc_cred = state->owner->so_cred,
5830 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005831 struct rpc_task_setup task_setup_data = {
5832 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005833 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005834 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005835 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005836 .flags = RPC_TASK_ASYNC,
5837 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005838 int ret;
5839
Harvey Harrison3110ff82008-05-02 13:42:44 -07005840 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005841 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005842 fl->fl_u.nfs4_fl.owner,
5843 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005844 if (data == NULL)
5845 return -ENOMEM;
5846 if (IS_SETLKW(cmd))
5847 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005848 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005849 msg.rpc_argp = &data->arg;
5850 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005851 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005852 if (recovery_type > NFS_LOCK_NEW) {
5853 if (recovery_type == NFS_LOCK_RECLAIM)
5854 data->arg.reclaim = NFS_LOCK_RECLAIM;
5855 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005856 } else
5857 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005858 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005859 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005860 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005861 ret = nfs4_wait_for_completion_rpc_task(task);
5862 if (ret == 0) {
5863 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005864 if (ret)
5865 nfs4_handle_setlk_error(data->server, data->lsp,
5866 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005867 } else
5868 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005869 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005870 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005871 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872}
5873
5874static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5875{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005876 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005877 struct nfs4_exception exception = {
5878 .inode = state->inode,
5879 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005880 int err;
5881
5882 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005883 /* Cache the lock if possible... */
5884 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5885 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005886 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005887 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005888 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005889 break;
5890 nfs4_handle_exception(server, err, &exception);
5891 } while (exception.retry);
5892 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893}
5894
5895static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5896{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005897 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005898 struct nfs4_exception exception = {
5899 .inode = state->inode,
5900 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005901 int err;
5902
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005903 err = nfs4_set_lock_state(state, request);
5904 if (err != 0)
5905 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005906 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005907 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5908 return 0;
5909 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005910 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005911 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5912 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005913 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005914 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005915 switch (err) {
5916 default:
5917 goto out;
5918 case -NFS4ERR_GRACE:
5919 case -NFS4ERR_DELAY:
5920 nfs4_handle_exception(server, err, &exception);
5921 err = 0;
5922 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005923 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005924out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005925 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926}
5927
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005928#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005929/**
5930 * nfs41_check_expired_locks - possibly free a lock stateid
5931 *
5932 * @state: NFSv4 state for an inode
5933 *
5934 * Returns NFS_OK if recovery for this stateid is now finished.
5935 * Otherwise a negative NFS4ERR value is returned.
5936 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005937static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005938{
Chuck Levereb64cf92012-07-11 16:30:05 -04005939 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005940 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005941 struct nfs_server *server = NFS_SERVER(state->inode);
5942
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005943 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005944 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005945 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5946
5947 status = nfs41_test_stateid(server,
5948 &lsp->ls_stateid,
5949 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005950 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005951 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005952 /* Free the stateid unless the server
5953 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005954 if (status != -NFS4ERR_BAD_STATEID)
5955 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005956 &lsp->ls_stateid,
5957 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005958 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005959 ret = status;
5960 }
5961 }
5962 };
5963
5964 return ret;
5965}
5966
5967static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5968{
5969 int status = NFS_OK;
5970
5971 if (test_bit(LK_STATE_IN_USE, &state->flags))
5972 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005973 if (status != NFS_OK)
5974 status = nfs4_lock_expired(state, request);
5975 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005976}
5977#endif
5978
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5980{
Trond Myklebust19e03c52008-12-23 15:21:44 -05005981 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005982 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005983 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005985 if ((fl_flags & FL_POSIX) &&
5986 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5987 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005988 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005989 status = nfs4_set_lock_state(state, request);
5990 if (status != 0)
5991 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005992 request->fl_flags |= FL_ACCESS;
5993 status = do_vfs_lock(request->fl_file, request);
5994 if (status < 0)
5995 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005996 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005997 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005998 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005999 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006000 request->fl_flags = fl_flags & ~FL_SLEEP;
6001 status = do_vfs_lock(request->fl_file, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006002 up_read(&nfsi->rwsem);
6003 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006004 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006005 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006006 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006007out:
6008 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 return status;
6010}
6011
6012static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6013{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006014 struct nfs4_exception exception = {
6015 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006016 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006017 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 int err;
6019
6020 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006021 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006022 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07006023 if (err == -NFS4ERR_DENIED)
6024 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006026 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 } while (exception.retry);
6028 return err;
6029}
6030
6031static int
6032nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6033{
6034 struct nfs_open_context *ctx;
6035 struct nfs4_state *state;
6036 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6037 int status;
6038
6039 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006040 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 state = ctx->state;
6042
6043 if (request->fl_start < 0 || request->fl_end < 0)
6044 return -EINVAL;
6045
Trond Myklebustd9531262009-07-21 19:22:38 -04006046 if (IS_GETLK(cmd)) {
6047 if (state != NULL)
6048 return nfs4_proc_getlk(state, F_GETLK, request);
6049 return 0;
6050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
6052 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6053 return -EINVAL;
6054
Trond Myklebustd9531262009-07-21 19:22:38 -04006055 if (request->fl_type == F_UNLCK) {
6056 if (state != NULL)
6057 return nfs4_proc_unlck(state, cmd, request);
6058 return 0;
6059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Trond Myklebustd9531262009-07-21 19:22:38 -04006061 if (state == NULL)
6062 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006063 /*
6064 * Don't rely on the VFS having checked the file open mode,
6065 * since it won't do this for flock() locks.
6066 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006067 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006068 case F_RDLCK:
6069 if (!(filp->f_mode & FMODE_READ))
6070 return -EBADF;
6071 break;
6072 case F_WRLCK:
6073 if (!(filp->f_mode & FMODE_WRITE))
6074 return -EBADF;
6075 }
6076
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 do {
6078 status = nfs4_proc_setlk(state, cmd, request);
6079 if ((status != -EAGAIN) || IS_SETLK(cmd))
6080 break;
6081 timeout = nfs4_set_lock_task_retry(timeout);
6082 status = -ERESTARTSYS;
6083 if (signalled())
6084 break;
6085 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 return status;
6087}
6088
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006089int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006090{
6091 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006092 int err;
6093
6094 err = nfs4_set_lock_state(state, fl);
6095 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006096 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006097 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006098 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006099}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006100
Trond Myklebustcf470c32012-03-07 13:49:12 -05006101struct nfs_release_lockowner_data {
6102 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006103 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006104 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006105 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006106 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006107};
6108
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006109static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6110{
6111 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006112 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006113 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6114 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006115 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006116 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006117}
6118
6119static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6120{
6121 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006122 struct nfs_server *server = data->server;
6123
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006124 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006125
6126 switch (task->tk_status) {
6127 case 0:
6128 renew_lease(server, data->timestamp);
6129 break;
6130 case -NFS4ERR_STALE_CLIENTID:
6131 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006132 nfs4_schedule_lease_recovery(server->nfs_client);
6133 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006134 case -NFS4ERR_LEASE_MOVED:
6135 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006136 if (nfs4_async_handle_error(task, server,
6137 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006138 rpc_restart_call_prepare(task);
6139 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006140}
6141
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006142static void nfs4_release_lockowner_release(void *calldata)
6143{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006144 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006145 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006146 kfree(calldata);
6147}
6148
Trond Myklebust17280172012-03-11 13:11:00 -04006149static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006150 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6151 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006152 .rpc_release = nfs4_release_lockowner_release,
6153};
6154
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006155static void
6156nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006157{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006158 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006159 struct rpc_message msg = {
6160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6161 };
6162
6163 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006164 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006165
Trond Myklebustcf470c32012-03-07 13:49:12 -05006166 data = kmalloc(sizeof(*data), GFP_NOFS);
6167 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006168 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006169 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006170 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006171 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6172 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6173 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006174
Trond Myklebustcf470c32012-03-07 13:49:12 -05006175 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006176 msg.rpc_resp = &data->res;
6177 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006178 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006179}
6180
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006181#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6182
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006183static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6184 const void *buf, size_t buflen,
6185 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006186{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006187 if (strcmp(key, "") != 0)
6188 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006189
David Howells2b0143b2015-03-17 22:25:59 +00006190 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006191}
6192
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006193static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6194 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006195{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006196 if (strcmp(key, "") != 0)
6197 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006198
David Howells2b0143b2015-03-17 22:25:59 +00006199 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006200}
6201
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006202static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6203 size_t list_len, const char *name,
6204 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006205{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006206 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006207
David Howells2b0143b2015-03-17 22:25:59 +00006208 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
J. Bruce Fields096455a2006-03-20 23:23:42 -05006209 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006210
6211 if (list && len <= list_len)
6212 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006213 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006214}
6215
David Quigleyc9bccef2013-05-22 12:50:45 -04006216#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6217static inline int nfs4_server_supports_labels(struct nfs_server *server)
6218{
6219 return server->caps & NFS_CAP_SECURITY_LABEL;
6220}
6221
6222static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6223 const void *buf, size_t buflen,
6224 int flags, int type)
6225{
6226 if (security_ismaclabel(key))
6227 return nfs4_set_security_label(dentry, buf, buflen);
6228
6229 return -EOPNOTSUPP;
6230}
6231
6232static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6233 void *buf, size_t buflen, int type)
6234{
6235 if (security_ismaclabel(key))
David Howells2b0143b2015-03-17 22:25:59 +00006236 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006237 return -EOPNOTSUPP;
6238}
6239
6240static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6241 size_t list_len, const char *name,
6242 size_t name_len, int type)
6243{
6244 size_t len = 0;
6245
David Howells2b0143b2015-03-17 22:25:59 +00006246 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6247 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
David Quigleyc9bccef2013-05-22 12:50:45 -04006248 if (list && len <= list_len)
David Howells2b0143b2015-03-17 22:25:59 +00006249 security_inode_listsecurity(d_inode(dentry), list, len);
David Quigleyc9bccef2013-05-22 12:50:45 -04006250 }
6251 return len;
6252}
6253
6254static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6255 .prefix = XATTR_SECURITY_PREFIX,
6256 .list = nfs4_xattr_list_nfs4_label,
6257 .get = nfs4_xattr_get_nfs4_label,
6258 .set = nfs4_xattr_set_nfs4_label,
6259};
6260#endif
6261
6262
Andy Adamson533eb462011-06-13 18:25:56 -04006263/*
6264 * nfs_fhget will use either the mounted_on_fileid or the fileid
6265 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006266static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6267{
Andy Adamson533eb462011-06-13 18:25:56 -04006268 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6269 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6270 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006271 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006272 return;
6273
6274 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006275 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006276 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6277 fattr->nlink = 2;
6278}
6279
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006280static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6281 const struct qstr *name,
6282 struct nfs4_fs_locations *fs_locations,
6283 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006284{
6285 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006286 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006287 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006288 };
6289 struct nfs4_fs_locations_arg args = {
6290 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006291 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006292 .page = page,
6293 .bitmask = bitmask,
6294 };
Benny Halevy22958462009-04-01 09:22:02 -04006295 struct nfs4_fs_locations_res res = {
6296 .fs_locations = fs_locations,
6297 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006298 struct rpc_message msg = {
6299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6300 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006301 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006302 };
6303 int status;
6304
Harvey Harrison3110ff82008-05-02 13:42:44 -07006305 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006306
6307 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6308 * is not supported */
6309 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6310 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6311 else
6312 bitmask[0] |= FATTR4_WORD0_FILEID;
6313
Trond Myklebustc228fd32007-01-13 02:28:11 -05006314 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006315 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006316 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006317 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006318 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006319 return status;
6320}
6321
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006322int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6323 const struct qstr *name,
6324 struct nfs4_fs_locations *fs_locations,
6325 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006326{
6327 struct nfs4_exception exception = { };
6328 int err;
6329 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006330 err = _nfs4_proc_fs_locations(client, dir, name,
6331 fs_locations, page);
6332 trace_nfs4_get_fs_locations(dir, name, err);
6333 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006334 &exception);
6335 } while (exception.retry);
6336 return err;
6337}
6338
Chuck Leverb03d7352013-10-17 14:12:50 -04006339/*
6340 * This operation also signals the server that this client is
6341 * performing migration recovery. The server can stop returning
6342 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6343 * appended to this compound to identify the client ID which is
6344 * performing recovery.
6345 */
6346static int _nfs40_proc_get_locations(struct inode *inode,
6347 struct nfs4_fs_locations *locations,
6348 struct page *page, struct rpc_cred *cred)
6349{
6350 struct nfs_server *server = NFS_SERVER(inode);
6351 struct rpc_clnt *clnt = server->client;
6352 u32 bitmask[2] = {
6353 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6354 };
6355 struct nfs4_fs_locations_arg args = {
6356 .clientid = server->nfs_client->cl_clientid,
6357 .fh = NFS_FH(inode),
6358 .page = page,
6359 .bitmask = bitmask,
6360 .migration = 1, /* skip LOOKUP */
6361 .renew = 1, /* append RENEW */
6362 };
6363 struct nfs4_fs_locations_res res = {
6364 .fs_locations = locations,
6365 .migration = 1,
6366 .renew = 1,
6367 };
6368 struct rpc_message msg = {
6369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6370 .rpc_argp = &args,
6371 .rpc_resp = &res,
6372 .rpc_cred = cred,
6373 };
6374 unsigned long now = jiffies;
6375 int status;
6376
6377 nfs_fattr_init(&locations->fattr);
6378 locations->server = server;
6379 locations->nlocations = 0;
6380
6381 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6382 nfs4_set_sequence_privileged(&args.seq_args);
6383 status = nfs4_call_sync_sequence(clnt, server, &msg,
6384 &args.seq_args, &res.seq_res);
6385 if (status)
6386 return status;
6387
6388 renew_lease(server, now);
6389 return 0;
6390}
6391
6392#ifdef CONFIG_NFS_V4_1
6393
6394/*
6395 * This operation also signals the server that this client is
6396 * performing migration recovery. The server can stop asserting
6397 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6398 * performing this operation is identified in the SEQUENCE
6399 * operation in this compound.
6400 *
6401 * When the client supports GETATTR(fs_locations_info), it can
6402 * be plumbed in here.
6403 */
6404static int _nfs41_proc_get_locations(struct inode *inode,
6405 struct nfs4_fs_locations *locations,
6406 struct page *page, struct rpc_cred *cred)
6407{
6408 struct nfs_server *server = NFS_SERVER(inode);
6409 struct rpc_clnt *clnt = server->client;
6410 u32 bitmask[2] = {
6411 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6412 };
6413 struct nfs4_fs_locations_arg args = {
6414 .fh = NFS_FH(inode),
6415 .page = page,
6416 .bitmask = bitmask,
6417 .migration = 1, /* skip LOOKUP */
6418 };
6419 struct nfs4_fs_locations_res res = {
6420 .fs_locations = locations,
6421 .migration = 1,
6422 };
6423 struct rpc_message msg = {
6424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6425 .rpc_argp = &args,
6426 .rpc_resp = &res,
6427 .rpc_cred = cred,
6428 };
6429 int status;
6430
6431 nfs_fattr_init(&locations->fattr);
6432 locations->server = server;
6433 locations->nlocations = 0;
6434
6435 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6436 nfs4_set_sequence_privileged(&args.seq_args);
6437 status = nfs4_call_sync_sequence(clnt, server, &msg,
6438 &args.seq_args, &res.seq_res);
6439 if (status == NFS4_OK &&
6440 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6441 status = -NFS4ERR_LEASE_MOVED;
6442 return status;
6443}
6444
6445#endif /* CONFIG_NFS_V4_1 */
6446
6447/**
6448 * nfs4_proc_get_locations - discover locations for a migrated FSID
6449 * @inode: inode on FSID that is migrating
6450 * @locations: result of query
6451 * @page: buffer
6452 * @cred: credential to use for this operation
6453 *
6454 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6455 * operation failed, or a negative errno if a local error occurred.
6456 *
6457 * On success, "locations" is filled in, but if the server has
6458 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6459 * asserted.
6460 *
6461 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6462 * from this client that require migration recovery.
6463 */
6464int nfs4_proc_get_locations(struct inode *inode,
6465 struct nfs4_fs_locations *locations,
6466 struct page *page, struct rpc_cred *cred)
6467{
6468 struct nfs_server *server = NFS_SERVER(inode);
6469 struct nfs_client *clp = server->nfs_client;
6470 const struct nfs4_mig_recovery_ops *ops =
6471 clp->cl_mvops->mig_recovery_ops;
6472 struct nfs4_exception exception = { };
6473 int status;
6474
6475 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6476 (unsigned long long)server->fsid.major,
6477 (unsigned long long)server->fsid.minor,
6478 clp->cl_hostname);
6479 nfs_display_fhandle(NFS_FH(inode), __func__);
6480
6481 do {
6482 status = ops->get_locations(inode, locations, page, cred);
6483 if (status != -NFS4ERR_DELAY)
6484 break;
6485 nfs4_handle_exception(server, status, &exception);
6486 } while (exception.retry);
6487 return status;
6488}
6489
Chuck Lever44c99932013-10-17 14:13:30 -04006490/*
6491 * This operation also signals the server that this client is
6492 * performing "lease moved" recovery. The server can stop
6493 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6494 * is appended to this compound to identify the client ID which is
6495 * performing recovery.
6496 */
6497static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6498{
6499 struct nfs_server *server = NFS_SERVER(inode);
6500 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6501 struct rpc_clnt *clnt = server->client;
6502 struct nfs4_fsid_present_arg args = {
6503 .fh = NFS_FH(inode),
6504 .clientid = clp->cl_clientid,
6505 .renew = 1, /* append RENEW */
6506 };
6507 struct nfs4_fsid_present_res res = {
6508 .renew = 1,
6509 };
6510 struct rpc_message msg = {
6511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6512 .rpc_argp = &args,
6513 .rpc_resp = &res,
6514 .rpc_cred = cred,
6515 };
6516 unsigned long now = jiffies;
6517 int status;
6518
6519 res.fh = nfs_alloc_fhandle();
6520 if (res.fh == NULL)
6521 return -ENOMEM;
6522
6523 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6524 nfs4_set_sequence_privileged(&args.seq_args);
6525 status = nfs4_call_sync_sequence(clnt, server, &msg,
6526 &args.seq_args, &res.seq_res);
6527 nfs_free_fhandle(res.fh);
6528 if (status)
6529 return status;
6530
6531 do_renew_lease(clp, now);
6532 return 0;
6533}
6534
6535#ifdef CONFIG_NFS_V4_1
6536
6537/*
6538 * This operation also signals the server that this client is
6539 * performing "lease moved" recovery. The server can stop asserting
6540 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6541 * this operation is identified in the SEQUENCE operation in this
6542 * compound.
6543 */
6544static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6545{
6546 struct nfs_server *server = NFS_SERVER(inode);
6547 struct rpc_clnt *clnt = server->client;
6548 struct nfs4_fsid_present_arg args = {
6549 .fh = NFS_FH(inode),
6550 };
6551 struct nfs4_fsid_present_res res = {
6552 };
6553 struct rpc_message msg = {
6554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6555 .rpc_argp = &args,
6556 .rpc_resp = &res,
6557 .rpc_cred = cred,
6558 };
6559 int status;
6560
6561 res.fh = nfs_alloc_fhandle();
6562 if (res.fh == NULL)
6563 return -ENOMEM;
6564
6565 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6566 nfs4_set_sequence_privileged(&args.seq_args);
6567 status = nfs4_call_sync_sequence(clnt, server, &msg,
6568 &args.seq_args, &res.seq_res);
6569 nfs_free_fhandle(res.fh);
6570 if (status == NFS4_OK &&
6571 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6572 status = -NFS4ERR_LEASE_MOVED;
6573 return status;
6574}
6575
6576#endif /* CONFIG_NFS_V4_1 */
6577
6578/**
6579 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6580 * @inode: inode on FSID to check
6581 * @cred: credential to use for this operation
6582 *
6583 * Server indicates whether the FSID is present, moved, or not
6584 * recognized. This operation is necessary to clear a LEASE_MOVED
6585 * condition for this client ID.
6586 *
6587 * Returns NFS4_OK if the FSID is present on this server,
6588 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6589 * NFS4ERR code if some error occurred on the server, or a
6590 * negative errno if a local failure occurred.
6591 */
6592int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6593{
6594 struct nfs_server *server = NFS_SERVER(inode);
6595 struct nfs_client *clp = server->nfs_client;
6596 const struct nfs4_mig_recovery_ops *ops =
6597 clp->cl_mvops->mig_recovery_ops;
6598 struct nfs4_exception exception = { };
6599 int status;
6600
6601 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6602 (unsigned long long)server->fsid.major,
6603 (unsigned long long)server->fsid.minor,
6604 clp->cl_hostname);
6605 nfs_display_fhandle(NFS_FH(inode), __func__);
6606
6607 do {
6608 status = ops->fsid_present(inode, cred);
6609 if (status != -NFS4ERR_DELAY)
6610 break;
6611 nfs4_handle_exception(server, status, &exception);
6612 } while (exception.retry);
6613 return status;
6614}
6615
Andy Adamson5ec16a82013-08-08 10:57:55 -04006616/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006617 * If 'use_integrity' is true and the state managment nfs_client
6618 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6619 * and the machine credential as per RFC3530bis and RFC5661 Security
6620 * Considerations sections. Otherwise, just use the user cred with the
6621 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006622 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006623static 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 +00006624{
6625 int status;
6626 struct nfs4_secinfo_arg args = {
6627 .dir_fh = NFS_FH(dir),
6628 .name = name,
6629 };
6630 struct nfs4_secinfo_res res = {
6631 .flavors = flavors,
6632 };
6633 struct rpc_message msg = {
6634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6635 .rpc_argp = &args,
6636 .rpc_resp = &res,
6637 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006638 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006639 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006640
6641 if (use_integrity) {
6642 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006643 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6644 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006645 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006646
6647 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006648
6649 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6650 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6651
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006652 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6653 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006654 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006655
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006656 if (cred)
6657 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006658
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006659 return status;
6660}
6661
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006662int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6663 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006664{
6665 struct nfs4_exception exception = { };
6666 int err;
6667 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006668 err = -NFS4ERR_WRONGSEC;
6669
6670 /* try to use integrity protection with machine cred */
6671 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6672 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6673
6674 /*
6675 * if unable to use integrity protection, or SECINFO with
6676 * integrity protection returns NFS4ERR_WRONGSEC (which is
6677 * disallowed by spec, but exists in deployed servers) use
6678 * the current filesystem's rpc_client and the user cred.
6679 */
6680 if (err == -NFS4ERR_WRONGSEC)
6681 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6682
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006683 trace_nfs4_secinfo(dir, name, err);
6684 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006685 &exception);
6686 } while (exception.retry);
6687 return err;
6688}
6689
Andy Adamson557134a2009-04-01 09:21:53 -04006690#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006691/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006692 * Check the exchange flags returned by the server for invalid flags, having
6693 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6694 * DS flags set.
6695 */
6696static int nfs4_check_cl_exchange_flags(u32 flags)
6697{
6698 if (flags & ~EXCHGID4_FLAG_MASK_R)
6699 goto out_inval;
6700 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6701 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6702 goto out_inval;
6703 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6704 goto out_inval;
6705 return NFS_OK;
6706out_inval:
6707 return -NFS4ERR_INVAL;
6708}
6709
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006710static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006711nfs41_same_server_scope(struct nfs41_server_scope *a,
6712 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006713{
6714 if (a->server_scope_sz == b->server_scope_sz &&
6715 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6716 return true;
6717
6718 return false;
6719}
6720
Andy Adamson357f54d2010-12-14 10:11:57 -05006721/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006722 * nfs4_proc_bind_conn_to_session()
6723 *
6724 * The 4.1 client currently uses the same TCP connection for the
6725 * fore and backchannel.
6726 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006727int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006728{
6729 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006730 struct nfs41_bind_conn_to_session_args args = {
6731 .client = clp,
6732 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6733 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006734 struct nfs41_bind_conn_to_session_res res;
6735 struct rpc_message msg = {
6736 .rpc_proc =
6737 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006738 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006739 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006740 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006741 };
6742
6743 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006744
Trond Myklebust71a097c2015-02-18 09:27:18 -08006745 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6746 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6747 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006748
6749 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006750 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006751 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006752 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006753 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6754 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6755 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006756 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006757 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006758 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006759 dprintk("NFS: %s: Unexpected direction from server\n",
6760 __func__);
6761 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006762 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006763 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006764 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006765 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6766 __func__);
6767 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006768 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006769 }
6770 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006771out:
6772 dprintk("<-- %s status= %d\n", __func__, status);
6773 return status;
6774}
6775
6776/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006777 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6778 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006779 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006780static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6781 .how = SP4_MACH_CRED,
6782 .enforce.u.words = {
6783 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6784 1 << (OP_EXCHANGE_ID - 32) |
6785 1 << (OP_CREATE_SESSION - 32) |
6786 1 << (OP_DESTROY_SESSION - 32) |
6787 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006788 },
6789 .allow.u.words = {
6790 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006791 1 << (OP_LOCKU) |
6792 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006793 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006794 1 << (OP_SECINFO_NO_NAME - 32) |
6795 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006796 1 << (OP_FREE_STATEID - 32) |
6797 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006798 }
6799};
6800
6801/*
6802 * Select the state protection mode for client `clp' given the server results
6803 * from exchange_id in `sp'.
6804 *
6805 * Returns 0 on success, negative errno otherwise.
6806 */
6807static int nfs4_sp4_select_mode(struct nfs_client *clp,
6808 struct nfs41_state_protection *sp)
6809{
6810 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6811 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6812 1 << (OP_EXCHANGE_ID - 32) |
6813 1 << (OP_CREATE_SESSION - 32) |
6814 1 << (OP_DESTROY_SESSION - 32) |
6815 1 << (OP_DESTROY_CLIENTID - 32)
6816 };
6817 unsigned int i;
6818
6819 if (sp->how == SP4_MACH_CRED) {
6820 /* Print state protect result */
6821 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6822 for (i = 0; i <= LAST_NFS4_OP; i++) {
6823 if (test_bit(i, sp->enforce.u.longs))
6824 dfprintk(MOUNT, " enforce op %d\n", i);
6825 if (test_bit(i, sp->allow.u.longs))
6826 dfprintk(MOUNT, " allow op %d\n", i);
6827 }
6828
6829 /* make sure nothing is on enforce list that isn't supported */
6830 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6831 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6832 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6833 return -EINVAL;
6834 }
6835 }
6836
6837 /*
6838 * Minimal mode - state operations are allowed to use machine
6839 * credential. Note this already happens by default, so the
6840 * client doesn't have to do anything more than the negotiation.
6841 *
6842 * NOTE: we don't care if EXCHANGE_ID is in the list -
6843 * we're already using the machine cred for exchange_id
6844 * and will never use a different cred.
6845 */
6846 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6847 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6848 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6849 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6850 dfprintk(MOUNT, "sp4_mach_cred:\n");
6851 dfprintk(MOUNT, " minimal mode enabled\n");
6852 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6853 } else {
6854 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6855 return -EINVAL;
6856 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006857
6858 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6859 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6860 dfprintk(MOUNT, " cleanup mode enabled\n");
6861 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6862 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006863
6864 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6865 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6866 dfprintk(MOUNT, " secinfo mode enabled\n");
6867 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6868 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006869
6870 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6871 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6872 dfprintk(MOUNT, " stateid mode enabled\n");
6873 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6874 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006875
6876 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6877 dfprintk(MOUNT, " write mode enabled\n");
6878 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6879 }
6880
6881 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6882 dfprintk(MOUNT, " commit mode enabled\n");
6883 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6884 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006885 }
6886
6887 return 0;
6888}
6889
6890/*
6891 * _nfs4_proc_exchange_id()
6892 *
6893 * Wrapper for EXCHANGE_ID operation.
6894 */
6895static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6896 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006897{
6898 nfs4_verifier verifier;
6899 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006900 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006901 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006902#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006903 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006904 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6905 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6906#else
6907 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6908 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6909#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006910 };
6911 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006912 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006913 };
6914 int status;
6915 struct rpc_message msg = {
6916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6917 .rpc_argp = &args,
6918 .rpc_resp = &res,
6919 .rpc_cred = cred,
6920 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006921
Chuck Leverf0920752012-05-21 22:45:41 -04006922 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006923
6924 status = nfs4_init_uniform_client_string(clp);
6925 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04006926 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006927
6928 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006929 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006930 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006931
Chuck Leveracdeb692012-05-21 22:46:16 -04006932 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006933 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006934 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006935 status = -ENOMEM;
6936 goto out;
6937 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006938
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006939 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006940 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006941 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006942 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006943 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006944 }
6945
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006946 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006947 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006948 status = -ENOMEM;
6949 goto out_server_scope;
6950 }
6951
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006952 switch (sp4_how) {
6953 case SP4_NONE:
6954 args.state_protect.how = SP4_NONE;
6955 break;
6956
6957 case SP4_MACH_CRED:
6958 args.state_protect = nfs4_sp4_mach_cred_request;
6959 break;
6960
6961 default:
6962 /* unsupported! */
6963 WARN_ON_ONCE(1);
6964 status = -EINVAL;
6965 goto out_server_scope;
6966 }
6967
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006968 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006969 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006970 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006971 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006972
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006973 if (status == 0)
6974 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6975
Chuck Lever177313f2012-05-21 22:44:58 -04006976 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006977 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05006978 clp->cl_exchange_flags = res.flags;
6979 /* Client ID is not confirmed */
6980 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
6981 clear_bit(NFS4_SESSION_ESTABLISHED,
6982 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04006983 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05006984 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006985
Chuck Leveracdeb692012-05-21 22:46:16 -04006986 kfree(clp->cl_serverowner);
6987 clp->cl_serverowner = res.server_owner;
6988 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006989
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006990 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006991 kfree(clp->cl_implid);
6992 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006993
Chuck Lever177313f2012-05-21 22:44:58 -04006994 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006995 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006996 res.server_scope)) {
6997 dprintk("%s: server_scope mismatch detected\n",
6998 __func__);
6999 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007000 kfree(clp->cl_serverscope);
7001 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007002 }
7003
Chuck Lever177313f2012-05-21 22:44:58 -04007004 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007005 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007006 goto out;
7007 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007008 } else
7009 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007010
Chuck Leveracdeb692012-05-21 22:46:16 -04007011out_server_owner:
7012 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007013out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007014 kfree(res.server_scope);
7015out:
Chuck Lever177313f2012-05-21 22:44:58 -04007016 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007017 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007018 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007019 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007020 clp->cl_implid->date.seconds,
7021 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007022 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007023 return status;
7024}
7025
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007026/*
7027 * nfs4_proc_exchange_id()
7028 *
7029 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7030 *
7031 * Since the clientid has expired, all compounds using sessions
7032 * associated with the stale clientid will be returning
7033 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7034 * be in some phase of session reset.
7035 *
7036 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7037 */
7038int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7039{
7040 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7041 int status;
7042
7043 /* try SP4_MACH_CRED if krb5i/p */
7044 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7045 authflavor == RPC_AUTH_GSS_KRB5P) {
7046 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7047 if (!status)
7048 return 0;
7049 }
7050
7051 /* try SP4_NONE */
7052 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7053}
7054
Trond Myklebust66245532012-05-25 17:18:09 -04007055static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7056 struct rpc_cred *cred)
7057{
7058 struct rpc_message msg = {
7059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7060 .rpc_argp = clp,
7061 .rpc_cred = cred,
7062 };
7063 int status;
7064
7065 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007066 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007067 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007068 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007069 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7070 return status;
7071}
7072
7073static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7074 struct rpc_cred *cred)
7075{
7076 unsigned int loop;
7077 int ret;
7078
7079 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7080 ret = _nfs4_proc_destroy_clientid(clp, cred);
7081 switch (ret) {
7082 case -NFS4ERR_DELAY:
7083 case -NFS4ERR_CLIENTID_BUSY:
7084 ssleep(1);
7085 break;
7086 default:
7087 return ret;
7088 }
7089 }
7090 return 0;
7091}
7092
7093int nfs4_destroy_clientid(struct nfs_client *clp)
7094{
7095 struct rpc_cred *cred;
7096 int ret = 0;
7097
7098 if (clp->cl_mvops->minor_version < 1)
7099 goto out;
7100 if (clp->cl_exchange_flags == 0)
7101 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007102 if (clp->cl_preserve_clid)
7103 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007104 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007105 ret = nfs4_proc_destroy_clientid(clp, cred);
7106 if (cred)
7107 put_rpccred(cred);
7108 switch (ret) {
7109 case 0:
7110 case -NFS4ERR_STALE_CLIENTID:
7111 clp->cl_exchange_flags = 0;
7112 }
7113out:
7114 return ret;
7115}
7116
Andy Adamson2050f0c2009-04-01 09:22:30 -04007117struct nfs4_get_lease_time_data {
7118 struct nfs4_get_lease_time_args *args;
7119 struct nfs4_get_lease_time_res *res;
7120 struct nfs_client *clp;
7121};
7122
7123static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7124 void *calldata)
7125{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007126 struct nfs4_get_lease_time_data *data =
7127 (struct nfs4_get_lease_time_data *)calldata;
7128
7129 dprintk("--> %s\n", __func__);
7130 /* just setup sequence, do not trigger session recovery
7131 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007132 nfs41_setup_sequence(data->clp->cl_session,
7133 &data->args->la_seq_args,
7134 &data->res->lr_seq_res,
7135 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007136 dprintk("<-- %s\n", __func__);
7137}
7138
7139/*
7140 * Called from nfs4_state_manager thread for session setup, so don't recover
7141 * from sequence operation or clientid errors.
7142 */
7143static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7144{
7145 struct nfs4_get_lease_time_data *data =
7146 (struct nfs4_get_lease_time_data *)calldata;
7147
7148 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007149 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7150 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007151 switch (task->tk_status) {
7152 case -NFS4ERR_DELAY:
7153 case -NFS4ERR_GRACE:
7154 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7155 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7156 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007157 /* fall through */
7158 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007159 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007160 return;
7161 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007162 dprintk("<-- %s\n", __func__);
7163}
7164
Trond Myklebust17280172012-03-11 13:11:00 -04007165static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007166 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7167 .rpc_call_done = nfs4_get_lease_time_done,
7168};
7169
7170int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7171{
7172 struct rpc_task *task;
7173 struct nfs4_get_lease_time_args args;
7174 struct nfs4_get_lease_time_res res = {
7175 .lr_fsinfo = fsinfo,
7176 };
7177 struct nfs4_get_lease_time_data data = {
7178 .args = &args,
7179 .res = &res,
7180 .clp = clp,
7181 };
7182 struct rpc_message msg = {
7183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7184 .rpc_argp = &args,
7185 .rpc_resp = &res,
7186 };
7187 struct rpc_task_setup task_setup = {
7188 .rpc_client = clp->cl_rpcclient,
7189 .rpc_message = &msg,
7190 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007191 .callback_data = &data,
7192 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007193 };
7194 int status;
7195
Chuck Levera9c92d62013-08-09 12:48:18 -04007196 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007197 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007198 dprintk("--> %s\n", __func__);
7199 task = rpc_run_task(&task_setup);
7200
7201 if (IS_ERR(task))
7202 status = PTR_ERR(task);
7203 else {
7204 status = task->tk_status;
7205 rpc_put_task(task);
7206 }
7207 dprintk("<-- %s return %d\n", __func__, status);
7208
7209 return status;
7210}
7211
Andy Adamsonfc931582009-04-01 09:22:31 -04007212/*
7213 * Initialize the values to be used by the client in CREATE_SESSION
7214 * If nfs4_init_session set the fore channel request and response sizes,
7215 * use them.
7216 *
7217 * Set the back channel max_resp_sz_cached to zero to force the client to
7218 * always set csa_cachethis to FALSE because the current implementation
7219 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7220 */
7221static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7222{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007223 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007224
Andy Adamson18aad3d2013-06-26 12:21:49 -04007225 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7226 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7227
Andy Adamsonfc931582009-04-01 09:22:31 -04007228 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007229 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7230 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007231 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007232 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007233
7234 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007235 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007236 __func__,
7237 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007238 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007239
7240 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007241 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7242 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7243 args->bc_attrs.max_resp_sz_cached = 0;
7244 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7245 args->bc_attrs.max_reqs = 1;
7246
7247 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7248 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7249 __func__,
7250 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7251 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7252 args->bc_attrs.max_reqs);
7253}
7254
Trond Myklebust79969dd2015-02-18 11:30:18 -08007255static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7256 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007257{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007258 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007259 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007260
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007261 if (rcvd->max_resp_sz > sent->max_resp_sz)
7262 return -EINVAL;
7263 /*
7264 * Our requested max_ops is the minimum we need; we're not
7265 * prepared to break up compounds into smaller pieces than that.
7266 * So, no point even trying to continue if the server won't
7267 * cooperate:
7268 */
7269 if (rcvd->max_ops < sent->max_ops)
7270 return -EINVAL;
7271 if (rcvd->max_reqs == 0)
7272 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007273 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7274 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007275 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007276}
7277
Trond Myklebust79969dd2015-02-18 11:30:18 -08007278static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7279 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007280{
7281 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007282 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007283
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007284 if (!(res->flags & SESSION4_BACK_CHAN))
7285 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007286 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7287 return -EINVAL;
7288 if (rcvd->max_resp_sz < sent->max_resp_sz)
7289 return -EINVAL;
7290 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7291 return -EINVAL;
7292 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007293 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007294 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007295 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007296 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007297out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007298 return 0;
7299}
Andy Adamson8d353012009-04-01 09:22:32 -04007300
Andy Adamson8d353012009-04-01 09:22:32 -04007301static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007302 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007303{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007304 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007305
Trond Myklebust79969dd2015-02-18 11:30:18 -08007306 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007307 if (ret)
7308 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007309 return nfs4_verify_back_channel_attrs(args, res);
7310}
7311
7312static void nfs4_update_session(struct nfs4_session *session,
7313 struct nfs41_create_session_res *res)
7314{
7315 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007316 /* Mark client id and session as being confirmed */
7317 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7318 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007319 session->flags = res->flags;
7320 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007321 if (res->flags & SESSION4_BACK_CHAN)
7322 memcpy(&session->bc_attrs, &res->bc_attrs,
7323 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007324}
7325
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007326static int _nfs4_proc_create_session(struct nfs_client *clp,
7327 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007328{
7329 struct nfs4_session *session = clp->cl_session;
7330 struct nfs41_create_session_args args = {
7331 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007332 .clientid = clp->cl_clientid,
7333 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007334 .cb_program = NFS4_CALLBACK,
7335 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007336 struct nfs41_create_session_res res;
7337
Andy Adamsonfc931582009-04-01 09:22:31 -04007338 struct rpc_message msg = {
7339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7340 .rpc_argp = &args,
7341 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007342 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007343 };
7344 int status;
7345
7346 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007347 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007348
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007349 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007350 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007351
Trond Myklebust43095d32012-11-20 11:13:12 -05007352 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007353 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007354 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007355 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007356 if (clp->cl_seqid == res.seqid)
7357 clp->cl_seqid++;
7358 if (status)
7359 goto out;
7360 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007361 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007362out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007363 return status;
7364}
7365
7366/*
7367 * Issues a CREATE_SESSION operation to the server.
7368 * It is the responsibility of the caller to verify the session is
7369 * expired before calling this routine.
7370 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007371int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007372{
7373 int status;
7374 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007375 struct nfs4_session *session = clp->cl_session;
7376
7377 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7378
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007379 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007380 if (status)
7381 goto out;
7382
Andy Adamsonaacd5532011-11-09 13:58:21 -05007383 /* Init or reset the session slot tables */
7384 status = nfs4_setup_session_slot_tables(session);
7385 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007386 if (status)
7387 goto out;
7388
7389 ptr = (unsigned *)&session->sess_id.data[0];
7390 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7391 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007392out:
7393 dprintk("<-- %s\n", __func__);
7394 return status;
7395}
7396
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007397/*
7398 * Issue the over-the-wire RPC DESTROY_SESSION.
7399 * The caller must serialize access to this routine.
7400 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007401int nfs4_proc_destroy_session(struct nfs4_session *session,
7402 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007403{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007404 struct rpc_message msg = {
7405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7406 .rpc_argp = session,
7407 .rpc_cred = cred,
7408 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007409 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007410
7411 dprintk("--> nfs4_proc_destroy_session\n");
7412
7413 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007414 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7415 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007416
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007417 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007418 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007419
7420 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007421 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007422 "Session has been destroyed regardless...\n", status);
7423
7424 dprintk("<-- nfs4_proc_destroy_session\n");
7425 return status;
7426}
7427
Trond Myklebust7b38c362012-05-23 13:23:31 -04007428/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007429 * Renew the cl_session lease.
7430 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007431struct nfs4_sequence_data {
7432 struct nfs_client *clp;
7433 struct nfs4_sequence_args args;
7434 struct nfs4_sequence_res res;
7435};
7436
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007437static void nfs41_sequence_release(void *data)
7438{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007439 struct nfs4_sequence_data *calldata = data;
7440 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007441
Alexandros Batsakis71358402010-02-05 03:45:05 -08007442 if (atomic_read(&clp->cl_count) > 1)
7443 nfs4_schedule_state_renewal(clp);
7444 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007445 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007446}
7447
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007448static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7449{
7450 switch(task->tk_status) {
7451 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007452 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7453 return -EAGAIN;
7454 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007455 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007456 }
7457 return 0;
7458}
7459
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007460static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007461{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007462 struct nfs4_sequence_data *calldata = data;
7463 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007464
Trond Myklebust14516c32010-07-31 14:29:06 -04007465 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7466 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007467
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007468 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007469 if (task->tk_status < 0) {
7470 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007471 if (atomic_read(&clp->cl_count) == 1)
7472 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007473
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007474 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7475 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007476 return;
7477 }
7478 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007479 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007480out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007481 dprintk("<-- %s\n", __func__);
7482}
7483
7484static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7485{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007486 struct nfs4_sequence_data *calldata = data;
7487 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007488 struct nfs4_sequence_args *args;
7489 struct nfs4_sequence_res *res;
7490
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007491 args = task->tk_msg.rpc_argp;
7492 res = task->tk_msg.rpc_resp;
7493
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007494 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007495}
7496
7497static const struct rpc_call_ops nfs41_sequence_ops = {
7498 .rpc_call_done = nfs41_sequence_call_done,
7499 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007500 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007501};
7502
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007503static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7504 struct rpc_cred *cred,
7505 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007506{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007507 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007508 struct rpc_message msg = {
7509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7510 .rpc_cred = cred,
7511 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007512 struct rpc_task_setup task_setup_data = {
7513 .rpc_client = clp->cl_rpcclient,
7514 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007515 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007516 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007517 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007518
Alexandros Batsakis71358402010-02-05 03:45:05 -08007519 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007520 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007521 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007522 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007523 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007524 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007525 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007526 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007527 if (is_privileged)
7528 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007529 msg.rpc_argp = &calldata->args;
7530 msg.rpc_resp = &calldata->res;
7531 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007532 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007533
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007534 return rpc_run_task(&task_setup_data);
7535}
7536
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007537static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007538{
7539 struct rpc_task *task;
7540 int ret = 0;
7541
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007542 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007543 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007544 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007545 if (IS_ERR(task))
7546 ret = PTR_ERR(task);
7547 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007548 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007549 dprintk("<-- %s status=%d\n", __func__, ret);
7550 return ret;
7551}
7552
7553static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7554{
7555 struct rpc_task *task;
7556 int ret;
7557
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007558 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007559 if (IS_ERR(task)) {
7560 ret = PTR_ERR(task);
7561 goto out;
7562 }
7563 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007564 if (!ret) {
7565 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7566
7567 if (task->tk_status == 0)
7568 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007569 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007570 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007571 rpc_put_task(task);
7572out:
7573 dprintk("<-- %s status=%d\n", __func__, ret);
7574 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007575}
7576
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007577struct nfs4_reclaim_complete_data {
7578 struct nfs_client *clp;
7579 struct nfs41_reclaim_complete_args arg;
7580 struct nfs41_reclaim_complete_res res;
7581};
7582
7583static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7584{
7585 struct nfs4_reclaim_complete_data *calldata = data;
7586
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007587 nfs41_setup_sequence(calldata->clp->cl_session,
7588 &calldata->arg.seq_args,
7589 &calldata->res.seq_res,
7590 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007591}
7592
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007593static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7594{
7595 switch(task->tk_status) {
7596 case 0:
7597 case -NFS4ERR_COMPLETE_ALREADY:
7598 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7599 break;
7600 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007601 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007602 /* fall through */
7603 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007604 return -EAGAIN;
7605 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007606 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007607 }
7608 return 0;
7609}
7610
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007611static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7612{
7613 struct nfs4_reclaim_complete_data *calldata = data;
7614 struct nfs_client *clp = calldata->clp;
7615 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7616
7617 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007618 if (!nfs41_sequence_done(task, res))
7619 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007620
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007621 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007622 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7623 rpc_restart_call_prepare(task);
7624 return;
7625 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007626 dprintk("<-- %s\n", __func__);
7627}
7628
7629static void nfs4_free_reclaim_complete_data(void *data)
7630{
7631 struct nfs4_reclaim_complete_data *calldata = data;
7632
7633 kfree(calldata);
7634}
7635
7636static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7637 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7638 .rpc_call_done = nfs4_reclaim_complete_done,
7639 .rpc_release = nfs4_free_reclaim_complete_data,
7640};
7641
7642/*
7643 * Issue a global reclaim complete.
7644 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007645static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7646 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007647{
7648 struct nfs4_reclaim_complete_data *calldata;
7649 struct rpc_task *task;
7650 struct rpc_message msg = {
7651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007652 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007653 };
7654 struct rpc_task_setup task_setup_data = {
7655 .rpc_client = clp->cl_rpcclient,
7656 .rpc_message = &msg,
7657 .callback_ops = &nfs4_reclaim_complete_call_ops,
7658 .flags = RPC_TASK_ASYNC,
7659 };
7660 int status = -ENOMEM;
7661
7662 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007663 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007664 if (calldata == NULL)
7665 goto out;
7666 calldata->clp = clp;
7667 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007668
Chuck Levera9c92d62013-08-09 12:48:18 -04007669 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007670 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007671 msg.rpc_argp = &calldata->arg;
7672 msg.rpc_resp = &calldata->res;
7673 task_setup_data.callback_data = calldata;
7674 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007675 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007676 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007677 goto out;
7678 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007679 status = nfs4_wait_for_completion_rpc_task(task);
7680 if (status == 0)
7681 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007682 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007683 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007684out:
7685 dprintk("<-- %s status=%d\n", __func__, status);
7686 return status;
7687}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007688
7689static void
7690nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7691{
7692 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007693 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007694 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007695
7696 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007697 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7698 * right now covering the LAYOUTGET we are about to send.
7699 * However, that is not so catastrophic, and there seems
7700 * to be no way to prevent it completely.
7701 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007702 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007703 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007704 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007705 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7706 NFS_I(lgp->args.inode)->layout,
Peng Taoce6ab4f2014-09-06 00:53:24 +08007707 &lgp->args.range,
Fred Isamancf7d63f2011-01-06 11:36:25 +00007708 lgp->args.ctx->state)) {
7709 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007710 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007711}
7712
7713static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7714{
7715 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007716 struct inode *inode = lgp->args.inode;
7717 struct nfs_server *server = NFS_SERVER(inode);
7718 struct pnfs_layout_hdr *lo;
7719 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007720 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007721
Boaz Harroshed7e5422014-01-22 20:34:54 +02007722 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007723
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007724 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007725 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007726
7727 switch (task->tk_status) {
7728 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007729 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007730 /*
7731 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7732 * (or clients) writing to the same RAID stripe
7733 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007734 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007735 /*
7736 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7737 * existing layout before getting a new one).
7738 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007739 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007740 timeo = rpc_get_timeout(task->tk_client);
7741 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007742 now = jiffies;
7743 if (time_after(giveup, now)) {
7744 unsigned long delay;
7745
7746 /* Delay for:
7747 * - Not less then NFS4_POLL_RETRY_MIN.
7748 * - One last time a jiffie before we give up
7749 * - exponential backoff (time_now minus start_attempt)
7750 */
7751 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7752 min((giveup - now - 1),
7753 now - lgp->args.timestamp));
7754
7755 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7756 __func__, delay);
7757 rpc_delay(task, delay);
7758 task->tk_status = 0;
7759 rpc_restart_call_prepare(task);
7760 goto out; /* Do not call nfs4_async_handle_error() */
7761 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007762 break;
7763 case -NFS4ERR_EXPIRED:
7764 case -NFS4ERR_BAD_STATEID:
7765 spin_lock(&inode->i_lock);
7766 lo = NFS_I(inode)->layout;
7767 if (!lo || list_empty(&lo->plh_segs)) {
7768 spin_unlock(&inode->i_lock);
7769 /* If the open stateid was bad, then recover it. */
7770 state = lgp->args.ctx->state;
7771 } else {
7772 LIST_HEAD(head);
7773
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007774 /*
7775 * Mark the bad layout state as invalid, then retry
7776 * with the current stateid.
7777 */
Trond Myklebustee314c22012-10-01 17:25:48 -07007778 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7779 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007780 pnfs_free_lseg_list(&head);
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007781
7782 task->tk_status = 0;
7783 rpc_restart_call_prepare(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007784 }
7785 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007786 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebustee314c22012-10-01 17:25:48 -07007787 rpc_restart_call_prepare(task);
7788out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007789 dprintk("<-- %s\n", __func__);
7790}
7791
Idan Kedar85541162012-08-02 11:47:10 +03007792static size_t max_response_pages(struct nfs_server *server)
7793{
7794 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7795 return nfs_page_array_len(0, max_resp_sz);
7796}
7797
7798static void nfs4_free_pages(struct page **pages, size_t size)
7799{
7800 int i;
7801
7802 if (!pages)
7803 return;
7804
7805 for (i = 0; i < size; i++) {
7806 if (!pages[i])
7807 break;
7808 __free_page(pages[i]);
7809 }
7810 kfree(pages);
7811}
7812
7813static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7814{
7815 struct page **pages;
7816 int i;
7817
7818 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7819 if (!pages) {
7820 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7821 return NULL;
7822 }
7823
7824 for (i = 0; i < size; i++) {
7825 pages[i] = alloc_page(gfp_flags);
7826 if (!pages[i]) {
7827 dprintk("%s: failed to allocate page\n", __func__);
7828 nfs4_free_pages(pages, size);
7829 return NULL;
7830 }
7831 }
7832
7833 return pages;
7834}
7835
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007836static void nfs4_layoutget_release(void *calldata)
7837{
7838 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007839 struct inode *inode = lgp->args.inode;
7840 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007841 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007842
7843 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007844 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007845 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007846 put_nfs_open_context(lgp->args.ctx);
7847 kfree(calldata);
7848 dprintk("<-- %s\n", __func__);
7849}
7850
7851static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7852 .rpc_call_prepare = nfs4_layoutget_prepare,
7853 .rpc_call_done = nfs4_layoutget_done,
7854 .rpc_release = nfs4_layoutget_release,
7855};
7856
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007857struct pnfs_layout_segment *
7858nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007859{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007860 struct inode *inode = lgp->args.inode;
7861 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007862 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007863 struct rpc_task *task;
7864 struct rpc_message msg = {
7865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7866 .rpc_argp = &lgp->args,
7867 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007868 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007869 };
7870 struct rpc_task_setup task_setup_data = {
7871 .rpc_client = server->client,
7872 .rpc_message = &msg,
7873 .callback_ops = &nfs4_layoutget_call_ops,
7874 .callback_data = lgp,
7875 .flags = RPC_TASK_ASYNC,
7876 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007877 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007878 int status = 0;
7879
7880 dprintk("--> %s\n", __func__);
7881
Peng Tao4bd5a982014-11-17 11:05:17 +08007882 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7883 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7884
Idan Kedar85541162012-08-02 11:47:10 +03007885 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7886 if (!lgp->args.layout.pages) {
7887 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007888 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007889 }
7890 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007891 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007892
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007893 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007894 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007895 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007896
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007897 task = rpc_run_task(&task_setup_data);
7898 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007899 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007900 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007901 if (status == 0)
7902 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007903 trace_nfs4_layoutget(lgp->args.ctx,
7904 &lgp->args.range,
7905 &lgp->res.range,
7906 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007907 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7908 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007909 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007910 rpc_put_task(task);
7911 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007912 if (status)
7913 return ERR_PTR(status);
7914 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007915}
7916
Benny Halevycbe82602011-05-22 19:52:37 +03007917static void
7918nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7919{
7920 struct nfs4_layoutreturn *lrp = calldata;
7921
7922 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007923 nfs41_setup_sequence(lrp->clp->cl_session,
7924 &lrp->args.seq_args,
7925 &lrp->res.seq_res,
7926 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007927}
7928
7929static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7930{
7931 struct nfs4_layoutreturn *lrp = calldata;
7932 struct nfs_server *server;
7933
7934 dprintk("--> %s\n", __func__);
7935
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007936 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007937 return;
7938
7939 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007940 switch (task->tk_status) {
7941 default:
7942 task->tk_status = 0;
7943 case 0:
7944 break;
7945 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007946 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007947 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007948 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007949 return;
7950 }
Benny Halevycbe82602011-05-22 19:52:37 +03007951 dprintk("<-- %s\n", __func__);
7952}
7953
7954static void nfs4_layoutreturn_release(void *calldata)
7955{
7956 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007957 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007958
7959 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007960 spin_lock(&lo->plh_inode->i_lock);
7961 if (lrp->res.lrs_present)
7962 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Tom Haynesd67ae822014-12-11 17:02:04 -05007963 pnfs_clear_layoutreturn_waitbit(lo);
Peng Tao193e3aa2014-11-17 09:30:41 +08007964 clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE, &lo->plh_flags);
7965 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
Trond Myklebust849b2862012-09-24 14:18:39 -04007966 lo->plh_block_lgets--;
7967 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007968 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05007969 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03007970 kfree(calldata);
7971 dprintk("<-- %s\n", __func__);
7972}
7973
7974static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7975 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7976 .rpc_call_done = nfs4_layoutreturn_done,
7977 .rpc_release = nfs4_layoutreturn_release,
7978};
7979
Peng Tao6c166052014-11-17 09:30:40 +08007980int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03007981{
7982 struct rpc_task *task;
7983 struct rpc_message msg = {
7984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7985 .rpc_argp = &lrp->args,
7986 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007987 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007988 };
7989 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007990 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007991 .rpc_message = &msg,
7992 .callback_ops = &nfs4_layoutreturn_call_ops,
7993 .callback_data = lrp,
7994 };
Peng Tao6c166052014-11-17 09:30:40 +08007995 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03007996
7997 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05007998 if (!sync) {
7999 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8000 if (!lrp->inode) {
8001 nfs4_layoutreturn_release(lrp);
8002 return -EAGAIN;
8003 }
8004 task_setup_data.flags |= RPC_TASK_ASYNC;
8005 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008006 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008007 task = rpc_run_task(&task_setup_data);
8008 if (IS_ERR(task))
8009 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008010 if (sync)
8011 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008012 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008013 dprintk("<-- %s status=%d\n", __func__, status);
8014 rpc_put_task(task);
8015 return status;
8016}
8017
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008018static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008019_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8020 struct pnfs_device *pdev,
8021 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008022{
8023 struct nfs4_getdeviceinfo_args args = {
8024 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008025 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8026 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008027 };
8028 struct nfs4_getdeviceinfo_res res = {
8029 .pdev = pdev,
8030 };
8031 struct rpc_message msg = {
8032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8033 .rpc_argp = &args,
8034 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008035 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008036 };
8037 int status;
8038
8039 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008040 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008041 if (res.notification & ~args.notify_types)
8042 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008043 if (res.notification != args.notify_types)
8044 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008045
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008046 dprintk("<-- %s status=%d\n", __func__, status);
8047
8048 return status;
8049}
8050
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008051int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8052 struct pnfs_device *pdev,
8053 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008054{
8055 struct nfs4_exception exception = { };
8056 int err;
8057
8058 do {
8059 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008060 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008061 &exception);
8062 } while (exception.retry);
8063 return err;
8064}
8065EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8066
Andy Adamson863a3c62011-03-23 13:27:54 +00008067static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8068{
8069 struct nfs4_layoutcommit_data *data = calldata;
8070 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008071 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008072
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008073 nfs41_setup_sequence(session,
8074 &data->args.seq_args,
8075 &data->res.seq_res,
8076 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008077}
8078
8079static void
8080nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8081{
8082 struct nfs4_layoutcommit_data *data = calldata;
8083 struct nfs_server *server = NFS_SERVER(data->args.inode);
8084
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008085 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008086 return;
8087
8088 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008089 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8090 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8091 case -NFS4ERR_BADLAYOUT: /* no layout */
8092 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008093 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008094 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008095 break;
8096 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008097 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008098 rpc_restart_call_prepare(task);
8099 return;
8100 }
8101 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008102}
8103
8104static void nfs4_layoutcommit_release(void *calldata)
8105{
8106 struct nfs4_layoutcommit_data *data = calldata;
8107
Andy Adamsondb29c082011-07-30 20:52:38 -04008108 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008109 nfs_post_op_update_inode_force_wcc(data->args.inode,
8110 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008111 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008112 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008113 kfree(data);
8114}
8115
8116static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8117 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8118 .rpc_call_done = nfs4_layoutcommit_done,
8119 .rpc_release = nfs4_layoutcommit_release,
8120};
8121
8122int
Andy Adamsonef311532011-03-12 02:58:10 -05008123nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008124{
8125 struct rpc_message msg = {
8126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8127 .rpc_argp = &data->args,
8128 .rpc_resp = &data->res,
8129 .rpc_cred = data->cred,
8130 };
8131 struct rpc_task_setup task_setup_data = {
8132 .task = &data->task,
8133 .rpc_client = NFS_CLIENT(data->args.inode),
8134 .rpc_message = &msg,
8135 .callback_ops = &nfs4_layoutcommit_ops,
8136 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008137 };
8138 struct rpc_task *task;
8139 int status = 0;
8140
8141 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
8142 "lbw: %llu inode %lu\n",
8143 data->task.tk_pid, sync,
8144 data->args.lastbytewritten,
8145 data->args.inode->i_ino);
8146
Trond Myklebust472e2592015-02-05 16:50:30 -05008147 if (!sync) {
8148 data->inode = nfs_igrab_and_active(data->args.inode);
8149 if (data->inode == NULL) {
8150 nfs4_layoutcommit_release(data);
8151 return -EAGAIN;
8152 }
8153 task_setup_data.flags = RPC_TASK_ASYNC;
8154 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008155 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008156 task = rpc_run_task(&task_setup_data);
8157 if (IS_ERR(task))
8158 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008159 if (sync)
8160 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008161 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008162 dprintk("%s: status %d\n", __func__, status);
8163 rpc_put_task(task);
8164 return status;
8165}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008166
Andy Adamson97431202013-08-08 10:57:56 -04008167/**
8168 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8169 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8170 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008171static int
8172_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008173 struct nfs_fsinfo *info,
8174 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008175{
8176 struct nfs41_secinfo_no_name_args args = {
8177 .style = SECINFO_STYLE_CURRENT_FH,
8178 };
8179 struct nfs4_secinfo_res res = {
8180 .flavors = flavors,
8181 };
8182 struct rpc_message msg = {
8183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8184 .rpc_argp = &args,
8185 .rpc_resp = &res,
8186 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008187 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008188 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008189 int status;
8190
8191 if (use_integrity) {
8192 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008193 cred = nfs4_get_clid_cred(server->nfs_client);
8194 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008195 }
8196
8197 dprintk("--> %s\n", __func__);
8198 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8199 &res.seq_res, 0);
8200 dprintk("<-- %s status=%d\n", __func__, status);
8201
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008202 if (cred)
8203 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008204
8205 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008206}
8207
8208static int
8209nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8210 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8211{
8212 struct nfs4_exception exception = { };
8213 int err;
8214 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008215 /* first try using integrity protection */
8216 err = -NFS4ERR_WRONGSEC;
8217
8218 /* try to use integrity protection with machine cred */
8219 if (_nfs4_is_integrity_protected(server->nfs_client))
8220 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8221 flavors, true);
8222
8223 /*
8224 * if unable to use integrity protection, or SECINFO with
8225 * integrity protection returns NFS4ERR_WRONGSEC (which is
8226 * disallowed by spec, but exists in deployed servers) use
8227 * the current filesystem's rpc_client and the user cred.
8228 */
8229 if (err == -NFS4ERR_WRONGSEC)
8230 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8231 flavors, false);
8232
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008233 switch (err) {
8234 case 0:
8235 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008236 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008237 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008238 default:
8239 err = nfs4_handle_exception(server, err, &exception);
8240 }
8241 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008242out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008243 return err;
8244}
8245
8246static int
8247nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8248 struct nfs_fsinfo *info)
8249{
8250 int err;
8251 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008252 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008253 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008254 struct nfs4_secinfo4 *secinfo;
8255 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008256
8257 page = alloc_page(GFP_KERNEL);
8258 if (!page) {
8259 err = -ENOMEM;
8260 goto out;
8261 }
8262
8263 flavors = page_address(page);
8264 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8265
8266 /*
8267 * Fall back on "guess and check" method if
8268 * the server doesn't support SECINFO_NO_NAME
8269 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008270 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008271 err = nfs4_find_root_sec(server, fhandle, info);
8272 goto out_freepage;
8273 }
8274 if (err)
8275 goto out_freepage;
8276
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008277 for (i = 0; i < flavors->num_flavors; i++) {
8278 secinfo = &flavors->flavors[i];
8279
8280 switch (secinfo->flavor) {
8281 case RPC_AUTH_NULL:
8282 case RPC_AUTH_UNIX:
8283 case RPC_AUTH_GSS:
8284 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8285 &secinfo->flavor_info);
8286 break;
8287 default:
8288 flavor = RPC_AUTH_MAXFLAVOR;
8289 break;
8290 }
8291
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008292 if (!nfs_auth_info_match(&server->auth_info, flavor))
8293 flavor = RPC_AUTH_MAXFLAVOR;
8294
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008295 if (flavor != RPC_AUTH_MAXFLAVOR) {
8296 err = nfs4_lookup_root_sec(server, fhandle,
8297 info, flavor);
8298 if (!err)
8299 break;
8300 }
8301 }
8302
8303 if (flavor == RPC_AUTH_MAXFLAVOR)
8304 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008305
8306out_freepage:
8307 put_page(page);
8308 if (err == -EACCES)
8309 return -EPERM;
8310out:
8311 return err;
8312}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008313
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008314static int _nfs41_test_stateid(struct nfs_server *server,
8315 nfs4_stateid *stateid,
8316 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008317{
8318 int status;
8319 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008320 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008321 };
8322 struct nfs41_test_stateid_res res;
8323 struct rpc_message msg = {
8324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8325 .rpc_argp = &args,
8326 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008327 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008328 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008329 struct rpc_clnt *rpc_client = server->client;
8330
8331 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8332 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008333
Chuck Lever38527b12012-07-11 16:30:23 -04008334 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008335 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008336 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008337 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008338 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008339 if (status != NFS_OK) {
8340 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008341 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008342 }
8343 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008344 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008345}
8346
Chuck Lever38527b12012-07-11 16:30:23 -04008347/**
8348 * nfs41_test_stateid - perform a TEST_STATEID operation
8349 *
8350 * @server: server / transport on which to perform the operation
8351 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008352 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008353 *
8354 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8355 * Otherwise a negative NFS4ERR value is returned if the operation
8356 * failed or the state ID is not currently valid.
8357 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008358static int nfs41_test_stateid(struct nfs_server *server,
8359 nfs4_stateid *stateid,
8360 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008361{
8362 struct nfs4_exception exception = { };
8363 int err;
8364 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008365 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008366 if (err != -NFS4ERR_DELAY)
8367 break;
8368 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008369 } while (exception.retry);
8370 return err;
8371}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008372
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008373struct nfs_free_stateid_data {
8374 struct nfs_server *server;
8375 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008376 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008377};
8378
8379static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8380{
8381 struct nfs_free_stateid_data *data = calldata;
8382 nfs41_setup_sequence(nfs4_get_session(data->server),
8383 &data->args.seq_args,
8384 &data->res.seq_res,
8385 task);
8386}
8387
8388static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8389{
8390 struct nfs_free_stateid_data *data = calldata;
8391
8392 nfs41_sequence_done(task, &data->res.seq_res);
8393
8394 switch (task->tk_status) {
8395 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008396 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008397 rpc_restart_call_prepare(task);
8398 }
8399}
8400
8401static void nfs41_free_stateid_release(void *calldata)
8402{
8403 kfree(calldata);
8404}
8405
Trond Myklebust17f26b12013-08-21 15:48:42 -04008406static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008407 .rpc_call_prepare = nfs41_free_stateid_prepare,
8408 .rpc_call_done = nfs41_free_stateid_done,
8409 .rpc_release = nfs41_free_stateid_release,
8410};
8411
8412static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8413 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008414 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008415 bool privileged)
8416{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008417 struct rpc_message msg = {
8418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008419 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008420 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008421 struct rpc_task_setup task_setup = {
8422 .rpc_client = server->client,
8423 .rpc_message = &msg,
8424 .callback_ops = &nfs41_free_stateid_ops,
8425 .flags = RPC_TASK_ASYNC,
8426 };
8427 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008428
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008429 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8430 &task_setup.rpc_client, &msg);
8431
Chuck Lever38527b12012-07-11 16:30:23 -04008432 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008433 data = kmalloc(sizeof(*data), GFP_NOFS);
8434 if (!data)
8435 return ERR_PTR(-ENOMEM);
8436 data->server = server;
8437 nfs4_stateid_copy(&data->args.stateid, stateid);
8438
8439 task_setup.callback_data = data;
8440
8441 msg.rpc_argp = &data->args;
8442 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008443 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008444 if (privileged)
8445 nfs4_set_sequence_privileged(&data->args.seq_args);
8446
8447 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008448}
8449
Chuck Lever38527b12012-07-11 16:30:23 -04008450/**
8451 * nfs41_free_stateid - perform a FREE_STATEID operation
8452 *
8453 * @server: server / transport on which to perform the operation
8454 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008455 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008456 *
8457 * Returns NFS_OK if the server freed "stateid". Otherwise a
8458 * negative NFS4ERR value is returned.
8459 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008460static int nfs41_free_stateid(struct nfs_server *server,
8461 nfs4_stateid *stateid,
8462 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008463{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008464 struct rpc_task *task;
8465 int ret;
8466
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008467 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008468 if (IS_ERR(task))
8469 return PTR_ERR(task);
8470 ret = rpc_wait_for_completion_task(task);
8471 if (!ret)
8472 ret = task->tk_status;
8473 rpc_put_task(task);
8474 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008475}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008476
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008477static void
8478nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008479{
8480 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008481 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008482
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008483 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008484 nfs4_free_lock_state(server, lsp);
8485 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008486 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008487 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008488}
8489
Trond Myklebust36281ca2012-03-04 18:13:56 -05008490static bool nfs41_match_stateid(const nfs4_stateid *s1,
8491 const nfs4_stateid *s2)
8492{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008493 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008494 return false;
8495
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008496 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008497 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008498 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008499 return true;
8500
8501 return false;
8502}
8503
Andy Adamson557134a2009-04-01 09:21:53 -04008504#endif /* CONFIG_NFS_V4_1 */
8505
Trond Myklebust36281ca2012-03-04 18:13:56 -05008506static bool nfs4_match_stateid(const nfs4_stateid *s1,
8507 const nfs4_stateid *s2)
8508{
Trond Myklebustf597c532012-03-04 18:13:56 -05008509 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008510}
8511
8512
Trond Myklebust17280172012-03-11 13:11:00 -04008513static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008514 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008515 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 .recover_open = nfs4_open_reclaim,
8517 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008518 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008519 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520};
8521
Andy Adamson591d71c2009-04-01 09:22:47 -04008522#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008523static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008524 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8525 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8526 .recover_open = nfs4_open_reclaim,
8527 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008528 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008529 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008530 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008531};
8532#endif /* CONFIG_NFS_V4_1 */
8533
Trond Myklebust17280172012-03-11 13:11:00 -04008534static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008535 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008536 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008537 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008539 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540};
8541
Andy Adamson591d71c2009-04-01 09:22:47 -04008542#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008543static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008544 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8545 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008546 .recover_open = nfs41_open_expired,
8547 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008548 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008549};
8550#endif /* CONFIG_NFS_V4_1 */
8551
Trond Myklebust17280172012-03-11 13:11:00 -04008552static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008553 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008554 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008555 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008556};
8557
8558#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008559static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008560 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008561 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008562 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008563};
8564#endif
8565
Chuck Leverec011fe2013-10-17 14:12:39 -04008566static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008567 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008568 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008569};
8570
8571#if defined(CONFIG_NFS_V4_1)
8572static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008573 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008574 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008575};
8576#endif /* CONFIG_NFS_V4_1 */
8577
Trond Myklebust97dc1352010-06-16 09:52:26 -04008578static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8579 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008580 .init_caps = NFS_CAP_READDIRPLUS
8581 | NFS_CAP_ATOMIC_OPEN
8582 | NFS_CAP_CHANGE_ATTR
8583 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008584 .init_client = nfs40_init_client,
8585 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008586 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008587 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008588 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008589 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008590 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008591 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8592 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8593 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008594 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008595};
8596
8597#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008598static struct nfs_seqid *
8599nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8600{
8601 return NULL;
8602}
8603
Trond Myklebust97dc1352010-06-16 09:52:26 -04008604static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8605 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008606 .init_caps = NFS_CAP_READDIRPLUS
8607 | NFS_CAP_ATOMIC_OPEN
8608 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008609 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008610 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008611 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008612 .init_client = nfs41_init_client,
8613 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008614 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008615 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008616 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008617 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008618 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008619 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8620 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8621 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008622 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008623};
8624#endif
8625
Steve Dickson42c2c422013-05-22 12:50:38 -04008626#if defined(CONFIG_NFS_V4_2)
8627static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8628 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008629 .init_caps = NFS_CAP_READDIRPLUS
8630 | NFS_CAP_ATOMIC_OPEN
8631 | NFS_CAP_CHANGE_ATTR
8632 | NFS_CAP_POSIX_LOCK
8633 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008634 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008635 | NFS_CAP_ALLOCATE
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008636 | NFS_CAP_DEALLOCATE
Anna Schumakere9831202014-10-22 15:53:10 -04008637 | NFS_CAP_SEEK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008638 .init_client = nfs41_init_client,
8639 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008640 .match_stateid = nfs41_match_stateid,
8641 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008642 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008643 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008644 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008645 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8646 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8647 .state_renewal_ops = &nfs41_state_renewal_ops,
8648};
8649#endif
8650
Trond Myklebust97dc1352010-06-16 09:52:26 -04008651const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8652 [0] = &nfs_v4_0_minor_ops,
8653#if defined(CONFIG_NFS_V4_1)
8654 [1] = &nfs_v4_1_minor_ops,
8655#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008656#if defined(CONFIG_NFS_V4_2)
8657 [2] = &nfs_v4_2_minor_ops,
8658#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008659};
8660
Trond Myklebust17f26b12013-08-21 15:48:42 -04008661static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008662 .create = nfs_create,
8663 .lookup = nfs_lookup,
8664 .atomic_open = nfs_atomic_open,
8665 .link = nfs_link,
8666 .unlink = nfs_unlink,
8667 .symlink = nfs_symlink,
8668 .mkdir = nfs_mkdir,
8669 .rmdir = nfs_rmdir,
8670 .mknod = nfs_mknod,
8671 .rename = nfs_rename,
8672 .permission = nfs_permission,
8673 .getattr = nfs_getattr,
8674 .setattr = nfs_setattr,
8675 .getxattr = generic_getxattr,
8676 .setxattr = generic_setxattr,
8677 .listxattr = generic_listxattr,
8678 .removexattr = generic_removexattr,
8679};
8680
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008681static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008682 .permission = nfs_permission,
8683 .getattr = nfs_getattr,
8684 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008685 .getxattr = generic_getxattr,
8686 .setxattr = generic_setxattr,
8687 .listxattr = generic_listxattr,
8688 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008689};
8690
David Howells509de812006-08-22 20:06:11 -04008691const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 .version = 4, /* protocol version */
8693 .dentry_ops = &nfs4_dentry_operations,
8694 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008695 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008696 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008698 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008699 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008700 .getattr = nfs4_proc_getattr,
8701 .setattr = nfs4_proc_setattr,
8702 .lookup = nfs4_proc_lookup,
8703 .access = nfs4_proc_access,
8704 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705 .create = nfs4_proc_create,
8706 .remove = nfs4_proc_remove,
8707 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008708 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008710 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008711 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008712 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008713 .link = nfs4_proc_link,
8714 .symlink = nfs4_proc_symlink,
8715 .mkdir = nfs4_proc_mkdir,
8716 .rmdir = nfs4_proc_remove,
8717 .readdir = nfs4_proc_readdir,
8718 .mknod = nfs4_proc_mknod,
8719 .statfs = nfs4_proc_statfs,
8720 .fsinfo = nfs4_proc_fsinfo,
8721 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008722 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008724 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008726 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008728 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008729 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008730 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008731 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008733 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008734 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008735 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008736 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008737 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008738 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008739 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008740 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008741 .create_server = nfs4_create_server,
8742 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743};
8744
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008745static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8746 .prefix = XATTR_NAME_NFSV4_ACL,
8747 .list = nfs4_xattr_list_nfs4_acl,
8748 .get = nfs4_xattr_get_nfs4_acl,
8749 .set = nfs4_xattr_set_nfs4_acl,
8750};
8751
8752const struct xattr_handler *nfs4_xattr_handlers[] = {
8753 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008754#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8755 &nfs4_xattr_nfs4_label_handler,
8756#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008757 NULL
8758};
8759
Linus Torvalds1da177e2005-04-16 15:20:36 -07008760/*
8761 * Local variables:
8762 * c-basic-offset: 8
8763 * End:
8764 */