blob: 8814fbe626236fdfc21fb35de1d74e5757a82204 [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
Peng Tao2a92ee92015-09-26 02:24:37 +0800242 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243};
244
David Quigleya09df2c2013-05-22 12:50:41 -0400245const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400246 FATTR4_WORD0_TYPE
247 | FATTR4_WORD0_CHANGE
248 | FATTR4_WORD0_SIZE
249 | FATTR4_WORD0_FSID
250 | FATTR4_WORD0_FILEID
251 | FATTR4_WORD0_FS_LOCATIONS,
252 FATTR4_WORD1_MODE
253 | FATTR4_WORD1_NUMLINKS
254 | FATTR4_WORD1_OWNER
255 | FATTR4_WORD1_OWNER_GROUP
256 | FATTR4_WORD1_RAWDEV
257 | FATTR4_WORD1_SPACE_USED
258 | FATTR4_WORD1_TIME_ACCESS
259 | FATTR4_WORD1_TIME_METADATA
260 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400261 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400262};
263
Al Virobc4785c2006-10-19 23:28:51 -0700264static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct nfs4_readdir_arg *readdir)
266{
Al Viro0dbb4c62006-10-19 23:28:49 -0700267 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000270 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
272 return;
273 }
274
275 readdir->cookie = 0;
276 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
277 if (cookie == 2)
278 return;
279
280 /*
281 * NFSv4 servers do not return entries for '.' and '..'
282 * Therefore, we fake these entries here. We let '.'
283 * have cookie 0 and '..' have cookie 1. Note that
284 * when talking to the server, we always send cookie 0
285 * instead of 1 or 2.
286 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800287 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 if (cookie == 0) {
290 *p++ = xdr_one; /* next */
291 *p++ = xdr_zero; /* cookie, first word */
292 *p++ = xdr_one; /* cookie, second word */
293 *p++ = xdr_one; /* entry len */
294 memcpy(p, ".\0\0\0", 4); /* entry */
295 p++;
296 *p++ = xdr_one; /* bitmap length */
297 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
298 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000299 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
302 *p++ = xdr_one; /* next */
303 *p++ = xdr_zero; /* cookie, first word */
304 *p++ = xdr_two; /* cookie, second word */
305 *p++ = xdr_two; /* entry len */
306 memcpy(p, "..\0\0", 4); /* entry */
307 p++;
308 *p++ = xdr_one; /* bitmap length */
309 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
310 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000311 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 readdir->pgbase = (char *)p - (char *)start;
314 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800315 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
NeilBrown8478eaa2014-09-18 16:09:27 +1000318static long nfs4_update_delay(long *timeout)
319{
320 long ret;
321 if (!timeout)
322 return NFS4_POLL_RETRY_MAX;
323 if (*timeout <= 0)
324 *timeout = NFS4_POLL_RETRY_MIN;
325 if (*timeout > NFS4_POLL_RETRY_MAX)
326 *timeout = NFS4_POLL_RETRY_MAX;
327 ret = *timeout;
328 *timeout <<= 1;
329 return ret;
330}
331
Trond Myklebust65de8722008-12-23 15:21:44 -0500332static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
333{
334 int res = 0;
335
336 might_sleep();
337
NeilBrown8478eaa2014-09-18 16:09:27 +1000338 freezable_schedule_timeout_killable_unsafe(
339 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500340 if (fatal_signal_pending(current))
341 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500342 return res;
343}
344
345/* This is the error handling routine for processes that are allowed
346 * to sleep.
347 */
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500348int nfs4_handle_exception(struct nfs_server *server, 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
355 exception->retry = 0;
356 switch(errorcode) {
357 case 0:
358 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500359 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400360 case -NFS4ERR_DELEG_REVOKED:
361 case -NFS4ERR_ADMIN_REVOKED:
362 case -NFS4ERR_BAD_STATEID:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400363 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400364 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500365 exception->retry = 1;
366 return 0;
367 }
368 if (state == NULL)
369 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400370 ret = nfs4_schedule_stateid_recovery(server, state);
371 if (ret < 0)
372 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500373 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400374 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400375 if (state != NULL) {
376 ret = nfs4_schedule_stateid_recovery(server, state);
377 if (ret < 0)
378 break;
379 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500381 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500382 nfs4_schedule_lease_recovery(clp);
383 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400384 case -NFS4ERR_MOVED:
385 ret = nfs4_schedule_migration_recovery(server);
386 if (ret < 0)
387 break;
388 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400389 case -NFS4ERR_LEASE_MOVED:
390 nfs4_schedule_lease_moved_recovery(clp);
391 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500392#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
401 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400403 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500404#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
408 * fail
409 */
410 ret = -EBUSY;
411 break;
412 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_GRACE:
414 case -NFS4ERR_DELAY:
415 ret = nfs4_delay(server->client, &exception->timeout);
416 if (ret != 0)
417 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400418 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500419 case -NFS4ERR_OLD_STATEID:
420 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800421 break;
422 case -NFS4ERR_BADOWNER:
423 /* The following works around a Linux server bug! */
424 case -NFS4ERR_BADNAME:
425 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
426 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
427 exception->retry = 1;
428 printk(KERN_WARNING "NFS: v4 server %s "
429 "does not accept raw "
430 "uid/gids. "
431 "Reenabling the idmapper.\n",
432 server->nfs_client->cl_hostname);
433 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500434 }
435 /* We failed to handle the error */
436 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500437wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500438 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400439 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
440 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500441 if (ret == 0)
442 exception->retry = 1;
443 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500444}
445
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400446/*
447 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
448 * or 'false' otherwise.
449 */
450static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
451{
452 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
453
454 if (flavor == RPC_AUTH_GSS_KRB5I ||
455 flavor == RPC_AUTH_GSS_KRB5P)
456 return true;
457
458 return false;
459}
Trond Myklebust65de8722008-12-23 15:21:44 -0500460
Trond Myklebust452e9352010-07-31 14:29:06 -0400461static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 spin_lock(&clp->cl_lock);
464 if (time_before(clp->cl_last_renewal,timestamp))
465 clp->cl_last_renewal = timestamp;
466 spin_unlock(&clp->cl_lock);
467}
468
Trond Myklebust452e9352010-07-31 14:29:06 -0400469static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
470{
Trond Myklebustbe824162015-07-05 14:50:46 -0400471 struct nfs_client *clp = server->nfs_client;
472
473 if (!nfs4_has_session(clp))
474 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400475}
476
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400477struct nfs4_call_sync_data {
478 const struct nfs_server *seq_server;
479 struct nfs4_sequence_args *seq_args;
480 struct nfs4_sequence_res *seq_res;
481};
482
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800483void nfs4_init_sequence(struct nfs4_sequence_args *args,
484 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400485{
486 args->sa_slot = NULL;
487 args->sa_cache_this = cache_reply;
488 args->sa_privileged = 0;
489
490 res->sr_slot = NULL;
491}
492
493static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
494{
495 args->sa_privileged = 1;
496}
497
Peng Taocb04ad22014-06-11 05:24:15 +0800498int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
499 struct nfs4_sequence_args *args,
500 struct nfs4_sequence_res *res,
501 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400502{
Chuck Lever3bd23842013-08-09 12:49:19 -0400503 struct nfs4_slot *slot;
504
505 /* slot already allocated? */
506 if (res->sr_slot != NULL)
507 goto out_start;
508
509 spin_lock(&tbl->slot_tbl_lock);
510 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
511 goto out_sleep;
512
513 slot = nfs4_alloc_slot(tbl);
514 if (IS_ERR(slot)) {
515 if (slot == ERR_PTR(-ENOMEM))
516 task->tk_timeout = HZ >> 2;
517 goto out_sleep;
518 }
519 spin_unlock(&tbl->slot_tbl_lock);
520
521 args->sa_slot = slot;
522 res->sr_slot = slot;
523
524out_start:
525 rpc_call_start(task);
526 return 0;
527
528out_sleep:
529 if (args->sa_privileged)
530 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
531 NULL, RPC_PRIORITY_PRIVILEGED);
532 else
533 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
534 spin_unlock(&tbl->slot_tbl_lock);
535 return -EAGAIN;
536}
Peng Taocb04ad22014-06-11 05:24:15 +0800537EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400538
539static int nfs40_sequence_done(struct rpc_task *task,
540 struct nfs4_sequence_res *res)
541{
542 struct nfs4_slot *slot = res->sr_slot;
543 struct nfs4_slot_table *tbl;
544
Trond Myklebustcab92c12014-01-29 12:12:15 -0500545 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400546 goto out;
547
548 tbl = slot->table;
549 spin_lock(&tbl->slot_tbl_lock);
550 if (!nfs41_wake_and_assign_slot(tbl, slot))
551 nfs4_free_slot(tbl, slot);
552 spin_unlock(&tbl->slot_tbl_lock);
553
554 res->sr_slot = NULL;
555out:
556 return 1;
557}
558
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400559#if defined(CONFIG_NFS_V4_1)
560
Trond Myklebustd185a332010-06-16 09:52:25 -0400561static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400562{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500563 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400564 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500565 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500566 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400567
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500568 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500569 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500570
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500571 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500572 /* Be nice to the server: try to ensure that the last transmitted
573 * value for highest_user_slotid <= target_highest_slotid
574 */
575 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
576 send_new_highest_used_slotid = true;
577
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500578 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500579 send_new_highest_used_slotid = false;
580 goto out_unlock;
581 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500582 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500583
584 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
585 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500586out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500587 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400588 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500589 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400590 nfs41_notify_server(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400591}
592
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500593int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400594{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500595 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500596 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400597 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500598 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500599 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400600
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500601 if (slot == NULL)
602 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400603 /* don't increment the sequence number if the task wasn't sent */
604 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400605 goto out;
606
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500607 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500608
Trond Myklebustac20d162012-12-15 15:36:07 -0500609 if (slot->interrupted) {
610 slot->interrupted = 0;
611 interrupted = true;
612 }
613
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400614 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500615 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400616 switch (res->sr_status) {
617 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400618 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500619 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500620 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500621 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500622 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400623 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500624 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400625 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500626 case 1:
627 /*
628 * sr_status remains 1 if an RPC level error occurred.
629 * The server may or may not have processed the sequence
630 * operation..
631 * Mark the slot as having hosted an interrupted RPC call.
632 */
633 slot->interrupted = 1;
634 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400635 case -NFS4ERR_DELAY:
636 /* The server detected a resend of the RPC call and
637 * returned NFS4ERR_DELAY as per Section 2.10.6.2
638 * of RFC5661.
639 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500640 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400641 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500642 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500643 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400644 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500645 case -NFS4ERR_BADSLOT:
646 /*
647 * The slot id we used was probably retired. Try again
648 * using a different slot id.
649 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500650 goto retry_nowait;
651 case -NFS4ERR_SEQ_MISORDERED:
652 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500653 * Was the last operation on this sequence interrupted?
654 * If so, retry after bumping the sequence number.
655 */
656 if (interrupted) {
657 ++slot->seq_nr;
658 goto retry_nowait;
659 }
660 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500661 * Could this slot have been previously retired?
662 * If so, then the server may be expecting seq_nr = 1!
663 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500664 if (slot->seq_nr != 1) {
665 slot->seq_nr = 1;
666 goto retry_nowait;
667 }
668 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500669 case -NFS4ERR_SEQ_FALSE_RETRY:
670 ++slot->seq_nr;
671 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400672 default:
673 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500674 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400675 }
676out:
677 /* The session may be reset by one of the error handlers. */
678 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400679 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500680out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500681 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500682retry_nowait:
683 if (rpc_restart_call_prepare(task)) {
684 task->tk_status = 0;
685 ret = 0;
686 }
687 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400688out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400689 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400690 goto out;
691 rpc_delay(task, NFS4_POLL_RETRY_MAX);
692 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400693}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500694EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400695
Peng Tao2c4b1312014-06-11 05:24:16 +0800696int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400697{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400699 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400700 if (!res->sr_slot->table->session)
701 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400702 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400703}
Peng Tao2c4b1312014-06-11 05:24:16 +0800704EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400705
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000706int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707 struct nfs4_sequence_args *args,
708 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400709 struct rpc_task *task)
710{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400711 struct nfs4_slot *slot;
712 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400713
714 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400715 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400716 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400717 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400718
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400719 tbl = &session->fc_slot_table;
720
Trond Myklebust69d206b2012-11-22 13:21:02 -0500721 task->tk_timeout = 0;
722
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400723 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400724 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400725 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500726 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500727 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400728 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400729 }
730
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500731 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500732 if (IS_ERR(slot)) {
733 /* If out of memory, try again in 1/4 second */
734 if (slot == ERR_PTR(-ENOMEM))
735 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400736 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400737 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400738 }
739 spin_unlock(&tbl->slot_tbl_lock);
740
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500741 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400742
Chuck Levere8d92382013-08-09 12:47:51 -0400743 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500744 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400745
Benny Halevydfb4f3092010-09-24 09:17:01 -0400746 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500747 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400748 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400749 /*
750 * sr_status is only set in decode_sequence, and so will remain
751 * set to 1 if an rpc level failure occurs.
752 */
753 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400754 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400755out_success:
756 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400757 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400758out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400759 /* Privileged tasks are queued with top priority */
760 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400761 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
762 NULL, RPC_PRIORITY_PRIVILEGED);
763 else
764 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400765 spin_unlock(&tbl->slot_tbl_lock);
766 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400767}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000768EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400769
Chuck Lever5a580e02013-08-09 12:48:09 -0400770static int nfs4_setup_sequence(const struct nfs_server *server,
771 struct nfs4_sequence_args *args,
772 struct nfs4_sequence_res *res,
773 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400774{
Trond Myklebust035168a2010-06-16 09:52:26 -0400775 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400776 int ret = 0;
777
Chuck Lever3bd23842013-08-09 12:49:19 -0400778 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800779 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
780 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400781
Chuck Levere8d92382013-08-09 12:47:51 -0400782 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400783 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400784 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400785
Trond Myklebust9d12b212012-01-17 22:04:25 -0500786 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400787
Andy Adamsonce5039c2009-04-01 09:22:13 -0400788 dprintk("<-- %s status=%d\n", __func__, ret);
789 return ret;
790}
791
Andy Adamsonce5039c2009-04-01 09:22:13 -0400792static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
793{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400794 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400795 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400796
Trond Myklebust035168a2010-06-16 09:52:26 -0400797 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
798
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400799 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400800}
801
Andy Adamson69ab40c2009-04-01 09:22:19 -0400802static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
803{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400804 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400805
Trond Myklebust14516c32010-07-31 14:29:06 -0400806 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400807}
808
Trond Myklebust17280172012-03-11 13:11:00 -0400809static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400810 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400811 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400812};
813
Chuck Lever3bd23842013-08-09 12:49:19 -0400814#else /* !CONFIG_NFS_V4_1 */
815
Chuck Lever5a580e02013-08-09 12:48:09 -0400816static int nfs4_setup_sequence(const struct nfs_server *server,
817 struct nfs4_sequence_args *args,
818 struct nfs4_sequence_res *res,
819 struct rpc_task *task)
820{
Peng Taocb04ad22014-06-11 05:24:15 +0800821 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
822 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400823}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400824
Peng Tao2c4b1312014-06-11 05:24:16 +0800825int nfs4_sequence_done(struct rpc_task *task,
826 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400827{
Chuck Lever3bd23842013-08-09 12:49:19 -0400828 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400829}
Peng Tao2c4b1312014-06-11 05:24:16 +0800830EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400831
832#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400833
Chuck Lever9915ea72013-08-09 12:48:27 -0400834static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
835{
836 struct nfs4_call_sync_data *data = calldata;
837 nfs4_setup_sequence(data->seq_server,
838 data->seq_args, data->seq_res, task);
839}
840
841static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
842{
843 struct nfs4_call_sync_data *data = calldata;
844 nfs4_sequence_done(task, data->seq_res);
845}
846
847static const struct rpc_call_ops nfs40_call_sync_ops = {
848 .rpc_call_prepare = nfs40_call_sync_prepare,
849 .rpc_call_done = nfs40_call_sync_done,
850};
851
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400852static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400853 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 struct nfs4_sequence_args *args,
856 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400857{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858 int ret;
859 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400860 struct nfs_client *clp = server->nfs_client;
861 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100862 .seq_server = server,
863 .seq_args = args,
864 .seq_res = res,
865 };
866 struct rpc_task_setup task_setup = {
867 .rpc_client = clnt,
868 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400869 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 .callback_data = &data
871 };
872
873 task = rpc_run_task(&task_setup);
874 if (IS_ERR(task))
875 ret = PTR_ERR(task);
876 else {
877 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400878 rpc_put_task(task);
879 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100880 return ret;
881}
882
Bryan Schumaker7c513052011-03-24 17:12:24 +0000883int nfs4_call_sync(struct rpc_clnt *clnt,
884 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000885 struct rpc_message *msg,
886 struct nfs4_sequence_args *args,
887 struct nfs4_sequence_res *res,
888 int cache_reply)
889{
Chuck Levera9c92d62013-08-09 12:48:18 -0400890 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400891 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000892}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
895{
896 struct nfs_inode *nfsi = NFS_I(dir);
897
898 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400899 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700900 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700902 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500903 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000904 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_unlock(&dir->i_lock);
906}
907
908struct nfs4_opendata {
909 struct kref kref;
910 struct nfs_openargs o_arg;
911 struct nfs_openres o_res;
912 struct nfs_open_confirmargs c_arg;
913 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500914 struct nfs4_string owner_name;
915 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +0800916 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400918 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400920 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct nfs4_state_owner *owner;
922 struct nfs4_state *state;
923 struct iattr attrs;
924 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400925 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400926 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400927 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400928 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 int cancelled;
930};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400931
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400932static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
933 int err, struct nfs4_exception *exception)
934{
935 if (err != -EINVAL)
936 return false;
937 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
938 return false;
939 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
940 exception->retry = 1;
941 return true;
942}
943
Trond Myklebust6ae37332015-01-30 14:21:14 -0500944static u32
945nfs4_map_atomic_open_share(struct nfs_server *server,
946 fmode_t fmode, int openflags)
947{
948 u32 res = 0;
949
950 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
951 case FMODE_READ:
952 res = NFS4_SHARE_ACCESS_READ;
953 break;
954 case FMODE_WRITE:
955 res = NFS4_SHARE_ACCESS_WRITE;
956 break;
957 case FMODE_READ|FMODE_WRITE:
958 res = NFS4_SHARE_ACCESS_BOTH;
959 }
960 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
961 goto out;
962 /* Want no delegation if we're using O_DIRECT */
963 if (openflags & O_DIRECT)
964 res |= NFS4_SHARE_WANT_NO_DELEG;
965out:
966 return res;
967}
968
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400969static enum open_claim_type4
970nfs4_map_atomic_open_claim(struct nfs_server *server,
971 enum open_claim_type4 claim)
972{
973 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
974 return claim;
975 switch (claim) {
976 default:
977 return claim;
978 case NFS4_OPEN_CLAIM_FH:
979 return NFS4_OPEN_CLAIM_NULL;
980 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
981 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
982 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
983 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
984 }
985}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100988{
989 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400990 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100991 p->o_res.seqid = p->o_arg.seqid;
992 p->c_res.seqid = p->c_arg.seqid;
993 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400994 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100995 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500996 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100997}
998
Al Viro82a2c1b2011-06-22 18:30:55 -0400999static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001000 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001001 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001002 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001003 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001004 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001005{
Al Viro82a2c1b2011-06-22 18:30:55 -04001006 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001007 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001008 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001009 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001010 struct nfs4_opendata *p;
1011
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001012 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001013 if (p == NULL)
1014 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001015
1016 p->f_label = nfs4_label_alloc(server, gfp_mask);
1017 if (IS_ERR(p->f_label))
1018 goto err_free_p;
1019
Kinglong Meea49c2692015-07-27 15:31:38 +08001020 p->a_label = nfs4_label_alloc(server, gfp_mask);
1021 if (IS_ERR(p->a_label))
1022 goto err_free_f;
1023
Trond Myklebust63f5f792015-01-23 19:19:25 -05001024 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1025 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001026 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001027 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001028 nfs_sb_active(dentry->d_sb);
1029 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001030 p->dir = parent;
1031 p->owner = sp;
1032 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 p->o_arg.open_flags = flags;
1034 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001035 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1036 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001037 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1038 * will return permission denied for all bits until close */
1039 if (!(flags & O_EXCL)) {
1040 /* ask server to check for all possible rights as results
1041 * are cached */
1042 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1043 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001044 }
David Howells7539bba2006-08-22 20:06:09 -04001045 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001046 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1047 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001048 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001049 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001050 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001051 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001052 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001053 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1054 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001055 case NFS4_OPEN_CLAIM_NULL:
1056 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1057 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1058 p->o_arg.fh = NFS_FH(dir);
1059 break;
1060 case NFS4_OPEN_CLAIM_PREVIOUS:
1061 case NFS4_OPEN_CLAIM_FH:
1062 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1063 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001064 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001065 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001066 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001067 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001068
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001069 p->o_arg.u.attrs = &p->attrs;
1070 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001071
1072 verf[0] = jiffies;
1073 verf[1] = current->pid;
1074 memcpy(p->o_arg.u.verifier.data, verf,
1075 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001076 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001077 p->c_arg.fh = &p->o_res.fh;
1078 p->c_arg.stateid = &p->o_res.stateid;
1079 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001080 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001081 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001082 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001083
1084err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001085 nfs4_label_free(p->a_label);
1086err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001087 nfs4_label_free(p->f_label);
1088err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001089 kfree(p);
1090err:
1091 dput(parent);
1092 return NULL;
1093}
1094
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001095static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001096{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001097 struct nfs4_opendata *p = container_of(kref,
1098 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001099 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001100
1101 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 if (p->state != NULL)
1103 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001104 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001105
Kinglong Meea49c2692015-07-27 15:31:38 +08001106 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001107 nfs4_label_free(p->f_label);
1108
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001109 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001110 dput(p->dentry);
1111 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001112 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001113 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001114 kfree(p);
1115}
1116
1117static void nfs4_opendata_put(struct nfs4_opendata *p)
1118{
1119 if (p != NULL)
1120 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001121}
1122
Trond Myklebust06f814a2006-01-03 09:55:07 +01001123static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1124{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001125 int ret;
1126
Trond Myklebust06f814a2006-01-03 09:55:07 +01001127 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001128 return ret;
1129}
1130
Trond Myklebust24311f82015-09-20 10:50:17 -04001131static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1132 fmode_t fmode)
1133{
1134 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1135 case FMODE_READ|FMODE_WRITE:
1136 return state->n_rdwr != 0;
1137 case FMODE_WRITE:
1138 return state->n_wronly != 0;
1139 case FMODE_READ:
1140 return state->n_rdonly != 0;
1141 }
1142 WARN_ON_ONCE(1);
1143 return false;
1144}
1145
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001146static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001147{
1148 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001149
Trond Myklebust536e43d2012-01-17 22:04:26 -05001150 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151 goto out;
1152 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001153 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001154 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1155 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001156 break;
1157 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001158 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1159 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001160 break;
1161 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001162 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1163 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001164 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001165out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001166 return ret;
1167}
1168
Trond Myklebust2a606182015-08-19 22:30:00 -05001169static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1170 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001171{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001172 if (delegation == NULL)
1173 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001174 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001175 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001176 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1177 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001178 switch (claim) {
1179 case NFS4_OPEN_CLAIM_NULL:
1180 case NFS4_OPEN_CLAIM_FH:
1181 break;
1182 case NFS4_OPEN_CLAIM_PREVIOUS:
1183 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1184 break;
1185 default:
1186 return 0;
1187 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001188 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001189 return 1;
1190}
1191
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001192static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001193{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001194 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001195 case FMODE_WRITE:
1196 state->n_wronly++;
1197 break;
1198 case FMODE_READ:
1199 state->n_rdonly++;
1200 break;
1201 case FMODE_READ|FMODE_WRITE:
1202 state->n_rdwr++;
1203 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001204 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001205}
1206
Trond Myklebust4f14c192014-02-12 19:15:06 -05001207static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001208{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001209 struct nfs_client *clp = state->owner->so_server->nfs_client;
1210 bool need_recover = false;
1211
1212 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1213 need_recover = true;
1214 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1215 need_recover = true;
1216 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1217 need_recover = true;
1218 if (need_recover)
1219 nfs4_state_mark_reclaim_nograce(clp, state);
1220}
1221
Trond Myklebuste999e802014-02-10 18:20:47 -05001222static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1223 nfs4_stateid *stateid)
1224{
1225 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1226 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001227 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1228 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001229 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001230 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001231 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1232 return true;
1233 return false;
1234}
1235
Trond Myklebustf95549c2015-01-23 18:06:09 -05001236static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1237{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001238 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1239 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001240 if (state->n_wronly)
1241 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1242 if (state->n_rdonly)
1243 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1244 if (state->n_rdwr)
1245 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001246 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001247}
1248
Trond Myklebust226056c2014-02-11 10:41:07 -05001249static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001250 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001251 nfs4_stateid *stateid, fmode_t fmode)
1252{
1253 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1254 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1255 case FMODE_WRITE:
1256 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1257 break;
1258 case FMODE_READ:
1259 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1260 break;
1261 case 0:
1262 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1263 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1264 clear_bit(NFS_OPEN_STATE, &state->flags);
1265 }
1266 if (stateid == NULL)
1267 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001268 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001269 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1270 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1271 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001272 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001273 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001274 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001275 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001276 nfs4_stateid_copy(&state->stateid, stateid);
1277 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001278}
1279
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001280static void nfs_clear_open_stateid(struct nfs4_state *state,
1281 nfs4_stateid *arg_stateid,
1282 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001283{
1284 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001285 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001286 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001287 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1288 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001289}
1290
Trond Myklebust003707c2007-07-05 18:07:55 -04001291static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1292{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001293 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001294 case FMODE_READ:
1295 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1296 break;
1297 case FMODE_WRITE:
1298 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1299 break;
1300 case FMODE_READ|FMODE_WRITE:
1301 set_bit(NFS_O_RDWR_STATE, &state->flags);
1302 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001303 if (!nfs_need_update_open_stateid(state, stateid))
1304 return;
1305 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1306 nfs4_stateid_copy(&state->stateid, stateid);
1307 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001308}
1309
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001310static 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 -07001311{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001312 /*
1313 * Protect the call to nfs4_state_set_mode_locked and
1314 * serialise the stateid update
1315 */
1316 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001317 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001318 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001319 set_bit(NFS_DELEGATED_STATE, &state->flags);
1320 }
1321 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001322 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001323 write_sequnlock(&state->seqlock);
1324 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001325 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001326 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001329static 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 -05001330{
1331 struct nfs_inode *nfsi = NFS_I(state->inode);
1332 struct nfs_delegation *deleg_cur;
1333 int ret = 0;
1334
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001336
1337 rcu_read_lock();
1338 deleg_cur = rcu_dereference(nfsi->delegation);
1339 if (deleg_cur == NULL)
1340 goto no_delegation;
1341
1342 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001343 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001344 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001345 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001346 goto no_delegation_unlock;
1347
1348 if (delegation == NULL)
1349 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001350 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001351 goto no_delegation_unlock;
1352
Trond Myklebustb7391f42008-12-23 15:21:52 -05001353 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001354 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001355 ret = 1;
1356no_delegation_unlock:
1357 spin_unlock(&deleg_cur->lock);
1358no_delegation:
1359 rcu_read_unlock();
1360
1361 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001362 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001363 ret = 1;
1364 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001365 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1366 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001367
1368 return ret;
1369}
1370
Trond Myklebust39071e62015-01-24 15:07:56 -05001371static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1372 const nfs4_stateid *stateid)
1373{
1374 struct nfs4_state *state = lsp->ls_state;
1375 bool ret = false;
1376
1377 spin_lock(&state->state_lock);
1378 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1379 goto out_noupdate;
1380 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1381 goto out_noupdate;
1382 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1383 ret = true;
1384out_noupdate:
1385 spin_unlock(&state->state_lock);
1386 return ret;
1387}
Trond Myklebust34310432008-12-23 15:21:38 -05001388
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001389static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001390{
1391 struct nfs_delegation *delegation;
1392
1393 rcu_read_lock();
1394 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001395 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001396 rcu_read_unlock();
1397 return;
1398 }
1399 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001400 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001401}
1402
Trond Myklebust6ee41262007-07-08 14:11:36 -04001403static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001404{
1405 struct nfs4_state *state = opendata->state;
1406 struct nfs_inode *nfsi = NFS_I(state->inode);
1407 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001408 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001409 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001410 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001411 nfs4_stateid stateid;
1412 int ret = -EAGAIN;
1413
Trond Myklebustaac00a82007-07-05 19:02:21 -04001414 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001415 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001416 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001417 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001418 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001419 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001420 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001421 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001422 rcu_read_lock();
1423 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001424 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001425 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001426 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001427 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001428 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001429 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001430 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001431 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001432 if (!opendata->is_recover) {
1433 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1434 if (ret != 0)
1435 goto out;
1436 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001437 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001438
1439 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001440 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001441 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001442 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001443out:
1444 return ERR_PTR(ret);
1445out_return_state:
1446 atomic_inc(&state->count);
1447 return state;
1448}
1449
Andy Adamsone23008e2012-10-02 21:07:32 -04001450static void
1451nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1452{
1453 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1454 struct nfs_delegation *delegation;
1455 int delegation_flags = 0;
1456
1457 rcu_read_lock();
1458 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1459 if (delegation)
1460 delegation_flags = delegation->flags;
1461 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001462 switch (data->o_arg.claim) {
1463 default:
1464 break;
1465 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1466 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001467 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1468 "returning a delegation for "
1469 "OPEN(CLAIM_DELEGATE_CUR)\n",
1470 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001471 return;
1472 }
1473 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001474 nfs_inode_set_delegation(state->inode,
1475 data->owner->so_cred,
1476 &data->o_res);
1477 else
1478 nfs_inode_reclaim_delegation(state->inode,
1479 data->owner->so_cred,
1480 &data->o_res);
1481}
1482
1483/*
1484 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1485 * and update the nfs4_state.
1486 */
1487static struct nfs4_state *
1488_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1489{
1490 struct inode *inode = data->state->inode;
1491 struct nfs4_state *state = data->state;
1492 int ret;
1493
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001494 if (!data->rpc_done) {
1495 if (data->rpc_status) {
1496 ret = data->rpc_status;
1497 goto err;
1498 }
1499 /* cached opens have already been processed */
1500 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001501 }
1502
Andy Adamsone23008e2012-10-02 21:07:32 -04001503 ret = nfs_refresh_inode(inode, &data->f_attr);
1504 if (ret)
1505 goto err;
1506
1507 if (data->o_res.delegation_type != 0)
1508 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001509update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001510 update_open_stateid(state, &data->o_res.stateid, NULL,
1511 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001512 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001513
1514 return state;
1515err:
1516 return ERR_PTR(ret);
1517
1518}
1519
1520static struct nfs4_state *
1521_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522{
1523 struct inode *inode;
1524 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001525 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526
Trond Myklebustaac00a82007-07-05 19:02:21 -04001527 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001528 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001529 goto out;
1530 }
1531
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001532 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001534 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001535 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001536 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001537 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001538 goto err;
1539 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540 state = nfs4_get_open_state(inode, data->owner);
1541 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001542 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001543 if (data->o_res.delegation_type != 0)
1544 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001545 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001546 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001547 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001549 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001550 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001551err_put_inode:
1552 iput(inode);
1553err:
1554 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001555}
1556
Andy Adamsone23008e2012-10-02 21:07:32 -04001557static struct nfs4_state *
1558nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1559{
1560 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1561 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1562 return _nfs4_opendata_to_nfs4_state(data);
1563}
1564
Trond Myklebust864472e2006-01-03 09:55:15 +01001565static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1566{
1567 struct nfs_inode *nfsi = NFS_I(state->inode);
1568 struct nfs_open_context *ctx;
1569
1570 spin_lock(&state->inode->i_lock);
1571 list_for_each_entry(ctx, &nfsi->open_files, list) {
1572 if (ctx->state != state)
1573 continue;
1574 get_nfs_open_context(ctx);
1575 spin_unlock(&state->inode->i_lock);
1576 return ctx;
1577 }
1578 spin_unlock(&state->inode->i_lock);
1579 return ERR_PTR(-ENOENT);
1580}
1581
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001582static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1583 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001584{
1585 struct nfs4_opendata *opendata;
1586
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001587 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001588 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001589 if (opendata == NULL)
1590 return ERR_PTR(-ENOMEM);
1591 opendata->state = state;
1592 atomic_inc(&state->count);
1593 return opendata;
1594}
1595
Trond Myklebust24311f82015-09-20 10:50:17 -04001596static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1597 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001598{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001599 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001600 int ret;
1601
Trond Myklebust24311f82015-09-20 10:50:17 -04001602 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001603 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001604 opendata->o_arg.open_flags = 0;
1605 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001606 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1607 NFS_SB(opendata->dentry->d_sb),
1608 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001609 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1610 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1611 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001612 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001613 if (ret != 0)
1614 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001615 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001616 if (IS_ERR(newstate))
1617 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001618 if (newstate != opendata->state)
1619 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001620 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001621 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001622}
1623
1624static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1625{
Trond Myklebust864472e2006-01-03 09:55:15 +01001626 int ret;
1627
Trond Myklebust4f14c192014-02-12 19:15:06 -05001628 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1629 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1630 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1631 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001632 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001633 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001634 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001635 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001636 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1637 if (ret != 0)
1638 return ret;
1639 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1640 if (ret != 0)
1641 return ret;
1642 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1643 if (ret != 0)
1644 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001645 /*
1646 * We may have performed cached opens for all three recoveries.
1647 * Check if we need to update the current stateid.
1648 */
1649 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001650 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001651 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001652 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001653 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001654 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001655 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001656 return 0;
1657}
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659/*
1660 * OPEN_RECLAIM:
1661 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001663static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001665 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001666 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001667 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int status;
1669
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001670 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1671 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001672 if (IS_ERR(opendata))
1673 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001674 rcu_read_lock();
1675 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001676 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001677 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001678 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001679 opendata->o_arg.u.delegation_type = delegation_type;
1680 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001681 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return status;
1683}
1684
Trond Myklebust539cd032007-06-05 11:46:42 -04001685static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 struct nfs_server *server = NFS_SERVER(state->inode);
1688 struct nfs4_exception exception = { };
1689 int err;
1690 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001691 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001692 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001693 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1694 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001695 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001696 break;
1697 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 } while (exception.retry);
1699 return err;
1700}
1701
Trond Myklebust864472e2006-01-03 09:55:15 +01001702static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1703{
1704 struct nfs_open_context *ctx;
1705 int ret;
1706
1707 ctx = nfs4_state_find_open_context(state);
1708 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001709 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001710 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001711 put_nfs_open_context(ctx);
1712 return ret;
1713}
1714
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001715static 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 -07001716{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001717 switch (err) {
1718 default:
1719 printk(KERN_ERR "NFS: %s: unhandled error "
1720 "%d.\n", __func__, err);
1721 case 0:
1722 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001723 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001724 case -ESTALE:
1725 break;
1726 case -NFS4ERR_BADSESSION:
1727 case -NFS4ERR_BADSLOT:
1728 case -NFS4ERR_BAD_HIGH_SLOT:
1729 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1730 case -NFS4ERR_DEADSESSION:
1731 set_bit(NFS_DELEGATED_STATE, &state->flags);
1732 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1733 return -EAGAIN;
1734 case -NFS4ERR_STALE_CLIENTID:
1735 case -NFS4ERR_STALE_STATEID:
1736 set_bit(NFS_DELEGATED_STATE, &state->flags);
1737 case -NFS4ERR_EXPIRED:
1738 /* Don't recall a delegation if it was lost */
1739 nfs4_schedule_lease_recovery(server->nfs_client);
1740 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001741 case -NFS4ERR_MOVED:
1742 nfs4_schedule_migration_recovery(server);
1743 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001744 case -NFS4ERR_LEASE_MOVED:
1745 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1746 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001747 case -NFS4ERR_DELEG_REVOKED:
1748 case -NFS4ERR_ADMIN_REVOKED:
1749 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001750 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001751 nfs_inode_find_state_and_recover(state->inode,
1752 stateid);
1753 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001754 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001755 case -NFS4ERR_DELAY:
1756 case -NFS4ERR_GRACE:
1757 set_bit(NFS_DELEGATED_STATE, &state->flags);
1758 ssleep(1);
1759 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001760 case -ENOMEM:
1761 case -NFS4ERR_DENIED:
1762 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1763 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return err;
1766}
1767
Trond Myklebust24311f82015-09-20 10:50:17 -04001768int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1769 struct nfs4_state *state, const nfs4_stateid *stateid,
1770 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001771{
1772 struct nfs_server *server = NFS_SERVER(state->inode);
1773 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001774 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001775
1776 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1777 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1778 if (IS_ERR(opendata))
1779 return PTR_ERR(opendata);
1780 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001781 write_seqlock(&state->seqlock);
1782 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1783 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001784 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1785 switch (type & (FMODE_READ|FMODE_WRITE)) {
1786 case FMODE_READ|FMODE_WRITE:
1787 case FMODE_WRITE:
1788 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1789 if (err)
1790 break;
1791 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1792 if (err)
1793 break;
1794 case FMODE_READ:
1795 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1796 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001797 nfs4_opendata_put(opendata);
1798 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1799}
1800
Chuck Leverbe05c862013-08-09 12:49:47 -04001801static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1802{
1803 struct nfs4_opendata *data = calldata;
1804
Peng Taocb04ad22014-06-11 05:24:15 +08001805 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1806 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001807}
1808
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001809static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1810{
1811 struct nfs4_opendata *data = calldata;
1812
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001813 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001814
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001815 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001816 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001817 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001818 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001819 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001820 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001821 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001822}
1823
1824static void nfs4_open_confirm_release(void *calldata)
1825{
1826 struct nfs4_opendata *data = calldata;
1827 struct nfs4_state *state = NULL;
1828
1829 /* If this request hasn't been cancelled, do nothing */
1830 if (data->cancelled == 0)
1831 goto out_free;
1832 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001833 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001834 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001835 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001836 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001837 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001838out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001839 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001840}
1841
1842static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001843 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001844 .rpc_call_done = nfs4_open_confirm_done,
1845 .rpc_release = nfs4_open_confirm_release,
1846};
1847
1848/*
1849 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1850 */
1851static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1852{
David Howells2b0143b2015-03-17 22:25:59 +00001853 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001854 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001855 struct rpc_message msg = {
1856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1857 .rpc_argp = &data->c_arg,
1858 .rpc_resp = &data->c_res,
1859 .rpc_cred = data->owner->so_cred,
1860 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001861 struct rpc_task_setup task_setup_data = {
1862 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001863 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001864 .callback_ops = &nfs4_open_confirm_ops,
1865 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001866 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001867 .flags = RPC_TASK_ASYNC,
1868 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 int status;
1870
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001871 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001872 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001873 data->rpc_done = 0;
1874 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001875 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04001876 if (data->is_recover)
1877 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001878 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001879 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001880 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001881 status = nfs4_wait_for_completion_rpc_task(task);
1882 if (status != 0) {
1883 data->cancelled = 1;
1884 smp_wmb();
1885 } else
1886 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001887 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return status;
1889}
1890
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001891static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001893 struct nfs4_opendata *data = calldata;
1894 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001895 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05001896 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001897
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001898 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001899 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001900 /*
1901 * Check if we still need to send an OPEN call, or if we can use
1902 * a delegation instead.
1903 */
1904 if (data->state != NULL) {
1905 struct nfs_delegation *delegation;
1906
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001907 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001908 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001909 rcu_read_lock();
1910 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001911 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05001912 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001913 rcu_read_unlock();
1914 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001915 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001916 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05001917 switch (claim) {
1918 default:
1919 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04001920 case NFS4_OPEN_CLAIM_PREVIOUS:
1921 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1922 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001923 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001924 case NFS4_OPEN_CLAIM_FH:
1925 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001926 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1927 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001928 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001929 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001930 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001931 &data->o_res.seq_res,
1932 task) != 0)
1933 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001934
1935 /* Set the create mode (note dependency on the session type) */
1936 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1937 if (data->o_arg.open_flags & O_EXCL) {
1938 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1939 if (nfs4_has_persistent_session(clp))
1940 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1941 else if (clp->cl_mvops->minor_version > 0)
1942 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1943 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001944 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001945unlock_no_action:
1946 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001947out_no_action:
1948 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001949out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001950 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001951}
1952
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001953static void nfs4_open_done(struct rpc_task *task, void *calldata)
1954{
1955 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001957 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001958
Trond Myklebust14516c32010-07-31 14:29:06 -04001959 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1960 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001961
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001962 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001963 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1964 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001965 case S_IFREG:
1966 break;
1967 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001968 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001969 break;
1970 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001971 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001972 break;
1973 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001974 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001975 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001976 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001977 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001978 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1979 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001980 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001981 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001982}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001983
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001984static void nfs4_open_release(void *calldata)
1985{
1986 struct nfs4_opendata *data = calldata;
1987 struct nfs4_state *state = NULL;
1988
1989 /* If this request hasn't been cancelled, do nothing */
1990 if (data->cancelled == 0)
1991 goto out_free;
1992 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001993 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001994 goto out_free;
1995 /* In case we need an open_confirm, no cleanup! */
1996 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1997 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001998 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001999 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002000 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002001out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002002 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002003}
2004
2005static const struct rpc_call_ops nfs4_open_ops = {
2006 .rpc_call_prepare = nfs4_open_prepare,
2007 .rpc_call_done = nfs4_open_done,
2008 .rpc_release = nfs4_open_release,
2009};
2010
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002011static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002012{
David Howells2b0143b2015-03-17 22:25:59 +00002013 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002014 struct nfs_server *server = NFS_SERVER(dir);
2015 struct nfs_openargs *o_arg = &data->o_arg;
2016 struct nfs_openres *o_res = &data->o_res;
2017 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002018 struct rpc_message msg = {
2019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2020 .rpc_argp = o_arg,
2021 .rpc_resp = o_res,
2022 .rpc_cred = data->owner->so_cred,
2023 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002024 struct rpc_task_setup task_setup_data = {
2025 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002026 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002027 .callback_ops = &nfs4_open_ops,
2028 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002029 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002030 .flags = RPC_TASK_ASYNC,
2031 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002032 int status;
2033
Chuck Levera9c92d62013-08-09 12:48:18 -04002034 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002035 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002036 data->rpc_done = 0;
2037 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002038 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002039 data->is_recover = 0;
2040 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002041 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002042 data->is_recover = 1;
2043 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002044 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002045 if (IS_ERR(task))
2046 return PTR_ERR(task);
2047 status = nfs4_wait_for_completion_rpc_task(task);
2048 if (status != 0) {
2049 data->cancelled = 1;
2050 smp_wmb();
2051 } else
2052 status = data->rpc_status;
2053 rpc_put_task(task);
2054
2055 return status;
2056}
2057
2058static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2059{
David Howells2b0143b2015-03-17 22:25:59 +00002060 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002061 struct nfs_openres *o_res = &data->o_res;
2062 int status;
2063
2064 status = nfs4_run_open_task(data, 1);
2065 if (status != 0 || !data->rpc_done)
2066 return status;
2067
Trond Myklebust6926afd2012-01-07 13:22:46 -05002068 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2069
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002070 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2071 status = _nfs4_proc_open_confirm(data);
2072 if (status != 0)
2073 return status;
2074 }
2075
2076 return status;
2077}
2078
Trond Myklebustf3792d62014-07-10 08:54:32 -04002079/*
2080 * Additional permission checks in order to distinguish between an
2081 * open for read, and an open for execute. This works around the
2082 * fact that NFSv4 OPEN treats read and execute permissions as being
2083 * the same.
2084 * Note that in the non-execute case, we want to turn off permission
2085 * checking if we just created a new file (POSIX open() semantics).
2086 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002087static int nfs4_opendata_access(struct rpc_cred *cred,
2088 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002089 struct nfs4_state *state, fmode_t fmode,
2090 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002091{
2092 struct nfs_access_entry cache;
2093 u32 mask;
2094
2095 /* access call failed or for some reason the server doesn't
2096 * support any access modes -- defer access call until later */
2097 if (opendata->o_res.access_supported == 0)
2098 return 0;
2099
2100 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002101 /*
2102 * Use openflags to check for exec, because fmode won't
2103 * always have FMODE_EXEC set when file open for exec.
2104 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002105 if (openflags & __FMODE_EXEC) {
2106 /* ONLY check for exec rights */
2107 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002108 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002109 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002110
2111 cache.cred = cred;
2112 cache.jiffies = jiffies;
2113 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2114 nfs_access_add_cache(state->inode, &cache);
2115
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002116 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002117 return 0;
2118
2119 /* even though OPEN succeeded, access is denied. Close the file */
2120 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002121 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002122}
2123
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002124/*
2125 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2126 */
2127static int _nfs4_proc_open(struct nfs4_opendata *data)
2128{
David Howells2b0143b2015-03-17 22:25:59 +00002129 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002130 struct nfs_server *server = NFS_SERVER(dir);
2131 struct nfs_openargs *o_arg = &data->o_arg;
2132 struct nfs_openres *o_res = &data->o_res;
2133 int status;
2134
2135 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002136 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002137 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002138 if (status != 0) {
2139 if (status == -NFS4ERR_BADNAME &&
2140 !(o_arg->open_flags & O_CREAT))
2141 return -ENOENT;
2142 return status;
2143 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002144
Trond Myklebust6926afd2012-01-07 13:22:46 -05002145 nfs_fattr_map_and_free_names(server, &data->f_attr);
2146
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002147 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002148 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002149 if (o_arg->open_flags & O_EXCL)
2150 data->file_created = 1;
2151 else if (o_res->cinfo.before != o_res->cinfo.after)
2152 data->file_created = 1;
2153 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002154 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2155 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002157 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002159 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002162 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
Andy Adamsond83217c2011-03-01 01:34:17 +00002166static int nfs4_recover_expired_lease(struct nfs_server *server)
2167{
2168 return nfs4_client_recover_expired_lease(server->nfs_client);
2169}
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171/*
2172 * OPEN_EXPIRED:
2173 * reclaim state on the server after a network partition.
2174 * Assumes caller holds the appropriate lock
2175 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002176static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002178 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002179 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002181 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002182 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002183 if (IS_ERR(opendata))
2184 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002185 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002186 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002187 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002188 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002189 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002192static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002193{
Trond Myklebust539cd032007-06-05 11:46:42 -04002194 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002195 struct nfs4_exception exception = { };
2196 int err;
2197
2198 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002199 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002200 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002201 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2202 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002203 switch (err) {
2204 default:
2205 goto out;
2206 case -NFS4ERR_GRACE:
2207 case -NFS4ERR_DELAY:
2208 nfs4_handle_exception(server, err, &exception);
2209 err = 0;
2210 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002211 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002212out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002213 return err;
2214}
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2217{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002219 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Trond Myklebust864472e2006-01-03 09:55:15 +01002221 ctx = nfs4_state_find_open_context(state);
2222 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002223 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002224 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002225 put_nfs_open_context(ctx);
2226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002229static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2230{
2231 nfs_remove_bad_delegation(state->inode);
2232 write_seqlock(&state->seqlock);
2233 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2234 write_sequnlock(&state->seqlock);
2235 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2236}
2237
2238static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2239{
2240 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2241 nfs_finish_clear_delegation_stateid(state);
2242}
2243
2244static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2245{
2246 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2247 nfs40_clear_delegation_stateid(state);
2248 return nfs4_open_expired(sp, state);
2249}
2250
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002251#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002252static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002253{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002254 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002255 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002256 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002257 struct rpc_cred *cred;
2258 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002259
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002260 /* Get the delegation credential for use by test/free_stateid */
2261 rcu_read_lock();
2262 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002263 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002264 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002265 return;
2266 }
2267
2268 nfs4_stateid_copy(&stateid, &delegation->stateid);
2269 cred = get_rpccred(delegation->cred);
2270 rcu_read_unlock();
2271 status = nfs41_test_stateid(server, &stateid, cred);
2272 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002273
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002274 if (status != NFS_OK) {
2275 /* Free the stateid unless the server explicitly
2276 * informs us the stateid is unrecognized. */
2277 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002278 nfs41_free_stateid(server, &stateid, cred);
2279 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002280 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002281
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002282 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002283}
2284
2285/**
2286 * nfs41_check_open_stateid - possibly free an open stateid
2287 *
2288 * @state: NFSv4 state for an inode
2289 *
2290 * Returns NFS_OK if recovery for this stateid is now finished.
2291 * Otherwise a negative NFS4ERR value is returned.
2292 */
2293static int nfs41_check_open_stateid(struct nfs4_state *state)
2294{
2295 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002296 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002297 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002298 int status;
2299
2300 /* If a state reset has been done, test_stateid is unneeded */
2301 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2302 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2303 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2304 return -NFS4ERR_BAD_STATEID;
2305
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002306 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002307 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002308 if (status != NFS_OK) {
2309 /* Free the stateid unless the server explicitly
2310 * informs us the stateid is unrecognized. */
2311 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002312 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002313
2314 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2315 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2316 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002317 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002318 }
2319 return status;
2320}
2321
2322static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2323{
Chuck Levereb64cf92012-07-11 16:30:05 -04002324 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002325
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002326 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002327 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002328 if (status != NFS_OK)
2329 status = nfs4_open_expired(sp, state);
2330 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002331}
2332#endif
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002335 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2336 * fields corresponding to attributes that were used to store the verifier.
2337 * Make sure we clobber those fields in the later setattr call
2338 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002339static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2340 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002341{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002342 const u32 *attrset = opendata->o_res.attrset;
2343
2344 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002345 !(sattr->ia_valid & ATTR_ATIME_SET))
2346 sattr->ia_valid |= ATTR_ATIME;
2347
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002348 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002349 !(sattr->ia_valid & ATTR_MTIME_SET))
2350 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002351
2352 /* Except MODE, it seems harmless of setting twice. */
2353 if ((attrset[1] & FATTR4_WORD1_MODE))
2354 sattr->ia_valid &= ~ATTR_MODE;
2355
2356 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2357 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002358}
2359
Trond Myklebustc21443c2013-02-07 14:26:21 -05002360static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2361 fmode_t fmode,
2362 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002363 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002364{
2365 struct nfs4_state_owner *sp = opendata->owner;
2366 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002367 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002368 struct nfs4_state *state;
2369 unsigned int seq;
2370 int ret;
2371
2372 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2373
2374 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002375 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002376 goto out;
2377
2378 state = nfs4_opendata_to_nfs4_state(opendata);
2379 ret = PTR_ERR(state);
2380 if (IS_ERR(state))
2381 goto out;
2382 if (server->caps & NFS_CAP_POSIX_LOCK)
2383 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2384
Trond Myklebust275bb302013-05-29 13:11:28 -04002385 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002386 if (d_really_is_negative(dentry)) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002387 /* FIXME: Is this d_drop() ever needed? */
2388 d_drop(dentry);
2389 dentry = d_add_unique(dentry, igrab(state->inode));
2390 if (dentry == NULL) {
2391 dentry = opendata->dentry;
2392 } else if (dentry != ctx->dentry) {
2393 dput(ctx->dentry);
2394 ctx->dentry = dget(dentry);
2395 }
2396 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002397 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002398 }
2399
Trond Myklebustc21443c2013-02-07 14:26:21 -05002400 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2401 if (ret != 0)
2402 goto out;
2403
Trond Myklebust3efb9722013-05-29 13:17:04 -04002404 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002405 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002406 nfs_inode_attach_open_context(ctx);
2407 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2408 nfs4_schedule_stateid_recovery(server, state);
2409 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002410out:
2411 return ret;
2412}
2413
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002414/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002415 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 */
Andy Adamson82be4172012-05-23 05:02:35 -04002417static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002418 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002419 int flags,
2420 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002421 struct nfs4_label *label,
2422 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
2424 struct nfs4_state_owner *sp;
2425 struct nfs4_state *state = NULL;
2426 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002427 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002428 struct dentry *dentry = ctx->dentry;
2429 struct rpc_cred *cred = ctx->cred;
2430 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2431 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002432 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002433 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002434 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002438 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2439 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2441 goto out_err;
2442 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002443 status = nfs4_recover_expired_lease(server);
2444 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002445 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002446 if (d_really_is_positive(dentry))
2447 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002448 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002449 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002450 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002451 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002452 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002453 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002454 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
David Quigley14c43f72013-05-22 12:50:43 -04002456 if (label) {
2457 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2458 if (IS_ERR(olabel)) {
2459 status = PTR_ERR(olabel);
2460 goto err_opendata_put;
2461 }
2462 }
2463
Trond Myklebuste911b812014-03-26 13:24:37 -07002464 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2465 if (!opendata->f_attr.mdsthreshold) {
2466 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2467 if (!opendata->f_attr.mdsthreshold)
2468 goto err_free_label;
2469 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002470 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002471 }
David Howells2b0143b2015-03-17 22:25:59 +00002472 if (d_really_is_positive(dentry))
2473 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002474
Trond Myklebust3efb9722013-05-29 13:17:04 -04002475 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002476 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002477 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002478 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002479
NeilBrownefcbc042015-07-30 13:00:56 +10002480 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002481 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002482 nfs4_exclusive_attrset(opendata, sattr, &label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002483
2484 nfs_fattr_init(opendata->o_res.f_attr);
2485 status = nfs4_do_setattr(state->inode, cred,
2486 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002487 state, label, olabel);
2488 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05002489 nfs_setattr_update_inode(state->inode, sattr,
2490 opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002491 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002492 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002493 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002494 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002495 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002496
Trond Myklebuste911b812014-03-26 13:24:37 -07002497 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002498 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002499 opendata->f_attr.mdsthreshold = NULL;
2500 }
Andy Adamson82be4172012-05-23 05:02:35 -04002501
David Quigley14c43f72013-05-22 12:50:43 -04002502 nfs4_label_free(olabel);
2503
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002504 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002507err_free_label:
2508 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002509err_opendata_put:
2510 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002511err_put_state_owner:
2512 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return status;
2515}
2516
2517
Andy Adamson82be4172012-05-23 05:02:35 -04002518static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002519 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002520 int flags,
2521 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002522 struct nfs4_label *label,
2523 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002525 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 struct nfs4_exception exception = { };
2527 struct nfs4_state *res;
2528 int status;
2529
2530 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002531 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002532 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002533 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (status == 0)
2535 break;
2536 /* NOTE: BAD_SEQID means the server and client disagree about the
2537 * book-keeping w.r.t. state-changing operations
2538 * (OPEN/CLOSE/LOCK/LOCKU...)
2539 * It is actually a sign of a bug on the client or on the server.
2540 *
2541 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002542 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 * have unhashed the old state_owner for us, and that we can
2544 * therefore safely retry using a new one. We should still warn
2545 * the user though...
2546 */
2547 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002548 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002549 " returned a bad sequence-id error!\n",
2550 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 exception.retry = 1;
2552 continue;
2553 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002554 /*
2555 * BAD_STATEID on OPEN means that the server cancelled our
2556 * state before it received the OPEN_CONFIRM.
2557 * Recover by retrying the request as per the discussion
2558 * on Page 181 of RFC3530.
2559 */
2560 if (status == -NFS4ERR_BAD_STATEID) {
2561 exception.retry = 1;
2562 continue;
2563 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002564 if (status == -EAGAIN) {
2565 /* We must have found a delegation */
2566 exception.retry = 1;
2567 continue;
2568 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002569 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2570 continue;
2571 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 status, &exception));
2573 } while (exception.retry);
2574 return res;
2575}
2576
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002577static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2578 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002579 struct nfs4_state *state, struct nfs4_label *ilabel,
2580 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002582 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002584 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 .iap = sattr,
2586 .server = server,
2587 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002588 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 };
2590 struct nfs_setattrres res = {
2591 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002592 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 .server = server,
2594 };
2595 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2597 .rpc_argp = &arg,
2598 .rpc_resp = &res,
2599 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002601 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002602 fmode_t fmode;
2603 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002604 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
David Quigleyaa9c2662013-05-22 12:50:44 -04002606 arg.bitmask = nfs4_bitmask(server, ilabel);
2607 if (ilabel)
2608 arg.bitmask = nfs4_bitmask(server, olabel);
2609
Trond Myklebust0e574af2005-10-27 22:12:38 -04002610 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Trond Myklebustee3ae842013-04-29 10:35:36 -04002612 /* Servers should only apply open mode checks for file size changes */
2613 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2614 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2615
2616 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2617 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002618 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002619 struct nfs_lockowner lockowner = {
2620 .l_owner = current->files,
2621 .l_pid = current->tgid,
2622 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002623 if (!nfs4_valid_open_stateid(state))
2624 return -EBADF;
2625 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2626 &lockowner) == -EIO)
2627 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002628 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002629 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Bryan Schumaker7c513052011-03-24 17:12:24 +00002631 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002632 if (status == 0 && state != NULL)
2633 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002634 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002637static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2638 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002639 struct nfs4_state *state, struct nfs4_label *ilabel,
2640 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002642 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002643 struct nfs4_exception exception = {
2644 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002645 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002646 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 int err;
2648 do {
David Quigley1775fd32013-05-22 12:50:42 -04002649 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002650 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002651 switch (err) {
2652 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002653 if (!(sattr->ia_valid & ATTR_SIZE)) {
2654 pr_warn_once("NFSv4: server %s is incorrectly "
2655 "applying open mode checks to "
2656 "a SETATTR that is not "
2657 "changing file size.\n",
2658 server->nfs_client->cl_hostname);
2659 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002660 if (state && !(state->state & FMODE_WRITE)) {
2661 err = -EBADF;
2662 if (sattr->ia_valid & ATTR_OPEN)
2663 err = -EACCES;
2664 goto out;
2665 }
2666 }
2667 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002669out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return err;
2671}
2672
Peng Tao500d7012015-09-22 11:35:22 +08002673static bool
2674nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2675{
2676 if (inode == NULL || !nfs_have_layout(inode))
2677 return false;
2678
2679 return pnfs_wait_on_layoutreturn(inode, task);
2680}
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682struct nfs4_closedata {
2683 struct inode *inode;
2684 struct nfs4_state *state;
2685 struct nfs_closeargs arg;
2686 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002687 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002688 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002689 bool roc;
2690 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691};
2692
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002693static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002694{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002695 struct nfs4_closedata *calldata = data;
2696 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002697 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002698
Fred Isamanf7e89172011-01-06 11:36:32 +00002699 if (calldata->roc)
2700 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002701 nfs4_put_open_state(calldata->state);
2702 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002703 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002704 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002705 kfree(calldata);
2706}
2707
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002708static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002710 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002713 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Chuck Levera3ca5652012-03-01 17:00:40 -05002715 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002716 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2717 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002718 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 /* hmm. we are done with the inode, and in the process of freeing
2720 * the state_owner. we keep this around to process errors
2721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 switch (task->tk_status) {
2723 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002724 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002725 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002726 pnfs_roc_set_barrier(state->inode,
2727 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002728 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002729 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002730 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002732 case -NFS4ERR_OLD_STATEID:
2733 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002735 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002736 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002737 rpc_restart_call_prepare(task);
2738 goto out_release;
2739 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002740 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002741 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002743 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002744 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002745 goto out_release;
2746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002748 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2749 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002750out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002751 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002752 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002753 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002756static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002758 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002759 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002760 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002761 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002762 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002763
Chuck Levera3ca5652012-03-01 17:00:40 -05002764 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002765 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002766 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002767
Trond Myklebust88069f72009-12-08 08:33:16 -05002768 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002769 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002770 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2771 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2772 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002773 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002774 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002775 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002776 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002777 if (state->n_rdonly == 0)
2778 call_close |= is_rdonly;
2779 else if (is_rdonly)
2780 calldata->arg.fmode |= FMODE_READ;
2781 if (state->n_wronly == 0)
2782 call_close |= is_wronly;
2783 else if (is_wronly)
2784 calldata->arg.fmode |= FMODE_WRITE;
2785 } else if (is_rdwr)
2786 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2787
2788 if (calldata->arg.fmode == 0)
2789 call_close |= is_rdwr;
2790
Trond Myklebust5d422302013-03-14 16:57:48 -04002791 if (!nfs4_valid_open_stateid(state))
2792 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002793 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002794
2795 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002796 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002797 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002798 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002799
Peng Tao500d7012015-09-22 11:35:22 +08002800 if (nfs4_wait_on_layoutreturn(inode, task)) {
2801 nfs_release_seqid(calldata->arg.seqid);
2802 goto out_wait;
2803 }
2804
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002805 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002806 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002807 if (calldata->roc)
2808 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2809
Trond Myklebust6ae37332015-01-30 14:21:14 -05002810 calldata->arg.share_access =
2811 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2812 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002813
Trond Myklebust516a6af2005-10-27 22:12:41 -04002814 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002815 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002816 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002817 &calldata->arg.seq_args,
2818 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002819 task) != 0)
2820 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002821 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002822 return;
2823out_no_action:
2824 task->tk_action = NULL;
2825out_wait:
2826 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002829static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002830 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002831 .rpc_call_done = nfs4_close_done,
2832 .rpc_release = nfs4_free_closedata,
2833};
2834
Peng Taofe08c542014-07-03 13:05:01 +08002835static bool nfs4_roc(struct inode *inode)
2836{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002837 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002838 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002839 return pnfs_roc(inode);
2840}
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842/*
2843 * It is possible for data to be read/written from a mem-mapped file
2844 * after the sys_close call (which hits the vfs layer as a flush).
2845 * This means that we can't safely call nfsv4 close on a file until
2846 * the inode is cleared. This in turn means that we are not good
2847 * NFSv4 citizens - we do not indicate to the server to update the file's
2848 * share state even when we are done with one of the three share
2849 * stateid's in the inode.
2850 *
2851 * NOTE: Caller must be holding the sp->so_owner semaphore!
2852 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002853int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002855 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002856 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002858 struct nfs4_state_owner *sp = state->owner;
2859 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002860 struct rpc_message msg = {
2861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2862 .rpc_cred = state->owner->so_cred,
2863 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002864 struct rpc_task_setup task_setup_data = {
2865 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002866 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002867 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002868 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002869 .flags = RPC_TASK_ASYNC,
2870 };
Trond Myklebust95121352005-10-18 14:20:12 -07002871 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002873 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2874 &task_setup_data.rpc_client, &msg);
2875
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002876 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002878 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002879 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002880 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002882 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002884 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2885 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002886 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002887 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002888 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002889 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002890 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002891 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002892 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002893 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002894 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002895
Trond Myklebust1174dd12010-12-21 10:52:24 -05002896 msg.rpc_argp = &calldata->arg;
2897 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002898 task_setup_data.callback_data = calldata;
2899 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002900 if (IS_ERR(task))
2901 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002902 status = 0;
2903 if (wait)
2904 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002905 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002906 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002907out_free_calldata:
2908 kfree(calldata);
2909out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002910 nfs4_put_open_state(state);
2911 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002912 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
Trond Myklebust2b484292010-09-17 10:56:51 -04002915static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002916nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2917 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002920 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2921
2922 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Trond Myklebust565277f2007-10-15 18:17:53 -04002924 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002925 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002926
2927 nfs4_label_release_security(label);
2928
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002929 if (IS_ERR(state))
2930 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002931 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Trond Myklebust1185a552009-12-03 15:54:02 -05002934static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002935{
2936 if (ctx->state == NULL)
2937 return;
2938 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002939 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002940 else
Al Viro643168c2011-06-22 18:20:23 -04002941 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002942}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Trond Myklebustb944dba2013-11-04 15:20:20 -05002944#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2945#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002946#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2949{
Kinglong Mee8c612822015-08-26 21:12:58 +08002950 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04002951 struct nfs4_server_caps_arg args = {
2952 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08002953 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04002954 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 struct nfs4_server_caps_res res = {};
2956 struct rpc_message msg = {
2957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002958 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 .rpc_resp = &res,
2960 };
2961 int status;
2962
Kinglong Mee8c612822015-08-26 21:12:58 +08002963 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
2964 FATTR4_WORD0_FH_EXPIRE_TYPE |
2965 FATTR4_WORD0_LINK_SUPPORT |
2966 FATTR4_WORD0_SYMLINK_SUPPORT |
2967 FATTR4_WORD0_ACLSUPPORT;
2968 if (minorversion)
2969 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
2970
Bryan Schumaker7c513052011-03-24 17:12:24 +00002971 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002973 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08002974 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002975 case 0:
2976 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2977 res.attr_bitmask[2] = 0;
2978 break;
2979 case 1:
2980 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2981 break;
2982 case 2:
2983 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002986 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2987 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2988 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2989 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002990 NFS_CAP_CTIME|NFS_CAP_MTIME|
2991 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002992 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2993 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 server->caps |= NFS_CAP_ACLS;
2995 if (res.has_links != 0)
2996 server->caps |= NFS_CAP_HARDLINKS;
2997 if (res.has_symlinks != 0)
2998 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002999 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3000 server->caps |= NFS_CAP_FILEID;
3001 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3002 server->caps |= NFS_CAP_MODE;
3003 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3004 server->caps |= NFS_CAP_NLINK;
3005 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3006 server->caps |= NFS_CAP_OWNER;
3007 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3008 server->caps |= NFS_CAP_OWNER_GROUP;
3009 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3010 server->caps |= NFS_CAP_ATIME;
3011 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3012 server->caps |= NFS_CAP_CTIME;
3013 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3014 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003015#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3016 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3017 server->caps |= NFS_CAP_SECURITY_LABEL;
3018#endif
3019 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3020 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003021 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003022
Trond Myklebusta65318b2009-03-11 14:10:28 -04003023 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3024 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3025 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003026 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003027 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3028 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003030 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return status;
3034}
3035
Trond Myklebust55a97592006-06-09 09:34:19 -04003036int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 struct nfs4_exception exception = { };
3039 int err;
3040 do {
3041 err = nfs4_handle_exception(server,
3042 _nfs4_server_capabilities(server, fhandle),
3043 &exception);
3044 } while (exception.retry);
3045 return err;
3046}
3047
3048static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3049 struct nfs_fsinfo *info)
3050{
David Quigleyaa9c2662013-05-22 12:50:44 -04003051 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003053 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 };
3055 struct nfs4_lookup_res res = {
3056 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003057 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 .fh = fhandle,
3059 };
3060 struct rpc_message msg = {
3061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3062 .rpc_argp = &args,
3063 .rpc_resp = &res,
3064 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003065
David Quigleyaa9c2662013-05-22 12:50:44 -04003066 bitmask[0] = nfs4_fattr_bitmap[0];
3067 bitmask[1] = nfs4_fattr_bitmap[1];
3068 /*
3069 * Process the label in the upcoming getfattr
3070 */
3071 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3072
Trond Myklebust0e574af2005-10-27 22:12:38 -04003073 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003074 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075}
3076
3077static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3078 struct nfs_fsinfo *info)
3079{
3080 struct nfs4_exception exception = { };
3081 int err;
3082 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003083 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003084 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003085 switch (err) {
3086 case 0:
3087 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003088 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003089 default:
3090 err = nfs4_handle_exception(server, err, &exception);
3091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003093out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return err;
3095}
3096
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003097static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3098 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3099{
Trond Myklebustc2190662013-08-26 19:23:04 -04003100 struct rpc_auth_create_args auth_args = {
3101 .pseudoflavor = flavor,
3102 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003103 struct rpc_auth *auth;
3104 int ret;
3105
Trond Myklebustc2190662013-08-26 19:23:04 -04003106 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003107 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003108 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003109 goto out;
3110 }
3111 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003112out:
3113 return ret;
3114}
3115
Chuck Lever9a744ba2013-03-16 15:56:02 -04003116/*
3117 * Retry pseudoroot lookup with various security flavors. We do this when:
3118 *
3119 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3120 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3121 *
3122 * Returns zero on success, or a negative NFS4ERR value, or a
3123 * negative errno value.
3124 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003125static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003126 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003128 /* Per 3530bis 15.33.5 */
3129 static const rpc_authflavor_t flav_array[] = {
3130 RPC_AUTH_GSS_KRB5P,
3131 RPC_AUTH_GSS_KRB5I,
3132 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003133 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003134 RPC_AUTH_NULL,
3135 };
3136 int status = -EPERM;
3137 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003139 if (server->auth_info.flavor_len > 0) {
3140 /* try each flavor specified by user */
3141 for (i = 0; i < server->auth_info.flavor_len; i++) {
3142 status = nfs4_lookup_root_sec(server, fhandle, info,
3143 server->auth_info.flavors[i]);
3144 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3145 continue;
3146 break;
3147 }
3148 } else {
3149 /* no flavors specified by user, try default list */
3150 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3151 status = nfs4_lookup_root_sec(server, fhandle, info,
3152 flav_array[i]);
3153 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3154 continue;
3155 break;
3156 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003157 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003158
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003159 /*
3160 * -EACCESS could mean that the user doesn't have correct permissions
3161 * to access the mount. It could also mean that we tried to mount
3162 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3163 * existing mount programs don't handle -EACCES very well so it should
3164 * be mapped to -EPERM instead.
3165 */
3166 if (status == -EACCES)
3167 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003168 return status;
3169}
3170
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003171static int nfs4_do_find_root_sec(struct nfs_server *server,
3172 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3173{
3174 int mv = server->nfs_client->cl_minorversion;
3175 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3176}
3177
3178/**
3179 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3180 * @server: initialized nfs_server handle
3181 * @fhandle: we fill in the pseudo-fs root file handle
3182 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003183 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003184 *
3185 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003186 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003187int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003188 struct nfs_fsinfo *info,
3189 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003190{
Andre Przywarac7757072015-04-23 17:17:40 +01003191 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003192
Andre Przywarac7757072015-04-23 17:17:40 +01003193 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003194 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003195
3196 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003197 status = nfs4_do_find_root_sec(server, fhandle, info);
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (status == 0)
3200 status = nfs4_server_capabilities(server, fhandle);
3201 if (status == 0)
3202 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003203
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003204 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Bryan Schumakerbae36242012-05-10 15:07:31 -04003207static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3208 struct nfs_fsinfo *info)
3209{
3210 int error;
3211 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003212 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003213
3214 error = nfs4_server_capabilities(server, mntfh);
3215 if (error < 0) {
3216 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3217 return error;
3218 }
3219
David Quigley14c43f72013-05-22 12:50:43 -04003220 label = nfs4_label_alloc(server, GFP_KERNEL);
3221 if (IS_ERR(label))
3222 return PTR_ERR(label);
3223
David Quigley1775fd32013-05-22 12:50:42 -04003224 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003225 if (error < 0) {
3226 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003227 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003228 }
3229
3230 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3231 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3232 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3233
David Quigley14c43f72013-05-22 12:50:43 -04003234err_free_label:
3235 nfs4_label_free(label);
3236
Bryan Schumakerbae36242012-05-10 15:07:31 -04003237 return error;
3238}
3239
Manoj Naik6b97fd32006-06-09 09:34:29 -04003240/*
3241 * Get locations and (maybe) other attributes of a referral.
3242 * Note that we'll actually follow the referral later when
3243 * we detect fsid mismatch in inode revalidation
3244 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003245static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3246 const struct qstr *name, struct nfs_fattr *fattr,
3247 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003248{
3249 int status = -ENOMEM;
3250 struct page *page = NULL;
3251 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003252
3253 page = alloc_page(GFP_KERNEL);
3254 if (page == NULL)
3255 goto out;
3256 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3257 if (locations == NULL)
3258 goto out;
3259
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003260 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003261 if (status != 0)
3262 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003263
3264 /*
3265 * If the fsid didn't change, this is a migration event, not a
3266 * referral. Cause us to drop into the exception handler, which
3267 * will kick off migration recovery.
3268 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003269 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003270 dprintk("%s: server did not return a different fsid for"
3271 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003272 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003273 goto out;
3274 }
Andy Adamson533eb462011-06-13 18:25:56 -04003275 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3276 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003277
Andy Adamson533eb462011-06-13 18:25:56 -04003278 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003279 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003280 memset(fhandle, 0, sizeof(struct nfs_fh));
3281out:
3282 if (page)
3283 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003284 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003285 return status;
3286}
3287
David Quigley1775fd32013-05-22 12:50:42 -04003288static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3289 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
3291 struct nfs4_getattr_arg args = {
3292 .fh = fhandle,
3293 .bitmask = server->attr_bitmask,
3294 };
3295 struct nfs4_getattr_res res = {
3296 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003297 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 .server = server,
3299 };
3300 struct rpc_message msg = {
3301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3302 .rpc_argp = &args,
3303 .rpc_resp = &res,
3304 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003305
3306 args.bitmask = nfs4_bitmask(server, label);
3307
Trond Myklebust0e574af2005-10-27 22:12:38 -04003308 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003309 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
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_exception exception = { };
3316 int err;
3317 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003318 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3319 trace_nfs4_getattr(server, fhandle, fattr, err);
3320 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 &exception);
3322 } while (exception.retry);
3323 return err;
3324}
3325
3326/*
3327 * The file is not closed if it is opened due to the a request to change
3328 * the size of the file. The open call will not be needed once the
3329 * VFS layer lookup-intents are implemented.
3330 *
3331 * Close is called when the inode is destroyed.
3332 * If we haven't opened the file for O_WRONLY, we
3333 * need to in the size_change case to obtain a stateid.
3334 *
3335 * Got race?
3336 * Because OPEN is always done by name in nfsv4, it is
3337 * possible that we opened a different file by the same
3338 * name. We can recognize this race condition, but we
3339 * can't do anything about it besides returning an error.
3340 *
3341 * This will be fixed with VFS changes (lookup-intent).
3342 */
3343static int
3344nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3345 struct iattr *sattr)
3346{
David Howells2b0143b2015-03-17 22:25:59 +00003347 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003348 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003349 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003350 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 int status;
3352
Peng Tao88ac8152014-09-12 11:04:10 +08003353 if (pnfs_ld_layoutret_on_setattr(inode) &&
3354 sattr->ia_valid & ATTR_SIZE &&
3355 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003356 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003357
Trond Myklebust0e574af2005-10-27 22:12:38 -04003358 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Andy Adamson26699402012-05-30 16:12:24 -04003360 /* Deal with open(O_TRUNC) */
3361 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003362 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003363
3364 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003365 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003366 return 0;
3367
Trond Myklebustd5308382005-11-04 15:33:38 -05003368 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003369 if (sattr->ia_valid & ATTR_FILE) {
3370 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003371
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003372 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003373 if (ctx) {
3374 cred = ctx->cred;
3375 state = ctx->state;
3376 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003377 }
3378
David Quigley14c43f72013-05-22 12:50:43 -04003379 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3380 if (IS_ERR(label))
3381 return PTR_ERR(label);
3382
3383 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003384 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003385 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003386 nfs_setsecurity(inode, fattr, label);
3387 }
David Quigley14c43f72013-05-22 12:50:43 -04003388 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 return status;
3390}
3391
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003392static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3393 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003394 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003395{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003396 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003397 int status;
3398 struct nfs4_lookup_arg args = {
3399 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003400 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003401 .name = name,
3402 };
3403 struct nfs4_lookup_res res = {
3404 .server = server,
3405 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003406 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003407 .fh = fhandle,
3408 };
3409 struct rpc_message msg = {
3410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3411 .rpc_argp = &args,
3412 .rpc_resp = &res,
3413 };
3414
David Quigleyaa9c2662013-05-22 12:50:44 -04003415 args.bitmask = nfs4_bitmask(server, label);
3416
David Howells2b3de442006-08-22 20:06:09 -04003417 nfs_fattr_init(fattr);
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003420 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 dprintk("NFS reply lookup: %d\n", status);
3422 return status;
3423}
3424
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003425static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003426{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003427 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003428 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003429 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3430 fattr->nlink = 2;
3431}
3432
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003433static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3434 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003435 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003436{
3437 struct nfs4_exception exception = { };
3438 struct rpc_clnt *client = *clnt;
3439 int err;
3440 do {
David Quigley1775fd32013-05-22 12:50:42 -04003441 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003442 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003443 switch (err) {
3444 case -NFS4ERR_BADNAME:
3445 err = -ENOENT;
3446 goto out;
3447 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003448 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003449 if (err == -NFS4ERR_MOVED)
3450 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003451 goto out;
3452 case -NFS4ERR_WRONGSEC:
3453 err = -EPERM;
3454 if (client != *clnt)
3455 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003456 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003457 if (IS_ERR(client))
3458 return PTR_ERR(client);
3459
3460 exception.retry = 1;
3461 break;
3462 default:
3463 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3464 }
3465 } while (exception.retry);
3466
3467out:
3468 if (err == 0)
3469 *clnt = client;
3470 else if (client != *clnt)
3471 rpc_shutdown_client(client);
3472
3473 return err;
3474}
3475
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003476static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003477 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3478 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003480 int status;
3481 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003482
David Quigley1775fd32013-05-22 12:50:42 -04003483 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003484 if (client != NFS_CLIENT(dir)) {
3485 rpc_shutdown_client(client);
3486 nfs_fixup_secinfo_attributes(fattr);
3487 }
3488 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003491struct rpc_clnt *
3492nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3493 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3494{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003495 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003496 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003497
David Quigley1775fd32013-05-22 12:50:42 -04003498 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003499 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003500 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003501 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003502}
3503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3505{
Trond Myklebust76b32992007-08-10 17:45:11 -04003506 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 struct nfs4_accessargs args = {
3508 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003509 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003511 struct nfs4_accessres res = {
3512 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003513 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 struct rpc_message msg = {
3515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3516 .rpc_argp = &args,
3517 .rpc_resp = &res,
3518 .rpc_cred = entry->cred,
3519 };
3520 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003521 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
3523 /*
3524 * Determine which access bits we want to ask for...
3525 */
3526 if (mode & MAY_READ)
3527 args.access |= NFS4_ACCESS_READ;
3528 if (S_ISDIR(inode->i_mode)) {
3529 if (mode & MAY_WRITE)
3530 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3531 if (mode & MAY_EXEC)
3532 args.access |= NFS4_ACCESS_LOOKUP;
3533 } else {
3534 if (mode & MAY_WRITE)
3535 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3536 if (mode & MAY_EXEC)
3537 args.access |= NFS4_ACCESS_EXECUTE;
3538 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003539
3540 res.fattr = nfs_alloc_fattr();
3541 if (res.fattr == NULL)
3542 return -ENOMEM;
3543
Bryan Schumaker7c513052011-03-24 17:12:24 +00003544 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003546 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003547 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003549 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 return status;
3551}
3552
3553static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3554{
3555 struct nfs4_exception exception = { };
3556 int err;
3557 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003558 err = _nfs4_proc_access(inode, entry);
3559 trace_nfs4_access(inode, err);
3560 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 &exception);
3562 } while (exception.retry);
3563 return err;
3564}
3565
3566/*
3567 * TODO: For the time being, we don't try to get any attributes
3568 * along with any of the zero-copy operations READ, READDIR,
3569 * READLINK, WRITE.
3570 *
3571 * In the case of the first three, we want to put the GETATTR
3572 * after the read-type operation -- this is because it is hard
3573 * to predict the length of a GETATTR response in v4, and thus
3574 * align the READ data correctly. This means that the GETATTR
3575 * may end up partially falling into the page cache, and we should
3576 * shift it into the 'tail' of the xdr_buf before processing.
3577 * To do this efficiently, we need to know the total length
3578 * of data received, which doesn't seem to be available outside
3579 * of the RPC layer.
3580 *
3581 * In the case of WRITE, we also want to put the GETATTR after
3582 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003583 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 *
3585 * Both of these changes to the XDR layer would in fact be quite
3586 * minor, but I decided to leave them for a subsequent patch.
3587 */
3588static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3589 unsigned int pgbase, unsigned int pglen)
3590{
3591 struct nfs4_readlink args = {
3592 .fh = NFS_FH(inode),
3593 .pgbase = pgbase,
3594 .pglen = pglen,
3595 .pages = &page,
3596 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003597 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 struct rpc_message msg = {
3599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3600 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003601 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 };
3603
Bryan Schumaker7c513052011-03-24 17:12:24 +00003604 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 -07003605}
3606
3607static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3608 unsigned int pgbase, unsigned int pglen)
3609{
3610 struct nfs4_exception exception = { };
3611 int err;
3612 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003613 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3614 trace_nfs4_readlink(inode, err);
3615 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 &exception);
3617 } while (exception.retry);
3618 return err;
3619}
3620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003622 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624static int
3625nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003626 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
David Quigleyaa9c2662013-05-22 12:50:44 -04003628 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003629 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 int status = 0;
3632
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003633 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3634 if (IS_ERR(ctx))
3635 return PTR_ERR(ctx);
3636
David Quigleyaa9c2662013-05-22 12:50:44 -04003637 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3638
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003639 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003640 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 if (IS_ERR(state)) {
3642 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003643 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003646 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003647 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return status;
3649}
3650
3651static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3652{
Trond Myklebust16e42952005-10-27 22:12:44 -04003653 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003654 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003656 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003658 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003659 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003660 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3663 .rpc_argp = &args,
3664 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003666 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003667
Bryan Schumaker7c513052011-03-24 17:12:24 +00003668 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003669 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003670 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return status;
3672}
3673
3674static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3675{
3676 struct nfs4_exception exception = { };
3677 int err;
3678 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003679 err = _nfs4_proc_remove(dir, name);
3680 trace_nfs4_remove(dir, name, err);
3681 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 &exception);
3683 } while (exception.retry);
3684 return err;
3685}
3686
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003687static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003689 struct nfs_server *server = NFS_SERVER(dir);
3690 struct nfs_removeargs *args = msg->rpc_argp;
3691 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003693 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003695 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003696
3697 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698}
3699
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003700static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3701{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003702 nfs4_setup_sequence(NFS_SERVER(data->dir),
3703 &data->args.seq_args,
3704 &data->res.seq_res,
3705 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
3707
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003708static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003710 struct nfs_unlinkdata *data = task->tk_calldata;
3711 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003712
Trond Myklebust14516c32010-07-31 14:29:06 -04003713 if (!nfs4_sequence_done(task, &res->seq_res))
3714 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003715 if (nfs4_async_handle_error(task, res->server, NULL,
3716 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003717 return 0;
3718 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003719 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
Jeff Laytond3d41522010-09-17 17:31:57 -04003722static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3723{
3724 struct nfs_server *server = NFS_SERVER(dir);
3725 struct nfs_renameargs *arg = msg->rpc_argp;
3726 struct nfs_renameres *res = msg->rpc_resp;
3727
3728 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003729 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003730 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003731}
3732
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003733static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3734{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003735 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3736 &data->args.seq_args,
3737 &data->res.seq_res,
3738 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003739}
3740
3741static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3742 struct inode *new_dir)
3743{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003744 struct nfs_renamedata *data = task->tk_calldata;
3745 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003746
3747 if (!nfs4_sequence_done(task, &res->seq_res))
3748 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003749 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003750 return 0;
3751
3752 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003753 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003754 return 1;
3755}
3756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3758{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003759 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 struct nfs4_link_arg arg = {
3761 .fh = NFS_FH(inode),
3762 .dir_fh = NFS_FH(dir),
3763 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003764 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003766 struct nfs4_link_res res = {
3767 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003768 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003769 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 struct rpc_message msg = {
3771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3772 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003773 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003775 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Trond Myklebust136f2622010-04-16 16:22:49 -04003777 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003778 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003779 goto out;
3780
David Quigley14c43f72013-05-22 12:50:43 -04003781 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3782 if (IS_ERR(res.label)) {
3783 status = PTR_ERR(res.label);
3784 goto out;
3785 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003786 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003787
Bryan Schumaker7c513052011-03-24 17:12:24 +00003788 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003789 if (!status) {
3790 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003791 status = nfs_post_op_update_inode(inode, res.fattr);
3792 if (!status)
3793 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003794 }
David Quigley14c43f72013-05-22 12:50:43 -04003795
3796
3797 nfs4_label_free(res.label);
3798
Trond Myklebust136f2622010-04-16 16:22:49 -04003799out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003800 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 return status;
3802}
3803
3804static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3805{
3806 struct nfs4_exception exception = { };
3807 int err;
3808 do {
3809 err = nfs4_handle_exception(NFS_SERVER(inode),
3810 _nfs4_proc_link(inode, dir, name),
3811 &exception);
3812 } while (exception.retry);
3813 return err;
3814}
3815
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003816struct nfs4_createdata {
3817 struct rpc_message msg;
3818 struct nfs4_create_arg arg;
3819 struct nfs4_create_res res;
3820 struct nfs_fh fh;
3821 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003822 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003823};
3824
3825static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3826 struct qstr *name, struct iattr *sattr, u32 ftype)
3827{
3828 struct nfs4_createdata *data;
3829
3830 data = kzalloc(sizeof(*data), GFP_KERNEL);
3831 if (data != NULL) {
3832 struct nfs_server *server = NFS_SERVER(dir);
3833
David Quigley14c43f72013-05-22 12:50:43 -04003834 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3835 if (IS_ERR(data->label))
3836 goto out_free;
3837
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003838 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3839 data->msg.rpc_argp = &data->arg;
3840 data->msg.rpc_resp = &data->res;
3841 data->arg.dir_fh = NFS_FH(dir);
3842 data->arg.server = server;
3843 data->arg.name = name;
3844 data->arg.attrs = sattr;
3845 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003846 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003847 data->res.server = server;
3848 data->res.fh = &data->fh;
3849 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003850 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003851 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003852 }
3853 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003854out_free:
3855 kfree(data);
3856 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003857}
3858
3859static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3860{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003861 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003862 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003863 if (status == 0) {
3864 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003865 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003866 }
3867 return status;
3868}
3869
3870static void nfs4_free_createdata(struct nfs4_createdata *data)
3871{
David Quigley14c43f72013-05-22 12:50:43 -04003872 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003873 kfree(data);
3874}
3875
Chuck Lever4f390c12006-08-22 20:06:22 -04003876static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003877 struct page *page, unsigned int len, struct iattr *sattr,
3878 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003880 struct nfs4_createdata *data;
3881 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Chuck Lever94a6d752006-08-22 20:06:23 -04003883 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003884 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003885
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003886 status = -ENOMEM;
3887 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3888 if (data == NULL)
3889 goto out;
3890
3891 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3892 data->arg.u.symlink.pages = &page;
3893 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003894 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003896 status = nfs4_do_create(dir, dentry, data);
3897
3898 nfs4_free_createdata(data);
3899out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 return status;
3901}
3902
Chuck Lever4f390c12006-08-22 20:06:22 -04003903static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003904 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905{
3906 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003907 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003909
3910 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003913 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3914 trace_nfs4_symlink(dir, &dentry->d_name, err);
3915 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 &exception);
3917 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003918
3919 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 return err;
3921}
3922
3923static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003924 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003926 struct nfs4_createdata *data;
3927 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003929 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3930 if (data == NULL)
3931 goto out;
3932
David Quigley1775fd32013-05-22 12:50:42 -04003933 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003934 status = nfs4_do_create(dir, dentry, data);
3935
3936 nfs4_free_createdata(data);
3937out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 return status;
3939}
3940
3941static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3942 struct iattr *sattr)
3943{
3944 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003945 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003947
David Quigleyaa9c2662013-05-22 12:50:44 -04003948 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3949
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003950 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003952 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3953 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3954 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 &exception);
3956 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003957 nfs4_label_release_security(label);
3958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 return err;
3960}
3961
3962static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003963 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
David Howells2b0143b2015-03-17 22:25:59 +00003965 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 struct nfs4_readdir_arg args = {
3967 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003968 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 .pgbase = 0,
3970 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00003971 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003972 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 };
3974 struct nfs4_readdir_res res;
3975 struct rpc_message msg = {
3976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3977 .rpc_argp = &args,
3978 .rpc_resp = &res,
3979 .rpc_cred = cred,
3980 };
3981 int status;
3982
Al Viro6de14722013-09-16 10:53:17 -04003983 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3984 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003985 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003986 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003988 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 -05003989 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003990 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003991 status += args.pgbase;
3992 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003993
3994 nfs_invalidate_atime(dir);
3995
Harvey Harrison3110ff82008-05-02 13:42:44 -07003996 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 return status;
3998}
3999
4000static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004001 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
4003 struct nfs4_exception exception = { };
4004 int err;
4005 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004006 err = _nfs4_proc_readdir(dentry, cred, cookie,
4007 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004008 trace_nfs4_readdir(d_inode(dentry), err);
4009 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 &exception);
4011 } while (exception.retry);
4012 return err;
4013}
4014
4015static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004016 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004018 struct nfs4_createdata *data;
4019 int mode = sattr->ia_mode;
4020 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004022 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4023 if (data == NULL)
4024 goto out;
4025
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004027 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004029 data->arg.ftype = NF4BLK;
4030 data->arg.u.device.specdata1 = MAJOR(rdev);
4031 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 }
4033 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004034 data->arg.ftype = NF4CHR;
4035 data->arg.u.device.specdata1 = MAJOR(rdev);
4036 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004037 } else if (!S_ISSOCK(mode)) {
4038 status = -EINVAL;
4039 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
David Quigley1775fd32013-05-22 12:50:42 -04004041
David Quigleyaa9c2662013-05-22 12:50:44 -04004042 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004043 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004044out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004045 nfs4_free_createdata(data);
4046out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 return status;
4048}
4049
4050static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4051 struct iattr *sattr, dev_t rdev)
4052{
4053 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004054 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004056
David Quigleyaa9c2662013-05-22 12:50:44 -04004057 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4058
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004059 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004061 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4062 trace_nfs4_mknod(dir, &dentry->d_name, err);
4063 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 &exception);
4065 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004066
4067 nfs4_label_release_security(label);
4068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 return err;
4070}
4071
4072static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4073 struct nfs_fsstat *fsstat)
4074{
4075 struct nfs4_statfs_arg args = {
4076 .fh = fhandle,
4077 .bitmask = server->attr_bitmask,
4078 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004079 struct nfs4_statfs_res res = {
4080 .fsstat = fsstat,
4081 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 struct rpc_message msg = {
4083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4084 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004085 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 };
4087
Trond Myklebust0e574af2005-10-27 22:12:38 -04004088 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004089 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091
4092static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4093{
4094 struct nfs4_exception exception = { };
4095 int err;
4096 do {
4097 err = nfs4_handle_exception(server,
4098 _nfs4_proc_statfs(server, fhandle, fsstat),
4099 &exception);
4100 } while (exception.retry);
4101 return err;
4102}
4103
4104static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4105 struct nfs_fsinfo *fsinfo)
4106{
4107 struct nfs4_fsinfo_arg args = {
4108 .fh = fhandle,
4109 .bitmask = server->attr_bitmask,
4110 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004111 struct nfs4_fsinfo_res res = {
4112 .fsinfo = fsinfo,
4113 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 struct rpc_message msg = {
4115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4116 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004117 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 };
4119
Bryan Schumaker7c513052011-03-24 17:12:24 +00004120 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121}
4122
4123static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4124{
4125 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004126 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 int err;
4128
4129 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004130 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004131 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004132 if (err == 0) {
4133 struct nfs_client *clp = server->nfs_client;
4134
4135 spin_lock(&clp->cl_lock);
4136 clp->cl_lease_time = fsinfo->lease_time * HZ;
4137 clp->cl_last_renewal = now;
4138 spin_unlock(&clp->cl_lock);
4139 break;
4140 }
4141 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 } while (exception.retry);
4143 return err;
4144}
4145
4146static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4147{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004148 int error;
4149
Trond Myklebust0e574af2005-10-27 22:12:38 -04004150 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004151 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004152 if (error == 0) {
4153 /* block layout checks this! */
4154 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004155 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004156 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004157
4158 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159}
4160
4161static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4162 struct nfs_pathconf *pathconf)
4163{
4164 struct nfs4_pathconf_arg args = {
4165 .fh = fhandle,
4166 .bitmask = server->attr_bitmask,
4167 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004168 struct nfs4_pathconf_res res = {
4169 .pathconf = pathconf,
4170 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 struct rpc_message msg = {
4172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4173 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004174 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 };
4176
4177 /* None of the pathconf attributes are mandatory to implement */
4178 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4179 memset(pathconf, 0, sizeof(*pathconf));
4180 return 0;
4181 }
4182
Trond Myklebust0e574af2005-10-27 22:12:38 -04004183 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004184 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185}
4186
4187static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4188 struct nfs_pathconf *pathconf)
4189{
4190 struct nfs4_exception exception = { };
4191 int err;
4192
4193 do {
4194 err = nfs4_handle_exception(server,
4195 _nfs4_proc_pathconf(server, fhandle, pathconf),
4196 &exception);
4197 } while (exception.retry);
4198 return err;
4199}
4200
Trond Myklebust5521abf2013-03-16 20:54:34 -04004201int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004202 const struct nfs_open_context *ctx,
4203 const struct nfs_lock_context *l_ctx,
4204 fmode_t fmode)
4205{
4206 const struct nfs_lockowner *lockowner = NULL;
4207
4208 if (l_ctx != NULL)
4209 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004210 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004211}
4212EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4213
Trond Myklebust5521abf2013-03-16 20:54:34 -04004214static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4215 const struct nfs_open_context *ctx,
4216 const struct nfs_lock_context *l_ctx,
4217 fmode_t fmode)
4218{
4219 nfs4_stateid current_stateid;
4220
Trond Myklebuste1253be2014-03-05 08:44:23 -05004221 /* If the current stateid represents a lost lock, then exit */
4222 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4223 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004224 return nfs4_stateid_match(stateid, &current_stateid);
4225}
4226
4227static bool nfs4_error_stateid_expired(int err)
4228{
4229 switch (err) {
4230 case -NFS4ERR_DELEG_REVOKED:
4231 case -NFS4ERR_ADMIN_REVOKED:
4232 case -NFS4ERR_BAD_STATEID:
4233 case -NFS4ERR_STALE_STATEID:
4234 case -NFS4ERR_OLD_STATEID:
4235 case -NFS4ERR_OPENMODE:
4236 case -NFS4ERR_EXPIRED:
4237 return true;
4238 }
4239 return false;
4240}
4241
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004242void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004243{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004244 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004245}
4246
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004247static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004249 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004251 trace_nfs4_read(hdr, task->tk_status);
4252 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004253 hdr->args.context->state,
4254 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004255 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004256 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004258
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004259 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004261 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263}
4264
Trond Myklebust5521abf2013-03-16 20:54:34 -04004265static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004266 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004267{
4268
4269 if (!nfs4_error_stateid_expired(task->tk_status) ||
4270 nfs4_stateid_is_current(&args->stateid,
4271 args->context,
4272 args->lock_context,
4273 FMODE_READ))
4274 return false;
4275 rpc_restart_call_prepare(task);
4276 return true;
4277}
4278
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004279static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004280{
4281
4282 dprintk("--> %s\n", __func__);
4283
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004284 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004285 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004286 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004287 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004288 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4289 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004290}
4291
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004292static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4293 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004295 hdr->timestamp = jiffies;
4296 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004297 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004298 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004301static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4302 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004303{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004304 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4305 &hdr->args.seq_args,
4306 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004307 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004308 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004309 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4310 hdr->args.lock_context,
4311 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004312 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004313 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004314 return -EIO;
4315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004318static int nfs4_write_done_cb(struct rpc_task *task,
4319 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004321 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004322
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004323 trace_nfs4_write(hdr, task->tk_status);
4324 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004325 hdr->args.context->state,
4326 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004327 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004328 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004330 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004331 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004332 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004333 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335}
4336
Trond Myklebust5521abf2013-03-16 20:54:34 -04004337static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004338 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004339{
4340
4341 if (!nfs4_error_stateid_expired(task->tk_status) ||
4342 nfs4_stateid_is_current(&args->stateid,
4343 args->context,
4344 args->lock_context,
4345 FMODE_WRITE))
4346 return false;
4347 rpc_restart_call_prepare(task);
4348 return true;
4349}
4350
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004351static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004352{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004353 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004354 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004355 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004356 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004357 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4358 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004359}
4360
Trond Myklebust5a37f852012-04-28 14:55:16 -04004361static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004362bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004363{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004364 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004365 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004366 return false;
4367 /* Otherwise, request attributes if and only if we don't hold
4368 * a delegation
4369 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004370 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004371}
Fred Isamana69aef12011-03-03 15:13:47 +00004372
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004373static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4374 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004376 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004377
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004378 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4379 hdr->args.bitmask = NULL;
4380 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004381 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004382 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004383
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004384 if (!hdr->pgio_done_cb)
4385 hdr->pgio_done_cb = nfs4_write_done_cb;
4386 hdr->res.server = server;
4387 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004389 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004390 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391}
4392
Fred Isaman0b7c0152012-04-20 14:47:39 -04004393static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4394{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004395 nfs4_setup_sequence(NFS_SERVER(data->inode),
4396 &data->args.seq_args,
4397 &data->res.seq_res,
4398 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399}
4400
Fred Isaman0b7c0152012-04-20 14:47:39 -04004401static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004404
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004405 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004406 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4407 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004408 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004409 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412}
4413
Fred Isaman0b7c0152012-04-20 14:47:39 -04004414static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004415{
4416 if (!nfs4_sequence_done(task, &data->res.seq_res))
4417 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004418 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004419}
4420
Fred Isaman0b7c0152012-04-20 14:47:39 -04004421static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004423 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004424
Fred Isaman0b7c0152012-04-20 14:47:39 -04004425 if (data->commit_done_cb == NULL)
4426 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004427 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004428 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004429 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430}
4431
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004432struct nfs4_renewdata {
4433 struct nfs_client *client;
4434 unsigned long timestamp;
4435};
4436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437/*
4438 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4439 * standalone procedure for queueing an asynchronous RENEW.
4440 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004441static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004442{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004443 struct nfs4_renewdata *data = calldata;
4444 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004445
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004446 if (atomic_read(&clp->cl_count) > 1)
4447 nfs4_schedule_state_renewal(clp);
4448 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004449 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004450}
4451
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004452static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004454 struct nfs4_renewdata *data = calldata;
4455 struct nfs_client *clp = data->client;
4456 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004458 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004459 switch (task->tk_status) {
4460 case 0:
4461 break;
4462 case -NFS4ERR_LEASE_MOVED:
4463 nfs4_schedule_lease_moved_recovery(clp);
4464 break;
4465 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004466 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004467 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4468 return;
4469 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004470 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004471 return;
4472 }
4473 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004475 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476}
4477
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004478static const struct rpc_call_ops nfs4_renew_ops = {
4479 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004480 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004481};
4482
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004483static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484{
4485 struct rpc_message msg = {
4486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4487 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004488 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004490 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004492 if (renew_flags == 0)
4493 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004494 if (!atomic_inc_not_zero(&clp->cl_count))
4495 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004496 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004497 if (data == NULL)
4498 return -ENOMEM;
4499 data->client = clp;
4500 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004501 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004502 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004505static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506{
4507 struct rpc_message msg = {
4508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4509 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004510 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 };
4512 unsigned long now = jiffies;
4513 int status;
4514
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004515 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 if (status < 0)
4517 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004518 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 return 0;
4520}
4521
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004522static inline int nfs4_server_supports_acls(struct nfs_server *server)
4523{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004524 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004525}
4526
Trond Myklebust21f498c2012-08-24 10:59:25 -04004527/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4528 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004529 * the stack.
4530 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004531#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004532
Neil Hormane9e3d722011-03-04 19:26:03 -05004533static int buf_to_pages_noslab(const void *buf, size_t buflen,
4534 struct page **pages, unsigned int *pgbase)
4535{
4536 struct page *newpage, **spages;
4537 int rc = 0;
4538 size_t len;
4539 spages = pages;
4540
4541 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004542 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004543 newpage = alloc_page(GFP_KERNEL);
4544
4545 if (newpage == NULL)
4546 goto unwind;
4547 memcpy(page_address(newpage), buf, len);
4548 buf += len;
4549 buflen -= len;
4550 *pages++ = newpage;
4551 rc++;
4552 } while (buflen != 0);
4553
4554 return rc;
4555
4556unwind:
4557 for(; rc > 0; rc--)
4558 __free_page(spages[rc-1]);
4559 return -ENOMEM;
4560}
4561
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004562struct nfs4_cached_acl {
4563 int cached;
4564 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004565 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004566};
4567
4568static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004569{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004570 struct nfs_inode *nfsi = NFS_I(inode);
4571
4572 spin_lock(&inode->i_lock);
4573 kfree(nfsi->nfs4_acl);
4574 nfsi->nfs4_acl = acl;
4575 spin_unlock(&inode->i_lock);
4576}
4577
4578static void nfs4_zap_acl_attr(struct inode *inode)
4579{
4580 nfs4_set_cached_acl(inode, NULL);
4581}
4582
4583static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4584{
4585 struct nfs_inode *nfsi = NFS_I(inode);
4586 struct nfs4_cached_acl *acl;
4587 int ret = -ENOENT;
4588
4589 spin_lock(&inode->i_lock);
4590 acl = nfsi->nfs4_acl;
4591 if (acl == NULL)
4592 goto out;
4593 if (buf == NULL) /* user is just asking for length */
4594 goto out_len;
4595 if (acl->cached == 0)
4596 goto out;
4597 ret = -ERANGE; /* see getxattr(2) man page */
4598 if (acl->len > buflen)
4599 goto out;
4600 memcpy(buf, acl->data, acl->len);
4601out_len:
4602 ret = acl->len;
4603out:
4604 spin_unlock(&inode->i_lock);
4605 return ret;
4606}
4607
Sachin Prabhu5794d212012-04-17 14:36:40 +01004608static 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 +00004609{
4610 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004611 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004612
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004613 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004614 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004615 if (acl == NULL)
4616 goto out;
4617 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004618 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004619 } else {
4620 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4621 if (acl == NULL)
4622 goto out;
4623 acl->cached = 0;
4624 }
4625 acl->len = acl_len;
4626out:
4627 nfs4_set_cached_acl(inode, acl);
4628}
4629
Andy Adamsonbf118a32011-12-07 11:55:27 -05004630/*
4631 * The getxattr API returns the required buffer length when called with a
4632 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4633 * the required buf. On a NULL buf, we send a page of data to the server
4634 * guessing that the ACL request can be serviced by a page. If so, we cache
4635 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4636 * the cache. If not so, we throw away the page, and cache the required
4637 * length. The next getxattr call will then produce another round trip to
4638 * the server, this time with the input buf of the required size.
4639 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004640static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004641{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004642 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004643 struct nfs_getaclargs args = {
4644 .fh = NFS_FH(inode),
4645 .acl_pages = pages,
4646 .acl_len = buflen,
4647 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004648 struct nfs_getaclres res = {
4649 .acl_len = buflen,
4650 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004651 struct rpc_message msg = {
4652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4653 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004654 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004655 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004656 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4657 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004658
Andy Adamsonbf118a32011-12-07 11:55:27 -05004659 /* As long as we're doing a round trip to the server anyway,
4660 * let's be prepared for a page of acl data. */
4661 if (npages == 0)
4662 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004663 if (npages > ARRAY_SIZE(pages))
4664 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004665
Andy Adamsonbf118a32011-12-07 11:55:27 -05004666 for (i = 0; i < npages; i++) {
4667 pages[i] = alloc_page(GFP_KERNEL);
4668 if (!pages[i])
4669 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004670 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004671
4672 /* for decoding across pages */
4673 res.acl_scratch = alloc_page(GFP_KERNEL);
4674 if (!res.acl_scratch)
4675 goto out_free;
4676
Andy Adamsonbf118a32011-12-07 11:55:27 -05004677 args.acl_len = npages * PAGE_SIZE;
4678 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004679
Peng Taode040be2012-01-10 22:42:47 +08004680 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004681 __func__, buf, buflen, npages, args.acl_len);
4682 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4683 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004684 if (ret)
4685 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004686
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004687 /* Handle the case where the passed-in buffer is too short */
4688 if (res.acl_flags & NFS4_ACL_TRUNC) {
4689 /* Did the user only issue a request for the acl length? */
4690 if (buf == NULL)
4691 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004692 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004693 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004694 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004695 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004696 if (buf) {
4697 if (res.acl_len > buflen) {
4698 ret = -ERANGE;
4699 goto out_free;
4700 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004701 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004702 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004703out_ok:
4704 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004705out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004706 for (i = 0; i < npages; i++)
4707 if (pages[i])
4708 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004709 if (res.acl_scratch)
4710 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004711 return ret;
4712}
4713
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004714static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4715{
4716 struct nfs4_exception exception = { };
4717 ssize_t ret;
4718 do {
4719 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004720 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004721 if (ret >= 0)
4722 break;
4723 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4724 } while (exception.retry);
4725 return ret;
4726}
4727
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004728static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4729{
4730 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004731 int ret;
4732
4733 if (!nfs4_server_supports_acls(server))
4734 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004735 ret = nfs_revalidate_inode(server, inode);
4736 if (ret < 0)
4737 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004738 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4739 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004740 ret = nfs4_read_cached_acl(inode, buf, buflen);
4741 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004742 /* -ENOENT is returned if there is no ACL or if there is an ACL
4743 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004744 return ret;
4745 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004746}
4747
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004748static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004749{
4750 struct nfs_server *server = NFS_SERVER(inode);
4751 struct page *pages[NFS4ACL_MAXPAGES];
4752 struct nfs_setaclargs arg = {
4753 .fh = NFS_FH(inode),
4754 .acl_pages = pages,
4755 .acl_len = buflen,
4756 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004757 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004758 struct rpc_message msg = {
4759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4760 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004761 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004762 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004763 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004764 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004765
4766 if (!nfs4_server_supports_acls(server))
4767 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004768 if (npages > ARRAY_SIZE(pages))
4769 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004770 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4771 if (i < 0)
4772 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004773 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004774 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004775
4776 /*
4777 * Free each page after tx, so the only ref left is
4778 * held by the network stack
4779 */
4780 for (; i > 0; i--)
4781 put_page(pages[i-1]);
4782
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004783 /*
4784 * Acl update can result in inode attribute update.
4785 * so mark the attribute cache invalid.
4786 */
4787 spin_lock(&inode->i_lock);
4788 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4789 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004790 nfs_access_zap_cache(inode);
4791 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004792 return ret;
4793}
4794
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004795static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4796{
4797 struct nfs4_exception exception = { };
4798 int err;
4799 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004800 err = __nfs4_proc_set_acl(inode, buf, buflen);
4801 trace_nfs4_set_acl(inode, err);
4802 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004803 &exception);
4804 } while (exception.retry);
4805 return err;
4806}
4807
David Quigleyaa9c2662013-05-22 12:50:44 -04004808#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4809static int _nfs4_get_security_label(struct inode *inode, void *buf,
4810 size_t buflen)
4811{
4812 struct nfs_server *server = NFS_SERVER(inode);
4813 struct nfs_fattr fattr;
4814 struct nfs4_label label = {0, 0, buflen, buf};
4815
4816 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004817 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004818 .fh = NFS_FH(inode),
4819 .bitmask = bitmask,
4820 };
4821 struct nfs4_getattr_res res = {
4822 .fattr = &fattr,
4823 .label = &label,
4824 .server = server,
4825 };
4826 struct rpc_message msg = {
4827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004828 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004829 .rpc_resp = &res,
4830 };
4831 int ret;
4832
4833 nfs_fattr_init(&fattr);
4834
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004835 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004836 if (ret)
4837 return ret;
4838 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4839 return -ENOENT;
4840 if (buflen < label.len)
4841 return -ERANGE;
4842 return 0;
4843}
4844
4845static int nfs4_get_security_label(struct inode *inode, void *buf,
4846 size_t buflen)
4847{
4848 struct nfs4_exception exception = { };
4849 int err;
4850
4851 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4852 return -EOPNOTSUPP;
4853
4854 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004855 err = _nfs4_get_security_label(inode, buf, buflen);
4856 trace_nfs4_get_security_label(inode, err);
4857 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004858 &exception);
4859 } while (exception.retry);
4860 return err;
4861}
4862
4863static int _nfs4_do_set_security_label(struct inode *inode,
4864 struct nfs4_label *ilabel,
4865 struct nfs_fattr *fattr,
4866 struct nfs4_label *olabel)
4867{
4868
4869 struct iattr sattr = {0};
4870 struct nfs_server *server = NFS_SERVER(inode);
4871 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004872 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004873 .fh = NFS_FH(inode),
4874 .iap = &sattr,
4875 .server = server,
4876 .bitmask = bitmask,
4877 .label = ilabel,
4878 };
4879 struct nfs_setattrres res = {
4880 .fattr = fattr,
4881 .label = olabel,
4882 .server = server,
4883 };
4884 struct rpc_message msg = {
4885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004886 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004887 .rpc_resp = &res,
4888 };
4889 int status;
4890
Jeff Layton12207f62013-11-01 10:49:32 -04004891 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004892
Jeff Layton12207f62013-11-01 10:49:32 -04004893 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004894 if (status)
4895 dprintk("%s failed: %d\n", __func__, status);
4896
4897 return status;
4898}
4899
4900static int nfs4_do_set_security_label(struct inode *inode,
4901 struct nfs4_label *ilabel,
4902 struct nfs_fattr *fattr,
4903 struct nfs4_label *olabel)
4904{
4905 struct nfs4_exception exception = { };
4906 int err;
4907
4908 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004909 err = _nfs4_do_set_security_label(inode, ilabel,
4910 fattr, olabel);
4911 trace_nfs4_set_security_label(inode, err);
4912 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004913 &exception);
4914 } while (exception.retry);
4915 return err;
4916}
4917
4918static int
4919nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4920{
4921 struct nfs4_label ilabel, *olabel = NULL;
4922 struct nfs_fattr fattr;
4923 struct rpc_cred *cred;
David Howells2b0143b2015-03-17 22:25:59 +00004924 struct inode *inode = d_inode(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004925 int status;
4926
4927 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4928 return -EOPNOTSUPP;
4929
4930 nfs_fattr_init(&fattr);
4931
4932 ilabel.pi = 0;
4933 ilabel.lfs = 0;
4934 ilabel.label = (char *)buf;
4935 ilabel.len = buflen;
4936
4937 cred = rpc_lookup_cred();
4938 if (IS_ERR(cred))
4939 return PTR_ERR(cred);
4940
4941 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4942 if (IS_ERR(olabel)) {
4943 status = -PTR_ERR(olabel);
4944 goto out;
4945 }
4946
4947 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4948 if (status == 0)
4949 nfs_setsecurity(inode, &fattr, olabel);
4950
4951 nfs4_label_free(olabel);
4952out:
4953 put_rpccred(cred);
4954 return status;
4955}
4956#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4957
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004960nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4961 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004963 struct nfs_client *clp = server->nfs_client;
4964
4965 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 return 0;
4967 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004968 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004969 case -NFS4ERR_ADMIN_REVOKED:
4970 case -NFS4ERR_BAD_STATEID:
4971 case -NFS4ERR_OPENMODE:
4972 if (state == NULL)
4973 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004974 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004975 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004976 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004977 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004978 if (state != NULL) {
4979 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004980 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004983 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004984 nfs4_schedule_lease_recovery(clp);
4985 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004986 case -NFS4ERR_MOVED:
4987 if (nfs4_schedule_migration_recovery(server) < 0)
4988 goto recovery_failed;
4989 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004990 case -NFS4ERR_LEASE_MOVED:
4991 nfs4_schedule_lease_moved_recovery(clp);
4992 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004993#if defined(CONFIG_NFS_V4_1)
4994 case -NFS4ERR_BADSESSION:
4995 case -NFS4ERR_BADSLOT:
4996 case -NFS4ERR_BAD_HIGH_SLOT:
4997 case -NFS4ERR_DEADSESSION:
4998 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4999 case -NFS4ERR_SEQ_FALSE_RETRY:
5000 case -NFS4ERR_SEQ_MISORDERED:
5001 dprintk("%s ERROR %d, Reset session\n", __func__,
5002 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04005003 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05005004 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04005005#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005007 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10005008 rpc_delay(task, nfs4_update_delay(timeout));
5009 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05005010 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005012 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04005014 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 }
5016 task->tk_status = nfs4_map_errors(task->tk_status);
5017 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04005018recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04005019 task->tk_status = -EIO;
5020 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005021wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005022 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005023 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
5024 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04005025 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
5026 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04005027restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005028 task->tk_status = 0;
5029 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030}
5031
Chuck Leverf0920752012-05-21 22:45:41 -04005032static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5033 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005034{
5035 __be32 verf[2];
5036
Chuck Lever2c820d92012-05-21 22:45:33 -04005037 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5038 /* An impossible timestamp guarantees this value
5039 * will never match a generated boot time. */
5040 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005041 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005042 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005043 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005044 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5045 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005046 }
Chuck Levercd937102012-03-02 17:14:31 -05005047 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5048}
5049
Jeff Laytona3192682015-06-09 19:43:59 -04005050static int
5051nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005052{
Jeff Laytona3192682015-06-09 19:43:59 -04005053 int result;
5054 size_t len;
5055 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005056
Trond Myklebustceb3a162015-01-03 15:16:04 -05005057 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005058 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005059
Jeff Laytona3192682015-06-09 19:43:59 -04005060 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005061 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005062 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5063 1 +
5064 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5065 1;
5066 rcu_read_unlock();
5067
5068 if (len > NFS4_OPAQUE_LIMIT + 1)
5069 return -EINVAL;
5070
5071 /*
5072 * Since this string is allocated at mount time, and held until the
5073 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5074 * about a memory-reclaim deadlock.
5075 */
5076 str = kmalloc(len, GFP_KERNEL);
5077 if (!str)
5078 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005079
Chuck Levere984a552012-09-14 17:24:21 -04005080 rcu_read_lock();
Jeff Laytona3192682015-06-09 19:43:59 -04005081 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5082 clp->cl_ipaddr,
5083 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5084 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005085 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005086
Jeff Laytona3192682015-06-09 19:43:59 -04005087 clp->cl_owner_id = str;
5088 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005089}
5090
Jeff Layton873e3852015-06-09 19:44:00 -04005091static int
5092nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005093{
Jeff Layton873e3852015-06-09 19:44:00 -04005094 int result;
5095 size_t len;
5096 char *str;
5097
5098 len = 10 + 10 + 1 + 10 + 1 +
5099 strlen(nfs4_client_id_uniquifier) + 1 +
5100 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5101
5102 if (len > NFS4_OPAQUE_LIMIT + 1)
5103 return -EINVAL;
5104
5105 /*
5106 * Since this string is allocated at mount time, and held until the
5107 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5108 * about a memory-reclaim deadlock.
5109 */
5110 str = kmalloc(len, GFP_KERNEL);
5111 if (!str)
5112 return -ENOMEM;
5113
5114 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5115 clp->rpc_ops->version, clp->cl_minorversion,
5116 nfs4_client_id_uniquifier,
5117 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005118 clp->cl_owner_id = str;
5119 return 0;
5120}
5121
5122static int
5123nfs4_init_uniform_client_string(struct nfs_client *clp)
5124{
5125 int result;
5126 size_t len;
5127 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005128
5129 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005130 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005131
5132 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005133 return nfs4_init_uniquifier_client_string(clp);
5134
5135 len = 10 + 10 + 1 + 10 + 1 +
5136 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5137
5138 if (len > NFS4_OPAQUE_LIMIT + 1)
5139 return -EINVAL;
5140
5141 /*
5142 * Since this string is allocated at mount time, and held until the
5143 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5144 * about a memory-reclaim deadlock.
5145 */
5146 str = kmalloc(len, GFP_KERNEL);
5147 if (!str)
5148 return -ENOMEM;
5149
5150 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5151 clp->rpc_ops->version, clp->cl_minorversion,
5152 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005153 clp->cl_owner_id = str;
5154 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005155}
5156
Chuck Lever706cb8d2014-03-12 12:51:47 -04005157/*
5158 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5159 * services. Advertise one based on the address family of the
5160 * clientaddr.
5161 */
5162static unsigned int
5163nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5164{
5165 if (strchr(clp->cl_ipaddr, ':') != NULL)
5166 return scnprintf(buf, len, "tcp6");
5167 else
5168 return scnprintf(buf, len, "tcp");
5169}
5170
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005171static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5172{
5173 struct nfs4_setclientid *sc = calldata;
5174
5175 if (task->tk_status == 0)
5176 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5177}
5178
5179static const struct rpc_call_ops nfs4_setclientid_ops = {
5180 .rpc_call_done = nfs4_setclientid_done,
5181};
5182
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005183/**
5184 * nfs4_proc_setclientid - Negotiate client ID
5185 * @clp: state data structure
5186 * @program: RPC program for NFSv4 callback service
5187 * @port: IP port number for NFS4 callback service
5188 * @cred: RPC credential to use for this call
5189 * @res: where to place the result
5190 *
5191 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5192 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005193int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5194 unsigned short port, struct rpc_cred *cred,
5195 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196{
5197 nfs4_verifier sc_verifier;
5198 struct nfs4_setclientid setclientid = {
5199 .sc_verifier = &sc_verifier,
5200 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005201 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 };
5203 struct rpc_message msg = {
5204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5205 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005206 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005207 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005209 struct rpc_task *task;
5210 struct rpc_task_setup task_setup_data = {
5211 .rpc_client = clp->cl_rpcclient,
5212 .rpc_message = &msg,
5213 .callback_ops = &nfs4_setclientid_ops,
5214 .callback_data = &setclientid,
5215 .flags = RPC_TASK_TIMEOUT,
5216 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005217 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
Chuck Leverde734832012-07-11 16:30:50 -04005219 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005220 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005221
5222 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5223 status = nfs4_init_uniform_client_string(clp);
5224 else
Jeff Laytona3192682015-06-09 19:43:59 -04005225 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005226
5227 if (status)
5228 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005229
Chuck Leverde734832012-07-11 16:30:50 -04005230 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005231 setclientid.sc_netid_len =
5232 nfs4_init_callback_netid(clp,
5233 setclientid.sc_netid,
5234 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005235 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005236 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 clp->cl_ipaddr, port >> 8, port & 255);
5238
Jeff Layton3a6bb732015-06-09 19:43:57 -04005239 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005240 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005241 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005242 task = rpc_run_task(&task_setup_data);
5243 if (IS_ERR(task)) {
5244 status = PTR_ERR(task);
5245 goto out;
5246 }
5247 status = task->tk_status;
5248 if (setclientid.sc_cred) {
5249 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5250 put_rpccred(setclientid.sc_cred);
5251 }
5252 rpc_put_task(task);
5253out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005254 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005255 dprintk("NFS reply setclientid: %d\n", status);
5256 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257}
5258
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005259/**
5260 * nfs4_proc_setclientid_confirm - Confirm client ID
5261 * @clp: state data structure
5262 * @res: result of a previous SETCLIENTID
5263 * @cred: RPC credential to use for this call
5264 *
5265 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5266 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005267int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005268 struct nfs4_setclientid_res *arg,
5269 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 struct rpc_message msg = {
5272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005273 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005274 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 int status;
5277
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005278 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5279 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5280 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005281 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005282 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005283 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 return status;
5285}
5286
Trond Myklebustfe650402006-01-03 09:55:18 +01005287struct nfs4_delegreturndata {
5288 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005289 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005290 struct nfs_fh fh;
5291 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005292 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005293 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005294 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005295 struct inode *inode;
5296 bool roc;
5297 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005298};
5299
Trond Myklebustfe650402006-01-03 09:55:18 +01005300static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5301{
5302 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005303
Trond Myklebust14516c32010-07-31 14:29:06 -04005304 if (!nfs4_sequence_done(task, &data->res.seq_res))
5305 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005306
Trond Myklebustca8acf82013-08-13 10:36:56 -04005307 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005308 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005309 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005310 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005311 case -NFS4ERR_ADMIN_REVOKED:
5312 case -NFS4ERR_DELEG_REVOKED:
5313 case -NFS4ERR_BAD_STATEID:
5314 case -NFS4ERR_OLD_STATEID:
5315 case -NFS4ERR_STALE_STATEID:
5316 case -NFS4ERR_EXPIRED:
5317 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005318 if (data->roc)
5319 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005320 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005321 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005322 if (nfs4_async_handle_error(task, data->res.server,
5323 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005324 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005325 return;
5326 }
5327 }
5328 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005329}
5330
5331static void nfs4_delegreturn_release(void *calldata)
5332{
Peng Tao039b7562014-07-03 13:05:02 +08005333 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005334 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005335
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005336 if (inode) {
5337 if (data->roc)
5338 pnfs_roc_release(inode);
5339 nfs_iput_and_deactive(inode);
5340 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005341 kfree(calldata);
5342}
5343
Andy Adamson938e1012009-04-01 09:22:28 -04005344static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5345{
5346 struct nfs4_delegreturndata *d_data;
5347
5348 d_data = (struct nfs4_delegreturndata *)data;
5349
Peng Tao500d7012015-09-22 11:35:22 +08005350 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5351 return;
5352
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005353 if (d_data->roc)
5354 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005355
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005356 nfs4_setup_sequence(d_data->res.server,
5357 &d_data->args.seq_args,
5358 &d_data->res.seq_res,
5359 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005360}
Andy Adamson938e1012009-04-01 09:22:28 -04005361
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005362static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005363 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005364 .rpc_call_done = nfs4_delegreturn_done,
5365 .rpc_release = nfs4_delegreturn_release,
5366};
5367
Trond Myklebuste6f81072008-01-24 18:14:34 -05005368static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005369{
5370 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005371 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005372 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005373 struct rpc_message msg = {
5374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5375 .rpc_cred = cred,
5376 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005377 struct rpc_task_setup task_setup_data = {
5378 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005379 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005380 .callback_ops = &nfs4_delegreturn_ops,
5381 .flags = RPC_TASK_ASYNC,
5382 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005383 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005384
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005385 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005386 if (data == NULL)
5387 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005388 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005389 data->args.fhandle = &data->fh;
5390 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005391 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005392 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005393 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005394 data->res.fattr = &data->fattr;
5395 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005396 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005397 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005398 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005399 data->inode = nfs_igrab_and_active(inode);
5400 if (data->inode)
5401 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005402
Trond Myklebustc970aa82007-07-14 15:39:59 -04005403 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005404 msg.rpc_argp = &data->args;
5405 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005406 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005407 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005408 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005409 if (!issync)
5410 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005411 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005412 if (status != 0)
5413 goto out;
5414 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005415 if (status == 0)
5416 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5417 else
5418 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005419out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005420 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005421 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422}
5423
Trond Myklebuste6f81072008-01-24 18:14:34 -05005424int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425{
5426 struct nfs_server *server = NFS_SERVER(inode);
5427 struct nfs4_exception exception = { };
5428 int err;
5429 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005430 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005431 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 switch (err) {
5433 case -NFS4ERR_STALE_STATEID:
5434 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 case 0:
5436 return 0;
5437 }
5438 err = nfs4_handle_exception(server, err, &exception);
5439 } while (exception.retry);
5440 return err;
5441}
5442
5443#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5444#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5445
5446/*
5447 * sleep, with exponential backoff, and retry the LOCK operation.
5448 */
5449static unsigned long
5450nfs4_set_lock_task_retry(unsigned long timeout)
5451{
Colin Cross416ad3c2013-05-06 23:50:06 +00005452 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 timeout <<= 1;
5454 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5455 return NFS4_LOCK_MAXTIMEOUT;
5456 return timeout;
5457}
5458
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5460{
5461 struct inode *inode = state->inode;
5462 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005463 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005464 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005466 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005468 struct nfs_lockt_res res = {
5469 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 };
5471 struct rpc_message msg = {
5472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5473 .rpc_argp = &arg,
5474 .rpc_resp = &res,
5475 .rpc_cred = state->owner->so_cred,
5476 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 struct nfs4_lock_state *lsp;
5478 int status;
5479
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005480 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005481 status = nfs4_set_lock_state(state, request);
5482 if (status != 0)
5483 goto out;
5484 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005485 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005486 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005487 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005488 switch (status) {
5489 case 0:
5490 request->fl_type = F_UNLCK;
5491 break;
5492 case -NFS4ERR_DENIED:
5493 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005495 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005496 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005497out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 return status;
5499}
5500
5501static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5502{
5503 struct nfs4_exception exception = { };
5504 int err;
5505
5506 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005507 err = _nfs4_proc_getlk(state, cmd, request);
5508 trace_nfs4_get_lock(request, state, cmd, err);
5509 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 &exception);
5511 } while (exception.retry);
5512 return err;
5513}
5514
Jeff Layton83bfff22015-07-11 06:43:03 -04005515static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516{
5517 int res = 0;
5518 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5519 case FL_POSIX:
Jeff Layton83bfff22015-07-11 06:43:03 -04005520 res = posix_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 break;
5522 case FL_FLOCK:
Jeff Layton83bfff22015-07-11 06:43:03 -04005523 res = flock_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 break;
5525 default:
5526 BUG();
5527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 return res;
5529}
5530
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005531struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005532 struct nfs_locku_args arg;
5533 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005534 struct nfs4_lock_state *lsp;
5535 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005536 struct file_lock fl;
5537 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005538 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005539};
5540
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005541static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5542 struct nfs_open_context *ctx,
5543 struct nfs4_lock_state *lsp,
5544 struct nfs_seqid *seqid)
5545{
5546 struct nfs4_unlockdata *p;
5547 struct inode *inode = lsp->ls_state->inode;
5548
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005549 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005550 if (p == NULL)
5551 return NULL;
5552 p->arg.fh = NFS_FH(inode);
5553 p->arg.fl = &p->fl;
5554 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005555 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005556 p->lsp = lsp;
5557 atomic_inc(&lsp->ls_count);
5558 /* Ensure we don't close file until we're done freeing locks! */
5559 p->ctx = get_nfs_open_context(ctx);
5560 memcpy(&p->fl, fl, sizeof(p->fl));
5561 p->server = NFS_SERVER(inode);
5562 return p;
5563}
5564
Trond Myklebust06f814a2006-01-03 09:55:07 +01005565static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005566{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005567 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005568 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005569 nfs4_put_lock_state(calldata->lsp);
5570 put_nfs_open_context(calldata->ctx);
5571 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005572}
5573
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005574static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005575{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005576 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005577
Trond Myklebust14516c32010-07-31 14:29:06 -04005578 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5579 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005580 switch (task->tk_status) {
5581 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005582 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005583 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005584 if (nfs4_update_lock_stateid(calldata->lsp,
5585 &calldata->res.stateid))
5586 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005587 case -NFS4ERR_BAD_STATEID:
5588 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005589 case -NFS4ERR_STALE_STATEID:
5590 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005591 if (!nfs4_stateid_match(&calldata->arg.stateid,
5592 &calldata->lsp->ls_stateid))
5593 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005594 break;
5595 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005596 if (nfs4_async_handle_error(task, calldata->server,
5597 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005598 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005599 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005600 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005601}
5602
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005603static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005604{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005605 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005607 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005608 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005609 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005610 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005611 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005612 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005613 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005614 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005615 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005616 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005617 &calldata->res.seq_res,
5618 task) != 0)
5619 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005620 return;
5621out_no_action:
5622 task->tk_action = NULL;
5623out_wait:
5624 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625}
5626
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005627static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005628 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005629 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005630 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005631};
5632
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005633static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5634 struct nfs_open_context *ctx,
5635 struct nfs4_lock_state *lsp,
5636 struct nfs_seqid *seqid)
5637{
5638 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005639 struct rpc_message msg = {
5640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5641 .rpc_cred = ctx->cred,
5642 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005643 struct rpc_task_setup task_setup_data = {
5644 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005645 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005646 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005647 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005648 .flags = RPC_TASK_ASYNC,
5649 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005650
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005651 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5652 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5653
Frank Filz137d6ac2007-07-09 15:32:29 -07005654 /* Ensure this is an unlock - when canceling a lock, the
5655 * canceled lock is passed in, and it won't be an unlock.
5656 */
5657 fl->fl_type = F_UNLCK;
5658
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005659 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5660 if (data == NULL) {
5661 nfs_free_seqid(seqid);
5662 return ERR_PTR(-ENOMEM);
5663 }
5664
Chuck Levera9c92d62013-08-09 12:48:18 -04005665 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005666 msg.rpc_argp = &data->arg;
5667 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005668 task_setup_data.callback_data = data;
5669 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005670}
5671
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5673{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005674 struct inode *inode = state->inode;
5675 struct nfs4_state_owner *sp = state->owner;
5676 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005677 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005678 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005679 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005680 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005681 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005682 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Trond Myklebust9b073572006-06-29 16:38:34 -04005684 status = nfs4_set_lock_state(state, request);
5685 /* Unlock _before_ we do the RPC call */
5686 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005687 /* Exclude nfs_delegation_claim_locks() */
5688 mutex_lock(&sp->so_delegreturn_mutex);
5689 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005690 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005691 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005692 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005693 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005694 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005695 }
5696 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005697 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005698 if (status != 0)
5699 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005700 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005701 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005702 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5703 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005704 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5705 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005706 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005707 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005708 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005709 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005710 status = PTR_ERR(task);
5711 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005712 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005713 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005714 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005715out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005716 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005717 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005718 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719}
5720
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005721struct nfs4_lockdata {
5722 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005723 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005724 struct nfs4_lock_state *lsp;
5725 struct nfs_open_context *ctx;
5726 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005727 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005728 int rpc_status;
5729 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005730 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005731};
5732
5733static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005734 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5735 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005736{
5737 struct nfs4_lockdata *p;
5738 struct inode *inode = lsp->ls_state->inode;
5739 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005740 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005741
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005742 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005743 if (p == NULL)
5744 return NULL;
5745
5746 p->arg.fh = NFS_FH(inode);
5747 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005748 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005749 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005750 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005751 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5752 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005753 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005754 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005755 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005756 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005757 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005758 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005759 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005760 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005761 atomic_inc(&lsp->ls_count);
5762 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005763 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005764 memcpy(&p->fl, fl, sizeof(p->fl));
5765 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005766out_free_seqid:
5767 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005768out_free:
5769 kfree(p);
5770 return NULL;
5771}
5772
5773static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5774{
5775 struct nfs4_lockdata *data = calldata;
5776 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
Harvey Harrison3110ff82008-05-02 13:42:44 -07005778 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005779 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005780 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005781 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005782 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005783 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005784 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005785 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005786 nfs4_stateid_copy(&data->arg.open_stateid,
5787 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005788 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005789 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005790 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005791 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005792 nfs4_stateid_copy(&data->arg.lock_stateid,
5793 &data->lsp->ls_stateid);
5794 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005795 if (!nfs4_valid_open_stateid(state)) {
5796 data->rpc_status = -EBADF;
5797 task->tk_action = NULL;
5798 goto out_release_open_seqid;
5799 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005800 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005801 if (nfs4_setup_sequence(data->server,
5802 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005803 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005804 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005805 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005806out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005807 nfs_release_seqid(data->arg.open_seqid);
5808out_release_lock_seqid:
5809 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005810out_wait:
5811 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005812 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005813}
5814
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005815static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5816{
5817 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005818 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005819
Harvey Harrison3110ff82008-05-02 13:42:44 -07005820 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005821
Trond Myklebust14516c32010-07-31 14:29:06 -04005822 if (!nfs4_sequence_done(task, &data->res.seq_res))
5823 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005824
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005825 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005826 switch (task->tk_status) {
5827 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005828 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005829 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005830 if (data->arg.new_lock) {
5831 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005832 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005833 rpc_restart_call_prepare(task);
5834 break;
5835 }
5836 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005837 if (data->arg.new_lock_owner != 0) {
5838 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5839 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5840 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5841 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5842 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005843 break;
5844 case -NFS4ERR_BAD_STATEID:
5845 case -NFS4ERR_OLD_STATEID:
5846 case -NFS4ERR_STALE_STATEID:
5847 case -NFS4ERR_EXPIRED:
5848 if (data->arg.new_lock_owner != 0) {
5849 if (!nfs4_stateid_match(&data->arg.open_stateid,
5850 &lsp->ls_state->open_stateid))
5851 rpc_restart_call_prepare(task);
5852 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5853 &lsp->ls_stateid))
5854 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005855 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005856 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005857}
5858
5859static void nfs4_lock_release(void *calldata)
5860{
5861 struct nfs4_lockdata *data = calldata;
5862
Harvey Harrison3110ff82008-05-02 13:42:44 -07005863 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005864 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005865 if (data->cancelled != 0) {
5866 struct rpc_task *task;
5867 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5868 data->arg.lock_seqid);
5869 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005870 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005871 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005872 } else
5873 nfs_free_seqid(data->arg.lock_seqid);
5874 nfs4_put_lock_state(data->lsp);
5875 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005876 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005877 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005878 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005879}
5880
5881static const struct rpc_call_ops nfs4_lock_ops = {
5882 .rpc_call_prepare = nfs4_lock_prepare,
5883 .rpc_call_done = nfs4_lock_done,
5884 .rpc_release = nfs4_lock_release,
5885};
5886
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005887static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5888{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005889 switch (error) {
5890 case -NFS4ERR_ADMIN_REVOKED:
5891 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005892 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005893 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005894 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005895 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005896 break;
5897 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005898 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005899 case -NFS4ERR_EXPIRED:
5900 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005901 };
5902}
5903
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005904static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005905{
5906 struct nfs4_lockdata *data;
5907 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005908 struct rpc_message msg = {
5909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5910 .rpc_cred = state->owner->so_cred,
5911 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005912 struct rpc_task_setup task_setup_data = {
5913 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005914 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005915 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005916 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005917 .flags = RPC_TASK_ASYNC,
5918 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005919 int ret;
5920
Harvey Harrison3110ff82008-05-02 13:42:44 -07005921 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005922 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005923 fl->fl_u.nfs4_fl.owner,
5924 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005925 if (data == NULL)
5926 return -ENOMEM;
5927 if (IS_SETLKW(cmd))
5928 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005929 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005930 msg.rpc_argp = &data->arg;
5931 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005932 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005933 if (recovery_type > NFS_LOCK_NEW) {
5934 if (recovery_type == NFS_LOCK_RECLAIM)
5935 data->arg.reclaim = NFS_LOCK_RECLAIM;
5936 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005937 } else
5938 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005939 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005940 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005941 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005942 ret = nfs4_wait_for_completion_rpc_task(task);
5943 if (ret == 0) {
5944 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005945 if (ret)
5946 nfs4_handle_setlk_error(data->server, data->lsp,
5947 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005948 } else
5949 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005950 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005951 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005952 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953}
5954
5955static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5956{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005957 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005958 struct nfs4_exception exception = {
5959 .inode = state->inode,
5960 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005961 int err;
5962
5963 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005964 /* Cache the lock if possible... */
5965 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5966 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005967 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005968 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005969 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005970 break;
5971 nfs4_handle_exception(server, err, &exception);
5972 } while (exception.retry);
5973 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974}
5975
5976static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5977{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005978 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005979 struct nfs4_exception exception = {
5980 .inode = state->inode,
5981 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005982 int err;
5983
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005984 err = nfs4_set_lock_state(state, request);
5985 if (err != 0)
5986 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005987 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005988 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5989 return 0;
5990 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005991 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005992 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5993 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005994 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005995 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005996 switch (err) {
5997 default:
5998 goto out;
5999 case -NFS4ERR_GRACE:
6000 case -NFS4ERR_DELAY:
6001 nfs4_handle_exception(server, err, &exception);
6002 err = 0;
6003 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006004 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006005out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007}
6008
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006009#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006010/**
6011 * nfs41_check_expired_locks - possibly free a lock stateid
6012 *
6013 * @state: NFSv4 state for an inode
6014 *
6015 * Returns NFS_OK if recovery for this stateid is now finished.
6016 * Otherwise a negative NFS4ERR value is returned.
6017 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006018static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006019{
Chuck Levereb64cf92012-07-11 16:30:05 -04006020 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006021 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006022 struct nfs_server *server = NFS_SERVER(state->inode);
6023
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006024 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006025 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006026 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6027
6028 status = nfs41_test_stateid(server,
6029 &lsp->ls_stateid,
6030 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006031 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006032 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006033 /* Free the stateid unless the server
6034 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006035 if (status != -NFS4ERR_BAD_STATEID)
6036 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006037 &lsp->ls_stateid,
6038 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006039 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006040 ret = status;
6041 }
6042 }
6043 };
6044
6045 return ret;
6046}
6047
6048static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6049{
6050 int status = NFS_OK;
6051
6052 if (test_bit(LK_STATE_IN_USE, &state->flags))
6053 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006054 if (status != NFS_OK)
6055 status = nfs4_lock_expired(state, request);
6056 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006057}
6058#endif
6059
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6061{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006062 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006063 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006064 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006066 if ((fl_flags & FL_POSIX) &&
6067 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6068 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006069 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006070 status = nfs4_set_lock_state(state, request);
6071 if (status != 0)
6072 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006073 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006074 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006075 if (status < 0)
6076 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006077 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006078 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006079 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006080 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006081 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006082 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006083 up_read(&nfsi->rwsem);
6084 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006085 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006086 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006087 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006088out:
6089 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 return status;
6091}
6092
6093static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6094{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006095 struct nfs4_exception exception = {
6096 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006097 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006098 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 int err;
6100
6101 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006102 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006103 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07006104 if (err == -NFS4ERR_DENIED)
6105 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006107 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 } while (exception.retry);
6109 return err;
6110}
6111
6112static int
6113nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6114{
6115 struct nfs_open_context *ctx;
6116 struct nfs4_state *state;
6117 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6118 int status;
6119
6120 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006121 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 state = ctx->state;
6123
6124 if (request->fl_start < 0 || request->fl_end < 0)
6125 return -EINVAL;
6126
Trond Myklebustd9531262009-07-21 19:22:38 -04006127 if (IS_GETLK(cmd)) {
6128 if (state != NULL)
6129 return nfs4_proc_getlk(state, F_GETLK, request);
6130 return 0;
6131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132
6133 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6134 return -EINVAL;
6135
Trond Myklebustd9531262009-07-21 19:22:38 -04006136 if (request->fl_type == F_UNLCK) {
6137 if (state != NULL)
6138 return nfs4_proc_unlck(state, cmd, request);
6139 return 0;
6140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
Trond Myklebustd9531262009-07-21 19:22:38 -04006142 if (state == NULL)
6143 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006144 /*
6145 * Don't rely on the VFS having checked the file open mode,
6146 * since it won't do this for flock() locks.
6147 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006148 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006149 case F_RDLCK:
6150 if (!(filp->f_mode & FMODE_READ))
6151 return -EBADF;
6152 break;
6153 case F_WRLCK:
6154 if (!(filp->f_mode & FMODE_WRITE))
6155 return -EBADF;
6156 }
6157
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 do {
6159 status = nfs4_proc_setlk(state, cmd, request);
6160 if ((status != -EAGAIN) || IS_SETLK(cmd))
6161 break;
6162 timeout = nfs4_set_lock_task_retry(timeout);
6163 status = -ERESTARTSYS;
6164 if (signalled())
6165 break;
6166 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 return status;
6168}
6169
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006170int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006171{
6172 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006173 int err;
6174
6175 err = nfs4_set_lock_state(state, fl);
6176 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006177 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006178 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006179 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006180}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006181
Trond Myklebustcf470c32012-03-07 13:49:12 -05006182struct nfs_release_lockowner_data {
6183 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006184 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006185 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006186 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006187 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006188};
6189
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006190static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6191{
6192 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006193 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006194 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6195 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006196 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006197 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006198}
6199
6200static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6201{
6202 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006203 struct nfs_server *server = data->server;
6204
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006205 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006206
6207 switch (task->tk_status) {
6208 case 0:
6209 renew_lease(server, data->timestamp);
6210 break;
6211 case -NFS4ERR_STALE_CLIENTID:
6212 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006213 nfs4_schedule_lease_recovery(server->nfs_client);
6214 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006215 case -NFS4ERR_LEASE_MOVED:
6216 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006217 if (nfs4_async_handle_error(task, server,
6218 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006219 rpc_restart_call_prepare(task);
6220 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006221}
6222
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006223static void nfs4_release_lockowner_release(void *calldata)
6224{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006225 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006226 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006227 kfree(calldata);
6228}
6229
Trond Myklebust17280172012-03-11 13:11:00 -04006230static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006231 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6232 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006233 .rpc_release = nfs4_release_lockowner_release,
6234};
6235
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006236static void
6237nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006238{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006239 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006240 struct rpc_message msg = {
6241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6242 };
6243
6244 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006245 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006246
Trond Myklebustcf470c32012-03-07 13:49:12 -05006247 data = kmalloc(sizeof(*data), GFP_NOFS);
6248 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006249 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006250 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006251 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006252 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6253 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6254 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006255
Trond Myklebustcf470c32012-03-07 13:49:12 -05006256 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006257 msg.rpc_resp = &data->res;
6258 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006259 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006260}
6261
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006262#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6263
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006264static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6265 const void *buf, size_t buflen,
6266 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006267{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006268 if (strcmp(key, "") != 0)
6269 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006270
David Howells2b0143b2015-03-17 22:25:59 +00006271 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006272}
6273
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006274static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6275 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006276{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006277 if (strcmp(key, "") != 0)
6278 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006279
David Howells2b0143b2015-03-17 22:25:59 +00006280 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006281}
6282
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006283static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6284 size_t list_len, const char *name,
6285 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006286{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006287 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006288
David Howells2b0143b2015-03-17 22:25:59 +00006289 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
J. Bruce Fields096455a2006-03-20 23:23:42 -05006290 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006291
6292 if (list && len <= list_len)
6293 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006294 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006295}
6296
David Quigleyc9bccef2013-05-22 12:50:45 -04006297#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6298static inline int nfs4_server_supports_labels(struct nfs_server *server)
6299{
6300 return server->caps & NFS_CAP_SECURITY_LABEL;
6301}
6302
6303static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6304 const void *buf, size_t buflen,
6305 int flags, int type)
6306{
6307 if (security_ismaclabel(key))
6308 return nfs4_set_security_label(dentry, buf, buflen);
6309
6310 return -EOPNOTSUPP;
6311}
6312
6313static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6314 void *buf, size_t buflen, int type)
6315{
6316 if (security_ismaclabel(key))
David Howells2b0143b2015-03-17 22:25:59 +00006317 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006318 return -EOPNOTSUPP;
6319}
6320
6321static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6322 size_t list_len, const char *name,
6323 size_t name_len, int type)
6324{
6325 size_t len = 0;
6326
David Howells2b0143b2015-03-17 22:25:59 +00006327 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6328 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
David Quigleyc9bccef2013-05-22 12:50:45 -04006329 if (list && len <= list_len)
David Howells2b0143b2015-03-17 22:25:59 +00006330 security_inode_listsecurity(d_inode(dentry), list, len);
David Quigleyc9bccef2013-05-22 12:50:45 -04006331 }
6332 return len;
6333}
6334
6335static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6336 .prefix = XATTR_SECURITY_PREFIX,
6337 .list = nfs4_xattr_list_nfs4_label,
6338 .get = nfs4_xattr_get_nfs4_label,
6339 .set = nfs4_xattr_set_nfs4_label,
6340};
6341#endif
6342
6343
Andy Adamson533eb462011-06-13 18:25:56 -04006344/*
6345 * nfs_fhget will use either the mounted_on_fileid or the fileid
6346 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006347static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6348{
Andy Adamson533eb462011-06-13 18:25:56 -04006349 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6350 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6351 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006352 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006353 return;
6354
6355 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006356 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006357 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6358 fattr->nlink = 2;
6359}
6360
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006361static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6362 const struct qstr *name,
6363 struct nfs4_fs_locations *fs_locations,
6364 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006365{
6366 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006367 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006368 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006369 };
6370 struct nfs4_fs_locations_arg args = {
6371 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006372 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006373 .page = page,
6374 .bitmask = bitmask,
6375 };
Benny Halevy22958462009-04-01 09:22:02 -04006376 struct nfs4_fs_locations_res res = {
6377 .fs_locations = fs_locations,
6378 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006379 struct rpc_message msg = {
6380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6381 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006382 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006383 };
6384 int status;
6385
Harvey Harrison3110ff82008-05-02 13:42:44 -07006386 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006387
6388 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6389 * is not supported */
6390 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6391 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6392 else
6393 bitmask[0] |= FATTR4_WORD0_FILEID;
6394
Trond Myklebustc228fd32007-01-13 02:28:11 -05006395 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006396 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006397 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006398 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006399 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006400 return status;
6401}
6402
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006403int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6404 const struct qstr *name,
6405 struct nfs4_fs_locations *fs_locations,
6406 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006407{
6408 struct nfs4_exception exception = { };
6409 int err;
6410 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006411 err = _nfs4_proc_fs_locations(client, dir, name,
6412 fs_locations, page);
6413 trace_nfs4_get_fs_locations(dir, name, err);
6414 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006415 &exception);
6416 } while (exception.retry);
6417 return err;
6418}
6419
Chuck Leverb03d7352013-10-17 14:12:50 -04006420/*
6421 * This operation also signals the server that this client is
6422 * performing migration recovery. The server can stop returning
6423 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6424 * appended to this compound to identify the client ID which is
6425 * performing recovery.
6426 */
6427static int _nfs40_proc_get_locations(struct inode *inode,
6428 struct nfs4_fs_locations *locations,
6429 struct page *page, struct rpc_cred *cred)
6430{
6431 struct nfs_server *server = NFS_SERVER(inode);
6432 struct rpc_clnt *clnt = server->client;
6433 u32 bitmask[2] = {
6434 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6435 };
6436 struct nfs4_fs_locations_arg args = {
6437 .clientid = server->nfs_client->cl_clientid,
6438 .fh = NFS_FH(inode),
6439 .page = page,
6440 .bitmask = bitmask,
6441 .migration = 1, /* skip LOOKUP */
6442 .renew = 1, /* append RENEW */
6443 };
6444 struct nfs4_fs_locations_res res = {
6445 .fs_locations = locations,
6446 .migration = 1,
6447 .renew = 1,
6448 };
6449 struct rpc_message msg = {
6450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6451 .rpc_argp = &args,
6452 .rpc_resp = &res,
6453 .rpc_cred = cred,
6454 };
6455 unsigned long now = jiffies;
6456 int status;
6457
6458 nfs_fattr_init(&locations->fattr);
6459 locations->server = server;
6460 locations->nlocations = 0;
6461
6462 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6463 nfs4_set_sequence_privileged(&args.seq_args);
6464 status = nfs4_call_sync_sequence(clnt, server, &msg,
6465 &args.seq_args, &res.seq_res);
6466 if (status)
6467 return status;
6468
6469 renew_lease(server, now);
6470 return 0;
6471}
6472
6473#ifdef CONFIG_NFS_V4_1
6474
6475/*
6476 * This operation also signals the server that this client is
6477 * performing migration recovery. The server can stop asserting
6478 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6479 * performing this operation is identified in the SEQUENCE
6480 * operation in this compound.
6481 *
6482 * When the client supports GETATTR(fs_locations_info), it can
6483 * be plumbed in here.
6484 */
6485static int _nfs41_proc_get_locations(struct inode *inode,
6486 struct nfs4_fs_locations *locations,
6487 struct page *page, struct rpc_cred *cred)
6488{
6489 struct nfs_server *server = NFS_SERVER(inode);
6490 struct rpc_clnt *clnt = server->client;
6491 u32 bitmask[2] = {
6492 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6493 };
6494 struct nfs4_fs_locations_arg args = {
6495 .fh = NFS_FH(inode),
6496 .page = page,
6497 .bitmask = bitmask,
6498 .migration = 1, /* skip LOOKUP */
6499 };
6500 struct nfs4_fs_locations_res res = {
6501 .fs_locations = locations,
6502 .migration = 1,
6503 };
6504 struct rpc_message msg = {
6505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6506 .rpc_argp = &args,
6507 .rpc_resp = &res,
6508 .rpc_cred = cred,
6509 };
6510 int status;
6511
6512 nfs_fattr_init(&locations->fattr);
6513 locations->server = server;
6514 locations->nlocations = 0;
6515
6516 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6517 nfs4_set_sequence_privileged(&args.seq_args);
6518 status = nfs4_call_sync_sequence(clnt, server, &msg,
6519 &args.seq_args, &res.seq_res);
6520 if (status == NFS4_OK &&
6521 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6522 status = -NFS4ERR_LEASE_MOVED;
6523 return status;
6524}
6525
6526#endif /* CONFIG_NFS_V4_1 */
6527
6528/**
6529 * nfs4_proc_get_locations - discover locations for a migrated FSID
6530 * @inode: inode on FSID that is migrating
6531 * @locations: result of query
6532 * @page: buffer
6533 * @cred: credential to use for this operation
6534 *
6535 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6536 * operation failed, or a negative errno if a local error occurred.
6537 *
6538 * On success, "locations" is filled in, but if the server has
6539 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6540 * asserted.
6541 *
6542 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6543 * from this client that require migration recovery.
6544 */
6545int nfs4_proc_get_locations(struct inode *inode,
6546 struct nfs4_fs_locations *locations,
6547 struct page *page, struct rpc_cred *cred)
6548{
6549 struct nfs_server *server = NFS_SERVER(inode);
6550 struct nfs_client *clp = server->nfs_client;
6551 const struct nfs4_mig_recovery_ops *ops =
6552 clp->cl_mvops->mig_recovery_ops;
6553 struct nfs4_exception exception = { };
6554 int status;
6555
6556 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6557 (unsigned long long)server->fsid.major,
6558 (unsigned long long)server->fsid.minor,
6559 clp->cl_hostname);
6560 nfs_display_fhandle(NFS_FH(inode), __func__);
6561
6562 do {
6563 status = ops->get_locations(inode, locations, page, cred);
6564 if (status != -NFS4ERR_DELAY)
6565 break;
6566 nfs4_handle_exception(server, status, &exception);
6567 } while (exception.retry);
6568 return status;
6569}
6570
Chuck Lever44c99932013-10-17 14:13:30 -04006571/*
6572 * This operation also signals the server that this client is
6573 * performing "lease moved" recovery. The server can stop
6574 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6575 * is appended to this compound to identify the client ID which is
6576 * performing recovery.
6577 */
6578static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6579{
6580 struct nfs_server *server = NFS_SERVER(inode);
6581 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6582 struct rpc_clnt *clnt = server->client;
6583 struct nfs4_fsid_present_arg args = {
6584 .fh = NFS_FH(inode),
6585 .clientid = clp->cl_clientid,
6586 .renew = 1, /* append RENEW */
6587 };
6588 struct nfs4_fsid_present_res res = {
6589 .renew = 1,
6590 };
6591 struct rpc_message msg = {
6592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6593 .rpc_argp = &args,
6594 .rpc_resp = &res,
6595 .rpc_cred = cred,
6596 };
6597 unsigned long now = jiffies;
6598 int status;
6599
6600 res.fh = nfs_alloc_fhandle();
6601 if (res.fh == NULL)
6602 return -ENOMEM;
6603
6604 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6605 nfs4_set_sequence_privileged(&args.seq_args);
6606 status = nfs4_call_sync_sequence(clnt, server, &msg,
6607 &args.seq_args, &res.seq_res);
6608 nfs_free_fhandle(res.fh);
6609 if (status)
6610 return status;
6611
6612 do_renew_lease(clp, now);
6613 return 0;
6614}
6615
6616#ifdef CONFIG_NFS_V4_1
6617
6618/*
6619 * This operation also signals the server that this client is
6620 * performing "lease moved" recovery. The server can stop asserting
6621 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6622 * this operation is identified in the SEQUENCE operation in this
6623 * compound.
6624 */
6625static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6626{
6627 struct nfs_server *server = NFS_SERVER(inode);
6628 struct rpc_clnt *clnt = server->client;
6629 struct nfs4_fsid_present_arg args = {
6630 .fh = NFS_FH(inode),
6631 };
6632 struct nfs4_fsid_present_res res = {
6633 };
6634 struct rpc_message msg = {
6635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6636 .rpc_argp = &args,
6637 .rpc_resp = &res,
6638 .rpc_cred = cred,
6639 };
6640 int status;
6641
6642 res.fh = nfs_alloc_fhandle();
6643 if (res.fh == NULL)
6644 return -ENOMEM;
6645
6646 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6647 nfs4_set_sequence_privileged(&args.seq_args);
6648 status = nfs4_call_sync_sequence(clnt, server, &msg,
6649 &args.seq_args, &res.seq_res);
6650 nfs_free_fhandle(res.fh);
6651 if (status == NFS4_OK &&
6652 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6653 status = -NFS4ERR_LEASE_MOVED;
6654 return status;
6655}
6656
6657#endif /* CONFIG_NFS_V4_1 */
6658
6659/**
6660 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6661 * @inode: inode on FSID to check
6662 * @cred: credential to use for this operation
6663 *
6664 * Server indicates whether the FSID is present, moved, or not
6665 * recognized. This operation is necessary to clear a LEASE_MOVED
6666 * condition for this client ID.
6667 *
6668 * Returns NFS4_OK if the FSID is present on this server,
6669 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6670 * NFS4ERR code if some error occurred on the server, or a
6671 * negative errno if a local failure occurred.
6672 */
6673int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6674{
6675 struct nfs_server *server = NFS_SERVER(inode);
6676 struct nfs_client *clp = server->nfs_client;
6677 const struct nfs4_mig_recovery_ops *ops =
6678 clp->cl_mvops->mig_recovery_ops;
6679 struct nfs4_exception exception = { };
6680 int status;
6681
6682 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6683 (unsigned long long)server->fsid.major,
6684 (unsigned long long)server->fsid.minor,
6685 clp->cl_hostname);
6686 nfs_display_fhandle(NFS_FH(inode), __func__);
6687
6688 do {
6689 status = ops->fsid_present(inode, cred);
6690 if (status != -NFS4ERR_DELAY)
6691 break;
6692 nfs4_handle_exception(server, status, &exception);
6693 } while (exception.retry);
6694 return status;
6695}
6696
Andy Adamson5ec16a82013-08-08 10:57:55 -04006697/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006698 * If 'use_integrity' is true and the state managment nfs_client
6699 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6700 * and the machine credential as per RFC3530bis and RFC5661 Security
6701 * Considerations sections. Otherwise, just use the user cred with the
6702 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006703 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006704static 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 +00006705{
6706 int status;
6707 struct nfs4_secinfo_arg args = {
6708 .dir_fh = NFS_FH(dir),
6709 .name = name,
6710 };
6711 struct nfs4_secinfo_res res = {
6712 .flavors = flavors,
6713 };
6714 struct rpc_message msg = {
6715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6716 .rpc_argp = &args,
6717 .rpc_resp = &res,
6718 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006719 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006720 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006721
6722 if (use_integrity) {
6723 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006724 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6725 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006726 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006727
6728 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006729
6730 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6731 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6732
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006733 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6734 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006735 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006736
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006737 if (cred)
6738 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006739
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006740 return status;
6741}
6742
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006743int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6744 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006745{
6746 struct nfs4_exception exception = { };
6747 int err;
6748 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006749 err = -NFS4ERR_WRONGSEC;
6750
6751 /* try to use integrity protection with machine cred */
6752 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6753 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6754
6755 /*
6756 * if unable to use integrity protection, or SECINFO with
6757 * integrity protection returns NFS4ERR_WRONGSEC (which is
6758 * disallowed by spec, but exists in deployed servers) use
6759 * the current filesystem's rpc_client and the user cred.
6760 */
6761 if (err == -NFS4ERR_WRONGSEC)
6762 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6763
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006764 trace_nfs4_secinfo(dir, name, err);
6765 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006766 &exception);
6767 } while (exception.retry);
6768 return err;
6769}
6770
Andy Adamson557134a2009-04-01 09:21:53 -04006771#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006772/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006773 * Check the exchange flags returned by the server for invalid flags, having
6774 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6775 * DS flags set.
6776 */
6777static int nfs4_check_cl_exchange_flags(u32 flags)
6778{
6779 if (flags & ~EXCHGID4_FLAG_MASK_R)
6780 goto out_inval;
6781 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6782 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6783 goto out_inval;
6784 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6785 goto out_inval;
6786 return NFS_OK;
6787out_inval:
6788 return -NFS4ERR_INVAL;
6789}
6790
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006791static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006792nfs41_same_server_scope(struct nfs41_server_scope *a,
6793 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006794{
6795 if (a->server_scope_sz == b->server_scope_sz &&
6796 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6797 return true;
6798
6799 return false;
6800}
6801
Andy Adamson357f54d2010-12-14 10:11:57 -05006802/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006803 * nfs4_proc_bind_conn_to_session()
6804 *
6805 * The 4.1 client currently uses the same TCP connection for the
6806 * fore and backchannel.
6807 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006808int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006809{
6810 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006811 struct nfs41_bind_conn_to_session_args args = {
6812 .client = clp,
6813 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6814 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006815 struct nfs41_bind_conn_to_session_res res;
6816 struct rpc_message msg = {
6817 .rpc_proc =
6818 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006819 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006820 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006821 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006822 };
6823
6824 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006825
Trond Myklebust71a097c2015-02-18 09:27:18 -08006826 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6827 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6828 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006829
6830 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006831 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006832 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006833 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006834 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6835 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6836 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006837 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006838 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006839 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006840 dprintk("NFS: %s: Unexpected direction from server\n",
6841 __func__);
6842 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006843 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006844 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006845 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006846 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6847 __func__);
6848 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006849 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006850 }
6851 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006852out:
6853 dprintk("<-- %s status= %d\n", __func__, status);
6854 return status;
6855}
6856
6857/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006858 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6859 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006860 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006861static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6862 .how = SP4_MACH_CRED,
6863 .enforce.u.words = {
6864 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6865 1 << (OP_EXCHANGE_ID - 32) |
6866 1 << (OP_CREATE_SESSION - 32) |
6867 1 << (OP_DESTROY_SESSION - 32) |
6868 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006869 },
6870 .allow.u.words = {
6871 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006872 1 << (OP_LOCKU) |
6873 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006874 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006875 1 << (OP_SECINFO_NO_NAME - 32) |
6876 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006877 1 << (OP_FREE_STATEID - 32) |
6878 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006879 }
6880};
6881
6882/*
6883 * Select the state protection mode for client `clp' given the server results
6884 * from exchange_id in `sp'.
6885 *
6886 * Returns 0 on success, negative errno otherwise.
6887 */
6888static int nfs4_sp4_select_mode(struct nfs_client *clp,
6889 struct nfs41_state_protection *sp)
6890{
6891 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6892 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6893 1 << (OP_EXCHANGE_ID - 32) |
6894 1 << (OP_CREATE_SESSION - 32) |
6895 1 << (OP_DESTROY_SESSION - 32) |
6896 1 << (OP_DESTROY_CLIENTID - 32)
6897 };
6898 unsigned int i;
6899
6900 if (sp->how == SP4_MACH_CRED) {
6901 /* Print state protect result */
6902 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6903 for (i = 0; i <= LAST_NFS4_OP; i++) {
6904 if (test_bit(i, sp->enforce.u.longs))
6905 dfprintk(MOUNT, " enforce op %d\n", i);
6906 if (test_bit(i, sp->allow.u.longs))
6907 dfprintk(MOUNT, " allow op %d\n", i);
6908 }
6909
6910 /* make sure nothing is on enforce list that isn't supported */
6911 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6912 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6913 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6914 return -EINVAL;
6915 }
6916 }
6917
6918 /*
6919 * Minimal mode - state operations are allowed to use machine
6920 * credential. Note this already happens by default, so the
6921 * client doesn't have to do anything more than the negotiation.
6922 *
6923 * NOTE: we don't care if EXCHANGE_ID is in the list -
6924 * we're already using the machine cred for exchange_id
6925 * and will never use a different cred.
6926 */
6927 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6928 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6929 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6930 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6931 dfprintk(MOUNT, "sp4_mach_cred:\n");
6932 dfprintk(MOUNT, " minimal mode enabled\n");
6933 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6934 } else {
6935 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6936 return -EINVAL;
6937 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006938
6939 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6940 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6941 dfprintk(MOUNT, " cleanup mode enabled\n");
6942 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6943 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006944
6945 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6946 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6947 dfprintk(MOUNT, " secinfo mode enabled\n");
6948 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6949 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006950
6951 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6952 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6953 dfprintk(MOUNT, " stateid mode enabled\n");
6954 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6955 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006956
6957 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6958 dfprintk(MOUNT, " write mode enabled\n");
6959 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6960 }
6961
6962 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6963 dfprintk(MOUNT, " commit mode enabled\n");
6964 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6965 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006966 }
6967
6968 return 0;
6969}
6970
6971/*
6972 * _nfs4_proc_exchange_id()
6973 *
6974 * Wrapper for EXCHANGE_ID operation.
6975 */
6976static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6977 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006978{
6979 nfs4_verifier verifier;
6980 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006981 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006982 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006983#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006984 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006985 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6986 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6987#else
6988 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6989 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6990#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006991 };
6992 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006993 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006994 };
6995 int status;
6996 struct rpc_message msg = {
6997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6998 .rpc_argp = &args,
6999 .rpc_resp = &res,
7000 .rpc_cred = cred,
7001 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04007002
Chuck Leverf0920752012-05-21 22:45:41 -04007003 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007004
7005 status = nfs4_init_uniform_client_string(clp);
7006 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04007007 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007008
7009 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007010 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007011 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007012
Chuck Leveracdeb692012-05-21 22:46:16 -04007013 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007014 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04007015 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007016 status = -ENOMEM;
7017 goto out;
7018 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007019
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007020 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007021 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007022 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04007023 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04007024 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007025 }
7026
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007027 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007028 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007029 status = -ENOMEM;
7030 goto out_server_scope;
7031 }
7032
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007033 switch (sp4_how) {
7034 case SP4_NONE:
7035 args.state_protect.how = SP4_NONE;
7036 break;
7037
7038 case SP4_MACH_CRED:
7039 args.state_protect = nfs4_sp4_mach_cred_request;
7040 break;
7041
7042 default:
7043 /* unsupported! */
7044 WARN_ON_ONCE(1);
7045 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007046 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007047 }
7048
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007049 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007050 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04007051 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04007052 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007053
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007054 if (status == 0)
7055 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7056
Chuck Lever177313f2012-05-21 22:44:58 -04007057 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04007058 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007059 clp->cl_exchange_flags = res.flags;
7060 /* Client ID is not confirmed */
7061 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7062 clear_bit(NFS4_SESSION_ESTABLISHED,
7063 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007064 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007065 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007066
Chuck Leveracdeb692012-05-21 22:46:16 -04007067 kfree(clp->cl_serverowner);
7068 clp->cl_serverowner = res.server_owner;
7069 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007070
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007071 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007072 kfree(clp->cl_implid);
7073 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007074 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007075
Chuck Lever177313f2012-05-21 22:44:58 -04007076 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007077 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007078 res.server_scope)) {
7079 dprintk("%s: server_scope mismatch detected\n",
7080 __func__);
7081 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007082 kfree(clp->cl_serverscope);
7083 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007084 }
7085
Chuck Lever177313f2012-05-21 22:44:58 -04007086 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007087 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007088 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007089 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007090 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007091
Kinglong Mee6b559702015-07-01 11:54:53 +08007092out_impl_id:
7093 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007094out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007095 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007096out_server_owner:
7097 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007098out:
Chuck Lever177313f2012-05-21 22:44:58 -04007099 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007100 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007101 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007102 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007103 clp->cl_implid->date.seconds,
7104 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007105 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007106 return status;
7107}
7108
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007109/*
7110 * nfs4_proc_exchange_id()
7111 *
7112 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7113 *
7114 * Since the clientid has expired, all compounds using sessions
7115 * associated with the stale clientid will be returning
7116 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7117 * be in some phase of session reset.
7118 *
7119 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7120 */
7121int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7122{
7123 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7124 int status;
7125
7126 /* try SP4_MACH_CRED if krb5i/p */
7127 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7128 authflavor == RPC_AUTH_GSS_KRB5P) {
7129 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7130 if (!status)
7131 return 0;
7132 }
7133
7134 /* try SP4_NONE */
7135 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7136}
7137
Trond Myklebust66245532012-05-25 17:18:09 -04007138static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7139 struct rpc_cred *cred)
7140{
7141 struct rpc_message msg = {
7142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7143 .rpc_argp = clp,
7144 .rpc_cred = cred,
7145 };
7146 int status;
7147
7148 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007149 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007150 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007151 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007152 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7153 return status;
7154}
7155
7156static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7157 struct rpc_cred *cred)
7158{
7159 unsigned int loop;
7160 int ret;
7161
7162 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7163 ret = _nfs4_proc_destroy_clientid(clp, cred);
7164 switch (ret) {
7165 case -NFS4ERR_DELAY:
7166 case -NFS4ERR_CLIENTID_BUSY:
7167 ssleep(1);
7168 break;
7169 default:
7170 return ret;
7171 }
7172 }
7173 return 0;
7174}
7175
7176int nfs4_destroy_clientid(struct nfs_client *clp)
7177{
7178 struct rpc_cred *cred;
7179 int ret = 0;
7180
7181 if (clp->cl_mvops->minor_version < 1)
7182 goto out;
7183 if (clp->cl_exchange_flags == 0)
7184 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007185 if (clp->cl_preserve_clid)
7186 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007187 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007188 ret = nfs4_proc_destroy_clientid(clp, cred);
7189 if (cred)
7190 put_rpccred(cred);
7191 switch (ret) {
7192 case 0:
7193 case -NFS4ERR_STALE_CLIENTID:
7194 clp->cl_exchange_flags = 0;
7195 }
7196out:
7197 return ret;
7198}
7199
Andy Adamson2050f0c2009-04-01 09:22:30 -04007200struct nfs4_get_lease_time_data {
7201 struct nfs4_get_lease_time_args *args;
7202 struct nfs4_get_lease_time_res *res;
7203 struct nfs_client *clp;
7204};
7205
7206static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7207 void *calldata)
7208{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007209 struct nfs4_get_lease_time_data *data =
7210 (struct nfs4_get_lease_time_data *)calldata;
7211
7212 dprintk("--> %s\n", __func__);
7213 /* just setup sequence, do not trigger session recovery
7214 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007215 nfs41_setup_sequence(data->clp->cl_session,
7216 &data->args->la_seq_args,
7217 &data->res->lr_seq_res,
7218 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007219 dprintk("<-- %s\n", __func__);
7220}
7221
7222/*
7223 * Called from nfs4_state_manager thread for session setup, so don't recover
7224 * from sequence operation or clientid errors.
7225 */
7226static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7227{
7228 struct nfs4_get_lease_time_data *data =
7229 (struct nfs4_get_lease_time_data *)calldata;
7230
7231 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007232 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7233 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007234 switch (task->tk_status) {
7235 case -NFS4ERR_DELAY:
7236 case -NFS4ERR_GRACE:
7237 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7238 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7239 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007240 /* fall through */
7241 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007242 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007243 return;
7244 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007245 dprintk("<-- %s\n", __func__);
7246}
7247
Trond Myklebust17280172012-03-11 13:11:00 -04007248static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007249 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7250 .rpc_call_done = nfs4_get_lease_time_done,
7251};
7252
7253int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7254{
7255 struct rpc_task *task;
7256 struct nfs4_get_lease_time_args args;
7257 struct nfs4_get_lease_time_res res = {
7258 .lr_fsinfo = fsinfo,
7259 };
7260 struct nfs4_get_lease_time_data data = {
7261 .args = &args,
7262 .res = &res,
7263 .clp = clp,
7264 };
7265 struct rpc_message msg = {
7266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7267 .rpc_argp = &args,
7268 .rpc_resp = &res,
7269 };
7270 struct rpc_task_setup task_setup = {
7271 .rpc_client = clp->cl_rpcclient,
7272 .rpc_message = &msg,
7273 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007274 .callback_data = &data,
7275 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007276 };
7277 int status;
7278
Chuck Levera9c92d62013-08-09 12:48:18 -04007279 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007280 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007281 dprintk("--> %s\n", __func__);
7282 task = rpc_run_task(&task_setup);
7283
7284 if (IS_ERR(task))
7285 status = PTR_ERR(task);
7286 else {
7287 status = task->tk_status;
7288 rpc_put_task(task);
7289 }
7290 dprintk("<-- %s return %d\n", __func__, status);
7291
7292 return status;
7293}
7294
Andy Adamsonfc931582009-04-01 09:22:31 -04007295/*
7296 * Initialize the values to be used by the client in CREATE_SESSION
7297 * If nfs4_init_session set the fore channel request and response sizes,
7298 * use them.
7299 *
7300 * Set the back channel max_resp_sz_cached to zero to force the client to
7301 * always set csa_cachethis to FALSE because the current implementation
7302 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7303 */
7304static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7305{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007306 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007307
Andy Adamson18aad3d2013-06-26 12:21:49 -04007308 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7309 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7310
Andy Adamsonfc931582009-04-01 09:22:31 -04007311 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007312 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7313 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007314 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007315 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007316
7317 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007318 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007319 __func__,
7320 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007321 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007322
7323 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007324 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7325 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7326 args->bc_attrs.max_resp_sz_cached = 0;
7327 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7328 args->bc_attrs.max_reqs = 1;
7329
7330 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7331 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7332 __func__,
7333 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7334 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7335 args->bc_attrs.max_reqs);
7336}
7337
Trond Myklebust79969dd2015-02-18 11:30:18 -08007338static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7339 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007340{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007341 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007342 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007343
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007344 if (rcvd->max_resp_sz > sent->max_resp_sz)
7345 return -EINVAL;
7346 /*
7347 * Our requested max_ops is the minimum we need; we're not
7348 * prepared to break up compounds into smaller pieces than that.
7349 * So, no point even trying to continue if the server won't
7350 * cooperate:
7351 */
7352 if (rcvd->max_ops < sent->max_ops)
7353 return -EINVAL;
7354 if (rcvd->max_reqs == 0)
7355 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007356 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7357 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007358 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007359}
7360
Trond Myklebust79969dd2015-02-18 11:30:18 -08007361static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7362 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007363{
7364 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007365 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007366
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007367 if (!(res->flags & SESSION4_BACK_CHAN))
7368 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007369 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7370 return -EINVAL;
7371 if (rcvd->max_resp_sz < sent->max_resp_sz)
7372 return -EINVAL;
7373 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7374 return -EINVAL;
7375 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007376 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007377 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007378 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007379 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007380out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007381 return 0;
7382}
Andy Adamson8d353012009-04-01 09:22:32 -04007383
Andy Adamson8d353012009-04-01 09:22:32 -04007384static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007385 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007386{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007387 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007388
Trond Myklebust79969dd2015-02-18 11:30:18 -08007389 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007390 if (ret)
7391 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007392 return nfs4_verify_back_channel_attrs(args, res);
7393}
7394
7395static void nfs4_update_session(struct nfs4_session *session,
7396 struct nfs41_create_session_res *res)
7397{
7398 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007399 /* Mark client id and session as being confirmed */
7400 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7401 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007402 session->flags = res->flags;
7403 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007404 if (res->flags & SESSION4_BACK_CHAN)
7405 memcpy(&session->bc_attrs, &res->bc_attrs,
7406 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007407}
7408
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007409static int _nfs4_proc_create_session(struct nfs_client *clp,
7410 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007411{
7412 struct nfs4_session *session = clp->cl_session;
7413 struct nfs41_create_session_args args = {
7414 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007415 .clientid = clp->cl_clientid,
7416 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007417 .cb_program = NFS4_CALLBACK,
7418 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007419 struct nfs41_create_session_res res;
7420
Andy Adamsonfc931582009-04-01 09:22:31 -04007421 struct rpc_message msg = {
7422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7423 .rpc_argp = &args,
7424 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007425 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007426 };
7427 int status;
7428
7429 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007430 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007431
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007432 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007433 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007434
Trond Myklebust43095d32012-11-20 11:13:12 -05007435 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007436 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007437 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007438 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007439 if (clp->cl_seqid == res.seqid)
7440 clp->cl_seqid++;
7441 if (status)
7442 goto out;
7443 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007444 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007445out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007446 return status;
7447}
7448
7449/*
7450 * Issues a CREATE_SESSION operation to the server.
7451 * It is the responsibility of the caller to verify the session is
7452 * expired before calling this routine.
7453 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007454int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007455{
7456 int status;
7457 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007458 struct nfs4_session *session = clp->cl_session;
7459
7460 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7461
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007462 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007463 if (status)
7464 goto out;
7465
Andy Adamsonaacd5532011-11-09 13:58:21 -05007466 /* Init or reset the session slot tables */
7467 status = nfs4_setup_session_slot_tables(session);
7468 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007469 if (status)
7470 goto out;
7471
7472 ptr = (unsigned *)&session->sess_id.data[0];
7473 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7474 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007475out:
7476 dprintk("<-- %s\n", __func__);
7477 return status;
7478}
7479
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007480/*
7481 * Issue the over-the-wire RPC DESTROY_SESSION.
7482 * The caller must serialize access to this routine.
7483 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007484int nfs4_proc_destroy_session(struct nfs4_session *session,
7485 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007486{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007487 struct rpc_message msg = {
7488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7489 .rpc_argp = session,
7490 .rpc_cred = cred,
7491 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007492 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007493
7494 dprintk("--> nfs4_proc_destroy_session\n");
7495
7496 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007497 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7498 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007499
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007500 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007501 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007502
7503 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007504 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007505 "Session has been destroyed regardless...\n", status);
7506
7507 dprintk("<-- nfs4_proc_destroy_session\n");
7508 return status;
7509}
7510
Trond Myklebust7b38c362012-05-23 13:23:31 -04007511/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007512 * Renew the cl_session lease.
7513 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007514struct nfs4_sequence_data {
7515 struct nfs_client *clp;
7516 struct nfs4_sequence_args args;
7517 struct nfs4_sequence_res res;
7518};
7519
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007520static void nfs41_sequence_release(void *data)
7521{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007522 struct nfs4_sequence_data *calldata = data;
7523 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007524
Alexandros Batsakis71358402010-02-05 03:45:05 -08007525 if (atomic_read(&clp->cl_count) > 1)
7526 nfs4_schedule_state_renewal(clp);
7527 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007528 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007529}
7530
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007531static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7532{
7533 switch(task->tk_status) {
7534 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007535 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7536 return -EAGAIN;
7537 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007538 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007539 }
7540 return 0;
7541}
7542
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007543static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007544{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007545 struct nfs4_sequence_data *calldata = data;
7546 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007547
Trond Myklebust14516c32010-07-31 14:29:06 -04007548 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7549 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007550
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007551 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007552 if (task->tk_status < 0) {
7553 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007554 if (atomic_read(&clp->cl_count) == 1)
7555 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007556
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007557 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7558 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007559 return;
7560 }
7561 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007562 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007563out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007564 dprintk("<-- %s\n", __func__);
7565}
7566
7567static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7568{
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 struct nfs4_sequence_args *args;
7572 struct nfs4_sequence_res *res;
7573
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007574 args = task->tk_msg.rpc_argp;
7575 res = task->tk_msg.rpc_resp;
7576
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007577 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007578}
7579
7580static const struct rpc_call_ops nfs41_sequence_ops = {
7581 .rpc_call_done = nfs41_sequence_call_done,
7582 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007583 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007584};
7585
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007586static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7587 struct rpc_cred *cred,
7588 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007589{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007590 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007591 struct rpc_message msg = {
7592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7593 .rpc_cred = cred,
7594 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007595 struct rpc_task_setup task_setup_data = {
7596 .rpc_client = clp->cl_rpcclient,
7597 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007598 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007599 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007600 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007601
Alexandros Batsakis71358402010-02-05 03:45:05 -08007602 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007603 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007604 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007605 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007606 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007607 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007608 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007609 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007610 if (is_privileged)
7611 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007612 msg.rpc_argp = &calldata->args;
7613 msg.rpc_resp = &calldata->res;
7614 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007615 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007616
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007617 return rpc_run_task(&task_setup_data);
7618}
7619
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007620static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007621{
7622 struct rpc_task *task;
7623 int ret = 0;
7624
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007625 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007626 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007627 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007628 if (IS_ERR(task))
7629 ret = PTR_ERR(task);
7630 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007631 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007632 dprintk("<-- %s status=%d\n", __func__, ret);
7633 return ret;
7634}
7635
7636static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7637{
7638 struct rpc_task *task;
7639 int ret;
7640
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007641 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007642 if (IS_ERR(task)) {
7643 ret = PTR_ERR(task);
7644 goto out;
7645 }
7646 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007647 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007648 ret = task->tk_status;
7649 rpc_put_task(task);
7650out:
7651 dprintk("<-- %s status=%d\n", __func__, ret);
7652 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007653}
7654
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007655struct nfs4_reclaim_complete_data {
7656 struct nfs_client *clp;
7657 struct nfs41_reclaim_complete_args arg;
7658 struct nfs41_reclaim_complete_res res;
7659};
7660
7661static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7662{
7663 struct nfs4_reclaim_complete_data *calldata = data;
7664
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007665 nfs41_setup_sequence(calldata->clp->cl_session,
7666 &calldata->arg.seq_args,
7667 &calldata->res.seq_res,
7668 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007669}
7670
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007671static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7672{
7673 switch(task->tk_status) {
7674 case 0:
7675 case -NFS4ERR_COMPLETE_ALREADY:
7676 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7677 break;
7678 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007679 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007680 /* fall through */
7681 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007682 return -EAGAIN;
7683 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007684 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007685 }
7686 return 0;
7687}
7688
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007689static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7690{
7691 struct nfs4_reclaim_complete_data *calldata = data;
7692 struct nfs_client *clp = calldata->clp;
7693 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7694
7695 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007696 if (!nfs41_sequence_done(task, res))
7697 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007698
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007699 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007700 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7701 rpc_restart_call_prepare(task);
7702 return;
7703 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007704 dprintk("<-- %s\n", __func__);
7705}
7706
7707static void nfs4_free_reclaim_complete_data(void *data)
7708{
7709 struct nfs4_reclaim_complete_data *calldata = data;
7710
7711 kfree(calldata);
7712}
7713
7714static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7715 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7716 .rpc_call_done = nfs4_reclaim_complete_done,
7717 .rpc_release = nfs4_free_reclaim_complete_data,
7718};
7719
7720/*
7721 * Issue a global reclaim complete.
7722 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007723static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7724 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007725{
7726 struct nfs4_reclaim_complete_data *calldata;
7727 struct rpc_task *task;
7728 struct rpc_message msg = {
7729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007730 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007731 };
7732 struct rpc_task_setup task_setup_data = {
7733 .rpc_client = clp->cl_rpcclient,
7734 .rpc_message = &msg,
7735 .callback_ops = &nfs4_reclaim_complete_call_ops,
7736 .flags = RPC_TASK_ASYNC,
7737 };
7738 int status = -ENOMEM;
7739
7740 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007741 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007742 if (calldata == NULL)
7743 goto out;
7744 calldata->clp = clp;
7745 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007746
Chuck Levera9c92d62013-08-09 12:48:18 -04007747 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007748 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007749 msg.rpc_argp = &calldata->arg;
7750 msg.rpc_resp = &calldata->res;
7751 task_setup_data.callback_data = calldata;
7752 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007753 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007754 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007755 goto out;
7756 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007757 status = nfs4_wait_for_completion_rpc_task(task);
7758 if (status == 0)
7759 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007760 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007761 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007762out:
7763 dprintk("<-- %s status=%d\n", __func__, status);
7764 return status;
7765}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007766
7767static void
7768nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7769{
7770 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007771 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007772 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007773
7774 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007775 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7776 * right now covering the LAYOUTGET we are about to send.
7777 * However, that is not so catastrophic, and there seems
7778 * to be no way to prevent it completely.
7779 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007780 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007781 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007782 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007783 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7784 NFS_I(lgp->args.inode)->layout,
Peng Taoce6ab4f2014-09-06 00:53:24 +08007785 &lgp->args.range,
Fred Isamancf7d63f2011-01-06 11:36:25 +00007786 lgp->args.ctx->state)) {
7787 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007788 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007789}
7790
7791static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7792{
7793 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007794 struct inode *inode = lgp->args.inode;
7795 struct nfs_server *server = NFS_SERVER(inode);
7796 struct pnfs_layout_hdr *lo;
7797 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007798 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007799
Boaz Harroshed7e5422014-01-22 20:34:54 +02007800 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007801
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007802 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007803 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007804
7805 switch (task->tk_status) {
7806 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007807 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007808 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07007809 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7810 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7811 */
7812 case -NFS4ERR_BADLAYOUT:
7813 goto out_overflow;
7814 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02007815 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07007816 * (or clients) writing to the same RAID stripe except when
7817 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Boaz Harroshed7e5422014-01-22 20:34:54 +02007818 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007819 case -NFS4ERR_LAYOUTTRYLATER:
Trond Myklebust21b874c2015-08-31 01:19:22 -07007820 if (lgp->args.minlength == 0)
7821 goto out_overflow;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007822 /*
7823 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7824 * existing layout before getting a new one).
7825 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007826 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007827 timeo = rpc_get_timeout(task->tk_client);
7828 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007829 now = jiffies;
7830 if (time_after(giveup, now)) {
7831 unsigned long delay;
7832
7833 /* Delay for:
7834 * - Not less then NFS4_POLL_RETRY_MIN.
7835 * - One last time a jiffie before we give up
7836 * - exponential backoff (time_now minus start_attempt)
7837 */
7838 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7839 min((giveup - now - 1),
7840 now - lgp->args.timestamp));
7841
7842 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7843 __func__, delay);
7844 rpc_delay(task, delay);
Trond Myklebust2259f962015-09-20 13:30:30 -04007845 /* Do not call nfs4_async_handle_error() */
7846 goto out_restart;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007847 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007848 break;
7849 case -NFS4ERR_EXPIRED:
7850 case -NFS4ERR_BAD_STATEID:
7851 spin_lock(&inode->i_lock);
Trond Myklebust2259f962015-09-20 13:30:30 -04007852 if (nfs4_stateid_match(&lgp->args.stateid,
7853 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007854 spin_unlock(&inode->i_lock);
7855 /* If the open stateid was bad, then recover it. */
7856 state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04007857 break;
7858 }
7859 lo = NFS_I(inode)->layout;
7860 if (lo && nfs4_stateid_match(&lgp->args.stateid,
7861 &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007862 LIST_HEAD(head);
7863
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007864 /*
7865 * Mark the bad layout state as invalid, then retry
7866 * with the current stateid.
7867 */
Kinglong Mee8714d462015-09-20 23:04:22 +08007868 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Trond Myklebustee314c22012-10-01 17:25:48 -07007869 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7870 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007871 pnfs_free_lseg_list(&head);
Trond Myklebust2259f962015-09-20 13:30:30 -04007872 } else
7873 spin_unlock(&inode->i_lock);
7874 goto out_restart;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007875 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007876 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebust2259f962015-09-20 13:30:30 -04007877 goto out_restart;
Trond Myklebustee314c22012-10-01 17:25:48 -07007878out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007879 dprintk("<-- %s\n", __func__);
Trond Myklebust21b874c2015-08-31 01:19:22 -07007880 return;
Trond Myklebust2259f962015-09-20 13:30:30 -04007881out_restart:
7882 task->tk_status = 0;
7883 rpc_restart_call_prepare(task);
7884 return;
Trond Myklebust21b874c2015-08-31 01:19:22 -07007885out_overflow:
7886 task->tk_status = -EOVERFLOW;
7887 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007888}
7889
Idan Kedar85541162012-08-02 11:47:10 +03007890static size_t max_response_pages(struct nfs_server *server)
7891{
7892 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7893 return nfs_page_array_len(0, max_resp_sz);
7894}
7895
7896static void nfs4_free_pages(struct page **pages, size_t size)
7897{
7898 int i;
7899
7900 if (!pages)
7901 return;
7902
7903 for (i = 0; i < size; i++) {
7904 if (!pages[i])
7905 break;
7906 __free_page(pages[i]);
7907 }
7908 kfree(pages);
7909}
7910
7911static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7912{
7913 struct page **pages;
7914 int i;
7915
7916 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7917 if (!pages) {
7918 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7919 return NULL;
7920 }
7921
7922 for (i = 0; i < size; i++) {
7923 pages[i] = alloc_page(gfp_flags);
7924 if (!pages[i]) {
7925 dprintk("%s: failed to allocate page\n", __func__);
7926 nfs4_free_pages(pages, size);
7927 return NULL;
7928 }
7929 }
7930
7931 return pages;
7932}
7933
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007934static void nfs4_layoutget_release(void *calldata)
7935{
7936 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007937 struct inode *inode = lgp->args.inode;
7938 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007939 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007940
7941 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007942 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007943 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007944 put_nfs_open_context(lgp->args.ctx);
7945 kfree(calldata);
7946 dprintk("<-- %s\n", __func__);
7947}
7948
7949static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7950 .rpc_call_prepare = nfs4_layoutget_prepare,
7951 .rpc_call_done = nfs4_layoutget_done,
7952 .rpc_release = nfs4_layoutget_release,
7953};
7954
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007955struct pnfs_layout_segment *
7956nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007957{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007958 struct inode *inode = lgp->args.inode;
7959 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007960 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007961 struct rpc_task *task;
7962 struct rpc_message msg = {
7963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7964 .rpc_argp = &lgp->args,
7965 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007966 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007967 };
7968 struct rpc_task_setup task_setup_data = {
7969 .rpc_client = server->client,
7970 .rpc_message = &msg,
7971 .callback_ops = &nfs4_layoutget_call_ops,
7972 .callback_data = lgp,
7973 .flags = RPC_TASK_ASYNC,
7974 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007975 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007976 int status = 0;
7977
7978 dprintk("--> %s\n", __func__);
7979
Peng Tao4bd5a982014-11-17 11:05:17 +08007980 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7981 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7982
Idan Kedar85541162012-08-02 11:47:10 +03007983 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7984 if (!lgp->args.layout.pages) {
7985 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007986 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007987 }
7988 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007989 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007990
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007991 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007992 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007993 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007994
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007995 task = rpc_run_task(&task_setup_data);
7996 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007997 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007998 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007999 if (status == 0)
8000 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008001 trace_nfs4_layoutget(lgp->args.ctx,
8002 &lgp->args.range,
8003 &lgp->res.range,
8004 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008005 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8006 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008007 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008008 rpc_put_task(task);
8009 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008010 if (status)
8011 return ERR_PTR(status);
8012 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008013}
8014
Benny Halevycbe82602011-05-22 19:52:37 +03008015static void
8016nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8017{
8018 struct nfs4_layoutreturn *lrp = calldata;
8019
8020 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008021 nfs41_setup_sequence(lrp->clp->cl_session,
8022 &lrp->args.seq_args,
8023 &lrp->res.seq_res,
8024 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008025}
8026
8027static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8028{
8029 struct nfs4_layoutreturn *lrp = calldata;
8030 struct nfs_server *server;
8031
8032 dprintk("--> %s\n", __func__);
8033
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008034 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008035 return;
8036
8037 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008038 switch (task->tk_status) {
8039 default:
8040 task->tk_status = 0;
8041 case 0:
8042 break;
8043 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008044 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008045 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008046 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008047 return;
8048 }
Benny Halevycbe82602011-05-22 19:52:37 +03008049 dprintk("<-- %s\n", __func__);
8050}
8051
8052static void nfs4_layoutreturn_release(void *calldata)
8053{
8054 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008055 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008056 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008057
8058 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008059 spin_lock(&lo->plh_inode->i_lock);
8060 if (lrp->res.lrs_present)
8061 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008062 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
Tom Haynesd67ae822014-12-11 17:02:04 -05008063 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008064 lo->plh_block_lgets--;
8065 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008066 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008067 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008068 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008069 kfree(calldata);
8070 dprintk("<-- %s\n", __func__);
8071}
8072
8073static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8074 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8075 .rpc_call_done = nfs4_layoutreturn_done,
8076 .rpc_release = nfs4_layoutreturn_release,
8077};
8078
Peng Tao6c166052014-11-17 09:30:40 +08008079int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008080{
8081 struct rpc_task *task;
8082 struct rpc_message msg = {
8083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8084 .rpc_argp = &lrp->args,
8085 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008086 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008087 };
8088 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008089 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008090 .rpc_message = &msg,
8091 .callback_ops = &nfs4_layoutreturn_call_ops,
8092 .callback_data = lrp,
8093 };
Peng Tao6c166052014-11-17 09:30:40 +08008094 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008095
8096 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008097 if (!sync) {
8098 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8099 if (!lrp->inode) {
8100 nfs4_layoutreturn_release(lrp);
8101 return -EAGAIN;
8102 }
8103 task_setup_data.flags |= RPC_TASK_ASYNC;
8104 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008105 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008106 task = rpc_run_task(&task_setup_data);
8107 if (IS_ERR(task))
8108 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008109 if (sync)
8110 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008111 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008112 dprintk("<-- %s status=%d\n", __func__, status);
8113 rpc_put_task(task);
8114 return status;
8115}
8116
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008117static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008118_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8119 struct pnfs_device *pdev,
8120 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008121{
8122 struct nfs4_getdeviceinfo_args args = {
8123 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008124 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8125 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008126 };
8127 struct nfs4_getdeviceinfo_res res = {
8128 .pdev = pdev,
8129 };
8130 struct rpc_message msg = {
8131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8132 .rpc_argp = &args,
8133 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008134 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008135 };
8136 int status;
8137
8138 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008139 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008140 if (res.notification & ~args.notify_types)
8141 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008142 if (res.notification != args.notify_types)
8143 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008144
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008145 dprintk("<-- %s status=%d\n", __func__, status);
8146
8147 return status;
8148}
8149
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008150int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8151 struct pnfs_device *pdev,
8152 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008153{
8154 struct nfs4_exception exception = { };
8155 int err;
8156
8157 do {
8158 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008159 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008160 &exception);
8161 } while (exception.retry);
8162 return err;
8163}
8164EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8165
Andy Adamson863a3c62011-03-23 13:27:54 +00008166static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8167{
8168 struct nfs4_layoutcommit_data *data = calldata;
8169 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008170 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008171
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008172 nfs41_setup_sequence(session,
8173 &data->args.seq_args,
8174 &data->res.seq_res,
8175 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008176}
8177
8178static void
8179nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8180{
8181 struct nfs4_layoutcommit_data *data = calldata;
8182 struct nfs_server *server = NFS_SERVER(data->args.inode);
8183
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008184 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008185 return;
8186
8187 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008188 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8189 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8190 case -NFS4ERR_BADLAYOUT: /* no layout */
8191 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008192 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008193 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008194 break;
8195 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008196 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008197 rpc_restart_call_prepare(task);
8198 return;
8199 }
8200 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008201}
8202
8203static void nfs4_layoutcommit_release(void *calldata)
8204{
8205 struct nfs4_layoutcommit_data *data = calldata;
8206
Andy Adamsondb29c082011-07-30 20:52:38 -04008207 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008208 nfs_post_op_update_inode_force_wcc(data->args.inode,
8209 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008210 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008211 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008212 kfree(data);
8213}
8214
8215static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8216 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8217 .rpc_call_done = nfs4_layoutcommit_done,
8218 .rpc_release = nfs4_layoutcommit_release,
8219};
8220
8221int
Andy Adamsonef311532011-03-12 02:58:10 -05008222nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008223{
8224 struct rpc_message msg = {
8225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8226 .rpc_argp = &data->args,
8227 .rpc_resp = &data->res,
8228 .rpc_cred = data->cred,
8229 };
8230 struct rpc_task_setup task_setup_data = {
8231 .task = &data->task,
8232 .rpc_client = NFS_CLIENT(data->args.inode),
8233 .rpc_message = &msg,
8234 .callback_ops = &nfs4_layoutcommit_ops,
8235 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008236 };
8237 struct rpc_task *task;
8238 int status = 0;
8239
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008240 dprintk("NFS: initiating layoutcommit call. sync %d "
8241 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008242 data->args.lastbytewritten,
8243 data->args.inode->i_ino);
8244
Trond Myklebust472e2592015-02-05 16:50:30 -05008245 if (!sync) {
8246 data->inode = nfs_igrab_and_active(data->args.inode);
8247 if (data->inode == NULL) {
8248 nfs4_layoutcommit_release(data);
8249 return -EAGAIN;
8250 }
8251 task_setup_data.flags = RPC_TASK_ASYNC;
8252 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008253 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008254 task = rpc_run_task(&task_setup_data);
8255 if (IS_ERR(task))
8256 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008257 if (sync)
8258 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008259 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008260 dprintk("%s: status %d\n", __func__, status);
8261 rpc_put_task(task);
8262 return status;
8263}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008264
Andy Adamson97431202013-08-08 10:57:56 -04008265/**
8266 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8267 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8268 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008269static int
8270_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008271 struct nfs_fsinfo *info,
8272 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008273{
8274 struct nfs41_secinfo_no_name_args args = {
8275 .style = SECINFO_STYLE_CURRENT_FH,
8276 };
8277 struct nfs4_secinfo_res res = {
8278 .flavors = flavors,
8279 };
8280 struct rpc_message msg = {
8281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8282 .rpc_argp = &args,
8283 .rpc_resp = &res,
8284 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008285 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008286 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008287 int status;
8288
8289 if (use_integrity) {
8290 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008291 cred = nfs4_get_clid_cred(server->nfs_client);
8292 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008293 }
8294
8295 dprintk("--> %s\n", __func__);
8296 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8297 &res.seq_res, 0);
8298 dprintk("<-- %s status=%d\n", __func__, status);
8299
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008300 if (cred)
8301 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008302
8303 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008304}
8305
8306static int
8307nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8308 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8309{
8310 struct nfs4_exception exception = { };
8311 int err;
8312 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008313 /* first try using integrity protection */
8314 err = -NFS4ERR_WRONGSEC;
8315
8316 /* try to use integrity protection with machine cred */
8317 if (_nfs4_is_integrity_protected(server->nfs_client))
8318 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8319 flavors, true);
8320
8321 /*
8322 * if unable to use integrity protection, or SECINFO with
8323 * integrity protection returns NFS4ERR_WRONGSEC (which is
8324 * disallowed by spec, but exists in deployed servers) use
8325 * the current filesystem's rpc_client and the user cred.
8326 */
8327 if (err == -NFS4ERR_WRONGSEC)
8328 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8329 flavors, false);
8330
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008331 switch (err) {
8332 case 0:
8333 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008334 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008335 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008336 default:
8337 err = nfs4_handle_exception(server, err, &exception);
8338 }
8339 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008340out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008341 return err;
8342}
8343
8344static int
8345nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8346 struct nfs_fsinfo *info)
8347{
8348 int err;
8349 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008350 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008351 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008352 struct nfs4_secinfo4 *secinfo;
8353 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008354
8355 page = alloc_page(GFP_KERNEL);
8356 if (!page) {
8357 err = -ENOMEM;
8358 goto out;
8359 }
8360
8361 flavors = page_address(page);
8362 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8363
8364 /*
8365 * Fall back on "guess and check" method if
8366 * the server doesn't support SECINFO_NO_NAME
8367 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008368 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008369 err = nfs4_find_root_sec(server, fhandle, info);
8370 goto out_freepage;
8371 }
8372 if (err)
8373 goto out_freepage;
8374
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008375 for (i = 0; i < flavors->num_flavors; i++) {
8376 secinfo = &flavors->flavors[i];
8377
8378 switch (secinfo->flavor) {
8379 case RPC_AUTH_NULL:
8380 case RPC_AUTH_UNIX:
8381 case RPC_AUTH_GSS:
8382 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8383 &secinfo->flavor_info);
8384 break;
8385 default:
8386 flavor = RPC_AUTH_MAXFLAVOR;
8387 break;
8388 }
8389
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008390 if (!nfs_auth_info_match(&server->auth_info, flavor))
8391 flavor = RPC_AUTH_MAXFLAVOR;
8392
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008393 if (flavor != RPC_AUTH_MAXFLAVOR) {
8394 err = nfs4_lookup_root_sec(server, fhandle,
8395 info, flavor);
8396 if (!err)
8397 break;
8398 }
8399 }
8400
8401 if (flavor == RPC_AUTH_MAXFLAVOR)
8402 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008403
8404out_freepage:
8405 put_page(page);
8406 if (err == -EACCES)
8407 return -EPERM;
8408out:
8409 return err;
8410}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008411
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008412static int _nfs41_test_stateid(struct nfs_server *server,
8413 nfs4_stateid *stateid,
8414 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008415{
8416 int status;
8417 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008418 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008419 };
8420 struct nfs41_test_stateid_res res;
8421 struct rpc_message msg = {
8422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8423 .rpc_argp = &args,
8424 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008425 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008426 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008427 struct rpc_clnt *rpc_client = server->client;
8428
8429 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8430 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008431
Chuck Lever38527b12012-07-11 16:30:23 -04008432 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008433 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008434 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008435 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008436 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008437 if (status != NFS_OK) {
8438 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008439 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008440 }
8441 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008442 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008443}
8444
Chuck Lever38527b12012-07-11 16:30:23 -04008445/**
8446 * nfs41_test_stateid - perform a TEST_STATEID operation
8447 *
8448 * @server: server / transport on which to perform the operation
8449 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008450 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008451 *
8452 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8453 * Otherwise a negative NFS4ERR value is returned if the operation
8454 * failed or the state ID is not currently valid.
8455 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008456static int nfs41_test_stateid(struct nfs_server *server,
8457 nfs4_stateid *stateid,
8458 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008459{
8460 struct nfs4_exception exception = { };
8461 int err;
8462 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008463 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008464 if (err != -NFS4ERR_DELAY)
8465 break;
8466 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008467 } while (exception.retry);
8468 return err;
8469}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008470
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008471struct nfs_free_stateid_data {
8472 struct nfs_server *server;
8473 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008474 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008475};
8476
8477static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8478{
8479 struct nfs_free_stateid_data *data = calldata;
8480 nfs41_setup_sequence(nfs4_get_session(data->server),
8481 &data->args.seq_args,
8482 &data->res.seq_res,
8483 task);
8484}
8485
8486static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8487{
8488 struct nfs_free_stateid_data *data = calldata;
8489
8490 nfs41_sequence_done(task, &data->res.seq_res);
8491
8492 switch (task->tk_status) {
8493 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008494 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008495 rpc_restart_call_prepare(task);
8496 }
8497}
8498
8499static void nfs41_free_stateid_release(void *calldata)
8500{
8501 kfree(calldata);
8502}
8503
Trond Myklebust17f26b12013-08-21 15:48:42 -04008504static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008505 .rpc_call_prepare = nfs41_free_stateid_prepare,
8506 .rpc_call_done = nfs41_free_stateid_done,
8507 .rpc_release = nfs41_free_stateid_release,
8508};
8509
8510static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8511 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008512 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008513 bool privileged)
8514{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008515 struct rpc_message msg = {
8516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008517 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008518 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008519 struct rpc_task_setup task_setup = {
8520 .rpc_client = server->client,
8521 .rpc_message = &msg,
8522 .callback_ops = &nfs41_free_stateid_ops,
8523 .flags = RPC_TASK_ASYNC,
8524 };
8525 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008526
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008527 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8528 &task_setup.rpc_client, &msg);
8529
Chuck Lever38527b12012-07-11 16:30:23 -04008530 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008531 data = kmalloc(sizeof(*data), GFP_NOFS);
8532 if (!data)
8533 return ERR_PTR(-ENOMEM);
8534 data->server = server;
8535 nfs4_stateid_copy(&data->args.stateid, stateid);
8536
8537 task_setup.callback_data = data;
8538
8539 msg.rpc_argp = &data->args;
8540 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008541 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008542 if (privileged)
8543 nfs4_set_sequence_privileged(&data->args.seq_args);
8544
8545 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008546}
8547
Chuck Lever38527b12012-07-11 16:30:23 -04008548/**
8549 * nfs41_free_stateid - perform a FREE_STATEID operation
8550 *
8551 * @server: server / transport on which to perform the operation
8552 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008553 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008554 *
8555 * Returns NFS_OK if the server freed "stateid". Otherwise a
8556 * negative NFS4ERR value is returned.
8557 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008558static int nfs41_free_stateid(struct nfs_server *server,
8559 nfs4_stateid *stateid,
8560 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008561{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008562 struct rpc_task *task;
8563 int ret;
8564
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008565 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008566 if (IS_ERR(task))
8567 return PTR_ERR(task);
8568 ret = rpc_wait_for_completion_task(task);
8569 if (!ret)
8570 ret = task->tk_status;
8571 rpc_put_task(task);
8572 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008573}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008574
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008575static void
8576nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008577{
8578 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008579 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008580
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008581 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008582 nfs4_free_lock_state(server, lsp);
8583 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008584 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008585 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008586}
8587
Trond Myklebust36281ca2012-03-04 18:13:56 -05008588static bool nfs41_match_stateid(const nfs4_stateid *s1,
8589 const nfs4_stateid *s2)
8590{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008591 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008592 return false;
8593
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008594 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008595 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008596 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008597 return true;
8598
8599 return false;
8600}
8601
Andy Adamson557134a2009-04-01 09:21:53 -04008602#endif /* CONFIG_NFS_V4_1 */
8603
Trond Myklebust36281ca2012-03-04 18:13:56 -05008604static bool nfs4_match_stateid(const nfs4_stateid *s1,
8605 const nfs4_stateid *s2)
8606{
Trond Myklebustf597c532012-03-04 18:13:56 -05008607 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008608}
8609
8610
Trond Myklebust17280172012-03-11 13:11:00 -04008611static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008612 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008613 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614 .recover_open = nfs4_open_reclaim,
8615 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008616 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008617 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008618};
8619
Andy Adamson591d71c2009-04-01 09:22:47 -04008620#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008621static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008622 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8623 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8624 .recover_open = nfs4_open_reclaim,
8625 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008626 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008627 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008628 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008629};
8630#endif /* CONFIG_NFS_V4_1 */
8631
Trond Myklebust17280172012-03-11 13:11:00 -04008632static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008633 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008634 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008635 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008637 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638};
8639
Andy Adamson591d71c2009-04-01 09:22:47 -04008640#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008641static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008642 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8643 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008644 .recover_open = nfs41_open_expired,
8645 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008646 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008647};
8648#endif /* CONFIG_NFS_V4_1 */
8649
Trond Myklebust17280172012-03-11 13:11:00 -04008650static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008651 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008652 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008653 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008654};
8655
8656#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008657static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008658 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008659 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008660 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008661};
8662#endif
8663
Chuck Leverec011fe2013-10-17 14:12:39 -04008664static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008665 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008666 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008667};
8668
8669#if defined(CONFIG_NFS_V4_1)
8670static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008671 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008672 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008673};
8674#endif /* CONFIG_NFS_V4_1 */
8675
Trond Myklebust97dc1352010-06-16 09:52:26 -04008676static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8677 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008678 .init_caps = NFS_CAP_READDIRPLUS
8679 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008680 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008681 .init_client = nfs40_init_client,
8682 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008683 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008684 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008685 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008686 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008687 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008688 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8689 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8690 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008691 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008692};
8693
8694#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008695static struct nfs_seqid *
8696nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8697{
8698 return NULL;
8699}
8700
Trond Myklebust97dc1352010-06-16 09:52:26 -04008701static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8702 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008703 .init_caps = NFS_CAP_READDIRPLUS
8704 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008705 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008706 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008707 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008708 .init_client = nfs41_init_client,
8709 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008710 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008711 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008712 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008713 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008714 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008715 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8716 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8717 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008718 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008719};
8720#endif
8721
Steve Dickson42c2c422013-05-22 12:50:38 -04008722#if defined(CONFIG_NFS_V4_2)
8723static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8724 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008725 .init_caps = NFS_CAP_READDIRPLUS
8726 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008727 | NFS_CAP_POSIX_LOCK
8728 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008729 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008730 | NFS_CAP_ALLOCATE
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008731 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008732 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08008733 | NFS_CAP_LAYOUTSTATS
8734 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008735 .init_client = nfs41_init_client,
8736 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008737 .match_stateid = nfs41_match_stateid,
8738 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008739 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008740 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008741 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008742 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8743 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8744 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08008745 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008746};
8747#endif
8748
Trond Myklebust97dc1352010-06-16 09:52:26 -04008749const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8750 [0] = &nfs_v4_0_minor_ops,
8751#if defined(CONFIG_NFS_V4_1)
8752 [1] = &nfs_v4_1_minor_ops,
8753#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008754#if defined(CONFIG_NFS_V4_2)
8755 [2] = &nfs_v4_2_minor_ops,
8756#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008757};
8758
Trond Myklebust17f26b12013-08-21 15:48:42 -04008759static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008760 .create = nfs_create,
8761 .lookup = nfs_lookup,
8762 .atomic_open = nfs_atomic_open,
8763 .link = nfs_link,
8764 .unlink = nfs_unlink,
8765 .symlink = nfs_symlink,
8766 .mkdir = nfs_mkdir,
8767 .rmdir = nfs_rmdir,
8768 .mknod = nfs_mknod,
8769 .rename = nfs_rename,
8770 .permission = nfs_permission,
8771 .getattr = nfs_getattr,
8772 .setattr = nfs_setattr,
8773 .getxattr = generic_getxattr,
8774 .setxattr = generic_setxattr,
8775 .listxattr = generic_listxattr,
8776 .removexattr = generic_removexattr,
8777};
8778
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008779static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008780 .permission = nfs_permission,
8781 .getattr = nfs_getattr,
8782 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008783 .getxattr = generic_getxattr,
8784 .setxattr = generic_setxattr,
8785 .listxattr = generic_listxattr,
8786 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008787};
8788
David Howells509de812006-08-22 20:06:11 -04008789const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008790 .version = 4, /* protocol version */
8791 .dentry_ops = &nfs4_dentry_operations,
8792 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008793 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008794 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008795 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008796 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008797 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798 .getattr = nfs4_proc_getattr,
8799 .setattr = nfs4_proc_setattr,
8800 .lookup = nfs4_proc_lookup,
8801 .access = nfs4_proc_access,
8802 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008803 .create = nfs4_proc_create,
8804 .remove = nfs4_proc_remove,
8805 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008806 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008807 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008808 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008809 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008810 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811 .link = nfs4_proc_link,
8812 .symlink = nfs4_proc_symlink,
8813 .mkdir = nfs4_proc_mkdir,
8814 .rmdir = nfs4_proc_remove,
8815 .readdir = nfs4_proc_readdir,
8816 .mknod = nfs4_proc_mknod,
8817 .statfs = nfs4_proc_statfs,
8818 .fsinfo = nfs4_proc_fsinfo,
8819 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008820 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008822 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008824 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008825 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008826 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008828 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008829 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008830 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008831 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008832 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008833 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008834 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008835 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008836 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008837 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008838 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008839 .create_server = nfs4_create_server,
8840 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008841};
8842
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008843static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8844 .prefix = XATTR_NAME_NFSV4_ACL,
8845 .list = nfs4_xattr_list_nfs4_acl,
8846 .get = nfs4_xattr_get_nfs4_acl,
8847 .set = nfs4_xattr_set_nfs4_acl,
8848};
8849
8850const struct xattr_handler *nfs4_xattr_handlers[] = {
8851 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008852#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8853 &nfs4_xattr_nfs4_label_handler,
8854#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008855 NULL
8856};
8857
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858/*
8859 * Local variables:
8860 * c-basic-offset: 8
8861 * End:
8862 */