blob: 69dc20a743f9d35a8bc4b090b96e2c727c186e54 [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>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.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"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
NeilBrown8478eaa2014-09-18 16:09:27 +100080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *, long *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static 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 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
David Quigleyaa9c2662013-05-22 12:50:44 -0400108 err = security_dentry_init_security(dentry, sattr->ia_mode,
109 &dentry->d_name, (void **)&label->label, &label->len);
110 if (err == 0)
111 return label;
112
113 return NULL;
114}
115static inline void
116nfs4_label_release_security(struct nfs4_label *label)
117{
118 if (label)
119 security_release_secctx(label->label, label->len);
120}
121static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
122{
123 if (label)
124 return server->attr_bitmask;
125
126 return server->attr_bitmask_nl;
127}
128#else
129static inline struct nfs4_label *
130nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
131 struct iattr *sattr, struct nfs4_label *l)
132{ return NULL; }
133static inline void
134nfs4_label_release_security(struct nfs4_label *label)
135{ return; }
136static inline u32 *
137nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138{ return server->attr_bitmask; }
139#endif
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500142static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Trond Myklebust52567b02009-10-23 14:46:42 -0400144 if (err >= -1000)
145 return err;
146 switch (err) {
147 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500148 case -NFS4ERR_LAYOUTTRYLATER:
149 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400150 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000151 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400152 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000153 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800154 case -NFS4ERR_BADOWNER:
155 case -NFS4ERR_BADNAME:
156 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400157 case -NFS4ERR_SHARE_DENIED:
158 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400159 case -NFS4ERR_MINOR_VERS_MISMATCH:
160 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400161 case -NFS4ERR_ACCESS:
162 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400163 case -NFS4ERR_FILE_OPEN:
164 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700167 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400170 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/*
174 * This is our standard bitmap for GETATTR requests.
175 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400176const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 FATTR4_WORD0_TYPE
178 | FATTR4_WORD0_CHANGE
179 | FATTR4_WORD0_SIZE
180 | FATTR4_WORD0_FSID
181 | FATTR4_WORD0_FILEID,
182 FATTR4_WORD1_MODE
183 | FATTR4_WORD1_NUMLINKS
184 | FATTR4_WORD1_OWNER
185 | FATTR4_WORD1_OWNER_GROUP
186 | FATTR4_WORD1_RAWDEV
187 | FATTR4_WORD1_SPACE_USED
188 | FATTR4_WORD1_TIME_ACCESS
189 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400190 | FATTR4_WORD1_TIME_MODIFY,
191#ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Trond Myklebust1549210f2012-06-05 09:16:47 -0400196static const u32 nfs4_pnfs_open_bitmap[3] = {
197 FATTR4_WORD0_TYPE
198 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_SIZE
200 | FATTR4_WORD0_FSID
201 | FATTR4_WORD0_FILEID,
202 FATTR4_WORD1_MODE
203 | FATTR4_WORD1_NUMLINKS
204 | FATTR4_WORD1_OWNER
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
212};
213
Andy Adamsone23008e2012-10-02 21:07:32 -0400214static const u32 nfs4_open_noattr_bitmap[3] = {
215 FATTR4_WORD0_TYPE
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
218};
219
David Quigleya09df2c2013-05-22 12:50:41 -0400220const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
227};
228
David Quigleya09df2c2013-05-22 12:50:41 -0400229const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 FATTR4_WORD0_MAXLINK
231 | FATTR4_WORD0_MAXNAME,
232 0
233};
234
Fred Isamandae100c2011-07-30 20:52:37 -0400235const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700239 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400245 FATTR4_WORD0_TYPE
246 | FATTR4_WORD0_CHANGE
247 | FATTR4_WORD0_SIZE
248 | FATTR4_WORD0_FSID
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
251 FATTR4_WORD1_MODE
252 | FATTR4_WORD1_NUMLINKS
253 | FATTR4_WORD1_OWNER
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400261};
262
Al Virobc4785c2006-10-19 23:28:51 -0700263static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct nfs4_readdir_arg *readdir)
265{
Al Viro0dbb4c62006-10-19 23:28:49 -0700266 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000269 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
271 return;
272 }
273
274 readdir->cookie = 0;
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
276 if (cookie == 2)
277 return;
278
279 /*
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
284 * instead of 1 or 2.
285 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800286 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (cookie == 0) {
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
294 p++;
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400298 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400310 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800314 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
NeilBrown8478eaa2014-09-18 16:09:27 +1000317static long nfs4_update_delay(long *timeout)
318{
319 long ret;
320 if (!timeout)
321 return NFS4_POLL_RETRY_MAX;
322 if (*timeout <= 0)
323 *timeout = NFS4_POLL_RETRY_MIN;
324 if (*timeout > NFS4_POLL_RETRY_MAX)
325 *timeout = NFS4_POLL_RETRY_MAX;
326 ret = *timeout;
327 *timeout <<= 1;
328 return ret;
329}
330
Trond Myklebust65de8722008-12-23 15:21:44 -0500331static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
332{
333 int res = 0;
334
335 might_sleep();
336
NeilBrown8478eaa2014-09-18 16:09:27 +1000337 freezable_schedule_timeout_killable_unsafe(
338 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 if (fatal_signal_pending(current))
340 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 return res;
342}
343
344/* This is the error handling routine for processes that are allowed
345 * to sleep.
346 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800347static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500348{
349 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500350 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500352 int ret = errorcode;
353
354 exception->retry = 0;
355 switch(errorcode) {
356 case 0:
357 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500358 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400359 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400360 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500361 exception->retry = 1;
362 return 0;
363 }
364 if (state == NULL)
365 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400366 ret = nfs4_schedule_stateid_recovery(server, state);
367 if (ret < 0)
368 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500369 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500370 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500371 case -NFS4ERR_ADMIN_REVOKED:
372 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500373 if (state == NULL)
374 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400375 ret = nfs4_schedule_stateid_recovery(server, state);
376 if (ret < 0)
377 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500378 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400379 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400380 if (state != NULL) {
381 ret = nfs4_schedule_stateid_recovery(server, state);
382 if (ret < 0)
383 break;
384 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500385 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500386 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500387 nfs4_schedule_lease_recovery(clp);
388 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400389 case -NFS4ERR_MOVED:
390 ret = nfs4_schedule_migration_recovery(server);
391 if (ret < 0)
392 break;
393 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400394 case -NFS4ERR_LEASE_MOVED:
395 nfs4_schedule_lease_moved_recovery(clp);
396 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500397#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400398 case -NFS4ERR_BADSESSION:
399 case -NFS4ERR_BADSLOT:
400 case -NFS4ERR_BAD_HIGH_SLOT:
401 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
402 case -NFS4ERR_DEADSESSION:
403 case -NFS4ERR_SEQ_FALSE_RETRY:
404 case -NFS4ERR_SEQ_MISORDERED:
405 dprintk("%s ERROR: %d Reset session\n", __func__,
406 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400407 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400408 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500409#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500410 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500411 if (exception->timeout > HZ) {
412 /* We have retried a decent amount, time to
413 * fail
414 */
415 ret = -EBUSY;
416 break;
417 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500418 case -NFS4ERR_GRACE:
419 case -NFS4ERR_DELAY:
420 ret = nfs4_delay(server->client, &exception->timeout);
421 if (ret != 0)
422 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400423 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500424 case -NFS4ERR_OLD_STATEID:
425 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800426 break;
427 case -NFS4ERR_BADOWNER:
428 /* The following works around a Linux server bug! */
429 case -NFS4ERR_BADNAME:
430 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
431 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
432 exception->retry = 1;
433 printk(KERN_WARNING "NFS: v4 server %s "
434 "does not accept raw "
435 "uid/gids. "
436 "Reenabling the idmapper.\n",
437 server->nfs_client->cl_hostname);
438 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500439 }
440 /* We failed to handle the error */
441 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500442wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500443 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400444 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
445 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500446 if (ret == 0)
447 exception->retry = 1;
448 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500449}
450
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400451/*
452 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
453 * or 'false' otherwise.
454 */
455static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
456{
457 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
458
459 if (flavor == RPC_AUTH_GSS_KRB5I ||
460 flavor == RPC_AUTH_GSS_KRB5P)
461 return true;
462
463 return false;
464}
Trond Myklebust65de8722008-12-23 15:21:44 -0500465
Trond Myklebust452e9352010-07-31 14:29:06 -0400466static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 spin_lock(&clp->cl_lock);
469 if (time_before(clp->cl_last_renewal,timestamp))
470 clp->cl_last_renewal = timestamp;
471 spin_unlock(&clp->cl_lock);
472}
473
Trond Myklebust452e9352010-07-31 14:29:06 -0400474static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
475{
476 do_renew_lease(server->nfs_client, timestamp);
477}
478
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400479struct nfs4_call_sync_data {
480 const struct nfs_server *seq_server;
481 struct nfs4_sequence_args *seq_args;
482 struct nfs4_sequence_res *seq_res;
483};
484
Chuck Levera9c92d62013-08-09 12:48:18 -0400485static void nfs4_init_sequence(struct nfs4_sequence_args *args,
486 struct nfs4_sequence_res *res, int cache_reply)
487{
488 args->sa_slot = NULL;
489 args->sa_cache_this = cache_reply;
490 args->sa_privileged = 0;
491
492 res->sr_slot = NULL;
493}
494
495static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
496{
497 args->sa_privileged = 1;
498}
499
Chuck Lever3bd23842013-08-09 12:49:19 -0400500static int nfs40_setup_sequence(const struct nfs_server *server,
501 struct nfs4_sequence_args *args,
502 struct nfs4_sequence_res *res,
503 struct rpc_task *task)
504{
505 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
506 struct nfs4_slot *slot;
507
508 /* slot already allocated? */
509 if (res->sr_slot != NULL)
510 goto out_start;
511
512 spin_lock(&tbl->slot_tbl_lock);
513 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
514 goto out_sleep;
515
516 slot = nfs4_alloc_slot(tbl);
517 if (IS_ERR(slot)) {
518 if (slot == ERR_PTR(-ENOMEM))
519 task->tk_timeout = HZ >> 2;
520 goto out_sleep;
521 }
522 spin_unlock(&tbl->slot_tbl_lock);
523
524 args->sa_slot = slot;
525 res->sr_slot = slot;
526
527out_start:
528 rpc_call_start(task);
529 return 0;
530
531out_sleep:
532 if (args->sa_privileged)
533 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
534 NULL, RPC_PRIORITY_PRIVILEGED);
535 else
536 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
537 spin_unlock(&tbl->slot_tbl_lock);
538 return -EAGAIN;
539}
540
541static int nfs40_sequence_done(struct rpc_task *task,
542 struct nfs4_sequence_res *res)
543{
544 struct nfs4_slot *slot = res->sr_slot;
545 struct nfs4_slot_table *tbl;
546
Trond Myklebustcab92c12014-01-29 12:12:15 -0500547 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400548 goto out;
549
550 tbl = slot->table;
551 spin_lock(&tbl->slot_tbl_lock);
552 if (!nfs41_wake_and_assign_slot(tbl, slot))
553 nfs4_free_slot(tbl, slot);
554 spin_unlock(&tbl->slot_tbl_lock);
555
556 res->sr_slot = NULL;
557out:
558 return 1;
559}
560
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400561#if defined(CONFIG_NFS_V4_1)
562
Trond Myklebustd185a332010-06-16 09:52:25 -0400563static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400564{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500565 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400566 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500567 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500568 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400569
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500570 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500571 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500572
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500573 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500574 /* Be nice to the server: try to ensure that the last transmitted
575 * value for highest_user_slotid <= target_highest_slotid
576 */
577 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
578 send_new_highest_used_slotid = true;
579
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500580 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500581 send_new_highest_used_slotid = false;
582 goto out_unlock;
583 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500584 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500585
586 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
587 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500588out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500589 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400590 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500591 if (send_new_highest_used_slotid)
592 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400593}
594
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500595int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400596{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500597 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500598 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400599 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500600 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500601 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400602
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500603 if (slot == NULL)
604 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400605 /* don't increment the sequence number if the task wasn't sent */
606 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400607 goto out;
608
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500609 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500610
Trond Myklebustac20d162012-12-15 15:36:07 -0500611 if (slot->interrupted) {
612 slot->interrupted = 0;
613 interrupted = true;
614 }
615
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400616 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500617 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400618 switch (res->sr_status) {
619 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400620 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500621 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500622 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500623 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500624 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500625 if (res->sr_status_flags != 0)
626 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500627 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400628 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500629 case 1:
630 /*
631 * sr_status remains 1 if an RPC level error occurred.
632 * The server may or may not have processed the sequence
633 * operation..
634 * Mark the slot as having hosted an interrupted RPC call.
635 */
636 slot->interrupted = 1;
637 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400638 case -NFS4ERR_DELAY:
639 /* The server detected a resend of the RPC call and
640 * returned NFS4ERR_DELAY as per Section 2.10.6.2
641 * of RFC5661.
642 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500643 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400644 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500645 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500646 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400647 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500648 case -NFS4ERR_BADSLOT:
649 /*
650 * The slot id we used was probably retired. Try again
651 * using a different slot id.
652 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500653 goto retry_nowait;
654 case -NFS4ERR_SEQ_MISORDERED:
655 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500656 * Was the last operation on this sequence interrupted?
657 * If so, retry after bumping the sequence number.
658 */
659 if (interrupted) {
660 ++slot->seq_nr;
661 goto retry_nowait;
662 }
663 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500664 * Could this slot have been previously retired?
665 * If so, then the server may be expecting seq_nr = 1!
666 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500667 if (slot->seq_nr != 1) {
668 slot->seq_nr = 1;
669 goto retry_nowait;
670 }
671 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500672 case -NFS4ERR_SEQ_FALSE_RETRY:
673 ++slot->seq_nr;
674 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400675 default:
676 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500677 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400678 }
679out:
680 /* The session may be reset by one of the error handlers. */
681 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400682 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500683out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500684 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500685retry_nowait:
686 if (rpc_restart_call_prepare(task)) {
687 task->tk_status = 0;
688 ret = 0;
689 }
690 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400691out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400692 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400693 goto out;
694 rpc_delay(task, NFS4_POLL_RETRY_MAX);
695 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400696}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500697EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400698
Trond Myklebust14516c32010-07-31 14:29:06 -0400699static int nfs4_sequence_done(struct rpc_task *task,
700 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400701{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500702 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400703 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400704 if (!res->sr_slot->table->session)
705 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400706 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400707}
708
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000709int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400710 struct nfs4_sequence_args *args,
711 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400712 struct rpc_task *task)
713{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400714 struct nfs4_slot *slot;
715 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400716
717 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400718 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400719 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400720 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400721
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400722 tbl = &session->fc_slot_table;
723
Trond Myklebust69d206b2012-11-22 13:21:02 -0500724 task->tk_timeout = 0;
725
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400726 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400727 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400728 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500729 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500730 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400731 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400732 }
733
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500734 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500735 if (IS_ERR(slot)) {
736 /* If out of memory, try again in 1/4 second */
737 if (slot == ERR_PTR(-ENOMEM))
738 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400739 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400740 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400741 }
742 spin_unlock(&tbl->slot_tbl_lock);
743
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500744 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400745
Chuck Levere8d92382013-08-09 12:47:51 -0400746 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500747 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400748
Benny Halevydfb4f3092010-09-24 09:17:01 -0400749 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500750 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400751 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400752 /*
753 * sr_status is only set in decode_sequence, and so will remain
754 * set to 1 if an rpc level failure occurs.
755 */
756 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400757 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400758out_success:
759 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400760 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400761out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400762 /* Privileged tasks are queued with top priority */
763 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400764 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
765 NULL, RPC_PRIORITY_PRIVILEGED);
766 else
767 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400768 spin_unlock(&tbl->slot_tbl_lock);
769 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400770}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000771EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400772
Chuck Lever5a580e02013-08-09 12:48:09 -0400773static int nfs4_setup_sequence(const struct nfs_server *server,
774 struct nfs4_sequence_args *args,
775 struct nfs4_sequence_res *res,
776 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400777{
Trond Myklebust035168a2010-06-16 09:52:26 -0400778 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400779 int ret = 0;
780
Chuck Lever3bd23842013-08-09 12:49:19 -0400781 if (!session)
782 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400783
Chuck Levere8d92382013-08-09 12:47:51 -0400784 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400785 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400786 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400787
Trond Myklebust9d12b212012-01-17 22:04:25 -0500788 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400789
Andy Adamsonce5039c2009-04-01 09:22:13 -0400790 dprintk("<-- %s status=%d\n", __func__, ret);
791 return ret;
792}
793
Andy Adamsonce5039c2009-04-01 09:22:13 -0400794static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
795{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400796 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400797 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400798
Trond Myklebust035168a2010-06-16 09:52:26 -0400799 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
800
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400801 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400802}
803
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
805{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400806 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400807
Trond Myklebust14516c32010-07-31 14:29:06 -0400808 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400809}
810
Trond Myklebust17280172012-03-11 13:11:00 -0400811static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400812 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400813 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400814};
815
Chuck Lever3bd23842013-08-09 12:49:19 -0400816#else /* !CONFIG_NFS_V4_1 */
817
Chuck Lever5a580e02013-08-09 12:48:09 -0400818static int nfs4_setup_sequence(const struct nfs_server *server,
819 struct nfs4_sequence_args *args,
820 struct nfs4_sequence_res *res,
821 struct rpc_task *task)
822{
Chuck Lever3bd23842013-08-09 12:49:19 -0400823 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400824}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400825
Trond Myklebust14516c32010-07-31 14:29:06 -0400826static int nfs4_sequence_done(struct rpc_task *task,
827 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400828{
Chuck Lever3bd23842013-08-09 12:49:19 -0400829 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400830}
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;
David Howellsde242c02012-12-20 21:52:38 +0000903 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 spin_unlock(&dir->i_lock);
905}
906
907struct nfs4_opendata {
908 struct kref kref;
909 struct nfs_openargs o_arg;
910 struct nfs_openres o_res;
911 struct nfs_open_confirmargs c_arg;
912 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500913 struct nfs4_string owner_name;
914 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400916 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400918 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct nfs4_state_owner *owner;
920 struct nfs4_state *state;
921 struct iattr attrs;
922 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400923 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400924 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400925 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400926 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int cancelled;
928};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400929
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400930static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
931 int err, struct nfs4_exception *exception)
932{
933 if (err != -EINVAL)
934 return false;
935 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
936 return false;
937 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
938 exception->retry = 1;
939 return true;
940}
941
942static enum open_claim_type4
943nfs4_map_atomic_open_claim(struct nfs_server *server,
944 enum open_claim_type4 claim)
945{
946 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
947 return claim;
948 switch (claim) {
949 default:
950 return claim;
951 case NFS4_OPEN_CLAIM_FH:
952 return NFS4_OPEN_CLAIM_NULL;
953 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
954 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
955 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
956 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
957 }
958}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961{
962 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400963 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 p->o_res.seqid = p->o_arg.seqid;
965 p->c_res.seqid = p->c_arg.seqid;
966 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400967 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500969 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100970}
971
Al Viro82a2c1b2011-06-22 18:30:55 -0400972static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100973 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400974 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400975 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400976 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400977 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100978{
Al Viro82a2c1b2011-06-22 18:30:55 -0400979 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100980 struct inode *dir = parent->d_inode;
981 struct nfs_server *server = NFS_SERVER(dir);
982 struct nfs4_opendata *p;
983
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400984 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100985 if (p == NULL)
986 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400987
988 p->f_label = nfs4_label_alloc(server, gfp_mask);
989 if (IS_ERR(p->f_label))
990 goto err_free_p;
991
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400992 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100993 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400994 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400995 nfs_sb_active(dentry->d_sb);
996 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100997 p->dir = parent;
998 p->owner = sp;
999 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 p->o_arg.open_flags = flags;
1001 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001002 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1003 * will return permission denied for all bits until close */
1004 if (!(flags & O_EXCL)) {
1005 /* ask server to check for all possible rights as results
1006 * are cached */
1007 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1008 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001009 }
David Howells7539bba2006-08-22 20:06:09 -04001010 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001011 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1012 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001013 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001014 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001015 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001016 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001017 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001018 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1019 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001020 case NFS4_OPEN_CLAIM_NULL:
1021 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1022 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1023 p->o_arg.fh = NFS_FH(dir);
1024 break;
1025 case NFS4_OPEN_CLAIM_PREVIOUS:
1026 case NFS4_OPEN_CLAIM_FH:
1027 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1028 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1029 p->o_arg.fh = NFS_FH(dentry->d_inode);
1030 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001031 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001032 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001033
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001034 p->o_arg.u.attrs = &p->attrs;
1035 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001036
1037 verf[0] = jiffies;
1038 verf[1] = current->pid;
1039 memcpy(p->o_arg.u.verifier.data, verf,
1040 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001041 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001042 p->c_arg.fh = &p->o_res.fh;
1043 p->c_arg.stateid = &p->o_res.stateid;
1044 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001045 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001046 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001048
1049err_free_label:
1050 nfs4_label_free(p->f_label);
1051err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 kfree(p);
1053err:
1054 dput(parent);
1055 return NULL;
1056}
1057
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001058static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001059{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001060 struct nfs4_opendata *p = container_of(kref,
1061 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001062 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001063
1064 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001065 if (p->state != NULL)
1066 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001067 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001068
1069 nfs4_label_free(p->f_label);
1070
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001071 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001072 dput(p->dentry);
1073 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001074 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001075 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001076 kfree(p);
1077}
1078
1079static void nfs4_opendata_put(struct nfs4_opendata *p)
1080{
1081 if (p != NULL)
1082 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001083}
1084
Trond Myklebust06f814a2006-01-03 09:55:07 +01001085static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1086{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001087 int ret;
1088
Trond Myklebust06f814a2006-01-03 09:55:07 +01001089 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001090 return ret;
1091}
1092
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001093static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001094{
1095 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096
Trond Myklebust536e43d2012-01-17 22:04:26 -05001097 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001098 goto out;
1099 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001101 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1102 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 break;
1104 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001105 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1106 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001107 break;
1108 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001109 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1110 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001111 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001112out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001113 return ret;
1114}
1115
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001116static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001118 if (delegation == NULL)
1119 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001122 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001123 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001124 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1125 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001126 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 return 1;
1128}
1129
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001130static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001131{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001132 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001133 case FMODE_WRITE:
1134 state->n_wronly++;
1135 break;
1136 case FMODE_READ:
1137 state->n_rdonly++;
1138 break;
1139 case FMODE_READ|FMODE_WRITE:
1140 state->n_rdwr++;
1141 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001142 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001143}
1144
Trond Myklebust4f14c192014-02-12 19:15:06 -05001145static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001146{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001147 struct nfs_client *clp = state->owner->so_server->nfs_client;
1148 bool need_recover = false;
1149
1150 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1151 need_recover = true;
1152 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1153 need_recover = true;
1154 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1155 need_recover = true;
1156 if (need_recover)
1157 nfs4_state_mark_reclaim_nograce(clp, state);
1158}
1159
Trond Myklebuste999e802014-02-10 18:20:47 -05001160static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1161 nfs4_stateid *stateid)
1162{
1163 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1164 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001165 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1166 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001167 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001168 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001169 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1170 return true;
1171 return false;
1172}
1173
Trond Myklebust226056c2014-02-11 10:41:07 -05001174static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1175 nfs4_stateid *stateid, fmode_t fmode)
1176{
1177 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1178 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1179 case FMODE_WRITE:
1180 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1181 break;
1182 case FMODE_READ:
1183 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1184 break;
1185 case 0:
1186 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1187 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1188 clear_bit(NFS_OPEN_STATE, &state->flags);
1189 }
1190 if (stateid == NULL)
1191 return;
1192 if (!nfs_need_update_open_stateid(state, stateid))
1193 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001194 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001195 nfs4_stateid_copy(&state->stateid, stateid);
1196 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001197}
1198
1199static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1200{
1201 write_seqlock(&state->seqlock);
1202 nfs_clear_open_stateid_locked(state, stateid, fmode);
1203 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001204 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1205 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001206}
1207
Trond Myklebust003707c2007-07-05 18:07:55 -04001208static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1209{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001211 case FMODE_READ:
1212 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1213 break;
1214 case FMODE_WRITE:
1215 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1216 break;
1217 case FMODE_READ|FMODE_WRITE:
1218 set_bit(NFS_O_RDWR_STATE, &state->flags);
1219 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001220 if (!nfs_need_update_open_stateid(state, stateid))
1221 return;
1222 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1223 nfs4_stateid_copy(&state->stateid, stateid);
1224 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001225}
1226
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001227static 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 -07001228{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001229 /*
1230 * Protect the call to nfs4_state_set_mode_locked and
1231 * serialise the stateid update
1232 */
1233 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001234 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001235 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001236 set_bit(NFS_DELEGATED_STATE, &state->flags);
1237 }
1238 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001239 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001240 write_sequnlock(&state->seqlock);
1241 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001242 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001243 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001246static 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 -05001247{
1248 struct nfs_inode *nfsi = NFS_I(state->inode);
1249 struct nfs_delegation *deleg_cur;
1250 int ret = 0;
1251
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001252 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001253
1254 rcu_read_lock();
1255 deleg_cur = rcu_dereference(nfsi->delegation);
1256 if (deleg_cur == NULL)
1257 goto no_delegation;
1258
1259 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001260 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001261 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001262 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001263 goto no_delegation_unlock;
1264
1265 if (delegation == NULL)
1266 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001267 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001268 goto no_delegation_unlock;
1269
Trond Myklebustb7391f42008-12-23 15:21:52 -05001270 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001271 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001272 ret = 1;
1273no_delegation_unlock:
1274 spin_unlock(&deleg_cur->lock);
1275no_delegation:
1276 rcu_read_unlock();
1277
1278 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001279 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001280 ret = 1;
1281 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001282 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1283 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001284
1285 return ret;
1286}
1287
1288
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001289static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001290{
1291 struct nfs_delegation *delegation;
1292
1293 rcu_read_lock();
1294 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001295 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001296 rcu_read_unlock();
1297 return;
1298 }
1299 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001300 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001301}
1302
Trond Myklebust6ee41262007-07-08 14:11:36 -04001303static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001304{
1305 struct nfs4_state *state = opendata->state;
1306 struct nfs_inode *nfsi = NFS_I(state->inode);
1307 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001308 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001310 nfs4_stateid stateid;
1311 int ret = -EAGAIN;
1312
Trond Myklebustaac00a82007-07-05 19:02:21 -04001313 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001314 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001315 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001316 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001317 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001318 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001319 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001320 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001321 rcu_read_lock();
1322 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001323 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001324 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001325 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001326 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001327 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001328 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001329 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001330 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001331 if (!opendata->is_recover) {
1332 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1333 if (ret != 0)
1334 goto out;
1335 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001336 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001337
1338 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001339 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001340 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001341 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001342out:
1343 return ERR_PTR(ret);
1344out_return_state:
1345 atomic_inc(&state->count);
1346 return state;
1347}
1348
Andy Adamsone23008e2012-10-02 21:07:32 -04001349static void
1350nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1351{
1352 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1353 struct nfs_delegation *delegation;
1354 int delegation_flags = 0;
1355
1356 rcu_read_lock();
1357 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1358 if (delegation)
1359 delegation_flags = delegation->flags;
1360 rcu_read_unlock();
1361 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1362 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1363 "returning a delegation for "
1364 "OPEN(CLAIM_DELEGATE_CUR)\n",
1365 clp->cl_hostname);
1366 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1367 nfs_inode_set_delegation(state->inode,
1368 data->owner->so_cred,
1369 &data->o_res);
1370 else
1371 nfs_inode_reclaim_delegation(state->inode,
1372 data->owner->so_cred,
1373 &data->o_res);
1374}
1375
1376/*
1377 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1378 * and update the nfs4_state.
1379 */
1380static struct nfs4_state *
1381_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1382{
1383 struct inode *inode = data->state->inode;
1384 struct nfs4_state *state = data->state;
1385 int ret;
1386
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001387 if (!data->rpc_done) {
1388 if (data->rpc_status) {
1389 ret = data->rpc_status;
1390 goto err;
1391 }
1392 /* cached opens have already been processed */
1393 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001394 }
1395
Andy Adamsone23008e2012-10-02 21:07:32 -04001396 ret = nfs_refresh_inode(inode, &data->f_attr);
1397 if (ret)
1398 goto err;
1399
1400 if (data->o_res.delegation_type != 0)
1401 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001402update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001403 update_open_stateid(state, &data->o_res.stateid, NULL,
1404 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001405 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001406
1407 return state;
1408err:
1409 return ERR_PTR(ret);
1410
1411}
1412
1413static struct nfs4_state *
1414_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001415{
1416 struct inode *inode;
1417 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001418 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419
Trond Myklebustaac00a82007-07-05 19:02:21 -04001420 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001421 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001422 goto out;
1423 }
1424
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001425 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001427 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001428 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001429 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001430 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001431 goto err;
1432 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433 state = nfs4_get_open_state(inode, data->owner);
1434 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001435 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001436 if (data->o_res.delegation_type != 0)
1437 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001438 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001439 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001440 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001441out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001442 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001443 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001444err_put_inode:
1445 iput(inode);
1446err:
1447 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448}
1449
Andy Adamsone23008e2012-10-02 21:07:32 -04001450static struct nfs4_state *
1451nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1452{
1453 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1454 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1455 return _nfs4_opendata_to_nfs4_state(data);
1456}
1457
Trond Myklebust864472e2006-01-03 09:55:15 +01001458static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1459{
1460 struct nfs_inode *nfsi = NFS_I(state->inode);
1461 struct nfs_open_context *ctx;
1462
1463 spin_lock(&state->inode->i_lock);
1464 list_for_each_entry(ctx, &nfsi->open_files, list) {
1465 if (ctx->state != state)
1466 continue;
1467 get_nfs_open_context(ctx);
1468 spin_unlock(&state->inode->i_lock);
1469 return ctx;
1470 }
1471 spin_unlock(&state->inode->i_lock);
1472 return ERR_PTR(-ENOENT);
1473}
1474
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001475static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1476 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001477{
1478 struct nfs4_opendata *opendata;
1479
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001480 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001481 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001482 if (opendata == NULL)
1483 return ERR_PTR(-ENOMEM);
1484 opendata->state = state;
1485 atomic_inc(&state->count);
1486 return opendata;
1487}
1488
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001489static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001490{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001491 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001492 int ret;
1493
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001494 opendata->o_arg.open_flags = 0;
1495 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001496 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1497 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1498 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001499 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001500 if (ret != 0)
1501 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001502 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001503 if (IS_ERR(newstate))
1504 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001505 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001506 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001507 return 0;
1508}
1509
1510static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1511{
Trond Myklebust864472e2006-01-03 09:55:15 +01001512 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001513 int ret;
1514
Trond Myklebust4f14c192014-02-12 19:15:06 -05001515 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1516 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1517 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1518 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001519 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001520 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001521 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001522 smp_rmb();
1523 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001524 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001525 if (ret != 0)
1526 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001527 if (newstate != state)
1528 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001529 }
1530 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001531 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001532 if (ret != 0)
1533 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001534 if (newstate != state)
1535 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001536 }
1537 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001538 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001539 if (ret != 0)
1540 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001541 if (newstate != state)
1542 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001543 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001544 /*
1545 * We may have performed cached opens for all three recoveries.
1546 * Check if we need to update the current stateid.
1547 */
1548 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001549 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001550 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001551 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001552 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001553 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001554 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001555 return 0;
1556}
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558/*
1559 * OPEN_RECLAIM:
1560 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001562static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001564 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001565 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001566 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 int status;
1568
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001569 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1570 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001571 if (IS_ERR(opendata))
1572 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001573 rcu_read_lock();
1574 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001575 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001576 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001577 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001578 opendata->o_arg.u.delegation_type = delegation_type;
1579 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001580 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return status;
1582}
1583
Trond Myklebust539cd032007-06-05 11:46:42 -04001584static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 struct nfs_server *server = NFS_SERVER(state->inode);
1587 struct nfs4_exception exception = { };
1588 int err;
1589 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001590 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001591 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001592 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1593 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001594 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001595 break;
1596 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 } while (exception.retry);
1598 return err;
1599}
1600
Trond Myklebust864472e2006-01-03 09:55:15 +01001601static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1602{
1603 struct nfs_open_context *ctx;
1604 int ret;
1605
1606 ctx = nfs4_state_find_open_context(state);
1607 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001608 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001609 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001610 put_nfs_open_context(ctx);
1611 return ret;
1612}
1613
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001614static 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 -07001615{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001616 switch (err) {
1617 default:
1618 printk(KERN_ERR "NFS: %s: unhandled error "
1619 "%d.\n", __func__, err);
1620 case 0:
1621 case -ENOENT:
1622 case -ESTALE:
1623 break;
1624 case -NFS4ERR_BADSESSION:
1625 case -NFS4ERR_BADSLOT:
1626 case -NFS4ERR_BAD_HIGH_SLOT:
1627 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1628 case -NFS4ERR_DEADSESSION:
1629 set_bit(NFS_DELEGATED_STATE, &state->flags);
1630 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1631 return -EAGAIN;
1632 case -NFS4ERR_STALE_CLIENTID:
1633 case -NFS4ERR_STALE_STATEID:
1634 set_bit(NFS_DELEGATED_STATE, &state->flags);
1635 case -NFS4ERR_EXPIRED:
1636 /* Don't recall a delegation if it was lost */
1637 nfs4_schedule_lease_recovery(server->nfs_client);
1638 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001639 case -NFS4ERR_MOVED:
1640 nfs4_schedule_migration_recovery(server);
1641 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001642 case -NFS4ERR_LEASE_MOVED:
1643 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1644 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001645 case -NFS4ERR_DELEG_REVOKED:
1646 case -NFS4ERR_ADMIN_REVOKED:
1647 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001648 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001649 nfs_inode_find_state_and_recover(state->inode,
1650 stateid);
1651 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001652 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001653 case -NFS4ERR_DELAY:
1654 case -NFS4ERR_GRACE:
1655 set_bit(NFS_DELEGATED_STATE, &state->flags);
1656 ssleep(1);
1657 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001658 case -ENOMEM:
1659 case -NFS4ERR_DENIED:
1660 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1661 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return err;
1664}
1665
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001666int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1667{
1668 struct nfs_server *server = NFS_SERVER(state->inode);
1669 struct nfs4_opendata *opendata;
1670 int err;
1671
1672 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1673 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1674 if (IS_ERR(opendata))
1675 return PTR_ERR(opendata);
1676 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1677 err = nfs4_open_recover(opendata, state);
1678 nfs4_opendata_put(opendata);
1679 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1680}
1681
Chuck Leverbe05c862013-08-09 12:49:47 -04001682static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1683{
1684 struct nfs4_opendata *data = calldata;
1685
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001686 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1687 &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001688}
1689
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001690static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1691{
1692 struct nfs4_opendata *data = calldata;
1693
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001694 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001695
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001696 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001697 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001698 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001699 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001700 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001701 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001702 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001703}
1704
1705static void nfs4_open_confirm_release(void *calldata)
1706{
1707 struct nfs4_opendata *data = calldata;
1708 struct nfs4_state *state = NULL;
1709
1710 /* If this request hasn't been cancelled, do nothing */
1711 if (data->cancelled == 0)
1712 goto out_free;
1713 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001714 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001715 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001716 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001717 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001718 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001719out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001720 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001721}
1722
1723static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001724 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001725 .rpc_call_done = nfs4_open_confirm_done,
1726 .rpc_release = nfs4_open_confirm_release,
1727};
1728
1729/*
1730 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1731 */
1732static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1733{
1734 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1735 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001736 struct rpc_message msg = {
1737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1738 .rpc_argp = &data->c_arg,
1739 .rpc_resp = &data->c_res,
1740 .rpc_cred = data->owner->so_cred,
1741 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001742 struct rpc_task_setup task_setup_data = {
1743 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001744 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001745 .callback_ops = &nfs4_open_confirm_ops,
1746 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001747 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001748 .flags = RPC_TASK_ASYNC,
1749 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 int status;
1751
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001752 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001753 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001754 data->rpc_done = 0;
1755 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001756 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001757 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001758 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001759 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001760 status = nfs4_wait_for_completion_rpc_task(task);
1761 if (status != 0) {
1762 data->cancelled = 1;
1763 smp_wmb();
1764 } else
1765 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001766 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return status;
1768}
1769
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001770static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 struct nfs4_opendata *data = calldata;
1773 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001774 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001775
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001776 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001777 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001778 /*
1779 * Check if we still need to send an OPEN call, or if we can use
1780 * a delegation instead.
1781 */
1782 if (data->state != NULL) {
1783 struct nfs_delegation *delegation;
1784
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001785 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001786 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001787 rcu_read_lock();
1788 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001789 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001790 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001791 can_open_delegated(delegation, data->o_arg.fmode))
1792 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001793 rcu_read_unlock();
1794 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001795 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001796 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001797 switch (data->o_arg.claim) {
1798 case NFS4_OPEN_CLAIM_PREVIOUS:
1799 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1800 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001801 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001802 case NFS4_OPEN_CLAIM_FH:
1803 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001804 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1805 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001806 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001807 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001808 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001809 &data->o_res.seq_res,
1810 task) != 0)
1811 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001812
1813 /* Set the create mode (note dependency on the session type) */
1814 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1815 if (data->o_arg.open_flags & O_EXCL) {
1816 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1817 if (nfs4_has_persistent_session(clp))
1818 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1819 else if (clp->cl_mvops->minor_version > 0)
1820 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1821 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001822 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001823unlock_no_action:
1824 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001825out_no_action:
1826 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001827out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001828 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001829}
1830
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001831static void nfs4_open_done(struct rpc_task *task, void *calldata)
1832{
1833 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001835 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001836
Trond Myklebust14516c32010-07-31 14:29:06 -04001837 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1838 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001839
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001840 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001841 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1842 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001843 case S_IFREG:
1844 break;
1845 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001846 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001847 break;
1848 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001849 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001850 break;
1851 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001852 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001853 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001854 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001855 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001856 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1857 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001858 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001859 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001860}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001861
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001862static void nfs4_open_release(void *calldata)
1863{
1864 struct nfs4_opendata *data = calldata;
1865 struct nfs4_state *state = NULL;
1866
1867 /* If this request hasn't been cancelled, do nothing */
1868 if (data->cancelled == 0)
1869 goto out_free;
1870 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001871 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001872 goto out_free;
1873 /* In case we need an open_confirm, no cleanup! */
1874 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1875 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001876 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001877 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001878 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001879out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001880 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001881}
1882
1883static const struct rpc_call_ops nfs4_open_ops = {
1884 .rpc_call_prepare = nfs4_open_prepare,
1885 .rpc_call_done = nfs4_open_done,
1886 .rpc_release = nfs4_open_release,
1887};
1888
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001889static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001890{
1891 struct inode *dir = data->dir->d_inode;
1892 struct nfs_server *server = NFS_SERVER(dir);
1893 struct nfs_openargs *o_arg = &data->o_arg;
1894 struct nfs_openres *o_res = &data->o_res;
1895 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001896 struct rpc_message msg = {
1897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1898 .rpc_argp = o_arg,
1899 .rpc_resp = o_res,
1900 .rpc_cred = data->owner->so_cred,
1901 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001902 struct rpc_task_setup task_setup_data = {
1903 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001904 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001905 .callback_ops = &nfs4_open_ops,
1906 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001907 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001908 .flags = RPC_TASK_ASYNC,
1909 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001910 int status;
1911
Chuck Levera9c92d62013-08-09 12:48:18 -04001912 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001913 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001914 data->rpc_done = 0;
1915 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001916 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001917 data->is_recover = 0;
1918 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001919 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001920 data->is_recover = 1;
1921 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001922 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001923 if (IS_ERR(task))
1924 return PTR_ERR(task);
1925 status = nfs4_wait_for_completion_rpc_task(task);
1926 if (status != 0) {
1927 data->cancelled = 1;
1928 smp_wmb();
1929 } else
1930 status = data->rpc_status;
1931 rpc_put_task(task);
1932
1933 return status;
1934}
1935
1936static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1937{
1938 struct inode *dir = data->dir->d_inode;
1939 struct nfs_openres *o_res = &data->o_res;
1940 int status;
1941
1942 status = nfs4_run_open_task(data, 1);
1943 if (status != 0 || !data->rpc_done)
1944 return status;
1945
Trond Myklebust6926afd2012-01-07 13:22:46 -05001946 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1947
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001948 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1949 status = _nfs4_proc_open_confirm(data);
1950 if (status != 0)
1951 return status;
1952 }
1953
1954 return status;
1955}
1956
Trond Myklebustf3792d62014-07-10 08:54:32 -04001957/*
1958 * Additional permission checks in order to distinguish between an
1959 * open for read, and an open for execute. This works around the
1960 * fact that NFSv4 OPEN treats read and execute permissions as being
1961 * the same.
1962 * Note that in the non-execute case, we want to turn off permission
1963 * checking if we just created a new file (POSIX open() semantics).
1964 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001965static int nfs4_opendata_access(struct rpc_cred *cred,
1966 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001967 struct nfs4_state *state, fmode_t fmode,
1968 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001969{
1970 struct nfs_access_entry cache;
1971 u32 mask;
1972
1973 /* access call failed or for some reason the server doesn't
1974 * support any access modes -- defer access call until later */
1975 if (opendata->o_res.access_supported == 0)
1976 return 0;
1977
1978 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04001979 /*
1980 * Use openflags to check for exec, because fmode won't
1981 * always have FMODE_EXEC set when file open for exec.
1982 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001983 if (openflags & __FMODE_EXEC) {
1984 /* ONLY check for exec rights */
1985 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04001986 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001987 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001988
1989 cache.cred = cred;
1990 cache.jiffies = jiffies;
1991 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1992 nfs_access_add_cache(state->inode, &cache);
1993
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001994 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001995 return 0;
1996
1997 /* even though OPEN succeeded, access is denied. Close the file */
1998 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001999 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002000}
2001
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002002/*
2003 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2004 */
2005static int _nfs4_proc_open(struct nfs4_opendata *data)
2006{
2007 struct inode *dir = data->dir->d_inode;
2008 struct nfs_server *server = NFS_SERVER(dir);
2009 struct nfs_openargs *o_arg = &data->o_arg;
2010 struct nfs_openres *o_res = &data->o_res;
2011 int status;
2012
2013 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002014 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002015 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002016 if (status != 0) {
2017 if (status == -NFS4ERR_BADNAME &&
2018 !(o_arg->open_flags & O_CREAT))
2019 return -ENOENT;
2020 return status;
2021 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002022
Trond Myklebust6926afd2012-01-07 13:22:46 -05002023 nfs_fattr_map_and_free_names(server, &data->f_attr);
2024
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002025 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002026 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002027 if (o_arg->open_flags & O_EXCL)
2028 data->file_created = 1;
2029 else if (o_res->cinfo.before != o_res->cinfo.after)
2030 data->file_created = 1;
2031 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002032 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2033 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002035 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002037 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
2039 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002040 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Andy Adamsond83217c2011-03-01 01:34:17 +00002044static int nfs4_recover_expired_lease(struct nfs_server *server)
2045{
2046 return nfs4_client_recover_expired_lease(server->nfs_client);
2047}
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049/*
2050 * OPEN_EXPIRED:
2051 * reclaim state on the server after a network partition.
2052 * Assumes caller holds the appropriate lock
2053 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002054static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002056 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002057 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002059 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002060 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002061 if (IS_ERR(opendata))
2062 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002063 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002064 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002065 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002066 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002067 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002070static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002071{
Trond Myklebust539cd032007-06-05 11:46:42 -04002072 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002073 struct nfs4_exception exception = { };
2074 int err;
2075
2076 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002077 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002078 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002079 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2080 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002081 switch (err) {
2082 default:
2083 goto out;
2084 case -NFS4ERR_GRACE:
2085 case -NFS4ERR_DELAY:
2086 nfs4_handle_exception(server, err, &exception);
2087 err = 0;
2088 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002089 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002090out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002091 return err;
2092}
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002097 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Trond Myklebust864472e2006-01-03 09:55:15 +01002099 ctx = nfs4_state_find_open_context(state);
2100 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002101 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002102 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002103 put_nfs_open_context(ctx);
2104 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002107static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2108{
2109 nfs_remove_bad_delegation(state->inode);
2110 write_seqlock(&state->seqlock);
2111 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2112 write_sequnlock(&state->seqlock);
2113 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2114}
2115
2116static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2117{
2118 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2119 nfs_finish_clear_delegation_stateid(state);
2120}
2121
2122static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2123{
2124 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2125 nfs40_clear_delegation_stateid(state);
2126 return nfs4_open_expired(sp, state);
2127}
2128
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002129#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002130static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002131{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002132 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002133 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002134 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002135 struct rpc_cred *cred;
2136 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002137
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002138 /* Get the delegation credential for use by test/free_stateid */
2139 rcu_read_lock();
2140 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002141 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002142 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002143 return;
2144 }
2145
2146 nfs4_stateid_copy(&stateid, &delegation->stateid);
2147 cred = get_rpccred(delegation->cred);
2148 rcu_read_unlock();
2149 status = nfs41_test_stateid(server, &stateid, cred);
2150 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002151
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002152 if (status != NFS_OK) {
2153 /* Free the stateid unless the server explicitly
2154 * informs us the stateid is unrecognized. */
2155 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002156 nfs41_free_stateid(server, &stateid, cred);
2157 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002158 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002159
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002160 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002161}
2162
2163/**
2164 * nfs41_check_open_stateid - possibly free an open stateid
2165 *
2166 * @state: NFSv4 state for an inode
2167 *
2168 * Returns NFS_OK if recovery for this stateid is now finished.
2169 * Otherwise a negative NFS4ERR value is returned.
2170 */
2171static int nfs41_check_open_stateid(struct nfs4_state *state)
2172{
2173 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002174 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002175 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002176 int status;
2177
2178 /* If a state reset has been done, test_stateid is unneeded */
2179 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2180 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2181 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2182 return -NFS4ERR_BAD_STATEID;
2183
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002184 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002185 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002186 if (status != NFS_OK) {
2187 /* Free the stateid unless the server explicitly
2188 * informs us the stateid is unrecognized. */
2189 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002190 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002191
2192 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2193 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2194 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002195 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002196 }
2197 return status;
2198}
2199
2200static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2201{
Chuck Levereb64cf92012-07-11 16:30:05 -04002202 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002203
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002204 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002205 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002206 if (status != NFS_OK)
2207 status = nfs4_open_expired(sp, state);
2208 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002209}
2210#endif
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002213 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2214 * fields corresponding to attributes that were used to store the verifier.
2215 * Make sure we clobber those fields in the later setattr call
2216 */
2217static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2218{
2219 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2220 !(sattr->ia_valid & ATTR_ATIME_SET))
2221 sattr->ia_valid |= ATTR_ATIME;
2222
2223 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2224 !(sattr->ia_valid & ATTR_MTIME_SET))
2225 sattr->ia_valid |= ATTR_MTIME;
2226}
2227
Trond Myklebustc21443c2013-02-07 14:26:21 -05002228static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2229 fmode_t fmode,
2230 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002231 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002232{
2233 struct nfs4_state_owner *sp = opendata->owner;
2234 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002235 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002236 struct nfs4_state *state;
2237 unsigned int seq;
2238 int ret;
2239
2240 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2241
2242 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002243 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002244 goto out;
2245
2246 state = nfs4_opendata_to_nfs4_state(opendata);
2247 ret = PTR_ERR(state);
2248 if (IS_ERR(state))
2249 goto out;
2250 if (server->caps & NFS_CAP_POSIX_LOCK)
2251 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2252
Trond Myklebust275bb302013-05-29 13:11:28 -04002253 dentry = opendata->dentry;
2254 if (dentry->d_inode == NULL) {
2255 /* FIXME: Is this d_drop() ever needed? */
2256 d_drop(dentry);
2257 dentry = d_add_unique(dentry, igrab(state->inode));
2258 if (dentry == NULL) {
2259 dentry = opendata->dentry;
2260 } else if (dentry != ctx->dentry) {
2261 dput(ctx->dentry);
2262 ctx->dentry = dget(dentry);
2263 }
2264 nfs_set_verifier(dentry,
2265 nfs_save_change_attribute(opendata->dir->d_inode));
2266 }
2267
Trond Myklebustc21443c2013-02-07 14:26:21 -05002268 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2269 if (ret != 0)
2270 goto out;
2271
Trond Myklebust3efb9722013-05-29 13:17:04 -04002272 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002273 if (dentry->d_inode == state->inode) {
2274 nfs_inode_attach_open_context(ctx);
2275 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2276 nfs4_schedule_stateid_recovery(server, state);
2277 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002278out:
2279 return ret;
2280}
2281
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002282/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002283 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 */
Andy Adamson82be4172012-05-23 05:02:35 -04002285static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002286 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002287 int flags,
2288 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002289 struct nfs4_label *label,
2290 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 struct nfs4_state_owner *sp;
2293 struct nfs4_state *state = NULL;
2294 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002295 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002296 struct dentry *dentry = ctx->dentry;
2297 struct rpc_cred *cred = ctx->cred;
2298 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2299 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002300 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002301 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002302 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002306 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2307 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2309 goto out_err;
2310 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002311 status = nfs4_recover_expired_lease(server);
2312 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002313 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002314 if (dentry->d_inode != NULL)
2315 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002316 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002317 if (dentry->d_inode)
2318 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002319 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002320 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002321 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002322 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
David Quigley14c43f72013-05-22 12:50:43 -04002324 if (label) {
2325 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2326 if (IS_ERR(olabel)) {
2327 status = PTR_ERR(olabel);
2328 goto err_opendata_put;
2329 }
2330 }
2331
Trond Myklebuste911b812014-03-26 13:24:37 -07002332 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2333 if (!opendata->f_attr.mdsthreshold) {
2334 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2335 if (!opendata->f_attr.mdsthreshold)
2336 goto err_free_label;
2337 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002338 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002339 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002340 if (dentry->d_inode != NULL)
2341 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002342
Trond Myklebust3efb9722013-05-29 13:17:04 -04002343 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002344 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002345 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002346 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002347
Trond Myklebust549b19c2013-04-16 18:42:34 -04002348 if ((opendata->o_arg.open_flags & O_EXCL) &&
2349 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002350 nfs4_exclusive_attrset(opendata, sattr);
2351
2352 nfs_fattr_init(opendata->o_res.f_attr);
2353 status = nfs4_do_setattr(state->inode, cred,
2354 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002355 state, label, olabel);
2356 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002357 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002358 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002359 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002360 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002361 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002362 if (opendata->file_created)
2363 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002364
Trond Myklebuste911b812014-03-26 13:24:37 -07002365 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002366 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002367 opendata->f_attr.mdsthreshold = NULL;
2368 }
Andy Adamson82be4172012-05-23 05:02:35 -04002369
David Quigley14c43f72013-05-22 12:50:43 -04002370 nfs4_label_free(olabel);
2371
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002372 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002375err_free_label:
2376 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002377err_opendata_put:
2378 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002379err_put_state_owner:
2380 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return status;
2383}
2384
2385
Andy Adamson82be4172012-05-23 05:02:35 -04002386static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002387 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002388 int flags,
2389 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002390 struct nfs4_label *label,
2391 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002393 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct nfs4_exception exception = { };
2395 struct nfs4_state *res;
2396 int status;
2397
2398 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002399 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002400 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002401 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (status == 0)
2403 break;
2404 /* NOTE: BAD_SEQID means the server and client disagree about the
2405 * book-keeping w.r.t. state-changing operations
2406 * (OPEN/CLOSE/LOCK/LOCKU...)
2407 * It is actually a sign of a bug on the client or on the server.
2408 *
2409 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002410 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 * have unhashed the old state_owner for us, and that we can
2412 * therefore safely retry using a new one. We should still warn
2413 * the user though...
2414 */
2415 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002416 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002417 " returned a bad sequence-id error!\n",
2418 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 exception.retry = 1;
2420 continue;
2421 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002422 /*
2423 * BAD_STATEID on OPEN means that the server cancelled our
2424 * state before it received the OPEN_CONFIRM.
2425 * Recover by retrying the request as per the discussion
2426 * on Page 181 of RFC3530.
2427 */
2428 if (status == -NFS4ERR_BAD_STATEID) {
2429 exception.retry = 1;
2430 continue;
2431 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002432 if (status == -EAGAIN) {
2433 /* We must have found a delegation */
2434 exception.retry = 1;
2435 continue;
2436 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002437 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2438 continue;
2439 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 status, &exception));
2441 } while (exception.retry);
2442 return res;
2443}
2444
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002445static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2446 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002447 struct nfs4_state *state, struct nfs4_label *ilabel,
2448 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002450 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002452 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 .iap = sattr,
2454 .server = server,
2455 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002456 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 };
2458 struct nfs_setattrres res = {
2459 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002460 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 .server = server,
2462 };
2463 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2465 .rpc_argp = &arg,
2466 .rpc_resp = &res,
2467 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002469 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002470 fmode_t fmode;
2471 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002472 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
David Quigleyaa9c2662013-05-22 12:50:44 -04002474 arg.bitmask = nfs4_bitmask(server, ilabel);
2475 if (ilabel)
2476 arg.bitmask = nfs4_bitmask(server, olabel);
2477
Trond Myklebust0e574af2005-10-27 22:12:38 -04002478 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Trond Myklebustee3ae842013-04-29 10:35:36 -04002480 /* Servers should only apply open mode checks for file size changes */
2481 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2482 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2483
2484 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2485 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002486 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002487 struct nfs_lockowner lockowner = {
2488 .l_owner = current->files,
2489 .l_pid = current->tgid,
2490 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002491 if (!nfs4_valid_open_stateid(state))
2492 return -EBADF;
2493 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2494 &lockowner) == -EIO)
2495 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002496 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002497 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Bryan Schumaker7c513052011-03-24 17:12:24 +00002499 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002500 if (status == 0 && state != NULL)
2501 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002502 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002505static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2506 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002507 struct nfs4_state *state, struct nfs4_label *ilabel,
2508 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002510 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002511 struct nfs4_exception exception = {
2512 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002513 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002514 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 int err;
2516 do {
David Quigley1775fd32013-05-22 12:50:42 -04002517 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002518 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002519 switch (err) {
2520 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002521 if (!(sattr->ia_valid & ATTR_SIZE)) {
2522 pr_warn_once("NFSv4: server %s is incorrectly "
2523 "applying open mode checks to "
2524 "a SETATTR that is not "
2525 "changing file size.\n",
2526 server->nfs_client->cl_hostname);
2527 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002528 if (state && !(state->state & FMODE_WRITE)) {
2529 err = -EBADF;
2530 if (sattr->ia_valid & ATTR_OPEN)
2531 err = -EACCES;
2532 goto out;
2533 }
2534 }
2535 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002537out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return err;
2539}
2540
2541struct nfs4_closedata {
2542 struct inode *inode;
2543 struct nfs4_state *state;
2544 struct nfs_closeargs arg;
2545 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002546 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002547 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002548 bool roc;
2549 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550};
2551
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002552static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002553{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002554 struct nfs4_closedata *calldata = data;
2555 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002556 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002557
Fred Isamanf7e89172011-01-06 11:36:32 +00002558 if (calldata->roc)
2559 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002560 nfs4_put_open_state(calldata->state);
2561 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002562 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002563 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002564 kfree(calldata);
2565}
2566
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002567static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002569 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002572 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Chuck Levera3ca5652012-03-01 17:00:40 -05002574 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002575 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2576 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002577 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /* hmm. we are done with the inode, and in the process of freeing
2579 * the state_owner. we keep this around to process errors
2580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 switch (task->tk_status) {
2582 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002583 res_stateid = &calldata->res.stateid;
2584 if (calldata->arg.fmode == 0 && calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002585 pnfs_roc_set_barrier(state->inode,
2586 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002587 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002588 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002589 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002591 case -NFS4ERR_OLD_STATEID:
2592 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002594 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002595 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002597 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002598 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002599 goto out_release;
2600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
Trond Myklebust412f6c42014-08-25 22:09:08 -04002602 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002603out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002604 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002605 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002606 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002609static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002611 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002612 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002613 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002614 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002615 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002616
Chuck Levera3ca5652012-03-01 17:00:40 -05002617 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002618 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002619 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002620
Trond Myklebust88069f72009-12-08 08:33:16 -05002621 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002622 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002623 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2624 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2625 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04002626 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002627 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002628 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002629 if (state->n_rdonly == 0)
2630 call_close |= is_rdonly;
2631 else if (is_rdonly)
2632 calldata->arg.fmode |= FMODE_READ;
2633 if (state->n_wronly == 0)
2634 call_close |= is_wronly;
2635 else if (is_wronly)
2636 calldata->arg.fmode |= FMODE_WRITE;
2637 } else if (is_rdwr)
2638 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2639
2640 if (calldata->arg.fmode == 0)
2641 call_close |= is_rdwr;
2642
Trond Myklebust5d422302013-03-14 16:57:48 -04002643 if (!nfs4_valid_open_stateid(state))
2644 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002645 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002646
2647 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002648 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002649 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002650 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002651
Fred Isamanf7e89172011-01-06 11:36:32 +00002652 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002653 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002654 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002655 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2656 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002657 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002658 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002659 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002660
Trond Myklebust516a6af2005-10-27 22:12:41 -04002661 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002662 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002663 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002664 &calldata->arg.seq_args,
2665 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002666 task) != 0)
2667 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002668 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002669 return;
2670out_no_action:
2671 task->tk_action = NULL;
2672out_wait:
2673 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002676static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002677 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002678 .rpc_call_done = nfs4_close_done,
2679 .rpc_release = nfs4_free_closedata,
2680};
2681
Peng Taofe08c542014-07-03 13:05:01 +08002682static bool nfs4_state_has_opener(struct nfs4_state *state)
2683{
2684 /* first check existing openers */
2685 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 &&
2686 state->n_rdonly != 0)
2687 return true;
2688
2689 if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 &&
2690 state->n_wronly != 0)
2691 return true;
2692
2693 if (test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 &&
2694 state->n_rdwr != 0)
2695 return true;
2696
2697 return false;
2698}
2699
2700static bool nfs4_roc(struct inode *inode)
2701{
2702 struct nfs_inode *nfsi = NFS_I(inode);
2703 struct nfs_open_context *ctx;
2704 struct nfs4_state *state;
2705
2706 spin_lock(&inode->i_lock);
2707 list_for_each_entry(ctx, &nfsi->open_files, list) {
2708 state = ctx->state;
2709 if (state == NULL)
2710 continue;
2711 if (nfs4_state_has_opener(state)) {
2712 spin_unlock(&inode->i_lock);
2713 return false;
2714 }
2715 }
2716 spin_unlock(&inode->i_lock);
2717
2718 if (nfs4_check_delegation(inode, FMODE_READ))
2719 return false;
2720
2721 return pnfs_roc(inode);
2722}
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724/*
2725 * It is possible for data to be read/written from a mem-mapped file
2726 * after the sys_close call (which hits the vfs layer as a flush).
2727 * This means that we can't safely call nfsv4 close on a file until
2728 * the inode is cleared. This in turn means that we are not good
2729 * NFSv4 citizens - we do not indicate to the server to update the file's
2730 * share state even when we are done with one of the three share
2731 * stateid's in the inode.
2732 *
2733 * NOTE: Caller must be holding the sp->so_owner semaphore!
2734 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002735int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002737 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002739 struct nfs4_state_owner *sp = state->owner;
2740 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002741 struct rpc_message msg = {
2742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2743 .rpc_cred = state->owner->so_cred,
2744 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002745 struct rpc_task_setup task_setup_data = {
2746 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002747 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002748 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002749 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002750 .flags = RPC_TASK_ASYNC,
2751 };
Trond Myklebust95121352005-10-18 14:20:12 -07002752 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002754 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2755 &task_setup_data.rpc_client, &msg);
2756
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002757 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002759 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002760 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002761 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002763 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002764 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002766 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002767 if (calldata->arg.seqid == NULL)
2768 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002769 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002770 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002771 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002772 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002773 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002774 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002775 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002776
Trond Myklebust1174dd12010-12-21 10:52:24 -05002777 msg.rpc_argp = &calldata->arg;
2778 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002779 task_setup_data.callback_data = calldata;
2780 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002781 if (IS_ERR(task))
2782 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002783 status = 0;
2784 if (wait)
2785 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002786 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002787 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002788out_free_calldata:
2789 kfree(calldata);
2790out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002791 nfs4_put_open_state(state);
2792 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002793 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
Trond Myklebust2b484292010-09-17 10:56:51 -04002796static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002797nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2798 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002801 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2802
2803 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Trond Myklebust565277f2007-10-15 18:17:53 -04002805 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002806 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002807
2808 nfs4_label_release_security(label);
2809
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002810 if (IS_ERR(state))
2811 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002812 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813}
2814
Trond Myklebust1185a552009-12-03 15:54:02 -05002815static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002816{
2817 if (ctx->state == NULL)
2818 return;
2819 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002820 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002821 else
Al Viro643168c2011-06-22 18:20:23 -04002822 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002823}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Trond Myklebustb944dba2013-11-04 15:20:20 -05002825#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2826#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002827#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2830{
Benny Halevy43652ad2009-04-01 09:21:54 -04002831 struct nfs4_server_caps_arg args = {
2832 .fhandle = fhandle,
2833 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 struct nfs4_server_caps_res res = {};
2835 struct rpc_message msg = {
2836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002837 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 .rpc_resp = &res,
2839 };
2840 int status;
2841
Bryan Schumaker7c513052011-03-24 17:12:24 +00002842 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002844 /* Sanity check the server answers */
2845 switch (server->nfs_client->cl_minorversion) {
2846 case 0:
2847 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2848 res.attr_bitmask[2] = 0;
2849 break;
2850 case 1:
2851 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2852 break;
2853 case 2:
2854 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002857 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2858 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2859 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2860 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002861 NFS_CAP_CTIME|NFS_CAP_MTIME|
2862 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002863 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2864 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 server->caps |= NFS_CAP_ACLS;
2866 if (res.has_links != 0)
2867 server->caps |= NFS_CAP_HARDLINKS;
2868 if (res.has_symlinks != 0)
2869 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002870 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2871 server->caps |= NFS_CAP_FILEID;
2872 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2873 server->caps |= NFS_CAP_MODE;
2874 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2875 server->caps |= NFS_CAP_NLINK;
2876 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2877 server->caps |= NFS_CAP_OWNER;
2878 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2879 server->caps |= NFS_CAP_OWNER_GROUP;
2880 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2881 server->caps |= NFS_CAP_ATIME;
2882 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2883 server->caps |= NFS_CAP_CTIME;
2884 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2885 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002886#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2887 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2888 server->caps |= NFS_CAP_SECURITY_LABEL;
2889#endif
2890 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2891 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002892 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002893
Trond Myklebusta65318b2009-03-11 14:10:28 -04002894 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2895 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2896 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002897 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002899 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return status;
2903}
2904
Trond Myklebust55a97592006-06-09 09:34:19 -04002905int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 struct nfs4_exception exception = { };
2908 int err;
2909 do {
2910 err = nfs4_handle_exception(server,
2911 _nfs4_server_capabilities(server, fhandle),
2912 &exception);
2913 } while (exception.retry);
2914 return err;
2915}
2916
2917static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2918 struct nfs_fsinfo *info)
2919{
David Quigleyaa9c2662013-05-22 12:50:44 -04002920 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002922 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 };
2924 struct nfs4_lookup_res res = {
2925 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002926 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 .fh = fhandle,
2928 };
2929 struct rpc_message msg = {
2930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2931 .rpc_argp = &args,
2932 .rpc_resp = &res,
2933 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002934
David Quigleyaa9c2662013-05-22 12:50:44 -04002935 bitmask[0] = nfs4_fattr_bitmap[0];
2936 bitmask[1] = nfs4_fattr_bitmap[1];
2937 /*
2938 * Process the label in the upcoming getfattr
2939 */
2940 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2941
Trond Myklebust0e574af2005-10-27 22:12:38 -04002942 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002943 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
2946static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2947 struct nfs_fsinfo *info)
2948{
2949 struct nfs4_exception exception = { };
2950 int err;
2951 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002952 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002953 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002954 switch (err) {
2955 case 0:
2956 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002957 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002958 default:
2959 err = nfs4_handle_exception(server, err, &exception);
2960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002962out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return err;
2964}
2965
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002966static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2967 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2968{
Trond Myklebustc2190662013-08-26 19:23:04 -04002969 struct rpc_auth_create_args auth_args = {
2970 .pseudoflavor = flavor,
2971 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002972 struct rpc_auth *auth;
2973 int ret;
2974
Trond Myklebustc2190662013-08-26 19:23:04 -04002975 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002976 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002977 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002978 goto out;
2979 }
2980 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002981out:
2982 return ret;
2983}
2984
Chuck Lever9a744ba2013-03-16 15:56:02 -04002985/*
2986 * Retry pseudoroot lookup with various security flavors. We do this when:
2987 *
2988 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2989 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2990 *
2991 * Returns zero on success, or a negative NFS4ERR value, or a
2992 * negative errno value.
2993 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002994static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002995 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002997 /* Per 3530bis 15.33.5 */
2998 static const rpc_authflavor_t flav_array[] = {
2999 RPC_AUTH_GSS_KRB5P,
3000 RPC_AUTH_GSS_KRB5I,
3001 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003002 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003003 RPC_AUTH_NULL,
3004 };
3005 int status = -EPERM;
3006 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003008 if (server->auth_info.flavor_len > 0) {
3009 /* try each flavor specified by user */
3010 for (i = 0; i < server->auth_info.flavor_len; i++) {
3011 status = nfs4_lookup_root_sec(server, fhandle, info,
3012 server->auth_info.flavors[i]);
3013 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3014 continue;
3015 break;
3016 }
3017 } else {
3018 /* no flavors specified by user, try default list */
3019 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3020 status = nfs4_lookup_root_sec(server, fhandle, info,
3021 flav_array[i]);
3022 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3023 continue;
3024 break;
3025 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003026 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003027
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003028 /*
3029 * -EACCESS could mean that the user doesn't have correct permissions
3030 * to access the mount. It could also mean that we tried to mount
3031 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3032 * existing mount programs don't handle -EACCES very well so it should
3033 * be mapped to -EPERM instead.
3034 */
3035 if (status == -EACCES)
3036 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003037 return status;
3038}
3039
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003040static int nfs4_do_find_root_sec(struct nfs_server *server,
3041 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3042{
3043 int mv = server->nfs_client->cl_minorversion;
3044 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3045}
3046
3047/**
3048 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3049 * @server: initialized nfs_server handle
3050 * @fhandle: we fill in the pseudo-fs root file handle
3051 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003052 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003053 *
3054 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003055 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003056int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003057 struct nfs_fsinfo *info,
3058 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003059{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003060 int status;
3061
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003062 switch (auth_probe) {
3063 case false:
3064 status = nfs4_lookup_root(server, fhandle, info);
3065 if (status != -NFS4ERR_WRONGSEC)
3066 break;
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003067 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003068 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003069 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (status == 0)
3072 status = nfs4_server_capabilities(server, fhandle);
3073 if (status == 0)
3074 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003075
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003076 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
Bryan Schumakerbae36242012-05-10 15:07:31 -04003079static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3080 struct nfs_fsinfo *info)
3081{
3082 int error;
3083 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003084 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003085
3086 error = nfs4_server_capabilities(server, mntfh);
3087 if (error < 0) {
3088 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3089 return error;
3090 }
3091
David Quigley14c43f72013-05-22 12:50:43 -04003092 label = nfs4_label_alloc(server, GFP_KERNEL);
3093 if (IS_ERR(label))
3094 return PTR_ERR(label);
3095
David Quigley1775fd32013-05-22 12:50:42 -04003096 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003097 if (error < 0) {
3098 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003099 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003100 }
3101
3102 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3103 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3104 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3105
David Quigley14c43f72013-05-22 12:50:43 -04003106err_free_label:
3107 nfs4_label_free(label);
3108
Bryan Schumakerbae36242012-05-10 15:07:31 -04003109 return error;
3110}
3111
Manoj Naik6b97fd32006-06-09 09:34:29 -04003112/*
3113 * Get locations and (maybe) other attributes of a referral.
3114 * Note that we'll actually follow the referral later when
3115 * we detect fsid mismatch in inode revalidation
3116 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003117static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3118 const struct qstr *name, struct nfs_fattr *fattr,
3119 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003120{
3121 int status = -ENOMEM;
3122 struct page *page = NULL;
3123 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003124
3125 page = alloc_page(GFP_KERNEL);
3126 if (page == NULL)
3127 goto out;
3128 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3129 if (locations == NULL)
3130 goto out;
3131
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003132 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003133 if (status != 0)
3134 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003135
3136 /*
3137 * If the fsid didn't change, this is a migration event, not a
3138 * referral. Cause us to drop into the exception handler, which
3139 * will kick off migration recovery.
3140 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003141 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003142 dprintk("%s: server did not return a different fsid for"
3143 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003144 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003145 goto out;
3146 }
Andy Adamson533eb462011-06-13 18:25:56 -04003147 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3148 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003149
Andy Adamson533eb462011-06-13 18:25:56 -04003150 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003151 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003152 memset(fhandle, 0, sizeof(struct nfs_fh));
3153out:
3154 if (page)
3155 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003156 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003157 return status;
3158}
3159
David Quigley1775fd32013-05-22 12:50:42 -04003160static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3161 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 struct nfs4_getattr_arg args = {
3164 .fh = fhandle,
3165 .bitmask = server->attr_bitmask,
3166 };
3167 struct nfs4_getattr_res res = {
3168 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003169 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 .server = server,
3171 };
3172 struct rpc_message msg = {
3173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3174 .rpc_argp = &args,
3175 .rpc_resp = &res,
3176 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003177
3178 args.bitmask = nfs4_bitmask(server, label);
3179
Trond Myklebust0e574af2005-10-27 22:12:38 -04003180 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003181 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
David Quigley1775fd32013-05-22 12:50:42 -04003184static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3185 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
3187 struct nfs4_exception exception = { };
3188 int err;
3189 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003190 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3191 trace_nfs4_getattr(server, fhandle, fattr, err);
3192 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 &exception);
3194 } while (exception.retry);
3195 return err;
3196}
3197
3198/*
3199 * The file is not closed if it is opened due to the a request to change
3200 * the size of the file. The open call will not be needed once the
3201 * VFS layer lookup-intents are implemented.
3202 *
3203 * Close is called when the inode is destroyed.
3204 * If we haven't opened the file for O_WRONLY, we
3205 * need to in the size_change case to obtain a stateid.
3206 *
3207 * Got race?
3208 * Because OPEN is always done by name in nfsv4, it is
3209 * possible that we opened a different file by the same
3210 * name. We can recognize this race condition, but we
3211 * can't do anything about it besides returning an error.
3212 *
3213 * This will be fixed with VFS changes (lookup-intent).
3214 */
3215static int
3216nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3217 struct iattr *sattr)
3218{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003219 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003220 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003221 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003222 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 int status;
3224
Peng Tao88ac8152014-09-12 11:04:10 +08003225 if (pnfs_ld_layoutret_on_setattr(inode) &&
3226 sattr->ia_valid & ATTR_SIZE &&
3227 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003228 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003229
Trond Myklebust0e574af2005-10-27 22:12:38 -04003230 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Andy Adamson26699402012-05-30 16:12:24 -04003232 /* Deal with open(O_TRUNC) */
3233 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003234 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003235
3236 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003237 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003238 return 0;
3239
Trond Myklebustd5308382005-11-04 15:33:38 -05003240 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003241 if (sattr->ia_valid & ATTR_FILE) {
3242 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003243
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003244 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003245 if (ctx) {
3246 cred = ctx->cred;
3247 state = ctx->state;
3248 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003249 }
3250
David Quigley14c43f72013-05-22 12:50:43 -04003251 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3252 if (IS_ERR(label))
3253 return PTR_ERR(label);
3254
3255 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003256 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003257 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003258 nfs_setsecurity(inode, fattr, label);
3259 }
David Quigley14c43f72013-05-22 12:50:43 -04003260 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return status;
3262}
3263
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003264static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3265 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003266 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003267{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003268 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003269 int status;
3270 struct nfs4_lookup_arg args = {
3271 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003272 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003273 .name = name,
3274 };
3275 struct nfs4_lookup_res res = {
3276 .server = server,
3277 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003278 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003279 .fh = fhandle,
3280 };
3281 struct rpc_message msg = {
3282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3283 .rpc_argp = &args,
3284 .rpc_resp = &res,
3285 };
3286
David Quigleyaa9c2662013-05-22 12:50:44 -04003287 args.bitmask = nfs4_bitmask(server, label);
3288
David Howells2b3de442006-08-22 20:06:09 -04003289 nfs_fattr_init(fattr);
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003292 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 dprintk("NFS reply lookup: %d\n", status);
3294 return status;
3295}
3296
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003297static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003298{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003299 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003300 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003301 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3302 fattr->nlink = 2;
3303}
3304
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003305static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3306 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003307 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003308{
3309 struct nfs4_exception exception = { };
3310 struct rpc_clnt *client = *clnt;
3311 int err;
3312 do {
David Quigley1775fd32013-05-22 12:50:42 -04003313 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003314 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003315 switch (err) {
3316 case -NFS4ERR_BADNAME:
3317 err = -ENOENT;
3318 goto out;
3319 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003320 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003321 goto out;
3322 case -NFS4ERR_WRONGSEC:
3323 err = -EPERM;
3324 if (client != *clnt)
3325 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003326 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003327 if (IS_ERR(client))
3328 return PTR_ERR(client);
3329
3330 exception.retry = 1;
3331 break;
3332 default:
3333 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3334 }
3335 } while (exception.retry);
3336
3337out:
3338 if (err == 0)
3339 *clnt = client;
3340 else if (client != *clnt)
3341 rpc_shutdown_client(client);
3342
3343 return err;
3344}
3345
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003346static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003347 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3348 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003350 int status;
3351 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003352
David Quigley1775fd32013-05-22 12:50:42 -04003353 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003354 if (client != NFS_CLIENT(dir)) {
3355 rpc_shutdown_client(client);
3356 nfs_fixup_secinfo_attributes(fattr);
3357 }
3358 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003361struct rpc_clnt *
3362nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3363 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3364{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003365 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003366 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003367
David Quigley1775fd32013-05-22 12:50:42 -04003368 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003369 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003370 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003371 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003372}
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3375{
Trond Myklebust76b32992007-08-10 17:45:11 -04003376 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 struct nfs4_accessargs args = {
3378 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003379 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003381 struct nfs4_accessres res = {
3382 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003383 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 struct rpc_message msg = {
3385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3386 .rpc_argp = &args,
3387 .rpc_resp = &res,
3388 .rpc_cred = entry->cred,
3389 };
3390 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003391 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393 /*
3394 * Determine which access bits we want to ask for...
3395 */
3396 if (mode & MAY_READ)
3397 args.access |= NFS4_ACCESS_READ;
3398 if (S_ISDIR(inode->i_mode)) {
3399 if (mode & MAY_WRITE)
3400 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3401 if (mode & MAY_EXEC)
3402 args.access |= NFS4_ACCESS_LOOKUP;
3403 } else {
3404 if (mode & MAY_WRITE)
3405 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3406 if (mode & MAY_EXEC)
3407 args.access |= NFS4_ACCESS_EXECUTE;
3408 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003409
3410 res.fattr = nfs_alloc_fattr();
3411 if (res.fattr == NULL)
3412 return -ENOMEM;
3413
Bryan Schumaker7c513052011-03-24 17:12:24 +00003414 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003416 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003417 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003419 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 return status;
3421}
3422
3423static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3424{
3425 struct nfs4_exception exception = { };
3426 int err;
3427 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003428 err = _nfs4_proc_access(inode, entry);
3429 trace_nfs4_access(inode, err);
3430 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 &exception);
3432 } while (exception.retry);
3433 return err;
3434}
3435
3436/*
3437 * TODO: For the time being, we don't try to get any attributes
3438 * along with any of the zero-copy operations READ, READDIR,
3439 * READLINK, WRITE.
3440 *
3441 * In the case of the first three, we want to put the GETATTR
3442 * after the read-type operation -- this is because it is hard
3443 * to predict the length of a GETATTR response in v4, and thus
3444 * align the READ data correctly. This means that the GETATTR
3445 * may end up partially falling into the page cache, and we should
3446 * shift it into the 'tail' of the xdr_buf before processing.
3447 * To do this efficiently, we need to know the total length
3448 * of data received, which doesn't seem to be available outside
3449 * of the RPC layer.
3450 *
3451 * In the case of WRITE, we also want to put the GETATTR after
3452 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003453 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 *
3455 * Both of these changes to the XDR layer would in fact be quite
3456 * minor, but I decided to leave them for a subsequent patch.
3457 */
3458static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3459 unsigned int pgbase, unsigned int pglen)
3460{
3461 struct nfs4_readlink args = {
3462 .fh = NFS_FH(inode),
3463 .pgbase = pgbase,
3464 .pglen = pglen,
3465 .pages = &page,
3466 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003467 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 struct rpc_message msg = {
3469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3470 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003471 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 };
3473
Bryan Schumaker7c513052011-03-24 17:12:24 +00003474 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 -07003475}
3476
3477static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3478 unsigned int pgbase, unsigned int pglen)
3479{
3480 struct nfs4_exception exception = { };
3481 int err;
3482 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003483 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3484 trace_nfs4_readlink(inode, err);
3485 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 &exception);
3487 } while (exception.retry);
3488 return err;
3489}
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003492 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494static int
3495nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003496 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
David Quigleyaa9c2662013-05-22 12:50:44 -04003498 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003499 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003501 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 int status = 0;
3503
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003504 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3505 if (IS_ERR(ctx))
3506 return PTR_ERR(ctx);
3507
David Quigleyaa9c2662013-05-22 12:50:44 -04003508 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3509
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003510 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003511 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 if (IS_ERR(state)) {
3513 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003514 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003517 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003518 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 return status;
3520}
3521
3522static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3523{
Trond Myklebust16e42952005-10-27 22:12:44 -04003524 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003525 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003527 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003529 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003530 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003531 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3534 .rpc_argp = &args,
3535 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003537 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003538
Bryan Schumaker7c513052011-03-24 17:12:24 +00003539 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003540 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003541 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 return status;
3543}
3544
3545static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3546{
3547 struct nfs4_exception exception = { };
3548 int err;
3549 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003550 err = _nfs4_proc_remove(dir, name);
3551 trace_nfs4_remove(dir, name, err);
3552 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 &exception);
3554 } while (exception.retry);
3555 return err;
3556}
3557
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003558static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003560 struct nfs_server *server = NFS_SERVER(dir);
3561 struct nfs_removeargs *args = msg->rpc_argp;
3562 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003564 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003566 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003567
3568 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569}
3570
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003571static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3572{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003573 nfs4_setup_sequence(NFS_SERVER(data->dir),
3574 &data->args.seq_args,
3575 &data->res.seq_res,
3576 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003579static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003581 struct nfs_unlinkdata *data = task->tk_calldata;
3582 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003583
Trond Myklebust14516c32010-07-31 14:29:06 -04003584 if (!nfs4_sequence_done(task, &res->seq_res))
3585 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003586 if (nfs4_async_handle_error(task, res->server, NULL,
3587 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003588 return 0;
3589 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003590 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
Jeff Laytond3d41522010-09-17 17:31:57 -04003593static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3594{
3595 struct nfs_server *server = NFS_SERVER(dir);
3596 struct nfs_renameargs *arg = msg->rpc_argp;
3597 struct nfs_renameres *res = msg->rpc_resp;
3598
3599 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003600 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003601 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003602}
3603
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003604static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3605{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003606 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3607 &data->args.seq_args,
3608 &data->res.seq_res,
3609 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003610}
3611
3612static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3613 struct inode *new_dir)
3614{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003615 struct nfs_renamedata *data = task->tk_calldata;
3616 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003617
3618 if (!nfs4_sequence_done(task, &res->seq_res))
3619 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003620 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003621 return 0;
3622
3623 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003624 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003625 return 1;
3626}
3627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3629{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003630 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 struct nfs4_link_arg arg = {
3632 .fh = NFS_FH(inode),
3633 .dir_fh = NFS_FH(dir),
3634 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003635 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003637 struct nfs4_link_res res = {
3638 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003639 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003640 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 struct rpc_message msg = {
3642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3643 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003644 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003646 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Trond Myklebust136f2622010-04-16 16:22:49 -04003648 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003649 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003650 goto out;
3651
David Quigley14c43f72013-05-22 12:50:43 -04003652 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3653 if (IS_ERR(res.label)) {
3654 status = PTR_ERR(res.label);
3655 goto out;
3656 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003657 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003658
Bryan Schumaker7c513052011-03-24 17:12:24 +00003659 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003660 if (!status) {
3661 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003662 status = nfs_post_op_update_inode(inode, res.fattr);
3663 if (!status)
3664 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003665 }
David Quigley14c43f72013-05-22 12:50:43 -04003666
3667
3668 nfs4_label_free(res.label);
3669
Trond Myklebust136f2622010-04-16 16:22:49 -04003670out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003671 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 return status;
3673}
3674
3675static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3676{
3677 struct nfs4_exception exception = { };
3678 int err;
3679 do {
3680 err = nfs4_handle_exception(NFS_SERVER(inode),
3681 _nfs4_proc_link(inode, dir, name),
3682 &exception);
3683 } while (exception.retry);
3684 return err;
3685}
3686
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003687struct nfs4_createdata {
3688 struct rpc_message msg;
3689 struct nfs4_create_arg arg;
3690 struct nfs4_create_res res;
3691 struct nfs_fh fh;
3692 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003693 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003694};
3695
3696static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3697 struct qstr *name, struct iattr *sattr, u32 ftype)
3698{
3699 struct nfs4_createdata *data;
3700
3701 data = kzalloc(sizeof(*data), GFP_KERNEL);
3702 if (data != NULL) {
3703 struct nfs_server *server = NFS_SERVER(dir);
3704
David Quigley14c43f72013-05-22 12:50:43 -04003705 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3706 if (IS_ERR(data->label))
3707 goto out_free;
3708
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003709 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3710 data->msg.rpc_argp = &data->arg;
3711 data->msg.rpc_resp = &data->res;
3712 data->arg.dir_fh = NFS_FH(dir);
3713 data->arg.server = server;
3714 data->arg.name = name;
3715 data->arg.attrs = sattr;
3716 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003717 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003718 data->res.server = server;
3719 data->res.fh = &data->fh;
3720 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003721 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003722 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003723 }
3724 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003725out_free:
3726 kfree(data);
3727 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003728}
3729
3730static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3731{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003732 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003733 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003734 if (status == 0) {
3735 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003736 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003737 }
3738 return status;
3739}
3740
3741static void nfs4_free_createdata(struct nfs4_createdata *data)
3742{
David Quigley14c43f72013-05-22 12:50:43 -04003743 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003744 kfree(data);
3745}
3746
Chuck Lever4f390c12006-08-22 20:06:22 -04003747static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003748 struct page *page, unsigned int len, struct iattr *sattr,
3749 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003751 struct nfs4_createdata *data;
3752 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Chuck Lever94a6d752006-08-22 20:06:23 -04003754 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003755 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003756
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003757 status = -ENOMEM;
3758 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3759 if (data == NULL)
3760 goto out;
3761
3762 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3763 data->arg.u.symlink.pages = &page;
3764 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003765 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003767 status = nfs4_do_create(dir, dentry, data);
3768
3769 nfs4_free_createdata(data);
3770out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return status;
3772}
3773
Chuck Lever4f390c12006-08-22 20:06:22 -04003774static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003775 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
3777 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003778 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003780
3781 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003784 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3785 trace_nfs4_symlink(dir, &dentry->d_name, err);
3786 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 &exception);
3788 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003789
3790 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return err;
3792}
3793
3794static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003795 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003797 struct nfs4_createdata *data;
3798 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003800 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3801 if (data == NULL)
3802 goto out;
3803
David Quigley1775fd32013-05-22 12:50:42 -04003804 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003805 status = nfs4_do_create(dir, dentry, data);
3806
3807 nfs4_free_createdata(data);
3808out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 return status;
3810}
3811
3812static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3813 struct iattr *sattr)
3814{
3815 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003816 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003818
David Quigleyaa9c2662013-05-22 12:50:44 -04003819 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3820
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003821 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003823 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3824 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3825 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 &exception);
3827 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003828 nfs4_label_release_security(label);
3829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 return err;
3831}
3832
3833static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003834 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835{
3836 struct inode *dir = dentry->d_inode;
3837 struct nfs4_readdir_arg args = {
3838 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003839 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 .pgbase = 0,
3841 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003842 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003843 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 };
3845 struct nfs4_readdir_res res;
3846 struct rpc_message msg = {
3847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3848 .rpc_argp = &args,
3849 .rpc_resp = &res,
3850 .rpc_cred = cred,
3851 };
3852 int status;
3853
Al Viro6de14722013-09-16 10:53:17 -04003854 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3855 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003856 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003857 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003859 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 -05003860 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003861 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003862 status += args.pgbase;
3863 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003864
3865 nfs_invalidate_atime(dir);
3866
Harvey Harrison3110ff82008-05-02 13:42:44 -07003867 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 return status;
3869}
3870
3871static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003872 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873{
3874 struct nfs4_exception exception = { };
3875 int err;
3876 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003877 err = _nfs4_proc_readdir(dentry, cred, cookie,
3878 pages, count, plus);
3879 trace_nfs4_readdir(dentry->d_inode, err);
3880 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 &exception);
3882 } while (exception.retry);
3883 return err;
3884}
3885
3886static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003887 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003889 struct nfs4_createdata *data;
3890 int mode = sattr->ia_mode;
3891 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003893 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3894 if (data == NULL)
3895 goto out;
3896
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003898 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003900 data->arg.ftype = NF4BLK;
3901 data->arg.u.device.specdata1 = MAJOR(rdev);
3902 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 }
3904 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003905 data->arg.ftype = NF4CHR;
3906 data->arg.u.device.specdata1 = MAJOR(rdev);
3907 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003908 } else if (!S_ISSOCK(mode)) {
3909 status = -EINVAL;
3910 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 }
David Quigley1775fd32013-05-22 12:50:42 -04003912
David Quigleyaa9c2662013-05-22 12:50:44 -04003913 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003914 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003915out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003916 nfs4_free_createdata(data);
3917out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 return status;
3919}
3920
3921static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3922 struct iattr *sattr, dev_t rdev)
3923{
3924 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003925 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003927
David Quigleyaa9c2662013-05-22 12:50:44 -04003928 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3929
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003930 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003932 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3933 trace_nfs4_mknod(dir, &dentry->d_name, err);
3934 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 &exception);
3936 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003937
3938 nfs4_label_release_security(label);
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 return err;
3941}
3942
3943static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3944 struct nfs_fsstat *fsstat)
3945{
3946 struct nfs4_statfs_arg args = {
3947 .fh = fhandle,
3948 .bitmask = server->attr_bitmask,
3949 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003950 struct nfs4_statfs_res res = {
3951 .fsstat = fsstat,
3952 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 struct rpc_message msg = {
3954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3955 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003956 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 };
3958
Trond Myklebust0e574af2005-10-27 22:12:38 -04003959 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003960 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961}
3962
3963static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3964{
3965 struct nfs4_exception exception = { };
3966 int err;
3967 do {
3968 err = nfs4_handle_exception(server,
3969 _nfs4_proc_statfs(server, fhandle, fsstat),
3970 &exception);
3971 } while (exception.retry);
3972 return err;
3973}
3974
3975static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3976 struct nfs_fsinfo *fsinfo)
3977{
3978 struct nfs4_fsinfo_arg args = {
3979 .fh = fhandle,
3980 .bitmask = server->attr_bitmask,
3981 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003982 struct nfs4_fsinfo_res res = {
3983 .fsinfo = fsinfo,
3984 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 struct rpc_message msg = {
3986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3987 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003988 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 };
3990
Bryan Schumaker7c513052011-03-24 17:12:24 +00003991 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
3993
3994static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3995{
3996 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003997 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 int err;
3999
4000 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004001 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004002 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004003 if (err == 0) {
4004 struct nfs_client *clp = server->nfs_client;
4005
4006 spin_lock(&clp->cl_lock);
4007 clp->cl_lease_time = fsinfo->lease_time * HZ;
4008 clp->cl_last_renewal = now;
4009 spin_unlock(&clp->cl_lock);
4010 break;
4011 }
4012 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 } while (exception.retry);
4014 return err;
4015}
4016
4017static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4018{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004019 int error;
4020
Trond Myklebust0e574af2005-10-27 22:12:38 -04004021 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004022 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004023 if (error == 0) {
4024 /* block layout checks this! */
4025 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004026 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004027 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004028
4029 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030}
4031
4032static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4033 struct nfs_pathconf *pathconf)
4034{
4035 struct nfs4_pathconf_arg args = {
4036 .fh = fhandle,
4037 .bitmask = server->attr_bitmask,
4038 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004039 struct nfs4_pathconf_res res = {
4040 .pathconf = pathconf,
4041 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 struct rpc_message msg = {
4043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4044 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004045 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 };
4047
4048 /* None of the pathconf attributes are mandatory to implement */
4049 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4050 memset(pathconf, 0, sizeof(*pathconf));
4051 return 0;
4052 }
4053
Trond Myklebust0e574af2005-10-27 22:12:38 -04004054 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004055 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056}
4057
4058static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4059 struct nfs_pathconf *pathconf)
4060{
4061 struct nfs4_exception exception = { };
4062 int err;
4063
4064 do {
4065 err = nfs4_handle_exception(server,
4066 _nfs4_proc_pathconf(server, fhandle, pathconf),
4067 &exception);
4068 } while (exception.retry);
4069 return err;
4070}
4071
Trond Myklebust5521abf2013-03-16 20:54:34 -04004072int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004073 const struct nfs_open_context *ctx,
4074 const struct nfs_lock_context *l_ctx,
4075 fmode_t fmode)
4076{
4077 const struct nfs_lockowner *lockowner = NULL;
4078
4079 if (l_ctx != NULL)
4080 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004081 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004082}
4083EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4084
Trond Myklebust5521abf2013-03-16 20:54:34 -04004085static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4086 const struct nfs_open_context *ctx,
4087 const struct nfs_lock_context *l_ctx,
4088 fmode_t fmode)
4089{
4090 nfs4_stateid current_stateid;
4091
Trond Myklebuste1253be2014-03-05 08:44:23 -05004092 /* If the current stateid represents a lost lock, then exit */
4093 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4094 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004095 return nfs4_stateid_match(stateid, &current_stateid);
4096}
4097
4098static bool nfs4_error_stateid_expired(int err)
4099{
4100 switch (err) {
4101 case -NFS4ERR_DELEG_REVOKED:
4102 case -NFS4ERR_ADMIN_REVOKED:
4103 case -NFS4ERR_BAD_STATEID:
4104 case -NFS4ERR_STALE_STATEID:
4105 case -NFS4ERR_OLD_STATEID:
4106 case -NFS4ERR_OPENMODE:
4107 case -NFS4ERR_EXPIRED:
4108 return true;
4109 }
4110 return false;
4111}
4112
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004113void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004114{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004115 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004116}
4117
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004118static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004120 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004122 trace_nfs4_read(hdr, task->tk_status);
4123 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004124 hdr->args.context->state,
4125 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004126 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004127 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004129
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004130 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004132 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134}
4135
Trond Myklebust5521abf2013-03-16 20:54:34 -04004136static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004137 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004138{
4139
4140 if (!nfs4_error_stateid_expired(task->tk_status) ||
4141 nfs4_stateid_is_current(&args->stateid,
4142 args->context,
4143 args->lock_context,
4144 FMODE_READ))
4145 return false;
4146 rpc_restart_call_prepare(task);
4147 return true;
4148}
4149
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004150static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004151{
4152
4153 dprintk("--> %s\n", __func__);
4154
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004155 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004156 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004157 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004158 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004159 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4160 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004161}
4162
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004163static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4164 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004166 hdr->timestamp = jiffies;
4167 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004168 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004169 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004172static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4173 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004174{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004175 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4176 &hdr->args.seq_args,
4177 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004178 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004179 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004180 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4181 hdr->args.lock_context,
4182 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004183 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004184 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004185 return -EIO;
4186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
4188
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004189static int nfs4_write_done_cb(struct rpc_task *task,
4190 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004192 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004193
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004194 trace_nfs4_write(hdr, task->tk_status);
4195 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004196 hdr->args.context->state,
4197 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004198 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004199 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004201 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004202 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4203 nfs_post_op_update_inode_force_wcc(inode, &hdr->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004204 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206}
4207
Trond Myklebust5521abf2013-03-16 20:54:34 -04004208static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004209 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004210{
4211
4212 if (!nfs4_error_stateid_expired(task->tk_status) ||
4213 nfs4_stateid_is_current(&args->stateid,
4214 args->context,
4215 args->lock_context,
4216 FMODE_WRITE))
4217 return false;
4218 rpc_restart_call_prepare(task);
4219 return true;
4220}
4221
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004222static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004223{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004224 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004225 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004226 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004227 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004228 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4229 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004230}
4231
Trond Myklebust5a37f852012-04-28 14:55:16 -04004232static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004233bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004234{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004235 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004236 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004237 return false;
4238 /* Otherwise, request attributes if and only if we don't hold
4239 * a delegation
4240 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004241 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004242}
Fred Isamana69aef12011-03-03 15:13:47 +00004243
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004244static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4245 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004247 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004248
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004249 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4250 hdr->args.bitmask = NULL;
4251 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004252 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004253 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004254
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004255 if (!hdr->pgio_done_cb)
4256 hdr->pgio_done_cb = nfs4_write_done_cb;
4257 hdr->res.server = server;
4258 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004260 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004261 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262}
4263
Fred Isaman0b7c0152012-04-20 14:47:39 -04004264static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4265{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004266 nfs4_setup_sequence(NFS_SERVER(data->inode),
4267 &data->args.seq_args,
4268 &data->res.seq_res,
4269 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270}
4271
Fred Isaman0b7c0152012-04-20 14:47:39 -04004272static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004275
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004276 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004277 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4278 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004279 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004280 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}
4284
Fred Isaman0b7c0152012-04-20 14:47:39 -04004285static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004286{
4287 if (!nfs4_sequence_done(task, &data->res.seq_res))
4288 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004289 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004290}
4291
Fred Isaman0b7c0152012-04-20 14:47:39 -04004292static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004294 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004295
Fred Isaman0b7c0152012-04-20 14:47:39 -04004296 if (data->commit_done_cb == NULL)
4297 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004298 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004299 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004300 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301}
4302
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004303struct nfs4_renewdata {
4304 struct nfs_client *client;
4305 unsigned long timestamp;
4306};
4307
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308/*
4309 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4310 * standalone procedure for queueing an asynchronous RENEW.
4311 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004312static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004313{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004314 struct nfs4_renewdata *data = calldata;
4315 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004316
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004317 if (atomic_read(&clp->cl_count) > 1)
4318 nfs4_schedule_state_renewal(clp);
4319 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004320 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004321}
4322
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004323static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004325 struct nfs4_renewdata *data = calldata;
4326 struct nfs_client *clp = data->client;
4327 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004329 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004330 switch (task->tk_status) {
4331 case 0:
4332 break;
4333 case -NFS4ERR_LEASE_MOVED:
4334 nfs4_schedule_lease_moved_recovery(clp);
4335 break;
4336 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004337 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004338 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4339 return;
4340 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004341 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004342 return;
4343 }
4344 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004346 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347}
4348
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004349static const struct rpc_call_ops nfs4_renew_ops = {
4350 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004351 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004352};
4353
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004354static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
4356 struct rpc_message msg = {
4357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4358 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004359 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004361 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004363 if (renew_flags == 0)
4364 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004365 if (!atomic_inc_not_zero(&clp->cl_count))
4366 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004367 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004368 if (data == NULL)
4369 return -ENOMEM;
4370 data->client = clp;
4371 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004372 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004373 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374}
4375
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004376static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
4378 struct rpc_message msg = {
4379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4380 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004381 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 };
4383 unsigned long now = jiffies;
4384 int status;
4385
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004386 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 if (status < 0)
4388 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004389 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return 0;
4391}
4392
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004393static inline int nfs4_server_supports_acls(struct nfs_server *server)
4394{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004395 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004396}
4397
Trond Myklebust21f498c2012-08-24 10:59:25 -04004398/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4399 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004400 * the stack.
4401 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004402#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004403
Neil Hormane9e3d722011-03-04 19:26:03 -05004404static int buf_to_pages_noslab(const void *buf, size_t buflen,
4405 struct page **pages, unsigned int *pgbase)
4406{
4407 struct page *newpage, **spages;
4408 int rc = 0;
4409 size_t len;
4410 spages = pages;
4411
4412 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004413 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004414 newpage = alloc_page(GFP_KERNEL);
4415
4416 if (newpage == NULL)
4417 goto unwind;
4418 memcpy(page_address(newpage), buf, len);
4419 buf += len;
4420 buflen -= len;
4421 *pages++ = newpage;
4422 rc++;
4423 } while (buflen != 0);
4424
4425 return rc;
4426
4427unwind:
4428 for(; rc > 0; rc--)
4429 __free_page(spages[rc-1]);
4430 return -ENOMEM;
4431}
4432
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004433struct nfs4_cached_acl {
4434 int cached;
4435 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004436 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004437};
4438
4439static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004440{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004441 struct nfs_inode *nfsi = NFS_I(inode);
4442
4443 spin_lock(&inode->i_lock);
4444 kfree(nfsi->nfs4_acl);
4445 nfsi->nfs4_acl = acl;
4446 spin_unlock(&inode->i_lock);
4447}
4448
4449static void nfs4_zap_acl_attr(struct inode *inode)
4450{
4451 nfs4_set_cached_acl(inode, NULL);
4452}
4453
4454static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4455{
4456 struct nfs_inode *nfsi = NFS_I(inode);
4457 struct nfs4_cached_acl *acl;
4458 int ret = -ENOENT;
4459
4460 spin_lock(&inode->i_lock);
4461 acl = nfsi->nfs4_acl;
4462 if (acl == NULL)
4463 goto out;
4464 if (buf == NULL) /* user is just asking for length */
4465 goto out_len;
4466 if (acl->cached == 0)
4467 goto out;
4468 ret = -ERANGE; /* see getxattr(2) man page */
4469 if (acl->len > buflen)
4470 goto out;
4471 memcpy(buf, acl->data, acl->len);
4472out_len:
4473 ret = acl->len;
4474out:
4475 spin_unlock(&inode->i_lock);
4476 return ret;
4477}
4478
Sachin Prabhu5794d212012-04-17 14:36:40 +01004479static 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 +00004480{
4481 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004482 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004483
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004484 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004485 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004486 if (acl == NULL)
4487 goto out;
4488 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004489 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004490 } else {
4491 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4492 if (acl == NULL)
4493 goto out;
4494 acl->cached = 0;
4495 }
4496 acl->len = acl_len;
4497out:
4498 nfs4_set_cached_acl(inode, acl);
4499}
4500
Andy Adamsonbf118a32011-12-07 11:55:27 -05004501/*
4502 * The getxattr API returns the required buffer length when called with a
4503 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4504 * the required buf. On a NULL buf, we send a page of data to the server
4505 * guessing that the ACL request can be serviced by a page. If so, we cache
4506 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4507 * the cache. If not so, we throw away the page, and cache the required
4508 * length. The next getxattr call will then produce another round trip to
4509 * the server, this time with the input buf of the required size.
4510 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004511static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004512{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004513 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004514 struct nfs_getaclargs args = {
4515 .fh = NFS_FH(inode),
4516 .acl_pages = pages,
4517 .acl_len = buflen,
4518 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004519 struct nfs_getaclres res = {
4520 .acl_len = buflen,
4521 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004522 struct rpc_message msg = {
4523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4524 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004525 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004526 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004527 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4528 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004529
Andy Adamsonbf118a32011-12-07 11:55:27 -05004530 /* As long as we're doing a round trip to the server anyway,
4531 * let's be prepared for a page of acl data. */
4532 if (npages == 0)
4533 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004534 if (npages > ARRAY_SIZE(pages))
4535 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004536
Andy Adamsonbf118a32011-12-07 11:55:27 -05004537 for (i = 0; i < npages; i++) {
4538 pages[i] = alloc_page(GFP_KERNEL);
4539 if (!pages[i])
4540 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004541 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004542
4543 /* for decoding across pages */
4544 res.acl_scratch = alloc_page(GFP_KERNEL);
4545 if (!res.acl_scratch)
4546 goto out_free;
4547
Andy Adamsonbf118a32011-12-07 11:55:27 -05004548 args.acl_len = npages * PAGE_SIZE;
4549 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004550
Peng Taode040be2012-01-10 22:42:47 +08004551 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004552 __func__, buf, buflen, npages, args.acl_len);
4553 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4554 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004555 if (ret)
4556 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004557
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004558 /* Handle the case where the passed-in buffer is too short */
4559 if (res.acl_flags & NFS4_ACL_TRUNC) {
4560 /* Did the user only issue a request for the acl length? */
4561 if (buf == NULL)
4562 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004563 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004564 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004565 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004566 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004567 if (buf) {
4568 if (res.acl_len > buflen) {
4569 ret = -ERANGE;
4570 goto out_free;
4571 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004572 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004573 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004574out_ok:
4575 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004576out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004577 for (i = 0; i < npages; i++)
4578 if (pages[i])
4579 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004580 if (res.acl_scratch)
4581 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004582 return ret;
4583}
4584
Trond Myklebust16b42892006-08-24 12:27:15 -04004585static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4586{
4587 struct nfs4_exception exception = { };
4588 ssize_t ret;
4589 do {
4590 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004591 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04004592 if (ret >= 0)
4593 break;
4594 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4595 } while (exception.retry);
4596 return ret;
4597}
4598
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004599static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4600{
4601 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004602 int ret;
4603
4604 if (!nfs4_server_supports_acls(server))
4605 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004606 ret = nfs_revalidate_inode(server, inode);
4607 if (ret < 0)
4608 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004609 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4610 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004611 ret = nfs4_read_cached_acl(inode, buf, buflen);
4612 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004613 /* -ENOENT is returned if there is no ACL or if there is an ACL
4614 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004615 return ret;
4616 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004617}
4618
Trond Myklebust16b42892006-08-24 12:27:15 -04004619static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004620{
4621 struct nfs_server *server = NFS_SERVER(inode);
4622 struct page *pages[NFS4ACL_MAXPAGES];
4623 struct nfs_setaclargs arg = {
4624 .fh = NFS_FH(inode),
4625 .acl_pages = pages,
4626 .acl_len = buflen,
4627 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004628 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004629 struct rpc_message msg = {
4630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4631 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004632 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004633 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004634 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004635 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004636
4637 if (!nfs4_server_supports_acls(server))
4638 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004639 if (npages > ARRAY_SIZE(pages))
4640 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004641 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4642 if (i < 0)
4643 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004644 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004645 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004646
4647 /*
4648 * Free each page after tx, so the only ref left is
4649 * held by the network stack
4650 */
4651 for (; i > 0; i--)
4652 put_page(pages[i-1]);
4653
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004654 /*
4655 * Acl update can result in inode attribute update.
4656 * so mark the attribute cache invalid.
4657 */
4658 spin_lock(&inode->i_lock);
4659 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4660 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004661 nfs_access_zap_cache(inode);
4662 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004663 return ret;
4664}
4665
Trond Myklebust16b42892006-08-24 12:27:15 -04004666static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4667{
4668 struct nfs4_exception exception = { };
4669 int err;
4670 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004671 err = __nfs4_proc_set_acl(inode, buf, buflen);
4672 trace_nfs4_set_acl(inode, err);
4673 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04004674 &exception);
4675 } while (exception.retry);
4676 return err;
4677}
4678
David Quigleyaa9c2662013-05-22 12:50:44 -04004679#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4680static int _nfs4_get_security_label(struct inode *inode, void *buf,
4681 size_t buflen)
4682{
4683 struct nfs_server *server = NFS_SERVER(inode);
4684 struct nfs_fattr fattr;
4685 struct nfs4_label label = {0, 0, buflen, buf};
4686
4687 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004688 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004689 .fh = NFS_FH(inode),
4690 .bitmask = bitmask,
4691 };
4692 struct nfs4_getattr_res res = {
4693 .fattr = &fattr,
4694 .label = &label,
4695 .server = server,
4696 };
4697 struct rpc_message msg = {
4698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004699 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004700 .rpc_resp = &res,
4701 };
4702 int ret;
4703
4704 nfs_fattr_init(&fattr);
4705
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004706 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004707 if (ret)
4708 return ret;
4709 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4710 return -ENOENT;
4711 if (buflen < label.len)
4712 return -ERANGE;
4713 return 0;
4714}
4715
4716static int nfs4_get_security_label(struct inode *inode, void *buf,
4717 size_t buflen)
4718{
4719 struct nfs4_exception exception = { };
4720 int err;
4721
4722 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4723 return -EOPNOTSUPP;
4724
4725 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004726 err = _nfs4_get_security_label(inode, buf, buflen);
4727 trace_nfs4_get_security_label(inode, err);
4728 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004729 &exception);
4730 } while (exception.retry);
4731 return err;
4732}
4733
4734static int _nfs4_do_set_security_label(struct inode *inode,
4735 struct nfs4_label *ilabel,
4736 struct nfs_fattr *fattr,
4737 struct nfs4_label *olabel)
4738{
4739
4740 struct iattr sattr = {0};
4741 struct nfs_server *server = NFS_SERVER(inode);
4742 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004743 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004744 .fh = NFS_FH(inode),
4745 .iap = &sattr,
4746 .server = server,
4747 .bitmask = bitmask,
4748 .label = ilabel,
4749 };
4750 struct nfs_setattrres res = {
4751 .fattr = fattr,
4752 .label = olabel,
4753 .server = server,
4754 };
4755 struct rpc_message msg = {
4756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004757 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004758 .rpc_resp = &res,
4759 };
4760 int status;
4761
Jeff Layton12207f62013-11-01 10:49:32 -04004762 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004763
Jeff Layton12207f62013-11-01 10:49:32 -04004764 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004765 if (status)
4766 dprintk("%s failed: %d\n", __func__, status);
4767
4768 return status;
4769}
4770
4771static int nfs4_do_set_security_label(struct inode *inode,
4772 struct nfs4_label *ilabel,
4773 struct nfs_fattr *fattr,
4774 struct nfs4_label *olabel)
4775{
4776 struct nfs4_exception exception = { };
4777 int err;
4778
4779 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004780 err = _nfs4_do_set_security_label(inode, ilabel,
4781 fattr, olabel);
4782 trace_nfs4_set_security_label(inode, err);
4783 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004784 &exception);
4785 } while (exception.retry);
4786 return err;
4787}
4788
4789static int
4790nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4791{
4792 struct nfs4_label ilabel, *olabel = NULL;
4793 struct nfs_fattr fattr;
4794 struct rpc_cred *cred;
4795 struct inode *inode = dentry->d_inode;
4796 int status;
4797
4798 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4799 return -EOPNOTSUPP;
4800
4801 nfs_fattr_init(&fattr);
4802
4803 ilabel.pi = 0;
4804 ilabel.lfs = 0;
4805 ilabel.label = (char *)buf;
4806 ilabel.len = buflen;
4807
4808 cred = rpc_lookup_cred();
4809 if (IS_ERR(cred))
4810 return PTR_ERR(cred);
4811
4812 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4813 if (IS_ERR(olabel)) {
4814 status = -PTR_ERR(olabel);
4815 goto out;
4816 }
4817
4818 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4819 if (status == 0)
4820 nfs_setsecurity(inode, &fattr, olabel);
4821
4822 nfs4_label_free(olabel);
4823out:
4824 put_rpccred(cred);
4825 return status;
4826}
4827#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4828
4829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004831nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4832 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004834 struct nfs_client *clp = server->nfs_client;
4835
4836 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 return 0;
4838 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004839 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004840 case -NFS4ERR_ADMIN_REVOKED:
4841 case -NFS4ERR_BAD_STATEID:
4842 case -NFS4ERR_OPENMODE:
4843 if (state == NULL)
4844 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004845 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004846 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004847 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004848 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004849 if (state != NULL) {
4850 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004851 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004854 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004855 nfs4_schedule_lease_recovery(clp);
4856 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004857 case -NFS4ERR_MOVED:
4858 if (nfs4_schedule_migration_recovery(server) < 0)
4859 goto recovery_failed;
4860 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004861 case -NFS4ERR_LEASE_MOVED:
4862 nfs4_schedule_lease_moved_recovery(clp);
4863 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004864#if defined(CONFIG_NFS_V4_1)
4865 case -NFS4ERR_BADSESSION:
4866 case -NFS4ERR_BADSLOT:
4867 case -NFS4ERR_BAD_HIGH_SLOT:
4868 case -NFS4ERR_DEADSESSION:
4869 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4870 case -NFS4ERR_SEQ_FALSE_RETRY:
4871 case -NFS4ERR_SEQ_MISORDERED:
4872 dprintk("%s ERROR %d, Reset session\n", __func__,
4873 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004874 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004875 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004876#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004878 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10004879 rpc_delay(task, nfs4_update_delay(timeout));
4880 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05004881 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004883 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004885 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 }
4887 task->tk_status = nfs4_map_errors(task->tk_status);
4888 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004889recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004890 task->tk_status = -EIO;
4891 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004892wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004893 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004894 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4895 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004896 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4897 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004898restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004899 task->tk_status = 0;
4900 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901}
4902
Chuck Leverf0920752012-05-21 22:45:41 -04004903static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4904 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004905{
4906 __be32 verf[2];
4907
Chuck Lever2c820d92012-05-21 22:45:33 -04004908 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4909 /* An impossible timestamp guarantees this value
4910 * will never match a generated boot time. */
4911 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004912 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004913 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004914 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004915 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4916 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004917 }
Chuck Levercd937102012-03-02 17:14:31 -05004918 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4919}
4920
Chuck Levere984a552012-09-14 17:24:21 -04004921static unsigned int
4922nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4923 char *buf, size_t len)
4924{
4925 unsigned int result;
4926
4927 rcu_read_lock();
4928 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4929 clp->cl_ipaddr,
4930 rpc_peeraddr2str(clp->cl_rpcclient,
4931 RPC_DISPLAY_ADDR),
4932 rpc_peeraddr2str(clp->cl_rpcclient,
4933 RPC_DISPLAY_PROTO));
4934 rcu_read_unlock();
4935 return result;
4936}
4937
4938static unsigned int
4939nfs4_init_uniform_client_string(const struct nfs_client *clp,
4940 char *buf, size_t len)
4941{
Trond Myklebust55b59292013-07-24 16:36:35 -04004942 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004943
4944 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004945 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4946 clp->rpc_ops->version,
4947 clp->cl_minorversion,
4948 nfs4_client_id_uniquifier,
4949 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004950 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4951 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004952 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004953}
4954
Chuck Lever706cb8d2014-03-12 12:51:47 -04004955/*
4956 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4957 * services. Advertise one based on the address family of the
4958 * clientaddr.
4959 */
4960static unsigned int
4961nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4962{
4963 if (strchr(clp->cl_ipaddr, ':') != NULL)
4964 return scnprintf(buf, len, "tcp6");
4965 else
4966 return scnprintf(buf, len, "tcp");
4967}
4968
Jeff Laytonf11b2a12014-06-21 20:52:17 -04004969static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
4970{
4971 struct nfs4_setclientid *sc = calldata;
4972
4973 if (task->tk_status == 0)
4974 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
4975}
4976
4977static const struct rpc_call_ops nfs4_setclientid_ops = {
4978 .rpc_call_done = nfs4_setclientid_done,
4979};
4980
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004981/**
4982 * nfs4_proc_setclientid - Negotiate client ID
4983 * @clp: state data structure
4984 * @program: RPC program for NFSv4 callback service
4985 * @port: IP port number for NFS4 callback service
4986 * @cred: RPC credential to use for this call
4987 * @res: where to place the result
4988 *
4989 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4990 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004991int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4992 unsigned short port, struct rpc_cred *cred,
4993 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994{
4995 nfs4_verifier sc_verifier;
4996 struct nfs4_setclientid setclientid = {
4997 .sc_verifier = &sc_verifier,
4998 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004999 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 };
5001 struct rpc_message msg = {
5002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5003 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005004 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005005 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005007 struct rpc_task *task;
5008 struct rpc_task_setup task_setup_data = {
5009 .rpc_client = clp->cl_rpcclient,
5010 .rpc_message = &msg,
5011 .callback_ops = &nfs4_setclientid_ops,
5012 .callback_data = &setclientid,
5013 .flags = RPC_TASK_TIMEOUT,
5014 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005015 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Chuck Leverde734832012-07-11 16:30:50 -04005017 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005018 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005019 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5020 setclientid.sc_name_len =
5021 nfs4_init_uniform_client_string(clp,
5022 setclientid.sc_name,
5023 sizeof(setclientid.sc_name));
5024 else
5025 setclientid.sc_name_len =
5026 nfs4_init_nonuniform_client_string(clp,
5027 setclientid.sc_name,
5028 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04005029 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005030 setclientid.sc_netid_len =
5031 nfs4_init_callback_netid(clp,
5032 setclientid.sc_netid,
5033 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005034 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005035 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 clp->cl_ipaddr, port >> 8, port & 255);
5037
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005038 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
5039 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5040 setclientid.sc_name_len, setclientid.sc_name);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005041 task = rpc_run_task(&task_setup_data);
5042 if (IS_ERR(task)) {
5043 status = PTR_ERR(task);
5044 goto out;
5045 }
5046 status = task->tk_status;
5047 if (setclientid.sc_cred) {
5048 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5049 put_rpccred(setclientid.sc_cred);
5050 }
5051 rpc_put_task(task);
5052out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005053 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005054 dprintk("NFS reply setclientid: %d\n", status);
5055 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056}
5057
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005058/**
5059 * nfs4_proc_setclientid_confirm - Confirm client ID
5060 * @clp: state data structure
5061 * @res: result of a previous SETCLIENTID
5062 * @cred: RPC credential to use for this call
5063 *
5064 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5065 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005066int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005067 struct nfs4_setclientid_res *arg,
5068 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 struct rpc_message msg = {
5071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005072 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005073 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 int status;
5076
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005077 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5078 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5079 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005080 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005081 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005082 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 return status;
5084}
5085
Trond Myklebustfe650402006-01-03 09:55:18 +01005086struct nfs4_delegreturndata {
5087 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005088 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005089 struct nfs_fh fh;
5090 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005091 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005092 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005093 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005094 struct inode *inode;
5095 bool roc;
5096 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005097};
5098
Trond Myklebustfe650402006-01-03 09:55:18 +01005099static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5100{
5101 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005102
Trond Myklebust14516c32010-07-31 14:29:06 -04005103 if (!nfs4_sequence_done(task, &data->res.seq_res))
5104 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005105
Trond Myklebustca8acf82013-08-13 10:36:56 -04005106 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005107 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005108 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005109 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005110 case -NFS4ERR_ADMIN_REVOKED:
5111 case -NFS4ERR_DELEG_REVOKED:
5112 case -NFS4ERR_BAD_STATEID:
5113 case -NFS4ERR_OLD_STATEID:
5114 case -NFS4ERR_STALE_STATEID:
5115 case -NFS4ERR_EXPIRED:
5116 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005117 if (data->roc)
5118 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005119 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005120 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005121 if (nfs4_async_handle_error(task, data->res.server,
5122 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005123 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005124 return;
5125 }
5126 }
5127 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005128}
5129
5130static void nfs4_delegreturn_release(void *calldata)
5131{
Peng Tao039b7562014-07-03 13:05:02 +08005132 struct nfs4_delegreturndata *data = calldata;
5133
5134 if (data->roc)
5135 pnfs_roc_release(data->inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005136 kfree(calldata);
5137}
5138
Andy Adamson938e1012009-04-01 09:22:28 -04005139static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5140{
5141 struct nfs4_delegreturndata *d_data;
5142
5143 d_data = (struct nfs4_delegreturndata *)data;
5144
Peng Tao039b7562014-07-03 13:05:02 +08005145 if (d_data->roc &&
5146 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5147 return;
5148
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005149 nfs4_setup_sequence(d_data->res.server,
5150 &d_data->args.seq_args,
5151 &d_data->res.seq_res,
5152 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005153}
Andy Adamson938e1012009-04-01 09:22:28 -04005154
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005155static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005156 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005157 .rpc_call_done = nfs4_delegreturn_done,
5158 .rpc_release = nfs4_delegreturn_release,
5159};
5160
Trond Myklebuste6f81072008-01-24 18:14:34 -05005161static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005162{
5163 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005164 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005165 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005166 struct rpc_message msg = {
5167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5168 .rpc_cred = cred,
5169 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005170 struct rpc_task_setup task_setup_data = {
5171 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005172 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005173 .callback_ops = &nfs4_delegreturn_ops,
5174 .flags = RPC_TASK_ASYNC,
5175 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005176 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005177
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005178 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005179 if (data == NULL)
5180 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005181 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005182 data->args.fhandle = &data->fh;
5183 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005184 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005185 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005186 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005187 data->res.fattr = &data->fattr;
5188 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005189 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005190 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005191 data->rpc_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005192 data->inode = inode;
5193 data->roc = list_empty(&NFS_I(inode)->open_files) ?
5194 pnfs_roc(inode) : false;
Trond Myklebustfe650402006-01-03 09:55:18 +01005195
Trond Myklebustc970aa82007-07-14 15:39:59 -04005196 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005197 msg.rpc_argp = &data->args;
5198 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005199 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005200 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005201 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005202 if (!issync)
5203 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005204 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005205 if (status != 0)
5206 goto out;
5207 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005208 if (status == 0)
5209 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5210 else
5211 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005212out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005213 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005214 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215}
5216
Trond Myklebuste6f81072008-01-24 18:14:34 -05005217int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218{
5219 struct nfs_server *server = NFS_SERVER(inode);
5220 struct nfs4_exception exception = { };
5221 int err;
5222 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005223 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005224 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 switch (err) {
5226 case -NFS4ERR_STALE_STATEID:
5227 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 case 0:
5229 return 0;
5230 }
5231 err = nfs4_handle_exception(server, err, &exception);
5232 } while (exception.retry);
5233 return err;
5234}
5235
5236#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5237#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5238
5239/*
5240 * sleep, with exponential backoff, and retry the LOCK operation.
5241 */
5242static unsigned long
5243nfs4_set_lock_task_retry(unsigned long timeout)
5244{
Colin Cross416ad3c2013-05-06 23:50:06 +00005245 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 timeout <<= 1;
5247 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5248 return NFS4_LOCK_MAXTIMEOUT;
5249 return timeout;
5250}
5251
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5253{
5254 struct inode *inode = state->inode;
5255 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005256 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005257 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005259 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005261 struct nfs_lockt_res res = {
5262 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 };
5264 struct rpc_message msg = {
5265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5266 .rpc_argp = &arg,
5267 .rpc_resp = &res,
5268 .rpc_cred = state->owner->so_cred,
5269 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 struct nfs4_lock_state *lsp;
5271 int status;
5272
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005273 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005274 status = nfs4_set_lock_state(state, request);
5275 if (status != 0)
5276 goto out;
5277 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005278 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005279 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005280 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005281 switch (status) {
5282 case 0:
5283 request->fl_type = F_UNLCK;
5284 break;
5285 case -NFS4ERR_DENIED:
5286 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005288 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005289 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005290out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 return status;
5292}
5293
5294static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5295{
5296 struct nfs4_exception exception = { };
5297 int err;
5298
5299 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005300 err = _nfs4_proc_getlk(state, cmd, request);
5301 trace_nfs4_get_lock(request, state, cmd, err);
5302 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 &exception);
5304 } while (exception.retry);
5305 return err;
5306}
5307
5308static int do_vfs_lock(struct file *file, struct file_lock *fl)
5309{
5310 int res = 0;
5311 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5312 case FL_POSIX:
5313 res = posix_lock_file_wait(file, fl);
5314 break;
5315 case FL_FLOCK:
5316 res = flock_lock_file_wait(file, fl);
5317 break;
5318 default:
5319 BUG();
5320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 return res;
5322}
5323
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005324struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005325 struct nfs_locku_args arg;
5326 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005327 struct nfs4_lock_state *lsp;
5328 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005329 struct file_lock fl;
5330 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005331 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005332};
5333
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005334static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5335 struct nfs_open_context *ctx,
5336 struct nfs4_lock_state *lsp,
5337 struct nfs_seqid *seqid)
5338{
5339 struct nfs4_unlockdata *p;
5340 struct inode *inode = lsp->ls_state->inode;
5341
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005342 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005343 if (p == NULL)
5344 return NULL;
5345 p->arg.fh = NFS_FH(inode);
5346 p->arg.fl = &p->fl;
5347 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005348 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005349 p->arg.stateid = &lsp->ls_stateid;
5350 p->lsp = lsp;
5351 atomic_inc(&lsp->ls_count);
5352 /* Ensure we don't close file until we're done freeing locks! */
5353 p->ctx = get_nfs_open_context(ctx);
5354 memcpy(&p->fl, fl, sizeof(p->fl));
5355 p->server = NFS_SERVER(inode);
5356 return p;
5357}
5358
Trond Myklebust06f814a2006-01-03 09:55:07 +01005359static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005360{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005361 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005362 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005363 nfs4_put_lock_state(calldata->lsp);
5364 put_nfs_open_context(calldata->ctx);
5365 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005366}
5367
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005368static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005369{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005370 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005371
Trond Myklebust14516c32010-07-31 14:29:06 -04005372 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5373 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005374 switch (task->tk_status) {
5375 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005376 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5377 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005378 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005379 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005380 case -NFS4ERR_BAD_STATEID:
5381 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005382 case -NFS4ERR_STALE_STATEID:
5383 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005384 break;
5385 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005386 if (nfs4_async_handle_error(task, calldata->server,
5387 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005388 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005389 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005390 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005391}
5392
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005393static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005394{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005395 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005397 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005398 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005399 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005400 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005401 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005402 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005403 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005404 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005405 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005406 &calldata->res.seq_res,
5407 task) != 0)
5408 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005409 return;
5410out_no_action:
5411 task->tk_action = NULL;
5412out_wait:
5413 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414}
5415
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005416static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005417 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005418 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005419 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005420};
5421
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005422static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5423 struct nfs_open_context *ctx,
5424 struct nfs4_lock_state *lsp,
5425 struct nfs_seqid *seqid)
5426{
5427 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005428 struct rpc_message msg = {
5429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5430 .rpc_cred = ctx->cred,
5431 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005432 struct rpc_task_setup task_setup_data = {
5433 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005434 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005435 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005436 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005437 .flags = RPC_TASK_ASYNC,
5438 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005439
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005440 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5441 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5442
Frank Filz137d6ac2007-07-09 15:32:29 -07005443 /* Ensure this is an unlock - when canceling a lock, the
5444 * canceled lock is passed in, and it won't be an unlock.
5445 */
5446 fl->fl_type = F_UNLCK;
5447
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005448 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5449 if (data == NULL) {
5450 nfs_free_seqid(seqid);
5451 return ERR_PTR(-ENOMEM);
5452 }
5453
Chuck Levera9c92d62013-08-09 12:48:18 -04005454 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005455 msg.rpc_argp = &data->arg;
5456 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005457 task_setup_data.callback_data = data;
5458 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005459}
5460
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5462{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005463 struct inode *inode = state->inode;
5464 struct nfs4_state_owner *sp = state->owner;
5465 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005466 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005467 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005468 struct rpc_task *task;
5469 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005470 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
Trond Myklebust9b073572006-06-29 16:38:34 -04005472 status = nfs4_set_lock_state(state, request);
5473 /* Unlock _before_ we do the RPC call */
5474 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005475 /* Exclude nfs_delegation_claim_locks() */
5476 mutex_lock(&sp->so_delegreturn_mutex);
5477 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005478 down_read(&nfsi->rwsem);
5479 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5480 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005481 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005482 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005483 }
5484 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005485 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005486 if (status != 0)
5487 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005488 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005489 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005490 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5491 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005492 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005493 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005494 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005495 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005496 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005497 status = PTR_ERR(task);
5498 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005499 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005500 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005501 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005502out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005503 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005504 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005505 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506}
5507
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005508struct nfs4_lockdata {
5509 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005510 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005511 struct nfs4_lock_state *lsp;
5512 struct nfs_open_context *ctx;
5513 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005514 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005515 int rpc_status;
5516 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005517 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005518};
5519
5520static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005521 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5522 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005523{
5524 struct nfs4_lockdata *p;
5525 struct inode *inode = lsp->ls_state->inode;
5526 struct nfs_server *server = NFS_SERVER(inode);
5527
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005528 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005529 if (p == NULL)
5530 return NULL;
5531
5532 p->arg.fh = NFS_FH(inode);
5533 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005534 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005535 if (p->arg.open_seqid == NULL)
5536 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005537 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005538 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005539 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005540 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005541 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005542 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005543 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005544 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005545 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005546 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005547 atomic_inc(&lsp->ls_count);
5548 p->ctx = get_nfs_open_context(ctx);
5549 memcpy(&p->fl, fl, sizeof(p->fl));
5550 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005551out_free_seqid:
5552 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005553out_free:
5554 kfree(p);
5555 return NULL;
5556}
5557
5558static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5559{
5560 struct nfs4_lockdata *data = calldata;
5561 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562
Harvey Harrison3110ff82008-05-02 13:42:44 -07005563 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005564 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005565 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005566 /* Do we need to do an open_to_lock_owner? */
5567 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005568 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005569 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005570 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005571 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005572 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005573 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005574 } else
5575 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005576 if (!nfs4_valid_open_stateid(state)) {
5577 data->rpc_status = -EBADF;
5578 task->tk_action = NULL;
5579 goto out_release_open_seqid;
5580 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005581 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005582 if (nfs4_setup_sequence(data->server,
5583 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005584 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005585 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005586 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005587out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005588 nfs_release_seqid(data->arg.open_seqid);
5589out_release_lock_seqid:
5590 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005591out_wait:
5592 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005593 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005594}
5595
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005596static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5597{
5598 struct nfs4_lockdata *data = calldata;
5599
Harvey Harrison3110ff82008-05-02 13:42:44 -07005600 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005601
Trond Myklebust14516c32010-07-31 14:29:06 -04005602 if (!nfs4_sequence_done(task, &data->res.seq_res))
5603 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005604
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005605 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005606 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005607 if (data->rpc_status == 0)
5608 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5609 else
5610 goto out;
5611 }
5612 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005613 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005614 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005615 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005616 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005617out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005618 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005619}
5620
5621static void nfs4_lock_release(void *calldata)
5622{
5623 struct nfs4_lockdata *data = calldata;
5624
Harvey Harrison3110ff82008-05-02 13:42:44 -07005625 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005626 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005627 if (data->cancelled != 0) {
5628 struct rpc_task *task;
5629 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5630 data->arg.lock_seqid);
5631 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005632 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005633 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005634 } else
5635 nfs_free_seqid(data->arg.lock_seqid);
5636 nfs4_put_lock_state(data->lsp);
5637 put_nfs_open_context(data->ctx);
5638 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005639 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005640}
5641
5642static const struct rpc_call_ops nfs4_lock_ops = {
5643 .rpc_call_prepare = nfs4_lock_prepare,
5644 .rpc_call_done = nfs4_lock_done,
5645 .rpc_release = nfs4_lock_release,
5646};
5647
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005648static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5649{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005650 switch (error) {
5651 case -NFS4ERR_ADMIN_REVOKED:
5652 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005653 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005654 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005655 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005656 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005657 break;
5658 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005659 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005660 case -NFS4ERR_EXPIRED:
5661 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005662 };
5663}
5664
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005665static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005666{
5667 struct nfs4_lockdata *data;
5668 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005669 struct rpc_message msg = {
5670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5671 .rpc_cred = state->owner->so_cred,
5672 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005673 struct rpc_task_setup task_setup_data = {
5674 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005675 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005676 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005677 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005678 .flags = RPC_TASK_ASYNC,
5679 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005680 int ret;
5681
Harvey Harrison3110ff82008-05-02 13:42:44 -07005682 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005683 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005684 fl->fl_u.nfs4_fl.owner,
5685 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005686 if (data == NULL)
5687 return -ENOMEM;
5688 if (IS_SETLKW(cmd))
5689 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005690 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005691 msg.rpc_argp = &data->arg;
5692 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005693 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005694 if (recovery_type > NFS_LOCK_NEW) {
5695 if (recovery_type == NFS_LOCK_RECLAIM)
5696 data->arg.reclaim = NFS_LOCK_RECLAIM;
5697 nfs4_set_sequence_privileged(&data->arg.seq_args);
5698 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005699 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005700 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005701 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005702 ret = nfs4_wait_for_completion_rpc_task(task);
5703 if (ret == 0) {
5704 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005705 if (ret)
5706 nfs4_handle_setlk_error(data->server, data->lsp,
5707 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005708 } else
5709 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005710 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005711 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005712 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713}
5714
5715static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5716{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005717 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005718 struct nfs4_exception exception = {
5719 .inode = state->inode,
5720 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005721 int err;
5722
5723 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005724 /* Cache the lock if possible... */
5725 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5726 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005727 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005728 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005729 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005730 break;
5731 nfs4_handle_exception(server, err, &exception);
5732 } while (exception.retry);
5733 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734}
5735
5736static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5737{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005738 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005739 struct nfs4_exception exception = {
5740 .inode = state->inode,
5741 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005742 int err;
5743
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005744 err = nfs4_set_lock_state(state, request);
5745 if (err != 0)
5746 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005747 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005748 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5749 return 0;
5750 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005751 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005752 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5753 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005754 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005755 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005756 switch (err) {
5757 default:
5758 goto out;
5759 case -NFS4ERR_GRACE:
5760 case -NFS4ERR_DELAY:
5761 nfs4_handle_exception(server, err, &exception);
5762 err = 0;
5763 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005764 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005765out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005766 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767}
5768
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005769#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005770/**
5771 * nfs41_check_expired_locks - possibly free a lock stateid
5772 *
5773 * @state: NFSv4 state for an inode
5774 *
5775 * Returns NFS_OK if recovery for this stateid is now finished.
5776 * Otherwise a negative NFS4ERR value is returned.
5777 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005778static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005779{
Chuck Levereb64cf92012-07-11 16:30:05 -04005780 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005781 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005782 struct nfs_server *server = NFS_SERVER(state->inode);
5783
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005784 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005785 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005786 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5787
5788 status = nfs41_test_stateid(server,
5789 &lsp->ls_stateid,
5790 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005791 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005792 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005793 /* Free the stateid unless the server
5794 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005795 if (status != -NFS4ERR_BAD_STATEID)
5796 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005797 &lsp->ls_stateid,
5798 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005799 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005800 ret = status;
5801 }
5802 }
5803 };
5804
5805 return ret;
5806}
5807
5808static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5809{
5810 int status = NFS_OK;
5811
5812 if (test_bit(LK_STATE_IN_USE, &state->flags))
5813 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005814 if (status != NFS_OK)
5815 status = nfs4_lock_expired(state, request);
5816 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005817}
5818#endif
5819
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5821{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005822 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005823 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005824 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005825 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005826 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005828 if ((fl_flags & FL_POSIX) &&
5829 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5830 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005831 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005832 status = nfs4_set_lock_state(state, request);
5833 if (status != 0)
5834 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005835 request->fl_flags |= FL_ACCESS;
5836 status = do_vfs_lock(request->fl_file, request);
5837 if (status < 0)
5838 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005839 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005840 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005841 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005842 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005843 request->fl_flags = fl_flags & ~FL_SLEEP;
5844 status = do_vfs_lock(request->fl_file, request);
5845 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005846 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005847 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5848 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005849 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005850 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005851 goto out;
5852 down_read(&nfsi->rwsem);
5853 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5854 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005855 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005856 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005857 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005858 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005859 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005860 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5861 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005862out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005863 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005864out:
5865 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 return status;
5867}
5868
5869static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5870{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005871 struct nfs4_exception exception = {
5872 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005873 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005874 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 int err;
5876
5877 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005878 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005879 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005880 if (err == -NFS4ERR_DENIED)
5881 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005883 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 } while (exception.retry);
5885 return err;
5886}
5887
5888static int
5889nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5890{
5891 struct nfs_open_context *ctx;
5892 struct nfs4_state *state;
5893 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5894 int status;
5895
5896 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005897 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 state = ctx->state;
5899
5900 if (request->fl_start < 0 || request->fl_end < 0)
5901 return -EINVAL;
5902
Trond Myklebustd9531262009-07-21 19:22:38 -04005903 if (IS_GETLK(cmd)) {
5904 if (state != NULL)
5905 return nfs4_proc_getlk(state, F_GETLK, request);
5906 return 0;
5907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5910 return -EINVAL;
5911
Trond Myklebustd9531262009-07-21 19:22:38 -04005912 if (request->fl_type == F_UNLCK) {
5913 if (state != NULL)
5914 return nfs4_proc_unlck(state, cmd, request);
5915 return 0;
5916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
Trond Myklebustd9531262009-07-21 19:22:38 -04005918 if (state == NULL)
5919 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005920 /*
5921 * Don't rely on the VFS having checked the file open mode,
5922 * since it won't do this for flock() locks.
5923 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005924 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005925 case F_RDLCK:
5926 if (!(filp->f_mode & FMODE_READ))
5927 return -EBADF;
5928 break;
5929 case F_WRLCK:
5930 if (!(filp->f_mode & FMODE_WRITE))
5931 return -EBADF;
5932 }
5933
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 do {
5935 status = nfs4_proc_setlk(state, cmd, request);
5936 if ((status != -EAGAIN) || IS_SETLK(cmd))
5937 break;
5938 timeout = nfs4_set_lock_task_retry(timeout);
5939 status = -ERESTARTSYS;
5940 if (signalled())
5941 break;
5942 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 return status;
5944}
5945
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005946int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005947{
5948 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005949 int err;
5950
5951 err = nfs4_set_lock_state(state, fl);
5952 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005953 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005954 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005955 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005956}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005957
Trond Myklebustcf470c32012-03-07 13:49:12 -05005958struct nfs_release_lockowner_data {
5959 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005960 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005961 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005962 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04005963 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005964};
5965
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005966static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5967{
5968 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08005969 struct nfs_server *server = data->server;
5970 nfs40_setup_sequence(server, &data->args.seq_args,
5971 &data->res.seq_res, task);
5972 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04005973 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005974}
5975
5976static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5977{
5978 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04005979 struct nfs_server *server = data->server;
5980
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005981 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04005982
5983 switch (task->tk_status) {
5984 case 0:
5985 renew_lease(server, data->timestamp);
5986 break;
5987 case -NFS4ERR_STALE_CLIENTID:
5988 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08005989 nfs4_schedule_lease_recovery(server->nfs_client);
5990 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04005991 case -NFS4ERR_LEASE_MOVED:
5992 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10005993 if (nfs4_async_handle_error(task, server,
5994 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04005995 rpc_restart_call_prepare(task);
5996 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005997}
5998
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005999static void nfs4_release_lockowner_release(void *calldata)
6000{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006001 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006002 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006003 kfree(calldata);
6004}
6005
Trond Myklebust17280172012-03-11 13:11:00 -04006006static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006007 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6008 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006009 .rpc_release = nfs4_release_lockowner_release,
6010};
6011
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006012static void
6013nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006014{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006015 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006016 struct rpc_message msg = {
6017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6018 };
6019
6020 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006021 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006022
Trond Myklebustcf470c32012-03-07 13:49:12 -05006023 data = kmalloc(sizeof(*data), GFP_NOFS);
6024 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006025 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006026 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006027 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006028 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6029 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6030 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006031
Trond Myklebustcf470c32012-03-07 13:49:12 -05006032 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006033 msg.rpc_resp = &data->res;
6034 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006035 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006036}
6037
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006038#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6039
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006040static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6041 const void *buf, size_t buflen,
6042 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006043{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006044 if (strcmp(key, "") != 0)
6045 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006046
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006047 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006048}
6049
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006050static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6051 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006052{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006053 if (strcmp(key, "") != 0)
6054 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006055
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006056 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006057}
6058
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006059static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6060 size_t list_len, const char *name,
6061 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006062{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006063 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006064
J. Bruce Fields096455a2006-03-20 23:23:42 -05006065 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
6066 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006067
6068 if (list && len <= list_len)
6069 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006070 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006071}
6072
David Quigleyc9bccef2013-05-22 12:50:45 -04006073#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6074static inline int nfs4_server_supports_labels(struct nfs_server *server)
6075{
6076 return server->caps & NFS_CAP_SECURITY_LABEL;
6077}
6078
6079static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6080 const void *buf, size_t buflen,
6081 int flags, int type)
6082{
6083 if (security_ismaclabel(key))
6084 return nfs4_set_security_label(dentry, buf, buflen);
6085
6086 return -EOPNOTSUPP;
6087}
6088
6089static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6090 void *buf, size_t buflen, int type)
6091{
6092 if (security_ismaclabel(key))
6093 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
6094 return -EOPNOTSUPP;
6095}
6096
6097static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6098 size_t list_len, const char *name,
6099 size_t name_len, int type)
6100{
6101 size_t len = 0;
6102
6103 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
6104 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
6105 if (list && len <= list_len)
6106 security_inode_listsecurity(dentry->d_inode, list, len);
6107 }
6108 return len;
6109}
6110
6111static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6112 .prefix = XATTR_SECURITY_PREFIX,
6113 .list = nfs4_xattr_list_nfs4_label,
6114 .get = nfs4_xattr_get_nfs4_label,
6115 .set = nfs4_xattr_set_nfs4_label,
6116};
6117#endif
6118
6119
Andy Adamson533eb462011-06-13 18:25:56 -04006120/*
6121 * nfs_fhget will use either the mounted_on_fileid or the fileid
6122 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006123static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6124{
Andy Adamson533eb462011-06-13 18:25:56 -04006125 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6126 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6127 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006128 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006129 return;
6130
6131 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006132 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006133 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6134 fattr->nlink = 2;
6135}
6136
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006137static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6138 const struct qstr *name,
6139 struct nfs4_fs_locations *fs_locations,
6140 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006141{
6142 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006143 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006144 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006145 };
6146 struct nfs4_fs_locations_arg args = {
6147 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006148 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006149 .page = page,
6150 .bitmask = bitmask,
6151 };
Benny Halevy22958462009-04-01 09:22:02 -04006152 struct nfs4_fs_locations_res res = {
6153 .fs_locations = fs_locations,
6154 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006155 struct rpc_message msg = {
6156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6157 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006158 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006159 };
6160 int status;
6161
Harvey Harrison3110ff82008-05-02 13:42:44 -07006162 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006163
6164 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6165 * is not supported */
6166 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6167 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6168 else
6169 bitmask[0] |= FATTR4_WORD0_FILEID;
6170
Trond Myklebustc228fd32007-01-13 02:28:11 -05006171 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006172 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006173 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006174 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006175 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006176 return status;
6177}
6178
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006179int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6180 const struct qstr *name,
6181 struct nfs4_fs_locations *fs_locations,
6182 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006183{
6184 struct nfs4_exception exception = { };
6185 int err;
6186 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006187 err = _nfs4_proc_fs_locations(client, dir, name,
6188 fs_locations, page);
6189 trace_nfs4_get_fs_locations(dir, name, err);
6190 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006191 &exception);
6192 } while (exception.retry);
6193 return err;
6194}
6195
Chuck Leverb03d7352013-10-17 14:12:50 -04006196/*
6197 * This operation also signals the server that this client is
6198 * performing migration recovery. The server can stop returning
6199 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6200 * appended to this compound to identify the client ID which is
6201 * performing recovery.
6202 */
6203static int _nfs40_proc_get_locations(struct inode *inode,
6204 struct nfs4_fs_locations *locations,
6205 struct page *page, struct rpc_cred *cred)
6206{
6207 struct nfs_server *server = NFS_SERVER(inode);
6208 struct rpc_clnt *clnt = server->client;
6209 u32 bitmask[2] = {
6210 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6211 };
6212 struct nfs4_fs_locations_arg args = {
6213 .clientid = server->nfs_client->cl_clientid,
6214 .fh = NFS_FH(inode),
6215 .page = page,
6216 .bitmask = bitmask,
6217 .migration = 1, /* skip LOOKUP */
6218 .renew = 1, /* append RENEW */
6219 };
6220 struct nfs4_fs_locations_res res = {
6221 .fs_locations = locations,
6222 .migration = 1,
6223 .renew = 1,
6224 };
6225 struct rpc_message msg = {
6226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6227 .rpc_argp = &args,
6228 .rpc_resp = &res,
6229 .rpc_cred = cred,
6230 };
6231 unsigned long now = jiffies;
6232 int status;
6233
6234 nfs_fattr_init(&locations->fattr);
6235 locations->server = server;
6236 locations->nlocations = 0;
6237
6238 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6239 nfs4_set_sequence_privileged(&args.seq_args);
6240 status = nfs4_call_sync_sequence(clnt, server, &msg,
6241 &args.seq_args, &res.seq_res);
6242 if (status)
6243 return status;
6244
6245 renew_lease(server, now);
6246 return 0;
6247}
6248
6249#ifdef CONFIG_NFS_V4_1
6250
6251/*
6252 * This operation also signals the server that this client is
6253 * performing migration recovery. The server can stop asserting
6254 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6255 * performing this operation is identified in the SEQUENCE
6256 * operation in this compound.
6257 *
6258 * When the client supports GETATTR(fs_locations_info), it can
6259 * be plumbed in here.
6260 */
6261static int _nfs41_proc_get_locations(struct inode *inode,
6262 struct nfs4_fs_locations *locations,
6263 struct page *page, struct rpc_cred *cred)
6264{
6265 struct nfs_server *server = NFS_SERVER(inode);
6266 struct rpc_clnt *clnt = server->client;
6267 u32 bitmask[2] = {
6268 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6269 };
6270 struct nfs4_fs_locations_arg args = {
6271 .fh = NFS_FH(inode),
6272 .page = page,
6273 .bitmask = bitmask,
6274 .migration = 1, /* skip LOOKUP */
6275 };
6276 struct nfs4_fs_locations_res res = {
6277 .fs_locations = locations,
6278 .migration = 1,
6279 };
6280 struct rpc_message msg = {
6281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6282 .rpc_argp = &args,
6283 .rpc_resp = &res,
6284 .rpc_cred = cred,
6285 };
6286 int status;
6287
6288 nfs_fattr_init(&locations->fattr);
6289 locations->server = server;
6290 locations->nlocations = 0;
6291
6292 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6293 nfs4_set_sequence_privileged(&args.seq_args);
6294 status = nfs4_call_sync_sequence(clnt, server, &msg,
6295 &args.seq_args, &res.seq_res);
6296 if (status == NFS4_OK &&
6297 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6298 status = -NFS4ERR_LEASE_MOVED;
6299 return status;
6300}
6301
6302#endif /* CONFIG_NFS_V4_1 */
6303
6304/**
6305 * nfs4_proc_get_locations - discover locations for a migrated FSID
6306 * @inode: inode on FSID that is migrating
6307 * @locations: result of query
6308 * @page: buffer
6309 * @cred: credential to use for this operation
6310 *
6311 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6312 * operation failed, or a negative errno if a local error occurred.
6313 *
6314 * On success, "locations" is filled in, but if the server has
6315 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6316 * asserted.
6317 *
6318 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6319 * from this client that require migration recovery.
6320 */
6321int nfs4_proc_get_locations(struct inode *inode,
6322 struct nfs4_fs_locations *locations,
6323 struct page *page, struct rpc_cred *cred)
6324{
6325 struct nfs_server *server = NFS_SERVER(inode);
6326 struct nfs_client *clp = server->nfs_client;
6327 const struct nfs4_mig_recovery_ops *ops =
6328 clp->cl_mvops->mig_recovery_ops;
6329 struct nfs4_exception exception = { };
6330 int status;
6331
6332 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6333 (unsigned long long)server->fsid.major,
6334 (unsigned long long)server->fsid.minor,
6335 clp->cl_hostname);
6336 nfs_display_fhandle(NFS_FH(inode), __func__);
6337
6338 do {
6339 status = ops->get_locations(inode, locations, page, cred);
6340 if (status != -NFS4ERR_DELAY)
6341 break;
6342 nfs4_handle_exception(server, status, &exception);
6343 } while (exception.retry);
6344 return status;
6345}
6346
Chuck Lever44c99932013-10-17 14:13:30 -04006347/*
6348 * This operation also signals the server that this client is
6349 * performing "lease moved" recovery. The server can stop
6350 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6351 * is appended to this compound to identify the client ID which is
6352 * performing recovery.
6353 */
6354static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6355{
6356 struct nfs_server *server = NFS_SERVER(inode);
6357 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6358 struct rpc_clnt *clnt = server->client;
6359 struct nfs4_fsid_present_arg args = {
6360 .fh = NFS_FH(inode),
6361 .clientid = clp->cl_clientid,
6362 .renew = 1, /* append RENEW */
6363 };
6364 struct nfs4_fsid_present_res res = {
6365 .renew = 1,
6366 };
6367 struct rpc_message msg = {
6368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6369 .rpc_argp = &args,
6370 .rpc_resp = &res,
6371 .rpc_cred = cred,
6372 };
6373 unsigned long now = jiffies;
6374 int status;
6375
6376 res.fh = nfs_alloc_fhandle();
6377 if (res.fh == NULL)
6378 return -ENOMEM;
6379
6380 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6381 nfs4_set_sequence_privileged(&args.seq_args);
6382 status = nfs4_call_sync_sequence(clnt, server, &msg,
6383 &args.seq_args, &res.seq_res);
6384 nfs_free_fhandle(res.fh);
6385 if (status)
6386 return status;
6387
6388 do_renew_lease(clp, now);
6389 return 0;
6390}
6391
6392#ifdef CONFIG_NFS_V4_1
6393
6394/*
6395 * This operation also signals the server that this client is
6396 * performing "lease moved" recovery. The server can stop asserting
6397 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6398 * this operation is identified in the SEQUENCE operation in this
6399 * compound.
6400 */
6401static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6402{
6403 struct nfs_server *server = NFS_SERVER(inode);
6404 struct rpc_clnt *clnt = server->client;
6405 struct nfs4_fsid_present_arg args = {
6406 .fh = NFS_FH(inode),
6407 };
6408 struct nfs4_fsid_present_res res = {
6409 };
6410 struct rpc_message msg = {
6411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6412 .rpc_argp = &args,
6413 .rpc_resp = &res,
6414 .rpc_cred = cred,
6415 };
6416 int status;
6417
6418 res.fh = nfs_alloc_fhandle();
6419 if (res.fh == NULL)
6420 return -ENOMEM;
6421
6422 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6423 nfs4_set_sequence_privileged(&args.seq_args);
6424 status = nfs4_call_sync_sequence(clnt, server, &msg,
6425 &args.seq_args, &res.seq_res);
6426 nfs_free_fhandle(res.fh);
6427 if (status == NFS4_OK &&
6428 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6429 status = -NFS4ERR_LEASE_MOVED;
6430 return status;
6431}
6432
6433#endif /* CONFIG_NFS_V4_1 */
6434
6435/**
6436 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6437 * @inode: inode on FSID to check
6438 * @cred: credential to use for this operation
6439 *
6440 * Server indicates whether the FSID is present, moved, or not
6441 * recognized. This operation is necessary to clear a LEASE_MOVED
6442 * condition for this client ID.
6443 *
6444 * Returns NFS4_OK if the FSID is present on this server,
6445 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6446 * NFS4ERR code if some error occurred on the server, or a
6447 * negative errno if a local failure occurred.
6448 */
6449int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6450{
6451 struct nfs_server *server = NFS_SERVER(inode);
6452 struct nfs_client *clp = server->nfs_client;
6453 const struct nfs4_mig_recovery_ops *ops =
6454 clp->cl_mvops->mig_recovery_ops;
6455 struct nfs4_exception exception = { };
6456 int status;
6457
6458 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6459 (unsigned long long)server->fsid.major,
6460 (unsigned long long)server->fsid.minor,
6461 clp->cl_hostname);
6462 nfs_display_fhandle(NFS_FH(inode), __func__);
6463
6464 do {
6465 status = ops->fsid_present(inode, cred);
6466 if (status != -NFS4ERR_DELAY)
6467 break;
6468 nfs4_handle_exception(server, status, &exception);
6469 } while (exception.retry);
6470 return status;
6471}
6472
Andy Adamson5ec16a82013-08-08 10:57:55 -04006473/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006474 * If 'use_integrity' is true and the state managment nfs_client
6475 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6476 * and the machine credential as per RFC3530bis and RFC5661 Security
6477 * Considerations sections. Otherwise, just use the user cred with the
6478 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006479 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006480static 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 +00006481{
6482 int status;
6483 struct nfs4_secinfo_arg args = {
6484 .dir_fh = NFS_FH(dir),
6485 .name = name,
6486 };
6487 struct nfs4_secinfo_res res = {
6488 .flavors = flavors,
6489 };
6490 struct rpc_message msg = {
6491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6492 .rpc_argp = &args,
6493 .rpc_resp = &res,
6494 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006495 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006496 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006497
6498 if (use_integrity) {
6499 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006500 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6501 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006502 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006503
6504 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006505
6506 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6507 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6508
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006509 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6510 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006511 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006512
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006513 if (cred)
6514 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006515
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006516 return status;
6517}
6518
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006519int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6520 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006521{
6522 struct nfs4_exception exception = { };
6523 int err;
6524 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006525 err = -NFS4ERR_WRONGSEC;
6526
6527 /* try to use integrity protection with machine cred */
6528 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6529 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6530
6531 /*
6532 * if unable to use integrity protection, or SECINFO with
6533 * integrity protection returns NFS4ERR_WRONGSEC (which is
6534 * disallowed by spec, but exists in deployed servers) use
6535 * the current filesystem's rpc_client and the user cred.
6536 */
6537 if (err == -NFS4ERR_WRONGSEC)
6538 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6539
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006540 trace_nfs4_secinfo(dir, name, err);
6541 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006542 &exception);
6543 } while (exception.retry);
6544 return err;
6545}
6546
Andy Adamson557134a2009-04-01 09:21:53 -04006547#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006548/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006549 * Check the exchange flags returned by the server for invalid flags, having
6550 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6551 * DS flags set.
6552 */
6553static int nfs4_check_cl_exchange_flags(u32 flags)
6554{
6555 if (flags & ~EXCHGID4_FLAG_MASK_R)
6556 goto out_inval;
6557 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6558 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6559 goto out_inval;
6560 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6561 goto out_inval;
6562 return NFS_OK;
6563out_inval:
6564 return -NFS4ERR_INVAL;
6565}
6566
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006567static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006568nfs41_same_server_scope(struct nfs41_server_scope *a,
6569 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006570{
6571 if (a->server_scope_sz == b->server_scope_sz &&
6572 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6573 return true;
6574
6575 return false;
6576}
6577
Andy Adamson357f54d2010-12-14 10:11:57 -05006578/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006579 * nfs4_proc_bind_conn_to_session()
6580 *
6581 * The 4.1 client currently uses the same TCP connection for the
6582 * fore and backchannel.
6583 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006584int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006585{
6586 int status;
6587 struct nfs41_bind_conn_to_session_res res;
6588 struct rpc_message msg = {
6589 .rpc_proc =
6590 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6591 .rpc_argp = clp,
6592 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006593 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006594 };
6595
6596 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006597
6598 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6599 if (unlikely(res.session == NULL)) {
6600 status = -ENOMEM;
6601 goto out;
6602 }
6603
6604 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006605 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006606 if (status == 0) {
6607 if (memcmp(res.session->sess_id.data,
6608 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6609 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6610 status = -EIO;
6611 goto out_session;
6612 }
6613 if (res.dir != NFS4_CDFS4_BOTH) {
6614 dprintk("NFS: %s: Unexpected direction from server\n",
6615 __func__);
6616 status = -EIO;
6617 goto out_session;
6618 }
6619 if (res.use_conn_in_rdma_mode) {
6620 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6621 __func__);
6622 status = -EIO;
6623 goto out_session;
6624 }
6625 }
6626out_session:
6627 kfree(res.session);
6628out:
6629 dprintk("<-- %s status= %d\n", __func__, status);
6630 return status;
6631}
6632
6633/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006634 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6635 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006636 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006637static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6638 .how = SP4_MACH_CRED,
6639 .enforce.u.words = {
6640 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6641 1 << (OP_EXCHANGE_ID - 32) |
6642 1 << (OP_CREATE_SESSION - 32) |
6643 1 << (OP_DESTROY_SESSION - 32) |
6644 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006645 },
6646 .allow.u.words = {
6647 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006648 1 << (OP_LOCKU) |
6649 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006650 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006651 1 << (OP_SECINFO_NO_NAME - 32) |
6652 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006653 1 << (OP_FREE_STATEID - 32) |
6654 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006655 }
6656};
6657
6658/*
6659 * Select the state protection mode for client `clp' given the server results
6660 * from exchange_id in `sp'.
6661 *
6662 * Returns 0 on success, negative errno otherwise.
6663 */
6664static int nfs4_sp4_select_mode(struct nfs_client *clp,
6665 struct nfs41_state_protection *sp)
6666{
6667 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6668 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6669 1 << (OP_EXCHANGE_ID - 32) |
6670 1 << (OP_CREATE_SESSION - 32) |
6671 1 << (OP_DESTROY_SESSION - 32) |
6672 1 << (OP_DESTROY_CLIENTID - 32)
6673 };
6674 unsigned int i;
6675
6676 if (sp->how == SP4_MACH_CRED) {
6677 /* Print state protect result */
6678 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6679 for (i = 0; i <= LAST_NFS4_OP; i++) {
6680 if (test_bit(i, sp->enforce.u.longs))
6681 dfprintk(MOUNT, " enforce op %d\n", i);
6682 if (test_bit(i, sp->allow.u.longs))
6683 dfprintk(MOUNT, " allow op %d\n", i);
6684 }
6685
6686 /* make sure nothing is on enforce list that isn't supported */
6687 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6688 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6689 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6690 return -EINVAL;
6691 }
6692 }
6693
6694 /*
6695 * Minimal mode - state operations are allowed to use machine
6696 * credential. Note this already happens by default, so the
6697 * client doesn't have to do anything more than the negotiation.
6698 *
6699 * NOTE: we don't care if EXCHANGE_ID is in the list -
6700 * we're already using the machine cred for exchange_id
6701 * and will never use a different cred.
6702 */
6703 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6704 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6705 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6706 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6707 dfprintk(MOUNT, "sp4_mach_cred:\n");
6708 dfprintk(MOUNT, " minimal mode enabled\n");
6709 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6710 } else {
6711 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6712 return -EINVAL;
6713 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006714
6715 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6716 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6717 dfprintk(MOUNT, " cleanup mode enabled\n");
6718 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6719 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006720
6721 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6722 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6723 dfprintk(MOUNT, " secinfo mode enabled\n");
6724 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6725 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006726
6727 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6728 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6729 dfprintk(MOUNT, " stateid mode enabled\n");
6730 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6731 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006732
6733 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6734 dfprintk(MOUNT, " write mode enabled\n");
6735 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6736 }
6737
6738 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6739 dfprintk(MOUNT, " commit mode enabled\n");
6740 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6741 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006742 }
6743
6744 return 0;
6745}
6746
6747/*
6748 * _nfs4_proc_exchange_id()
6749 *
6750 * Wrapper for EXCHANGE_ID operation.
6751 */
6752static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6753 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006754{
6755 nfs4_verifier verifier;
6756 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006757 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006758 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006759#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006760 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006761 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6762 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6763#else
6764 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6765 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6766#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006767 };
6768 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006769 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006770 };
6771 int status;
6772 struct rpc_message msg = {
6773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6774 .rpc_argp = &args,
6775 .rpc_resp = &res,
6776 .rpc_cred = cred,
6777 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006778
Chuck Leverf0920752012-05-21 22:45:41 -04006779 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006780 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6781 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006782 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6783 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6784 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006785
Chuck Leveracdeb692012-05-21 22:46:16 -04006786 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006787 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006788 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006789 status = -ENOMEM;
6790 goto out;
6791 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006792
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006793 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006794 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006795 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006796 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006797 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006798 }
6799
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006800 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006801 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006802 status = -ENOMEM;
6803 goto out_server_scope;
6804 }
6805
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006806 switch (sp4_how) {
6807 case SP4_NONE:
6808 args.state_protect.how = SP4_NONE;
6809 break;
6810
6811 case SP4_MACH_CRED:
6812 args.state_protect = nfs4_sp4_mach_cred_request;
6813 break;
6814
6815 default:
6816 /* unsupported! */
6817 WARN_ON_ONCE(1);
6818 status = -EINVAL;
6819 goto out_server_scope;
6820 }
6821
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006822 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006823 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006824 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006825 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006826
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006827 if (status == 0)
6828 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6829
Chuck Lever177313f2012-05-21 22:44:58 -04006830 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006831 clp->cl_clientid = res.clientid;
6832 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6833 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6834 clp->cl_seqid = res.seqid;
6835
Chuck Leveracdeb692012-05-21 22:46:16 -04006836 kfree(clp->cl_serverowner);
6837 clp->cl_serverowner = res.server_owner;
6838 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006839
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006840 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006841 kfree(clp->cl_implid);
6842 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006843
Chuck Lever177313f2012-05-21 22:44:58 -04006844 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006845 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006846 res.server_scope)) {
6847 dprintk("%s: server_scope mismatch detected\n",
6848 __func__);
6849 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006850 kfree(clp->cl_serverscope);
6851 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006852 }
6853
Chuck Lever177313f2012-05-21 22:44:58 -04006854 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006855 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006856 goto out;
6857 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006858 } else
6859 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006860
Chuck Leveracdeb692012-05-21 22:46:16 -04006861out_server_owner:
6862 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006863out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006864 kfree(res.server_scope);
6865out:
Chuck Lever177313f2012-05-21 22:44:58 -04006866 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006867 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006868 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006869 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006870 clp->cl_implid->date.seconds,
6871 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006872 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006873 return status;
6874}
6875
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006876/*
6877 * nfs4_proc_exchange_id()
6878 *
6879 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6880 *
6881 * Since the clientid has expired, all compounds using sessions
6882 * associated with the stale clientid will be returning
6883 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6884 * be in some phase of session reset.
6885 *
6886 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6887 */
6888int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6889{
6890 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6891 int status;
6892
6893 /* try SP4_MACH_CRED if krb5i/p */
6894 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6895 authflavor == RPC_AUTH_GSS_KRB5P) {
6896 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6897 if (!status)
6898 return 0;
6899 }
6900
6901 /* try SP4_NONE */
6902 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6903}
6904
Trond Myklebust66245532012-05-25 17:18:09 -04006905static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6906 struct rpc_cred *cred)
6907{
6908 struct rpc_message msg = {
6909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6910 .rpc_argp = clp,
6911 .rpc_cred = cred,
6912 };
6913 int status;
6914
6915 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006916 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006917 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006918 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006919 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6920 return status;
6921}
6922
6923static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6924 struct rpc_cred *cred)
6925{
6926 unsigned int loop;
6927 int ret;
6928
6929 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6930 ret = _nfs4_proc_destroy_clientid(clp, cred);
6931 switch (ret) {
6932 case -NFS4ERR_DELAY:
6933 case -NFS4ERR_CLIENTID_BUSY:
6934 ssleep(1);
6935 break;
6936 default:
6937 return ret;
6938 }
6939 }
6940 return 0;
6941}
6942
6943int nfs4_destroy_clientid(struct nfs_client *clp)
6944{
6945 struct rpc_cred *cred;
6946 int ret = 0;
6947
6948 if (clp->cl_mvops->minor_version < 1)
6949 goto out;
6950 if (clp->cl_exchange_flags == 0)
6951 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006952 if (clp->cl_preserve_clid)
6953 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006954 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006955 ret = nfs4_proc_destroy_clientid(clp, cred);
6956 if (cred)
6957 put_rpccred(cred);
6958 switch (ret) {
6959 case 0:
6960 case -NFS4ERR_STALE_CLIENTID:
6961 clp->cl_exchange_flags = 0;
6962 }
6963out:
6964 return ret;
6965}
6966
Andy Adamson2050f0c2009-04-01 09:22:30 -04006967struct nfs4_get_lease_time_data {
6968 struct nfs4_get_lease_time_args *args;
6969 struct nfs4_get_lease_time_res *res;
6970 struct nfs_client *clp;
6971};
6972
6973static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6974 void *calldata)
6975{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006976 struct nfs4_get_lease_time_data *data =
6977 (struct nfs4_get_lease_time_data *)calldata;
6978
6979 dprintk("--> %s\n", __func__);
6980 /* just setup sequence, do not trigger session recovery
6981 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006982 nfs41_setup_sequence(data->clp->cl_session,
6983 &data->args->la_seq_args,
6984 &data->res->lr_seq_res,
6985 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006986 dprintk("<-- %s\n", __func__);
6987}
6988
6989/*
6990 * Called from nfs4_state_manager thread for session setup, so don't recover
6991 * from sequence operation or clientid errors.
6992 */
6993static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6994{
6995 struct nfs4_get_lease_time_data *data =
6996 (struct nfs4_get_lease_time_data *)calldata;
6997
6998 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006999 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7000 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007001 switch (task->tk_status) {
7002 case -NFS4ERR_DELAY:
7003 case -NFS4ERR_GRACE:
7004 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7005 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7006 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007007 /* fall through */
7008 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007009 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007010 return;
7011 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007012 dprintk("<-- %s\n", __func__);
7013}
7014
Trond Myklebust17280172012-03-11 13:11:00 -04007015static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007016 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7017 .rpc_call_done = nfs4_get_lease_time_done,
7018};
7019
7020int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7021{
7022 struct rpc_task *task;
7023 struct nfs4_get_lease_time_args args;
7024 struct nfs4_get_lease_time_res res = {
7025 .lr_fsinfo = fsinfo,
7026 };
7027 struct nfs4_get_lease_time_data data = {
7028 .args = &args,
7029 .res = &res,
7030 .clp = clp,
7031 };
7032 struct rpc_message msg = {
7033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7034 .rpc_argp = &args,
7035 .rpc_resp = &res,
7036 };
7037 struct rpc_task_setup task_setup = {
7038 .rpc_client = clp->cl_rpcclient,
7039 .rpc_message = &msg,
7040 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007041 .callback_data = &data,
7042 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007043 };
7044 int status;
7045
Chuck Levera9c92d62013-08-09 12:48:18 -04007046 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007047 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007048 dprintk("--> %s\n", __func__);
7049 task = rpc_run_task(&task_setup);
7050
7051 if (IS_ERR(task))
7052 status = PTR_ERR(task);
7053 else {
7054 status = task->tk_status;
7055 rpc_put_task(task);
7056 }
7057 dprintk("<-- %s return %d\n", __func__, status);
7058
7059 return status;
7060}
7061
Andy Adamsonfc931582009-04-01 09:22:31 -04007062/*
7063 * Initialize the values to be used by the client in CREATE_SESSION
7064 * If nfs4_init_session set the fore channel request and response sizes,
7065 * use them.
7066 *
7067 * Set the back channel max_resp_sz_cached to zero to force the client to
7068 * always set csa_cachethis to FALSE because the current implementation
7069 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7070 */
7071static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7072{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007073 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007074
Andy Adamson18aad3d2013-06-26 12:21:49 -04007075 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7076 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7077
Andy Adamsonfc931582009-04-01 09:22:31 -04007078 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007079 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7080 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007081 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007082 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007083
7084 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007085 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007086 __func__,
7087 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007088 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007089
7090 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007091 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7092 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7093 args->bc_attrs.max_resp_sz_cached = 0;
7094 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7095 args->bc_attrs.max_reqs = 1;
7096
7097 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7098 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7099 __func__,
7100 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7101 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7102 args->bc_attrs.max_reqs);
7103}
7104
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007105static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04007106{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007107 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7108 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
7109
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007110 if (rcvd->max_resp_sz > sent->max_resp_sz)
7111 return -EINVAL;
7112 /*
7113 * Our requested max_ops is the minimum we need; we're not
7114 * prepared to break up compounds into smaller pieces than that.
7115 * So, no point even trying to continue if the server won't
7116 * cooperate:
7117 */
7118 if (rcvd->max_ops < sent->max_ops)
7119 return -EINVAL;
7120 if (rcvd->max_reqs == 0)
7121 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007122 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7123 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007124 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007125}
7126
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007127static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7128{
7129 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7130 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007131
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007132 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7133 return -EINVAL;
7134 if (rcvd->max_resp_sz < sent->max_resp_sz)
7135 return -EINVAL;
7136 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7137 return -EINVAL;
7138 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007139 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007140 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007141 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007142 return -EINVAL;
7143 return 0;
7144}
Andy Adamson8d353012009-04-01 09:22:32 -04007145
Andy Adamson8d353012009-04-01 09:22:32 -04007146static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7147 struct nfs4_session *session)
7148{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007149 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007150
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007151 ret = nfs4_verify_fore_channel_attrs(args, session);
7152 if (ret)
7153 return ret;
7154 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04007155}
7156
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007157static int _nfs4_proc_create_session(struct nfs_client *clp,
7158 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007159{
7160 struct nfs4_session *session = clp->cl_session;
7161 struct nfs41_create_session_args args = {
7162 .client = clp,
7163 .cb_program = NFS4_CALLBACK,
7164 };
7165 struct nfs41_create_session_res res = {
7166 .client = clp,
7167 };
7168 struct rpc_message msg = {
7169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7170 .rpc_argp = &args,
7171 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007172 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007173 };
7174 int status;
7175
7176 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007177 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007178
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007179 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007180 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007181
Trond Myklebust43095d32012-11-20 11:13:12 -05007182 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007183 /* Verify the session's negotiated channel_attrs values */
7184 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04007185 /* Increment the clientid slot sequence id */
7186 clp->cl_seqid++;
7187 }
7188
7189 return status;
7190}
7191
7192/*
7193 * Issues a CREATE_SESSION operation to the server.
7194 * It is the responsibility of the caller to verify the session is
7195 * expired before calling this routine.
7196 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007197int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007198{
7199 int status;
7200 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007201 struct nfs4_session *session = clp->cl_session;
7202
7203 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7204
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007205 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007206 if (status)
7207 goto out;
7208
Andy Adamsonaacd5532011-11-09 13:58:21 -05007209 /* Init or reset the session slot tables */
7210 status = nfs4_setup_session_slot_tables(session);
7211 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007212 if (status)
7213 goto out;
7214
7215 ptr = (unsigned *)&session->sess_id.data[0];
7216 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7217 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007218out:
7219 dprintk("<-- %s\n", __func__);
7220 return status;
7221}
7222
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007223/*
7224 * Issue the over-the-wire RPC DESTROY_SESSION.
7225 * The caller must serialize access to this routine.
7226 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007227int nfs4_proc_destroy_session(struct nfs4_session *session,
7228 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007229{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007230 struct rpc_message msg = {
7231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7232 .rpc_argp = session,
7233 .rpc_cred = cred,
7234 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007235 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007236
7237 dprintk("--> nfs4_proc_destroy_session\n");
7238
7239 /* session is still being setup */
7240 if (session->clp->cl_cons_state != NFS_CS_READY)
7241 return status;
7242
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007243 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007244 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007245
7246 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007247 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007248 "Session has been destroyed regardless...\n", status);
7249
7250 dprintk("<-- nfs4_proc_destroy_session\n");
7251 return status;
7252}
7253
Trond Myklebust7b38c362012-05-23 13:23:31 -04007254/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007255 * Renew the cl_session lease.
7256 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007257struct nfs4_sequence_data {
7258 struct nfs_client *clp;
7259 struct nfs4_sequence_args args;
7260 struct nfs4_sequence_res res;
7261};
7262
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007263static void nfs41_sequence_release(void *data)
7264{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007265 struct nfs4_sequence_data *calldata = data;
7266 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007267
Alexandros Batsakis71358402010-02-05 03:45:05 -08007268 if (atomic_read(&clp->cl_count) > 1)
7269 nfs4_schedule_state_renewal(clp);
7270 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007271 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007272}
7273
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007274static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7275{
7276 switch(task->tk_status) {
7277 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007278 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7279 return -EAGAIN;
7280 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007281 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007282 }
7283 return 0;
7284}
7285
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007286static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007287{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007288 struct nfs4_sequence_data *calldata = data;
7289 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007290
Trond Myklebust14516c32010-07-31 14:29:06 -04007291 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7292 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007293
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007294 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007295 if (task->tk_status < 0) {
7296 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007297 if (atomic_read(&clp->cl_count) == 1)
7298 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007299
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007300 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7301 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007302 return;
7303 }
7304 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007305 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007306out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007307 dprintk("<-- %s\n", __func__);
7308}
7309
7310static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7311{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007312 struct nfs4_sequence_data *calldata = data;
7313 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007314 struct nfs4_sequence_args *args;
7315 struct nfs4_sequence_res *res;
7316
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007317 args = task->tk_msg.rpc_argp;
7318 res = task->tk_msg.rpc_resp;
7319
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007320 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007321}
7322
7323static const struct rpc_call_ops nfs41_sequence_ops = {
7324 .rpc_call_done = nfs41_sequence_call_done,
7325 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007326 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007327};
7328
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007329static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7330 struct rpc_cred *cred,
7331 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007332{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007333 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007334 struct rpc_message msg = {
7335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7336 .rpc_cred = cred,
7337 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007338 struct rpc_task_setup task_setup_data = {
7339 .rpc_client = clp->cl_rpcclient,
7340 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007341 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007342 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007343 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007344
Alexandros Batsakis71358402010-02-05 03:45:05 -08007345 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007346 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007347 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007348 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007349 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007350 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007351 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007352 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007353 if (is_privileged)
7354 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007355 msg.rpc_argp = &calldata->args;
7356 msg.rpc_resp = &calldata->res;
7357 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007358 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007359
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007360 return rpc_run_task(&task_setup_data);
7361}
7362
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007363static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007364{
7365 struct rpc_task *task;
7366 int ret = 0;
7367
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007368 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007369 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007370 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007371 if (IS_ERR(task))
7372 ret = PTR_ERR(task);
7373 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007374 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007375 dprintk("<-- %s status=%d\n", __func__, ret);
7376 return ret;
7377}
7378
7379static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7380{
7381 struct rpc_task *task;
7382 int ret;
7383
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007384 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007385 if (IS_ERR(task)) {
7386 ret = PTR_ERR(task);
7387 goto out;
7388 }
7389 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007390 if (!ret) {
7391 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7392
7393 if (task->tk_status == 0)
7394 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007395 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007396 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007397 rpc_put_task(task);
7398out:
7399 dprintk("<-- %s status=%d\n", __func__, ret);
7400 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007401}
7402
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007403struct nfs4_reclaim_complete_data {
7404 struct nfs_client *clp;
7405 struct nfs41_reclaim_complete_args arg;
7406 struct nfs41_reclaim_complete_res res;
7407};
7408
7409static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7410{
7411 struct nfs4_reclaim_complete_data *calldata = data;
7412
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007413 nfs41_setup_sequence(calldata->clp->cl_session,
7414 &calldata->arg.seq_args,
7415 &calldata->res.seq_res,
7416 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007417}
7418
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007419static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7420{
7421 switch(task->tk_status) {
7422 case 0:
7423 case -NFS4ERR_COMPLETE_ALREADY:
7424 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7425 break;
7426 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007427 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007428 /* fall through */
7429 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007430 return -EAGAIN;
7431 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007432 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007433 }
7434 return 0;
7435}
7436
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007437static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7438{
7439 struct nfs4_reclaim_complete_data *calldata = data;
7440 struct nfs_client *clp = calldata->clp;
7441 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7442
7443 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007444 if (!nfs41_sequence_done(task, res))
7445 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007446
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007447 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007448 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7449 rpc_restart_call_prepare(task);
7450 return;
7451 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007452 dprintk("<-- %s\n", __func__);
7453}
7454
7455static void nfs4_free_reclaim_complete_data(void *data)
7456{
7457 struct nfs4_reclaim_complete_data *calldata = data;
7458
7459 kfree(calldata);
7460}
7461
7462static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7463 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7464 .rpc_call_done = nfs4_reclaim_complete_done,
7465 .rpc_release = nfs4_free_reclaim_complete_data,
7466};
7467
7468/*
7469 * Issue a global reclaim complete.
7470 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007471static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7472 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007473{
7474 struct nfs4_reclaim_complete_data *calldata;
7475 struct rpc_task *task;
7476 struct rpc_message msg = {
7477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007478 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007479 };
7480 struct rpc_task_setup task_setup_data = {
7481 .rpc_client = clp->cl_rpcclient,
7482 .rpc_message = &msg,
7483 .callback_ops = &nfs4_reclaim_complete_call_ops,
7484 .flags = RPC_TASK_ASYNC,
7485 };
7486 int status = -ENOMEM;
7487
7488 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007489 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007490 if (calldata == NULL)
7491 goto out;
7492 calldata->clp = clp;
7493 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007494
Chuck Levera9c92d62013-08-09 12:48:18 -04007495 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007496 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007497 msg.rpc_argp = &calldata->arg;
7498 msg.rpc_resp = &calldata->res;
7499 task_setup_data.callback_data = calldata;
7500 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007501 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007502 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007503 goto out;
7504 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007505 status = nfs4_wait_for_completion_rpc_task(task);
7506 if (status == 0)
7507 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007508 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007509 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007510out:
7511 dprintk("<-- %s status=%d\n", __func__, status);
7512 return status;
7513}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007514
7515static void
7516nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7517{
7518 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007519 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007520 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007521
7522 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007523 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7524 * right now covering the LAYOUTGET we are about to send.
7525 * However, that is not so catastrophic, and there seems
7526 * to be no way to prevent it completely.
7527 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007528 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007529 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007530 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007531 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7532 NFS_I(lgp->args.inode)->layout,
7533 lgp->args.ctx->state)) {
7534 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007535 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007536}
7537
7538static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7539{
7540 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007541 struct inode *inode = lgp->args.inode;
7542 struct nfs_server *server = NFS_SERVER(inode);
7543 struct pnfs_layout_hdr *lo;
7544 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007545 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007546
Boaz Harroshed7e5422014-01-22 20:34:54 +02007547 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007548
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007549 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007550 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007551
7552 switch (task->tk_status) {
7553 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007554 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007555 /*
7556 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7557 * (or clients) writing to the same RAID stripe
7558 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007559 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007560 /*
7561 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7562 * existing layout before getting a new one).
7563 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007564 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007565 timeo = rpc_get_timeout(task->tk_client);
7566 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007567 now = jiffies;
7568 if (time_after(giveup, now)) {
7569 unsigned long delay;
7570
7571 /* Delay for:
7572 * - Not less then NFS4_POLL_RETRY_MIN.
7573 * - One last time a jiffie before we give up
7574 * - exponential backoff (time_now minus start_attempt)
7575 */
7576 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7577 min((giveup - now - 1),
7578 now - lgp->args.timestamp));
7579
7580 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7581 __func__, delay);
7582 rpc_delay(task, delay);
7583 task->tk_status = 0;
7584 rpc_restart_call_prepare(task);
7585 goto out; /* Do not call nfs4_async_handle_error() */
7586 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007587 break;
7588 case -NFS4ERR_EXPIRED:
7589 case -NFS4ERR_BAD_STATEID:
7590 spin_lock(&inode->i_lock);
7591 lo = NFS_I(inode)->layout;
7592 if (!lo || list_empty(&lo->plh_segs)) {
7593 spin_unlock(&inode->i_lock);
7594 /* If the open stateid was bad, then recover it. */
7595 state = lgp->args.ctx->state;
7596 } else {
7597 LIST_HEAD(head);
7598
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007599 /*
7600 * Mark the bad layout state as invalid, then retry
7601 * with the current stateid.
7602 */
Trond Myklebustee314c22012-10-01 17:25:48 -07007603 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7604 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007605 pnfs_free_lseg_list(&head);
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007606
7607 task->tk_status = 0;
7608 rpc_restart_call_prepare(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007609 }
7610 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007611 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebustee314c22012-10-01 17:25:48 -07007612 rpc_restart_call_prepare(task);
7613out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007614 dprintk("<-- %s\n", __func__);
7615}
7616
Idan Kedar85541162012-08-02 11:47:10 +03007617static size_t max_response_pages(struct nfs_server *server)
7618{
7619 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7620 return nfs_page_array_len(0, max_resp_sz);
7621}
7622
7623static void nfs4_free_pages(struct page **pages, size_t size)
7624{
7625 int i;
7626
7627 if (!pages)
7628 return;
7629
7630 for (i = 0; i < size; i++) {
7631 if (!pages[i])
7632 break;
7633 __free_page(pages[i]);
7634 }
7635 kfree(pages);
7636}
7637
7638static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7639{
7640 struct page **pages;
7641 int i;
7642
7643 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7644 if (!pages) {
7645 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7646 return NULL;
7647 }
7648
7649 for (i = 0; i < size; i++) {
7650 pages[i] = alloc_page(gfp_flags);
7651 if (!pages[i]) {
7652 dprintk("%s: failed to allocate page\n", __func__);
7653 nfs4_free_pages(pages, size);
7654 return NULL;
7655 }
7656 }
7657
7658 return pages;
7659}
7660
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007661static void nfs4_layoutget_release(void *calldata)
7662{
7663 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007664 struct inode *inode = lgp->args.inode;
7665 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007666 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007667
7668 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007669 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007670 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007671 put_nfs_open_context(lgp->args.ctx);
7672 kfree(calldata);
7673 dprintk("<-- %s\n", __func__);
7674}
7675
7676static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7677 .rpc_call_prepare = nfs4_layoutget_prepare,
7678 .rpc_call_done = nfs4_layoutget_done,
7679 .rpc_release = nfs4_layoutget_release,
7680};
7681
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007682struct pnfs_layout_segment *
7683nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007684{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007685 struct inode *inode = lgp->args.inode;
7686 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007687 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007688 struct rpc_task *task;
7689 struct rpc_message msg = {
7690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7691 .rpc_argp = &lgp->args,
7692 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007693 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007694 };
7695 struct rpc_task_setup task_setup_data = {
7696 .rpc_client = server->client,
7697 .rpc_message = &msg,
7698 .callback_ops = &nfs4_layoutget_call_ops,
7699 .callback_data = lgp,
7700 .flags = RPC_TASK_ASYNC,
7701 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007702 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007703 int status = 0;
7704
7705 dprintk("--> %s\n", __func__);
7706
Idan Kedar85541162012-08-02 11:47:10 +03007707 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7708 if (!lgp->args.layout.pages) {
7709 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007710 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007711 }
7712 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007713 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007714
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007715 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007716 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007717 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007718
7719 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7720 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7721
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007722 task = rpc_run_task(&task_setup_data);
7723 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007724 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007725 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007726 if (status == 0)
7727 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007728 trace_nfs4_layoutget(lgp->args.ctx,
7729 &lgp->args.range,
7730 &lgp->res.range,
7731 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007732 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7733 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007734 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007735 rpc_put_task(task);
7736 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007737 if (status)
7738 return ERR_PTR(status);
7739 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007740}
7741
Benny Halevycbe82602011-05-22 19:52:37 +03007742static void
7743nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7744{
7745 struct nfs4_layoutreturn *lrp = calldata;
7746
7747 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007748 nfs41_setup_sequence(lrp->clp->cl_session,
7749 &lrp->args.seq_args,
7750 &lrp->res.seq_res,
7751 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007752}
7753
7754static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7755{
7756 struct nfs4_layoutreturn *lrp = calldata;
7757 struct nfs_server *server;
7758
7759 dprintk("--> %s\n", __func__);
7760
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007761 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007762 return;
7763
7764 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007765 switch (task->tk_status) {
7766 default:
7767 task->tk_status = 0;
7768 case 0:
7769 break;
7770 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007771 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007772 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007773 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007774 return;
7775 }
Benny Halevycbe82602011-05-22 19:52:37 +03007776 dprintk("<-- %s\n", __func__);
7777}
7778
7779static void nfs4_layoutreturn_release(void *calldata)
7780{
7781 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007782 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007783
7784 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007785 spin_lock(&lo->plh_inode->i_lock);
7786 if (lrp->res.lrs_present)
7787 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7788 lo->plh_block_lgets--;
7789 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007790 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007791 kfree(calldata);
7792 dprintk("<-- %s\n", __func__);
7793}
7794
7795static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7796 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7797 .rpc_call_done = nfs4_layoutreturn_done,
7798 .rpc_release = nfs4_layoutreturn_release,
7799};
7800
7801int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7802{
7803 struct rpc_task *task;
7804 struct rpc_message msg = {
7805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7806 .rpc_argp = &lrp->args,
7807 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007808 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007809 };
7810 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007811 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007812 .rpc_message = &msg,
7813 .callback_ops = &nfs4_layoutreturn_call_ops,
7814 .callback_data = lrp,
7815 };
7816 int status;
7817
7818 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007819 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007820 task = rpc_run_task(&task_setup_data);
7821 if (IS_ERR(task))
7822 return PTR_ERR(task);
7823 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007824 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007825 dprintk("<-- %s status=%d\n", __func__, status);
7826 rpc_put_task(task);
7827 return status;
7828}
7829
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007830static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007831_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7832 struct pnfs_device *pdev,
7833 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007834{
7835 struct nfs4_getdeviceinfo_args args = {
7836 .pdev = pdev,
7837 };
7838 struct nfs4_getdeviceinfo_res res = {
7839 .pdev = pdev,
7840 };
7841 struct rpc_message msg = {
7842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7843 .rpc_argp = &args,
7844 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007845 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007846 };
7847 int status;
7848
7849 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007850 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007851 dprintk("<-- %s status=%d\n", __func__, status);
7852
7853 return status;
7854}
7855
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007856int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7857 struct pnfs_device *pdev,
7858 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007859{
7860 struct nfs4_exception exception = { };
7861 int err;
7862
7863 do {
7864 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007865 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007866 &exception);
7867 } while (exception.retry);
7868 return err;
7869}
7870EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7871
Andy Adamson863a3c62011-03-23 13:27:54 +00007872static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7873{
7874 struct nfs4_layoutcommit_data *data = calldata;
7875 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007876 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007877
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007878 nfs41_setup_sequence(session,
7879 &data->args.seq_args,
7880 &data->res.seq_res,
7881 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007882}
7883
7884static void
7885nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7886{
7887 struct nfs4_layoutcommit_data *data = calldata;
7888 struct nfs_server *server = NFS_SERVER(data->args.inode);
7889
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007890 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007891 return;
7892
7893 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007894 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7895 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7896 case -NFS4ERR_BADLAYOUT: /* no layout */
7897 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007898 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007899 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007900 break;
7901 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10007902 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007903 rpc_restart_call_prepare(task);
7904 return;
7905 }
7906 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007907}
7908
7909static void nfs4_layoutcommit_release(void *calldata)
7910{
7911 struct nfs4_layoutcommit_data *data = calldata;
7912
Andy Adamsondb29c082011-07-30 20:52:38 -04007913 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05007914 nfs_post_op_update_inode_force_wcc(data->args.inode,
7915 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00007916 put_rpccred(data->cred);
7917 kfree(data);
7918}
7919
7920static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7921 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7922 .rpc_call_done = nfs4_layoutcommit_done,
7923 .rpc_release = nfs4_layoutcommit_release,
7924};
7925
7926int
Andy Adamsonef311532011-03-12 02:58:10 -05007927nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007928{
7929 struct rpc_message msg = {
7930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7931 .rpc_argp = &data->args,
7932 .rpc_resp = &data->res,
7933 .rpc_cred = data->cred,
7934 };
7935 struct rpc_task_setup task_setup_data = {
7936 .task = &data->task,
7937 .rpc_client = NFS_CLIENT(data->args.inode),
7938 .rpc_message = &msg,
7939 .callback_ops = &nfs4_layoutcommit_ops,
7940 .callback_data = data,
7941 .flags = RPC_TASK_ASYNC,
7942 };
7943 struct rpc_task *task;
7944 int status = 0;
7945
7946 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7947 "lbw: %llu inode %lu\n",
7948 data->task.tk_pid, sync,
7949 data->args.lastbytewritten,
7950 data->args.inode->i_ino);
7951
Chuck Levera9c92d62013-08-09 12:48:18 -04007952 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007953 task = rpc_run_task(&task_setup_data);
7954 if (IS_ERR(task))
7955 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007956 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007957 goto out;
7958 status = nfs4_wait_for_completion_rpc_task(task);
7959 if (status != 0)
7960 goto out;
7961 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007962 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007963out:
7964 dprintk("%s: status %d\n", __func__, status);
7965 rpc_put_task(task);
7966 return status;
7967}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007968
Andy Adamson97431202013-08-08 10:57:56 -04007969/**
7970 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7971 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7972 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007973static int
7974_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007975 struct nfs_fsinfo *info,
7976 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007977{
7978 struct nfs41_secinfo_no_name_args args = {
7979 .style = SECINFO_STYLE_CURRENT_FH,
7980 };
7981 struct nfs4_secinfo_res res = {
7982 .flavors = flavors,
7983 };
7984 struct rpc_message msg = {
7985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7986 .rpc_argp = &args,
7987 .rpc_resp = &res,
7988 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007989 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007990 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007991 int status;
7992
7993 if (use_integrity) {
7994 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007995 cred = nfs4_get_clid_cred(server->nfs_client);
7996 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007997 }
7998
7999 dprintk("--> %s\n", __func__);
8000 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8001 &res.seq_res, 0);
8002 dprintk("<-- %s status=%d\n", __func__, status);
8003
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008004 if (cred)
8005 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008006
8007 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008008}
8009
8010static int
8011nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8012 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8013{
8014 struct nfs4_exception exception = { };
8015 int err;
8016 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008017 /* first try using integrity protection */
8018 err = -NFS4ERR_WRONGSEC;
8019
8020 /* try to use integrity protection with machine cred */
8021 if (_nfs4_is_integrity_protected(server->nfs_client))
8022 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8023 flavors, true);
8024
8025 /*
8026 * if unable to use integrity protection, or SECINFO with
8027 * integrity protection returns NFS4ERR_WRONGSEC (which is
8028 * disallowed by spec, but exists in deployed servers) use
8029 * the current filesystem's rpc_client and the user cred.
8030 */
8031 if (err == -NFS4ERR_WRONGSEC)
8032 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8033 flavors, false);
8034
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008035 switch (err) {
8036 case 0:
8037 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008038 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008039 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008040 default:
8041 err = nfs4_handle_exception(server, err, &exception);
8042 }
8043 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008044out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008045 return err;
8046}
8047
8048static int
8049nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8050 struct nfs_fsinfo *info)
8051{
8052 int err;
8053 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008054 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008055 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008056 struct nfs4_secinfo4 *secinfo;
8057 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008058
8059 page = alloc_page(GFP_KERNEL);
8060 if (!page) {
8061 err = -ENOMEM;
8062 goto out;
8063 }
8064
8065 flavors = page_address(page);
8066 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8067
8068 /*
8069 * Fall back on "guess and check" method if
8070 * the server doesn't support SECINFO_NO_NAME
8071 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008072 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008073 err = nfs4_find_root_sec(server, fhandle, info);
8074 goto out_freepage;
8075 }
8076 if (err)
8077 goto out_freepage;
8078
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008079 for (i = 0; i < flavors->num_flavors; i++) {
8080 secinfo = &flavors->flavors[i];
8081
8082 switch (secinfo->flavor) {
8083 case RPC_AUTH_NULL:
8084 case RPC_AUTH_UNIX:
8085 case RPC_AUTH_GSS:
8086 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8087 &secinfo->flavor_info);
8088 break;
8089 default:
8090 flavor = RPC_AUTH_MAXFLAVOR;
8091 break;
8092 }
8093
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008094 if (!nfs_auth_info_match(&server->auth_info, flavor))
8095 flavor = RPC_AUTH_MAXFLAVOR;
8096
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008097 if (flavor != RPC_AUTH_MAXFLAVOR) {
8098 err = nfs4_lookup_root_sec(server, fhandle,
8099 info, flavor);
8100 if (!err)
8101 break;
8102 }
8103 }
8104
8105 if (flavor == RPC_AUTH_MAXFLAVOR)
8106 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008107
8108out_freepage:
8109 put_page(page);
8110 if (err == -EACCES)
8111 return -EPERM;
8112out:
8113 return err;
8114}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008115
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008116static int _nfs41_test_stateid(struct nfs_server *server,
8117 nfs4_stateid *stateid,
8118 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008119{
8120 int status;
8121 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008122 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008123 };
8124 struct nfs41_test_stateid_res res;
8125 struct rpc_message msg = {
8126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8127 .rpc_argp = &args,
8128 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008129 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008130 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008131 struct rpc_clnt *rpc_client = server->client;
8132
8133 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8134 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008135
Chuck Lever38527b12012-07-11 16:30:23 -04008136 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008137 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008138 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008139 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008140 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008141 if (status != NFS_OK) {
8142 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008143 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008144 }
8145 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008146 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008147}
8148
Chuck Lever38527b12012-07-11 16:30:23 -04008149/**
8150 * nfs41_test_stateid - perform a TEST_STATEID operation
8151 *
8152 * @server: server / transport on which to perform the operation
8153 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008154 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008155 *
8156 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8157 * Otherwise a negative NFS4ERR value is returned if the operation
8158 * failed or the state ID is not currently valid.
8159 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008160static int nfs41_test_stateid(struct nfs_server *server,
8161 nfs4_stateid *stateid,
8162 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008163{
8164 struct nfs4_exception exception = { };
8165 int err;
8166 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008167 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008168 if (err != -NFS4ERR_DELAY)
8169 break;
8170 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008171 } while (exception.retry);
8172 return err;
8173}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008174
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008175struct nfs_free_stateid_data {
8176 struct nfs_server *server;
8177 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008178 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008179};
8180
8181static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8182{
8183 struct nfs_free_stateid_data *data = calldata;
8184 nfs41_setup_sequence(nfs4_get_session(data->server),
8185 &data->args.seq_args,
8186 &data->res.seq_res,
8187 task);
8188}
8189
8190static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8191{
8192 struct nfs_free_stateid_data *data = calldata;
8193
8194 nfs41_sequence_done(task, &data->res.seq_res);
8195
8196 switch (task->tk_status) {
8197 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008198 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008199 rpc_restart_call_prepare(task);
8200 }
8201}
8202
8203static void nfs41_free_stateid_release(void *calldata)
8204{
8205 kfree(calldata);
8206}
8207
Trond Myklebust17f26b12013-08-21 15:48:42 -04008208static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008209 .rpc_call_prepare = nfs41_free_stateid_prepare,
8210 .rpc_call_done = nfs41_free_stateid_done,
8211 .rpc_release = nfs41_free_stateid_release,
8212};
8213
8214static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8215 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008216 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008217 bool privileged)
8218{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008219 struct rpc_message msg = {
8220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008221 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008222 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008223 struct rpc_task_setup task_setup = {
8224 .rpc_client = server->client,
8225 .rpc_message = &msg,
8226 .callback_ops = &nfs41_free_stateid_ops,
8227 .flags = RPC_TASK_ASYNC,
8228 };
8229 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008230
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008231 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8232 &task_setup.rpc_client, &msg);
8233
Chuck Lever38527b12012-07-11 16:30:23 -04008234 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008235 data = kmalloc(sizeof(*data), GFP_NOFS);
8236 if (!data)
8237 return ERR_PTR(-ENOMEM);
8238 data->server = server;
8239 nfs4_stateid_copy(&data->args.stateid, stateid);
8240
8241 task_setup.callback_data = data;
8242
8243 msg.rpc_argp = &data->args;
8244 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008245 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008246 if (privileged)
8247 nfs4_set_sequence_privileged(&data->args.seq_args);
8248
8249 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008250}
8251
Chuck Lever38527b12012-07-11 16:30:23 -04008252/**
8253 * nfs41_free_stateid - perform a FREE_STATEID operation
8254 *
8255 * @server: server / transport on which to perform the operation
8256 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008257 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008258 *
8259 * Returns NFS_OK if the server freed "stateid". Otherwise a
8260 * negative NFS4ERR value is returned.
8261 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008262static int nfs41_free_stateid(struct nfs_server *server,
8263 nfs4_stateid *stateid,
8264 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008265{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008266 struct rpc_task *task;
8267 int ret;
8268
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008269 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008270 if (IS_ERR(task))
8271 return PTR_ERR(task);
8272 ret = rpc_wait_for_completion_task(task);
8273 if (!ret)
8274 ret = task->tk_status;
8275 rpc_put_task(task);
8276 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008277}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008278
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008279static void
8280nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008281{
8282 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008283 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008284
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008285 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008286 nfs4_free_lock_state(server, lsp);
8287 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008288 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008289 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008290}
8291
Trond Myklebust36281ca2012-03-04 18:13:56 -05008292static bool nfs41_match_stateid(const nfs4_stateid *s1,
8293 const nfs4_stateid *s2)
8294{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008295 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008296 return false;
8297
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008298 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008299 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008300 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008301 return true;
8302
8303 return false;
8304}
8305
Andy Adamson557134a2009-04-01 09:21:53 -04008306#endif /* CONFIG_NFS_V4_1 */
8307
Trond Myklebust36281ca2012-03-04 18:13:56 -05008308static bool nfs4_match_stateid(const nfs4_stateid *s1,
8309 const nfs4_stateid *s2)
8310{
Trond Myklebustf597c532012-03-04 18:13:56 -05008311 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008312}
8313
8314
Trond Myklebust17280172012-03-11 13:11:00 -04008315static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008316 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008317 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318 .recover_open = nfs4_open_reclaim,
8319 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008320 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008321 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008322};
8323
Andy Adamson591d71c2009-04-01 09:22:47 -04008324#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008325static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008326 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8327 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8328 .recover_open = nfs4_open_reclaim,
8329 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008330 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008331 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008332 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008333};
8334#endif /* CONFIG_NFS_V4_1 */
8335
Trond Myklebust17280172012-03-11 13:11:00 -04008336static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008337 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008338 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008339 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008341 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342};
8343
Andy Adamson591d71c2009-04-01 09:22:47 -04008344#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008345static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008346 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8347 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008348 .recover_open = nfs41_open_expired,
8349 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008350 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008351};
8352#endif /* CONFIG_NFS_V4_1 */
8353
Trond Myklebust17280172012-03-11 13:11:00 -04008354static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008355 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008356 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008357 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008358};
8359
8360#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008361static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008362 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008363 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008364 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008365};
8366#endif
8367
Chuck Leverec011fe2013-10-17 14:12:39 -04008368static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008369 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008370 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008371};
8372
8373#if defined(CONFIG_NFS_V4_1)
8374static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008375 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008376 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008377};
8378#endif /* CONFIG_NFS_V4_1 */
8379
Trond Myklebust97dc1352010-06-16 09:52:26 -04008380static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8381 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008382 .init_caps = NFS_CAP_READDIRPLUS
8383 | NFS_CAP_ATOMIC_OPEN
8384 | NFS_CAP_CHANGE_ATTR
8385 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008386 .init_client = nfs40_init_client,
8387 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008388 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008389 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008390 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008391 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008392 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8393 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8394 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008395 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008396};
8397
8398#if defined(CONFIG_NFS_V4_1)
8399static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8400 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008401 .init_caps = NFS_CAP_READDIRPLUS
8402 | NFS_CAP_ATOMIC_OPEN
8403 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008404 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008405 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008406 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008407 .init_client = nfs41_init_client,
8408 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008409 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008410 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008411 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008412 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008413 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8414 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8415 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008416 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008417};
8418#endif
8419
Steve Dickson42c2c422013-05-22 12:50:38 -04008420#if defined(CONFIG_NFS_V4_2)
8421static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8422 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008423 .init_caps = NFS_CAP_READDIRPLUS
8424 | NFS_CAP_ATOMIC_OPEN
8425 | NFS_CAP_CHANGE_ATTR
8426 | NFS_CAP_POSIX_LOCK
8427 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008428 | NFS_CAP_ATOMIC_OPEN_V1
8429 | NFS_CAP_SEEK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008430 .init_client = nfs41_init_client,
8431 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008432 .match_stateid = nfs41_match_stateid,
8433 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008434 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008435 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008436 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8437 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8438 .state_renewal_ops = &nfs41_state_renewal_ops,
8439};
8440#endif
8441
Trond Myklebust97dc1352010-06-16 09:52:26 -04008442const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8443 [0] = &nfs_v4_0_minor_ops,
8444#if defined(CONFIG_NFS_V4_1)
8445 [1] = &nfs_v4_1_minor_ops,
8446#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008447#if defined(CONFIG_NFS_V4_2)
8448 [2] = &nfs_v4_2_minor_ops,
8449#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008450};
8451
Trond Myklebust17f26b12013-08-21 15:48:42 -04008452static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008453 .create = nfs_create,
8454 .lookup = nfs_lookup,
8455 .atomic_open = nfs_atomic_open,
8456 .link = nfs_link,
8457 .unlink = nfs_unlink,
8458 .symlink = nfs_symlink,
8459 .mkdir = nfs_mkdir,
8460 .rmdir = nfs_rmdir,
8461 .mknod = nfs_mknod,
8462 .rename = nfs_rename,
8463 .permission = nfs_permission,
8464 .getattr = nfs_getattr,
8465 .setattr = nfs_setattr,
8466 .getxattr = generic_getxattr,
8467 .setxattr = generic_setxattr,
8468 .listxattr = generic_listxattr,
8469 .removexattr = generic_removexattr,
8470};
8471
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008472static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008473 .permission = nfs_permission,
8474 .getattr = nfs_getattr,
8475 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008476 .getxattr = generic_getxattr,
8477 .setxattr = generic_setxattr,
8478 .listxattr = generic_listxattr,
8479 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008480};
8481
David Howells509de812006-08-22 20:06:11 -04008482const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483 .version = 4, /* protocol version */
8484 .dentry_ops = &nfs4_dentry_operations,
8485 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008486 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008487 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008488 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008489 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008490 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491 .getattr = nfs4_proc_getattr,
8492 .setattr = nfs4_proc_setattr,
8493 .lookup = nfs4_proc_lookup,
8494 .access = nfs4_proc_access,
8495 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496 .create = nfs4_proc_create,
8497 .remove = nfs4_proc_remove,
8498 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008499 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008500 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008501 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008502 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008503 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504 .link = nfs4_proc_link,
8505 .symlink = nfs4_proc_symlink,
8506 .mkdir = nfs4_proc_mkdir,
8507 .rmdir = nfs4_proc_remove,
8508 .readdir = nfs4_proc_readdir,
8509 .mknod = nfs4_proc_mknod,
8510 .statfs = nfs4_proc_statfs,
8511 .fsinfo = nfs4_proc_fsinfo,
8512 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008513 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008515 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008517 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008519 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008521 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008522 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008524 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008525 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008526 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008527 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008528 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008529 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008530 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008531 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008532 .create_server = nfs4_create_server,
8533 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534};
8535
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008536static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8537 .prefix = XATTR_NAME_NFSV4_ACL,
8538 .list = nfs4_xattr_list_nfs4_acl,
8539 .get = nfs4_xattr_get_nfs4_acl,
8540 .set = nfs4_xattr_set_nfs4_acl,
8541};
8542
8543const struct xattr_handler *nfs4_xattr_handlers[] = {
8544 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008545#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8546 &nfs4_xattr_nfs4_label_handler,
8547#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008548 NULL
8549};
8550
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551/*
8552 * Local variables:
8553 * c-basic-offset: 8
8554 * End:
8555 */