blob: d5c2a461f28185485e640358663b862c5670b71b [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 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400347static int nfs4_do_handle_exception(struct nfs_server *server,
348 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500349{
350 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500351 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500352 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 int ret = errorcode;
354
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400355 exception->delay = 0;
356 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500357 exception->retry = 0;
358 switch(errorcode) {
359 case 0:
360 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500361 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400362 case -NFS4ERR_DELEG_REVOKED:
363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400365 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400366 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500367 exception->retry = 1;
368 return 0;
369 }
370 if (state == NULL)
371 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400372 ret = nfs4_schedule_stateid_recovery(server, state);
373 if (ret < 0)
374 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500375 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400376 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400377 if (state != NULL) {
378 ret = nfs4_schedule_stateid_recovery(server, state);
379 if (ret < 0)
380 break;
381 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500383 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500384 nfs4_schedule_lease_recovery(clp);
385 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400386 case -NFS4ERR_MOVED:
387 ret = nfs4_schedule_migration_recovery(server);
388 if (ret < 0)
389 break;
390 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400391 case -NFS4ERR_LEASE_MOVED:
392 nfs4_schedule_lease_moved_recovery(clp);
393 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500394#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400395 case -NFS4ERR_BADSESSION:
396 case -NFS4ERR_BADSLOT:
397 case -NFS4ERR_BAD_HIGH_SLOT:
398 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
399 case -NFS4ERR_DEADSESSION:
400 case -NFS4ERR_SEQ_FALSE_RETRY:
401 case -NFS4ERR_SEQ_MISORDERED:
402 dprintk("%s ERROR: %d Reset session\n", __func__,
403 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400404 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400405 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500406#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500408 if (exception->timeout > HZ) {
409 /* We have retried a decent amount, time to
410 * fail
411 */
412 ret = -EBUSY;
413 break;
414 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500415 case -NFS4ERR_GRACE:
416 case -NFS4ERR_DELAY:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400417 exception->delay = 1;
418 return 0;
419
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400420 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500421 case -NFS4ERR_OLD_STATEID:
422 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800423 break;
424 case -NFS4ERR_BADOWNER:
425 /* The following works around a Linux server bug! */
426 case -NFS4ERR_BADNAME:
427 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
428 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
429 exception->retry = 1;
430 printk(KERN_WARNING "NFS: v4 server %s "
431 "does not accept raw "
432 "uid/gids. "
433 "Reenabling the idmapper.\n",
434 server->nfs_client->cl_hostname);
435 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500436 }
437 /* We failed to handle the error */
438 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500439wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400440 exception->recovering = 1;
441 return 0;
442}
443
444/* This is the error handling routine for processes that are allowed
445 * to sleep.
446 */
447int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
448{
449 struct nfs_client *clp = server->nfs_client;
450 int ret;
451
452 ret = nfs4_do_handle_exception(server, errorcode, exception);
453 if (exception->delay) {
454 ret = nfs4_delay(server->client, &exception->timeout);
455 goto out_retry;
456 }
457 if (exception->recovering) {
458 ret = nfs4_wait_clnt_recover(clp);
459 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
460 return -EIO;
461 goto out_retry;
462 }
463 return ret;
464out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500465 if (ret == 0)
466 exception->retry = 1;
467 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500468}
469
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400470/*
471 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
472 * or 'false' otherwise.
473 */
474static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
475{
476 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
477
478 if (flavor == RPC_AUTH_GSS_KRB5I ||
479 flavor == RPC_AUTH_GSS_KRB5P)
480 return true;
481
482 return false;
483}
Trond Myklebust65de8722008-12-23 15:21:44 -0500484
Trond Myklebust452e9352010-07-31 14:29:06 -0400485static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 spin_lock(&clp->cl_lock);
488 if (time_before(clp->cl_last_renewal,timestamp))
489 clp->cl_last_renewal = timestamp;
490 spin_unlock(&clp->cl_lock);
491}
492
Trond Myklebust452e9352010-07-31 14:29:06 -0400493static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
494{
Trond Myklebustbe824162015-07-05 14:50:46 -0400495 struct nfs_client *clp = server->nfs_client;
496
497 if (!nfs4_has_session(clp))
498 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400499}
500
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400501struct nfs4_call_sync_data {
502 const struct nfs_server *seq_server;
503 struct nfs4_sequence_args *seq_args;
504 struct nfs4_sequence_res *seq_res;
505};
506
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800507void nfs4_init_sequence(struct nfs4_sequence_args *args,
508 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400509{
510 args->sa_slot = NULL;
511 args->sa_cache_this = cache_reply;
512 args->sa_privileged = 0;
513
514 res->sr_slot = NULL;
515}
516
517static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
518{
519 args->sa_privileged = 1;
520}
521
Peng Taocb04ad22014-06-11 05:24:15 +0800522int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
523 struct nfs4_sequence_args *args,
524 struct nfs4_sequence_res *res,
525 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400526{
Chuck Lever3bd23842013-08-09 12:49:19 -0400527 struct nfs4_slot *slot;
528
529 /* slot already allocated? */
530 if (res->sr_slot != NULL)
531 goto out_start;
532
533 spin_lock(&tbl->slot_tbl_lock);
534 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
535 goto out_sleep;
536
537 slot = nfs4_alloc_slot(tbl);
538 if (IS_ERR(slot)) {
539 if (slot == ERR_PTR(-ENOMEM))
540 task->tk_timeout = HZ >> 2;
541 goto out_sleep;
542 }
543 spin_unlock(&tbl->slot_tbl_lock);
544
545 args->sa_slot = slot;
546 res->sr_slot = slot;
547
548out_start:
549 rpc_call_start(task);
550 return 0;
551
552out_sleep:
553 if (args->sa_privileged)
554 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
555 NULL, RPC_PRIORITY_PRIVILEGED);
556 else
557 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
558 spin_unlock(&tbl->slot_tbl_lock);
559 return -EAGAIN;
560}
Peng Taocb04ad22014-06-11 05:24:15 +0800561EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400562
563static int nfs40_sequence_done(struct rpc_task *task,
564 struct nfs4_sequence_res *res)
565{
566 struct nfs4_slot *slot = res->sr_slot;
567 struct nfs4_slot_table *tbl;
568
Trond Myklebustcab92c12014-01-29 12:12:15 -0500569 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400570 goto out;
571
572 tbl = slot->table;
573 spin_lock(&tbl->slot_tbl_lock);
574 if (!nfs41_wake_and_assign_slot(tbl, slot))
575 nfs4_free_slot(tbl, slot);
576 spin_unlock(&tbl->slot_tbl_lock);
577
578 res->sr_slot = NULL;
579out:
580 return 1;
581}
582
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400583#if defined(CONFIG_NFS_V4_1)
584
Trond Myklebustd185a332010-06-16 09:52:25 -0400585static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400586{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500587 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400588 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500589 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500590 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400591
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500592 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500593 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500594
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500595 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500596 /* Be nice to the server: try to ensure that the last transmitted
597 * value for highest_user_slotid <= target_highest_slotid
598 */
599 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
600 send_new_highest_used_slotid = true;
601
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500602 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500603 send_new_highest_used_slotid = false;
604 goto out_unlock;
605 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500606 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500607
608 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
609 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500610out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500611 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400612 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500613 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400614 nfs41_notify_server(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400615}
616
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500617int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400618{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500619 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500620 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400621 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500622 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500623 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400624
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500625 if (slot == NULL)
626 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400627 /* don't increment the sequence number if the task wasn't sent */
628 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400629 goto out;
630
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500631 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500632
Trond Myklebustac20d162012-12-15 15:36:07 -0500633 if (slot->interrupted) {
634 slot->interrupted = 0;
635 interrupted = true;
636 }
637
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400638 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500639 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400640 switch (res->sr_status) {
641 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400642 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500643 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500644 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500645 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500646 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400647 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500648 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400649 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500650 case 1:
651 /*
652 * sr_status remains 1 if an RPC level error occurred.
653 * The server may or may not have processed the sequence
654 * operation..
655 * Mark the slot as having hosted an interrupted RPC call.
656 */
657 slot->interrupted = 1;
658 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400659 case -NFS4ERR_DELAY:
660 /* The server detected a resend of the RPC call and
661 * returned NFS4ERR_DELAY as per Section 2.10.6.2
662 * of RFC5661.
663 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500664 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400665 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500666 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500667 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400668 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500669 case -NFS4ERR_BADSLOT:
670 /*
671 * The slot id we used was probably retired. Try again
672 * using a different slot id.
673 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500674 goto retry_nowait;
675 case -NFS4ERR_SEQ_MISORDERED:
676 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500677 * Was the last operation on this sequence interrupted?
678 * If so, retry after bumping the sequence number.
679 */
680 if (interrupted) {
681 ++slot->seq_nr;
682 goto retry_nowait;
683 }
684 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500685 * Could this slot have been previously retired?
686 * If so, then the server may be expecting seq_nr = 1!
687 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500688 if (slot->seq_nr != 1) {
689 slot->seq_nr = 1;
690 goto retry_nowait;
691 }
692 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500693 case -NFS4ERR_SEQ_FALSE_RETRY:
694 ++slot->seq_nr;
695 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400696 default:
697 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500698 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400699 }
700out:
701 /* The session may be reset by one of the error handlers. */
702 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400703 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500704out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500705 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500706retry_nowait:
707 if (rpc_restart_call_prepare(task)) {
708 task->tk_status = 0;
709 ret = 0;
710 }
711 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400712out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400713 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400714 goto out;
715 rpc_delay(task, NFS4_POLL_RETRY_MAX);
716 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400717}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500718EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400719
Peng Tao2c4b1312014-06-11 05:24:16 +0800720int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400721{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500722 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400723 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400724 if (!res->sr_slot->table->session)
725 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400726 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400727}
Peng Tao2c4b1312014-06-11 05:24:16 +0800728EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400729
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000730int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400731 struct nfs4_sequence_args *args,
732 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400733 struct rpc_task *task)
734{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400735 struct nfs4_slot *slot;
736 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400737
738 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400739 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400740 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400741 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400742
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400743 tbl = &session->fc_slot_table;
744
Trond Myklebust69d206b2012-11-22 13:21:02 -0500745 task->tk_timeout = 0;
746
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400747 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400748 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400749 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500750 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500751 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400752 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400753 }
754
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500755 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500756 if (IS_ERR(slot)) {
757 /* If out of memory, try again in 1/4 second */
758 if (slot == ERR_PTR(-ENOMEM))
759 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400760 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400761 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400762 }
763 spin_unlock(&tbl->slot_tbl_lock);
764
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500765 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400766
Chuck Levere8d92382013-08-09 12:47:51 -0400767 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500768 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400769
Benny Halevydfb4f3092010-09-24 09:17:01 -0400770 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500771 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400772 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400773 /*
774 * sr_status is only set in decode_sequence, and so will remain
775 * set to 1 if an rpc level failure occurs.
776 */
777 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400778 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400779out_success:
780 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400781 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400782out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400783 /* Privileged tasks are queued with top priority */
784 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400785 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
786 NULL, RPC_PRIORITY_PRIVILEGED);
787 else
788 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400789 spin_unlock(&tbl->slot_tbl_lock);
790 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400791}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000792EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400793
Chuck Lever5a580e02013-08-09 12:48:09 -0400794static int nfs4_setup_sequence(const struct nfs_server *server,
795 struct nfs4_sequence_args *args,
796 struct nfs4_sequence_res *res,
797 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400798{
Trond Myklebust035168a2010-06-16 09:52:26 -0400799 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400800 int ret = 0;
801
Chuck Lever3bd23842013-08-09 12:49:19 -0400802 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800803 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
804 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400805
Chuck Levere8d92382013-08-09 12:47:51 -0400806 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400807 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400808 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400809
Trond Myklebust9d12b212012-01-17 22:04:25 -0500810 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400811
Andy Adamsonce5039c2009-04-01 09:22:13 -0400812 dprintk("<-- %s status=%d\n", __func__, ret);
813 return ret;
814}
815
Andy Adamsonce5039c2009-04-01 09:22:13 -0400816static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
817{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400818 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400819 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400820
Trond Myklebust035168a2010-06-16 09:52:26 -0400821 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
822
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400823 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400824}
825
Andy Adamson69ab40c2009-04-01 09:22:19 -0400826static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
827{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400828 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400829
Trond Myklebust14516c32010-07-31 14:29:06 -0400830 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400831}
832
Trond Myklebust17280172012-03-11 13:11:00 -0400833static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400834 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400835 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400836};
837
Chuck Lever3bd23842013-08-09 12:49:19 -0400838#else /* !CONFIG_NFS_V4_1 */
839
Chuck Lever5a580e02013-08-09 12:48:09 -0400840static int nfs4_setup_sequence(const struct nfs_server *server,
841 struct nfs4_sequence_args *args,
842 struct nfs4_sequence_res *res,
843 struct rpc_task *task)
844{
Peng Taocb04ad22014-06-11 05:24:15 +0800845 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
846 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400847}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400848
Peng Tao2c4b1312014-06-11 05:24:16 +0800849int nfs4_sequence_done(struct rpc_task *task,
850 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400851{
Chuck Lever3bd23842013-08-09 12:49:19 -0400852 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400853}
Peng Tao2c4b1312014-06-11 05:24:16 +0800854EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400855
856#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400857
Chuck Lever9915ea72013-08-09 12:48:27 -0400858static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
859{
860 struct nfs4_call_sync_data *data = calldata;
861 nfs4_setup_sequence(data->seq_server,
862 data->seq_args, data->seq_res, task);
863}
864
865static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
866{
867 struct nfs4_call_sync_data *data = calldata;
868 nfs4_sequence_done(task, data->seq_res);
869}
870
871static const struct rpc_call_ops nfs40_call_sync_ops = {
872 .rpc_call_prepare = nfs40_call_sync_prepare,
873 .rpc_call_done = nfs40_call_sync_done,
874};
875
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400876static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400877 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500878 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 struct nfs4_sequence_args *args,
880 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400881{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100882 int ret;
883 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400884 struct nfs_client *clp = server->nfs_client;
885 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 .seq_server = server,
887 .seq_args = args,
888 .seq_res = res,
889 };
890 struct rpc_task_setup task_setup = {
891 .rpc_client = clnt,
892 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400893 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100894 .callback_data = &data
895 };
896
897 task = rpc_run_task(&task_setup);
898 if (IS_ERR(task))
899 ret = PTR_ERR(task);
900 else {
901 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400902 rpc_put_task(task);
903 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100904 return ret;
905}
906
Bryan Schumaker7c513052011-03-24 17:12:24 +0000907int nfs4_call_sync(struct rpc_clnt *clnt,
908 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000909 struct rpc_message *msg,
910 struct nfs4_sequence_args *args,
911 struct nfs4_sequence_res *res,
912 int cache_reply)
913{
Chuck Levera9c92d62013-08-09 12:48:18 -0400914 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400915 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000916}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
919{
920 struct nfs_inode *nfsi = NFS_I(dir);
921
922 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400923 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700924 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700926 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500927 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000928 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 spin_unlock(&dir->i_lock);
930}
931
932struct nfs4_opendata {
933 struct kref kref;
934 struct nfs_openargs o_arg;
935 struct nfs_openres o_res;
936 struct nfs_open_confirmargs c_arg;
937 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500938 struct nfs4_string owner_name;
939 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +0800940 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400942 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400944 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 struct nfs4_state_owner *owner;
946 struct nfs4_state *state;
947 struct iattr attrs;
948 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400949 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400950 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400951 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400952 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int cancelled;
954};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400955
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400956static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
957 int err, struct nfs4_exception *exception)
958{
959 if (err != -EINVAL)
960 return false;
961 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
962 return false;
963 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
964 exception->retry = 1;
965 return true;
966}
967
Trond Myklebust6ae37332015-01-30 14:21:14 -0500968static u32
969nfs4_map_atomic_open_share(struct nfs_server *server,
970 fmode_t fmode, int openflags)
971{
972 u32 res = 0;
973
974 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
975 case FMODE_READ:
976 res = NFS4_SHARE_ACCESS_READ;
977 break;
978 case FMODE_WRITE:
979 res = NFS4_SHARE_ACCESS_WRITE;
980 break;
981 case FMODE_READ|FMODE_WRITE:
982 res = NFS4_SHARE_ACCESS_BOTH;
983 }
984 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
985 goto out;
986 /* Want no delegation if we're using O_DIRECT */
987 if (openflags & O_DIRECT)
988 res |= NFS4_SHARE_WANT_NO_DELEG;
989out:
990 return res;
991}
992
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400993static enum open_claim_type4
994nfs4_map_atomic_open_claim(struct nfs_server *server,
995 enum open_claim_type4 claim)
996{
997 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
998 return claim;
999 switch (claim) {
1000 default:
1001 return claim;
1002 case NFS4_OPEN_CLAIM_FH:
1003 return NFS4_OPEN_CLAIM_NULL;
1004 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1005 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1006 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1007 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1008 }
1009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001012{
1013 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001014 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001015 p->o_res.seqid = p->o_arg.seqid;
1016 p->c_res.seqid = p->c_arg.seqid;
1017 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001018 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001019 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001020 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001021}
1022
Al Viro82a2c1b2011-06-22 18:30:55 -04001023static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001024 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001025 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001026 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001027 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001028 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001029{
Al Viro82a2c1b2011-06-22 18:30:55 -04001030 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001031 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001032 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001033 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001034 struct nfs4_opendata *p;
1035
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001036 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001037 if (p == NULL)
1038 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001039
1040 p->f_label = nfs4_label_alloc(server, gfp_mask);
1041 if (IS_ERR(p->f_label))
1042 goto err_free_p;
1043
Kinglong Meea49c2692015-07-27 15:31:38 +08001044 p->a_label = nfs4_label_alloc(server, gfp_mask);
1045 if (IS_ERR(p->a_label))
1046 goto err_free_f;
1047
Trond Myklebust63f5f792015-01-23 19:19:25 -05001048 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1049 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001050 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001051 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001052 nfs_sb_active(dentry->d_sb);
1053 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001054 p->dir = parent;
1055 p->owner = sp;
1056 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001057 p->o_arg.open_flags = flags;
1058 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001059 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1060 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001061 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1062 * will return permission denied for all bits until close */
1063 if (!(flags & O_EXCL)) {
1064 /* ask server to check for all possible rights as results
1065 * are cached */
1066 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1067 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001068 }
David Howells7539bba2006-08-22 20:06:09 -04001069 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001070 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1071 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001072 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001073 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001074 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001075 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001076 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001077 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1078 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001079 case NFS4_OPEN_CLAIM_NULL:
1080 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1081 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1082 p->o_arg.fh = NFS_FH(dir);
1083 break;
1084 case NFS4_OPEN_CLAIM_PREVIOUS:
1085 case NFS4_OPEN_CLAIM_FH:
1086 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1087 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001088 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001089 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001090 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001091 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001092
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001093 p->o_arg.u.attrs = &p->attrs;
1094 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001095
1096 verf[0] = jiffies;
1097 verf[1] = current->pid;
1098 memcpy(p->o_arg.u.verifier.data, verf,
1099 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001100 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001101 p->c_arg.fh = &p->o_res.fh;
1102 p->c_arg.stateid = &p->o_res.stateid;
1103 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001104 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001105 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001107
1108err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001109 nfs4_label_free(p->a_label);
1110err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001111 nfs4_label_free(p->f_label);
1112err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001113 kfree(p);
1114err:
1115 dput(parent);
1116 return NULL;
1117}
1118
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001119static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001120{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001121 struct nfs4_opendata *p = container_of(kref,
1122 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001123 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001124
1125 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001126 if (p->state != NULL)
1127 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001128 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001129
Kinglong Meea49c2692015-07-27 15:31:38 +08001130 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001131 nfs4_label_free(p->f_label);
1132
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001133 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001134 dput(p->dentry);
1135 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001136 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001137 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001138 kfree(p);
1139}
1140
1141static void nfs4_opendata_put(struct nfs4_opendata *p)
1142{
1143 if (p != NULL)
1144 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001145}
1146
Trond Myklebust06f814a2006-01-03 09:55:07 +01001147static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1148{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001149 int ret;
1150
Trond Myklebust06f814a2006-01-03 09:55:07 +01001151 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001152 return ret;
1153}
1154
Trond Myklebust24311f82015-09-20 10:50:17 -04001155static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1156 fmode_t fmode)
1157{
1158 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1159 case FMODE_READ|FMODE_WRITE:
1160 return state->n_rdwr != 0;
1161 case FMODE_WRITE:
1162 return state->n_wronly != 0;
1163 case FMODE_READ:
1164 return state->n_rdonly != 0;
1165 }
1166 WARN_ON_ONCE(1);
1167 return false;
1168}
1169
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001170static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001171{
1172 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001173
Trond Myklebust536e43d2012-01-17 22:04:26 -05001174 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001175 goto out;
1176 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001177 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001178 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1179 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001180 break;
1181 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001182 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1183 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001184 break;
1185 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001186 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1187 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001188 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001189out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001190 return ret;
1191}
1192
Trond Myklebust2a606182015-08-19 22:30:00 -05001193static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1194 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001195{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001196 if (delegation == NULL)
1197 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001198 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001199 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001200 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1201 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001202 switch (claim) {
1203 case NFS4_OPEN_CLAIM_NULL:
1204 case NFS4_OPEN_CLAIM_FH:
1205 break;
1206 case NFS4_OPEN_CLAIM_PREVIOUS:
1207 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1208 break;
1209 default:
1210 return 0;
1211 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001212 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001213 return 1;
1214}
1215
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001216static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001217{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001218 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001219 case FMODE_WRITE:
1220 state->n_wronly++;
1221 break;
1222 case FMODE_READ:
1223 state->n_rdonly++;
1224 break;
1225 case FMODE_READ|FMODE_WRITE:
1226 state->n_rdwr++;
1227 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001228 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001229}
1230
Trond Myklebust4f14c192014-02-12 19:15:06 -05001231static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001232{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001233 struct nfs_client *clp = state->owner->so_server->nfs_client;
1234 bool need_recover = false;
1235
1236 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1237 need_recover = true;
1238 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1239 need_recover = true;
1240 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1241 need_recover = true;
1242 if (need_recover)
1243 nfs4_state_mark_reclaim_nograce(clp, state);
1244}
1245
Trond Myklebuste999e802014-02-10 18:20:47 -05001246static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1247 nfs4_stateid *stateid)
1248{
1249 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1250 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001251 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1252 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001253 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001254 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001255 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1256 return true;
1257 return false;
1258}
1259
Trond Myklebustf95549c2015-01-23 18:06:09 -05001260static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1261{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001262 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1263 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001264 if (state->n_wronly)
1265 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1266 if (state->n_rdonly)
1267 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1268 if (state->n_rdwr)
1269 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001270 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001271}
1272
Trond Myklebust226056c2014-02-11 10:41:07 -05001273static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001274 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001275 nfs4_stateid *stateid, fmode_t fmode)
1276{
1277 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1278 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1279 case FMODE_WRITE:
1280 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1281 break;
1282 case FMODE_READ:
1283 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1284 break;
1285 case 0:
1286 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1287 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1288 clear_bit(NFS_OPEN_STATE, &state->flags);
1289 }
1290 if (stateid == NULL)
1291 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001292 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001293 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1294 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1295 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001296 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001297 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001298 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001299 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001300 nfs4_stateid_copy(&state->stateid, stateid);
1301 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001302}
1303
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001304static void nfs_clear_open_stateid(struct nfs4_state *state,
1305 nfs4_stateid *arg_stateid,
1306 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001307{
1308 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001309 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001310 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001311 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1312 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001313}
1314
Trond Myklebust003707c2007-07-05 18:07:55 -04001315static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1316{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001317 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001318 case FMODE_READ:
1319 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1320 break;
1321 case FMODE_WRITE:
1322 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1323 break;
1324 case FMODE_READ|FMODE_WRITE:
1325 set_bit(NFS_O_RDWR_STATE, &state->flags);
1326 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001327 if (!nfs_need_update_open_stateid(state, stateid))
1328 return;
1329 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1330 nfs4_stateid_copy(&state->stateid, stateid);
1331 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001332}
1333
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334static 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 -07001335{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001336 /*
1337 * Protect the call to nfs4_state_set_mode_locked and
1338 * serialise the stateid update
1339 */
1340 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001341 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001342 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001343 set_bit(NFS_DELEGATED_STATE, &state->flags);
1344 }
1345 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001347 write_sequnlock(&state->seqlock);
1348 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001349 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001350 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353static 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 -05001354{
1355 struct nfs_inode *nfsi = NFS_I(state->inode);
1356 struct nfs_delegation *deleg_cur;
1357 int ret = 0;
1358
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001359 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001360
1361 rcu_read_lock();
1362 deleg_cur = rcu_dereference(nfsi->delegation);
1363 if (deleg_cur == NULL)
1364 goto no_delegation;
1365
1366 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001367 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001368 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001369 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001370 goto no_delegation_unlock;
1371
1372 if (delegation == NULL)
1373 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001374 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001375 goto no_delegation_unlock;
1376
Trond Myklebustb7391f42008-12-23 15:21:52 -05001377 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001378 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001379 ret = 1;
1380no_delegation_unlock:
1381 spin_unlock(&deleg_cur->lock);
1382no_delegation:
1383 rcu_read_unlock();
1384
1385 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001386 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001387 ret = 1;
1388 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001389 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1390 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001391
1392 return ret;
1393}
1394
Trond Myklebust39071e62015-01-24 15:07:56 -05001395static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1396 const nfs4_stateid *stateid)
1397{
1398 struct nfs4_state *state = lsp->ls_state;
1399 bool ret = false;
1400
1401 spin_lock(&state->state_lock);
1402 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1403 goto out_noupdate;
1404 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1405 goto out_noupdate;
1406 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1407 ret = true;
1408out_noupdate:
1409 spin_unlock(&state->state_lock);
1410 return ret;
1411}
Trond Myklebust34310432008-12-23 15:21:38 -05001412
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001413static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001414{
1415 struct nfs_delegation *delegation;
1416
1417 rcu_read_lock();
1418 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001419 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001420 rcu_read_unlock();
1421 return;
1422 }
1423 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001424 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001425}
1426
Trond Myklebust6ee41262007-07-08 14:11:36 -04001427static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001428{
1429 struct nfs4_state *state = opendata->state;
1430 struct nfs_inode *nfsi = NFS_I(state->inode);
1431 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001432 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001433 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001434 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001435 nfs4_stateid stateid;
1436 int ret = -EAGAIN;
1437
Trond Myklebustaac00a82007-07-05 19:02:21 -04001438 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001439 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001440 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001441 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001442 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001443 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001444 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001445 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001446 rcu_read_lock();
1447 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001448 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001449 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001450 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001451 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001452 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001453 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001454 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001455 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001456 if (!opendata->is_recover) {
1457 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1458 if (ret != 0)
1459 goto out;
1460 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001461 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001462
1463 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001464 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001465 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001466 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001467out:
1468 return ERR_PTR(ret);
1469out_return_state:
1470 atomic_inc(&state->count);
1471 return state;
1472}
1473
Andy Adamsone23008e2012-10-02 21:07:32 -04001474static void
1475nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1476{
1477 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1478 struct nfs_delegation *delegation;
1479 int delegation_flags = 0;
1480
1481 rcu_read_lock();
1482 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1483 if (delegation)
1484 delegation_flags = delegation->flags;
1485 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001486 switch (data->o_arg.claim) {
1487 default:
1488 break;
1489 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1490 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001491 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1492 "returning a delegation for "
1493 "OPEN(CLAIM_DELEGATE_CUR)\n",
1494 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001495 return;
1496 }
1497 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001498 nfs_inode_set_delegation(state->inode,
1499 data->owner->so_cred,
1500 &data->o_res);
1501 else
1502 nfs_inode_reclaim_delegation(state->inode,
1503 data->owner->so_cred,
1504 &data->o_res);
1505}
1506
1507/*
1508 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1509 * and update the nfs4_state.
1510 */
1511static struct nfs4_state *
1512_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1513{
1514 struct inode *inode = data->state->inode;
1515 struct nfs4_state *state = data->state;
1516 int ret;
1517
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001518 if (!data->rpc_done) {
1519 if (data->rpc_status) {
1520 ret = data->rpc_status;
1521 goto err;
1522 }
1523 /* cached opens have already been processed */
1524 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001525 }
1526
Andy Adamsone23008e2012-10-02 21:07:32 -04001527 ret = nfs_refresh_inode(inode, &data->f_attr);
1528 if (ret)
1529 goto err;
1530
1531 if (data->o_res.delegation_type != 0)
1532 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001533update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001534 update_open_stateid(state, &data->o_res.stateid, NULL,
1535 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001536 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001537
1538 return state;
1539err:
1540 return ERR_PTR(ret);
1541
1542}
1543
1544static struct nfs4_state *
1545_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546{
1547 struct inode *inode;
1548 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001549 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001550
Trond Myklebustaac00a82007-07-05 19:02:21 -04001551 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001552 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001553 goto out;
1554 }
1555
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001556 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001558 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001559 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001560 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001561 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001562 goto err;
1563 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001564 state = nfs4_get_open_state(inode, data->owner);
1565 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001566 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001567 if (data->o_res.delegation_type != 0)
1568 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001569 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001570 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001571 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001572out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001573 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001574 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001575err_put_inode:
1576 iput(inode);
1577err:
1578 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001579}
1580
Andy Adamsone23008e2012-10-02 21:07:32 -04001581static struct nfs4_state *
1582nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1583{
1584 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1585 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1586 return _nfs4_opendata_to_nfs4_state(data);
1587}
1588
Trond Myklebust864472e2006-01-03 09:55:15 +01001589static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1590{
1591 struct nfs_inode *nfsi = NFS_I(state->inode);
1592 struct nfs_open_context *ctx;
1593
1594 spin_lock(&state->inode->i_lock);
1595 list_for_each_entry(ctx, &nfsi->open_files, list) {
1596 if (ctx->state != state)
1597 continue;
1598 get_nfs_open_context(ctx);
1599 spin_unlock(&state->inode->i_lock);
1600 return ctx;
1601 }
1602 spin_unlock(&state->inode->i_lock);
1603 return ERR_PTR(-ENOENT);
1604}
1605
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001606static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1607 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001608{
1609 struct nfs4_opendata *opendata;
1610
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001611 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001612 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001613 if (opendata == NULL)
1614 return ERR_PTR(-ENOMEM);
1615 opendata->state = state;
1616 atomic_inc(&state->count);
1617 return opendata;
1618}
1619
Trond Myklebust24311f82015-09-20 10:50:17 -04001620static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1621 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001622{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001623 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001624 int ret;
1625
Trond Myklebust24311f82015-09-20 10:50:17 -04001626 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001627 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001628 opendata->o_arg.open_flags = 0;
1629 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001630 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1631 NFS_SB(opendata->dentry->d_sb),
1632 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001633 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1634 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1635 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001636 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001637 if (ret != 0)
1638 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001639 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001640 if (IS_ERR(newstate))
1641 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001642 if (newstate != opendata->state)
1643 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001644 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001645 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001646}
1647
1648static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1649{
Trond Myklebust864472e2006-01-03 09:55:15 +01001650 int ret;
1651
Trond Myklebust4f14c192014-02-12 19:15:06 -05001652 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1653 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1654 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1655 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001656 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001657 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001658 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001659 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001660 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1661 if (ret != 0)
1662 return ret;
1663 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1664 if (ret != 0)
1665 return ret;
1666 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1667 if (ret != 0)
1668 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001669 /*
1670 * We may have performed cached opens for all three recoveries.
1671 * Check if we need to update the current stateid.
1672 */
1673 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001674 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001675 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001676 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001677 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001678 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001679 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001680 return 0;
1681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683/*
1684 * OPEN_RECLAIM:
1685 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001687static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001689 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001690 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001691 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 int status;
1693
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001694 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1695 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001696 if (IS_ERR(opendata))
1697 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001698 rcu_read_lock();
1699 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001700 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001701 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001702 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001703 opendata->o_arg.u.delegation_type = delegation_type;
1704 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001705 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return status;
1707}
1708
Trond Myklebust539cd032007-06-05 11:46:42 -04001709static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 struct nfs_server *server = NFS_SERVER(state->inode);
1712 struct nfs4_exception exception = { };
1713 int err;
1714 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001715 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001716 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001717 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1718 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001719 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001720 break;
1721 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 } while (exception.retry);
1723 return err;
1724}
1725
Trond Myklebust864472e2006-01-03 09:55:15 +01001726static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1727{
1728 struct nfs_open_context *ctx;
1729 int ret;
1730
1731 ctx = nfs4_state_find_open_context(state);
1732 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001733 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001734 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001735 put_nfs_open_context(ctx);
1736 return ret;
1737}
1738
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001739static 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 -07001740{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001741 switch (err) {
1742 default:
1743 printk(KERN_ERR "NFS: %s: unhandled error "
1744 "%d.\n", __func__, err);
1745 case 0:
1746 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001747 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001748 case -ESTALE:
1749 break;
1750 case -NFS4ERR_BADSESSION:
1751 case -NFS4ERR_BADSLOT:
1752 case -NFS4ERR_BAD_HIGH_SLOT:
1753 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1754 case -NFS4ERR_DEADSESSION:
1755 set_bit(NFS_DELEGATED_STATE, &state->flags);
1756 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1757 return -EAGAIN;
1758 case -NFS4ERR_STALE_CLIENTID:
1759 case -NFS4ERR_STALE_STATEID:
1760 set_bit(NFS_DELEGATED_STATE, &state->flags);
1761 case -NFS4ERR_EXPIRED:
1762 /* Don't recall a delegation if it was lost */
1763 nfs4_schedule_lease_recovery(server->nfs_client);
1764 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001765 case -NFS4ERR_MOVED:
1766 nfs4_schedule_migration_recovery(server);
1767 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001768 case -NFS4ERR_LEASE_MOVED:
1769 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1770 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001771 case -NFS4ERR_DELEG_REVOKED:
1772 case -NFS4ERR_ADMIN_REVOKED:
1773 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001774 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001775 nfs_inode_find_state_and_recover(state->inode,
1776 stateid);
1777 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001778 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001779 case -NFS4ERR_DELAY:
1780 case -NFS4ERR_GRACE:
1781 set_bit(NFS_DELEGATED_STATE, &state->flags);
1782 ssleep(1);
1783 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001784 case -ENOMEM:
1785 case -NFS4ERR_DENIED:
1786 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1787 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return err;
1790}
1791
Trond Myklebust24311f82015-09-20 10:50:17 -04001792int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1793 struct nfs4_state *state, const nfs4_stateid *stateid,
1794 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001795{
1796 struct nfs_server *server = NFS_SERVER(state->inode);
1797 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001798 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001799
1800 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1801 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1802 if (IS_ERR(opendata))
1803 return PTR_ERR(opendata);
1804 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001805 write_seqlock(&state->seqlock);
1806 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1807 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001808 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1809 switch (type & (FMODE_READ|FMODE_WRITE)) {
1810 case FMODE_READ|FMODE_WRITE:
1811 case FMODE_WRITE:
1812 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1813 if (err)
1814 break;
1815 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1816 if (err)
1817 break;
1818 case FMODE_READ:
1819 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1820 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001821 nfs4_opendata_put(opendata);
1822 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1823}
1824
Chuck Leverbe05c862013-08-09 12:49:47 -04001825static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1826{
1827 struct nfs4_opendata *data = calldata;
1828
Peng Taocb04ad22014-06-11 05:24:15 +08001829 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1830 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001831}
1832
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001833static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1834{
1835 struct nfs4_opendata *data = calldata;
1836
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001837 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001838
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001839 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001840 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001841 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001842 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001843 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001844 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001845 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001846}
1847
1848static void nfs4_open_confirm_release(void *calldata)
1849{
1850 struct nfs4_opendata *data = calldata;
1851 struct nfs4_state *state = NULL;
1852
1853 /* If this request hasn't been cancelled, do nothing */
1854 if (data->cancelled == 0)
1855 goto out_free;
1856 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001857 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001858 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001859 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001860 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001861 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001862out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001863 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001864}
1865
1866static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001867 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001868 .rpc_call_done = nfs4_open_confirm_done,
1869 .rpc_release = nfs4_open_confirm_release,
1870};
1871
1872/*
1873 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1874 */
1875static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1876{
David Howells2b0143b2015-03-17 22:25:59 +00001877 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001878 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001879 struct rpc_message msg = {
1880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1881 .rpc_argp = &data->c_arg,
1882 .rpc_resp = &data->c_res,
1883 .rpc_cred = data->owner->so_cred,
1884 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001885 struct rpc_task_setup task_setup_data = {
1886 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001887 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001888 .callback_ops = &nfs4_open_confirm_ops,
1889 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001890 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001891 .flags = RPC_TASK_ASYNC,
1892 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 int status;
1894
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001895 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001896 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001897 data->rpc_done = 0;
1898 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001899 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04001900 if (data->is_recover)
1901 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001902 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001903 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001904 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001905 status = nfs4_wait_for_completion_rpc_task(task);
1906 if (status != 0) {
1907 data->cancelled = 1;
1908 smp_wmb();
1909 } else
1910 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001911 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return status;
1913}
1914
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001915static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001917 struct nfs4_opendata *data = calldata;
1918 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001919 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05001920 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001921
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001922 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001923 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001924 /*
1925 * Check if we still need to send an OPEN call, or if we can use
1926 * a delegation instead.
1927 */
1928 if (data->state != NULL) {
1929 struct nfs_delegation *delegation;
1930
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001931 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001932 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001933 rcu_read_lock();
1934 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001935 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05001936 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001937 rcu_read_unlock();
1938 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001939 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001940 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05001941 switch (claim) {
1942 default:
1943 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04001944 case NFS4_OPEN_CLAIM_PREVIOUS:
1945 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1946 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001947 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001948 case NFS4_OPEN_CLAIM_FH:
1949 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001950 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1951 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001952 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001953 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001954 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001955 &data->o_res.seq_res,
1956 task) != 0)
1957 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001958
1959 /* Set the create mode (note dependency on the session type) */
1960 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1961 if (data->o_arg.open_flags & O_EXCL) {
1962 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1963 if (nfs4_has_persistent_session(clp))
1964 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1965 else if (clp->cl_mvops->minor_version > 0)
1966 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1967 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001968 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001969unlock_no_action:
1970 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001971out_no_action:
1972 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001973out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001974 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001975}
1976
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001977static void nfs4_open_done(struct rpc_task *task, void *calldata)
1978{
1979 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001981 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001982
Trond Myklebust14516c32010-07-31 14:29:06 -04001983 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1984 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001985
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001986 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001987 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1988 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001989 case S_IFREG:
1990 break;
1991 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001992 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001993 break;
1994 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001995 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001996 break;
1997 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001998 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001999 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002000 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002001 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002002 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2003 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002004 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002005 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002006}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002007
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002008static void nfs4_open_release(void *calldata)
2009{
2010 struct nfs4_opendata *data = calldata;
2011 struct nfs4_state *state = NULL;
2012
2013 /* If this request hasn't been cancelled, do nothing */
2014 if (data->cancelled == 0)
2015 goto out_free;
2016 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002017 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002018 goto out_free;
2019 /* In case we need an open_confirm, no cleanup! */
2020 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2021 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002022 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002023 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002024 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002025out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002026 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002027}
2028
2029static const struct rpc_call_ops nfs4_open_ops = {
2030 .rpc_call_prepare = nfs4_open_prepare,
2031 .rpc_call_done = nfs4_open_done,
2032 .rpc_release = nfs4_open_release,
2033};
2034
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002035static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002036{
David Howells2b0143b2015-03-17 22:25:59 +00002037 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002038 struct nfs_server *server = NFS_SERVER(dir);
2039 struct nfs_openargs *o_arg = &data->o_arg;
2040 struct nfs_openres *o_res = &data->o_res;
2041 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002042 struct rpc_message msg = {
2043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2044 .rpc_argp = o_arg,
2045 .rpc_resp = o_res,
2046 .rpc_cred = data->owner->so_cred,
2047 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002048 struct rpc_task_setup task_setup_data = {
2049 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002050 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002051 .callback_ops = &nfs4_open_ops,
2052 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002053 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002054 .flags = RPC_TASK_ASYNC,
2055 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002056 int status;
2057
Chuck Levera9c92d62013-08-09 12:48:18 -04002058 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002059 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002060 data->rpc_done = 0;
2061 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002062 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002063 data->is_recover = 0;
2064 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002065 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002066 data->is_recover = 1;
2067 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002068 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002069 if (IS_ERR(task))
2070 return PTR_ERR(task);
2071 status = nfs4_wait_for_completion_rpc_task(task);
2072 if (status != 0) {
2073 data->cancelled = 1;
2074 smp_wmb();
2075 } else
2076 status = data->rpc_status;
2077 rpc_put_task(task);
2078
2079 return status;
2080}
2081
2082static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2083{
David Howells2b0143b2015-03-17 22:25:59 +00002084 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002085 struct nfs_openres *o_res = &data->o_res;
2086 int status;
2087
2088 status = nfs4_run_open_task(data, 1);
2089 if (status != 0 || !data->rpc_done)
2090 return status;
2091
Trond Myklebust6926afd2012-01-07 13:22:46 -05002092 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2093
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002094 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2095 status = _nfs4_proc_open_confirm(data);
2096 if (status != 0)
2097 return status;
2098 }
2099
2100 return status;
2101}
2102
Trond Myklebustf3792d62014-07-10 08:54:32 -04002103/*
2104 * Additional permission checks in order to distinguish between an
2105 * open for read, and an open for execute. This works around the
2106 * fact that NFSv4 OPEN treats read and execute permissions as being
2107 * the same.
2108 * Note that in the non-execute case, we want to turn off permission
2109 * checking if we just created a new file (POSIX open() semantics).
2110 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002111static int nfs4_opendata_access(struct rpc_cred *cred,
2112 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002113 struct nfs4_state *state, fmode_t fmode,
2114 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002115{
2116 struct nfs_access_entry cache;
2117 u32 mask;
2118
2119 /* access call failed or for some reason the server doesn't
2120 * support any access modes -- defer access call until later */
2121 if (opendata->o_res.access_supported == 0)
2122 return 0;
2123
2124 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002125 /*
2126 * Use openflags to check for exec, because fmode won't
2127 * always have FMODE_EXEC set when file open for exec.
2128 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002129 if (openflags & __FMODE_EXEC) {
2130 /* ONLY check for exec rights */
2131 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002132 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002133 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002134
2135 cache.cred = cred;
2136 cache.jiffies = jiffies;
2137 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2138 nfs_access_add_cache(state->inode, &cache);
2139
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002140 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002141 return 0;
2142
2143 /* even though OPEN succeeded, access is denied. Close the file */
2144 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002145 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002146}
2147
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002148/*
2149 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2150 */
2151static int _nfs4_proc_open(struct nfs4_opendata *data)
2152{
David Howells2b0143b2015-03-17 22:25:59 +00002153 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002154 struct nfs_server *server = NFS_SERVER(dir);
2155 struct nfs_openargs *o_arg = &data->o_arg;
2156 struct nfs_openres *o_res = &data->o_res;
2157 int status;
2158
2159 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002160 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002161 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002162 if (status != 0) {
2163 if (status == -NFS4ERR_BADNAME &&
2164 !(o_arg->open_flags & O_CREAT))
2165 return -ENOENT;
2166 return status;
2167 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002168
Trond Myklebust6926afd2012-01-07 13:22:46 -05002169 nfs_fattr_map_and_free_names(server, &data->f_attr);
2170
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002171 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002172 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002173 if (o_arg->open_flags & O_EXCL)
2174 data->file_created = 1;
2175 else if (o_res->cinfo.before != o_res->cinfo.after)
2176 data->file_created = 1;
2177 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002178 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2179 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002181 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002183 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002186 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Andy Adamsond83217c2011-03-01 01:34:17 +00002190static int nfs4_recover_expired_lease(struct nfs_server *server)
2191{
2192 return nfs4_client_recover_expired_lease(server->nfs_client);
2193}
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195/*
2196 * OPEN_EXPIRED:
2197 * reclaim state on the server after a network partition.
2198 * Assumes caller holds the appropriate lock
2199 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002200static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002202 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002203 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002205 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002206 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002207 if (IS_ERR(opendata))
2208 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002209 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002210 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002211 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002212 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002213 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002216static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002217{
Trond Myklebust539cd032007-06-05 11:46:42 -04002218 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002219 struct nfs4_exception exception = { };
2220 int err;
2221
2222 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002223 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002224 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002225 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2226 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002227 switch (err) {
2228 default:
2229 goto out;
2230 case -NFS4ERR_GRACE:
2231 case -NFS4ERR_DELAY:
2232 nfs4_handle_exception(server, err, &exception);
2233 err = 0;
2234 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002235 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002236out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002237 return err;
2238}
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002243 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Trond Myklebust864472e2006-01-03 09:55:15 +01002245 ctx = nfs4_state_find_open_context(state);
2246 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002247 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002248 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002249 put_nfs_open_context(ctx);
2250 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002253static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2254{
2255 nfs_remove_bad_delegation(state->inode);
2256 write_seqlock(&state->seqlock);
2257 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2258 write_sequnlock(&state->seqlock);
2259 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2260}
2261
2262static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2263{
2264 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2265 nfs_finish_clear_delegation_stateid(state);
2266}
2267
2268static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2269{
2270 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2271 nfs40_clear_delegation_stateid(state);
2272 return nfs4_open_expired(sp, state);
2273}
2274
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002275#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002276static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002277{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002278 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002279 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002280 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002281 struct rpc_cred *cred;
2282 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002283
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002284 /* Get the delegation credential for use by test/free_stateid */
2285 rcu_read_lock();
2286 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002287 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002288 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002289 return;
2290 }
2291
2292 nfs4_stateid_copy(&stateid, &delegation->stateid);
2293 cred = get_rpccred(delegation->cred);
2294 rcu_read_unlock();
2295 status = nfs41_test_stateid(server, &stateid, cred);
2296 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002297
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002298 if (status != NFS_OK) {
2299 /* Free the stateid unless the server explicitly
2300 * informs us the stateid is unrecognized. */
2301 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002302 nfs41_free_stateid(server, &stateid, cred);
2303 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002304 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002305
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002306 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002307}
2308
2309/**
2310 * nfs41_check_open_stateid - possibly free an open stateid
2311 *
2312 * @state: NFSv4 state for an inode
2313 *
2314 * Returns NFS_OK if recovery for this stateid is now finished.
2315 * Otherwise a negative NFS4ERR value is returned.
2316 */
2317static int nfs41_check_open_stateid(struct nfs4_state *state)
2318{
2319 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002320 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002321 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002322 int status;
2323
2324 /* If a state reset has been done, test_stateid is unneeded */
2325 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2326 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2327 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2328 return -NFS4ERR_BAD_STATEID;
2329
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002330 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002331 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002332 if (status != NFS_OK) {
2333 /* Free the stateid unless the server explicitly
2334 * informs us the stateid is unrecognized. */
2335 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002336 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002337
2338 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2339 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2340 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002341 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002342 }
2343 return status;
2344}
2345
2346static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2347{
Chuck Levereb64cf92012-07-11 16:30:05 -04002348 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002349
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002350 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002351 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002352 if (status != NFS_OK)
2353 status = nfs4_open_expired(sp, state);
2354 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002355}
2356#endif
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002359 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2360 * fields corresponding to attributes that were used to store the verifier.
2361 * Make sure we clobber those fields in the later setattr call
2362 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002363static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2364 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002365{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002366 const u32 *attrset = opendata->o_res.attrset;
2367
2368 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002369 !(sattr->ia_valid & ATTR_ATIME_SET))
2370 sattr->ia_valid |= ATTR_ATIME;
2371
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002372 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002373 !(sattr->ia_valid & ATTR_MTIME_SET))
2374 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002375
2376 /* Except MODE, it seems harmless of setting twice. */
2377 if ((attrset[1] & FATTR4_WORD1_MODE))
2378 sattr->ia_valid &= ~ATTR_MODE;
2379
2380 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2381 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002382}
2383
Trond Myklebustc21443c2013-02-07 14:26:21 -05002384static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2385 fmode_t fmode,
2386 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002387 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002388{
2389 struct nfs4_state_owner *sp = opendata->owner;
2390 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002391 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002392 struct nfs4_state *state;
2393 unsigned int seq;
2394 int ret;
2395
2396 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2397
2398 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002399 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002400 goto out;
2401
2402 state = nfs4_opendata_to_nfs4_state(opendata);
2403 ret = PTR_ERR(state);
2404 if (IS_ERR(state))
2405 goto out;
2406 if (server->caps & NFS_CAP_POSIX_LOCK)
2407 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2408
Trond Myklebust275bb302013-05-29 13:11:28 -04002409 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002410 if (d_really_is_negative(dentry)) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002411 /* FIXME: Is this d_drop() ever needed? */
2412 d_drop(dentry);
2413 dentry = d_add_unique(dentry, igrab(state->inode));
2414 if (dentry == NULL) {
2415 dentry = opendata->dentry;
2416 } else if (dentry != ctx->dentry) {
2417 dput(ctx->dentry);
2418 ctx->dentry = dget(dentry);
2419 }
2420 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002421 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002422 }
2423
Trond Myklebustc21443c2013-02-07 14:26:21 -05002424 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2425 if (ret != 0)
2426 goto out;
2427
Trond Myklebust3efb9722013-05-29 13:17:04 -04002428 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002429 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002430 nfs_inode_attach_open_context(ctx);
2431 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2432 nfs4_schedule_stateid_recovery(server, state);
2433 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002434out:
2435 return ret;
2436}
2437
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002438/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002439 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 */
Andy Adamson82be4172012-05-23 05:02:35 -04002441static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002442 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002443 int flags,
2444 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002445 struct nfs4_label *label,
2446 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
2448 struct nfs4_state_owner *sp;
2449 struct nfs4_state *state = NULL;
2450 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002451 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002452 struct dentry *dentry = ctx->dentry;
2453 struct rpc_cred *cred = ctx->cred;
2454 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2455 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002456 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002457 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002458 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002462 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2463 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2465 goto out_err;
2466 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002467 status = nfs4_recover_expired_lease(server);
2468 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002469 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002470 if (d_really_is_positive(dentry))
2471 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002472 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002473 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002474 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002475 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002476 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002477 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002478 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
David Quigley14c43f72013-05-22 12:50:43 -04002480 if (label) {
2481 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2482 if (IS_ERR(olabel)) {
2483 status = PTR_ERR(olabel);
2484 goto err_opendata_put;
2485 }
2486 }
2487
Trond Myklebuste911b812014-03-26 13:24:37 -07002488 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2489 if (!opendata->f_attr.mdsthreshold) {
2490 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2491 if (!opendata->f_attr.mdsthreshold)
2492 goto err_free_label;
2493 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002494 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002495 }
David Howells2b0143b2015-03-17 22:25:59 +00002496 if (d_really_is_positive(dentry))
2497 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002498
Trond Myklebust3efb9722013-05-29 13:17:04 -04002499 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002500 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002501 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002502 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002503
NeilBrownefcbc042015-07-30 13:00:56 +10002504 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002505 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002506 nfs4_exclusive_attrset(opendata, sattr, &label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002507
2508 nfs_fattr_init(opendata->o_res.f_attr);
2509 status = nfs4_do_setattr(state->inode, cred,
2510 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002511 state, label, olabel);
2512 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05002513 nfs_setattr_update_inode(state->inode, sattr,
2514 opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002515 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002516 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002517 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002518 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002519 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002520
Trond Myklebuste911b812014-03-26 13:24:37 -07002521 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002522 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002523 opendata->f_attr.mdsthreshold = NULL;
2524 }
Andy Adamson82be4172012-05-23 05:02:35 -04002525
David Quigley14c43f72013-05-22 12:50:43 -04002526 nfs4_label_free(olabel);
2527
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002528 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002531err_free_label:
2532 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002533err_opendata_put:
2534 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002535err_put_state_owner:
2536 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return status;
2539}
2540
2541
Andy Adamson82be4172012-05-23 05:02:35 -04002542static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002543 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002544 int flags,
2545 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002546 struct nfs4_label *label,
2547 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002549 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 struct nfs4_exception exception = { };
2551 struct nfs4_state *res;
2552 int status;
2553
2554 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002555 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002556 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002557 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (status == 0)
2559 break;
2560 /* NOTE: BAD_SEQID means the server and client disagree about the
2561 * book-keeping w.r.t. state-changing operations
2562 * (OPEN/CLOSE/LOCK/LOCKU...)
2563 * It is actually a sign of a bug on the client or on the server.
2564 *
2565 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002566 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 * have unhashed the old state_owner for us, and that we can
2568 * therefore safely retry using a new one. We should still warn
2569 * the user though...
2570 */
2571 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002572 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002573 " returned a bad sequence-id error!\n",
2574 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 exception.retry = 1;
2576 continue;
2577 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002578 /*
2579 * BAD_STATEID on OPEN means that the server cancelled our
2580 * state before it received the OPEN_CONFIRM.
2581 * Recover by retrying the request as per the discussion
2582 * on Page 181 of RFC3530.
2583 */
2584 if (status == -NFS4ERR_BAD_STATEID) {
2585 exception.retry = 1;
2586 continue;
2587 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002588 if (status == -EAGAIN) {
2589 /* We must have found a delegation */
2590 exception.retry = 1;
2591 continue;
2592 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002593 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2594 continue;
2595 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 status, &exception));
2597 } while (exception.retry);
2598 return res;
2599}
2600
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002601static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2602 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002603 struct nfs4_state *state, struct nfs4_label *ilabel,
2604 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002606 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002608 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 .iap = sattr,
2610 .server = server,
2611 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002612 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 };
2614 struct nfs_setattrres res = {
2615 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002616 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 .server = server,
2618 };
2619 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2621 .rpc_argp = &arg,
2622 .rpc_resp = &res,
2623 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002625 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002626 fmode_t fmode;
2627 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002628 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
David Quigleyaa9c2662013-05-22 12:50:44 -04002630 arg.bitmask = nfs4_bitmask(server, ilabel);
2631 if (ilabel)
2632 arg.bitmask = nfs4_bitmask(server, olabel);
2633
Trond Myklebust0e574af2005-10-27 22:12:38 -04002634 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Trond Myklebustee3ae842013-04-29 10:35:36 -04002636 /* Servers should only apply open mode checks for file size changes */
2637 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2638 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2639
2640 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2641 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002642 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002643 struct nfs_lockowner lockowner = {
2644 .l_owner = current->files,
2645 .l_pid = current->tgid,
2646 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002647 if (!nfs4_valid_open_stateid(state))
2648 return -EBADF;
2649 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2650 &lockowner) == -EIO)
2651 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002652 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002653 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Bryan Schumaker7c513052011-03-24 17:12:24 +00002655 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002656 if (status == 0 && state != NULL)
2657 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002658 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002661static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2662 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002663 struct nfs4_state *state, struct nfs4_label *ilabel,
2664 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002666 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002667 struct nfs4_exception exception = {
2668 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002669 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002670 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 int err;
2672 do {
David Quigley1775fd32013-05-22 12:50:42 -04002673 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002674 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002675 switch (err) {
2676 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002677 if (!(sattr->ia_valid & ATTR_SIZE)) {
2678 pr_warn_once("NFSv4: server %s is incorrectly "
2679 "applying open mode checks to "
2680 "a SETATTR that is not "
2681 "changing file size.\n",
2682 server->nfs_client->cl_hostname);
2683 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002684 if (state && !(state->state & FMODE_WRITE)) {
2685 err = -EBADF;
2686 if (sattr->ia_valid & ATTR_OPEN)
2687 err = -EACCES;
2688 goto out;
2689 }
2690 }
2691 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002693out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return err;
2695}
2696
Peng Tao500d7012015-09-22 11:35:22 +08002697static bool
2698nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2699{
2700 if (inode == NULL || !nfs_have_layout(inode))
2701 return false;
2702
2703 return pnfs_wait_on_layoutreturn(inode, task);
2704}
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706struct nfs4_closedata {
2707 struct inode *inode;
2708 struct nfs4_state *state;
2709 struct nfs_closeargs arg;
2710 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002711 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002712 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002713 bool roc;
2714 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715};
2716
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002717static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002718{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002719 struct nfs4_closedata *calldata = data;
2720 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002721 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002722
Fred Isamanf7e89172011-01-06 11:36:32 +00002723 if (calldata->roc)
2724 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002725 nfs4_put_open_state(calldata->state);
2726 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002727 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002728 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002729 kfree(calldata);
2730}
2731
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002732static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002734 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002737 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Chuck Levera3ca5652012-03-01 17:00:40 -05002739 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002740 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2741 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002742 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 /* hmm. we are done with the inode, and in the process of freeing
2744 * the state_owner. we keep this around to process errors
2745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 switch (task->tk_status) {
2747 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002748 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002749 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002750 pnfs_roc_set_barrier(state->inode,
2751 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002752 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002753 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002754 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002756 case -NFS4ERR_OLD_STATEID:
2757 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002759 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002760 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002761 rpc_restart_call_prepare(task);
2762 goto out_release;
2763 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002764 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002765 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002767 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002768 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002769 goto out_release;
2770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002772 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2773 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002774out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002775 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002776 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002777 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002780static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002782 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002783 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002784 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002785 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002786 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002787
Chuck Levera3ca5652012-03-01 17:00:40 -05002788 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002789 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002790 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002791
Trond Myklebust88069f72009-12-08 08:33:16 -05002792 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002793 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002794 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2795 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2796 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002797 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002798 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002799 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002800 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002801 if (state->n_rdonly == 0)
2802 call_close |= is_rdonly;
2803 else if (is_rdonly)
2804 calldata->arg.fmode |= FMODE_READ;
2805 if (state->n_wronly == 0)
2806 call_close |= is_wronly;
2807 else if (is_wronly)
2808 calldata->arg.fmode |= FMODE_WRITE;
2809 } else if (is_rdwr)
2810 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2811
2812 if (calldata->arg.fmode == 0)
2813 call_close |= is_rdwr;
2814
Trond Myklebust5d422302013-03-14 16:57:48 -04002815 if (!nfs4_valid_open_stateid(state))
2816 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002817 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002818
2819 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002820 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002821 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002822 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002823
Peng Tao500d7012015-09-22 11:35:22 +08002824 if (nfs4_wait_on_layoutreturn(inode, task)) {
2825 nfs_release_seqid(calldata->arg.seqid);
2826 goto out_wait;
2827 }
2828
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002829 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002830 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002831 if (calldata->roc)
2832 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2833
Trond Myklebust6ae37332015-01-30 14:21:14 -05002834 calldata->arg.share_access =
2835 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2836 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002837
Trond Myklebust516a6af2005-10-27 22:12:41 -04002838 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002839 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002840 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002841 &calldata->arg.seq_args,
2842 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002843 task) != 0)
2844 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002845 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002846 return;
2847out_no_action:
2848 task->tk_action = NULL;
2849out_wait:
2850 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002853static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002854 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002855 .rpc_call_done = nfs4_close_done,
2856 .rpc_release = nfs4_free_closedata,
2857};
2858
Peng Taofe08c542014-07-03 13:05:01 +08002859static bool nfs4_roc(struct inode *inode)
2860{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002861 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002862 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002863 return pnfs_roc(inode);
2864}
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866/*
2867 * It is possible for data to be read/written from a mem-mapped file
2868 * after the sys_close call (which hits the vfs layer as a flush).
2869 * This means that we can't safely call nfsv4 close on a file until
2870 * the inode is cleared. This in turn means that we are not good
2871 * NFSv4 citizens - we do not indicate to the server to update the file's
2872 * share state even when we are done with one of the three share
2873 * stateid's in the inode.
2874 *
2875 * NOTE: Caller must be holding the sp->so_owner semaphore!
2876 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002877int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002879 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002880 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002882 struct nfs4_state_owner *sp = state->owner;
2883 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002884 struct rpc_message msg = {
2885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2886 .rpc_cred = state->owner->so_cred,
2887 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002888 struct rpc_task_setup task_setup_data = {
2889 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002890 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002891 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002892 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002893 .flags = RPC_TASK_ASYNC,
2894 };
Trond Myklebust95121352005-10-18 14:20:12 -07002895 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002897 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2898 &task_setup_data.rpc_client, &msg);
2899
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002900 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002902 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002903 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002904 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002906 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002908 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2909 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002910 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002911 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002912 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002913 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002914 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002915 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002916 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002917 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002918 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002919
Trond Myklebust1174dd12010-12-21 10:52:24 -05002920 msg.rpc_argp = &calldata->arg;
2921 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002922 task_setup_data.callback_data = calldata;
2923 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002924 if (IS_ERR(task))
2925 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002926 status = 0;
2927 if (wait)
2928 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002929 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002930 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002931out_free_calldata:
2932 kfree(calldata);
2933out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002934 nfs4_put_open_state(state);
2935 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002936 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
Trond Myklebust2b484292010-09-17 10:56:51 -04002939static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002940nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2941 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002944 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2945
2946 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Trond Myklebust565277f2007-10-15 18:17:53 -04002948 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002949 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002950
2951 nfs4_label_release_security(label);
2952
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002953 if (IS_ERR(state))
2954 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002955 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}
2957
Trond Myklebust1185a552009-12-03 15:54:02 -05002958static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002959{
2960 if (ctx->state == NULL)
2961 return;
2962 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002963 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002964 else
Al Viro643168c2011-06-22 18:20:23 -04002965 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Trond Myklebustb944dba2013-11-04 15:20:20 -05002968#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2969#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002970#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2973{
Kinglong Mee8c612822015-08-26 21:12:58 +08002974 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04002975 struct nfs4_server_caps_arg args = {
2976 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08002977 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04002978 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 struct nfs4_server_caps_res res = {};
2980 struct rpc_message msg = {
2981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002982 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 .rpc_resp = &res,
2984 };
2985 int status;
2986
Kinglong Mee8c612822015-08-26 21:12:58 +08002987 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
2988 FATTR4_WORD0_FH_EXPIRE_TYPE |
2989 FATTR4_WORD0_LINK_SUPPORT |
2990 FATTR4_WORD0_SYMLINK_SUPPORT |
2991 FATTR4_WORD0_ACLSUPPORT;
2992 if (minorversion)
2993 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
2994
Bryan Schumaker7c513052011-03-24 17:12:24 +00002995 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002997 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08002998 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002999 case 0:
3000 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3001 res.attr_bitmask[2] = 0;
3002 break;
3003 case 1:
3004 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3005 break;
3006 case 2:
3007 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003010 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3011 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3012 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3013 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003014 NFS_CAP_CTIME|NFS_CAP_MTIME|
3015 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003016 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3017 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 server->caps |= NFS_CAP_ACLS;
3019 if (res.has_links != 0)
3020 server->caps |= NFS_CAP_HARDLINKS;
3021 if (res.has_symlinks != 0)
3022 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003023 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3024 server->caps |= NFS_CAP_FILEID;
3025 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3026 server->caps |= NFS_CAP_MODE;
3027 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3028 server->caps |= NFS_CAP_NLINK;
3029 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3030 server->caps |= NFS_CAP_OWNER;
3031 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3032 server->caps |= NFS_CAP_OWNER_GROUP;
3033 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3034 server->caps |= NFS_CAP_ATIME;
3035 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3036 server->caps |= NFS_CAP_CTIME;
3037 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3038 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003039#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3040 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3041 server->caps |= NFS_CAP_SECURITY_LABEL;
3042#endif
3043 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3044 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003045 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003046
Trond Myklebusta65318b2009-03-11 14:10:28 -04003047 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3048 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3049 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003050 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003051 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3052 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003054 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return status;
3058}
3059
Trond Myklebust55a97592006-06-09 09:34:19 -04003060int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 struct nfs4_exception exception = { };
3063 int err;
3064 do {
3065 err = nfs4_handle_exception(server,
3066 _nfs4_server_capabilities(server, fhandle),
3067 &exception);
3068 } while (exception.retry);
3069 return err;
3070}
3071
3072static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3073 struct nfs_fsinfo *info)
3074{
David Quigleyaa9c2662013-05-22 12:50:44 -04003075 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003077 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 };
3079 struct nfs4_lookup_res res = {
3080 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003081 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 .fh = fhandle,
3083 };
3084 struct rpc_message msg = {
3085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3086 .rpc_argp = &args,
3087 .rpc_resp = &res,
3088 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003089
David Quigleyaa9c2662013-05-22 12:50:44 -04003090 bitmask[0] = nfs4_fattr_bitmap[0];
3091 bitmask[1] = nfs4_fattr_bitmap[1];
3092 /*
3093 * Process the label in the upcoming getfattr
3094 */
3095 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3096
Trond Myklebust0e574af2005-10-27 22:12:38 -04003097 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003098 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
3101static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3102 struct nfs_fsinfo *info)
3103{
3104 struct nfs4_exception exception = { };
3105 int err;
3106 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003107 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003108 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003109 switch (err) {
3110 case 0:
3111 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003112 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003113 default:
3114 err = nfs4_handle_exception(server, err, &exception);
3115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003117out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return err;
3119}
3120
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003121static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3122 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3123{
Trond Myklebustc2190662013-08-26 19:23:04 -04003124 struct rpc_auth_create_args auth_args = {
3125 .pseudoflavor = flavor,
3126 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003127 struct rpc_auth *auth;
3128 int ret;
3129
Trond Myklebustc2190662013-08-26 19:23:04 -04003130 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003131 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003132 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003133 goto out;
3134 }
3135 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003136out:
3137 return ret;
3138}
3139
Chuck Lever9a744ba2013-03-16 15:56:02 -04003140/*
3141 * Retry pseudoroot lookup with various security flavors. We do this when:
3142 *
3143 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3144 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3145 *
3146 * Returns zero on success, or a negative NFS4ERR value, or a
3147 * negative errno value.
3148 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003149static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003150 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003152 /* Per 3530bis 15.33.5 */
3153 static const rpc_authflavor_t flav_array[] = {
3154 RPC_AUTH_GSS_KRB5P,
3155 RPC_AUTH_GSS_KRB5I,
3156 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003157 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003158 RPC_AUTH_NULL,
3159 };
3160 int status = -EPERM;
3161 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003163 if (server->auth_info.flavor_len > 0) {
3164 /* try each flavor specified by user */
3165 for (i = 0; i < server->auth_info.flavor_len; i++) {
3166 status = nfs4_lookup_root_sec(server, fhandle, info,
3167 server->auth_info.flavors[i]);
3168 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3169 continue;
3170 break;
3171 }
3172 } else {
3173 /* no flavors specified by user, try default list */
3174 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3175 status = nfs4_lookup_root_sec(server, fhandle, info,
3176 flav_array[i]);
3177 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3178 continue;
3179 break;
3180 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003181 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003182
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003183 /*
3184 * -EACCESS could mean that the user doesn't have correct permissions
3185 * to access the mount. It could also mean that we tried to mount
3186 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3187 * existing mount programs don't handle -EACCES very well so it should
3188 * be mapped to -EPERM instead.
3189 */
3190 if (status == -EACCES)
3191 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003192 return status;
3193}
3194
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003195static int nfs4_do_find_root_sec(struct nfs_server *server,
3196 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3197{
3198 int mv = server->nfs_client->cl_minorversion;
3199 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3200}
3201
3202/**
3203 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3204 * @server: initialized nfs_server handle
3205 * @fhandle: we fill in the pseudo-fs root file handle
3206 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003207 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003208 *
3209 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003210 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003211int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003212 struct nfs_fsinfo *info,
3213 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003214{
Andre Przywarac7757072015-04-23 17:17:40 +01003215 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003216
Andre Przywarac7757072015-04-23 17:17:40 +01003217 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003218 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003219
3220 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003221 status = nfs4_do_find_root_sec(server, fhandle, info);
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 if (status == 0)
3224 status = nfs4_server_capabilities(server, fhandle);
3225 if (status == 0)
3226 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003227
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003228 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Bryan Schumakerbae36242012-05-10 15:07:31 -04003231static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3232 struct nfs_fsinfo *info)
3233{
3234 int error;
3235 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003236 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003237
3238 error = nfs4_server_capabilities(server, mntfh);
3239 if (error < 0) {
3240 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3241 return error;
3242 }
3243
David Quigley14c43f72013-05-22 12:50:43 -04003244 label = nfs4_label_alloc(server, GFP_KERNEL);
3245 if (IS_ERR(label))
3246 return PTR_ERR(label);
3247
David Quigley1775fd32013-05-22 12:50:42 -04003248 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003249 if (error < 0) {
3250 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003251 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003252 }
3253
3254 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3255 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3256 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3257
David Quigley14c43f72013-05-22 12:50:43 -04003258err_free_label:
3259 nfs4_label_free(label);
3260
Bryan Schumakerbae36242012-05-10 15:07:31 -04003261 return error;
3262}
3263
Manoj Naik6b97fd32006-06-09 09:34:29 -04003264/*
3265 * Get locations and (maybe) other attributes of a referral.
3266 * Note that we'll actually follow the referral later when
3267 * we detect fsid mismatch in inode revalidation
3268 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003269static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3270 const struct qstr *name, struct nfs_fattr *fattr,
3271 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003272{
3273 int status = -ENOMEM;
3274 struct page *page = NULL;
3275 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003276
3277 page = alloc_page(GFP_KERNEL);
3278 if (page == NULL)
3279 goto out;
3280 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3281 if (locations == NULL)
3282 goto out;
3283
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003284 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003285 if (status != 0)
3286 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003287
3288 /*
3289 * If the fsid didn't change, this is a migration event, not a
3290 * referral. Cause us to drop into the exception handler, which
3291 * will kick off migration recovery.
3292 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003293 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003294 dprintk("%s: server did not return a different fsid for"
3295 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003296 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003297 goto out;
3298 }
Andy Adamson533eb462011-06-13 18:25:56 -04003299 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3300 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003301
Andy Adamson533eb462011-06-13 18:25:56 -04003302 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003303 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003304 memset(fhandle, 0, sizeof(struct nfs_fh));
3305out:
3306 if (page)
3307 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003308 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003309 return status;
3310}
3311
David Quigley1775fd32013-05-22 12:50:42 -04003312static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3313 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
3315 struct nfs4_getattr_arg args = {
3316 .fh = fhandle,
3317 .bitmask = server->attr_bitmask,
3318 };
3319 struct nfs4_getattr_res res = {
3320 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003321 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 .server = server,
3323 };
3324 struct rpc_message msg = {
3325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3326 .rpc_argp = &args,
3327 .rpc_resp = &res,
3328 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003329
3330 args.bitmask = nfs4_bitmask(server, label);
3331
Trond Myklebust0e574af2005-10-27 22:12:38 -04003332 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003333 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
David Quigley1775fd32013-05-22 12:50:42 -04003336static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3337 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 struct nfs4_exception exception = { };
3340 int err;
3341 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003342 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3343 trace_nfs4_getattr(server, fhandle, fattr, err);
3344 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 &exception);
3346 } while (exception.retry);
3347 return err;
3348}
3349
3350/*
3351 * The file is not closed if it is opened due to the a request to change
3352 * the size of the file. The open call will not be needed once the
3353 * VFS layer lookup-intents are implemented.
3354 *
3355 * Close is called when the inode is destroyed.
3356 * If we haven't opened the file for O_WRONLY, we
3357 * need to in the size_change case to obtain a stateid.
3358 *
3359 * Got race?
3360 * Because OPEN is always done by name in nfsv4, it is
3361 * possible that we opened a different file by the same
3362 * name. We can recognize this race condition, but we
3363 * can't do anything about it besides returning an error.
3364 *
3365 * This will be fixed with VFS changes (lookup-intent).
3366 */
3367static int
3368nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3369 struct iattr *sattr)
3370{
David Howells2b0143b2015-03-17 22:25:59 +00003371 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003372 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003373 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003374 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 int status;
3376
Peng Tao88ac8152014-09-12 11:04:10 +08003377 if (pnfs_ld_layoutret_on_setattr(inode) &&
3378 sattr->ia_valid & ATTR_SIZE &&
3379 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003380 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003381
Trond Myklebust0e574af2005-10-27 22:12:38 -04003382 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Andy Adamson26699402012-05-30 16:12:24 -04003384 /* Deal with open(O_TRUNC) */
3385 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003386 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003387
3388 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003389 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003390 return 0;
3391
Trond Myklebustd5308382005-11-04 15:33:38 -05003392 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003393 if (sattr->ia_valid & ATTR_FILE) {
3394 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003395
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003396 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003397 if (ctx) {
3398 cred = ctx->cred;
3399 state = ctx->state;
3400 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003401 }
3402
David Quigley14c43f72013-05-22 12:50:43 -04003403 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3404 if (IS_ERR(label))
3405 return PTR_ERR(label);
3406
3407 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003408 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003409 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003410 nfs_setsecurity(inode, fattr, label);
3411 }
David Quigley14c43f72013-05-22 12:50:43 -04003412 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return status;
3414}
3415
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003416static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3417 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003418 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003419{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003420 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003421 int status;
3422 struct nfs4_lookup_arg args = {
3423 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003424 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003425 .name = name,
3426 };
3427 struct nfs4_lookup_res res = {
3428 .server = server,
3429 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003430 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003431 .fh = fhandle,
3432 };
3433 struct rpc_message msg = {
3434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3435 .rpc_argp = &args,
3436 .rpc_resp = &res,
3437 };
3438
David Quigleyaa9c2662013-05-22 12:50:44 -04003439 args.bitmask = nfs4_bitmask(server, label);
3440
David Howells2b3de442006-08-22 20:06:09 -04003441 nfs_fattr_init(fattr);
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003444 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 dprintk("NFS reply lookup: %d\n", status);
3446 return status;
3447}
3448
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003449static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003450{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003451 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003452 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003453 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3454 fattr->nlink = 2;
3455}
3456
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003457static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3458 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003459 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003460{
3461 struct nfs4_exception exception = { };
3462 struct rpc_clnt *client = *clnt;
3463 int err;
3464 do {
David Quigley1775fd32013-05-22 12:50:42 -04003465 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003466 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003467 switch (err) {
3468 case -NFS4ERR_BADNAME:
3469 err = -ENOENT;
3470 goto out;
3471 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003472 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003473 if (err == -NFS4ERR_MOVED)
3474 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003475 goto out;
3476 case -NFS4ERR_WRONGSEC:
3477 err = -EPERM;
3478 if (client != *clnt)
3479 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003480 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003481 if (IS_ERR(client))
3482 return PTR_ERR(client);
3483
3484 exception.retry = 1;
3485 break;
3486 default:
3487 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3488 }
3489 } while (exception.retry);
3490
3491out:
3492 if (err == 0)
3493 *clnt = client;
3494 else if (client != *clnt)
3495 rpc_shutdown_client(client);
3496
3497 return err;
3498}
3499
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003500static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003501 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3502 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003504 int status;
3505 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003506
David Quigley1775fd32013-05-22 12:50:42 -04003507 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003508 if (client != NFS_CLIENT(dir)) {
3509 rpc_shutdown_client(client);
3510 nfs_fixup_secinfo_attributes(fattr);
3511 }
3512 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003515struct rpc_clnt *
3516nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3517 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3518{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003519 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003520 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003521
David Quigley1775fd32013-05-22 12:50:42 -04003522 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003523 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003524 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003525 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003526}
3527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3529{
Trond Myklebust76b32992007-08-10 17:45:11 -04003530 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 struct nfs4_accessargs args = {
3532 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003533 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003535 struct nfs4_accessres res = {
3536 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003537 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 struct rpc_message msg = {
3539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3540 .rpc_argp = &args,
3541 .rpc_resp = &res,
3542 .rpc_cred = entry->cred,
3543 };
3544 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003545 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547 /*
3548 * Determine which access bits we want to ask for...
3549 */
3550 if (mode & MAY_READ)
3551 args.access |= NFS4_ACCESS_READ;
3552 if (S_ISDIR(inode->i_mode)) {
3553 if (mode & MAY_WRITE)
3554 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3555 if (mode & MAY_EXEC)
3556 args.access |= NFS4_ACCESS_LOOKUP;
3557 } else {
3558 if (mode & MAY_WRITE)
3559 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3560 if (mode & MAY_EXEC)
3561 args.access |= NFS4_ACCESS_EXECUTE;
3562 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003563
3564 res.fattr = nfs_alloc_fattr();
3565 if (res.fattr == NULL)
3566 return -ENOMEM;
3567
Bryan Schumaker7c513052011-03-24 17:12:24 +00003568 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003570 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003571 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003573 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return status;
3575}
3576
3577static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3578{
3579 struct nfs4_exception exception = { };
3580 int err;
3581 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003582 err = _nfs4_proc_access(inode, entry);
3583 trace_nfs4_access(inode, err);
3584 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 &exception);
3586 } while (exception.retry);
3587 return err;
3588}
3589
3590/*
3591 * TODO: For the time being, we don't try to get any attributes
3592 * along with any of the zero-copy operations READ, READDIR,
3593 * READLINK, WRITE.
3594 *
3595 * In the case of the first three, we want to put the GETATTR
3596 * after the read-type operation -- this is because it is hard
3597 * to predict the length of a GETATTR response in v4, and thus
3598 * align the READ data correctly. This means that the GETATTR
3599 * may end up partially falling into the page cache, and we should
3600 * shift it into the 'tail' of the xdr_buf before processing.
3601 * To do this efficiently, we need to know the total length
3602 * of data received, which doesn't seem to be available outside
3603 * of the RPC layer.
3604 *
3605 * In the case of WRITE, we also want to put the GETATTR after
3606 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003607 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 *
3609 * Both of these changes to the XDR layer would in fact be quite
3610 * minor, but I decided to leave them for a subsequent patch.
3611 */
3612static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3613 unsigned int pgbase, unsigned int pglen)
3614{
3615 struct nfs4_readlink args = {
3616 .fh = NFS_FH(inode),
3617 .pgbase = pgbase,
3618 .pglen = pglen,
3619 .pages = &page,
3620 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003621 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 struct rpc_message msg = {
3623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3624 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003625 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 };
3627
Bryan Schumaker7c513052011-03-24 17:12:24 +00003628 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 -07003629}
3630
3631static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3632 unsigned int pgbase, unsigned int pglen)
3633{
3634 struct nfs4_exception exception = { };
3635 int err;
3636 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003637 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3638 trace_nfs4_readlink(inode, err);
3639 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 &exception);
3641 } while (exception.retry);
3642 return err;
3643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003646 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648static int
3649nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003650 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
David Quigleyaa9c2662013-05-22 12:50:44 -04003652 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003653 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 int status = 0;
3656
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003657 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3658 if (IS_ERR(ctx))
3659 return PTR_ERR(ctx);
3660
David Quigleyaa9c2662013-05-22 12:50:44 -04003661 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3662
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003663 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003664 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 if (IS_ERR(state)) {
3666 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003667 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003670 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003671 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 return status;
3673}
3674
3675static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3676{
Trond Myklebust16e42952005-10-27 22:12:44 -04003677 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003678 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003680 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003682 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003683 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003684 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3687 .rpc_argp = &args,
3688 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003690 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003691
Bryan Schumaker7c513052011-03-24 17:12:24 +00003692 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003693 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003694 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return status;
3696}
3697
3698static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3699{
3700 struct nfs4_exception exception = { };
3701 int err;
3702 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003703 err = _nfs4_proc_remove(dir, name);
3704 trace_nfs4_remove(dir, name, err);
3705 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 &exception);
3707 } while (exception.retry);
3708 return err;
3709}
3710
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003711static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003713 struct nfs_server *server = NFS_SERVER(dir);
3714 struct nfs_removeargs *args = msg->rpc_argp;
3715 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003717 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003719 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003720
3721 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722}
3723
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003724static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3725{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003726 nfs4_setup_sequence(NFS_SERVER(data->dir),
3727 &data->args.seq_args,
3728 &data->res.seq_res,
3729 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730}
3731
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003732static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003734 struct nfs_unlinkdata *data = task->tk_calldata;
3735 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003736
Trond Myklebust14516c32010-07-31 14:29:06 -04003737 if (!nfs4_sequence_done(task, &res->seq_res))
3738 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003739 if (nfs4_async_handle_error(task, res->server, NULL,
3740 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003741 return 0;
3742 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003743 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744}
3745
Jeff Laytond3d41522010-09-17 17:31:57 -04003746static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3747{
3748 struct nfs_server *server = NFS_SERVER(dir);
3749 struct nfs_renameargs *arg = msg->rpc_argp;
3750 struct nfs_renameres *res = msg->rpc_resp;
3751
3752 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003753 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003754 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003755}
3756
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003757static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3758{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003759 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3760 &data->args.seq_args,
3761 &data->res.seq_res,
3762 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003763}
3764
3765static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3766 struct inode *new_dir)
3767{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003768 struct nfs_renamedata *data = task->tk_calldata;
3769 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003770
3771 if (!nfs4_sequence_done(task, &res->seq_res))
3772 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003773 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003774 return 0;
3775
3776 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003777 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003778 return 1;
3779}
3780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3782{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003783 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 struct nfs4_link_arg arg = {
3785 .fh = NFS_FH(inode),
3786 .dir_fh = NFS_FH(dir),
3787 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003788 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003790 struct nfs4_link_res res = {
3791 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003792 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003793 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 struct rpc_message msg = {
3795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3796 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003797 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003799 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
Trond Myklebust136f2622010-04-16 16:22:49 -04003801 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003802 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003803 goto out;
3804
David Quigley14c43f72013-05-22 12:50:43 -04003805 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3806 if (IS_ERR(res.label)) {
3807 status = PTR_ERR(res.label);
3808 goto out;
3809 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003810 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003811
Bryan Schumaker7c513052011-03-24 17:12:24 +00003812 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003813 if (!status) {
3814 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003815 status = nfs_post_op_update_inode(inode, res.fattr);
3816 if (!status)
3817 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003818 }
David Quigley14c43f72013-05-22 12:50:43 -04003819
3820
3821 nfs4_label_free(res.label);
3822
Trond Myklebust136f2622010-04-16 16:22:49 -04003823out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003824 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 return status;
3826}
3827
3828static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3829{
3830 struct nfs4_exception exception = { };
3831 int err;
3832 do {
3833 err = nfs4_handle_exception(NFS_SERVER(inode),
3834 _nfs4_proc_link(inode, dir, name),
3835 &exception);
3836 } while (exception.retry);
3837 return err;
3838}
3839
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003840struct nfs4_createdata {
3841 struct rpc_message msg;
3842 struct nfs4_create_arg arg;
3843 struct nfs4_create_res res;
3844 struct nfs_fh fh;
3845 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003846 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003847};
3848
3849static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3850 struct qstr *name, struct iattr *sattr, u32 ftype)
3851{
3852 struct nfs4_createdata *data;
3853
3854 data = kzalloc(sizeof(*data), GFP_KERNEL);
3855 if (data != NULL) {
3856 struct nfs_server *server = NFS_SERVER(dir);
3857
David Quigley14c43f72013-05-22 12:50:43 -04003858 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3859 if (IS_ERR(data->label))
3860 goto out_free;
3861
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003862 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3863 data->msg.rpc_argp = &data->arg;
3864 data->msg.rpc_resp = &data->res;
3865 data->arg.dir_fh = NFS_FH(dir);
3866 data->arg.server = server;
3867 data->arg.name = name;
3868 data->arg.attrs = sattr;
3869 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003870 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003871 data->res.server = server;
3872 data->res.fh = &data->fh;
3873 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003874 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003875 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003876 }
3877 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003878out_free:
3879 kfree(data);
3880 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003881}
3882
3883static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3884{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003885 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003886 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003887 if (status == 0) {
3888 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003889 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003890 }
3891 return status;
3892}
3893
3894static void nfs4_free_createdata(struct nfs4_createdata *data)
3895{
David Quigley14c43f72013-05-22 12:50:43 -04003896 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003897 kfree(data);
3898}
3899
Chuck Lever4f390c12006-08-22 20:06:22 -04003900static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003901 struct page *page, unsigned int len, struct iattr *sattr,
3902 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003904 struct nfs4_createdata *data;
3905 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Chuck Lever94a6d752006-08-22 20:06:23 -04003907 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003908 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003909
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003910 status = -ENOMEM;
3911 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3912 if (data == NULL)
3913 goto out;
3914
3915 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3916 data->arg.u.symlink.pages = &page;
3917 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003918 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003920 status = nfs4_do_create(dir, dentry, data);
3921
3922 nfs4_free_createdata(data);
3923out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 return status;
3925}
3926
Chuck Lever4f390c12006-08-22 20:06:22 -04003927static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003928 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
3930 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003931 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003933
3934 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003937 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3938 trace_nfs4_symlink(dir, &dentry->d_name, err);
3939 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 &exception);
3941 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003942
3943 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 return err;
3945}
3946
3947static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003948 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003950 struct nfs4_createdata *data;
3951 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003953 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3954 if (data == NULL)
3955 goto out;
3956
David Quigley1775fd32013-05-22 12:50:42 -04003957 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003958 status = nfs4_do_create(dir, dentry, data);
3959
3960 nfs4_free_createdata(data);
3961out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 return status;
3963}
3964
3965static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3966 struct iattr *sattr)
3967{
3968 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003969 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003971
David Quigleyaa9c2662013-05-22 12:50:44 -04003972 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3973
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003974 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003976 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3977 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3978 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 &exception);
3980 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003981 nfs4_label_release_security(label);
3982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return err;
3984}
3985
3986static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003987 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
David Howells2b0143b2015-03-17 22:25:59 +00003989 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 struct nfs4_readdir_arg args = {
3991 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003992 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 .pgbase = 0,
3994 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00003995 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003996 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 };
3998 struct nfs4_readdir_res res;
3999 struct rpc_message msg = {
4000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4001 .rpc_argp = &args,
4002 .rpc_resp = &res,
4003 .rpc_cred = cred,
4004 };
4005 int status;
4006
Al Viro6de14722013-09-16 10:53:17 -04004007 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4008 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004009 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004010 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004012 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 -05004013 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004014 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004015 status += args.pgbase;
4016 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004017
4018 nfs_invalidate_atime(dir);
4019
Harvey Harrison3110ff82008-05-02 13:42:44 -07004020 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 return status;
4022}
4023
4024static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004025 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
4027 struct nfs4_exception exception = { };
4028 int err;
4029 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004030 err = _nfs4_proc_readdir(dentry, cred, cookie,
4031 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004032 trace_nfs4_readdir(d_inode(dentry), err);
4033 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 &exception);
4035 } while (exception.retry);
4036 return err;
4037}
4038
4039static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004040 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004042 struct nfs4_createdata *data;
4043 int mode = sattr->ia_mode;
4044 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004046 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4047 if (data == NULL)
4048 goto out;
4049
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004051 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004053 data->arg.ftype = NF4BLK;
4054 data->arg.u.device.specdata1 = MAJOR(rdev);
4055 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 }
4057 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004058 data->arg.ftype = NF4CHR;
4059 data->arg.u.device.specdata1 = MAJOR(rdev);
4060 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004061 } else if (!S_ISSOCK(mode)) {
4062 status = -EINVAL;
4063 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 }
David Quigley1775fd32013-05-22 12:50:42 -04004065
David Quigleyaa9c2662013-05-22 12:50:44 -04004066 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004067 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004068out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004069 nfs4_free_createdata(data);
4070out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 return status;
4072}
4073
4074static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4075 struct iattr *sattr, dev_t rdev)
4076{
4077 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004078 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004080
David Quigleyaa9c2662013-05-22 12:50:44 -04004081 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4082
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004083 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004085 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4086 trace_nfs4_mknod(dir, &dentry->d_name, err);
4087 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 &exception);
4089 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004090
4091 nfs4_label_release_security(label);
4092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 return err;
4094}
4095
4096static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4097 struct nfs_fsstat *fsstat)
4098{
4099 struct nfs4_statfs_arg args = {
4100 .fh = fhandle,
4101 .bitmask = server->attr_bitmask,
4102 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004103 struct nfs4_statfs_res res = {
4104 .fsstat = fsstat,
4105 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 struct rpc_message msg = {
4107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4108 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004109 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 };
4111
Trond Myklebust0e574af2005-10-27 22:12:38 -04004112 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004113 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114}
4115
4116static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4117{
4118 struct nfs4_exception exception = { };
4119 int err;
4120 do {
4121 err = nfs4_handle_exception(server,
4122 _nfs4_proc_statfs(server, fhandle, fsstat),
4123 &exception);
4124 } while (exception.retry);
4125 return err;
4126}
4127
4128static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4129 struct nfs_fsinfo *fsinfo)
4130{
4131 struct nfs4_fsinfo_arg args = {
4132 .fh = fhandle,
4133 .bitmask = server->attr_bitmask,
4134 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004135 struct nfs4_fsinfo_res res = {
4136 .fsinfo = fsinfo,
4137 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 struct rpc_message msg = {
4139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4140 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004141 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 };
4143
Bryan Schumaker7c513052011-03-24 17:12:24 +00004144 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145}
4146
4147static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4148{
4149 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004150 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 int err;
4152
4153 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004154 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004155 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004156 if (err == 0) {
4157 struct nfs_client *clp = server->nfs_client;
4158
4159 spin_lock(&clp->cl_lock);
4160 clp->cl_lease_time = fsinfo->lease_time * HZ;
4161 clp->cl_last_renewal = now;
4162 spin_unlock(&clp->cl_lock);
4163 break;
4164 }
4165 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 } while (exception.retry);
4167 return err;
4168}
4169
4170static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4171{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004172 int error;
4173
Trond Myklebust0e574af2005-10-27 22:12:38 -04004174 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004175 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004176 if (error == 0) {
4177 /* block layout checks this! */
4178 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004179 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004180 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004181
4182 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183}
4184
4185static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4186 struct nfs_pathconf *pathconf)
4187{
4188 struct nfs4_pathconf_arg args = {
4189 .fh = fhandle,
4190 .bitmask = server->attr_bitmask,
4191 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004192 struct nfs4_pathconf_res res = {
4193 .pathconf = pathconf,
4194 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 struct rpc_message msg = {
4196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4197 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004198 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 };
4200
4201 /* None of the pathconf attributes are mandatory to implement */
4202 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4203 memset(pathconf, 0, sizeof(*pathconf));
4204 return 0;
4205 }
4206
Trond Myklebust0e574af2005-10-27 22:12:38 -04004207 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004208 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209}
4210
4211static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4212 struct nfs_pathconf *pathconf)
4213{
4214 struct nfs4_exception exception = { };
4215 int err;
4216
4217 do {
4218 err = nfs4_handle_exception(server,
4219 _nfs4_proc_pathconf(server, fhandle, pathconf),
4220 &exception);
4221 } while (exception.retry);
4222 return err;
4223}
4224
Trond Myklebust5521abf2013-03-16 20:54:34 -04004225int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004226 const struct nfs_open_context *ctx,
4227 const struct nfs_lock_context *l_ctx,
4228 fmode_t fmode)
4229{
4230 const struct nfs_lockowner *lockowner = NULL;
4231
4232 if (l_ctx != NULL)
4233 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004234 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004235}
4236EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4237
Trond Myklebust5521abf2013-03-16 20:54:34 -04004238static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4239 const struct nfs_open_context *ctx,
4240 const struct nfs_lock_context *l_ctx,
4241 fmode_t fmode)
4242{
4243 nfs4_stateid current_stateid;
4244
Trond Myklebuste1253be2014-03-05 08:44:23 -05004245 /* If the current stateid represents a lost lock, then exit */
4246 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4247 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004248 return nfs4_stateid_match(stateid, &current_stateid);
4249}
4250
4251static bool nfs4_error_stateid_expired(int err)
4252{
4253 switch (err) {
4254 case -NFS4ERR_DELEG_REVOKED:
4255 case -NFS4ERR_ADMIN_REVOKED:
4256 case -NFS4ERR_BAD_STATEID:
4257 case -NFS4ERR_STALE_STATEID:
4258 case -NFS4ERR_OLD_STATEID:
4259 case -NFS4ERR_OPENMODE:
4260 case -NFS4ERR_EXPIRED:
4261 return true;
4262 }
4263 return false;
4264}
4265
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004266void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004267{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004268 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004269}
4270
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004271static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004273 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004275 trace_nfs4_read(hdr, task->tk_status);
4276 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004277 hdr->args.context->state,
4278 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004279 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004280 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004282
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004283 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004285 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287}
4288
Trond Myklebust5521abf2013-03-16 20:54:34 -04004289static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004290 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004291{
4292
4293 if (!nfs4_error_stateid_expired(task->tk_status) ||
4294 nfs4_stateid_is_current(&args->stateid,
4295 args->context,
4296 args->lock_context,
4297 FMODE_READ))
4298 return false;
4299 rpc_restart_call_prepare(task);
4300 return true;
4301}
4302
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004303static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004304{
4305
4306 dprintk("--> %s\n", __func__);
4307
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004308 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004309 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004310 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004311 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004312 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4313 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004314}
4315
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004316static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4317 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004319 hdr->timestamp = jiffies;
4320 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004321 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004322 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004325static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4326 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004327{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004328 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4329 &hdr->args.seq_args,
4330 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004331 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004332 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004333 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4334 hdr->args.lock_context,
4335 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004336 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004337 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004338 return -EIO;
4339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340}
4341
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004342static int nfs4_write_done_cb(struct rpc_task *task,
4343 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004345 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004346
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004347 trace_nfs4_write(hdr, task->tk_status);
4348 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004349 hdr->args.context->state,
4350 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004351 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004352 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004354 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004355 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004356 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004357 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359}
4360
Trond Myklebust5521abf2013-03-16 20:54:34 -04004361static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004362 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004363{
4364
4365 if (!nfs4_error_stateid_expired(task->tk_status) ||
4366 nfs4_stateid_is_current(&args->stateid,
4367 args->context,
4368 args->lock_context,
4369 FMODE_WRITE))
4370 return false;
4371 rpc_restart_call_prepare(task);
4372 return true;
4373}
4374
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004375static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004376{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004377 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004378 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004379 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004380 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004381 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4382 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004383}
4384
Trond Myklebust5a37f852012-04-28 14:55:16 -04004385static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004386bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004387{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004388 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004389 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004390 return false;
4391 /* Otherwise, request attributes if and only if we don't hold
4392 * a delegation
4393 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004394 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004395}
Fred Isamana69aef12011-03-03 15:13:47 +00004396
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004397static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4398 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004400 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004401
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004402 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4403 hdr->args.bitmask = NULL;
4404 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004405 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004406 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004407
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004408 if (!hdr->pgio_done_cb)
4409 hdr->pgio_done_cb = nfs4_write_done_cb;
4410 hdr->res.server = server;
4411 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004413 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004414 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415}
4416
Fred Isaman0b7c0152012-04-20 14:47:39 -04004417static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4418{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004419 nfs4_setup_sequence(NFS_SERVER(data->inode),
4420 &data->args.seq_args,
4421 &data->res.seq_res,
4422 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423}
4424
Fred Isaman0b7c0152012-04-20 14:47:39 -04004425static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004428
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004429 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004430 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4431 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004432 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004433 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004435 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436}
4437
Fred Isaman0b7c0152012-04-20 14:47:39 -04004438static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004439{
4440 if (!nfs4_sequence_done(task, &data->res.seq_res))
4441 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004442 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004443}
4444
Fred Isaman0b7c0152012-04-20 14:47:39 -04004445static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004447 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004448
Fred Isaman0b7c0152012-04-20 14:47:39 -04004449 if (data->commit_done_cb == NULL)
4450 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004451 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004452 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004453 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454}
4455
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004456struct nfs4_renewdata {
4457 struct nfs_client *client;
4458 unsigned long timestamp;
4459};
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461/*
4462 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4463 * standalone procedure for queueing an asynchronous RENEW.
4464 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004465static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004466{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004467 struct nfs4_renewdata *data = calldata;
4468 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004469
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004470 if (atomic_read(&clp->cl_count) > 1)
4471 nfs4_schedule_state_renewal(clp);
4472 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004473 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004474}
4475
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004476static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004478 struct nfs4_renewdata *data = calldata;
4479 struct nfs_client *clp = data->client;
4480 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004482 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004483 switch (task->tk_status) {
4484 case 0:
4485 break;
4486 case -NFS4ERR_LEASE_MOVED:
4487 nfs4_schedule_lease_moved_recovery(clp);
4488 break;
4489 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004490 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004491 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4492 return;
4493 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004494 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004495 return;
4496 }
4497 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004499 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500}
4501
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004502static const struct rpc_call_ops nfs4_renew_ops = {
4503 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004504 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004505};
4506
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004507static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508{
4509 struct rpc_message msg = {
4510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4511 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004512 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004514 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004516 if (renew_flags == 0)
4517 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004518 if (!atomic_inc_not_zero(&clp->cl_count))
4519 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004520 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004521 if (data == NULL)
4522 return -ENOMEM;
4523 data->client = clp;
4524 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004525 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004526 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527}
4528
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004529static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530{
4531 struct rpc_message msg = {
4532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4533 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004534 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 };
4536 unsigned long now = jiffies;
4537 int status;
4538
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004539 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 if (status < 0)
4541 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004542 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 return 0;
4544}
4545
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004546static inline int nfs4_server_supports_acls(struct nfs_server *server)
4547{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004548 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004549}
4550
Trond Myklebust21f498c2012-08-24 10:59:25 -04004551/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4552 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004553 * the stack.
4554 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004555#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004556
Neil Hormane9e3d722011-03-04 19:26:03 -05004557static int buf_to_pages_noslab(const void *buf, size_t buflen,
4558 struct page **pages, unsigned int *pgbase)
4559{
4560 struct page *newpage, **spages;
4561 int rc = 0;
4562 size_t len;
4563 spages = pages;
4564
4565 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004566 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004567 newpage = alloc_page(GFP_KERNEL);
4568
4569 if (newpage == NULL)
4570 goto unwind;
4571 memcpy(page_address(newpage), buf, len);
4572 buf += len;
4573 buflen -= len;
4574 *pages++ = newpage;
4575 rc++;
4576 } while (buflen != 0);
4577
4578 return rc;
4579
4580unwind:
4581 for(; rc > 0; rc--)
4582 __free_page(spages[rc-1]);
4583 return -ENOMEM;
4584}
4585
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004586struct nfs4_cached_acl {
4587 int cached;
4588 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004589 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004590};
4591
4592static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004593{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004594 struct nfs_inode *nfsi = NFS_I(inode);
4595
4596 spin_lock(&inode->i_lock);
4597 kfree(nfsi->nfs4_acl);
4598 nfsi->nfs4_acl = acl;
4599 spin_unlock(&inode->i_lock);
4600}
4601
4602static void nfs4_zap_acl_attr(struct inode *inode)
4603{
4604 nfs4_set_cached_acl(inode, NULL);
4605}
4606
4607static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4608{
4609 struct nfs_inode *nfsi = NFS_I(inode);
4610 struct nfs4_cached_acl *acl;
4611 int ret = -ENOENT;
4612
4613 spin_lock(&inode->i_lock);
4614 acl = nfsi->nfs4_acl;
4615 if (acl == NULL)
4616 goto out;
4617 if (buf == NULL) /* user is just asking for length */
4618 goto out_len;
4619 if (acl->cached == 0)
4620 goto out;
4621 ret = -ERANGE; /* see getxattr(2) man page */
4622 if (acl->len > buflen)
4623 goto out;
4624 memcpy(buf, acl->data, acl->len);
4625out_len:
4626 ret = acl->len;
4627out:
4628 spin_unlock(&inode->i_lock);
4629 return ret;
4630}
4631
Sachin Prabhu5794d212012-04-17 14:36:40 +01004632static 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 +00004633{
4634 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004635 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004636
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004637 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004638 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004639 if (acl == NULL)
4640 goto out;
4641 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004642 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004643 } else {
4644 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4645 if (acl == NULL)
4646 goto out;
4647 acl->cached = 0;
4648 }
4649 acl->len = acl_len;
4650out:
4651 nfs4_set_cached_acl(inode, acl);
4652}
4653
Andy Adamsonbf118a32011-12-07 11:55:27 -05004654/*
4655 * The getxattr API returns the required buffer length when called with a
4656 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4657 * the required buf. On a NULL buf, we send a page of data to the server
4658 * guessing that the ACL request can be serviced by a page. If so, we cache
4659 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4660 * the cache. If not so, we throw away the page, and cache the required
4661 * length. The next getxattr call will then produce another round trip to
4662 * the server, this time with the input buf of the required size.
4663 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004664static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004665{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004666 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004667 struct nfs_getaclargs args = {
4668 .fh = NFS_FH(inode),
4669 .acl_pages = pages,
4670 .acl_len = buflen,
4671 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004672 struct nfs_getaclres res = {
4673 .acl_len = buflen,
4674 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004675 struct rpc_message msg = {
4676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4677 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004678 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004679 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004680 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4681 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004682
Andy Adamsonbf118a32011-12-07 11:55:27 -05004683 /* As long as we're doing a round trip to the server anyway,
4684 * let's be prepared for a page of acl data. */
4685 if (npages == 0)
4686 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004687 if (npages > ARRAY_SIZE(pages))
4688 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004689
Andy Adamsonbf118a32011-12-07 11:55:27 -05004690 for (i = 0; i < npages; i++) {
4691 pages[i] = alloc_page(GFP_KERNEL);
4692 if (!pages[i])
4693 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004694 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004695
4696 /* for decoding across pages */
4697 res.acl_scratch = alloc_page(GFP_KERNEL);
4698 if (!res.acl_scratch)
4699 goto out_free;
4700
Andy Adamsonbf118a32011-12-07 11:55:27 -05004701 args.acl_len = npages * PAGE_SIZE;
4702 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004703
Peng Taode040be2012-01-10 22:42:47 +08004704 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004705 __func__, buf, buflen, npages, args.acl_len);
4706 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4707 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004708 if (ret)
4709 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004710
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004711 /* Handle the case where the passed-in buffer is too short */
4712 if (res.acl_flags & NFS4_ACL_TRUNC) {
4713 /* Did the user only issue a request for the acl length? */
4714 if (buf == NULL)
4715 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004716 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004717 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004718 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004719 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004720 if (buf) {
4721 if (res.acl_len > buflen) {
4722 ret = -ERANGE;
4723 goto out_free;
4724 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004725 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004726 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004727out_ok:
4728 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004729out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004730 for (i = 0; i < npages; i++)
4731 if (pages[i])
4732 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004733 if (res.acl_scratch)
4734 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004735 return ret;
4736}
4737
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004738static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4739{
4740 struct nfs4_exception exception = { };
4741 ssize_t ret;
4742 do {
4743 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004744 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004745 if (ret >= 0)
4746 break;
4747 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4748 } while (exception.retry);
4749 return ret;
4750}
4751
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004752static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4753{
4754 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004755 int ret;
4756
4757 if (!nfs4_server_supports_acls(server))
4758 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004759 ret = nfs_revalidate_inode(server, inode);
4760 if (ret < 0)
4761 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004762 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4763 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004764 ret = nfs4_read_cached_acl(inode, buf, buflen);
4765 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004766 /* -ENOENT is returned if there is no ACL or if there is an ACL
4767 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004768 return ret;
4769 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004770}
4771
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004772static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004773{
4774 struct nfs_server *server = NFS_SERVER(inode);
4775 struct page *pages[NFS4ACL_MAXPAGES];
4776 struct nfs_setaclargs arg = {
4777 .fh = NFS_FH(inode),
4778 .acl_pages = pages,
4779 .acl_len = buflen,
4780 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004781 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004782 struct rpc_message msg = {
4783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4784 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004785 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004786 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004787 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004788 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004789
4790 if (!nfs4_server_supports_acls(server))
4791 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004792 if (npages > ARRAY_SIZE(pages))
4793 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004794 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4795 if (i < 0)
4796 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004797 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004798 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004799
4800 /*
4801 * Free each page after tx, so the only ref left is
4802 * held by the network stack
4803 */
4804 for (; i > 0; i--)
4805 put_page(pages[i-1]);
4806
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004807 /*
4808 * Acl update can result in inode attribute update.
4809 * so mark the attribute cache invalid.
4810 */
4811 spin_lock(&inode->i_lock);
4812 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4813 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004814 nfs_access_zap_cache(inode);
4815 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004816 return ret;
4817}
4818
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004819static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4820{
4821 struct nfs4_exception exception = { };
4822 int err;
4823 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004824 err = __nfs4_proc_set_acl(inode, buf, buflen);
4825 trace_nfs4_set_acl(inode, err);
4826 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004827 &exception);
4828 } while (exception.retry);
4829 return err;
4830}
4831
David Quigleyaa9c2662013-05-22 12:50:44 -04004832#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4833static int _nfs4_get_security_label(struct inode *inode, void *buf,
4834 size_t buflen)
4835{
4836 struct nfs_server *server = NFS_SERVER(inode);
4837 struct nfs_fattr fattr;
4838 struct nfs4_label label = {0, 0, buflen, buf};
4839
4840 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004841 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004842 .fh = NFS_FH(inode),
4843 .bitmask = bitmask,
4844 };
4845 struct nfs4_getattr_res res = {
4846 .fattr = &fattr,
4847 .label = &label,
4848 .server = server,
4849 };
4850 struct rpc_message msg = {
4851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004852 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004853 .rpc_resp = &res,
4854 };
4855 int ret;
4856
4857 nfs_fattr_init(&fattr);
4858
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004859 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004860 if (ret)
4861 return ret;
4862 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4863 return -ENOENT;
4864 if (buflen < label.len)
4865 return -ERANGE;
4866 return 0;
4867}
4868
4869static int nfs4_get_security_label(struct inode *inode, void *buf,
4870 size_t buflen)
4871{
4872 struct nfs4_exception exception = { };
4873 int err;
4874
4875 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4876 return -EOPNOTSUPP;
4877
4878 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004879 err = _nfs4_get_security_label(inode, buf, buflen);
4880 trace_nfs4_get_security_label(inode, err);
4881 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004882 &exception);
4883 } while (exception.retry);
4884 return err;
4885}
4886
4887static int _nfs4_do_set_security_label(struct inode *inode,
4888 struct nfs4_label *ilabel,
4889 struct nfs_fattr *fattr,
4890 struct nfs4_label *olabel)
4891{
4892
4893 struct iattr sattr = {0};
4894 struct nfs_server *server = NFS_SERVER(inode);
4895 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004896 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004897 .fh = NFS_FH(inode),
4898 .iap = &sattr,
4899 .server = server,
4900 .bitmask = bitmask,
4901 .label = ilabel,
4902 };
4903 struct nfs_setattrres res = {
4904 .fattr = fattr,
4905 .label = olabel,
4906 .server = server,
4907 };
4908 struct rpc_message msg = {
4909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004910 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004911 .rpc_resp = &res,
4912 };
4913 int status;
4914
Jeff Layton12207f62013-11-01 10:49:32 -04004915 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004916
Jeff Layton12207f62013-11-01 10:49:32 -04004917 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004918 if (status)
4919 dprintk("%s failed: %d\n", __func__, status);
4920
4921 return status;
4922}
4923
4924static int nfs4_do_set_security_label(struct inode *inode,
4925 struct nfs4_label *ilabel,
4926 struct nfs_fattr *fattr,
4927 struct nfs4_label *olabel)
4928{
4929 struct nfs4_exception exception = { };
4930 int err;
4931
4932 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004933 err = _nfs4_do_set_security_label(inode, ilabel,
4934 fattr, olabel);
4935 trace_nfs4_set_security_label(inode, err);
4936 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004937 &exception);
4938 } while (exception.retry);
4939 return err;
4940}
4941
4942static int
4943nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4944{
4945 struct nfs4_label ilabel, *olabel = NULL;
4946 struct nfs_fattr fattr;
4947 struct rpc_cred *cred;
David Howells2b0143b2015-03-17 22:25:59 +00004948 struct inode *inode = d_inode(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004949 int status;
4950
4951 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4952 return -EOPNOTSUPP;
4953
4954 nfs_fattr_init(&fattr);
4955
4956 ilabel.pi = 0;
4957 ilabel.lfs = 0;
4958 ilabel.label = (char *)buf;
4959 ilabel.len = buflen;
4960
4961 cred = rpc_lookup_cred();
4962 if (IS_ERR(cred))
4963 return PTR_ERR(cred);
4964
4965 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4966 if (IS_ERR(olabel)) {
4967 status = -PTR_ERR(olabel);
4968 goto out;
4969 }
4970
4971 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4972 if (status == 0)
4973 nfs_setsecurity(inode, &fattr, olabel);
4974
4975 nfs4_label_free(olabel);
4976out:
4977 put_rpccred(cred);
4978 return status;
4979}
4980#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4981
4982
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004984nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4985 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004987 struct nfs_client *clp = server->nfs_client;
4988
4989 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 return 0;
4991 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004992 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004993 case -NFS4ERR_ADMIN_REVOKED:
4994 case -NFS4ERR_BAD_STATEID:
4995 case -NFS4ERR_OPENMODE:
4996 if (state == NULL)
4997 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004998 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004999 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005000 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005001 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04005002 if (state != NULL) {
5003 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04005004 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04005005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005007 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005008 nfs4_schedule_lease_recovery(clp);
5009 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04005010 case -NFS4ERR_MOVED:
5011 if (nfs4_schedule_migration_recovery(server) < 0)
5012 goto recovery_failed;
5013 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04005014 case -NFS4ERR_LEASE_MOVED:
5015 nfs4_schedule_lease_moved_recovery(clp);
5016 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04005017#if defined(CONFIG_NFS_V4_1)
5018 case -NFS4ERR_BADSESSION:
5019 case -NFS4ERR_BADSLOT:
5020 case -NFS4ERR_BAD_HIGH_SLOT:
5021 case -NFS4ERR_DEADSESSION:
5022 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5023 case -NFS4ERR_SEQ_FALSE_RETRY:
5024 case -NFS4ERR_SEQ_MISORDERED:
5025 dprintk("%s ERROR %d, Reset session\n", __func__,
5026 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04005027 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05005028 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04005029#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005031 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10005032 rpc_delay(task, nfs4_update_delay(timeout));
5033 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05005034 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005036 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04005038 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 }
5040 task->tk_status = nfs4_map_errors(task->tk_status);
5041 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04005042recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04005043 task->tk_status = -EIO;
5044 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005045wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005046 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005047 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
5048 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04005049 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
5050 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04005051restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005052 task->tk_status = 0;
5053 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054}
5055
Chuck Leverf0920752012-05-21 22:45:41 -04005056static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5057 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005058{
5059 __be32 verf[2];
5060
Chuck Lever2c820d92012-05-21 22:45:33 -04005061 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5062 /* An impossible timestamp guarantees this value
5063 * will never match a generated boot time. */
5064 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005065 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005066 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005067 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005068 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5069 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005070 }
Chuck Levercd937102012-03-02 17:14:31 -05005071 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5072}
5073
Jeff Laytona3192682015-06-09 19:43:59 -04005074static int
5075nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005076{
Jeff Laytona3192682015-06-09 19:43:59 -04005077 int result;
5078 size_t len;
5079 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005080
Trond Myklebustceb3a162015-01-03 15:16:04 -05005081 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005082 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005083
Jeff Laytona3192682015-06-09 19:43:59 -04005084 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005085 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005086 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5087 1 +
5088 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5089 1;
5090 rcu_read_unlock();
5091
5092 if (len > NFS4_OPAQUE_LIMIT + 1)
5093 return -EINVAL;
5094
5095 /*
5096 * Since this string is allocated at mount time, and held until the
5097 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5098 * about a memory-reclaim deadlock.
5099 */
5100 str = kmalloc(len, GFP_KERNEL);
5101 if (!str)
5102 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005103
Chuck Levere984a552012-09-14 17:24:21 -04005104 rcu_read_lock();
Jeff Laytona3192682015-06-09 19:43:59 -04005105 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5106 clp->cl_ipaddr,
5107 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5108 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005109 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005110
Jeff Laytona3192682015-06-09 19:43:59 -04005111 clp->cl_owner_id = str;
5112 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005113}
5114
Jeff Layton873e3852015-06-09 19:44:00 -04005115static int
5116nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005117{
Jeff Layton873e3852015-06-09 19:44:00 -04005118 int result;
5119 size_t len;
5120 char *str;
5121
5122 len = 10 + 10 + 1 + 10 + 1 +
5123 strlen(nfs4_client_id_uniquifier) + 1 +
5124 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5125
5126 if (len > NFS4_OPAQUE_LIMIT + 1)
5127 return -EINVAL;
5128
5129 /*
5130 * Since this string is allocated at mount time, and held until the
5131 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5132 * about a memory-reclaim deadlock.
5133 */
5134 str = kmalloc(len, GFP_KERNEL);
5135 if (!str)
5136 return -ENOMEM;
5137
5138 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5139 clp->rpc_ops->version, clp->cl_minorversion,
5140 nfs4_client_id_uniquifier,
5141 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005142 clp->cl_owner_id = str;
5143 return 0;
5144}
5145
5146static int
5147nfs4_init_uniform_client_string(struct nfs_client *clp)
5148{
5149 int result;
5150 size_t len;
5151 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005152
5153 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005154 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005155
5156 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005157 return nfs4_init_uniquifier_client_string(clp);
5158
5159 len = 10 + 10 + 1 + 10 + 1 +
5160 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5161
5162 if (len > NFS4_OPAQUE_LIMIT + 1)
5163 return -EINVAL;
5164
5165 /*
5166 * Since this string is allocated at mount time, and held until the
5167 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5168 * about a memory-reclaim deadlock.
5169 */
5170 str = kmalloc(len, GFP_KERNEL);
5171 if (!str)
5172 return -ENOMEM;
5173
5174 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5175 clp->rpc_ops->version, clp->cl_minorversion,
5176 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005177 clp->cl_owner_id = str;
5178 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005179}
5180
Chuck Lever706cb8d2014-03-12 12:51:47 -04005181/*
5182 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5183 * services. Advertise one based on the address family of the
5184 * clientaddr.
5185 */
5186static unsigned int
5187nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5188{
5189 if (strchr(clp->cl_ipaddr, ':') != NULL)
5190 return scnprintf(buf, len, "tcp6");
5191 else
5192 return scnprintf(buf, len, "tcp");
5193}
5194
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005195static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5196{
5197 struct nfs4_setclientid *sc = calldata;
5198
5199 if (task->tk_status == 0)
5200 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5201}
5202
5203static const struct rpc_call_ops nfs4_setclientid_ops = {
5204 .rpc_call_done = nfs4_setclientid_done,
5205};
5206
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005207/**
5208 * nfs4_proc_setclientid - Negotiate client ID
5209 * @clp: state data structure
5210 * @program: RPC program for NFSv4 callback service
5211 * @port: IP port number for NFS4 callback service
5212 * @cred: RPC credential to use for this call
5213 * @res: where to place the result
5214 *
5215 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5216 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005217int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5218 unsigned short port, struct rpc_cred *cred,
5219 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220{
5221 nfs4_verifier sc_verifier;
5222 struct nfs4_setclientid setclientid = {
5223 .sc_verifier = &sc_verifier,
5224 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005225 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 };
5227 struct rpc_message msg = {
5228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5229 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005230 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005231 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005233 struct rpc_task *task;
5234 struct rpc_task_setup task_setup_data = {
5235 .rpc_client = clp->cl_rpcclient,
5236 .rpc_message = &msg,
5237 .callback_ops = &nfs4_setclientid_ops,
5238 .callback_data = &setclientid,
5239 .flags = RPC_TASK_TIMEOUT,
5240 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005241 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
Chuck Leverde734832012-07-11 16:30:50 -04005243 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005244 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005245
5246 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5247 status = nfs4_init_uniform_client_string(clp);
5248 else
Jeff Laytona3192682015-06-09 19:43:59 -04005249 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005250
5251 if (status)
5252 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005253
Chuck Leverde734832012-07-11 16:30:50 -04005254 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005255 setclientid.sc_netid_len =
5256 nfs4_init_callback_netid(clp,
5257 setclientid.sc_netid,
5258 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005259 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005260 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 clp->cl_ipaddr, port >> 8, port & 255);
5262
Jeff Layton3a6bb732015-06-09 19:43:57 -04005263 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005264 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005265 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005266 task = rpc_run_task(&task_setup_data);
5267 if (IS_ERR(task)) {
5268 status = PTR_ERR(task);
5269 goto out;
5270 }
5271 status = task->tk_status;
5272 if (setclientid.sc_cred) {
5273 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5274 put_rpccred(setclientid.sc_cred);
5275 }
5276 rpc_put_task(task);
5277out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005278 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005279 dprintk("NFS reply setclientid: %d\n", status);
5280 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281}
5282
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005283/**
5284 * nfs4_proc_setclientid_confirm - Confirm client ID
5285 * @clp: state data structure
5286 * @res: result of a previous SETCLIENTID
5287 * @cred: RPC credential to use for this call
5288 *
5289 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5290 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005291int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005292 struct nfs4_setclientid_res *arg,
5293 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 struct rpc_message msg = {
5296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005297 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005298 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 int status;
5301
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005302 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5303 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5304 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005305 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005306 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005307 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 return status;
5309}
5310
Trond Myklebustfe650402006-01-03 09:55:18 +01005311struct nfs4_delegreturndata {
5312 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005313 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005314 struct nfs_fh fh;
5315 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005316 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005317 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005318 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005319 struct inode *inode;
5320 bool roc;
5321 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005322};
5323
Trond Myklebustfe650402006-01-03 09:55:18 +01005324static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5325{
5326 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005327
Trond Myklebust14516c32010-07-31 14:29:06 -04005328 if (!nfs4_sequence_done(task, &data->res.seq_res))
5329 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005330
Trond Myklebustca8acf82013-08-13 10:36:56 -04005331 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005332 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005333 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005334 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005335 case -NFS4ERR_ADMIN_REVOKED:
5336 case -NFS4ERR_DELEG_REVOKED:
5337 case -NFS4ERR_BAD_STATEID:
5338 case -NFS4ERR_OLD_STATEID:
5339 case -NFS4ERR_STALE_STATEID:
5340 case -NFS4ERR_EXPIRED:
5341 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005342 if (data->roc)
5343 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005344 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005345 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005346 if (nfs4_async_handle_error(task, data->res.server,
5347 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005348 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005349 return;
5350 }
5351 }
5352 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005353}
5354
5355static void nfs4_delegreturn_release(void *calldata)
5356{
Peng Tao039b7562014-07-03 13:05:02 +08005357 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005358 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005359
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005360 if (inode) {
5361 if (data->roc)
5362 pnfs_roc_release(inode);
5363 nfs_iput_and_deactive(inode);
5364 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005365 kfree(calldata);
5366}
5367
Andy Adamson938e1012009-04-01 09:22:28 -04005368static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5369{
5370 struct nfs4_delegreturndata *d_data;
5371
5372 d_data = (struct nfs4_delegreturndata *)data;
5373
Peng Tao500d7012015-09-22 11:35:22 +08005374 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5375 return;
5376
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005377 if (d_data->roc)
5378 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005379
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005380 nfs4_setup_sequence(d_data->res.server,
5381 &d_data->args.seq_args,
5382 &d_data->res.seq_res,
5383 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005384}
Andy Adamson938e1012009-04-01 09:22:28 -04005385
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005386static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005387 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005388 .rpc_call_done = nfs4_delegreturn_done,
5389 .rpc_release = nfs4_delegreturn_release,
5390};
5391
Trond Myklebuste6f81072008-01-24 18:14:34 -05005392static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005393{
5394 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005395 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005396 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005397 struct rpc_message msg = {
5398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5399 .rpc_cred = cred,
5400 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005401 struct rpc_task_setup task_setup_data = {
5402 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005403 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005404 .callback_ops = &nfs4_delegreturn_ops,
5405 .flags = RPC_TASK_ASYNC,
5406 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005407 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005408
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005409 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005410 if (data == NULL)
5411 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005412 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005413 data->args.fhandle = &data->fh;
5414 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005415 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005416 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005417 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005418 data->res.fattr = &data->fattr;
5419 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005420 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005421 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005422 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005423 data->inode = nfs_igrab_and_active(inode);
5424 if (data->inode)
5425 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005426
Trond Myklebustc970aa82007-07-14 15:39:59 -04005427 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005428 msg.rpc_argp = &data->args;
5429 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005430 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005431 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005432 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005433 if (!issync)
5434 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005435 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005436 if (status != 0)
5437 goto out;
5438 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005439 if (status == 0)
5440 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5441 else
5442 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005443out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005444 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005445 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446}
5447
Trond Myklebuste6f81072008-01-24 18:14:34 -05005448int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449{
5450 struct nfs_server *server = NFS_SERVER(inode);
5451 struct nfs4_exception exception = { };
5452 int err;
5453 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005454 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005455 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 switch (err) {
5457 case -NFS4ERR_STALE_STATEID:
5458 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 case 0:
5460 return 0;
5461 }
5462 err = nfs4_handle_exception(server, err, &exception);
5463 } while (exception.retry);
5464 return err;
5465}
5466
5467#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5468#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5469
5470/*
5471 * sleep, with exponential backoff, and retry the LOCK operation.
5472 */
5473static unsigned long
5474nfs4_set_lock_task_retry(unsigned long timeout)
5475{
Colin Cross416ad3c2013-05-06 23:50:06 +00005476 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 timeout <<= 1;
5478 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5479 return NFS4_LOCK_MAXTIMEOUT;
5480 return timeout;
5481}
5482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5484{
5485 struct inode *inode = state->inode;
5486 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005487 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005488 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005490 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005492 struct nfs_lockt_res res = {
5493 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 };
5495 struct rpc_message msg = {
5496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5497 .rpc_argp = &arg,
5498 .rpc_resp = &res,
5499 .rpc_cred = state->owner->so_cred,
5500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 struct nfs4_lock_state *lsp;
5502 int status;
5503
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005504 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005505 status = nfs4_set_lock_state(state, request);
5506 if (status != 0)
5507 goto out;
5508 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005509 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005510 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005511 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005512 switch (status) {
5513 case 0:
5514 request->fl_type = F_UNLCK;
5515 break;
5516 case -NFS4ERR_DENIED:
5517 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005519 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005520 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005521out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 return status;
5523}
5524
5525static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5526{
5527 struct nfs4_exception exception = { };
5528 int err;
5529
5530 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005531 err = _nfs4_proc_getlk(state, cmd, request);
5532 trace_nfs4_get_lock(request, state, cmd, err);
5533 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 &exception);
5535 } while (exception.retry);
5536 return err;
5537}
5538
Jeff Layton83bfff22015-07-11 06:43:03 -04005539static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540{
5541 int res = 0;
5542 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5543 case FL_POSIX:
Jeff Layton83bfff22015-07-11 06:43:03 -04005544 res = posix_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 break;
5546 case FL_FLOCK:
Jeff Layton83bfff22015-07-11 06:43:03 -04005547 res = flock_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 break;
5549 default:
5550 BUG();
5551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 return res;
5553}
5554
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005555struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005556 struct nfs_locku_args arg;
5557 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005558 struct nfs4_lock_state *lsp;
5559 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005560 struct file_lock fl;
5561 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005562 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005563};
5564
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005565static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5566 struct nfs_open_context *ctx,
5567 struct nfs4_lock_state *lsp,
5568 struct nfs_seqid *seqid)
5569{
5570 struct nfs4_unlockdata *p;
5571 struct inode *inode = lsp->ls_state->inode;
5572
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005573 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005574 if (p == NULL)
5575 return NULL;
5576 p->arg.fh = NFS_FH(inode);
5577 p->arg.fl = &p->fl;
5578 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005579 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005580 p->lsp = lsp;
5581 atomic_inc(&lsp->ls_count);
5582 /* Ensure we don't close file until we're done freeing locks! */
5583 p->ctx = get_nfs_open_context(ctx);
5584 memcpy(&p->fl, fl, sizeof(p->fl));
5585 p->server = NFS_SERVER(inode);
5586 return p;
5587}
5588
Trond Myklebust06f814a2006-01-03 09:55:07 +01005589static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005590{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005591 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005592 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005593 nfs4_put_lock_state(calldata->lsp);
5594 put_nfs_open_context(calldata->ctx);
5595 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005596}
5597
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005598static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005599{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005600 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005601
Trond Myklebust14516c32010-07-31 14:29:06 -04005602 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5603 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005604 switch (task->tk_status) {
5605 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005606 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005607 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005608 if (nfs4_update_lock_stateid(calldata->lsp,
5609 &calldata->res.stateid))
5610 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005611 case -NFS4ERR_BAD_STATEID:
5612 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005613 case -NFS4ERR_STALE_STATEID:
5614 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005615 if (!nfs4_stateid_match(&calldata->arg.stateid,
5616 &calldata->lsp->ls_stateid))
5617 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005618 break;
5619 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005620 if (nfs4_async_handle_error(task, calldata->server,
5621 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005622 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005623 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005624 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005625}
5626
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005627static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005628{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005629 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005631 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005632 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005633 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005634 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005635 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005636 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005637 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005638 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005639 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005640 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005641 &calldata->res.seq_res,
5642 task) != 0)
5643 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005644 return;
5645out_no_action:
5646 task->tk_action = NULL;
5647out_wait:
5648 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649}
5650
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005651static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005652 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005653 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005654 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005655};
5656
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005657static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5658 struct nfs_open_context *ctx,
5659 struct nfs4_lock_state *lsp,
5660 struct nfs_seqid *seqid)
5661{
5662 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005663 struct rpc_message msg = {
5664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5665 .rpc_cred = ctx->cred,
5666 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005667 struct rpc_task_setup task_setup_data = {
5668 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005669 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005670 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005671 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005672 .flags = RPC_TASK_ASYNC,
5673 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005674
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005675 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5676 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5677
Frank Filz137d6ac2007-07-09 15:32:29 -07005678 /* Ensure this is an unlock - when canceling a lock, the
5679 * canceled lock is passed in, and it won't be an unlock.
5680 */
5681 fl->fl_type = F_UNLCK;
5682
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005683 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5684 if (data == NULL) {
5685 nfs_free_seqid(seqid);
5686 return ERR_PTR(-ENOMEM);
5687 }
5688
Chuck Levera9c92d62013-08-09 12:48:18 -04005689 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005690 msg.rpc_argp = &data->arg;
5691 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005692 task_setup_data.callback_data = data;
5693 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005694}
5695
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5697{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005698 struct inode *inode = state->inode;
5699 struct nfs4_state_owner *sp = state->owner;
5700 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005701 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005702 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005703 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005704 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005705 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005706 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
Trond Myklebust9b073572006-06-29 16:38:34 -04005708 status = nfs4_set_lock_state(state, request);
5709 /* Unlock _before_ we do the RPC call */
5710 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005711 /* Exclude nfs_delegation_claim_locks() */
5712 mutex_lock(&sp->so_delegreturn_mutex);
5713 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005714 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005715 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005716 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005717 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005718 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005719 }
5720 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005721 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005722 if (status != 0)
5723 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005724 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005725 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005726 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5727 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005728 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5729 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005730 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005731 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005732 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005733 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005734 status = PTR_ERR(task);
5735 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005736 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005737 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005738 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005739out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005740 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005741 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005742 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743}
5744
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005745struct nfs4_lockdata {
5746 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005747 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005748 struct nfs4_lock_state *lsp;
5749 struct nfs_open_context *ctx;
5750 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005751 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005752 int rpc_status;
5753 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005754 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005755};
5756
5757static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005758 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5759 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005760{
5761 struct nfs4_lockdata *p;
5762 struct inode *inode = lsp->ls_state->inode;
5763 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005764 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005765
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005766 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005767 if (p == NULL)
5768 return NULL;
5769
5770 p->arg.fh = NFS_FH(inode);
5771 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005772 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005773 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005774 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005775 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5776 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005777 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005778 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005779 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005780 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005781 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005782 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005783 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005784 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005785 atomic_inc(&lsp->ls_count);
5786 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005787 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005788 memcpy(&p->fl, fl, sizeof(p->fl));
5789 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005790out_free_seqid:
5791 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005792out_free:
5793 kfree(p);
5794 return NULL;
5795}
5796
5797static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5798{
5799 struct nfs4_lockdata *data = calldata;
5800 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
Harvey Harrison3110ff82008-05-02 13:42:44 -07005802 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005803 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005804 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005805 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005806 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005807 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005808 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005809 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005810 nfs4_stateid_copy(&data->arg.open_stateid,
5811 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005812 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005813 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005814 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005815 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005816 nfs4_stateid_copy(&data->arg.lock_stateid,
5817 &data->lsp->ls_stateid);
5818 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005819 if (!nfs4_valid_open_stateid(state)) {
5820 data->rpc_status = -EBADF;
5821 task->tk_action = NULL;
5822 goto out_release_open_seqid;
5823 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005824 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005825 if (nfs4_setup_sequence(data->server,
5826 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005827 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005828 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005829 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005830out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005831 nfs_release_seqid(data->arg.open_seqid);
5832out_release_lock_seqid:
5833 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005834out_wait:
5835 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005836 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005837}
5838
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005839static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5840{
5841 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005842 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005843
Harvey Harrison3110ff82008-05-02 13:42:44 -07005844 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005845
Trond Myklebust14516c32010-07-31 14:29:06 -04005846 if (!nfs4_sequence_done(task, &data->res.seq_res))
5847 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005848
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005849 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005850 switch (task->tk_status) {
5851 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005852 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005853 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005854 if (data->arg.new_lock) {
5855 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005856 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005857 rpc_restart_call_prepare(task);
5858 break;
5859 }
5860 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005861 if (data->arg.new_lock_owner != 0) {
5862 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5863 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5864 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5865 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5866 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005867 break;
5868 case -NFS4ERR_BAD_STATEID:
5869 case -NFS4ERR_OLD_STATEID:
5870 case -NFS4ERR_STALE_STATEID:
5871 case -NFS4ERR_EXPIRED:
5872 if (data->arg.new_lock_owner != 0) {
5873 if (!nfs4_stateid_match(&data->arg.open_stateid,
5874 &lsp->ls_state->open_stateid))
5875 rpc_restart_call_prepare(task);
5876 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5877 &lsp->ls_stateid))
5878 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005879 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005880 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005881}
5882
5883static void nfs4_lock_release(void *calldata)
5884{
5885 struct nfs4_lockdata *data = calldata;
5886
Harvey Harrison3110ff82008-05-02 13:42:44 -07005887 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005888 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005889 if (data->cancelled != 0) {
5890 struct rpc_task *task;
5891 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5892 data->arg.lock_seqid);
5893 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005894 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005895 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005896 } else
5897 nfs_free_seqid(data->arg.lock_seqid);
5898 nfs4_put_lock_state(data->lsp);
5899 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005900 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005901 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005902 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005903}
5904
5905static const struct rpc_call_ops nfs4_lock_ops = {
5906 .rpc_call_prepare = nfs4_lock_prepare,
5907 .rpc_call_done = nfs4_lock_done,
5908 .rpc_release = nfs4_lock_release,
5909};
5910
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005911static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5912{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005913 switch (error) {
5914 case -NFS4ERR_ADMIN_REVOKED:
5915 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005916 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005917 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005918 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005919 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005920 break;
5921 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005922 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005923 case -NFS4ERR_EXPIRED:
5924 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005925 };
5926}
5927
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005928static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005929{
5930 struct nfs4_lockdata *data;
5931 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005932 struct rpc_message msg = {
5933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5934 .rpc_cred = state->owner->so_cred,
5935 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005936 struct rpc_task_setup task_setup_data = {
5937 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005938 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005939 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005940 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005941 .flags = RPC_TASK_ASYNC,
5942 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005943 int ret;
5944
Harvey Harrison3110ff82008-05-02 13:42:44 -07005945 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005946 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005947 fl->fl_u.nfs4_fl.owner,
5948 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005949 if (data == NULL)
5950 return -ENOMEM;
5951 if (IS_SETLKW(cmd))
5952 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005953 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005954 msg.rpc_argp = &data->arg;
5955 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005956 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005957 if (recovery_type > NFS_LOCK_NEW) {
5958 if (recovery_type == NFS_LOCK_RECLAIM)
5959 data->arg.reclaim = NFS_LOCK_RECLAIM;
5960 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005961 } else
5962 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005963 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005964 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005965 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005966 ret = nfs4_wait_for_completion_rpc_task(task);
5967 if (ret == 0) {
5968 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005969 if (ret)
5970 nfs4_handle_setlk_error(data->server, data->lsp,
5971 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005972 } else
5973 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005974 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005975 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005976 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977}
5978
5979static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5980{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005981 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005982 struct nfs4_exception exception = {
5983 .inode = state->inode,
5984 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005985 int err;
5986
5987 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005988 /* Cache the lock if possible... */
5989 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5990 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005991 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005992 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005993 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005994 break;
5995 nfs4_handle_exception(server, err, &exception);
5996 } while (exception.retry);
5997 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998}
5999
6000static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6001{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006002 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006003 struct nfs4_exception exception = {
6004 .inode = state->inode,
6005 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006006 int err;
6007
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006008 err = nfs4_set_lock_state(state, request);
6009 if (err != 0)
6010 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006011 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006012 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6013 return 0;
6014 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006015 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006016 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6017 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006018 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006019 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006020 switch (err) {
6021 default:
6022 goto out;
6023 case -NFS4ERR_GRACE:
6024 case -NFS4ERR_DELAY:
6025 nfs4_handle_exception(server, err, &exception);
6026 err = 0;
6027 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006028 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006029out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006030 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031}
6032
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006033#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006034/**
6035 * nfs41_check_expired_locks - possibly free a lock stateid
6036 *
6037 * @state: NFSv4 state for an inode
6038 *
6039 * Returns NFS_OK if recovery for this stateid is now finished.
6040 * Otherwise a negative NFS4ERR value is returned.
6041 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006042static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006043{
Chuck Levereb64cf92012-07-11 16:30:05 -04006044 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006045 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006046 struct nfs_server *server = NFS_SERVER(state->inode);
6047
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006048 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006049 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006050 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6051
6052 status = nfs41_test_stateid(server,
6053 &lsp->ls_stateid,
6054 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006055 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006056 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006057 /* Free the stateid unless the server
6058 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006059 if (status != -NFS4ERR_BAD_STATEID)
6060 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006061 &lsp->ls_stateid,
6062 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006063 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006064 ret = status;
6065 }
6066 }
6067 };
6068
6069 return ret;
6070}
6071
6072static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6073{
6074 int status = NFS_OK;
6075
6076 if (test_bit(LK_STATE_IN_USE, &state->flags))
6077 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006078 if (status != NFS_OK)
6079 status = nfs4_lock_expired(state, request);
6080 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006081}
6082#endif
6083
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6085{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006086 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006087 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006088 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006090 if ((fl_flags & FL_POSIX) &&
6091 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6092 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006093 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006094 status = nfs4_set_lock_state(state, request);
6095 if (status != 0)
6096 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006097 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006098 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006099 if (status < 0)
6100 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006101 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006102 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006103 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006104 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006105 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006106 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006107 up_read(&nfsi->rwsem);
6108 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006109 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006110 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006111 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006112out:
6113 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 return status;
6115}
6116
6117static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6118{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006119 struct nfs4_exception exception = {
6120 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006121 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006122 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 int err;
6124
6125 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006126 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006127 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07006128 if (err == -NFS4ERR_DENIED)
6129 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006131 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 } while (exception.retry);
6133 return err;
6134}
6135
6136static int
6137nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6138{
6139 struct nfs_open_context *ctx;
6140 struct nfs4_state *state;
6141 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6142 int status;
6143
6144 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006145 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 state = ctx->state;
6147
6148 if (request->fl_start < 0 || request->fl_end < 0)
6149 return -EINVAL;
6150
Trond Myklebustd9531262009-07-21 19:22:38 -04006151 if (IS_GETLK(cmd)) {
6152 if (state != NULL)
6153 return nfs4_proc_getlk(state, F_GETLK, request);
6154 return 0;
6155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
6157 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6158 return -EINVAL;
6159
Trond Myklebustd9531262009-07-21 19:22:38 -04006160 if (request->fl_type == F_UNLCK) {
6161 if (state != NULL)
6162 return nfs4_proc_unlck(state, cmd, request);
6163 return 0;
6164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
Trond Myklebustd9531262009-07-21 19:22:38 -04006166 if (state == NULL)
6167 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006168 /*
6169 * Don't rely on the VFS having checked the file open mode,
6170 * since it won't do this for flock() locks.
6171 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006172 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006173 case F_RDLCK:
6174 if (!(filp->f_mode & FMODE_READ))
6175 return -EBADF;
6176 break;
6177 case F_WRLCK:
6178 if (!(filp->f_mode & FMODE_WRITE))
6179 return -EBADF;
6180 }
6181
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 do {
6183 status = nfs4_proc_setlk(state, cmd, request);
6184 if ((status != -EAGAIN) || IS_SETLK(cmd))
6185 break;
6186 timeout = nfs4_set_lock_task_retry(timeout);
6187 status = -ERESTARTSYS;
6188 if (signalled())
6189 break;
6190 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 return status;
6192}
6193
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006194int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006195{
6196 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006197 int err;
6198
6199 err = nfs4_set_lock_state(state, fl);
6200 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006201 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006202 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006203 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006204}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006205
Trond Myklebustcf470c32012-03-07 13:49:12 -05006206struct nfs_release_lockowner_data {
6207 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006208 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006209 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006210 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006211 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006212};
6213
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006214static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6215{
6216 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006217 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006218 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6219 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006220 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006221 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006222}
6223
6224static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6225{
6226 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006227 struct nfs_server *server = data->server;
6228
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006229 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006230
6231 switch (task->tk_status) {
6232 case 0:
6233 renew_lease(server, data->timestamp);
6234 break;
6235 case -NFS4ERR_STALE_CLIENTID:
6236 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006237 nfs4_schedule_lease_recovery(server->nfs_client);
6238 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006239 case -NFS4ERR_LEASE_MOVED:
6240 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006241 if (nfs4_async_handle_error(task, server,
6242 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006243 rpc_restart_call_prepare(task);
6244 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006245}
6246
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006247static void nfs4_release_lockowner_release(void *calldata)
6248{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006249 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006250 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006251 kfree(calldata);
6252}
6253
Trond Myklebust17280172012-03-11 13:11:00 -04006254static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006255 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6256 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006257 .rpc_release = nfs4_release_lockowner_release,
6258};
6259
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006260static void
6261nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006262{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006263 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006264 struct rpc_message msg = {
6265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6266 };
6267
6268 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006269 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006270
Trond Myklebustcf470c32012-03-07 13:49:12 -05006271 data = kmalloc(sizeof(*data), GFP_NOFS);
6272 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006273 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006274 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006275 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006276 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6277 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6278 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006279
Trond Myklebustcf470c32012-03-07 13:49:12 -05006280 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006281 msg.rpc_resp = &data->res;
6282 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006283 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006284}
6285
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006286#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6287
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006288static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6289 const void *buf, size_t buflen,
6290 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006291{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006292 if (strcmp(key, "") != 0)
6293 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006294
David Howells2b0143b2015-03-17 22:25:59 +00006295 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006296}
6297
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006298static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6299 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006300{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006301 if (strcmp(key, "") != 0)
6302 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006303
David Howells2b0143b2015-03-17 22:25:59 +00006304 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006305}
6306
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006307static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6308 size_t list_len, const char *name,
6309 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006310{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006311 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006312
David Howells2b0143b2015-03-17 22:25:59 +00006313 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
J. Bruce Fields096455a2006-03-20 23:23:42 -05006314 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006315
6316 if (list && len <= list_len)
6317 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006318 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006319}
6320
David Quigleyc9bccef2013-05-22 12:50:45 -04006321#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6322static inline int nfs4_server_supports_labels(struct nfs_server *server)
6323{
6324 return server->caps & NFS_CAP_SECURITY_LABEL;
6325}
6326
6327static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6328 const void *buf, size_t buflen,
6329 int flags, int type)
6330{
6331 if (security_ismaclabel(key))
6332 return nfs4_set_security_label(dentry, buf, buflen);
6333
6334 return -EOPNOTSUPP;
6335}
6336
6337static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6338 void *buf, size_t buflen, int type)
6339{
6340 if (security_ismaclabel(key))
David Howells2b0143b2015-03-17 22:25:59 +00006341 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006342 return -EOPNOTSUPP;
6343}
6344
6345static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6346 size_t list_len, const char *name,
6347 size_t name_len, int type)
6348{
6349 size_t len = 0;
6350
David Howells2b0143b2015-03-17 22:25:59 +00006351 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6352 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
David Quigleyc9bccef2013-05-22 12:50:45 -04006353 if (list && len <= list_len)
David Howells2b0143b2015-03-17 22:25:59 +00006354 security_inode_listsecurity(d_inode(dentry), list, len);
David Quigleyc9bccef2013-05-22 12:50:45 -04006355 }
6356 return len;
6357}
6358
6359static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6360 .prefix = XATTR_SECURITY_PREFIX,
6361 .list = nfs4_xattr_list_nfs4_label,
6362 .get = nfs4_xattr_get_nfs4_label,
6363 .set = nfs4_xattr_set_nfs4_label,
6364};
6365#endif
6366
6367
Andy Adamson533eb462011-06-13 18:25:56 -04006368/*
6369 * nfs_fhget will use either the mounted_on_fileid or the fileid
6370 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006371static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6372{
Andy Adamson533eb462011-06-13 18:25:56 -04006373 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6374 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6375 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006376 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006377 return;
6378
6379 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006380 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006381 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6382 fattr->nlink = 2;
6383}
6384
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006385static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6386 const struct qstr *name,
6387 struct nfs4_fs_locations *fs_locations,
6388 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006389{
6390 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006391 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006392 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006393 };
6394 struct nfs4_fs_locations_arg args = {
6395 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006396 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006397 .page = page,
6398 .bitmask = bitmask,
6399 };
Benny Halevy22958462009-04-01 09:22:02 -04006400 struct nfs4_fs_locations_res res = {
6401 .fs_locations = fs_locations,
6402 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006403 struct rpc_message msg = {
6404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6405 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006406 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006407 };
6408 int status;
6409
Harvey Harrison3110ff82008-05-02 13:42:44 -07006410 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006411
6412 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6413 * is not supported */
6414 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6415 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6416 else
6417 bitmask[0] |= FATTR4_WORD0_FILEID;
6418
Trond Myklebustc228fd32007-01-13 02:28:11 -05006419 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006420 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006421 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006422 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006423 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006424 return status;
6425}
6426
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006427int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6428 const struct qstr *name,
6429 struct nfs4_fs_locations *fs_locations,
6430 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006431{
6432 struct nfs4_exception exception = { };
6433 int err;
6434 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006435 err = _nfs4_proc_fs_locations(client, dir, name,
6436 fs_locations, page);
6437 trace_nfs4_get_fs_locations(dir, name, err);
6438 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006439 &exception);
6440 } while (exception.retry);
6441 return err;
6442}
6443
Chuck Leverb03d7352013-10-17 14:12:50 -04006444/*
6445 * This operation also signals the server that this client is
6446 * performing migration recovery. The server can stop returning
6447 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6448 * appended to this compound to identify the client ID which is
6449 * performing recovery.
6450 */
6451static int _nfs40_proc_get_locations(struct inode *inode,
6452 struct nfs4_fs_locations *locations,
6453 struct page *page, struct rpc_cred *cred)
6454{
6455 struct nfs_server *server = NFS_SERVER(inode);
6456 struct rpc_clnt *clnt = server->client;
6457 u32 bitmask[2] = {
6458 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6459 };
6460 struct nfs4_fs_locations_arg args = {
6461 .clientid = server->nfs_client->cl_clientid,
6462 .fh = NFS_FH(inode),
6463 .page = page,
6464 .bitmask = bitmask,
6465 .migration = 1, /* skip LOOKUP */
6466 .renew = 1, /* append RENEW */
6467 };
6468 struct nfs4_fs_locations_res res = {
6469 .fs_locations = locations,
6470 .migration = 1,
6471 .renew = 1,
6472 };
6473 struct rpc_message msg = {
6474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6475 .rpc_argp = &args,
6476 .rpc_resp = &res,
6477 .rpc_cred = cred,
6478 };
6479 unsigned long now = jiffies;
6480 int status;
6481
6482 nfs_fattr_init(&locations->fattr);
6483 locations->server = server;
6484 locations->nlocations = 0;
6485
6486 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6487 nfs4_set_sequence_privileged(&args.seq_args);
6488 status = nfs4_call_sync_sequence(clnt, server, &msg,
6489 &args.seq_args, &res.seq_res);
6490 if (status)
6491 return status;
6492
6493 renew_lease(server, now);
6494 return 0;
6495}
6496
6497#ifdef CONFIG_NFS_V4_1
6498
6499/*
6500 * This operation also signals the server that this client is
6501 * performing migration recovery. The server can stop asserting
6502 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6503 * performing this operation is identified in the SEQUENCE
6504 * operation in this compound.
6505 *
6506 * When the client supports GETATTR(fs_locations_info), it can
6507 * be plumbed in here.
6508 */
6509static int _nfs41_proc_get_locations(struct inode *inode,
6510 struct nfs4_fs_locations *locations,
6511 struct page *page, struct rpc_cred *cred)
6512{
6513 struct nfs_server *server = NFS_SERVER(inode);
6514 struct rpc_clnt *clnt = server->client;
6515 u32 bitmask[2] = {
6516 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6517 };
6518 struct nfs4_fs_locations_arg args = {
6519 .fh = NFS_FH(inode),
6520 .page = page,
6521 .bitmask = bitmask,
6522 .migration = 1, /* skip LOOKUP */
6523 };
6524 struct nfs4_fs_locations_res res = {
6525 .fs_locations = locations,
6526 .migration = 1,
6527 };
6528 struct rpc_message msg = {
6529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6530 .rpc_argp = &args,
6531 .rpc_resp = &res,
6532 .rpc_cred = cred,
6533 };
6534 int status;
6535
6536 nfs_fattr_init(&locations->fattr);
6537 locations->server = server;
6538 locations->nlocations = 0;
6539
6540 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6541 nfs4_set_sequence_privileged(&args.seq_args);
6542 status = nfs4_call_sync_sequence(clnt, server, &msg,
6543 &args.seq_args, &res.seq_res);
6544 if (status == NFS4_OK &&
6545 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6546 status = -NFS4ERR_LEASE_MOVED;
6547 return status;
6548}
6549
6550#endif /* CONFIG_NFS_V4_1 */
6551
6552/**
6553 * nfs4_proc_get_locations - discover locations for a migrated FSID
6554 * @inode: inode on FSID that is migrating
6555 * @locations: result of query
6556 * @page: buffer
6557 * @cred: credential to use for this operation
6558 *
6559 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6560 * operation failed, or a negative errno if a local error occurred.
6561 *
6562 * On success, "locations" is filled in, but if the server has
6563 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6564 * asserted.
6565 *
6566 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6567 * from this client that require migration recovery.
6568 */
6569int nfs4_proc_get_locations(struct inode *inode,
6570 struct nfs4_fs_locations *locations,
6571 struct page *page, struct rpc_cred *cred)
6572{
6573 struct nfs_server *server = NFS_SERVER(inode);
6574 struct nfs_client *clp = server->nfs_client;
6575 const struct nfs4_mig_recovery_ops *ops =
6576 clp->cl_mvops->mig_recovery_ops;
6577 struct nfs4_exception exception = { };
6578 int status;
6579
6580 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6581 (unsigned long long)server->fsid.major,
6582 (unsigned long long)server->fsid.minor,
6583 clp->cl_hostname);
6584 nfs_display_fhandle(NFS_FH(inode), __func__);
6585
6586 do {
6587 status = ops->get_locations(inode, locations, page, cred);
6588 if (status != -NFS4ERR_DELAY)
6589 break;
6590 nfs4_handle_exception(server, status, &exception);
6591 } while (exception.retry);
6592 return status;
6593}
6594
Chuck Lever44c99932013-10-17 14:13:30 -04006595/*
6596 * This operation also signals the server that this client is
6597 * performing "lease moved" recovery. The server can stop
6598 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6599 * is appended to this compound to identify the client ID which is
6600 * performing recovery.
6601 */
6602static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6603{
6604 struct nfs_server *server = NFS_SERVER(inode);
6605 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6606 struct rpc_clnt *clnt = server->client;
6607 struct nfs4_fsid_present_arg args = {
6608 .fh = NFS_FH(inode),
6609 .clientid = clp->cl_clientid,
6610 .renew = 1, /* append RENEW */
6611 };
6612 struct nfs4_fsid_present_res res = {
6613 .renew = 1,
6614 };
6615 struct rpc_message msg = {
6616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6617 .rpc_argp = &args,
6618 .rpc_resp = &res,
6619 .rpc_cred = cred,
6620 };
6621 unsigned long now = jiffies;
6622 int status;
6623
6624 res.fh = nfs_alloc_fhandle();
6625 if (res.fh == NULL)
6626 return -ENOMEM;
6627
6628 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6629 nfs4_set_sequence_privileged(&args.seq_args);
6630 status = nfs4_call_sync_sequence(clnt, server, &msg,
6631 &args.seq_args, &res.seq_res);
6632 nfs_free_fhandle(res.fh);
6633 if (status)
6634 return status;
6635
6636 do_renew_lease(clp, now);
6637 return 0;
6638}
6639
6640#ifdef CONFIG_NFS_V4_1
6641
6642/*
6643 * This operation also signals the server that this client is
6644 * performing "lease moved" recovery. The server can stop asserting
6645 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6646 * this operation is identified in the SEQUENCE operation in this
6647 * compound.
6648 */
6649static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6650{
6651 struct nfs_server *server = NFS_SERVER(inode);
6652 struct rpc_clnt *clnt = server->client;
6653 struct nfs4_fsid_present_arg args = {
6654 .fh = NFS_FH(inode),
6655 };
6656 struct nfs4_fsid_present_res res = {
6657 };
6658 struct rpc_message msg = {
6659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6660 .rpc_argp = &args,
6661 .rpc_resp = &res,
6662 .rpc_cred = cred,
6663 };
6664 int status;
6665
6666 res.fh = nfs_alloc_fhandle();
6667 if (res.fh == NULL)
6668 return -ENOMEM;
6669
6670 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6671 nfs4_set_sequence_privileged(&args.seq_args);
6672 status = nfs4_call_sync_sequence(clnt, server, &msg,
6673 &args.seq_args, &res.seq_res);
6674 nfs_free_fhandle(res.fh);
6675 if (status == NFS4_OK &&
6676 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6677 status = -NFS4ERR_LEASE_MOVED;
6678 return status;
6679}
6680
6681#endif /* CONFIG_NFS_V4_1 */
6682
6683/**
6684 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6685 * @inode: inode on FSID to check
6686 * @cred: credential to use for this operation
6687 *
6688 * Server indicates whether the FSID is present, moved, or not
6689 * recognized. This operation is necessary to clear a LEASE_MOVED
6690 * condition for this client ID.
6691 *
6692 * Returns NFS4_OK if the FSID is present on this server,
6693 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6694 * NFS4ERR code if some error occurred on the server, or a
6695 * negative errno if a local failure occurred.
6696 */
6697int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6698{
6699 struct nfs_server *server = NFS_SERVER(inode);
6700 struct nfs_client *clp = server->nfs_client;
6701 const struct nfs4_mig_recovery_ops *ops =
6702 clp->cl_mvops->mig_recovery_ops;
6703 struct nfs4_exception exception = { };
6704 int status;
6705
6706 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6707 (unsigned long long)server->fsid.major,
6708 (unsigned long long)server->fsid.minor,
6709 clp->cl_hostname);
6710 nfs_display_fhandle(NFS_FH(inode), __func__);
6711
6712 do {
6713 status = ops->fsid_present(inode, cred);
6714 if (status != -NFS4ERR_DELAY)
6715 break;
6716 nfs4_handle_exception(server, status, &exception);
6717 } while (exception.retry);
6718 return status;
6719}
6720
Andy Adamson5ec16a82013-08-08 10:57:55 -04006721/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006722 * If 'use_integrity' is true and the state managment nfs_client
6723 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6724 * and the machine credential as per RFC3530bis and RFC5661 Security
6725 * Considerations sections. Otherwise, just use the user cred with the
6726 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006727 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006728static 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 +00006729{
6730 int status;
6731 struct nfs4_secinfo_arg args = {
6732 .dir_fh = NFS_FH(dir),
6733 .name = name,
6734 };
6735 struct nfs4_secinfo_res res = {
6736 .flavors = flavors,
6737 };
6738 struct rpc_message msg = {
6739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6740 .rpc_argp = &args,
6741 .rpc_resp = &res,
6742 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006743 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006744 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006745
6746 if (use_integrity) {
6747 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006748 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6749 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006750 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006751
6752 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006753
6754 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6755 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6756
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006757 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6758 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006759 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006760
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006761 if (cred)
6762 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006763
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006764 return status;
6765}
6766
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006767int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6768 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006769{
6770 struct nfs4_exception exception = { };
6771 int err;
6772 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006773 err = -NFS4ERR_WRONGSEC;
6774
6775 /* try to use integrity protection with machine cred */
6776 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6777 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6778
6779 /*
6780 * if unable to use integrity protection, or SECINFO with
6781 * integrity protection returns NFS4ERR_WRONGSEC (which is
6782 * disallowed by spec, but exists in deployed servers) use
6783 * the current filesystem's rpc_client and the user cred.
6784 */
6785 if (err == -NFS4ERR_WRONGSEC)
6786 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6787
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006788 trace_nfs4_secinfo(dir, name, err);
6789 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006790 &exception);
6791 } while (exception.retry);
6792 return err;
6793}
6794
Andy Adamson557134a2009-04-01 09:21:53 -04006795#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006796/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006797 * Check the exchange flags returned by the server for invalid flags, having
6798 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6799 * DS flags set.
6800 */
6801static int nfs4_check_cl_exchange_flags(u32 flags)
6802{
6803 if (flags & ~EXCHGID4_FLAG_MASK_R)
6804 goto out_inval;
6805 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6806 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6807 goto out_inval;
6808 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6809 goto out_inval;
6810 return NFS_OK;
6811out_inval:
6812 return -NFS4ERR_INVAL;
6813}
6814
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006815static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006816nfs41_same_server_scope(struct nfs41_server_scope *a,
6817 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006818{
6819 if (a->server_scope_sz == b->server_scope_sz &&
6820 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6821 return true;
6822
6823 return false;
6824}
6825
Andy Adamson357f54d2010-12-14 10:11:57 -05006826/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006827 * nfs4_proc_bind_conn_to_session()
6828 *
6829 * The 4.1 client currently uses the same TCP connection for the
6830 * fore and backchannel.
6831 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006832int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006833{
6834 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006835 struct nfs41_bind_conn_to_session_args args = {
6836 .client = clp,
6837 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6838 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006839 struct nfs41_bind_conn_to_session_res res;
6840 struct rpc_message msg = {
6841 .rpc_proc =
6842 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006843 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006844 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006845 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006846 };
6847
6848 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006849
Trond Myklebust71a097c2015-02-18 09:27:18 -08006850 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6851 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6852 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006853
6854 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006855 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006856 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006857 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006858 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6859 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6860 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006861 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006862 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006863 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006864 dprintk("NFS: %s: Unexpected direction from server\n",
6865 __func__);
6866 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006867 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006868 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006869 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006870 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6871 __func__);
6872 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006873 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006874 }
6875 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006876out:
6877 dprintk("<-- %s status= %d\n", __func__, status);
6878 return status;
6879}
6880
6881/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006882 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6883 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006884 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006885static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6886 .how = SP4_MACH_CRED,
6887 .enforce.u.words = {
6888 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6889 1 << (OP_EXCHANGE_ID - 32) |
6890 1 << (OP_CREATE_SESSION - 32) |
6891 1 << (OP_DESTROY_SESSION - 32) |
6892 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006893 },
6894 .allow.u.words = {
6895 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006896 1 << (OP_LOCKU) |
6897 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006898 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006899 1 << (OP_SECINFO_NO_NAME - 32) |
6900 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006901 1 << (OP_FREE_STATEID - 32) |
6902 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006903 }
6904};
6905
6906/*
6907 * Select the state protection mode for client `clp' given the server results
6908 * from exchange_id in `sp'.
6909 *
6910 * Returns 0 on success, negative errno otherwise.
6911 */
6912static int nfs4_sp4_select_mode(struct nfs_client *clp,
6913 struct nfs41_state_protection *sp)
6914{
6915 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6916 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6917 1 << (OP_EXCHANGE_ID - 32) |
6918 1 << (OP_CREATE_SESSION - 32) |
6919 1 << (OP_DESTROY_SESSION - 32) |
6920 1 << (OP_DESTROY_CLIENTID - 32)
6921 };
6922 unsigned int i;
6923
6924 if (sp->how == SP4_MACH_CRED) {
6925 /* Print state protect result */
6926 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6927 for (i = 0; i <= LAST_NFS4_OP; i++) {
6928 if (test_bit(i, sp->enforce.u.longs))
6929 dfprintk(MOUNT, " enforce op %d\n", i);
6930 if (test_bit(i, sp->allow.u.longs))
6931 dfprintk(MOUNT, " allow op %d\n", i);
6932 }
6933
6934 /* make sure nothing is on enforce list that isn't supported */
6935 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6936 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6937 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6938 return -EINVAL;
6939 }
6940 }
6941
6942 /*
6943 * Minimal mode - state operations are allowed to use machine
6944 * credential. Note this already happens by default, so the
6945 * client doesn't have to do anything more than the negotiation.
6946 *
6947 * NOTE: we don't care if EXCHANGE_ID is in the list -
6948 * we're already using the machine cred for exchange_id
6949 * and will never use a different cred.
6950 */
6951 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6952 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6953 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6954 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6955 dfprintk(MOUNT, "sp4_mach_cred:\n");
6956 dfprintk(MOUNT, " minimal mode enabled\n");
6957 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6958 } else {
6959 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6960 return -EINVAL;
6961 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006962
6963 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6964 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6965 dfprintk(MOUNT, " cleanup mode enabled\n");
6966 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6967 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006968
6969 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6970 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6971 dfprintk(MOUNT, " secinfo mode enabled\n");
6972 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6973 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006974
6975 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6976 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6977 dfprintk(MOUNT, " stateid mode enabled\n");
6978 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6979 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006980
6981 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6982 dfprintk(MOUNT, " write mode enabled\n");
6983 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6984 }
6985
6986 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6987 dfprintk(MOUNT, " commit mode enabled\n");
6988 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6989 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006990 }
6991
6992 return 0;
6993}
6994
6995/*
6996 * _nfs4_proc_exchange_id()
6997 *
6998 * Wrapper for EXCHANGE_ID operation.
6999 */
7000static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7001 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007002{
7003 nfs4_verifier verifier;
7004 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05007005 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04007006 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04007007#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04007008 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04007009 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7010 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7011#else
7012 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7013 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7014#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04007015 };
7016 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04007017 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04007018 };
7019 int status;
7020 struct rpc_message msg = {
7021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7022 .rpc_argp = &args,
7023 .rpc_resp = &res,
7024 .rpc_cred = cred,
7025 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04007026
Chuck Leverf0920752012-05-21 22:45:41 -04007027 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007028
7029 status = nfs4_init_uniform_client_string(clp);
7030 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04007031 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007032
7033 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007034 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007035 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007036
Chuck Leveracdeb692012-05-21 22:46:16 -04007037 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007038 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04007039 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007040 status = -ENOMEM;
7041 goto out;
7042 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007043
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007044 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007045 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007046 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04007047 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04007048 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007049 }
7050
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007051 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007052 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007053 status = -ENOMEM;
7054 goto out_server_scope;
7055 }
7056
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007057 switch (sp4_how) {
7058 case SP4_NONE:
7059 args.state_protect.how = SP4_NONE;
7060 break;
7061
7062 case SP4_MACH_CRED:
7063 args.state_protect = nfs4_sp4_mach_cred_request;
7064 break;
7065
7066 default:
7067 /* unsupported! */
7068 WARN_ON_ONCE(1);
7069 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007070 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007071 }
7072
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007073 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007074 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04007075 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04007076 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007077
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007078 if (status == 0)
7079 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7080
Chuck Lever177313f2012-05-21 22:44:58 -04007081 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04007082 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007083 clp->cl_exchange_flags = res.flags;
7084 /* Client ID is not confirmed */
7085 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7086 clear_bit(NFS4_SESSION_ESTABLISHED,
7087 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007088 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007089 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007090
Chuck Leveracdeb692012-05-21 22:46:16 -04007091 kfree(clp->cl_serverowner);
7092 clp->cl_serverowner = res.server_owner;
7093 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007094
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007095 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007096 kfree(clp->cl_implid);
7097 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007098 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007099
Chuck Lever177313f2012-05-21 22:44:58 -04007100 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007101 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007102 res.server_scope)) {
7103 dprintk("%s: server_scope mismatch detected\n",
7104 __func__);
7105 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007106 kfree(clp->cl_serverscope);
7107 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007108 }
7109
Chuck Lever177313f2012-05-21 22:44:58 -04007110 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007111 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007112 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007113 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007114 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007115
Kinglong Mee6b559702015-07-01 11:54:53 +08007116out_impl_id:
7117 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007118out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007119 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007120out_server_owner:
7121 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007122out:
Chuck Lever177313f2012-05-21 22:44:58 -04007123 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007124 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007125 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007126 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007127 clp->cl_implid->date.seconds,
7128 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007129 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007130 return status;
7131}
7132
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007133/*
7134 * nfs4_proc_exchange_id()
7135 *
7136 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7137 *
7138 * Since the clientid has expired, all compounds using sessions
7139 * associated with the stale clientid will be returning
7140 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7141 * be in some phase of session reset.
7142 *
7143 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7144 */
7145int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7146{
7147 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7148 int status;
7149
7150 /* try SP4_MACH_CRED if krb5i/p */
7151 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7152 authflavor == RPC_AUTH_GSS_KRB5P) {
7153 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7154 if (!status)
7155 return 0;
7156 }
7157
7158 /* try SP4_NONE */
7159 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7160}
7161
Trond Myklebust66245532012-05-25 17:18:09 -04007162static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7163 struct rpc_cred *cred)
7164{
7165 struct rpc_message msg = {
7166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7167 .rpc_argp = clp,
7168 .rpc_cred = cred,
7169 };
7170 int status;
7171
7172 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007173 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007174 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007175 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007176 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7177 return status;
7178}
7179
7180static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7181 struct rpc_cred *cred)
7182{
7183 unsigned int loop;
7184 int ret;
7185
7186 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7187 ret = _nfs4_proc_destroy_clientid(clp, cred);
7188 switch (ret) {
7189 case -NFS4ERR_DELAY:
7190 case -NFS4ERR_CLIENTID_BUSY:
7191 ssleep(1);
7192 break;
7193 default:
7194 return ret;
7195 }
7196 }
7197 return 0;
7198}
7199
7200int nfs4_destroy_clientid(struct nfs_client *clp)
7201{
7202 struct rpc_cred *cred;
7203 int ret = 0;
7204
7205 if (clp->cl_mvops->minor_version < 1)
7206 goto out;
7207 if (clp->cl_exchange_flags == 0)
7208 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007209 if (clp->cl_preserve_clid)
7210 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007211 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007212 ret = nfs4_proc_destroy_clientid(clp, cred);
7213 if (cred)
7214 put_rpccred(cred);
7215 switch (ret) {
7216 case 0:
7217 case -NFS4ERR_STALE_CLIENTID:
7218 clp->cl_exchange_flags = 0;
7219 }
7220out:
7221 return ret;
7222}
7223
Andy Adamson2050f0c2009-04-01 09:22:30 -04007224struct nfs4_get_lease_time_data {
7225 struct nfs4_get_lease_time_args *args;
7226 struct nfs4_get_lease_time_res *res;
7227 struct nfs_client *clp;
7228};
7229
7230static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7231 void *calldata)
7232{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007233 struct nfs4_get_lease_time_data *data =
7234 (struct nfs4_get_lease_time_data *)calldata;
7235
7236 dprintk("--> %s\n", __func__);
7237 /* just setup sequence, do not trigger session recovery
7238 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007239 nfs41_setup_sequence(data->clp->cl_session,
7240 &data->args->la_seq_args,
7241 &data->res->lr_seq_res,
7242 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007243 dprintk("<-- %s\n", __func__);
7244}
7245
7246/*
7247 * Called from nfs4_state_manager thread for session setup, so don't recover
7248 * from sequence operation or clientid errors.
7249 */
7250static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7251{
7252 struct nfs4_get_lease_time_data *data =
7253 (struct nfs4_get_lease_time_data *)calldata;
7254
7255 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007256 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7257 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007258 switch (task->tk_status) {
7259 case -NFS4ERR_DELAY:
7260 case -NFS4ERR_GRACE:
7261 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7262 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7263 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007264 /* fall through */
7265 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007266 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007267 return;
7268 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007269 dprintk("<-- %s\n", __func__);
7270}
7271
Trond Myklebust17280172012-03-11 13:11:00 -04007272static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007273 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7274 .rpc_call_done = nfs4_get_lease_time_done,
7275};
7276
7277int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7278{
7279 struct rpc_task *task;
7280 struct nfs4_get_lease_time_args args;
7281 struct nfs4_get_lease_time_res res = {
7282 .lr_fsinfo = fsinfo,
7283 };
7284 struct nfs4_get_lease_time_data data = {
7285 .args = &args,
7286 .res = &res,
7287 .clp = clp,
7288 };
7289 struct rpc_message msg = {
7290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7291 .rpc_argp = &args,
7292 .rpc_resp = &res,
7293 };
7294 struct rpc_task_setup task_setup = {
7295 .rpc_client = clp->cl_rpcclient,
7296 .rpc_message = &msg,
7297 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007298 .callback_data = &data,
7299 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007300 };
7301 int status;
7302
Chuck Levera9c92d62013-08-09 12:48:18 -04007303 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007304 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007305 dprintk("--> %s\n", __func__);
7306 task = rpc_run_task(&task_setup);
7307
7308 if (IS_ERR(task))
7309 status = PTR_ERR(task);
7310 else {
7311 status = task->tk_status;
7312 rpc_put_task(task);
7313 }
7314 dprintk("<-- %s return %d\n", __func__, status);
7315
7316 return status;
7317}
7318
Andy Adamsonfc931582009-04-01 09:22:31 -04007319/*
7320 * Initialize the values to be used by the client in CREATE_SESSION
7321 * If nfs4_init_session set the fore channel request and response sizes,
7322 * use them.
7323 *
7324 * Set the back channel max_resp_sz_cached to zero to force the client to
7325 * always set csa_cachethis to FALSE because the current implementation
7326 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7327 */
7328static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7329{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007330 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007331
Andy Adamson18aad3d2013-06-26 12:21:49 -04007332 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7333 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7334
Andy Adamsonfc931582009-04-01 09:22:31 -04007335 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007336 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7337 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007338 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007339 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007340
7341 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007342 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007343 __func__,
7344 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007345 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007346
7347 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007348 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7349 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7350 args->bc_attrs.max_resp_sz_cached = 0;
7351 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7352 args->bc_attrs.max_reqs = 1;
7353
7354 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7355 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7356 __func__,
7357 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7358 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7359 args->bc_attrs.max_reqs);
7360}
7361
Trond Myklebust79969dd2015-02-18 11:30:18 -08007362static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7363 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007364{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007365 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007366 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007367
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007368 if (rcvd->max_resp_sz > sent->max_resp_sz)
7369 return -EINVAL;
7370 /*
7371 * Our requested max_ops is the minimum we need; we're not
7372 * prepared to break up compounds into smaller pieces than that.
7373 * So, no point even trying to continue if the server won't
7374 * cooperate:
7375 */
7376 if (rcvd->max_ops < sent->max_ops)
7377 return -EINVAL;
7378 if (rcvd->max_reqs == 0)
7379 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007380 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7381 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007382 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007383}
7384
Trond Myklebust79969dd2015-02-18 11:30:18 -08007385static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7386 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007387{
7388 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007389 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007390
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007391 if (!(res->flags & SESSION4_BACK_CHAN))
7392 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007393 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7394 return -EINVAL;
7395 if (rcvd->max_resp_sz < sent->max_resp_sz)
7396 return -EINVAL;
7397 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7398 return -EINVAL;
7399 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007400 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007401 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007402 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007403 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007404out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007405 return 0;
7406}
Andy Adamson8d353012009-04-01 09:22:32 -04007407
Andy Adamson8d353012009-04-01 09:22:32 -04007408static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007409 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007410{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007411 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007412
Trond Myklebust79969dd2015-02-18 11:30:18 -08007413 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007414 if (ret)
7415 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007416 return nfs4_verify_back_channel_attrs(args, res);
7417}
7418
7419static void nfs4_update_session(struct nfs4_session *session,
7420 struct nfs41_create_session_res *res)
7421{
7422 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007423 /* Mark client id and session as being confirmed */
7424 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7425 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007426 session->flags = res->flags;
7427 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007428 if (res->flags & SESSION4_BACK_CHAN)
7429 memcpy(&session->bc_attrs, &res->bc_attrs,
7430 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007431}
7432
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007433static int _nfs4_proc_create_session(struct nfs_client *clp,
7434 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007435{
7436 struct nfs4_session *session = clp->cl_session;
7437 struct nfs41_create_session_args args = {
7438 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007439 .clientid = clp->cl_clientid,
7440 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007441 .cb_program = NFS4_CALLBACK,
7442 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007443 struct nfs41_create_session_res res;
7444
Andy Adamsonfc931582009-04-01 09:22:31 -04007445 struct rpc_message msg = {
7446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7447 .rpc_argp = &args,
7448 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007449 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007450 };
7451 int status;
7452
7453 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007454 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007455
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007456 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007457 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007458
Trond Myklebust43095d32012-11-20 11:13:12 -05007459 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007460 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007461 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007462 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007463 if (clp->cl_seqid == res.seqid)
7464 clp->cl_seqid++;
7465 if (status)
7466 goto out;
7467 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007468 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007469out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007470 return status;
7471}
7472
7473/*
7474 * Issues a CREATE_SESSION operation to the server.
7475 * It is the responsibility of the caller to verify the session is
7476 * expired before calling this routine.
7477 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007478int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007479{
7480 int status;
7481 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007482 struct nfs4_session *session = clp->cl_session;
7483
7484 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7485
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007486 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007487 if (status)
7488 goto out;
7489
Andy Adamsonaacd5532011-11-09 13:58:21 -05007490 /* Init or reset the session slot tables */
7491 status = nfs4_setup_session_slot_tables(session);
7492 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007493 if (status)
7494 goto out;
7495
7496 ptr = (unsigned *)&session->sess_id.data[0];
7497 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7498 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007499out:
7500 dprintk("<-- %s\n", __func__);
7501 return status;
7502}
7503
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007504/*
7505 * Issue the over-the-wire RPC DESTROY_SESSION.
7506 * The caller must serialize access to this routine.
7507 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007508int nfs4_proc_destroy_session(struct nfs4_session *session,
7509 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007510{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007511 struct rpc_message msg = {
7512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7513 .rpc_argp = session,
7514 .rpc_cred = cred,
7515 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007516 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007517
7518 dprintk("--> nfs4_proc_destroy_session\n");
7519
7520 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007521 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7522 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007523
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007524 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007525 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007526
7527 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007528 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007529 "Session has been destroyed regardless...\n", status);
7530
7531 dprintk("<-- nfs4_proc_destroy_session\n");
7532 return status;
7533}
7534
Trond Myklebust7b38c362012-05-23 13:23:31 -04007535/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007536 * Renew the cl_session lease.
7537 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007538struct nfs4_sequence_data {
7539 struct nfs_client *clp;
7540 struct nfs4_sequence_args args;
7541 struct nfs4_sequence_res res;
7542};
7543
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007544static void nfs41_sequence_release(void *data)
7545{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007546 struct nfs4_sequence_data *calldata = data;
7547 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007548
Alexandros Batsakis71358402010-02-05 03:45:05 -08007549 if (atomic_read(&clp->cl_count) > 1)
7550 nfs4_schedule_state_renewal(clp);
7551 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007552 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007553}
7554
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007555static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7556{
7557 switch(task->tk_status) {
7558 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007559 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7560 return -EAGAIN;
7561 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007562 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007563 }
7564 return 0;
7565}
7566
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007567static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007568{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007569 struct nfs4_sequence_data *calldata = data;
7570 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007571
Trond Myklebust14516c32010-07-31 14:29:06 -04007572 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7573 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007574
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007575 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007576 if (task->tk_status < 0) {
7577 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007578 if (atomic_read(&clp->cl_count) == 1)
7579 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007580
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007581 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7582 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007583 return;
7584 }
7585 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007586 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007587out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007588 dprintk("<-- %s\n", __func__);
7589}
7590
7591static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7592{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007593 struct nfs4_sequence_data *calldata = data;
7594 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007595 struct nfs4_sequence_args *args;
7596 struct nfs4_sequence_res *res;
7597
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007598 args = task->tk_msg.rpc_argp;
7599 res = task->tk_msg.rpc_resp;
7600
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007601 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007602}
7603
7604static const struct rpc_call_ops nfs41_sequence_ops = {
7605 .rpc_call_done = nfs41_sequence_call_done,
7606 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007607 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007608};
7609
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007610static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7611 struct rpc_cred *cred,
7612 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007613{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007614 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007615 struct rpc_message msg = {
7616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7617 .rpc_cred = cred,
7618 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007619 struct rpc_task_setup task_setup_data = {
7620 .rpc_client = clp->cl_rpcclient,
7621 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007622 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007623 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007624 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007625
Alexandros Batsakis71358402010-02-05 03:45:05 -08007626 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007627 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007628 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007629 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007630 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007631 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007632 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007633 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007634 if (is_privileged)
7635 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007636 msg.rpc_argp = &calldata->args;
7637 msg.rpc_resp = &calldata->res;
7638 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007639 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007640
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007641 return rpc_run_task(&task_setup_data);
7642}
7643
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007644static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007645{
7646 struct rpc_task *task;
7647 int ret = 0;
7648
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007649 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007650 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007651 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007652 if (IS_ERR(task))
7653 ret = PTR_ERR(task);
7654 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007655 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007656 dprintk("<-- %s status=%d\n", __func__, ret);
7657 return ret;
7658}
7659
7660static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7661{
7662 struct rpc_task *task;
7663 int ret;
7664
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007665 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007666 if (IS_ERR(task)) {
7667 ret = PTR_ERR(task);
7668 goto out;
7669 }
7670 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007671 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007672 ret = task->tk_status;
7673 rpc_put_task(task);
7674out:
7675 dprintk("<-- %s status=%d\n", __func__, ret);
7676 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007677}
7678
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007679struct nfs4_reclaim_complete_data {
7680 struct nfs_client *clp;
7681 struct nfs41_reclaim_complete_args arg;
7682 struct nfs41_reclaim_complete_res res;
7683};
7684
7685static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7686{
7687 struct nfs4_reclaim_complete_data *calldata = data;
7688
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007689 nfs41_setup_sequence(calldata->clp->cl_session,
7690 &calldata->arg.seq_args,
7691 &calldata->res.seq_res,
7692 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007693}
7694
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007695static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7696{
7697 switch(task->tk_status) {
7698 case 0:
7699 case -NFS4ERR_COMPLETE_ALREADY:
7700 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7701 break;
7702 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007703 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007704 /* fall through */
7705 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007706 return -EAGAIN;
7707 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007708 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007709 }
7710 return 0;
7711}
7712
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007713static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7714{
7715 struct nfs4_reclaim_complete_data *calldata = data;
7716 struct nfs_client *clp = calldata->clp;
7717 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7718
7719 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007720 if (!nfs41_sequence_done(task, res))
7721 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007722
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007723 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007724 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7725 rpc_restart_call_prepare(task);
7726 return;
7727 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007728 dprintk("<-- %s\n", __func__);
7729}
7730
7731static void nfs4_free_reclaim_complete_data(void *data)
7732{
7733 struct nfs4_reclaim_complete_data *calldata = data;
7734
7735 kfree(calldata);
7736}
7737
7738static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7739 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7740 .rpc_call_done = nfs4_reclaim_complete_done,
7741 .rpc_release = nfs4_free_reclaim_complete_data,
7742};
7743
7744/*
7745 * Issue a global reclaim complete.
7746 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007747static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7748 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007749{
7750 struct nfs4_reclaim_complete_data *calldata;
7751 struct rpc_task *task;
7752 struct rpc_message msg = {
7753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007754 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007755 };
7756 struct rpc_task_setup task_setup_data = {
7757 .rpc_client = clp->cl_rpcclient,
7758 .rpc_message = &msg,
7759 .callback_ops = &nfs4_reclaim_complete_call_ops,
7760 .flags = RPC_TASK_ASYNC,
7761 };
7762 int status = -ENOMEM;
7763
7764 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007765 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007766 if (calldata == NULL)
7767 goto out;
7768 calldata->clp = clp;
7769 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007770
Chuck Levera9c92d62013-08-09 12:48:18 -04007771 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007772 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007773 msg.rpc_argp = &calldata->arg;
7774 msg.rpc_resp = &calldata->res;
7775 task_setup_data.callback_data = calldata;
7776 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007777 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007778 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007779 goto out;
7780 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007781 status = nfs4_wait_for_completion_rpc_task(task);
7782 if (status == 0)
7783 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007784 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007785 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007786out:
7787 dprintk("<-- %s status=%d\n", __func__, status);
7788 return status;
7789}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007790
7791static void
7792nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7793{
7794 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007795 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007796 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007797
7798 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007799 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7800 * right now covering the LAYOUTGET we are about to send.
7801 * However, that is not so catastrophic, and there seems
7802 * to be no way to prevent it completely.
7803 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007804 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007805 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007806 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007807 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7808 NFS_I(lgp->args.inode)->layout,
Peng Taoce6ab4f2014-09-06 00:53:24 +08007809 &lgp->args.range,
Fred Isamancf7d63f2011-01-06 11:36:25 +00007810 lgp->args.ctx->state)) {
7811 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007812 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007813}
7814
7815static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7816{
7817 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007818 struct inode *inode = lgp->args.inode;
7819 struct nfs_server *server = NFS_SERVER(inode);
7820 struct pnfs_layout_hdr *lo;
7821 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007822 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007823
Boaz Harroshed7e5422014-01-22 20:34:54 +02007824 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007825
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007826 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007827 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007828
7829 switch (task->tk_status) {
7830 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007831 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007832 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07007833 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7834 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7835 */
7836 case -NFS4ERR_BADLAYOUT:
7837 goto out_overflow;
7838 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02007839 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07007840 * (or clients) writing to the same RAID stripe except when
7841 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Boaz Harroshed7e5422014-01-22 20:34:54 +02007842 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007843 case -NFS4ERR_LAYOUTTRYLATER:
Trond Myklebust21b874c2015-08-31 01:19:22 -07007844 if (lgp->args.minlength == 0)
7845 goto out_overflow;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007846 /*
7847 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7848 * existing layout before getting a new one).
7849 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007850 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007851 timeo = rpc_get_timeout(task->tk_client);
7852 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007853 now = jiffies;
7854 if (time_after(giveup, now)) {
7855 unsigned long delay;
7856
7857 /* Delay for:
7858 * - Not less then NFS4_POLL_RETRY_MIN.
7859 * - One last time a jiffie before we give up
7860 * - exponential backoff (time_now minus start_attempt)
7861 */
7862 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7863 min((giveup - now - 1),
7864 now - lgp->args.timestamp));
7865
7866 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7867 __func__, delay);
7868 rpc_delay(task, delay);
Trond Myklebust2259f962015-09-20 13:30:30 -04007869 /* Do not call nfs4_async_handle_error() */
7870 goto out_restart;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007871 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007872 break;
7873 case -NFS4ERR_EXPIRED:
7874 case -NFS4ERR_BAD_STATEID:
7875 spin_lock(&inode->i_lock);
Trond Myklebust2259f962015-09-20 13:30:30 -04007876 if (nfs4_stateid_match(&lgp->args.stateid,
7877 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007878 spin_unlock(&inode->i_lock);
7879 /* If the open stateid was bad, then recover it. */
7880 state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04007881 break;
7882 }
7883 lo = NFS_I(inode)->layout;
7884 if (lo && nfs4_stateid_match(&lgp->args.stateid,
7885 &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007886 LIST_HEAD(head);
7887
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007888 /*
7889 * Mark the bad layout state as invalid, then retry
7890 * with the current stateid.
7891 */
Kinglong Mee8714d462015-09-20 23:04:22 +08007892 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Trond Myklebustee314c22012-10-01 17:25:48 -07007893 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7894 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007895 pnfs_free_lseg_list(&head);
Trond Myklebust2259f962015-09-20 13:30:30 -04007896 } else
7897 spin_unlock(&inode->i_lock);
7898 goto out_restart;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007899 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007900 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebust2259f962015-09-20 13:30:30 -04007901 goto out_restart;
Trond Myklebustee314c22012-10-01 17:25:48 -07007902out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007903 dprintk("<-- %s\n", __func__);
Trond Myklebust21b874c2015-08-31 01:19:22 -07007904 return;
Trond Myklebust2259f962015-09-20 13:30:30 -04007905out_restart:
7906 task->tk_status = 0;
7907 rpc_restart_call_prepare(task);
7908 return;
Trond Myklebust21b874c2015-08-31 01:19:22 -07007909out_overflow:
7910 task->tk_status = -EOVERFLOW;
7911 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007912}
7913
Idan Kedar85541162012-08-02 11:47:10 +03007914static size_t max_response_pages(struct nfs_server *server)
7915{
7916 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7917 return nfs_page_array_len(0, max_resp_sz);
7918}
7919
7920static void nfs4_free_pages(struct page **pages, size_t size)
7921{
7922 int i;
7923
7924 if (!pages)
7925 return;
7926
7927 for (i = 0; i < size; i++) {
7928 if (!pages[i])
7929 break;
7930 __free_page(pages[i]);
7931 }
7932 kfree(pages);
7933}
7934
7935static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7936{
7937 struct page **pages;
7938 int i;
7939
7940 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7941 if (!pages) {
7942 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7943 return NULL;
7944 }
7945
7946 for (i = 0; i < size; i++) {
7947 pages[i] = alloc_page(gfp_flags);
7948 if (!pages[i]) {
7949 dprintk("%s: failed to allocate page\n", __func__);
7950 nfs4_free_pages(pages, size);
7951 return NULL;
7952 }
7953 }
7954
7955 return pages;
7956}
7957
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007958static void nfs4_layoutget_release(void *calldata)
7959{
7960 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007961 struct inode *inode = lgp->args.inode;
7962 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007963 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007964
7965 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007966 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007967 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007968 put_nfs_open_context(lgp->args.ctx);
7969 kfree(calldata);
7970 dprintk("<-- %s\n", __func__);
7971}
7972
7973static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7974 .rpc_call_prepare = nfs4_layoutget_prepare,
7975 .rpc_call_done = nfs4_layoutget_done,
7976 .rpc_release = nfs4_layoutget_release,
7977};
7978
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007979struct pnfs_layout_segment *
7980nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007981{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007982 struct inode *inode = lgp->args.inode;
7983 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007984 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007985 struct rpc_task *task;
7986 struct rpc_message msg = {
7987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7988 .rpc_argp = &lgp->args,
7989 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007990 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007991 };
7992 struct rpc_task_setup task_setup_data = {
7993 .rpc_client = server->client,
7994 .rpc_message = &msg,
7995 .callback_ops = &nfs4_layoutget_call_ops,
7996 .callback_data = lgp,
7997 .flags = RPC_TASK_ASYNC,
7998 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007999 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008000 int status = 0;
8001
8002 dprintk("--> %s\n", __func__);
8003
Peng Tao4bd5a982014-11-17 11:05:17 +08008004 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8005 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8006
Idan Kedar85541162012-08-02 11:47:10 +03008007 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8008 if (!lgp->args.layout.pages) {
8009 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008010 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008011 }
8012 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05008013 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03008014
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008015 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008016 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008017 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008018
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008019 task = rpc_run_task(&task_setup_data);
8020 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008021 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008022 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00008023 if (status == 0)
8024 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008025 trace_nfs4_layoutget(lgp->args.ctx,
8026 &lgp->args.range,
8027 &lgp->res.range,
8028 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008029 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8030 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008031 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008032 rpc_put_task(task);
8033 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008034 if (status)
8035 return ERR_PTR(status);
8036 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008037}
8038
Benny Halevycbe82602011-05-22 19:52:37 +03008039static void
8040nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8041{
8042 struct nfs4_layoutreturn *lrp = calldata;
8043
8044 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008045 nfs41_setup_sequence(lrp->clp->cl_session,
8046 &lrp->args.seq_args,
8047 &lrp->res.seq_res,
8048 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008049}
8050
8051static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8052{
8053 struct nfs4_layoutreturn *lrp = calldata;
8054 struct nfs_server *server;
8055
8056 dprintk("--> %s\n", __func__);
8057
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008058 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008059 return;
8060
8061 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008062 switch (task->tk_status) {
8063 default:
8064 task->tk_status = 0;
8065 case 0:
8066 break;
8067 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008068 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008069 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008070 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008071 return;
8072 }
Benny Halevycbe82602011-05-22 19:52:37 +03008073 dprintk("<-- %s\n", __func__);
8074}
8075
8076static void nfs4_layoutreturn_release(void *calldata)
8077{
8078 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008079 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008080 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008081
8082 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008083 spin_lock(&lo->plh_inode->i_lock);
8084 if (lrp->res.lrs_present)
8085 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008086 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
Tom Haynesd67ae822014-12-11 17:02:04 -05008087 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008088 lo->plh_block_lgets--;
8089 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008090 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008091 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008092 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008093 kfree(calldata);
8094 dprintk("<-- %s\n", __func__);
8095}
8096
8097static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8098 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8099 .rpc_call_done = nfs4_layoutreturn_done,
8100 .rpc_release = nfs4_layoutreturn_release,
8101};
8102
Peng Tao6c166052014-11-17 09:30:40 +08008103int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008104{
8105 struct rpc_task *task;
8106 struct rpc_message msg = {
8107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8108 .rpc_argp = &lrp->args,
8109 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008110 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008111 };
8112 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008113 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008114 .rpc_message = &msg,
8115 .callback_ops = &nfs4_layoutreturn_call_ops,
8116 .callback_data = lrp,
8117 };
Peng Tao6c166052014-11-17 09:30:40 +08008118 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008119
8120 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008121 if (!sync) {
8122 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8123 if (!lrp->inode) {
8124 nfs4_layoutreturn_release(lrp);
8125 return -EAGAIN;
8126 }
8127 task_setup_data.flags |= RPC_TASK_ASYNC;
8128 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008129 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008130 task = rpc_run_task(&task_setup_data);
8131 if (IS_ERR(task))
8132 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008133 if (sync)
8134 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008135 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008136 dprintk("<-- %s status=%d\n", __func__, status);
8137 rpc_put_task(task);
8138 return status;
8139}
8140
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008141static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008142_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8143 struct pnfs_device *pdev,
8144 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008145{
8146 struct nfs4_getdeviceinfo_args args = {
8147 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008148 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8149 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008150 };
8151 struct nfs4_getdeviceinfo_res res = {
8152 .pdev = pdev,
8153 };
8154 struct rpc_message msg = {
8155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8156 .rpc_argp = &args,
8157 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008158 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008159 };
8160 int status;
8161
8162 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008163 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008164 if (res.notification & ~args.notify_types)
8165 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008166 if (res.notification != args.notify_types)
8167 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008168
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008169 dprintk("<-- %s status=%d\n", __func__, status);
8170
8171 return status;
8172}
8173
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008174int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8175 struct pnfs_device *pdev,
8176 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008177{
8178 struct nfs4_exception exception = { };
8179 int err;
8180
8181 do {
8182 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008183 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008184 &exception);
8185 } while (exception.retry);
8186 return err;
8187}
8188EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8189
Andy Adamson863a3c62011-03-23 13:27:54 +00008190static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8191{
8192 struct nfs4_layoutcommit_data *data = calldata;
8193 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008194 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008195
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008196 nfs41_setup_sequence(session,
8197 &data->args.seq_args,
8198 &data->res.seq_res,
8199 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008200}
8201
8202static void
8203nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8204{
8205 struct nfs4_layoutcommit_data *data = calldata;
8206 struct nfs_server *server = NFS_SERVER(data->args.inode);
8207
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008208 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008209 return;
8210
8211 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008212 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8213 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8214 case -NFS4ERR_BADLAYOUT: /* no layout */
8215 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008216 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008217 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008218 break;
8219 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008220 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008221 rpc_restart_call_prepare(task);
8222 return;
8223 }
8224 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008225}
8226
8227static void nfs4_layoutcommit_release(void *calldata)
8228{
8229 struct nfs4_layoutcommit_data *data = calldata;
8230
Andy Adamsondb29c082011-07-30 20:52:38 -04008231 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008232 nfs_post_op_update_inode_force_wcc(data->args.inode,
8233 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008234 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008235 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008236 kfree(data);
8237}
8238
8239static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8240 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8241 .rpc_call_done = nfs4_layoutcommit_done,
8242 .rpc_release = nfs4_layoutcommit_release,
8243};
8244
8245int
Andy Adamsonef311532011-03-12 02:58:10 -05008246nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008247{
8248 struct rpc_message msg = {
8249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8250 .rpc_argp = &data->args,
8251 .rpc_resp = &data->res,
8252 .rpc_cred = data->cred,
8253 };
8254 struct rpc_task_setup task_setup_data = {
8255 .task = &data->task,
8256 .rpc_client = NFS_CLIENT(data->args.inode),
8257 .rpc_message = &msg,
8258 .callback_ops = &nfs4_layoutcommit_ops,
8259 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008260 };
8261 struct rpc_task *task;
8262 int status = 0;
8263
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008264 dprintk("NFS: initiating layoutcommit call. sync %d "
8265 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008266 data->args.lastbytewritten,
8267 data->args.inode->i_ino);
8268
Trond Myklebust472e2592015-02-05 16:50:30 -05008269 if (!sync) {
8270 data->inode = nfs_igrab_and_active(data->args.inode);
8271 if (data->inode == NULL) {
8272 nfs4_layoutcommit_release(data);
8273 return -EAGAIN;
8274 }
8275 task_setup_data.flags = RPC_TASK_ASYNC;
8276 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008277 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008278 task = rpc_run_task(&task_setup_data);
8279 if (IS_ERR(task))
8280 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008281 if (sync)
8282 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008283 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008284 dprintk("%s: status %d\n", __func__, status);
8285 rpc_put_task(task);
8286 return status;
8287}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008288
Andy Adamson97431202013-08-08 10:57:56 -04008289/**
8290 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8291 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8292 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008293static int
8294_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008295 struct nfs_fsinfo *info,
8296 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008297{
8298 struct nfs41_secinfo_no_name_args args = {
8299 .style = SECINFO_STYLE_CURRENT_FH,
8300 };
8301 struct nfs4_secinfo_res res = {
8302 .flavors = flavors,
8303 };
8304 struct rpc_message msg = {
8305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8306 .rpc_argp = &args,
8307 .rpc_resp = &res,
8308 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008309 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008310 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008311 int status;
8312
8313 if (use_integrity) {
8314 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008315 cred = nfs4_get_clid_cred(server->nfs_client);
8316 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008317 }
8318
8319 dprintk("--> %s\n", __func__);
8320 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8321 &res.seq_res, 0);
8322 dprintk("<-- %s status=%d\n", __func__, status);
8323
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008324 if (cred)
8325 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008326
8327 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008328}
8329
8330static int
8331nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8332 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8333{
8334 struct nfs4_exception exception = { };
8335 int err;
8336 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008337 /* first try using integrity protection */
8338 err = -NFS4ERR_WRONGSEC;
8339
8340 /* try to use integrity protection with machine cred */
8341 if (_nfs4_is_integrity_protected(server->nfs_client))
8342 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8343 flavors, true);
8344
8345 /*
8346 * if unable to use integrity protection, or SECINFO with
8347 * integrity protection returns NFS4ERR_WRONGSEC (which is
8348 * disallowed by spec, but exists in deployed servers) use
8349 * the current filesystem's rpc_client and the user cred.
8350 */
8351 if (err == -NFS4ERR_WRONGSEC)
8352 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8353 flavors, false);
8354
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008355 switch (err) {
8356 case 0:
8357 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008358 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008359 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008360 default:
8361 err = nfs4_handle_exception(server, err, &exception);
8362 }
8363 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008364out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008365 return err;
8366}
8367
8368static int
8369nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8370 struct nfs_fsinfo *info)
8371{
8372 int err;
8373 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008374 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008375 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008376 struct nfs4_secinfo4 *secinfo;
8377 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008378
8379 page = alloc_page(GFP_KERNEL);
8380 if (!page) {
8381 err = -ENOMEM;
8382 goto out;
8383 }
8384
8385 flavors = page_address(page);
8386 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8387
8388 /*
8389 * Fall back on "guess and check" method if
8390 * the server doesn't support SECINFO_NO_NAME
8391 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008392 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008393 err = nfs4_find_root_sec(server, fhandle, info);
8394 goto out_freepage;
8395 }
8396 if (err)
8397 goto out_freepage;
8398
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008399 for (i = 0; i < flavors->num_flavors; i++) {
8400 secinfo = &flavors->flavors[i];
8401
8402 switch (secinfo->flavor) {
8403 case RPC_AUTH_NULL:
8404 case RPC_AUTH_UNIX:
8405 case RPC_AUTH_GSS:
8406 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8407 &secinfo->flavor_info);
8408 break;
8409 default:
8410 flavor = RPC_AUTH_MAXFLAVOR;
8411 break;
8412 }
8413
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008414 if (!nfs_auth_info_match(&server->auth_info, flavor))
8415 flavor = RPC_AUTH_MAXFLAVOR;
8416
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008417 if (flavor != RPC_AUTH_MAXFLAVOR) {
8418 err = nfs4_lookup_root_sec(server, fhandle,
8419 info, flavor);
8420 if (!err)
8421 break;
8422 }
8423 }
8424
8425 if (flavor == RPC_AUTH_MAXFLAVOR)
8426 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008427
8428out_freepage:
8429 put_page(page);
8430 if (err == -EACCES)
8431 return -EPERM;
8432out:
8433 return err;
8434}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008435
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008436static int _nfs41_test_stateid(struct nfs_server *server,
8437 nfs4_stateid *stateid,
8438 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008439{
8440 int status;
8441 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008442 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008443 };
8444 struct nfs41_test_stateid_res res;
8445 struct rpc_message msg = {
8446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8447 .rpc_argp = &args,
8448 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008449 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008450 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008451 struct rpc_clnt *rpc_client = server->client;
8452
8453 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8454 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008455
Chuck Lever38527b12012-07-11 16:30:23 -04008456 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008457 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008458 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008459 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008460 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008461 if (status != NFS_OK) {
8462 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008463 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008464 }
8465 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008466 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008467}
8468
Chuck Lever38527b12012-07-11 16:30:23 -04008469/**
8470 * nfs41_test_stateid - perform a TEST_STATEID operation
8471 *
8472 * @server: server / transport on which to perform the operation
8473 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008474 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008475 *
8476 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8477 * Otherwise a negative NFS4ERR value is returned if the operation
8478 * failed or the state ID is not currently valid.
8479 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008480static int nfs41_test_stateid(struct nfs_server *server,
8481 nfs4_stateid *stateid,
8482 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008483{
8484 struct nfs4_exception exception = { };
8485 int err;
8486 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008487 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008488 if (err != -NFS4ERR_DELAY)
8489 break;
8490 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008491 } while (exception.retry);
8492 return err;
8493}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008494
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008495struct nfs_free_stateid_data {
8496 struct nfs_server *server;
8497 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008498 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008499};
8500
8501static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8502{
8503 struct nfs_free_stateid_data *data = calldata;
8504 nfs41_setup_sequence(nfs4_get_session(data->server),
8505 &data->args.seq_args,
8506 &data->res.seq_res,
8507 task);
8508}
8509
8510static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8511{
8512 struct nfs_free_stateid_data *data = calldata;
8513
8514 nfs41_sequence_done(task, &data->res.seq_res);
8515
8516 switch (task->tk_status) {
8517 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008518 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008519 rpc_restart_call_prepare(task);
8520 }
8521}
8522
8523static void nfs41_free_stateid_release(void *calldata)
8524{
8525 kfree(calldata);
8526}
8527
Trond Myklebust17f26b12013-08-21 15:48:42 -04008528static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008529 .rpc_call_prepare = nfs41_free_stateid_prepare,
8530 .rpc_call_done = nfs41_free_stateid_done,
8531 .rpc_release = nfs41_free_stateid_release,
8532};
8533
8534static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8535 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008536 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008537 bool privileged)
8538{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008539 struct rpc_message msg = {
8540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008541 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008542 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008543 struct rpc_task_setup task_setup = {
8544 .rpc_client = server->client,
8545 .rpc_message = &msg,
8546 .callback_ops = &nfs41_free_stateid_ops,
8547 .flags = RPC_TASK_ASYNC,
8548 };
8549 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008550
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008551 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8552 &task_setup.rpc_client, &msg);
8553
Chuck Lever38527b12012-07-11 16:30:23 -04008554 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008555 data = kmalloc(sizeof(*data), GFP_NOFS);
8556 if (!data)
8557 return ERR_PTR(-ENOMEM);
8558 data->server = server;
8559 nfs4_stateid_copy(&data->args.stateid, stateid);
8560
8561 task_setup.callback_data = data;
8562
8563 msg.rpc_argp = &data->args;
8564 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008565 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008566 if (privileged)
8567 nfs4_set_sequence_privileged(&data->args.seq_args);
8568
8569 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008570}
8571
Chuck Lever38527b12012-07-11 16:30:23 -04008572/**
8573 * nfs41_free_stateid - perform a FREE_STATEID operation
8574 *
8575 * @server: server / transport on which to perform the operation
8576 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008577 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008578 *
8579 * Returns NFS_OK if the server freed "stateid". Otherwise a
8580 * negative NFS4ERR value is returned.
8581 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008582static int nfs41_free_stateid(struct nfs_server *server,
8583 nfs4_stateid *stateid,
8584 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008585{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008586 struct rpc_task *task;
8587 int ret;
8588
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008589 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008590 if (IS_ERR(task))
8591 return PTR_ERR(task);
8592 ret = rpc_wait_for_completion_task(task);
8593 if (!ret)
8594 ret = task->tk_status;
8595 rpc_put_task(task);
8596 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008597}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008598
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008599static void
8600nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008601{
8602 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008603 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008604
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008605 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008606 nfs4_free_lock_state(server, lsp);
8607 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008608 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008609 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008610}
8611
Trond Myklebust36281ca2012-03-04 18:13:56 -05008612static bool nfs41_match_stateid(const nfs4_stateid *s1,
8613 const nfs4_stateid *s2)
8614{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008615 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008616 return false;
8617
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008618 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008619 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008620 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008621 return true;
8622
8623 return false;
8624}
8625
Andy Adamson557134a2009-04-01 09:21:53 -04008626#endif /* CONFIG_NFS_V4_1 */
8627
Trond Myklebust36281ca2012-03-04 18:13:56 -05008628static bool nfs4_match_stateid(const nfs4_stateid *s1,
8629 const nfs4_stateid *s2)
8630{
Trond Myklebustf597c532012-03-04 18:13:56 -05008631 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008632}
8633
8634
Trond Myklebust17280172012-03-11 13:11:00 -04008635static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008636 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008637 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638 .recover_open = nfs4_open_reclaim,
8639 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008640 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008641 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642};
8643
Andy Adamson591d71c2009-04-01 09:22:47 -04008644#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008645static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008646 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8647 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8648 .recover_open = nfs4_open_reclaim,
8649 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008650 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008651 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008652 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008653};
8654#endif /* CONFIG_NFS_V4_1 */
8655
Trond Myklebust17280172012-03-11 13:11:00 -04008656static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008657 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008658 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008659 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008661 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008662};
8663
Andy Adamson591d71c2009-04-01 09:22:47 -04008664#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008665static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008666 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8667 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008668 .recover_open = nfs41_open_expired,
8669 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008670 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008671};
8672#endif /* CONFIG_NFS_V4_1 */
8673
Trond Myklebust17280172012-03-11 13:11:00 -04008674static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008675 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008676 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008677 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008678};
8679
8680#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008681static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008682 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008683 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008684 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008685};
8686#endif
8687
Chuck Leverec011fe2013-10-17 14:12:39 -04008688static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008689 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008690 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008691};
8692
8693#if defined(CONFIG_NFS_V4_1)
8694static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008695 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008696 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008697};
8698#endif /* CONFIG_NFS_V4_1 */
8699
Trond Myklebust97dc1352010-06-16 09:52:26 -04008700static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8701 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008702 .init_caps = NFS_CAP_READDIRPLUS
8703 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008704 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008705 .init_client = nfs40_init_client,
8706 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008707 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008708 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008709 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008710 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008711 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008712 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8713 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8714 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008715 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008716};
8717
8718#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008719static struct nfs_seqid *
8720nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8721{
8722 return NULL;
8723}
8724
Trond Myklebust97dc1352010-06-16 09:52:26 -04008725static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8726 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008727 .init_caps = NFS_CAP_READDIRPLUS
8728 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008729 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008730 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008731 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008732 .init_client = nfs41_init_client,
8733 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008734 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008735 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008736 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008737 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008738 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008739 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8740 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8741 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008742 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008743};
8744#endif
8745
Steve Dickson42c2c422013-05-22 12:50:38 -04008746#if defined(CONFIG_NFS_V4_2)
8747static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8748 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008749 .init_caps = NFS_CAP_READDIRPLUS
8750 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008751 | NFS_CAP_POSIX_LOCK
8752 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008753 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008754 | NFS_CAP_ALLOCATE
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008755 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008756 | NFS_CAP_SEEK
8757 | NFS_CAP_LAYOUTSTATS,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008758 .init_client = nfs41_init_client,
8759 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008760 .match_stateid = nfs41_match_stateid,
8761 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008762 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008763 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008764 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008765 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8766 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8767 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08008768 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008769};
8770#endif
8771
Trond Myklebust97dc1352010-06-16 09:52:26 -04008772const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8773 [0] = &nfs_v4_0_minor_ops,
8774#if defined(CONFIG_NFS_V4_1)
8775 [1] = &nfs_v4_1_minor_ops,
8776#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008777#if defined(CONFIG_NFS_V4_2)
8778 [2] = &nfs_v4_2_minor_ops,
8779#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008780};
8781
Trond Myklebust17f26b12013-08-21 15:48:42 -04008782static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008783 .create = nfs_create,
8784 .lookup = nfs_lookup,
8785 .atomic_open = nfs_atomic_open,
8786 .link = nfs_link,
8787 .unlink = nfs_unlink,
8788 .symlink = nfs_symlink,
8789 .mkdir = nfs_mkdir,
8790 .rmdir = nfs_rmdir,
8791 .mknod = nfs_mknod,
8792 .rename = nfs_rename,
8793 .permission = nfs_permission,
8794 .getattr = nfs_getattr,
8795 .setattr = nfs_setattr,
8796 .getxattr = generic_getxattr,
8797 .setxattr = generic_setxattr,
8798 .listxattr = generic_listxattr,
8799 .removexattr = generic_removexattr,
8800};
8801
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008802static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008803 .permission = nfs_permission,
8804 .getattr = nfs_getattr,
8805 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008806 .getxattr = generic_getxattr,
8807 .setxattr = generic_setxattr,
8808 .listxattr = generic_listxattr,
8809 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008810};
8811
David Howells509de812006-08-22 20:06:11 -04008812const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008813 .version = 4, /* protocol version */
8814 .dentry_ops = &nfs4_dentry_operations,
8815 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008816 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008817 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008818 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008819 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008820 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821 .getattr = nfs4_proc_getattr,
8822 .setattr = nfs4_proc_setattr,
8823 .lookup = nfs4_proc_lookup,
8824 .access = nfs4_proc_access,
8825 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008826 .create = nfs4_proc_create,
8827 .remove = nfs4_proc_remove,
8828 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008829 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008830 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008831 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008832 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008833 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834 .link = nfs4_proc_link,
8835 .symlink = nfs4_proc_symlink,
8836 .mkdir = nfs4_proc_mkdir,
8837 .rmdir = nfs4_proc_remove,
8838 .readdir = nfs4_proc_readdir,
8839 .mknod = nfs4_proc_mknod,
8840 .statfs = nfs4_proc_statfs,
8841 .fsinfo = nfs4_proc_fsinfo,
8842 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008843 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008845 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008847 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008848 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008849 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008851 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008852 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008854 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008855 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008856 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008857 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008858 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008859 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008860 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008861 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008862 .create_server = nfs4_create_server,
8863 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008864};
8865
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008866static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8867 .prefix = XATTR_NAME_NFSV4_ACL,
8868 .list = nfs4_xattr_list_nfs4_acl,
8869 .get = nfs4_xattr_get_nfs4_acl,
8870 .set = nfs4_xattr_set_nfs4_acl,
8871};
8872
8873const struct xattr_handler *nfs4_xattr_handlers[] = {
8874 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008875#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8876 &nfs4_xattr_nfs4_label_handler,
8877#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008878 NULL
8879};
8880
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881/*
8882 * Local variables:
8883 * c-basic-offset: 8
8884 * End:
8885 */