blob: 45bce9ed6791cab4fd447589e53786e4ba3c49da [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 Myklebustb570a972013-04-12 15:04:51 -0400373 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
374 nfs_remove_bad_delegation(inode);
375 exception->retry = 1;
376 break;
377 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500378 if (state == NULL)
379 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400380 ret = nfs4_schedule_stateid_recovery(server, state);
381 if (ret < 0)
382 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500383 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400384 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400385 if (state != NULL) {
386 ret = nfs4_schedule_stateid_recovery(server, state);
387 if (ret < 0)
388 break;
389 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500390 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500391 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500392 nfs4_schedule_lease_recovery(clp);
393 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400394 case -NFS4ERR_MOVED:
395 ret = nfs4_schedule_migration_recovery(server);
396 if (ret < 0)
397 break;
398 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400399 case -NFS4ERR_LEASE_MOVED:
400 nfs4_schedule_lease_moved_recovery(clp);
401 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500402#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400403 case -NFS4ERR_BADSESSION:
404 case -NFS4ERR_BADSLOT:
405 case -NFS4ERR_BAD_HIGH_SLOT:
406 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
407 case -NFS4ERR_DEADSESSION:
408 case -NFS4ERR_SEQ_FALSE_RETRY:
409 case -NFS4ERR_SEQ_MISORDERED:
410 dprintk("%s ERROR: %d Reset session\n", __func__,
411 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400412 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400413 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500414#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500415 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500416 if (exception->timeout > HZ) {
417 /* We have retried a decent amount, time to
418 * fail
419 */
420 ret = -EBUSY;
421 break;
422 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500423 case -NFS4ERR_GRACE:
424 case -NFS4ERR_DELAY:
425 ret = nfs4_delay(server->client, &exception->timeout);
426 if (ret != 0)
427 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400428 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 case -NFS4ERR_OLD_STATEID:
430 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800431 break;
432 case -NFS4ERR_BADOWNER:
433 /* The following works around a Linux server bug! */
434 case -NFS4ERR_BADNAME:
435 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
436 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
437 exception->retry = 1;
438 printk(KERN_WARNING "NFS: v4 server %s "
439 "does not accept raw "
440 "uid/gids. "
441 "Reenabling the idmapper.\n",
442 server->nfs_client->cl_hostname);
443 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500444 }
445 /* We failed to handle the error */
446 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500447wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500448 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400449 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
450 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500451 if (ret == 0)
452 exception->retry = 1;
453 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500454}
455
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400456/*
457 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
458 * or 'false' otherwise.
459 */
460static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
461{
462 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
463
464 if (flavor == RPC_AUTH_GSS_KRB5I ||
465 flavor == RPC_AUTH_GSS_KRB5P)
466 return true;
467
468 return false;
469}
Trond Myklebust65de8722008-12-23 15:21:44 -0500470
Trond Myklebust452e9352010-07-31 14:29:06 -0400471static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 spin_lock(&clp->cl_lock);
474 if (time_before(clp->cl_last_renewal,timestamp))
475 clp->cl_last_renewal = timestamp;
476 spin_unlock(&clp->cl_lock);
477}
478
Trond Myklebust452e9352010-07-31 14:29:06 -0400479static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
480{
481 do_renew_lease(server->nfs_client, timestamp);
482}
483
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400484struct nfs4_call_sync_data {
485 const struct nfs_server *seq_server;
486 struct nfs4_sequence_args *seq_args;
487 struct nfs4_sequence_res *seq_res;
488};
489
Chuck Levera9c92d62013-08-09 12:48:18 -0400490static void nfs4_init_sequence(struct nfs4_sequence_args *args,
491 struct nfs4_sequence_res *res, int cache_reply)
492{
493 args->sa_slot = NULL;
494 args->sa_cache_this = cache_reply;
495 args->sa_privileged = 0;
496
497 res->sr_slot = NULL;
498}
499
500static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
501{
502 args->sa_privileged = 1;
503}
504
Chuck Lever3bd23842013-08-09 12:49:19 -0400505static int nfs40_setup_sequence(const struct nfs_server *server,
506 struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res,
508 struct rpc_task *task)
509{
510 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
511 struct nfs4_slot *slot;
512
513 /* slot already allocated? */
514 if (res->sr_slot != NULL)
515 goto out_start;
516
517 spin_lock(&tbl->slot_tbl_lock);
518 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
519 goto out_sleep;
520
521 slot = nfs4_alloc_slot(tbl);
522 if (IS_ERR(slot)) {
523 if (slot == ERR_PTR(-ENOMEM))
524 task->tk_timeout = HZ >> 2;
525 goto out_sleep;
526 }
527 spin_unlock(&tbl->slot_tbl_lock);
528
529 args->sa_slot = slot;
530 res->sr_slot = slot;
531
532out_start:
533 rpc_call_start(task);
534 return 0;
535
536out_sleep:
537 if (args->sa_privileged)
538 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
539 NULL, RPC_PRIORITY_PRIVILEGED);
540 else
541 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
542 spin_unlock(&tbl->slot_tbl_lock);
543 return -EAGAIN;
544}
545
546static int nfs40_sequence_done(struct rpc_task *task,
547 struct nfs4_sequence_res *res)
548{
549 struct nfs4_slot *slot = res->sr_slot;
550 struct nfs4_slot_table *tbl;
551
Trond Myklebustcab92c12014-01-29 12:12:15 -0500552 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400553 goto out;
554
555 tbl = slot->table;
556 spin_lock(&tbl->slot_tbl_lock);
557 if (!nfs41_wake_and_assign_slot(tbl, slot))
558 nfs4_free_slot(tbl, slot);
559 spin_unlock(&tbl->slot_tbl_lock);
560
561 res->sr_slot = NULL;
562out:
563 return 1;
564}
565
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400566#if defined(CONFIG_NFS_V4_1)
567
Trond Myklebustd185a332010-06-16 09:52:25 -0400568static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400569{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500570 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400571 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500572 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500573 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400574
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500575 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500576 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500577
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500578 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500579 /* Be nice to the server: try to ensure that the last transmitted
580 * value for highest_user_slotid <= target_highest_slotid
581 */
582 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
583 send_new_highest_used_slotid = true;
584
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500585 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500586 send_new_highest_used_slotid = false;
587 goto out_unlock;
588 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500589 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500590
591 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
592 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500593out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500594 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400595 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500596 if (send_new_highest_used_slotid)
597 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400598}
599
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500600int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400601{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500602 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500603 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400604 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500605 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500606 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400607
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500608 if (slot == NULL)
609 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400610 /* don't increment the sequence number if the task wasn't sent */
611 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400612 goto out;
613
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500614 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500615
Trond Myklebustac20d162012-12-15 15:36:07 -0500616 if (slot->interrupted) {
617 slot->interrupted = 0;
618 interrupted = true;
619 }
620
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400621 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500622 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400623 switch (res->sr_status) {
624 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400625 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500626 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500627 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500628 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500629 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500630 if (res->sr_status_flags != 0)
631 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500632 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400633 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500634 case 1:
635 /*
636 * sr_status remains 1 if an RPC level error occurred.
637 * The server may or may not have processed the sequence
638 * operation..
639 * Mark the slot as having hosted an interrupted RPC call.
640 */
641 slot->interrupted = 1;
642 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400643 case -NFS4ERR_DELAY:
644 /* The server detected a resend of the RPC call and
645 * returned NFS4ERR_DELAY as per Section 2.10.6.2
646 * of RFC5661.
647 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500648 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400649 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500650 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500651 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400652 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500653 case -NFS4ERR_BADSLOT:
654 /*
655 * The slot id we used was probably retired. Try again
656 * using a different slot id.
657 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500658 goto retry_nowait;
659 case -NFS4ERR_SEQ_MISORDERED:
660 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500661 * Was the last operation on this sequence interrupted?
662 * If so, retry after bumping the sequence number.
663 */
664 if (interrupted) {
665 ++slot->seq_nr;
666 goto retry_nowait;
667 }
668 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500669 * Could this slot have been previously retired?
670 * If so, then the server may be expecting seq_nr = 1!
671 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500672 if (slot->seq_nr != 1) {
673 slot->seq_nr = 1;
674 goto retry_nowait;
675 }
676 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500677 case -NFS4ERR_SEQ_FALSE_RETRY:
678 ++slot->seq_nr;
679 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400680 default:
681 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500682 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400683 }
684out:
685 /* The session may be reset by one of the error handlers. */
686 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400687 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500688out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500689 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500690retry_nowait:
691 if (rpc_restart_call_prepare(task)) {
692 task->tk_status = 0;
693 ret = 0;
694 }
695 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400696out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400697 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400698 goto out;
699 rpc_delay(task, NFS4_POLL_RETRY_MAX);
700 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400701}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500702EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400703
Trond Myklebust14516c32010-07-31 14:29:06 -0400704static int nfs4_sequence_done(struct rpc_task *task,
705 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400706{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500707 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400708 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400709 if (!res->sr_slot->table->session)
710 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400711 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400712}
713
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000714int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400715 struct nfs4_sequence_args *args,
716 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717 struct rpc_task *task)
718{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400719 struct nfs4_slot *slot;
720 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400721
722 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400724 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400725 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400727 tbl = &session->fc_slot_table;
728
Trond Myklebust69d206b2012-11-22 13:21:02 -0500729 task->tk_timeout = 0;
730
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400731 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400732 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400733 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500734 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500735 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400736 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400737 }
738
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500739 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500740 if (IS_ERR(slot)) {
741 /* If out of memory, try again in 1/4 second */
742 if (slot == ERR_PTR(-ENOMEM))
743 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400744 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400745 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400746 }
747 spin_unlock(&tbl->slot_tbl_lock);
748
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500749 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400750
Chuck Levere8d92382013-08-09 12:47:51 -0400751 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500752 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400753
Benny Halevydfb4f3092010-09-24 09:17:01 -0400754 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500755 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400756 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400757 /*
758 * sr_status is only set in decode_sequence, and so will remain
759 * set to 1 if an rpc level failure occurs.
760 */
761 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400762 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400763out_success:
764 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400765 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400766out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400767 /* Privileged tasks are queued with top priority */
768 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400769 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
770 NULL, RPC_PRIORITY_PRIVILEGED);
771 else
772 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400773 spin_unlock(&tbl->slot_tbl_lock);
774 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400775}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000776EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400777
Chuck Lever5a580e02013-08-09 12:48:09 -0400778static int nfs4_setup_sequence(const struct nfs_server *server,
779 struct nfs4_sequence_args *args,
780 struct nfs4_sequence_res *res,
781 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400782{
Trond Myklebust035168a2010-06-16 09:52:26 -0400783 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400784 int ret = 0;
785
Chuck Lever3bd23842013-08-09 12:49:19 -0400786 if (!session)
787 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400788
Chuck Levere8d92382013-08-09 12:47:51 -0400789 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400790 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400791 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400792
Trond Myklebust9d12b212012-01-17 22:04:25 -0500793 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400794
Andy Adamsonce5039c2009-04-01 09:22:13 -0400795 dprintk("<-- %s status=%d\n", __func__, ret);
796 return ret;
797}
798
Andy Adamsonce5039c2009-04-01 09:22:13 -0400799static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
800{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400801 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400802 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400803
Trond Myklebust035168a2010-06-16 09:52:26 -0400804 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
805
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400806 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400807}
808
Andy Adamson69ab40c2009-04-01 09:22:19 -0400809static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
810{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400811 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400812
Trond Myklebust14516c32010-07-31 14:29:06 -0400813 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400814}
815
Trond Myklebust17280172012-03-11 13:11:00 -0400816static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400817 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400818 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400819};
820
Chuck Lever3bd23842013-08-09 12:49:19 -0400821#else /* !CONFIG_NFS_V4_1 */
822
Chuck Lever5a580e02013-08-09 12:48:09 -0400823static int nfs4_setup_sequence(const struct nfs_server *server,
824 struct nfs4_sequence_args *args,
825 struct nfs4_sequence_res *res,
826 struct rpc_task *task)
827{
Chuck Lever3bd23842013-08-09 12:49:19 -0400828 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400829}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400830
Trond Myklebust14516c32010-07-31 14:29:06 -0400831static int nfs4_sequence_done(struct rpc_task *task,
832 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400833{
Chuck Lever3bd23842013-08-09 12:49:19 -0400834 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400835}
Chuck Lever3bd23842013-08-09 12:49:19 -0400836
837#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400838
Chuck Lever9915ea72013-08-09 12:48:27 -0400839static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
840{
841 struct nfs4_call_sync_data *data = calldata;
842 nfs4_setup_sequence(data->seq_server,
843 data->seq_args, data->seq_res, task);
844}
845
846static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
847{
848 struct nfs4_call_sync_data *data = calldata;
849 nfs4_sequence_done(task, data->seq_res);
850}
851
852static const struct rpc_call_ops nfs40_call_sync_ops = {
853 .rpc_call_prepare = nfs40_call_sync_prepare,
854 .rpc_call_done = nfs40_call_sync_done,
855};
856
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400857static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400858 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500859 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860 struct nfs4_sequence_args *args,
861 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400862{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100863 int ret;
864 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400865 struct nfs_client *clp = server->nfs_client;
866 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867 .seq_server = server,
868 .seq_args = args,
869 .seq_res = res,
870 };
871 struct rpc_task_setup task_setup = {
872 .rpc_client = clnt,
873 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400874 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875 .callback_data = &data
876 };
877
878 task = rpc_run_task(&task_setup);
879 if (IS_ERR(task))
880 ret = PTR_ERR(task);
881 else {
882 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400883 rpc_put_task(task);
884 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100885 return ret;
886}
887
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400888static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000889int nfs4_call_sync(struct rpc_clnt *clnt,
890 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000891 struct rpc_message *msg,
892 struct nfs4_sequence_args *args,
893 struct nfs4_sequence_res *res,
894 int cache_reply)
895{
Chuck Levera9c92d62013-08-09 12:48:18 -0400896 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400897 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000898}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
901{
902 struct nfs_inode *nfsi = NFS_I(dir);
903
904 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400905 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700906 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700908 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000909 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 spin_unlock(&dir->i_lock);
911}
912
913struct nfs4_opendata {
914 struct kref kref;
915 struct nfs_openargs o_arg;
916 struct nfs_openres o_res;
917 struct nfs_open_confirmargs c_arg;
918 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500919 struct nfs4_string owner_name;
920 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400922 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400924 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct nfs4_state_owner *owner;
926 struct nfs4_state *state;
927 struct iattr attrs;
928 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400929 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400930 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400931 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400932 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 int cancelled;
934};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400935
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400936static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
937 int err, struct nfs4_exception *exception)
938{
939 if (err != -EINVAL)
940 return false;
941 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
942 return false;
943 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
944 exception->retry = 1;
945 return true;
946}
947
948static enum open_claim_type4
949nfs4_map_atomic_open_claim(struct nfs_server *server,
950 enum open_claim_type4 claim)
951{
952 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
953 return claim;
954 switch (claim) {
955 default:
956 return claim;
957 case NFS4_OPEN_CLAIM_FH:
958 return NFS4_OPEN_CLAIM_NULL;
959 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
960 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
961 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
962 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
963 }
964}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100967{
968 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400969 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100970 p->o_res.seqid = p->o_arg.seqid;
971 p->c_res.seqid = p->c_arg.seqid;
972 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400973 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100974 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500975 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976}
977
Al Viro82a2c1b2011-06-22 18:30:55 -0400978static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100979 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400980 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400981 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400982 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400983 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100984{
Al Viro82a2c1b2011-06-22 18:30:55 -0400985 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986 struct inode *dir = parent->d_inode;
987 struct nfs_server *server = NFS_SERVER(dir);
988 struct nfs4_opendata *p;
989
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400990 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100991 if (p == NULL)
992 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400993
994 p->f_label = nfs4_label_alloc(server, gfp_mask);
995 if (IS_ERR(p->f_label))
996 goto err_free_p;
997
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400998 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100999 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -04001000 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001001 nfs_sb_active(dentry->d_sb);
1002 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003 p->dir = parent;
1004 p->owner = sp;
1005 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006 p->o_arg.open_flags = flags;
1007 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001008 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1009 * will return permission denied for all bits until close */
1010 if (!(flags & O_EXCL)) {
1011 /* ask server to check for all possible rights as results
1012 * are cached */
1013 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1014 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001015 }
David Howells7539bba2006-08-22 20:06:09 -04001016 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001017 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1018 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001019 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001020 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001021 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001022 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001023 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001024 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1025 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001026 case NFS4_OPEN_CLAIM_NULL:
1027 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1028 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1029 p->o_arg.fh = NFS_FH(dir);
1030 break;
1031 case NFS4_OPEN_CLAIM_PREVIOUS:
1032 case NFS4_OPEN_CLAIM_FH:
1033 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1034 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1035 p->o_arg.fh = NFS_FH(dentry->d_inode);
1036 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001037 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001038 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001039
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001040 p->o_arg.u.attrs = &p->attrs;
1041 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001042
1043 verf[0] = jiffies;
1044 verf[1] = current->pid;
1045 memcpy(p->o_arg.u.verifier.data, verf,
1046 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001048 p->c_arg.fh = &p->o_res.fh;
1049 p->c_arg.stateid = &p->o_res.stateid;
1050 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001051 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001052 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001053 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001054
1055err_free_label:
1056 nfs4_label_free(p->f_label);
1057err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001058 kfree(p);
1059err:
1060 dput(parent);
1061 return NULL;
1062}
1063
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001064static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001065{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001066 struct nfs4_opendata *p = container_of(kref,
1067 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001068 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001069
1070 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071 if (p->state != NULL)
1072 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001073 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001074
1075 nfs4_label_free(p->f_label);
1076
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001077 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001078 dput(p->dentry);
1079 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001080 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001081 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001082 kfree(p);
1083}
1084
1085static void nfs4_opendata_put(struct nfs4_opendata *p)
1086{
1087 if (p != NULL)
1088 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001089}
1090
Trond Myklebust06f814a2006-01-03 09:55:07 +01001091static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1092{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001093 int ret;
1094
Trond Myklebust06f814a2006-01-03 09:55:07 +01001095 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001096 return ret;
1097}
1098
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001099static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100{
1101 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102
Trond Myklebust536e43d2012-01-17 22:04:26 -05001103 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001104 goto out;
1105 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001106 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001107 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1108 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001109 break;
1110 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001111 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1112 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001113 break;
1114 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001115 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1116 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001117 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001118out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001119 return ret;
1120}
1121
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001122static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001123{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001124 if (delegation == NULL)
1125 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001126 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001128 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001129 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001130 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1131 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001132 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001133 return 1;
1134}
1135
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001136static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001137{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001138 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001139 case FMODE_WRITE:
1140 state->n_wronly++;
1141 break;
1142 case FMODE_READ:
1143 state->n_rdonly++;
1144 break;
1145 case FMODE_READ|FMODE_WRITE:
1146 state->n_rdwr++;
1147 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001149}
1150
Trond Myklebust4f14c192014-02-12 19:15:06 -05001151static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001152{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001153 struct nfs_client *clp = state->owner->so_server->nfs_client;
1154 bool need_recover = false;
1155
1156 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1157 need_recover = true;
1158 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1159 need_recover = true;
1160 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1161 need_recover = true;
1162 if (need_recover)
1163 nfs4_state_mark_reclaim_nograce(clp, state);
1164}
1165
Trond Myklebuste999e802014-02-10 18:20:47 -05001166static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1167 nfs4_stateid *stateid)
1168{
1169 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1170 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001171 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1172 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001173 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001174 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001175 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1176 return true;
1177 return false;
1178}
1179
Trond Myklebust226056c2014-02-11 10:41:07 -05001180static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1181 nfs4_stateid *stateid, fmode_t fmode)
1182{
1183 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1184 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1185 case FMODE_WRITE:
1186 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1187 break;
1188 case FMODE_READ:
1189 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1190 break;
1191 case 0:
1192 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1193 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1194 clear_bit(NFS_OPEN_STATE, &state->flags);
1195 }
1196 if (stateid == NULL)
1197 return;
1198 if (!nfs_need_update_open_stateid(state, stateid))
1199 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001200 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001201 nfs4_stateid_copy(&state->stateid, stateid);
1202 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001203}
1204
1205static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1206{
1207 write_seqlock(&state->seqlock);
1208 nfs_clear_open_stateid_locked(state, stateid, fmode);
1209 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001210 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1211 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001212}
1213
Trond Myklebust003707c2007-07-05 18:07:55 -04001214static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1215{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001216 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001217 case FMODE_READ:
1218 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1219 break;
1220 case FMODE_WRITE:
1221 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1222 break;
1223 case FMODE_READ|FMODE_WRITE:
1224 set_bit(NFS_O_RDWR_STATE, &state->flags);
1225 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001226 if (!nfs_need_update_open_stateid(state, stateid))
1227 return;
1228 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1229 nfs4_stateid_copy(&state->stateid, stateid);
1230 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001231}
1232
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001233static 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 -07001234{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001235 /*
1236 * Protect the call to nfs4_state_set_mode_locked and
1237 * serialise the stateid update
1238 */
1239 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001240 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001241 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001242 set_bit(NFS_DELEGATED_STATE, &state->flags);
1243 }
1244 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001246 write_sequnlock(&state->seqlock);
1247 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001248 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001249 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001252static 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 -05001253{
1254 struct nfs_inode *nfsi = NFS_I(state->inode);
1255 struct nfs_delegation *deleg_cur;
1256 int ret = 0;
1257
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001258 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001259
1260 rcu_read_lock();
1261 deleg_cur = rcu_dereference(nfsi->delegation);
1262 if (deleg_cur == NULL)
1263 goto no_delegation;
1264
1265 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001266 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001267 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001268 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001269 goto no_delegation_unlock;
1270
1271 if (delegation == NULL)
1272 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001273 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001274 goto no_delegation_unlock;
1275
Trond Myklebustb7391f42008-12-23 15:21:52 -05001276 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001277 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001278 ret = 1;
1279no_delegation_unlock:
1280 spin_unlock(&deleg_cur->lock);
1281no_delegation:
1282 rcu_read_unlock();
1283
1284 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001285 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001286 ret = 1;
1287 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001288 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1289 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001290
1291 return ret;
1292}
1293
1294
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001295static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001296{
1297 struct nfs_delegation *delegation;
1298
1299 rcu_read_lock();
1300 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001301 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001302 rcu_read_unlock();
1303 return;
1304 }
1305 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001306 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001307}
1308
Trond Myklebust6ee41262007-07-08 14:11:36 -04001309static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001310{
1311 struct nfs4_state *state = opendata->state;
1312 struct nfs_inode *nfsi = NFS_I(state->inode);
1313 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001314 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001315 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001316 nfs4_stateid stateid;
1317 int ret = -EAGAIN;
1318
Trond Myklebustaac00a82007-07-05 19:02:21 -04001319 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001320 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001321 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001322 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001323 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001324 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001325 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001326 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001327 rcu_read_lock();
1328 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001329 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001330 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001331 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001332 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001333 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001334 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001335 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001336 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001337 if (!opendata->is_recover) {
1338 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1339 if (ret != 0)
1340 goto out;
1341 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001342 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001343
1344 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001345 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001346 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001347 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001348out:
1349 return ERR_PTR(ret);
1350out_return_state:
1351 atomic_inc(&state->count);
1352 return state;
1353}
1354
Andy Adamsone23008e2012-10-02 21:07:32 -04001355static void
1356nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1357{
1358 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1359 struct nfs_delegation *delegation;
1360 int delegation_flags = 0;
1361
1362 rcu_read_lock();
1363 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1364 if (delegation)
1365 delegation_flags = delegation->flags;
1366 rcu_read_unlock();
1367 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1368 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1369 "returning a delegation for "
1370 "OPEN(CLAIM_DELEGATE_CUR)\n",
1371 clp->cl_hostname);
1372 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1373 nfs_inode_set_delegation(state->inode,
1374 data->owner->so_cred,
1375 &data->o_res);
1376 else
1377 nfs_inode_reclaim_delegation(state->inode,
1378 data->owner->so_cred,
1379 &data->o_res);
1380}
1381
1382/*
1383 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1384 * and update the nfs4_state.
1385 */
1386static struct nfs4_state *
1387_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1388{
1389 struct inode *inode = data->state->inode;
1390 struct nfs4_state *state = data->state;
1391 int ret;
1392
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001393 if (!data->rpc_done) {
1394 if (data->rpc_status) {
1395 ret = data->rpc_status;
1396 goto err;
1397 }
1398 /* cached opens have already been processed */
1399 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001400 }
1401
Andy Adamsone23008e2012-10-02 21:07:32 -04001402 ret = nfs_refresh_inode(inode, &data->f_attr);
1403 if (ret)
1404 goto err;
1405
1406 if (data->o_res.delegation_type != 0)
1407 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001408update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001409 update_open_stateid(state, &data->o_res.stateid, NULL,
1410 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001411 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001412
1413 return state;
1414err:
1415 return ERR_PTR(ret);
1416
1417}
1418
1419static struct nfs4_state *
1420_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001421{
1422 struct inode *inode;
1423 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001424 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001425
Trond Myklebustaac00a82007-07-05 19:02:21 -04001426 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001427 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001428 goto out;
1429 }
1430
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001431 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001433 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001434 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001435 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001436 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001437 goto err;
1438 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 state = nfs4_get_open_state(inode, data->owner);
1440 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001441 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001442 if (data->o_res.delegation_type != 0)
1443 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001444 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001445 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001447out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001448 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001450err_put_inode:
1451 iput(inode);
1452err:
1453 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454}
1455
Andy Adamsone23008e2012-10-02 21:07:32 -04001456static struct nfs4_state *
1457nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1458{
1459 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1460 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1461 return _nfs4_opendata_to_nfs4_state(data);
1462}
1463
Trond Myklebust864472e2006-01-03 09:55:15 +01001464static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1465{
1466 struct nfs_inode *nfsi = NFS_I(state->inode);
1467 struct nfs_open_context *ctx;
1468
1469 spin_lock(&state->inode->i_lock);
1470 list_for_each_entry(ctx, &nfsi->open_files, list) {
1471 if (ctx->state != state)
1472 continue;
1473 get_nfs_open_context(ctx);
1474 spin_unlock(&state->inode->i_lock);
1475 return ctx;
1476 }
1477 spin_unlock(&state->inode->i_lock);
1478 return ERR_PTR(-ENOENT);
1479}
1480
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001481static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1482 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001483{
1484 struct nfs4_opendata *opendata;
1485
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001486 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001487 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001488 if (opendata == NULL)
1489 return ERR_PTR(-ENOMEM);
1490 opendata->state = state;
1491 atomic_inc(&state->count);
1492 return opendata;
1493}
1494
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001495static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001496{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001497 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001498 int ret;
1499
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001500 opendata->o_arg.open_flags = 0;
1501 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001502 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1503 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1504 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001505 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001506 if (ret != 0)
1507 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001508 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001509 if (IS_ERR(newstate))
1510 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001511 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001512 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001513 return 0;
1514}
1515
1516static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1517{
Trond Myklebust864472e2006-01-03 09:55:15 +01001518 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001519 int ret;
1520
Trond Myklebust4f14c192014-02-12 19:15:06 -05001521 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1522 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1523 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1524 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001525 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001526 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001527 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001528 smp_rmb();
1529 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001530 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001531 if (ret != 0)
1532 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001533 if (newstate != state)
1534 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001535 }
1536 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001537 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001538 if (ret != 0)
1539 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001540 if (newstate != state)
1541 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001542 }
1543 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001544 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001545 if (ret != 0)
1546 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001547 if (newstate != state)
1548 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001549 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001550 /*
1551 * We may have performed cached opens for all three recoveries.
1552 * Check if we need to update the current stateid.
1553 */
1554 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001555 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001556 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001557 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001558 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001559 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001560 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001561 return 0;
1562}
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564/*
1565 * OPEN_RECLAIM:
1566 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001568static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001570 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001571 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001572 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 int status;
1574
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001575 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1576 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001577 if (IS_ERR(opendata))
1578 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001579 rcu_read_lock();
1580 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001581 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001582 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001583 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001584 opendata->o_arg.u.delegation_type = delegation_type;
1585 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001586 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return status;
1588}
1589
Trond Myklebust539cd032007-06-05 11:46:42 -04001590static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 struct nfs_server *server = NFS_SERVER(state->inode);
1593 struct nfs4_exception exception = { };
1594 int err;
1595 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001596 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001597 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001598 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1599 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001600 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001601 break;
1602 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 } while (exception.retry);
1604 return err;
1605}
1606
Trond Myklebust864472e2006-01-03 09:55:15 +01001607static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1608{
1609 struct nfs_open_context *ctx;
1610 int ret;
1611
1612 ctx = nfs4_state_find_open_context(state);
1613 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001614 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001615 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001616 put_nfs_open_context(ctx);
1617 return ret;
1618}
1619
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001620static 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 -07001621{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001622 switch (err) {
1623 default:
1624 printk(KERN_ERR "NFS: %s: unhandled error "
1625 "%d.\n", __func__, err);
1626 case 0:
1627 case -ENOENT:
1628 case -ESTALE:
1629 break;
1630 case -NFS4ERR_BADSESSION:
1631 case -NFS4ERR_BADSLOT:
1632 case -NFS4ERR_BAD_HIGH_SLOT:
1633 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1634 case -NFS4ERR_DEADSESSION:
1635 set_bit(NFS_DELEGATED_STATE, &state->flags);
1636 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1637 return -EAGAIN;
1638 case -NFS4ERR_STALE_CLIENTID:
1639 case -NFS4ERR_STALE_STATEID:
1640 set_bit(NFS_DELEGATED_STATE, &state->flags);
1641 case -NFS4ERR_EXPIRED:
1642 /* Don't recall a delegation if it was lost */
1643 nfs4_schedule_lease_recovery(server->nfs_client);
1644 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001645 case -NFS4ERR_MOVED:
1646 nfs4_schedule_migration_recovery(server);
1647 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001648 case -NFS4ERR_LEASE_MOVED:
1649 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1650 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001651 case -NFS4ERR_DELEG_REVOKED:
1652 case -NFS4ERR_ADMIN_REVOKED:
1653 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001654 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001655 nfs_inode_find_state_and_recover(state->inode,
1656 stateid);
1657 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001658 return 0;
1659 case -NFS4ERR_DELAY:
1660 case -NFS4ERR_GRACE:
1661 set_bit(NFS_DELEGATED_STATE, &state->flags);
1662 ssleep(1);
1663 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001664 case -ENOMEM:
1665 case -NFS4ERR_DENIED:
1666 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1667 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return err;
1670}
1671
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001672int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1673{
1674 struct nfs_server *server = NFS_SERVER(state->inode);
1675 struct nfs4_opendata *opendata;
1676 int err;
1677
1678 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1679 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1680 if (IS_ERR(opendata))
1681 return PTR_ERR(opendata);
1682 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1683 err = nfs4_open_recover(opendata, state);
1684 nfs4_opendata_put(opendata);
1685 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1686}
1687
Chuck Leverbe05c862013-08-09 12:49:47 -04001688static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1689{
1690 struct nfs4_opendata *data = calldata;
1691
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001692 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1693 &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001694}
1695
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001696static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1697{
1698 struct nfs4_opendata *data = calldata;
1699
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001700 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001701
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001702 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001703 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001704 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001705 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001706 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001707 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001708 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001709}
1710
1711static void nfs4_open_confirm_release(void *calldata)
1712{
1713 struct nfs4_opendata *data = calldata;
1714 struct nfs4_state *state = NULL;
1715
1716 /* If this request hasn't been cancelled, do nothing */
1717 if (data->cancelled == 0)
1718 goto out_free;
1719 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001720 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001721 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001722 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001723 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001724 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001725out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001726 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001727}
1728
1729static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001730 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001731 .rpc_call_done = nfs4_open_confirm_done,
1732 .rpc_release = nfs4_open_confirm_release,
1733};
1734
1735/*
1736 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1737 */
1738static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1739{
1740 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1741 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001742 struct rpc_message msg = {
1743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1744 .rpc_argp = &data->c_arg,
1745 .rpc_resp = &data->c_res,
1746 .rpc_cred = data->owner->so_cred,
1747 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001748 struct rpc_task_setup task_setup_data = {
1749 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001750 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001751 .callback_ops = &nfs4_open_confirm_ops,
1752 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001753 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001754 .flags = RPC_TASK_ASYNC,
1755 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int status;
1757
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001758 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001759 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001760 data->rpc_done = 0;
1761 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001762 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001763 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001764 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001765 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001766 status = nfs4_wait_for_completion_rpc_task(task);
1767 if (status != 0) {
1768 data->cancelled = 1;
1769 smp_wmb();
1770 } else
1771 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001772 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return status;
1774}
1775
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001776static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001778 struct nfs4_opendata *data = calldata;
1779 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001780 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001781
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001782 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001783 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001784 /*
1785 * Check if we still need to send an OPEN call, or if we can use
1786 * a delegation instead.
1787 */
1788 if (data->state != NULL) {
1789 struct nfs_delegation *delegation;
1790
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001791 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001792 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001793 rcu_read_lock();
1794 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001795 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001796 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001797 can_open_delegated(delegation, data->o_arg.fmode))
1798 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001799 rcu_read_unlock();
1800 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001801 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001802 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001803 switch (data->o_arg.claim) {
1804 case NFS4_OPEN_CLAIM_PREVIOUS:
1805 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1806 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001807 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001808 case NFS4_OPEN_CLAIM_FH:
1809 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001810 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1811 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001812 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001813 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001814 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001815 &data->o_res.seq_res,
1816 task) != 0)
1817 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001818
1819 /* Set the create mode (note dependency on the session type) */
1820 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1821 if (data->o_arg.open_flags & O_EXCL) {
1822 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1823 if (nfs4_has_persistent_session(clp))
1824 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1825 else if (clp->cl_mvops->minor_version > 0)
1826 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1827 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001828 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001829unlock_no_action:
1830 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001831out_no_action:
1832 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001833out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001834 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001835}
1836
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001837static void nfs4_open_done(struct rpc_task *task, void *calldata)
1838{
1839 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001841 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001842
Trond Myklebust14516c32010-07-31 14:29:06 -04001843 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1844 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001845
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001846 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001847 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1848 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001849 case S_IFREG:
1850 break;
1851 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001852 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001853 break;
1854 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001855 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001856 break;
1857 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001858 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001859 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001860 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001861 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001862 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1863 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001864 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001865 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001866}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001867
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001868static void nfs4_open_release(void *calldata)
1869{
1870 struct nfs4_opendata *data = calldata;
1871 struct nfs4_state *state = NULL;
1872
1873 /* If this request hasn't been cancelled, do nothing */
1874 if (data->cancelled == 0)
1875 goto out_free;
1876 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001877 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001878 goto out_free;
1879 /* In case we need an open_confirm, no cleanup! */
1880 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1881 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001882 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001883 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001884 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001885out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001886 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001887}
1888
1889static const struct rpc_call_ops nfs4_open_ops = {
1890 .rpc_call_prepare = nfs4_open_prepare,
1891 .rpc_call_done = nfs4_open_done,
1892 .rpc_release = nfs4_open_release,
1893};
1894
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001895static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001896{
1897 struct inode *dir = data->dir->d_inode;
1898 struct nfs_server *server = NFS_SERVER(dir);
1899 struct nfs_openargs *o_arg = &data->o_arg;
1900 struct nfs_openres *o_res = &data->o_res;
1901 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001902 struct rpc_message msg = {
1903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1904 .rpc_argp = o_arg,
1905 .rpc_resp = o_res,
1906 .rpc_cred = data->owner->so_cred,
1907 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001908 struct rpc_task_setup task_setup_data = {
1909 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001910 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001911 .callback_ops = &nfs4_open_ops,
1912 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001913 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001914 .flags = RPC_TASK_ASYNC,
1915 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001916 int status;
1917
Chuck Levera9c92d62013-08-09 12:48:18 -04001918 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001919 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001920 data->rpc_done = 0;
1921 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001922 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001923 data->is_recover = 0;
1924 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001925 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001926 data->is_recover = 1;
1927 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001928 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001929 if (IS_ERR(task))
1930 return PTR_ERR(task);
1931 status = nfs4_wait_for_completion_rpc_task(task);
1932 if (status != 0) {
1933 data->cancelled = 1;
1934 smp_wmb();
1935 } else
1936 status = data->rpc_status;
1937 rpc_put_task(task);
1938
1939 return status;
1940}
1941
1942static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1943{
1944 struct inode *dir = data->dir->d_inode;
1945 struct nfs_openres *o_res = &data->o_res;
1946 int status;
1947
1948 status = nfs4_run_open_task(data, 1);
1949 if (status != 0 || !data->rpc_done)
1950 return status;
1951
Trond Myklebust6926afd2012-01-07 13:22:46 -05001952 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1953
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001954 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1955 status = _nfs4_proc_open_confirm(data);
1956 if (status != 0)
1957 return status;
1958 }
1959
1960 return status;
1961}
1962
Trond Myklebustf3792d62014-07-10 08:54:32 -04001963/*
1964 * Additional permission checks in order to distinguish between an
1965 * open for read, and an open for execute. This works around the
1966 * fact that NFSv4 OPEN treats read and execute permissions as being
1967 * the same.
1968 * Note that in the non-execute case, we want to turn off permission
1969 * checking if we just created a new file (POSIX open() semantics).
1970 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001971static int nfs4_opendata_access(struct rpc_cred *cred,
1972 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001973 struct nfs4_state *state, fmode_t fmode,
1974 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001975{
1976 struct nfs_access_entry cache;
1977 u32 mask;
1978
1979 /* access call failed or for some reason the server doesn't
1980 * support any access modes -- defer access call until later */
1981 if (opendata->o_res.access_supported == 0)
1982 return 0;
1983
1984 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04001985 /*
1986 * Use openflags to check for exec, because fmode won't
1987 * always have FMODE_EXEC set when file open for exec.
1988 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001989 if (openflags & __FMODE_EXEC) {
1990 /* ONLY check for exec rights */
1991 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04001992 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001993 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001994
1995 cache.cred = cred;
1996 cache.jiffies = jiffies;
1997 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1998 nfs_access_add_cache(state->inode, &cache);
1999
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002000 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002001 return 0;
2002
2003 /* even though OPEN succeeded, access is denied. Close the file */
2004 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002005 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002006}
2007
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002008/*
2009 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2010 */
2011static int _nfs4_proc_open(struct nfs4_opendata *data)
2012{
2013 struct inode *dir = data->dir->d_inode;
2014 struct nfs_server *server = NFS_SERVER(dir);
2015 struct nfs_openargs *o_arg = &data->o_arg;
2016 struct nfs_openres *o_res = &data->o_res;
2017 int status;
2018
2019 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002020 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002021 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002022 if (status != 0) {
2023 if (status == -NFS4ERR_BADNAME &&
2024 !(o_arg->open_flags & O_CREAT))
2025 return -ENOENT;
2026 return status;
2027 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002028
Trond Myklebust6926afd2012-01-07 13:22:46 -05002029 nfs_fattr_map_and_free_names(server, &data->f_attr);
2030
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002031 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002032 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002033 if (o_arg->open_flags & O_EXCL)
2034 data->file_created = 1;
2035 else if (o_res->cinfo.before != o_res->cinfo.after)
2036 data->file_created = 1;
2037 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002038 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2039 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002041 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002043 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002046 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Andy Adamsond83217c2011-03-01 01:34:17 +00002050static int nfs4_recover_expired_lease(struct nfs_server *server)
2051{
2052 return nfs4_client_recover_expired_lease(server->nfs_client);
2053}
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055/*
2056 * OPEN_EXPIRED:
2057 * reclaim state on the server after a network partition.
2058 * Assumes caller holds the appropriate lock
2059 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002060static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002062 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002063 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002065 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002066 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002067 if (IS_ERR(opendata))
2068 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002069 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002070 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002071 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002072 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002073 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002076static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002077{
Trond Myklebust539cd032007-06-05 11:46:42 -04002078 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002079 struct nfs4_exception exception = { };
2080 int err;
2081
2082 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002083 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002084 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002085 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2086 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002087 switch (err) {
2088 default:
2089 goto out;
2090 case -NFS4ERR_GRACE:
2091 case -NFS4ERR_DELAY:
2092 nfs4_handle_exception(server, err, &exception);
2093 err = 0;
2094 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002095 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002096out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002097 return err;
2098}
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002103 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Trond Myklebust864472e2006-01-03 09:55:15 +01002105 ctx = nfs4_state_find_open_context(state);
2106 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002107 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002108 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002109 put_nfs_open_context(ctx);
2110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002113#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002114static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002115{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002116 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002117 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002118 struct nfs_delegation *delegation;
2119 struct rpc_cred *cred = NULL;
2120 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002121
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002122 /* If a state reset has been done, test_stateid is unneeded */
2123 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2124 return;
2125
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002126 /* Get the delegation credential for use by test/free_stateid */
2127 rcu_read_lock();
2128 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2129 if (delegation != NULL &&
2130 nfs4_stateid_match(&delegation->stateid, stateid)) {
2131 cred = get_rpccred(delegation->cred);
2132 rcu_read_unlock();
2133 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002134 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002135 } else
2136 rcu_read_unlock();
2137
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002138 if (status != NFS_OK) {
2139 /* Free the stateid unless the server explicitly
2140 * informs us the stateid is unrecognized. */
2141 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002142 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002143 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002144
Bryan Schumaker69388672012-09-26 15:25:52 -04002145 write_seqlock(&state->seqlock);
2146 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2147 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002148 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2149 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002150
2151 if (cred != NULL)
2152 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002153}
2154
2155/**
2156 * nfs41_check_open_stateid - possibly free an open stateid
2157 *
2158 * @state: NFSv4 state for an inode
2159 *
2160 * Returns NFS_OK if recovery for this stateid is now finished.
2161 * Otherwise a negative NFS4ERR value is returned.
2162 */
2163static int nfs41_check_open_stateid(struct nfs4_state *state)
2164{
2165 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002166 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002167 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002168 int status;
2169
2170 /* If a state reset has been done, test_stateid is unneeded */
2171 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2172 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2173 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2174 return -NFS4ERR_BAD_STATEID;
2175
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002176 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002177 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002178 if (status != NFS_OK) {
2179 /* Free the stateid unless the server explicitly
2180 * informs us the stateid is unrecognized. */
2181 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002182 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002183
2184 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2185 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2186 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002187 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002188 }
2189 return status;
2190}
2191
2192static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2193{
Chuck Levereb64cf92012-07-11 16:30:05 -04002194 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002195
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002196 nfs41_clear_delegation_stateid(state);
2197 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002198 if (status != NFS_OK)
2199 status = nfs4_open_expired(sp, state);
2200 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002201}
2202#endif
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002205 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2206 * fields corresponding to attributes that were used to store the verifier.
2207 * Make sure we clobber those fields in the later setattr call
2208 */
2209static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2210{
2211 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2212 !(sattr->ia_valid & ATTR_ATIME_SET))
2213 sattr->ia_valid |= ATTR_ATIME;
2214
2215 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2216 !(sattr->ia_valid & ATTR_MTIME_SET))
2217 sattr->ia_valid |= ATTR_MTIME;
2218}
2219
Trond Myklebustc21443c2013-02-07 14:26:21 -05002220static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2221 fmode_t fmode,
2222 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002223 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002224{
2225 struct nfs4_state_owner *sp = opendata->owner;
2226 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002227 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002228 struct nfs4_state *state;
2229 unsigned int seq;
2230 int ret;
2231
2232 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2233
2234 ret = _nfs4_proc_open(opendata);
NeilBrown4fa2c542014-07-21 13:28:28 +10002235 if (ret != 0) {
2236 if (ret == -ENOENT) {
2237 d_drop(opendata->dentry);
2238 d_add(opendata->dentry, NULL);
2239 nfs_set_verifier(opendata->dentry,
2240 nfs_save_change_attribute(opendata->dir->d_inode));
2241 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002242 goto out;
NeilBrown4fa2c542014-07-21 13:28:28 +10002243 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002244
2245 state = nfs4_opendata_to_nfs4_state(opendata);
2246 ret = PTR_ERR(state);
2247 if (IS_ERR(state))
2248 goto out;
2249 if (server->caps & NFS_CAP_POSIX_LOCK)
2250 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2251
Trond Myklebust275bb302013-05-29 13:11:28 -04002252 dentry = opendata->dentry;
2253 if (dentry->d_inode == NULL) {
2254 /* FIXME: Is this d_drop() ever needed? */
2255 d_drop(dentry);
2256 dentry = d_add_unique(dentry, igrab(state->inode));
2257 if (dentry == NULL) {
2258 dentry = opendata->dentry;
2259 } else if (dentry != ctx->dentry) {
2260 dput(ctx->dentry);
2261 ctx->dentry = dget(dentry);
2262 }
2263 nfs_set_verifier(dentry,
2264 nfs_save_change_attribute(opendata->dir->d_inode));
2265 }
2266
Trond Myklebustc21443c2013-02-07 14:26:21 -05002267 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2268 if (ret != 0)
2269 goto out;
2270
Trond Myklebust3efb9722013-05-29 13:17:04 -04002271 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002272 if (dentry->d_inode == state->inode) {
2273 nfs_inode_attach_open_context(ctx);
2274 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2275 nfs4_schedule_stateid_recovery(server, state);
2276 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002277out:
2278 return ret;
2279}
2280
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002281/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002282 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 */
Andy Adamson82be4172012-05-23 05:02:35 -04002284static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002285 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002286 int flags,
2287 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002288 struct nfs4_label *label,
2289 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 struct nfs4_state_owner *sp;
2292 struct nfs4_state *state = NULL;
2293 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002294 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002295 struct dentry *dentry = ctx->dentry;
2296 struct rpc_cred *cred = ctx->cred;
2297 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2298 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002299 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002300 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002301 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002305 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2306 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2308 goto out_err;
2309 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002310 status = nfs4_recover_expired_lease(server);
2311 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002312 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002313 if (dentry->d_inode != NULL)
2314 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002315 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002316 if (dentry->d_inode)
2317 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002318 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002319 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002320 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002321 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
David Quigley14c43f72013-05-22 12:50:43 -04002323 if (label) {
2324 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2325 if (IS_ERR(olabel)) {
2326 status = PTR_ERR(olabel);
2327 goto err_opendata_put;
2328 }
2329 }
2330
Trond Myklebuste911b812014-03-26 13:24:37 -07002331 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2332 if (!opendata->f_attr.mdsthreshold) {
2333 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2334 if (!opendata->f_attr.mdsthreshold)
2335 goto err_free_label;
2336 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002337 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002338 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002339 if (dentry->d_inode != NULL)
2340 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002341
Trond Myklebust3efb9722013-05-29 13:17:04 -04002342 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002343 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002344 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002345 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002346
Trond Myklebust549b19c2013-04-16 18:42:34 -04002347 if ((opendata->o_arg.open_flags & O_EXCL) &&
2348 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002349 nfs4_exclusive_attrset(opendata, sattr);
2350
2351 nfs_fattr_init(opendata->o_res.f_attr);
2352 status = nfs4_do_setattr(state->inode, cred,
2353 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002354 state, label, olabel);
2355 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002356 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002357 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002358 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002359 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002360 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002361 if (opendata->file_created)
2362 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002363
Trond Myklebuste911b812014-03-26 13:24:37 -07002364 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002365 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002366 opendata->f_attr.mdsthreshold = NULL;
2367 }
Andy Adamson82be4172012-05-23 05:02:35 -04002368
David Quigley14c43f72013-05-22 12:50:43 -04002369 nfs4_label_free(olabel);
2370
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002371 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002374err_free_label:
2375 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002376err_opendata_put:
2377 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002378err_put_state_owner:
2379 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return status;
2382}
2383
2384
Andy Adamson82be4172012-05-23 05:02:35 -04002385static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002386 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002387 int flags,
2388 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002389 struct nfs4_label *label,
2390 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002392 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 struct nfs4_exception exception = { };
2394 struct nfs4_state *res;
2395 int status;
2396
2397 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002398 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002399 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002400 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (status == 0)
2402 break;
2403 /* NOTE: BAD_SEQID means the server and client disagree about the
2404 * book-keeping w.r.t. state-changing operations
2405 * (OPEN/CLOSE/LOCK/LOCKU...)
2406 * It is actually a sign of a bug on the client or on the server.
2407 *
2408 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002409 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 * have unhashed the old state_owner for us, and that we can
2411 * therefore safely retry using a new one. We should still warn
2412 * the user though...
2413 */
2414 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002415 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002416 " returned a bad sequence-id error!\n",
2417 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 exception.retry = 1;
2419 continue;
2420 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002421 /*
2422 * BAD_STATEID on OPEN means that the server cancelled our
2423 * state before it received the OPEN_CONFIRM.
2424 * Recover by retrying the request as per the discussion
2425 * on Page 181 of RFC3530.
2426 */
2427 if (status == -NFS4ERR_BAD_STATEID) {
2428 exception.retry = 1;
2429 continue;
2430 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002431 if (status == -EAGAIN) {
2432 /* We must have found a delegation */
2433 exception.retry = 1;
2434 continue;
2435 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002436 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2437 continue;
2438 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 status, &exception));
2440 } while (exception.retry);
2441 return res;
2442}
2443
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002444static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2445 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002446 struct nfs4_state *state, struct nfs4_label *ilabel,
2447 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002449 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002451 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 .iap = sattr,
2453 .server = server,
2454 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002455 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 };
2457 struct nfs_setattrres res = {
2458 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002459 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 .server = server,
2461 };
2462 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2464 .rpc_argp = &arg,
2465 .rpc_resp = &res,
2466 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002468 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002469 fmode_t fmode;
2470 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002471 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
David Quigleyaa9c2662013-05-22 12:50:44 -04002473 arg.bitmask = nfs4_bitmask(server, ilabel);
2474 if (ilabel)
2475 arg.bitmask = nfs4_bitmask(server, olabel);
2476
Trond Myklebust0e574af2005-10-27 22:12:38 -04002477 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Trond Myklebustee3ae842013-04-29 10:35:36 -04002479 /* Servers should only apply open mode checks for file size changes */
2480 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2481 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2482
2483 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2484 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002485 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002486 struct nfs_lockowner lockowner = {
2487 .l_owner = current->files,
2488 .l_pid = current->tgid,
2489 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002490 if (!nfs4_valid_open_stateid(state))
2491 return -EBADF;
2492 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2493 &lockowner) == -EIO)
2494 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002495 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002496 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Bryan Schumaker7c513052011-03-24 17:12:24 +00002498 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002499 if (status == 0 && state != NULL)
2500 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002501 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002504static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2505 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002506 struct nfs4_state *state, struct nfs4_label *ilabel,
2507 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002509 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002510 struct nfs4_exception exception = {
2511 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002512 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002513 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 int err;
2515 do {
David Quigley1775fd32013-05-22 12:50:42 -04002516 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002517 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002518 switch (err) {
2519 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002520 if (!(sattr->ia_valid & ATTR_SIZE)) {
2521 pr_warn_once("NFSv4: server %s is incorrectly "
2522 "applying open mode checks to "
2523 "a SETATTR that is not "
2524 "changing file size.\n",
2525 server->nfs_client->cl_hostname);
2526 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002527 if (state && !(state->state & FMODE_WRITE)) {
2528 err = -EBADF;
2529 if (sattr->ia_valid & ATTR_OPEN)
2530 err = -EACCES;
2531 goto out;
2532 }
2533 }
2534 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002536out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return err;
2538}
2539
2540struct nfs4_closedata {
2541 struct inode *inode;
2542 struct nfs4_state *state;
2543 struct nfs_closeargs arg;
2544 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002545 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002546 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002547 bool roc;
2548 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549};
2550
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002551static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002552{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002553 struct nfs4_closedata *calldata = data;
2554 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002555 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002556
Fred Isamanf7e89172011-01-06 11:36:32 +00002557 if (calldata->roc)
2558 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002559 nfs4_put_open_state(calldata->state);
2560 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002561 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002562 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002563 kfree(calldata);
2564}
2565
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002566static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002568 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002571 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Chuck Levera3ca5652012-03-01 17:00:40 -05002573 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002574 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2575 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002576 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 /* hmm. we are done with the inode, and in the process of freeing
2578 * the state_owner. we keep this around to process errors
2579 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 switch (task->tk_status) {
2581 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002582 res_stateid = &calldata->res.stateid;
2583 if (calldata->arg.fmode == 0 && calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002584 pnfs_roc_set_barrier(state->inode,
2585 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002586 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002587 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002588 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002590 case -NFS4ERR_OLD_STATEID:
2591 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002593 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002596 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002597 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002598 goto out_release;
2599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
Trond Myklebust412f6c42014-08-25 22:09:08 -04002601 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002602out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002603 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002604 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002605 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002608static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002610 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002611 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002612 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002613 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002614 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002615
Chuck Levera3ca5652012-03-01 17:00:40 -05002616 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002617 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002618 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002619
Trond Myklebust88069f72009-12-08 08:33:16 -05002620 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002621 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002622 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2623 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2624 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2625 /* Calculate the current open share mode */
2626 calldata->arg.fmode = 0;
2627 if (is_rdonly || is_rdwr)
2628 calldata->arg.fmode |= FMODE_READ;
2629 if (is_wronly || is_rdwr)
2630 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002631 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002632 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002633 if (state->n_rdonly == 0) {
Trond Myklebustaee7af32014-08-25 22:33:12 -04002634 call_close |= is_rdonly || is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002635 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002636 }
2637 if (state->n_wronly == 0) {
Trond Myklebustaee7af32014-08-25 22:33:12 -04002638 call_close |= is_wronly || is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002639 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002640 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002641 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002642 if (!nfs4_valid_open_stateid(state))
2643 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002644 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002645
2646 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002647 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002648 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002649 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002650
Fred Isamanf7e89172011-01-06 11:36:32 +00002651 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002652 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002653 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002654 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2655 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002656 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002657 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002658 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002659
Trond Myklebust516a6af2005-10-27 22:12:41 -04002660 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002661 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002662 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002663 &calldata->arg.seq_args,
2664 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002665 task) != 0)
2666 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002667 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002668 return;
2669out_no_action:
2670 task->tk_action = NULL;
2671out_wait:
2672 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002675static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002676 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002677 .rpc_call_done = nfs4_close_done,
2678 .rpc_release = nfs4_free_closedata,
2679};
2680
Peng Taofe08c542014-07-03 13:05:01 +08002681static bool nfs4_state_has_opener(struct nfs4_state *state)
2682{
2683 /* first check existing openers */
2684 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 &&
2685 state->n_rdonly != 0)
2686 return true;
2687
2688 if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 &&
2689 state->n_wronly != 0)
2690 return true;
2691
2692 if (test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 &&
2693 state->n_rdwr != 0)
2694 return true;
2695
2696 return false;
2697}
2698
2699static bool nfs4_roc(struct inode *inode)
2700{
2701 struct nfs_inode *nfsi = NFS_I(inode);
2702 struct nfs_open_context *ctx;
2703 struct nfs4_state *state;
2704
2705 spin_lock(&inode->i_lock);
2706 list_for_each_entry(ctx, &nfsi->open_files, list) {
2707 state = ctx->state;
2708 if (state == NULL)
2709 continue;
2710 if (nfs4_state_has_opener(state)) {
2711 spin_unlock(&inode->i_lock);
2712 return false;
2713 }
2714 }
2715 spin_unlock(&inode->i_lock);
2716
2717 if (nfs4_check_delegation(inode, FMODE_READ))
2718 return false;
2719
2720 return pnfs_roc(inode);
2721}
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723/*
2724 * It is possible for data to be read/written from a mem-mapped file
2725 * after the sys_close call (which hits the vfs layer as a flush).
2726 * This means that we can't safely call nfsv4 close on a file until
2727 * the inode is cleared. This in turn means that we are not good
2728 * NFSv4 citizens - we do not indicate to the server to update the file's
2729 * share state even when we are done with one of the three share
2730 * stateid's in the inode.
2731 *
2732 * NOTE: Caller must be holding the sp->so_owner semaphore!
2733 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002734int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002736 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002738 struct nfs4_state_owner *sp = state->owner;
2739 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002740 struct rpc_message msg = {
2741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2742 .rpc_cred = state->owner->so_cred,
2743 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002744 struct rpc_task_setup task_setup_data = {
2745 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002746 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002747 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002748 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002749 .flags = RPC_TASK_ASYNC,
2750 };
Trond Myklebust95121352005-10-18 14:20:12 -07002751 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002753 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2754 &task_setup_data.rpc_client, &msg);
2755
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002756 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002758 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002759 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002760 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002762 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002763 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002765 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002766 if (calldata->arg.seqid == NULL)
2767 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002768 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002769 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002770 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002771 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002772 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002773 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002774 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002775
Trond Myklebust1174dd12010-12-21 10:52:24 -05002776 msg.rpc_argp = &calldata->arg;
2777 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002778 task_setup_data.callback_data = calldata;
2779 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002780 if (IS_ERR(task))
2781 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002782 status = 0;
2783 if (wait)
2784 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002785 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002786 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002787out_free_calldata:
2788 kfree(calldata);
2789out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002790 nfs4_put_open_state(state);
2791 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002792 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
Trond Myklebust2b484292010-09-17 10:56:51 -04002795static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002796nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2797 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002800 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2801
2802 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Trond Myklebust565277f2007-10-15 18:17:53 -04002804 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002805 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002806
2807 nfs4_label_release_security(label);
2808
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002809 if (IS_ERR(state))
2810 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002811 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
Trond Myklebust1185a552009-12-03 15:54:02 -05002814static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002815{
2816 if (ctx->state == NULL)
2817 return;
2818 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002819 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002820 else
Al Viro643168c2011-06-22 18:20:23 -04002821 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Trond Myklebustb944dba2013-11-04 15:20:20 -05002824#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2825#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002826#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2829{
Benny Halevy43652ad2009-04-01 09:21:54 -04002830 struct nfs4_server_caps_arg args = {
2831 .fhandle = fhandle,
2832 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 struct nfs4_server_caps_res res = {};
2834 struct rpc_message msg = {
2835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002836 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 .rpc_resp = &res,
2838 };
2839 int status;
2840
Bryan Schumaker7c513052011-03-24 17:12:24 +00002841 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002843 /* Sanity check the server answers */
2844 switch (server->nfs_client->cl_minorversion) {
2845 case 0:
2846 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2847 res.attr_bitmask[2] = 0;
2848 break;
2849 case 1:
2850 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2851 break;
2852 case 2:
2853 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002856 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2857 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2858 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2859 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002860 NFS_CAP_CTIME|NFS_CAP_MTIME|
2861 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002862 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2863 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 server->caps |= NFS_CAP_ACLS;
2865 if (res.has_links != 0)
2866 server->caps |= NFS_CAP_HARDLINKS;
2867 if (res.has_symlinks != 0)
2868 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002869 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2870 server->caps |= NFS_CAP_FILEID;
2871 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2872 server->caps |= NFS_CAP_MODE;
2873 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2874 server->caps |= NFS_CAP_NLINK;
2875 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2876 server->caps |= NFS_CAP_OWNER;
2877 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2878 server->caps |= NFS_CAP_OWNER_GROUP;
2879 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2880 server->caps |= NFS_CAP_ATIME;
2881 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2882 server->caps |= NFS_CAP_CTIME;
2883 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2884 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002885#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2886 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2887 server->caps |= NFS_CAP_SECURITY_LABEL;
2888#endif
2889 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2890 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002891 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002892
Trond Myklebusta65318b2009-03-11 14:10:28 -04002893 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2894 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2895 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002896 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002898 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return status;
2902}
2903
Trond Myklebust55a97592006-06-09 09:34:19 -04002904int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908 do {
2909 err = nfs4_handle_exception(server,
2910 _nfs4_server_capabilities(server, fhandle),
2911 &exception);
2912 } while (exception.retry);
2913 return err;
2914}
2915
2916static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2917 struct nfs_fsinfo *info)
2918{
David Quigleyaa9c2662013-05-22 12:50:44 -04002919 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002921 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 };
2923 struct nfs4_lookup_res res = {
2924 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002925 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 .fh = fhandle,
2927 };
2928 struct rpc_message msg = {
2929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2930 .rpc_argp = &args,
2931 .rpc_resp = &res,
2932 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002933
David Quigleyaa9c2662013-05-22 12:50:44 -04002934 bitmask[0] = nfs4_fattr_bitmap[0];
2935 bitmask[1] = nfs4_fattr_bitmap[1];
2936 /*
2937 * Process the label in the upcoming getfattr
2938 */
2939 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2940
Trond Myklebust0e574af2005-10-27 22:12:38 -04002941 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002942 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943}
2944
2945static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2946 struct nfs_fsinfo *info)
2947{
2948 struct nfs4_exception exception = { };
2949 int err;
2950 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002951 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002952 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002953 switch (err) {
2954 case 0:
2955 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002956 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002957 default:
2958 err = nfs4_handle_exception(server, err, &exception);
2959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002961out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return err;
2963}
2964
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002965static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2966 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2967{
Trond Myklebustc2190662013-08-26 19:23:04 -04002968 struct rpc_auth_create_args auth_args = {
2969 .pseudoflavor = flavor,
2970 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002971 struct rpc_auth *auth;
2972 int ret;
2973
Trond Myklebustc2190662013-08-26 19:23:04 -04002974 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002975 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002976 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002977 goto out;
2978 }
2979 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002980out:
2981 return ret;
2982}
2983
Chuck Lever9a744ba2013-03-16 15:56:02 -04002984/*
2985 * Retry pseudoroot lookup with various security flavors. We do this when:
2986 *
2987 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2988 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2989 *
2990 * Returns zero on success, or a negative NFS4ERR value, or a
2991 * negative errno value.
2992 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002993static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002994 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002996 /* Per 3530bis 15.33.5 */
2997 static const rpc_authflavor_t flav_array[] = {
2998 RPC_AUTH_GSS_KRB5P,
2999 RPC_AUTH_GSS_KRB5I,
3000 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003001 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003002 RPC_AUTH_NULL,
3003 };
3004 int status = -EPERM;
3005 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003007 if (server->auth_info.flavor_len > 0) {
3008 /* try each flavor specified by user */
3009 for (i = 0; i < server->auth_info.flavor_len; i++) {
3010 status = nfs4_lookup_root_sec(server, fhandle, info,
3011 server->auth_info.flavors[i]);
3012 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3013 continue;
3014 break;
3015 }
3016 } else {
3017 /* no flavors specified by user, try default list */
3018 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3019 status = nfs4_lookup_root_sec(server, fhandle, info,
3020 flav_array[i]);
3021 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3022 continue;
3023 break;
3024 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003025 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003026
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003027 /*
3028 * -EACCESS could mean that the user doesn't have correct permissions
3029 * to access the mount. It could also mean that we tried to mount
3030 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3031 * existing mount programs don't handle -EACCES very well so it should
3032 * be mapped to -EPERM instead.
3033 */
3034 if (status == -EACCES)
3035 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003036 return status;
3037}
3038
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003039static int nfs4_do_find_root_sec(struct nfs_server *server,
3040 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3041{
3042 int mv = server->nfs_client->cl_minorversion;
3043 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3044}
3045
3046/**
3047 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3048 * @server: initialized nfs_server handle
3049 * @fhandle: we fill in the pseudo-fs root file handle
3050 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003051 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003052 *
3053 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003054 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003055int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003056 struct nfs_fsinfo *info,
3057 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003058{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003059 int status;
3060
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003061 switch (auth_probe) {
3062 case false:
3063 status = nfs4_lookup_root(server, fhandle, info);
3064 if (status != -NFS4ERR_WRONGSEC)
3065 break;
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003066 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003067 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003068 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (status == 0)
3071 status = nfs4_server_capabilities(server, fhandle);
3072 if (status == 0)
3073 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003074
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003075 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
Bryan Schumakerbae36242012-05-10 15:07:31 -04003078static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3079 struct nfs_fsinfo *info)
3080{
3081 int error;
3082 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003083 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003084
3085 error = nfs4_server_capabilities(server, mntfh);
3086 if (error < 0) {
3087 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3088 return error;
3089 }
3090
David Quigley14c43f72013-05-22 12:50:43 -04003091 label = nfs4_label_alloc(server, GFP_KERNEL);
3092 if (IS_ERR(label))
3093 return PTR_ERR(label);
3094
David Quigley1775fd32013-05-22 12:50:42 -04003095 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003096 if (error < 0) {
3097 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003098 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003099 }
3100
3101 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3102 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3103 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3104
David Quigley14c43f72013-05-22 12:50:43 -04003105err_free_label:
3106 nfs4_label_free(label);
3107
Bryan Schumakerbae36242012-05-10 15:07:31 -04003108 return error;
3109}
3110
Manoj Naik6b97fd32006-06-09 09:34:29 -04003111/*
3112 * Get locations and (maybe) other attributes of a referral.
3113 * Note that we'll actually follow the referral later when
3114 * we detect fsid mismatch in inode revalidation
3115 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003116static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3117 const struct qstr *name, struct nfs_fattr *fattr,
3118 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003119{
3120 int status = -ENOMEM;
3121 struct page *page = NULL;
3122 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003123
3124 page = alloc_page(GFP_KERNEL);
3125 if (page == NULL)
3126 goto out;
3127 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3128 if (locations == NULL)
3129 goto out;
3130
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003131 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003132 if (status != 0)
3133 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003134
3135 /*
3136 * If the fsid didn't change, this is a migration event, not a
3137 * referral. Cause us to drop into the exception handler, which
3138 * will kick off migration recovery.
3139 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003140 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003141 dprintk("%s: server did not return a different fsid for"
3142 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003143 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003144 goto out;
3145 }
Andy Adamson533eb462011-06-13 18:25:56 -04003146 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3147 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003148
Andy Adamson533eb462011-06-13 18:25:56 -04003149 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003150 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003151 memset(fhandle, 0, sizeof(struct nfs_fh));
3152out:
3153 if (page)
3154 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003155 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003156 return status;
3157}
3158
David Quigley1775fd32013-05-22 12:50:42 -04003159static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3160 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 struct nfs4_getattr_arg args = {
3163 .fh = fhandle,
3164 .bitmask = server->attr_bitmask,
3165 };
3166 struct nfs4_getattr_res res = {
3167 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003168 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 .server = server,
3170 };
3171 struct rpc_message msg = {
3172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3173 .rpc_argp = &args,
3174 .rpc_resp = &res,
3175 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003176
3177 args.bitmask = nfs4_bitmask(server, label);
3178
Trond Myklebust0e574af2005-10-27 22:12:38 -04003179 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003180 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
David Quigley1775fd32013-05-22 12:50:42 -04003183static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3184 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
3186 struct nfs4_exception exception = { };
3187 int err;
3188 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003189 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3190 trace_nfs4_getattr(server, fhandle, fattr, err);
3191 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 &exception);
3193 } while (exception.retry);
3194 return err;
3195}
3196
3197/*
3198 * The file is not closed if it is opened due to the a request to change
3199 * the size of the file. The open call will not be needed once the
3200 * VFS layer lookup-intents are implemented.
3201 *
3202 * Close is called when the inode is destroyed.
3203 * If we haven't opened the file for O_WRONLY, we
3204 * need to in the size_change case to obtain a stateid.
3205 *
3206 * Got race?
3207 * Because OPEN is always done by name in nfsv4, it is
3208 * possible that we opened a different file by the same
3209 * name. We can recognize this race condition, but we
3210 * can't do anything about it besides returning an error.
3211 *
3212 * This will be fixed with VFS changes (lookup-intent).
3213 */
3214static int
3215nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3216 struct iattr *sattr)
3217{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003218 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003219 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003220 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003221 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 int status;
3223
Peng Tao88ac8152014-09-12 11:04:10 +08003224 if (pnfs_ld_layoutret_on_setattr(inode) &&
3225 sattr->ia_valid & ATTR_SIZE &&
3226 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003227 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003228
Trond Myklebust0e574af2005-10-27 22:12:38 -04003229 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Andy Adamson26699402012-05-30 16:12:24 -04003231 /* Deal with open(O_TRUNC) */
3232 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003233 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003234
3235 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003236 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003237 return 0;
3238
Trond Myklebustd5308382005-11-04 15:33:38 -05003239 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003240 if (sattr->ia_valid & ATTR_FILE) {
3241 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003242
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003243 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003244 if (ctx) {
3245 cred = ctx->cred;
3246 state = ctx->state;
3247 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003248 }
3249
David Quigley14c43f72013-05-22 12:50:43 -04003250 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3251 if (IS_ERR(label))
3252 return PTR_ERR(label);
3253
3254 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003255 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003256 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003257 nfs_setsecurity(inode, fattr, label);
3258 }
David Quigley14c43f72013-05-22 12:50:43 -04003259 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return status;
3261}
3262
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003263static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3264 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003265 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003266{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003267 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003268 int status;
3269 struct nfs4_lookup_arg args = {
3270 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003271 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003272 .name = name,
3273 };
3274 struct nfs4_lookup_res res = {
3275 .server = server,
3276 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003277 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003278 .fh = fhandle,
3279 };
3280 struct rpc_message msg = {
3281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3282 .rpc_argp = &args,
3283 .rpc_resp = &res,
3284 };
3285
David Quigleyaa9c2662013-05-22 12:50:44 -04003286 args.bitmask = nfs4_bitmask(server, label);
3287
David Howells2b3de442006-08-22 20:06:09 -04003288 nfs_fattr_init(fattr);
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003291 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 dprintk("NFS reply lookup: %d\n", status);
3293 return status;
3294}
3295
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003296static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003297{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003298 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003299 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003300 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3301 fattr->nlink = 2;
3302}
3303
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003304static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3305 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003306 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003307{
3308 struct nfs4_exception exception = { };
3309 struct rpc_clnt *client = *clnt;
3310 int err;
3311 do {
David Quigley1775fd32013-05-22 12:50:42 -04003312 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003313 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003314 switch (err) {
3315 case -NFS4ERR_BADNAME:
3316 err = -ENOENT;
3317 goto out;
3318 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003319 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003320 goto out;
3321 case -NFS4ERR_WRONGSEC:
3322 err = -EPERM;
3323 if (client != *clnt)
3324 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003325 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003326 if (IS_ERR(client))
3327 return PTR_ERR(client);
3328
3329 exception.retry = 1;
3330 break;
3331 default:
3332 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3333 }
3334 } while (exception.retry);
3335
3336out:
3337 if (err == 0)
3338 *clnt = client;
3339 else if (client != *clnt)
3340 rpc_shutdown_client(client);
3341
3342 return err;
3343}
3344
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003345static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003346 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3347 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003349 int status;
3350 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003351
David Quigley1775fd32013-05-22 12:50:42 -04003352 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003353 if (client != NFS_CLIENT(dir)) {
3354 rpc_shutdown_client(client);
3355 nfs_fixup_secinfo_attributes(fattr);
3356 }
3357 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003360struct rpc_clnt *
3361nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3362 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3363{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003364 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003365 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003366
David Quigley1775fd32013-05-22 12:50:42 -04003367 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003368 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003369 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003370 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003371}
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3374{
Trond Myklebust76b32992007-08-10 17:45:11 -04003375 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 struct nfs4_accessargs args = {
3377 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003378 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003380 struct nfs4_accessres res = {
3381 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3385 .rpc_argp = &args,
3386 .rpc_resp = &res,
3387 .rpc_cred = entry->cred,
3388 };
3389 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003390 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
3392 /*
3393 * Determine which access bits we want to ask for...
3394 */
3395 if (mode & MAY_READ)
3396 args.access |= NFS4_ACCESS_READ;
3397 if (S_ISDIR(inode->i_mode)) {
3398 if (mode & MAY_WRITE)
3399 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3400 if (mode & MAY_EXEC)
3401 args.access |= NFS4_ACCESS_LOOKUP;
3402 } else {
3403 if (mode & MAY_WRITE)
3404 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3405 if (mode & MAY_EXEC)
3406 args.access |= NFS4_ACCESS_EXECUTE;
3407 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003408
3409 res.fattr = nfs_alloc_fattr();
3410 if (res.fattr == NULL)
3411 return -ENOMEM;
3412
Bryan Schumaker7c513052011-03-24 17:12:24 +00003413 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003415 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003416 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003418 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return status;
3420}
3421
3422static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3423{
3424 struct nfs4_exception exception = { };
3425 int err;
3426 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003427 err = _nfs4_proc_access(inode, entry);
3428 trace_nfs4_access(inode, err);
3429 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 &exception);
3431 } while (exception.retry);
3432 return err;
3433}
3434
3435/*
3436 * TODO: For the time being, we don't try to get any attributes
3437 * along with any of the zero-copy operations READ, READDIR,
3438 * READLINK, WRITE.
3439 *
3440 * In the case of the first three, we want to put the GETATTR
3441 * after the read-type operation -- this is because it is hard
3442 * to predict the length of a GETATTR response in v4, and thus
3443 * align the READ data correctly. This means that the GETATTR
3444 * may end up partially falling into the page cache, and we should
3445 * shift it into the 'tail' of the xdr_buf before processing.
3446 * To do this efficiently, we need to know the total length
3447 * of data received, which doesn't seem to be available outside
3448 * of the RPC layer.
3449 *
3450 * In the case of WRITE, we also want to put the GETATTR after
3451 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003452 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 *
3454 * Both of these changes to the XDR layer would in fact be quite
3455 * minor, but I decided to leave them for a subsequent patch.
3456 */
3457static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3458 unsigned int pgbase, unsigned int pglen)
3459{
3460 struct nfs4_readlink args = {
3461 .fh = NFS_FH(inode),
3462 .pgbase = pgbase,
3463 .pglen = pglen,
3464 .pages = &page,
3465 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003466 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 struct rpc_message msg = {
3468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3469 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003470 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 };
3472
Bryan Schumaker7c513052011-03-24 17:12:24 +00003473 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 -07003474}
3475
3476static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3477 unsigned int pgbase, unsigned int pglen)
3478{
3479 struct nfs4_exception exception = { };
3480 int err;
3481 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003482 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3483 trace_nfs4_readlink(inode, err);
3484 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 &exception);
3486 } while (exception.retry);
3487 return err;
3488}
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003491 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493static int
3494nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003495 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
David Quigleyaa9c2662013-05-22 12:50:44 -04003497 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003498 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003500 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 int status = 0;
3502
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003503 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3504 if (IS_ERR(ctx))
3505 return PTR_ERR(ctx);
3506
David Quigleyaa9c2662013-05-22 12:50:44 -04003507 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3508
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003509 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003510 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (IS_ERR(state)) {
3512 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003516 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003517 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 return status;
3519}
3520
3521static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3522{
Trond Myklebust16e42952005-10-27 22:12:44 -04003523 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003524 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003526 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003528 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003529 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003530 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3533 .rpc_argp = &args,
3534 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003536 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003537
Bryan Schumaker7c513052011-03-24 17:12:24 +00003538 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003539 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003540 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 return status;
3542}
3543
3544static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3545{
3546 struct nfs4_exception exception = { };
3547 int err;
3548 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003549 err = _nfs4_proc_remove(dir, name);
3550 trace_nfs4_remove(dir, name, err);
3551 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 &exception);
3553 } while (exception.retry);
3554 return err;
3555}
3556
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003557static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003559 struct nfs_server *server = NFS_SERVER(dir);
3560 struct nfs_removeargs *args = msg->rpc_argp;
3561 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003563 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003565 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003566
3567 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003570static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3571{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003572 nfs4_setup_sequence(NFS_SERVER(data->dir),
3573 &data->args.seq_args,
3574 &data->res.seq_res,
3575 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003578static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003580 struct nfs_unlinkdata *data = task->tk_calldata;
3581 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003582
Trond Myklebust14516c32010-07-31 14:29:06 -04003583 if (!nfs4_sequence_done(task, &res->seq_res))
3584 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003585 if (nfs4_async_handle_error(task, res->server, NULL,
3586 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003587 return 0;
3588 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003589 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
3591
Jeff Laytond3d41522010-09-17 17:31:57 -04003592static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3593{
3594 struct nfs_server *server = NFS_SERVER(dir);
3595 struct nfs_renameargs *arg = msg->rpc_argp;
3596 struct nfs_renameres *res = msg->rpc_resp;
3597
3598 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003599 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003600 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003601}
3602
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003603static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3604{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003605 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3606 &data->args.seq_args,
3607 &data->res.seq_res,
3608 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003609}
3610
3611static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3612 struct inode *new_dir)
3613{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003614 struct nfs_renamedata *data = task->tk_calldata;
3615 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003616
3617 if (!nfs4_sequence_done(task, &res->seq_res))
3618 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003619 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003620 return 0;
3621
3622 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003623 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003624 return 1;
3625}
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3628{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003629 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 struct nfs4_link_arg arg = {
3631 .fh = NFS_FH(inode),
3632 .dir_fh = NFS_FH(dir),
3633 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003634 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003636 struct nfs4_link_res res = {
3637 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003638 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003639 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 struct rpc_message msg = {
3641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3642 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003643 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003645 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Trond Myklebust136f2622010-04-16 16:22:49 -04003647 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003648 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003649 goto out;
3650
David Quigley14c43f72013-05-22 12:50:43 -04003651 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3652 if (IS_ERR(res.label)) {
3653 status = PTR_ERR(res.label);
3654 goto out;
3655 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003656 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003657
Bryan Schumaker7c513052011-03-24 17:12:24 +00003658 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003659 if (!status) {
3660 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003661 status = nfs_post_op_update_inode(inode, res.fattr);
3662 if (!status)
3663 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003664 }
David Quigley14c43f72013-05-22 12:50:43 -04003665
3666
3667 nfs4_label_free(res.label);
3668
Trond Myklebust136f2622010-04-16 16:22:49 -04003669out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003670 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return status;
3672}
3673
3674static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3675{
3676 struct nfs4_exception exception = { };
3677 int err;
3678 do {
3679 err = nfs4_handle_exception(NFS_SERVER(inode),
3680 _nfs4_proc_link(inode, dir, name),
3681 &exception);
3682 } while (exception.retry);
3683 return err;
3684}
3685
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003686struct nfs4_createdata {
3687 struct rpc_message msg;
3688 struct nfs4_create_arg arg;
3689 struct nfs4_create_res res;
3690 struct nfs_fh fh;
3691 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003692 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003693};
3694
3695static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3696 struct qstr *name, struct iattr *sattr, u32 ftype)
3697{
3698 struct nfs4_createdata *data;
3699
3700 data = kzalloc(sizeof(*data), GFP_KERNEL);
3701 if (data != NULL) {
3702 struct nfs_server *server = NFS_SERVER(dir);
3703
David Quigley14c43f72013-05-22 12:50:43 -04003704 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3705 if (IS_ERR(data->label))
3706 goto out_free;
3707
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003708 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3709 data->msg.rpc_argp = &data->arg;
3710 data->msg.rpc_resp = &data->res;
3711 data->arg.dir_fh = NFS_FH(dir);
3712 data->arg.server = server;
3713 data->arg.name = name;
3714 data->arg.attrs = sattr;
3715 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003716 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003717 data->res.server = server;
3718 data->res.fh = &data->fh;
3719 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003720 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003721 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003722 }
3723 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003724out_free:
3725 kfree(data);
3726 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003727}
3728
3729static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3730{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003731 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003732 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003733 if (status == 0) {
3734 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003735 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003736 }
3737 return status;
3738}
3739
3740static void nfs4_free_createdata(struct nfs4_createdata *data)
3741{
David Quigley14c43f72013-05-22 12:50:43 -04003742 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003743 kfree(data);
3744}
3745
Chuck Lever4f390c12006-08-22 20:06:22 -04003746static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003747 struct page *page, unsigned int len, struct iattr *sattr,
3748 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003750 struct nfs4_createdata *data;
3751 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Chuck Lever94a6d752006-08-22 20:06:23 -04003753 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003754 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003755
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003756 status = -ENOMEM;
3757 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3758 if (data == NULL)
3759 goto out;
3760
3761 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3762 data->arg.u.symlink.pages = &page;
3763 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003764 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003766 status = nfs4_do_create(dir, dentry, data);
3767
3768 nfs4_free_createdata(data);
3769out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 return status;
3771}
3772
Chuck Lever4f390c12006-08-22 20:06:22 -04003773static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003774 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
3776 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003777 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003779
3780 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003783 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3784 trace_nfs4_symlink(dir, &dentry->d_name, err);
3785 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 &exception);
3787 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003788
3789 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return err;
3791}
3792
3793static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003794 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003796 struct nfs4_createdata *data;
3797 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003799 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3800 if (data == NULL)
3801 goto out;
3802
David Quigley1775fd32013-05-22 12:50:42 -04003803 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003804 status = nfs4_do_create(dir, dentry, data);
3805
3806 nfs4_free_createdata(data);
3807out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 return status;
3809}
3810
3811static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3812 struct iattr *sattr)
3813{
3814 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003815 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003817
David Quigleyaa9c2662013-05-22 12:50:44 -04003818 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3819
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003820 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003822 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3823 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3824 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 &exception);
3826 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003827 nfs4_label_release_security(label);
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 return err;
3830}
3831
3832static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003833 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
3835 struct inode *dir = dentry->d_inode;
3836 struct nfs4_readdir_arg args = {
3837 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003838 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 .pgbase = 0,
3840 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003841 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003842 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 };
3844 struct nfs4_readdir_res res;
3845 struct rpc_message msg = {
3846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3847 .rpc_argp = &args,
3848 .rpc_resp = &res,
3849 .rpc_cred = cred,
3850 };
3851 int status;
3852
Al Viro6de14722013-09-16 10:53:17 -04003853 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3854 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003855 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003856 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003858 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 -05003859 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003860 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003861 status += args.pgbase;
3862 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003863
3864 nfs_invalidate_atime(dir);
3865
Harvey Harrison3110ff82008-05-02 13:42:44 -07003866 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 return status;
3868}
3869
3870static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003871 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
3873 struct nfs4_exception exception = { };
3874 int err;
3875 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003876 err = _nfs4_proc_readdir(dentry, cred, cookie,
3877 pages, count, plus);
3878 trace_nfs4_readdir(dentry->d_inode, err);
3879 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 &exception);
3881 } while (exception.retry);
3882 return err;
3883}
3884
3885static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003886 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003888 struct nfs4_createdata *data;
3889 int mode = sattr->ia_mode;
3890 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003892 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3893 if (data == NULL)
3894 goto out;
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003897 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003899 data->arg.ftype = NF4BLK;
3900 data->arg.u.device.specdata1 = MAJOR(rdev);
3901 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
3903 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003904 data->arg.ftype = NF4CHR;
3905 data->arg.u.device.specdata1 = MAJOR(rdev);
3906 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003907 } else if (!S_ISSOCK(mode)) {
3908 status = -EINVAL;
3909 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
David Quigley1775fd32013-05-22 12:50:42 -04003911
David Quigleyaa9c2662013-05-22 12:50:44 -04003912 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003913 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003914out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003915 nfs4_free_createdata(data);
3916out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 return status;
3918}
3919
3920static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3921 struct iattr *sattr, dev_t rdev)
3922{
3923 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003924 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003926
David Quigleyaa9c2662013-05-22 12:50:44 -04003927 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3928
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003929 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003931 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3932 trace_nfs4_mknod(dir, &dentry->d_name, err);
3933 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 &exception);
3935 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003936
3937 nfs4_label_release_security(label);
3938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 return err;
3940}
3941
3942static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3943 struct nfs_fsstat *fsstat)
3944{
3945 struct nfs4_statfs_arg args = {
3946 .fh = fhandle,
3947 .bitmask = server->attr_bitmask,
3948 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003949 struct nfs4_statfs_res res = {
3950 .fsstat = fsstat,
3951 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 struct rpc_message msg = {
3953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3954 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003955 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 };
3957
Trond Myklebust0e574af2005-10-27 22:12:38 -04003958 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003959 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960}
3961
3962static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3963{
3964 struct nfs4_exception exception = { };
3965 int err;
3966 do {
3967 err = nfs4_handle_exception(server,
3968 _nfs4_proc_statfs(server, fhandle, fsstat),
3969 &exception);
3970 } while (exception.retry);
3971 return err;
3972}
3973
3974static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3975 struct nfs_fsinfo *fsinfo)
3976{
3977 struct nfs4_fsinfo_arg args = {
3978 .fh = fhandle,
3979 .bitmask = server->attr_bitmask,
3980 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003981 struct nfs4_fsinfo_res res = {
3982 .fsinfo = fsinfo,
3983 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 struct rpc_message msg = {
3985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3986 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003987 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 };
3989
Bryan Schumaker7c513052011-03-24 17:12:24 +00003990 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
3993static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3994{
3995 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003996 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 int err;
3998
3999 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004000 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004001 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004002 if (err == 0) {
4003 struct nfs_client *clp = server->nfs_client;
4004
4005 spin_lock(&clp->cl_lock);
4006 clp->cl_lease_time = fsinfo->lease_time * HZ;
4007 clp->cl_last_renewal = now;
4008 spin_unlock(&clp->cl_lock);
4009 break;
4010 }
4011 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 } while (exception.retry);
4013 return err;
4014}
4015
4016static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4017{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004018 int error;
4019
Trond Myklebust0e574af2005-10-27 22:12:38 -04004020 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004021 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004022 if (error == 0) {
4023 /* block layout checks this! */
4024 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004025 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004026 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004027
4028 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029}
4030
4031static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4032 struct nfs_pathconf *pathconf)
4033{
4034 struct nfs4_pathconf_arg args = {
4035 .fh = fhandle,
4036 .bitmask = server->attr_bitmask,
4037 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004038 struct nfs4_pathconf_res res = {
4039 .pathconf = pathconf,
4040 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 struct rpc_message msg = {
4042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4043 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004044 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 };
4046
4047 /* None of the pathconf attributes are mandatory to implement */
4048 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4049 memset(pathconf, 0, sizeof(*pathconf));
4050 return 0;
4051 }
4052
Trond Myklebust0e574af2005-10-27 22:12:38 -04004053 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004054 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
4057static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4058 struct nfs_pathconf *pathconf)
4059{
4060 struct nfs4_exception exception = { };
4061 int err;
4062
4063 do {
4064 err = nfs4_handle_exception(server,
4065 _nfs4_proc_pathconf(server, fhandle, pathconf),
4066 &exception);
4067 } while (exception.retry);
4068 return err;
4069}
4070
Trond Myklebust5521abf2013-03-16 20:54:34 -04004071int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004072 const struct nfs_open_context *ctx,
4073 const struct nfs_lock_context *l_ctx,
4074 fmode_t fmode)
4075{
4076 const struct nfs_lockowner *lockowner = NULL;
4077
4078 if (l_ctx != NULL)
4079 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004080 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004081}
4082EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4083
Trond Myklebust5521abf2013-03-16 20:54:34 -04004084static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4085 const struct nfs_open_context *ctx,
4086 const struct nfs_lock_context *l_ctx,
4087 fmode_t fmode)
4088{
4089 nfs4_stateid current_stateid;
4090
Trond Myklebuste1253be2014-03-05 08:44:23 -05004091 /* If the current stateid represents a lost lock, then exit */
4092 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4093 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004094 return nfs4_stateid_match(stateid, &current_stateid);
4095}
4096
4097static bool nfs4_error_stateid_expired(int err)
4098{
4099 switch (err) {
4100 case -NFS4ERR_DELEG_REVOKED:
4101 case -NFS4ERR_ADMIN_REVOKED:
4102 case -NFS4ERR_BAD_STATEID:
4103 case -NFS4ERR_STALE_STATEID:
4104 case -NFS4ERR_OLD_STATEID:
4105 case -NFS4ERR_OPENMODE:
4106 case -NFS4ERR_EXPIRED:
4107 return true;
4108 }
4109 return false;
4110}
4111
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004112void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004113{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004114 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004115}
4116
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004117static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004119 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004121 trace_nfs4_read(hdr, task->tk_status);
4122 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004123 hdr->args.context->state,
4124 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004125 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004126 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004128
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004129 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004131 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
Trond Myklebust5521abf2013-03-16 20:54:34 -04004135static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004136 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004137{
4138
4139 if (!nfs4_error_stateid_expired(task->tk_status) ||
4140 nfs4_stateid_is_current(&args->stateid,
4141 args->context,
4142 args->lock_context,
4143 FMODE_READ))
4144 return false;
4145 rpc_restart_call_prepare(task);
4146 return true;
4147}
4148
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004149static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004150{
4151
4152 dprintk("--> %s\n", __func__);
4153
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004154 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004155 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004156 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004157 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004158 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4159 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004160}
4161
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004162static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4163 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004165 hdr->timestamp = jiffies;
4166 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004167 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004168 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169}
4170
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004171static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4172 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004173{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004174 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4175 &hdr->args.seq_args,
4176 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004177 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004178 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004179 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4180 hdr->args.lock_context,
4181 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004182 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004183 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004184 return -EIO;
4185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
4187
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004188static int nfs4_write_done_cb(struct rpc_task *task,
4189 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004191 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004192
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004193 trace_nfs4_write(hdr, task->tk_status);
4194 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004195 hdr->args.context->state,
4196 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004197 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004198 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004200 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004201 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4202 nfs_post_op_update_inode_force_wcc(inode, &hdr->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004203 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205}
4206
Trond Myklebust5521abf2013-03-16 20:54:34 -04004207static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004208 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004209{
4210
4211 if (!nfs4_error_stateid_expired(task->tk_status) ||
4212 nfs4_stateid_is_current(&args->stateid,
4213 args->context,
4214 args->lock_context,
4215 FMODE_WRITE))
4216 return false;
4217 rpc_restart_call_prepare(task);
4218 return true;
4219}
4220
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004221static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004222{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004223 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004224 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004225 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004226 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004227 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4228 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004229}
4230
Trond Myklebust5a37f852012-04-28 14:55:16 -04004231static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004232bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004233{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004234 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004235 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004236 return false;
4237 /* Otherwise, request attributes if and only if we don't hold
4238 * a delegation
4239 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004240 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004241}
Fred Isamana69aef12011-03-03 15:13:47 +00004242
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004243static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4244 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004246 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004247
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004248 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4249 hdr->args.bitmask = NULL;
4250 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004251 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004252 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004253
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004254 if (!hdr->pgio_done_cb)
4255 hdr->pgio_done_cb = nfs4_write_done_cb;
4256 hdr->res.server = server;
4257 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004259 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004260 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261}
4262
Fred Isaman0b7c0152012-04-20 14:47:39 -04004263static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4264{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004265 nfs4_setup_sequence(NFS_SERVER(data->inode),
4266 &data->args.seq_args,
4267 &data->res.seq_res,
4268 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269}
4270
Fred Isaman0b7c0152012-04-20 14:47:39 -04004271static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004274
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004275 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004276 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4277 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004278 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004279 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282}
4283
Fred Isaman0b7c0152012-04-20 14:47:39 -04004284static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004285{
4286 if (!nfs4_sequence_done(task, &data->res.seq_res))
4287 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004288 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004289}
4290
Fred Isaman0b7c0152012-04-20 14:47:39 -04004291static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004293 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004294
Fred Isaman0b7c0152012-04-20 14:47:39 -04004295 if (data->commit_done_cb == NULL)
4296 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004297 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004298 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004299 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300}
4301
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004302struct nfs4_renewdata {
4303 struct nfs_client *client;
4304 unsigned long timestamp;
4305};
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307/*
4308 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4309 * standalone procedure for queueing an asynchronous RENEW.
4310 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004311static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004312{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004313 struct nfs4_renewdata *data = calldata;
4314 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004315
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004316 if (atomic_read(&clp->cl_count) > 1)
4317 nfs4_schedule_state_renewal(clp);
4318 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004319 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004320}
4321
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004322static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004324 struct nfs4_renewdata *data = calldata;
4325 struct nfs_client *clp = data->client;
4326 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004328 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004329 switch (task->tk_status) {
4330 case 0:
4331 break;
4332 case -NFS4ERR_LEASE_MOVED:
4333 nfs4_schedule_lease_moved_recovery(clp);
4334 break;
4335 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004336 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004337 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4338 return;
4339 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004340 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004341 return;
4342 }
4343 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004345 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346}
4347
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004348static const struct rpc_call_ops nfs4_renew_ops = {
4349 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004350 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004351};
4352
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004353static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
4355 struct rpc_message msg = {
4356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4357 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004358 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004360 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004362 if (renew_flags == 0)
4363 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004364 if (!atomic_inc_not_zero(&clp->cl_count))
4365 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004366 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004367 if (data == NULL)
4368 return -ENOMEM;
4369 data->client = clp;
4370 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004371 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004372 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373}
4374
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004375static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
4377 struct rpc_message msg = {
4378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4379 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004380 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 };
4382 unsigned long now = jiffies;
4383 int status;
4384
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004385 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 if (status < 0)
4387 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004388 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return 0;
4390}
4391
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004392static inline int nfs4_server_supports_acls(struct nfs_server *server)
4393{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004394 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004395}
4396
Trond Myklebust21f498c2012-08-24 10:59:25 -04004397/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4398 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004399 * the stack.
4400 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004401#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004402
Neil Hormane9e3d722011-03-04 19:26:03 -05004403static int buf_to_pages_noslab(const void *buf, size_t buflen,
4404 struct page **pages, unsigned int *pgbase)
4405{
4406 struct page *newpage, **spages;
4407 int rc = 0;
4408 size_t len;
4409 spages = pages;
4410
4411 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004412 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004413 newpage = alloc_page(GFP_KERNEL);
4414
4415 if (newpage == NULL)
4416 goto unwind;
4417 memcpy(page_address(newpage), buf, len);
4418 buf += len;
4419 buflen -= len;
4420 *pages++ = newpage;
4421 rc++;
4422 } while (buflen != 0);
4423
4424 return rc;
4425
4426unwind:
4427 for(; rc > 0; rc--)
4428 __free_page(spages[rc-1]);
4429 return -ENOMEM;
4430}
4431
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004432struct nfs4_cached_acl {
4433 int cached;
4434 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004435 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004436};
4437
4438static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004439{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004440 struct nfs_inode *nfsi = NFS_I(inode);
4441
4442 spin_lock(&inode->i_lock);
4443 kfree(nfsi->nfs4_acl);
4444 nfsi->nfs4_acl = acl;
4445 spin_unlock(&inode->i_lock);
4446}
4447
4448static void nfs4_zap_acl_attr(struct inode *inode)
4449{
4450 nfs4_set_cached_acl(inode, NULL);
4451}
4452
4453static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4454{
4455 struct nfs_inode *nfsi = NFS_I(inode);
4456 struct nfs4_cached_acl *acl;
4457 int ret = -ENOENT;
4458
4459 spin_lock(&inode->i_lock);
4460 acl = nfsi->nfs4_acl;
4461 if (acl == NULL)
4462 goto out;
4463 if (buf == NULL) /* user is just asking for length */
4464 goto out_len;
4465 if (acl->cached == 0)
4466 goto out;
4467 ret = -ERANGE; /* see getxattr(2) man page */
4468 if (acl->len > buflen)
4469 goto out;
4470 memcpy(buf, acl->data, acl->len);
4471out_len:
4472 ret = acl->len;
4473out:
4474 spin_unlock(&inode->i_lock);
4475 return ret;
4476}
4477
Sachin Prabhu5794d212012-04-17 14:36:40 +01004478static 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 +00004479{
4480 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004481 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004482
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004483 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004484 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004485 if (acl == NULL)
4486 goto out;
4487 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004488 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004489 } else {
4490 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4491 if (acl == NULL)
4492 goto out;
4493 acl->cached = 0;
4494 }
4495 acl->len = acl_len;
4496out:
4497 nfs4_set_cached_acl(inode, acl);
4498}
4499
Andy Adamsonbf118a32011-12-07 11:55:27 -05004500/*
4501 * The getxattr API returns the required buffer length when called with a
4502 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4503 * the required buf. On a NULL buf, we send a page of data to the server
4504 * guessing that the ACL request can be serviced by a page. If so, we cache
4505 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4506 * the cache. If not so, we throw away the page, and cache the required
4507 * length. The next getxattr call will then produce another round trip to
4508 * the server, this time with the input buf of the required size.
4509 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004510static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004511{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004512 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004513 struct nfs_getaclargs args = {
4514 .fh = NFS_FH(inode),
4515 .acl_pages = pages,
4516 .acl_len = buflen,
4517 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004518 struct nfs_getaclres res = {
4519 .acl_len = buflen,
4520 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004521 struct rpc_message msg = {
4522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4523 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004524 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004525 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004526 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4527 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004528
Andy Adamsonbf118a32011-12-07 11:55:27 -05004529 /* As long as we're doing a round trip to the server anyway,
4530 * let's be prepared for a page of acl data. */
4531 if (npages == 0)
4532 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004533 if (npages > ARRAY_SIZE(pages))
4534 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004535
Andy Adamsonbf118a32011-12-07 11:55:27 -05004536 for (i = 0; i < npages; i++) {
4537 pages[i] = alloc_page(GFP_KERNEL);
4538 if (!pages[i])
4539 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004540 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004541
4542 /* for decoding across pages */
4543 res.acl_scratch = alloc_page(GFP_KERNEL);
4544 if (!res.acl_scratch)
4545 goto out_free;
4546
Andy Adamsonbf118a32011-12-07 11:55:27 -05004547 args.acl_len = npages * PAGE_SIZE;
4548 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004549
Peng Taode040be2012-01-10 22:42:47 +08004550 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004551 __func__, buf, buflen, npages, args.acl_len);
4552 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4553 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004554 if (ret)
4555 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004556
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004557 /* Handle the case where the passed-in buffer is too short */
4558 if (res.acl_flags & NFS4_ACL_TRUNC) {
4559 /* Did the user only issue a request for the acl length? */
4560 if (buf == NULL)
4561 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004562 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004563 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004564 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004565 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004566 if (buf) {
4567 if (res.acl_len > buflen) {
4568 ret = -ERANGE;
4569 goto out_free;
4570 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004571 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004572 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004573out_ok:
4574 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004575out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004576 for (i = 0; i < npages; i++)
4577 if (pages[i])
4578 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004579 if (res.acl_scratch)
4580 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004581 return ret;
4582}
4583
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004584static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4585{
4586 struct nfs4_exception exception = { };
4587 ssize_t ret;
4588 do {
4589 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004590 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004591 if (ret >= 0)
4592 break;
4593 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4594 } while (exception.retry);
4595 return ret;
4596}
4597
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004598static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4599{
4600 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004601 int ret;
4602
4603 if (!nfs4_server_supports_acls(server))
4604 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004605 ret = nfs_revalidate_inode(server, inode);
4606 if (ret < 0)
4607 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004608 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4609 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004610 ret = nfs4_read_cached_acl(inode, buf, buflen);
4611 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004612 /* -ENOENT is returned if there is no ACL or if there is an ACL
4613 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004614 return ret;
4615 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004616}
4617
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004618static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004619{
4620 struct nfs_server *server = NFS_SERVER(inode);
4621 struct page *pages[NFS4ACL_MAXPAGES];
4622 struct nfs_setaclargs arg = {
4623 .fh = NFS_FH(inode),
4624 .acl_pages = pages,
4625 .acl_len = buflen,
4626 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004627 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004628 struct rpc_message msg = {
4629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4630 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004631 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004632 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004633 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004634 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004635
4636 if (!nfs4_server_supports_acls(server))
4637 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004638 if (npages > ARRAY_SIZE(pages))
4639 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004640 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4641 if (i < 0)
4642 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004643 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004644 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004645
4646 /*
4647 * Free each page after tx, so the only ref left is
4648 * held by the network stack
4649 */
4650 for (; i > 0; i--)
4651 put_page(pages[i-1]);
4652
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004653 /*
4654 * Acl update can result in inode attribute update.
4655 * so mark the attribute cache invalid.
4656 */
4657 spin_lock(&inode->i_lock);
4658 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4659 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004660 nfs_access_zap_cache(inode);
4661 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004662 return ret;
4663}
4664
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004665static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4666{
4667 struct nfs4_exception exception = { };
4668 int err;
4669 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004670 err = __nfs4_proc_set_acl(inode, buf, buflen);
4671 trace_nfs4_set_acl(inode, err);
4672 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004673 &exception);
4674 } while (exception.retry);
4675 return err;
4676}
4677
David Quigleyaa9c2662013-05-22 12:50:44 -04004678#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4679static int _nfs4_get_security_label(struct inode *inode, void *buf,
4680 size_t buflen)
4681{
4682 struct nfs_server *server = NFS_SERVER(inode);
4683 struct nfs_fattr fattr;
4684 struct nfs4_label label = {0, 0, buflen, buf};
4685
4686 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004687 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004688 .fh = NFS_FH(inode),
4689 .bitmask = bitmask,
4690 };
4691 struct nfs4_getattr_res res = {
4692 .fattr = &fattr,
4693 .label = &label,
4694 .server = server,
4695 };
4696 struct rpc_message msg = {
4697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004698 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004699 .rpc_resp = &res,
4700 };
4701 int ret;
4702
4703 nfs_fattr_init(&fattr);
4704
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004705 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004706 if (ret)
4707 return ret;
4708 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4709 return -ENOENT;
4710 if (buflen < label.len)
4711 return -ERANGE;
4712 return 0;
4713}
4714
4715static int nfs4_get_security_label(struct inode *inode, void *buf,
4716 size_t buflen)
4717{
4718 struct nfs4_exception exception = { };
4719 int err;
4720
4721 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4722 return -EOPNOTSUPP;
4723
4724 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004725 err = _nfs4_get_security_label(inode, buf, buflen);
4726 trace_nfs4_get_security_label(inode, err);
4727 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004728 &exception);
4729 } while (exception.retry);
4730 return err;
4731}
4732
4733static int _nfs4_do_set_security_label(struct inode *inode,
4734 struct nfs4_label *ilabel,
4735 struct nfs_fattr *fattr,
4736 struct nfs4_label *olabel)
4737{
4738
4739 struct iattr sattr = {0};
4740 struct nfs_server *server = NFS_SERVER(inode);
4741 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004742 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004743 .fh = NFS_FH(inode),
4744 .iap = &sattr,
4745 .server = server,
4746 .bitmask = bitmask,
4747 .label = ilabel,
4748 };
4749 struct nfs_setattrres res = {
4750 .fattr = fattr,
4751 .label = olabel,
4752 .server = server,
4753 };
4754 struct rpc_message msg = {
4755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004756 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004757 .rpc_resp = &res,
4758 };
4759 int status;
4760
Jeff Layton12207f62013-11-01 10:49:32 -04004761 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004762
Jeff Layton12207f62013-11-01 10:49:32 -04004763 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004764 if (status)
4765 dprintk("%s failed: %d\n", __func__, status);
4766
4767 return status;
4768}
4769
4770static int nfs4_do_set_security_label(struct inode *inode,
4771 struct nfs4_label *ilabel,
4772 struct nfs_fattr *fattr,
4773 struct nfs4_label *olabel)
4774{
4775 struct nfs4_exception exception = { };
4776 int err;
4777
4778 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004779 err = _nfs4_do_set_security_label(inode, ilabel,
4780 fattr, olabel);
4781 trace_nfs4_set_security_label(inode, err);
4782 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004783 &exception);
4784 } while (exception.retry);
4785 return err;
4786}
4787
4788static int
4789nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4790{
4791 struct nfs4_label ilabel, *olabel = NULL;
4792 struct nfs_fattr fattr;
4793 struct rpc_cred *cred;
4794 struct inode *inode = dentry->d_inode;
4795 int status;
4796
4797 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4798 return -EOPNOTSUPP;
4799
4800 nfs_fattr_init(&fattr);
4801
4802 ilabel.pi = 0;
4803 ilabel.lfs = 0;
4804 ilabel.label = (char *)buf;
4805 ilabel.len = buflen;
4806
4807 cred = rpc_lookup_cred();
4808 if (IS_ERR(cred))
4809 return PTR_ERR(cred);
4810
4811 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4812 if (IS_ERR(olabel)) {
4813 status = -PTR_ERR(olabel);
4814 goto out;
4815 }
4816
4817 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4818 if (status == 0)
4819 nfs_setsecurity(inode, &fattr, olabel);
4820
4821 nfs4_label_free(olabel);
4822out:
4823 put_rpccred(cred);
4824 return status;
4825}
4826#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4827
4828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004830nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4831 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004833 struct nfs_client *clp = server->nfs_client;
4834
4835 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 return 0;
4837 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004838 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004839 case -NFS4ERR_ADMIN_REVOKED:
4840 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004841 if (state == NULL)
4842 break;
4843 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004844 case -NFS4ERR_OPENMODE:
4845 if (state == NULL)
4846 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004847 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004848 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004849 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004850 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004851 if (state != NULL) {
4852 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004853 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004856 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004857 nfs4_schedule_lease_recovery(clp);
4858 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004859 case -NFS4ERR_MOVED:
4860 if (nfs4_schedule_migration_recovery(server) < 0)
4861 goto recovery_failed;
4862 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004863 case -NFS4ERR_LEASE_MOVED:
4864 nfs4_schedule_lease_moved_recovery(clp);
4865 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004866#if defined(CONFIG_NFS_V4_1)
4867 case -NFS4ERR_BADSESSION:
4868 case -NFS4ERR_BADSLOT:
4869 case -NFS4ERR_BAD_HIGH_SLOT:
4870 case -NFS4ERR_DEADSESSION:
4871 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4872 case -NFS4ERR_SEQ_FALSE_RETRY:
4873 case -NFS4ERR_SEQ_MISORDERED:
4874 dprintk("%s ERROR %d, Reset session\n", __func__,
4875 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004876 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004877 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004878#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004880 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10004881 rpc_delay(task, nfs4_update_delay(timeout));
4882 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05004883 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004885 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004887 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 }
4889 task->tk_status = nfs4_map_errors(task->tk_status);
4890 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004891recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004892 task->tk_status = -EIO;
4893 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004894wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004895 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004896 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4897 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004898 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4899 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004900restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004901 task->tk_status = 0;
4902 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903}
4904
Chuck Leverf0920752012-05-21 22:45:41 -04004905static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4906 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004907{
4908 __be32 verf[2];
4909
Chuck Lever2c820d92012-05-21 22:45:33 -04004910 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4911 /* An impossible timestamp guarantees this value
4912 * will never match a generated boot time. */
4913 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004914 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004915 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004916 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004917 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4918 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004919 }
Chuck Levercd937102012-03-02 17:14:31 -05004920 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4921}
4922
Chuck Levere984a552012-09-14 17:24:21 -04004923static unsigned int
4924nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4925 char *buf, size_t len)
4926{
4927 unsigned int result;
4928
4929 rcu_read_lock();
4930 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4931 clp->cl_ipaddr,
4932 rpc_peeraddr2str(clp->cl_rpcclient,
4933 RPC_DISPLAY_ADDR),
4934 rpc_peeraddr2str(clp->cl_rpcclient,
4935 RPC_DISPLAY_PROTO));
4936 rcu_read_unlock();
4937 return result;
4938}
4939
4940static unsigned int
4941nfs4_init_uniform_client_string(const struct nfs_client *clp,
4942 char *buf, size_t len)
4943{
Trond Myklebust55b59292013-07-24 16:36:35 -04004944 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004945
4946 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004947 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4948 clp->rpc_ops->version,
4949 clp->cl_minorversion,
4950 nfs4_client_id_uniquifier,
4951 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004952 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4953 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004954 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004955}
4956
Chuck Lever706cb8d2014-03-12 12:51:47 -04004957/*
4958 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4959 * services. Advertise one based on the address family of the
4960 * clientaddr.
4961 */
4962static unsigned int
4963nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4964{
4965 if (strchr(clp->cl_ipaddr, ':') != NULL)
4966 return scnprintf(buf, len, "tcp6");
4967 else
4968 return scnprintf(buf, len, "tcp");
4969}
4970
Jeff Laytonf11b2a12014-06-21 20:52:17 -04004971static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
4972{
4973 struct nfs4_setclientid *sc = calldata;
4974
4975 if (task->tk_status == 0)
4976 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
4977}
4978
4979static const struct rpc_call_ops nfs4_setclientid_ops = {
4980 .rpc_call_done = nfs4_setclientid_done,
4981};
4982
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004983/**
4984 * nfs4_proc_setclientid - Negotiate client ID
4985 * @clp: state data structure
4986 * @program: RPC program for NFSv4 callback service
4987 * @port: IP port number for NFS4 callback service
4988 * @cred: RPC credential to use for this call
4989 * @res: where to place the result
4990 *
4991 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4992 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004993int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4994 unsigned short port, struct rpc_cred *cred,
4995 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996{
4997 nfs4_verifier sc_verifier;
4998 struct nfs4_setclientid setclientid = {
4999 .sc_verifier = &sc_verifier,
5000 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00005001 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 };
5003 struct rpc_message msg = {
5004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5005 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005006 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005007 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005009 struct rpc_task *task;
5010 struct rpc_task_setup task_setup_data = {
5011 .rpc_client = clp->cl_rpcclient,
5012 .rpc_message = &msg,
5013 .callback_ops = &nfs4_setclientid_ops,
5014 .callback_data = &setclientid,
5015 .flags = RPC_TASK_TIMEOUT,
5016 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005017 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
Chuck Leverde734832012-07-11 16:30:50 -04005019 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005020 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005021 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5022 setclientid.sc_name_len =
5023 nfs4_init_uniform_client_string(clp,
5024 setclientid.sc_name,
5025 sizeof(setclientid.sc_name));
5026 else
5027 setclientid.sc_name_len =
5028 nfs4_init_nonuniform_client_string(clp,
5029 setclientid.sc_name,
5030 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04005031 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005032 setclientid.sc_netid_len =
5033 nfs4_init_callback_netid(clp,
5034 setclientid.sc_netid,
5035 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005036 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005037 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 clp->cl_ipaddr, port >> 8, port & 255);
5039
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005040 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
5041 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5042 setclientid.sc_name_len, setclientid.sc_name);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005043 task = rpc_run_task(&task_setup_data);
5044 if (IS_ERR(task)) {
5045 status = PTR_ERR(task);
5046 goto out;
5047 }
5048 status = task->tk_status;
5049 if (setclientid.sc_cred) {
5050 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5051 put_rpccred(setclientid.sc_cred);
5052 }
5053 rpc_put_task(task);
5054out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005055 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005056 dprintk("NFS reply setclientid: %d\n", status);
5057 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058}
5059
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005060/**
5061 * nfs4_proc_setclientid_confirm - Confirm client ID
5062 * @clp: state data structure
5063 * @res: result of a previous SETCLIENTID
5064 * @cred: RPC credential to use for this call
5065 *
5066 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5067 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005068int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005069 struct nfs4_setclientid_res *arg,
5070 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 struct rpc_message msg = {
5073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005074 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005075 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 int status;
5078
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005079 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5080 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5081 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005082 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005083 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005084 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 return status;
5086}
5087
Trond Myklebustfe650402006-01-03 09:55:18 +01005088struct nfs4_delegreturndata {
5089 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005090 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005091 struct nfs_fh fh;
5092 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005093 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005094 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005095 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005096 struct inode *inode;
5097 bool roc;
5098 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005099};
5100
Trond Myklebustfe650402006-01-03 09:55:18 +01005101static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5102{
5103 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005104
Trond Myklebust14516c32010-07-31 14:29:06 -04005105 if (!nfs4_sequence_done(task, &data->res.seq_res))
5106 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005107
Trond Myklebustca8acf82013-08-13 10:36:56 -04005108 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005109 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005110 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005111 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005112 case -NFS4ERR_ADMIN_REVOKED:
5113 case -NFS4ERR_DELEG_REVOKED:
5114 case -NFS4ERR_BAD_STATEID:
5115 case -NFS4ERR_OLD_STATEID:
5116 case -NFS4ERR_STALE_STATEID:
5117 case -NFS4ERR_EXPIRED:
5118 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005119 if (data->roc)
5120 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005121 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005122 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005123 if (nfs4_async_handle_error(task, data->res.server,
5124 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005125 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005126 return;
5127 }
5128 }
5129 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005130}
5131
5132static void nfs4_delegreturn_release(void *calldata)
5133{
Peng Tao039b7562014-07-03 13:05:02 +08005134 struct nfs4_delegreturndata *data = calldata;
5135
5136 if (data->roc)
5137 pnfs_roc_release(data->inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005138 kfree(calldata);
5139}
5140
Andy Adamson938e1012009-04-01 09:22:28 -04005141static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5142{
5143 struct nfs4_delegreturndata *d_data;
5144
5145 d_data = (struct nfs4_delegreturndata *)data;
5146
Peng Tao039b7562014-07-03 13:05:02 +08005147 if (d_data->roc &&
5148 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5149 return;
5150
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005151 nfs4_setup_sequence(d_data->res.server,
5152 &d_data->args.seq_args,
5153 &d_data->res.seq_res,
5154 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005155}
Andy Adamson938e1012009-04-01 09:22:28 -04005156
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005157static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005158 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005159 .rpc_call_done = nfs4_delegreturn_done,
5160 .rpc_release = nfs4_delegreturn_release,
5161};
5162
Trond Myklebuste6f81072008-01-24 18:14:34 -05005163static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005164{
5165 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005166 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005167 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005168 struct rpc_message msg = {
5169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5170 .rpc_cred = cred,
5171 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005172 struct rpc_task_setup task_setup_data = {
5173 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005174 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005175 .callback_ops = &nfs4_delegreturn_ops,
5176 .flags = RPC_TASK_ASYNC,
5177 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005178 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005179
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005180 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005181 if (data == NULL)
5182 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005183 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005184 data->args.fhandle = &data->fh;
5185 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005186 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005187 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005188 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005189 data->res.fattr = &data->fattr;
5190 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005191 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005192 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005193 data->rpc_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005194 data->inode = inode;
5195 data->roc = list_empty(&NFS_I(inode)->open_files) ?
5196 pnfs_roc(inode) : false;
Trond Myklebustfe650402006-01-03 09:55:18 +01005197
Trond Myklebustc970aa82007-07-14 15:39:59 -04005198 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005199 msg.rpc_argp = &data->args;
5200 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005201 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005202 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005203 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005204 if (!issync)
5205 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005206 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005207 if (status != 0)
5208 goto out;
5209 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005210 if (status == 0)
5211 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5212 else
5213 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005214out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005215 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005216 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217}
5218
Trond Myklebuste6f81072008-01-24 18:14:34 -05005219int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220{
5221 struct nfs_server *server = NFS_SERVER(inode);
5222 struct nfs4_exception exception = { };
5223 int err;
5224 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005225 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005226 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 switch (err) {
5228 case -NFS4ERR_STALE_STATEID:
5229 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 case 0:
5231 return 0;
5232 }
5233 err = nfs4_handle_exception(server, err, &exception);
5234 } while (exception.retry);
5235 return err;
5236}
5237
5238#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5239#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5240
5241/*
5242 * sleep, with exponential backoff, and retry the LOCK operation.
5243 */
5244static unsigned long
5245nfs4_set_lock_task_retry(unsigned long timeout)
5246{
Colin Cross416ad3c2013-05-06 23:50:06 +00005247 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 timeout <<= 1;
5249 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5250 return NFS4_LOCK_MAXTIMEOUT;
5251 return timeout;
5252}
5253
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5255{
5256 struct inode *inode = state->inode;
5257 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005258 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005259 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005261 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005263 struct nfs_lockt_res res = {
5264 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 };
5266 struct rpc_message msg = {
5267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5268 .rpc_argp = &arg,
5269 .rpc_resp = &res,
5270 .rpc_cred = state->owner->so_cred,
5271 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 struct nfs4_lock_state *lsp;
5273 int status;
5274
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005275 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005276 status = nfs4_set_lock_state(state, request);
5277 if (status != 0)
5278 goto out;
5279 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005280 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005281 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005282 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005283 switch (status) {
5284 case 0:
5285 request->fl_type = F_UNLCK;
5286 break;
5287 case -NFS4ERR_DENIED:
5288 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005290 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005291 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005292out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 return status;
5294}
5295
5296static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5297{
5298 struct nfs4_exception exception = { };
5299 int err;
5300
5301 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005302 err = _nfs4_proc_getlk(state, cmd, request);
5303 trace_nfs4_get_lock(request, state, cmd, err);
5304 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 &exception);
5306 } while (exception.retry);
5307 return err;
5308}
5309
5310static int do_vfs_lock(struct file *file, struct file_lock *fl)
5311{
5312 int res = 0;
5313 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5314 case FL_POSIX:
5315 res = posix_lock_file_wait(file, fl);
5316 break;
5317 case FL_FLOCK:
5318 res = flock_lock_file_wait(file, fl);
5319 break;
5320 default:
5321 BUG();
5322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 return res;
5324}
5325
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005326struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005327 struct nfs_locku_args arg;
5328 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005329 struct nfs4_lock_state *lsp;
5330 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005331 struct file_lock fl;
5332 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005333 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005334};
5335
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005336static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5337 struct nfs_open_context *ctx,
5338 struct nfs4_lock_state *lsp,
5339 struct nfs_seqid *seqid)
5340{
5341 struct nfs4_unlockdata *p;
5342 struct inode *inode = lsp->ls_state->inode;
5343
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005344 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005345 if (p == NULL)
5346 return NULL;
5347 p->arg.fh = NFS_FH(inode);
5348 p->arg.fl = &p->fl;
5349 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005350 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005351 p->arg.stateid = &lsp->ls_stateid;
5352 p->lsp = lsp;
5353 atomic_inc(&lsp->ls_count);
5354 /* Ensure we don't close file until we're done freeing locks! */
5355 p->ctx = get_nfs_open_context(ctx);
5356 memcpy(&p->fl, fl, sizeof(p->fl));
5357 p->server = NFS_SERVER(inode);
5358 return p;
5359}
5360
Trond Myklebust06f814a2006-01-03 09:55:07 +01005361static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005362{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005363 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005364 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005365 nfs4_put_lock_state(calldata->lsp);
5366 put_nfs_open_context(calldata->ctx);
5367 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005368}
5369
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005370static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005371{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005372 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005373
Trond Myklebust14516c32010-07-31 14:29:06 -04005374 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5375 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005376 switch (task->tk_status) {
5377 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005378 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5379 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005380 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005381 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005382 case -NFS4ERR_BAD_STATEID:
5383 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005384 case -NFS4ERR_STALE_STATEID:
5385 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005386 break;
5387 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005388 if (nfs4_async_handle_error(task, calldata->server,
5389 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005390 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005391 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005392 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005393}
5394
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005395static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005396{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005397 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005399 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005400 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005401 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005402 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005403 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005404 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005405 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005406 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005407 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005408 &calldata->res.seq_res,
5409 task) != 0)
5410 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005411 return;
5412out_no_action:
5413 task->tk_action = NULL;
5414out_wait:
5415 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416}
5417
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005418static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005419 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005420 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005421 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005422};
5423
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005424static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5425 struct nfs_open_context *ctx,
5426 struct nfs4_lock_state *lsp,
5427 struct nfs_seqid *seqid)
5428{
5429 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005430 struct rpc_message msg = {
5431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5432 .rpc_cred = ctx->cred,
5433 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005434 struct rpc_task_setup task_setup_data = {
5435 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005436 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005437 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005438 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005439 .flags = RPC_TASK_ASYNC,
5440 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005441
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005442 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5443 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5444
Frank Filz137d6ac2007-07-09 15:32:29 -07005445 /* Ensure this is an unlock - when canceling a lock, the
5446 * canceled lock is passed in, and it won't be an unlock.
5447 */
5448 fl->fl_type = F_UNLCK;
5449
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005450 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5451 if (data == NULL) {
5452 nfs_free_seqid(seqid);
5453 return ERR_PTR(-ENOMEM);
5454 }
5455
Chuck Levera9c92d62013-08-09 12:48:18 -04005456 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005457 msg.rpc_argp = &data->arg;
5458 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005459 task_setup_data.callback_data = data;
5460 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005461}
5462
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5464{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005465 struct inode *inode = state->inode;
5466 struct nfs4_state_owner *sp = state->owner;
5467 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005468 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005469 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005470 struct rpc_task *task;
5471 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005472 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
Trond Myklebust9b073572006-06-29 16:38:34 -04005474 status = nfs4_set_lock_state(state, request);
5475 /* Unlock _before_ we do the RPC call */
5476 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005477 /* Exclude nfs_delegation_claim_locks() */
5478 mutex_lock(&sp->so_delegreturn_mutex);
5479 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005480 down_read(&nfsi->rwsem);
5481 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5482 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005483 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005484 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005485 }
5486 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005487 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005488 if (status != 0)
5489 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005490 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005491 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005492 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5493 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005494 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005495 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005496 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005497 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005498 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005499 status = PTR_ERR(task);
5500 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005501 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005502 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005503 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005504out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005505 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005506 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005507 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508}
5509
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005510struct nfs4_lockdata {
5511 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005512 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005513 struct nfs4_lock_state *lsp;
5514 struct nfs_open_context *ctx;
5515 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005516 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005517 int rpc_status;
5518 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005519 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005520};
5521
5522static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005523 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5524 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005525{
5526 struct nfs4_lockdata *p;
5527 struct inode *inode = lsp->ls_state->inode;
5528 struct nfs_server *server = NFS_SERVER(inode);
5529
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005530 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005531 if (p == NULL)
5532 return NULL;
5533
5534 p->arg.fh = NFS_FH(inode);
5535 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005536 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005537 if (p->arg.open_seqid == NULL)
5538 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005539 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005540 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005541 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005542 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005543 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005544 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005545 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005546 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005547 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005548 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005549 atomic_inc(&lsp->ls_count);
5550 p->ctx = get_nfs_open_context(ctx);
5551 memcpy(&p->fl, fl, sizeof(p->fl));
5552 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005553out_free_seqid:
5554 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005555out_free:
5556 kfree(p);
5557 return NULL;
5558}
5559
5560static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5561{
5562 struct nfs4_lockdata *data = calldata;
5563 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564
Harvey Harrison3110ff82008-05-02 13:42:44 -07005565 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005566 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005567 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005568 /* Do we need to do an open_to_lock_owner? */
5569 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005570 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005571 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005572 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005573 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005574 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005575 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005576 } else
5577 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005578 if (!nfs4_valid_open_stateid(state)) {
5579 data->rpc_status = -EBADF;
5580 task->tk_action = NULL;
5581 goto out_release_open_seqid;
5582 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005583 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005584 if (nfs4_setup_sequence(data->server,
5585 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005586 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005587 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005588 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005589out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005590 nfs_release_seqid(data->arg.open_seqid);
5591out_release_lock_seqid:
5592 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005593out_wait:
5594 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005595 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005596}
5597
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005598static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5599{
5600 struct nfs4_lockdata *data = calldata;
5601
Harvey Harrison3110ff82008-05-02 13:42:44 -07005602 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005603
Trond Myklebust14516c32010-07-31 14:29:06 -04005604 if (!nfs4_sequence_done(task, &data->res.seq_res))
5605 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005606
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005607 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005608 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005609 if (data->rpc_status == 0)
5610 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5611 else
5612 goto out;
5613 }
5614 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005615 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005616 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005617 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005618 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005619out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005620 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005621}
5622
5623static void nfs4_lock_release(void *calldata)
5624{
5625 struct nfs4_lockdata *data = calldata;
5626
Harvey Harrison3110ff82008-05-02 13:42:44 -07005627 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005628 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005629 if (data->cancelled != 0) {
5630 struct rpc_task *task;
5631 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5632 data->arg.lock_seqid);
5633 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005634 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005635 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005636 } else
5637 nfs_free_seqid(data->arg.lock_seqid);
5638 nfs4_put_lock_state(data->lsp);
5639 put_nfs_open_context(data->ctx);
5640 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005641 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005642}
5643
5644static const struct rpc_call_ops nfs4_lock_ops = {
5645 .rpc_call_prepare = nfs4_lock_prepare,
5646 .rpc_call_done = nfs4_lock_done,
5647 .rpc_release = nfs4_lock_release,
5648};
5649
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005650static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5651{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005652 switch (error) {
5653 case -NFS4ERR_ADMIN_REVOKED:
5654 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005655 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005656 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005657 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005658 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005659 break;
5660 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005661 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005662 case -NFS4ERR_EXPIRED:
5663 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005664 };
5665}
5666
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005667static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005668{
5669 struct nfs4_lockdata *data;
5670 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005671 struct rpc_message msg = {
5672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5673 .rpc_cred = state->owner->so_cred,
5674 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005675 struct rpc_task_setup task_setup_data = {
5676 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005677 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005678 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005679 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005680 .flags = RPC_TASK_ASYNC,
5681 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005682 int ret;
5683
Harvey Harrison3110ff82008-05-02 13:42:44 -07005684 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005685 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005686 fl->fl_u.nfs4_fl.owner,
5687 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005688 if (data == NULL)
5689 return -ENOMEM;
5690 if (IS_SETLKW(cmd))
5691 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005692 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005693 msg.rpc_argp = &data->arg;
5694 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005695 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005696 if (recovery_type > NFS_LOCK_NEW) {
5697 if (recovery_type == NFS_LOCK_RECLAIM)
5698 data->arg.reclaim = NFS_LOCK_RECLAIM;
5699 nfs4_set_sequence_privileged(&data->arg.seq_args);
5700 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005701 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005702 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005703 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005704 ret = nfs4_wait_for_completion_rpc_task(task);
5705 if (ret == 0) {
5706 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005707 if (ret)
5708 nfs4_handle_setlk_error(data->server, data->lsp,
5709 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005710 } else
5711 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005712 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005713 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005714 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715}
5716
5717static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5718{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005719 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005720 struct nfs4_exception exception = {
5721 .inode = state->inode,
5722 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005723 int err;
5724
5725 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005726 /* Cache the lock if possible... */
5727 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5728 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005729 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005730 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005731 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005732 break;
5733 nfs4_handle_exception(server, err, &exception);
5734 } while (exception.retry);
5735 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736}
5737
5738static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5739{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005740 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005741 struct nfs4_exception exception = {
5742 .inode = state->inode,
5743 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005744 int err;
5745
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005746 err = nfs4_set_lock_state(state, request);
5747 if (err != 0)
5748 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005749 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005750 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5751 return 0;
5752 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005753 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005754 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5755 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005756 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005757 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005758 switch (err) {
5759 default:
5760 goto out;
5761 case -NFS4ERR_GRACE:
5762 case -NFS4ERR_DELAY:
5763 nfs4_handle_exception(server, err, &exception);
5764 err = 0;
5765 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005766 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005767out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005768 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769}
5770
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005771#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005772/**
5773 * nfs41_check_expired_locks - possibly free a lock stateid
5774 *
5775 * @state: NFSv4 state for an inode
5776 *
5777 * Returns NFS_OK if recovery for this stateid is now finished.
5778 * Otherwise a negative NFS4ERR value is returned.
5779 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005780static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005781{
Chuck Levereb64cf92012-07-11 16:30:05 -04005782 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005783 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005784 struct nfs_server *server = NFS_SERVER(state->inode);
5785
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005786 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005787 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005788 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5789
5790 status = nfs41_test_stateid(server,
5791 &lsp->ls_stateid,
5792 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005793 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005794 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005795 /* Free the stateid unless the server
5796 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005797 if (status != -NFS4ERR_BAD_STATEID)
5798 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005799 &lsp->ls_stateid,
5800 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005801 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005802 ret = status;
5803 }
5804 }
5805 };
5806
5807 return ret;
5808}
5809
5810static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5811{
5812 int status = NFS_OK;
5813
5814 if (test_bit(LK_STATE_IN_USE, &state->flags))
5815 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005816 if (status != NFS_OK)
5817 status = nfs4_lock_expired(state, request);
5818 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005819}
5820#endif
5821
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5823{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005824 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005825 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005826 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005827 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005828 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005830 if ((fl_flags & FL_POSIX) &&
5831 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5832 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005833 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005834 status = nfs4_set_lock_state(state, request);
5835 if (status != 0)
5836 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005837 request->fl_flags |= FL_ACCESS;
5838 status = do_vfs_lock(request->fl_file, request);
5839 if (status < 0)
5840 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005841 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005842 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005843 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005844 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005845 request->fl_flags = fl_flags & ~FL_SLEEP;
5846 status = do_vfs_lock(request->fl_file, request);
5847 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005848 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005849 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5850 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005851 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005852 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005853 goto out;
5854 down_read(&nfsi->rwsem);
5855 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5856 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005857 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005858 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005859 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005860 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005861 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005862 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5863 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005864out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005865 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005866out:
5867 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 return status;
5869}
5870
5871static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5872{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005873 struct nfs4_exception exception = {
5874 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005875 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005876 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 int err;
5878
5879 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005880 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005881 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005882 if (err == -NFS4ERR_DENIED)
5883 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005885 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 } while (exception.retry);
5887 return err;
5888}
5889
5890static int
5891nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5892{
5893 struct nfs_open_context *ctx;
5894 struct nfs4_state *state;
5895 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5896 int status;
5897
5898 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005899 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 state = ctx->state;
5901
5902 if (request->fl_start < 0 || request->fl_end < 0)
5903 return -EINVAL;
5904
Trond Myklebustd9531262009-07-21 19:22:38 -04005905 if (IS_GETLK(cmd)) {
5906 if (state != NULL)
5907 return nfs4_proc_getlk(state, F_GETLK, request);
5908 return 0;
5909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910
5911 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5912 return -EINVAL;
5913
Trond Myklebustd9531262009-07-21 19:22:38 -04005914 if (request->fl_type == F_UNLCK) {
5915 if (state != NULL)
5916 return nfs4_proc_unlck(state, cmd, request);
5917 return 0;
5918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
Trond Myklebustd9531262009-07-21 19:22:38 -04005920 if (state == NULL)
5921 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005922 /*
5923 * Don't rely on the VFS having checked the file open mode,
5924 * since it won't do this for flock() locks.
5925 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005926 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005927 case F_RDLCK:
5928 if (!(filp->f_mode & FMODE_READ))
5929 return -EBADF;
5930 break;
5931 case F_WRLCK:
5932 if (!(filp->f_mode & FMODE_WRITE))
5933 return -EBADF;
5934 }
5935
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 do {
5937 status = nfs4_proc_setlk(state, cmd, request);
5938 if ((status != -EAGAIN) || IS_SETLK(cmd))
5939 break;
5940 timeout = nfs4_set_lock_task_retry(timeout);
5941 status = -ERESTARTSYS;
5942 if (signalled())
5943 break;
5944 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 return status;
5946}
5947
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005948int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005949{
5950 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005951 int err;
5952
5953 err = nfs4_set_lock_state(state, fl);
5954 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005955 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005956 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005957 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005958}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005959
Trond Myklebustcf470c32012-03-07 13:49:12 -05005960struct nfs_release_lockowner_data {
5961 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005962 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005963 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005964 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04005965 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005966};
5967
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005968static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5969{
5970 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08005971 struct nfs_server *server = data->server;
5972 nfs40_setup_sequence(server, &data->args.seq_args,
5973 &data->res.seq_res, task);
5974 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04005975 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005976}
5977
5978static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5979{
5980 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04005981 struct nfs_server *server = data->server;
5982
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005983 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04005984
5985 switch (task->tk_status) {
5986 case 0:
5987 renew_lease(server, data->timestamp);
5988 break;
5989 case -NFS4ERR_STALE_CLIENTID:
5990 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08005991 nfs4_schedule_lease_recovery(server->nfs_client);
5992 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04005993 case -NFS4ERR_LEASE_MOVED:
5994 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10005995 if (nfs4_async_handle_error(task, server,
5996 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04005997 rpc_restart_call_prepare(task);
5998 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005999}
6000
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006001static void nfs4_release_lockowner_release(void *calldata)
6002{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006003 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006004 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006005 kfree(calldata);
6006}
6007
Trond Myklebust17280172012-03-11 13:11:00 -04006008static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006009 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6010 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006011 .rpc_release = nfs4_release_lockowner_release,
6012};
6013
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006014static void
6015nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006016{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006017 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006018 struct rpc_message msg = {
6019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6020 };
6021
6022 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006023 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006024
Trond Myklebustcf470c32012-03-07 13:49:12 -05006025 data = kmalloc(sizeof(*data), GFP_NOFS);
6026 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006027 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006028 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006029 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006030 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6031 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6032 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006033
Trond Myklebustcf470c32012-03-07 13:49:12 -05006034 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006035 msg.rpc_resp = &data->res;
6036 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006037 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006038}
6039
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006040#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6041
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006042static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6043 const void *buf, size_t buflen,
6044 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006045{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006046 if (strcmp(key, "") != 0)
6047 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006048
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006049 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006050}
6051
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006052static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6053 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006054{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006055 if (strcmp(key, "") != 0)
6056 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006057
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006058 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006059}
6060
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006061static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6062 size_t list_len, const char *name,
6063 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006064{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006065 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006066
J. Bruce Fields096455a2006-03-20 23:23:42 -05006067 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
6068 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006069
6070 if (list && len <= list_len)
6071 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006072 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006073}
6074
David Quigleyc9bccef2013-05-22 12:50:45 -04006075#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6076static inline int nfs4_server_supports_labels(struct nfs_server *server)
6077{
6078 return server->caps & NFS_CAP_SECURITY_LABEL;
6079}
6080
6081static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6082 const void *buf, size_t buflen,
6083 int flags, int type)
6084{
6085 if (security_ismaclabel(key))
6086 return nfs4_set_security_label(dentry, buf, buflen);
6087
6088 return -EOPNOTSUPP;
6089}
6090
6091static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6092 void *buf, size_t buflen, int type)
6093{
6094 if (security_ismaclabel(key))
6095 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
6096 return -EOPNOTSUPP;
6097}
6098
6099static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6100 size_t list_len, const char *name,
6101 size_t name_len, int type)
6102{
6103 size_t len = 0;
6104
6105 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
6106 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
6107 if (list && len <= list_len)
6108 security_inode_listsecurity(dentry->d_inode, list, len);
6109 }
6110 return len;
6111}
6112
6113static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6114 .prefix = XATTR_SECURITY_PREFIX,
6115 .list = nfs4_xattr_list_nfs4_label,
6116 .get = nfs4_xattr_get_nfs4_label,
6117 .set = nfs4_xattr_set_nfs4_label,
6118};
6119#endif
6120
6121
Andy Adamson533eb462011-06-13 18:25:56 -04006122/*
6123 * nfs_fhget will use either the mounted_on_fileid or the fileid
6124 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006125static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6126{
Andy Adamson533eb462011-06-13 18:25:56 -04006127 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6128 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6129 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006130 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006131 return;
6132
6133 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006134 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006135 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6136 fattr->nlink = 2;
6137}
6138
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006139static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6140 const struct qstr *name,
6141 struct nfs4_fs_locations *fs_locations,
6142 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006143{
6144 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006145 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006146 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006147 };
6148 struct nfs4_fs_locations_arg args = {
6149 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006150 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006151 .page = page,
6152 .bitmask = bitmask,
6153 };
Benny Halevy22958462009-04-01 09:22:02 -04006154 struct nfs4_fs_locations_res res = {
6155 .fs_locations = fs_locations,
6156 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006157 struct rpc_message msg = {
6158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6159 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006160 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006161 };
6162 int status;
6163
Harvey Harrison3110ff82008-05-02 13:42:44 -07006164 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006165
6166 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6167 * is not supported */
6168 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6169 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6170 else
6171 bitmask[0] |= FATTR4_WORD0_FILEID;
6172
Trond Myklebustc228fd32007-01-13 02:28:11 -05006173 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006174 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006175 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006176 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006177 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006178 return status;
6179}
6180
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006181int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6182 const struct qstr *name,
6183 struct nfs4_fs_locations *fs_locations,
6184 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006185{
6186 struct nfs4_exception exception = { };
6187 int err;
6188 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006189 err = _nfs4_proc_fs_locations(client, dir, name,
6190 fs_locations, page);
6191 trace_nfs4_get_fs_locations(dir, name, err);
6192 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006193 &exception);
6194 } while (exception.retry);
6195 return err;
6196}
6197
Chuck Leverb03d7352013-10-17 14:12:50 -04006198/*
6199 * This operation also signals the server that this client is
6200 * performing migration recovery. The server can stop returning
6201 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6202 * appended to this compound to identify the client ID which is
6203 * performing recovery.
6204 */
6205static int _nfs40_proc_get_locations(struct inode *inode,
6206 struct nfs4_fs_locations *locations,
6207 struct page *page, struct rpc_cred *cred)
6208{
6209 struct nfs_server *server = NFS_SERVER(inode);
6210 struct rpc_clnt *clnt = server->client;
6211 u32 bitmask[2] = {
6212 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6213 };
6214 struct nfs4_fs_locations_arg args = {
6215 .clientid = server->nfs_client->cl_clientid,
6216 .fh = NFS_FH(inode),
6217 .page = page,
6218 .bitmask = bitmask,
6219 .migration = 1, /* skip LOOKUP */
6220 .renew = 1, /* append RENEW */
6221 };
6222 struct nfs4_fs_locations_res res = {
6223 .fs_locations = locations,
6224 .migration = 1,
6225 .renew = 1,
6226 };
6227 struct rpc_message msg = {
6228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6229 .rpc_argp = &args,
6230 .rpc_resp = &res,
6231 .rpc_cred = cred,
6232 };
6233 unsigned long now = jiffies;
6234 int status;
6235
6236 nfs_fattr_init(&locations->fattr);
6237 locations->server = server;
6238 locations->nlocations = 0;
6239
6240 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6241 nfs4_set_sequence_privileged(&args.seq_args);
6242 status = nfs4_call_sync_sequence(clnt, server, &msg,
6243 &args.seq_args, &res.seq_res);
6244 if (status)
6245 return status;
6246
6247 renew_lease(server, now);
6248 return 0;
6249}
6250
6251#ifdef CONFIG_NFS_V4_1
6252
6253/*
6254 * This operation also signals the server that this client is
6255 * performing migration recovery. The server can stop asserting
6256 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6257 * performing this operation is identified in the SEQUENCE
6258 * operation in this compound.
6259 *
6260 * When the client supports GETATTR(fs_locations_info), it can
6261 * be plumbed in here.
6262 */
6263static int _nfs41_proc_get_locations(struct inode *inode,
6264 struct nfs4_fs_locations *locations,
6265 struct page *page, struct rpc_cred *cred)
6266{
6267 struct nfs_server *server = NFS_SERVER(inode);
6268 struct rpc_clnt *clnt = server->client;
6269 u32 bitmask[2] = {
6270 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6271 };
6272 struct nfs4_fs_locations_arg args = {
6273 .fh = NFS_FH(inode),
6274 .page = page,
6275 .bitmask = bitmask,
6276 .migration = 1, /* skip LOOKUP */
6277 };
6278 struct nfs4_fs_locations_res res = {
6279 .fs_locations = locations,
6280 .migration = 1,
6281 };
6282 struct rpc_message msg = {
6283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6284 .rpc_argp = &args,
6285 .rpc_resp = &res,
6286 .rpc_cred = cred,
6287 };
6288 int status;
6289
6290 nfs_fattr_init(&locations->fattr);
6291 locations->server = server;
6292 locations->nlocations = 0;
6293
6294 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6295 nfs4_set_sequence_privileged(&args.seq_args);
6296 status = nfs4_call_sync_sequence(clnt, server, &msg,
6297 &args.seq_args, &res.seq_res);
6298 if (status == NFS4_OK &&
6299 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6300 status = -NFS4ERR_LEASE_MOVED;
6301 return status;
6302}
6303
6304#endif /* CONFIG_NFS_V4_1 */
6305
6306/**
6307 * nfs4_proc_get_locations - discover locations for a migrated FSID
6308 * @inode: inode on FSID that is migrating
6309 * @locations: result of query
6310 * @page: buffer
6311 * @cred: credential to use for this operation
6312 *
6313 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6314 * operation failed, or a negative errno if a local error occurred.
6315 *
6316 * On success, "locations" is filled in, but if the server has
6317 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6318 * asserted.
6319 *
6320 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6321 * from this client that require migration recovery.
6322 */
6323int nfs4_proc_get_locations(struct inode *inode,
6324 struct nfs4_fs_locations *locations,
6325 struct page *page, struct rpc_cred *cred)
6326{
6327 struct nfs_server *server = NFS_SERVER(inode);
6328 struct nfs_client *clp = server->nfs_client;
6329 const struct nfs4_mig_recovery_ops *ops =
6330 clp->cl_mvops->mig_recovery_ops;
6331 struct nfs4_exception exception = { };
6332 int status;
6333
6334 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6335 (unsigned long long)server->fsid.major,
6336 (unsigned long long)server->fsid.minor,
6337 clp->cl_hostname);
6338 nfs_display_fhandle(NFS_FH(inode), __func__);
6339
6340 do {
6341 status = ops->get_locations(inode, locations, page, cred);
6342 if (status != -NFS4ERR_DELAY)
6343 break;
6344 nfs4_handle_exception(server, status, &exception);
6345 } while (exception.retry);
6346 return status;
6347}
6348
Chuck Lever44c99932013-10-17 14:13:30 -04006349/*
6350 * This operation also signals the server that this client is
6351 * performing "lease moved" recovery. The server can stop
6352 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6353 * is appended to this compound to identify the client ID which is
6354 * performing recovery.
6355 */
6356static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6357{
6358 struct nfs_server *server = NFS_SERVER(inode);
6359 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6360 struct rpc_clnt *clnt = server->client;
6361 struct nfs4_fsid_present_arg args = {
6362 .fh = NFS_FH(inode),
6363 .clientid = clp->cl_clientid,
6364 .renew = 1, /* append RENEW */
6365 };
6366 struct nfs4_fsid_present_res res = {
6367 .renew = 1,
6368 };
6369 struct rpc_message msg = {
6370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6371 .rpc_argp = &args,
6372 .rpc_resp = &res,
6373 .rpc_cred = cred,
6374 };
6375 unsigned long now = jiffies;
6376 int status;
6377
6378 res.fh = nfs_alloc_fhandle();
6379 if (res.fh == NULL)
6380 return -ENOMEM;
6381
6382 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6383 nfs4_set_sequence_privileged(&args.seq_args);
6384 status = nfs4_call_sync_sequence(clnt, server, &msg,
6385 &args.seq_args, &res.seq_res);
6386 nfs_free_fhandle(res.fh);
6387 if (status)
6388 return status;
6389
6390 do_renew_lease(clp, now);
6391 return 0;
6392}
6393
6394#ifdef CONFIG_NFS_V4_1
6395
6396/*
6397 * This operation also signals the server that this client is
6398 * performing "lease moved" recovery. The server can stop asserting
6399 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6400 * this operation is identified in the SEQUENCE operation in this
6401 * compound.
6402 */
6403static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6404{
6405 struct nfs_server *server = NFS_SERVER(inode);
6406 struct rpc_clnt *clnt = server->client;
6407 struct nfs4_fsid_present_arg args = {
6408 .fh = NFS_FH(inode),
6409 };
6410 struct nfs4_fsid_present_res res = {
6411 };
6412 struct rpc_message msg = {
6413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6414 .rpc_argp = &args,
6415 .rpc_resp = &res,
6416 .rpc_cred = cred,
6417 };
6418 int status;
6419
6420 res.fh = nfs_alloc_fhandle();
6421 if (res.fh == NULL)
6422 return -ENOMEM;
6423
6424 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6425 nfs4_set_sequence_privileged(&args.seq_args);
6426 status = nfs4_call_sync_sequence(clnt, server, &msg,
6427 &args.seq_args, &res.seq_res);
6428 nfs_free_fhandle(res.fh);
6429 if (status == NFS4_OK &&
6430 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6431 status = -NFS4ERR_LEASE_MOVED;
6432 return status;
6433}
6434
6435#endif /* CONFIG_NFS_V4_1 */
6436
6437/**
6438 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6439 * @inode: inode on FSID to check
6440 * @cred: credential to use for this operation
6441 *
6442 * Server indicates whether the FSID is present, moved, or not
6443 * recognized. This operation is necessary to clear a LEASE_MOVED
6444 * condition for this client ID.
6445 *
6446 * Returns NFS4_OK if the FSID is present on this server,
6447 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6448 * NFS4ERR code if some error occurred on the server, or a
6449 * negative errno if a local failure occurred.
6450 */
6451int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6452{
6453 struct nfs_server *server = NFS_SERVER(inode);
6454 struct nfs_client *clp = server->nfs_client;
6455 const struct nfs4_mig_recovery_ops *ops =
6456 clp->cl_mvops->mig_recovery_ops;
6457 struct nfs4_exception exception = { };
6458 int status;
6459
6460 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6461 (unsigned long long)server->fsid.major,
6462 (unsigned long long)server->fsid.minor,
6463 clp->cl_hostname);
6464 nfs_display_fhandle(NFS_FH(inode), __func__);
6465
6466 do {
6467 status = ops->fsid_present(inode, cred);
6468 if (status != -NFS4ERR_DELAY)
6469 break;
6470 nfs4_handle_exception(server, status, &exception);
6471 } while (exception.retry);
6472 return status;
6473}
6474
Andy Adamson5ec16a82013-08-08 10:57:55 -04006475/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006476 * If 'use_integrity' is true and the state managment nfs_client
6477 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6478 * and the machine credential as per RFC3530bis and RFC5661 Security
6479 * Considerations sections. Otherwise, just use the user cred with the
6480 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006481 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006482static 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 +00006483{
6484 int status;
6485 struct nfs4_secinfo_arg args = {
6486 .dir_fh = NFS_FH(dir),
6487 .name = name,
6488 };
6489 struct nfs4_secinfo_res res = {
6490 .flavors = flavors,
6491 };
6492 struct rpc_message msg = {
6493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6494 .rpc_argp = &args,
6495 .rpc_resp = &res,
6496 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006497 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006498 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006499
6500 if (use_integrity) {
6501 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006502 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6503 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006504 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006505
6506 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006507
6508 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6509 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6510
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006511 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6512 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006513 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006514
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006515 if (cred)
6516 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006517
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006518 return status;
6519}
6520
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006521int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6522 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006523{
6524 struct nfs4_exception exception = { };
6525 int err;
6526 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006527 err = -NFS4ERR_WRONGSEC;
6528
6529 /* try to use integrity protection with machine cred */
6530 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6531 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6532
6533 /*
6534 * if unable to use integrity protection, or SECINFO with
6535 * integrity protection returns NFS4ERR_WRONGSEC (which is
6536 * disallowed by spec, but exists in deployed servers) use
6537 * the current filesystem's rpc_client and the user cred.
6538 */
6539 if (err == -NFS4ERR_WRONGSEC)
6540 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6541
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006542 trace_nfs4_secinfo(dir, name, err);
6543 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006544 &exception);
6545 } while (exception.retry);
6546 return err;
6547}
6548
Andy Adamson557134a2009-04-01 09:21:53 -04006549#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006550/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006551 * Check the exchange flags returned by the server for invalid flags, having
6552 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6553 * DS flags set.
6554 */
6555static int nfs4_check_cl_exchange_flags(u32 flags)
6556{
6557 if (flags & ~EXCHGID4_FLAG_MASK_R)
6558 goto out_inval;
6559 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6560 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6561 goto out_inval;
6562 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6563 goto out_inval;
6564 return NFS_OK;
6565out_inval:
6566 return -NFS4ERR_INVAL;
6567}
6568
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006569static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006570nfs41_same_server_scope(struct nfs41_server_scope *a,
6571 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006572{
6573 if (a->server_scope_sz == b->server_scope_sz &&
6574 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6575 return true;
6576
6577 return false;
6578}
6579
Andy Adamson357f54d2010-12-14 10:11:57 -05006580/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006581 * nfs4_proc_bind_conn_to_session()
6582 *
6583 * The 4.1 client currently uses the same TCP connection for the
6584 * fore and backchannel.
6585 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006586int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006587{
6588 int status;
6589 struct nfs41_bind_conn_to_session_res res;
6590 struct rpc_message msg = {
6591 .rpc_proc =
6592 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6593 .rpc_argp = clp,
6594 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006595 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006596 };
6597
6598 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006599
6600 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6601 if (unlikely(res.session == NULL)) {
6602 status = -ENOMEM;
6603 goto out;
6604 }
6605
6606 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006607 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006608 if (status == 0) {
6609 if (memcmp(res.session->sess_id.data,
6610 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6611 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6612 status = -EIO;
6613 goto out_session;
6614 }
6615 if (res.dir != NFS4_CDFS4_BOTH) {
6616 dprintk("NFS: %s: Unexpected direction from server\n",
6617 __func__);
6618 status = -EIO;
6619 goto out_session;
6620 }
6621 if (res.use_conn_in_rdma_mode) {
6622 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6623 __func__);
6624 status = -EIO;
6625 goto out_session;
6626 }
6627 }
6628out_session:
6629 kfree(res.session);
6630out:
6631 dprintk("<-- %s status= %d\n", __func__, status);
6632 return status;
6633}
6634
6635/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006636 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6637 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006638 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006639static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6640 .how = SP4_MACH_CRED,
6641 .enforce.u.words = {
6642 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6643 1 << (OP_EXCHANGE_ID - 32) |
6644 1 << (OP_CREATE_SESSION - 32) |
6645 1 << (OP_DESTROY_SESSION - 32) |
6646 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006647 },
6648 .allow.u.words = {
6649 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006650 1 << (OP_LOCKU) |
6651 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006652 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006653 1 << (OP_SECINFO_NO_NAME - 32) |
6654 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006655 1 << (OP_FREE_STATEID - 32) |
6656 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006657 }
6658};
6659
6660/*
6661 * Select the state protection mode for client `clp' given the server results
6662 * from exchange_id in `sp'.
6663 *
6664 * Returns 0 on success, negative errno otherwise.
6665 */
6666static int nfs4_sp4_select_mode(struct nfs_client *clp,
6667 struct nfs41_state_protection *sp)
6668{
6669 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6670 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6671 1 << (OP_EXCHANGE_ID - 32) |
6672 1 << (OP_CREATE_SESSION - 32) |
6673 1 << (OP_DESTROY_SESSION - 32) |
6674 1 << (OP_DESTROY_CLIENTID - 32)
6675 };
6676 unsigned int i;
6677
6678 if (sp->how == SP4_MACH_CRED) {
6679 /* Print state protect result */
6680 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6681 for (i = 0; i <= LAST_NFS4_OP; i++) {
6682 if (test_bit(i, sp->enforce.u.longs))
6683 dfprintk(MOUNT, " enforce op %d\n", i);
6684 if (test_bit(i, sp->allow.u.longs))
6685 dfprintk(MOUNT, " allow op %d\n", i);
6686 }
6687
6688 /* make sure nothing is on enforce list that isn't supported */
6689 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6690 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6691 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6692 return -EINVAL;
6693 }
6694 }
6695
6696 /*
6697 * Minimal mode - state operations are allowed to use machine
6698 * credential. Note this already happens by default, so the
6699 * client doesn't have to do anything more than the negotiation.
6700 *
6701 * NOTE: we don't care if EXCHANGE_ID is in the list -
6702 * we're already using the machine cred for exchange_id
6703 * and will never use a different cred.
6704 */
6705 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6706 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6707 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6708 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6709 dfprintk(MOUNT, "sp4_mach_cred:\n");
6710 dfprintk(MOUNT, " minimal mode enabled\n");
6711 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6712 } else {
6713 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6714 return -EINVAL;
6715 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006716
6717 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6718 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6719 dfprintk(MOUNT, " cleanup mode enabled\n");
6720 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6721 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006722
6723 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6724 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6725 dfprintk(MOUNT, " secinfo mode enabled\n");
6726 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6727 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006728
6729 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6730 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6731 dfprintk(MOUNT, " stateid mode enabled\n");
6732 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6733 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006734
6735 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6736 dfprintk(MOUNT, " write mode enabled\n");
6737 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6738 }
6739
6740 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6741 dfprintk(MOUNT, " commit mode enabled\n");
6742 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6743 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006744 }
6745
6746 return 0;
6747}
6748
6749/*
6750 * _nfs4_proc_exchange_id()
6751 *
6752 * Wrapper for EXCHANGE_ID operation.
6753 */
6754static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6755 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006756{
6757 nfs4_verifier verifier;
6758 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006759 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006760 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006761#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006762 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006763 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6764 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6765#else
6766 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6767 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6768#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006769 };
6770 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006771 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006772 };
6773 int status;
6774 struct rpc_message msg = {
6775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6776 .rpc_argp = &args,
6777 .rpc_resp = &res,
6778 .rpc_cred = cred,
6779 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006780
Chuck Leverf0920752012-05-21 22:45:41 -04006781 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006782 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6783 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006784 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6785 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6786 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006787
Chuck Leveracdeb692012-05-21 22:46:16 -04006788 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006789 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006790 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006791 status = -ENOMEM;
6792 goto out;
6793 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006794
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006795 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006796 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006797 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006798 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006799 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006800 }
6801
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006802 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006803 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006804 status = -ENOMEM;
6805 goto out_server_scope;
6806 }
6807
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006808 switch (sp4_how) {
6809 case SP4_NONE:
6810 args.state_protect.how = SP4_NONE;
6811 break;
6812
6813 case SP4_MACH_CRED:
6814 args.state_protect = nfs4_sp4_mach_cred_request;
6815 break;
6816
6817 default:
6818 /* unsupported! */
6819 WARN_ON_ONCE(1);
6820 status = -EINVAL;
6821 goto out_server_scope;
6822 }
6823
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006824 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006825 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006826 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006827 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006828
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006829 if (status == 0)
6830 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6831
Chuck Lever177313f2012-05-21 22:44:58 -04006832 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006833 clp->cl_clientid = res.clientid;
6834 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6835 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6836 clp->cl_seqid = res.seqid;
6837
Chuck Leveracdeb692012-05-21 22:46:16 -04006838 kfree(clp->cl_serverowner);
6839 clp->cl_serverowner = res.server_owner;
6840 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006841
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006842 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006843 kfree(clp->cl_implid);
6844 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006845
Chuck Lever177313f2012-05-21 22:44:58 -04006846 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006847 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006848 res.server_scope)) {
6849 dprintk("%s: server_scope mismatch detected\n",
6850 __func__);
6851 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006852 kfree(clp->cl_serverscope);
6853 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006854 }
6855
Chuck Lever177313f2012-05-21 22:44:58 -04006856 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006857 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006858 goto out;
6859 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006860 } else
6861 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006862
Chuck Leveracdeb692012-05-21 22:46:16 -04006863out_server_owner:
6864 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006865out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006866 kfree(res.server_scope);
6867out:
Chuck Lever177313f2012-05-21 22:44:58 -04006868 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006869 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006870 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006871 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006872 clp->cl_implid->date.seconds,
6873 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006874 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006875 return status;
6876}
6877
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006878/*
6879 * nfs4_proc_exchange_id()
6880 *
6881 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6882 *
6883 * Since the clientid has expired, all compounds using sessions
6884 * associated with the stale clientid will be returning
6885 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6886 * be in some phase of session reset.
6887 *
6888 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6889 */
6890int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6891{
6892 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6893 int status;
6894
6895 /* try SP4_MACH_CRED if krb5i/p */
6896 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6897 authflavor == RPC_AUTH_GSS_KRB5P) {
6898 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6899 if (!status)
6900 return 0;
6901 }
6902
6903 /* try SP4_NONE */
6904 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6905}
6906
Trond Myklebust66245532012-05-25 17:18:09 -04006907static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6908 struct rpc_cred *cred)
6909{
6910 struct rpc_message msg = {
6911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6912 .rpc_argp = clp,
6913 .rpc_cred = cred,
6914 };
6915 int status;
6916
6917 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006918 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006919 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006920 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006921 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6922 return status;
6923}
6924
6925static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6926 struct rpc_cred *cred)
6927{
6928 unsigned int loop;
6929 int ret;
6930
6931 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6932 ret = _nfs4_proc_destroy_clientid(clp, cred);
6933 switch (ret) {
6934 case -NFS4ERR_DELAY:
6935 case -NFS4ERR_CLIENTID_BUSY:
6936 ssleep(1);
6937 break;
6938 default:
6939 return ret;
6940 }
6941 }
6942 return 0;
6943}
6944
6945int nfs4_destroy_clientid(struct nfs_client *clp)
6946{
6947 struct rpc_cred *cred;
6948 int ret = 0;
6949
6950 if (clp->cl_mvops->minor_version < 1)
6951 goto out;
6952 if (clp->cl_exchange_flags == 0)
6953 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006954 if (clp->cl_preserve_clid)
6955 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006956 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006957 ret = nfs4_proc_destroy_clientid(clp, cred);
6958 if (cred)
6959 put_rpccred(cred);
6960 switch (ret) {
6961 case 0:
6962 case -NFS4ERR_STALE_CLIENTID:
6963 clp->cl_exchange_flags = 0;
6964 }
6965out:
6966 return ret;
6967}
6968
Andy Adamson2050f0c2009-04-01 09:22:30 -04006969struct nfs4_get_lease_time_data {
6970 struct nfs4_get_lease_time_args *args;
6971 struct nfs4_get_lease_time_res *res;
6972 struct nfs_client *clp;
6973};
6974
6975static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6976 void *calldata)
6977{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006978 struct nfs4_get_lease_time_data *data =
6979 (struct nfs4_get_lease_time_data *)calldata;
6980
6981 dprintk("--> %s\n", __func__);
6982 /* just setup sequence, do not trigger session recovery
6983 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006984 nfs41_setup_sequence(data->clp->cl_session,
6985 &data->args->la_seq_args,
6986 &data->res->lr_seq_res,
6987 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006988 dprintk("<-- %s\n", __func__);
6989}
6990
6991/*
6992 * Called from nfs4_state_manager thread for session setup, so don't recover
6993 * from sequence operation or clientid errors.
6994 */
6995static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6996{
6997 struct nfs4_get_lease_time_data *data =
6998 (struct nfs4_get_lease_time_data *)calldata;
6999
7000 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007001 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7002 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007003 switch (task->tk_status) {
7004 case -NFS4ERR_DELAY:
7005 case -NFS4ERR_GRACE:
7006 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7007 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7008 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007009 /* fall through */
7010 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007011 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007012 return;
7013 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007014 dprintk("<-- %s\n", __func__);
7015}
7016
Trond Myklebust17280172012-03-11 13:11:00 -04007017static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007018 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7019 .rpc_call_done = nfs4_get_lease_time_done,
7020};
7021
7022int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7023{
7024 struct rpc_task *task;
7025 struct nfs4_get_lease_time_args args;
7026 struct nfs4_get_lease_time_res res = {
7027 .lr_fsinfo = fsinfo,
7028 };
7029 struct nfs4_get_lease_time_data data = {
7030 .args = &args,
7031 .res = &res,
7032 .clp = clp,
7033 };
7034 struct rpc_message msg = {
7035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7036 .rpc_argp = &args,
7037 .rpc_resp = &res,
7038 };
7039 struct rpc_task_setup task_setup = {
7040 .rpc_client = clp->cl_rpcclient,
7041 .rpc_message = &msg,
7042 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007043 .callback_data = &data,
7044 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007045 };
7046 int status;
7047
Chuck Levera9c92d62013-08-09 12:48:18 -04007048 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007049 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007050 dprintk("--> %s\n", __func__);
7051 task = rpc_run_task(&task_setup);
7052
7053 if (IS_ERR(task))
7054 status = PTR_ERR(task);
7055 else {
7056 status = task->tk_status;
7057 rpc_put_task(task);
7058 }
7059 dprintk("<-- %s return %d\n", __func__, status);
7060
7061 return status;
7062}
7063
Andy Adamsonfc931582009-04-01 09:22:31 -04007064/*
7065 * Initialize the values to be used by the client in CREATE_SESSION
7066 * If nfs4_init_session set the fore channel request and response sizes,
7067 * use them.
7068 *
7069 * Set the back channel max_resp_sz_cached to zero to force the client to
7070 * always set csa_cachethis to FALSE because the current implementation
7071 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7072 */
7073static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7074{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007075 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007076
Andy Adamson18aad3d2013-06-26 12:21:49 -04007077 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7078 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7079
Andy Adamsonfc931582009-04-01 09:22:31 -04007080 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007081 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7082 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007083 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007084 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007085
7086 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007087 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007088 __func__,
7089 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007090 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007091
7092 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007093 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7094 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7095 args->bc_attrs.max_resp_sz_cached = 0;
7096 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7097 args->bc_attrs.max_reqs = 1;
7098
7099 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7100 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7101 __func__,
7102 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7103 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7104 args->bc_attrs.max_reqs);
7105}
7106
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007107static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04007108{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007109 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7110 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
7111
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007112 if (rcvd->max_resp_sz > sent->max_resp_sz)
7113 return -EINVAL;
7114 /*
7115 * Our requested max_ops is the minimum we need; we're not
7116 * prepared to break up compounds into smaller pieces than that.
7117 * So, no point even trying to continue if the server won't
7118 * cooperate:
7119 */
7120 if (rcvd->max_ops < sent->max_ops)
7121 return -EINVAL;
7122 if (rcvd->max_reqs == 0)
7123 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007124 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7125 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007126 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007127}
7128
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007129static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7130{
7131 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7132 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007133
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007134 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7135 return -EINVAL;
7136 if (rcvd->max_resp_sz < sent->max_resp_sz)
7137 return -EINVAL;
7138 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7139 return -EINVAL;
7140 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007141 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007142 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007143 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007144 return -EINVAL;
7145 return 0;
7146}
Andy Adamson8d353012009-04-01 09:22:32 -04007147
Andy Adamson8d353012009-04-01 09:22:32 -04007148static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7149 struct nfs4_session *session)
7150{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007151 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007152
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007153 ret = nfs4_verify_fore_channel_attrs(args, session);
7154 if (ret)
7155 return ret;
7156 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04007157}
7158
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007159static int _nfs4_proc_create_session(struct nfs_client *clp,
7160 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007161{
7162 struct nfs4_session *session = clp->cl_session;
7163 struct nfs41_create_session_args args = {
7164 .client = clp,
7165 .cb_program = NFS4_CALLBACK,
7166 };
7167 struct nfs41_create_session_res res = {
7168 .client = clp,
7169 };
7170 struct rpc_message msg = {
7171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7172 .rpc_argp = &args,
7173 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007174 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007175 };
7176 int status;
7177
7178 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007179 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007180
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007181 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007182 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007183
Trond Myklebust43095d32012-11-20 11:13:12 -05007184 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007185 /* Verify the session's negotiated channel_attrs values */
7186 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04007187 /* Increment the clientid slot sequence id */
7188 clp->cl_seqid++;
7189 }
7190
7191 return status;
7192}
7193
7194/*
7195 * Issues a CREATE_SESSION operation to the server.
7196 * It is the responsibility of the caller to verify the session is
7197 * expired before calling this routine.
7198 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007199int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007200{
7201 int status;
7202 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007203 struct nfs4_session *session = clp->cl_session;
7204
7205 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7206
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007207 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007208 if (status)
7209 goto out;
7210
Andy Adamsonaacd5532011-11-09 13:58:21 -05007211 /* Init or reset the session slot tables */
7212 status = nfs4_setup_session_slot_tables(session);
7213 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007214 if (status)
7215 goto out;
7216
7217 ptr = (unsigned *)&session->sess_id.data[0];
7218 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7219 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007220out:
7221 dprintk("<-- %s\n", __func__);
7222 return status;
7223}
7224
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007225/*
7226 * Issue the over-the-wire RPC DESTROY_SESSION.
7227 * The caller must serialize access to this routine.
7228 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007229int nfs4_proc_destroy_session(struct nfs4_session *session,
7230 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007231{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007232 struct rpc_message msg = {
7233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7234 .rpc_argp = session,
7235 .rpc_cred = cred,
7236 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007237 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007238
7239 dprintk("--> nfs4_proc_destroy_session\n");
7240
7241 /* session is still being setup */
7242 if (session->clp->cl_cons_state != NFS_CS_READY)
7243 return status;
7244
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007245 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007246 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007247
7248 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007249 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007250 "Session has been destroyed regardless...\n", status);
7251
7252 dprintk("<-- nfs4_proc_destroy_session\n");
7253 return status;
7254}
7255
Trond Myklebust7b38c362012-05-23 13:23:31 -04007256/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007257 * Renew the cl_session lease.
7258 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007259struct nfs4_sequence_data {
7260 struct nfs_client *clp;
7261 struct nfs4_sequence_args args;
7262 struct nfs4_sequence_res res;
7263};
7264
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007265static void nfs41_sequence_release(void *data)
7266{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007267 struct nfs4_sequence_data *calldata = data;
7268 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007269
Alexandros Batsakis71358402010-02-05 03:45:05 -08007270 if (atomic_read(&clp->cl_count) > 1)
7271 nfs4_schedule_state_renewal(clp);
7272 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007273 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007274}
7275
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007276static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7277{
7278 switch(task->tk_status) {
7279 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007280 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7281 return -EAGAIN;
7282 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007283 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007284 }
7285 return 0;
7286}
7287
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007288static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007289{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007290 struct nfs4_sequence_data *calldata = data;
7291 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007292
Trond Myklebust14516c32010-07-31 14:29:06 -04007293 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7294 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007295
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007296 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007297 if (task->tk_status < 0) {
7298 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007299 if (atomic_read(&clp->cl_count) == 1)
7300 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007301
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007302 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7303 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007304 return;
7305 }
7306 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007307 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007308out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007309 dprintk("<-- %s\n", __func__);
7310}
7311
7312static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7313{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007314 struct nfs4_sequence_data *calldata = data;
7315 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007316 struct nfs4_sequence_args *args;
7317 struct nfs4_sequence_res *res;
7318
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007319 args = task->tk_msg.rpc_argp;
7320 res = task->tk_msg.rpc_resp;
7321
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007322 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007323}
7324
7325static const struct rpc_call_ops nfs41_sequence_ops = {
7326 .rpc_call_done = nfs41_sequence_call_done,
7327 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007328 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007329};
7330
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007331static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7332 struct rpc_cred *cred,
7333 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007334{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007335 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007336 struct rpc_message msg = {
7337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7338 .rpc_cred = cred,
7339 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007340 struct rpc_task_setup task_setup_data = {
7341 .rpc_client = clp->cl_rpcclient,
7342 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007343 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007344 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007345 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007346
Alexandros Batsakis71358402010-02-05 03:45:05 -08007347 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007348 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007349 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007350 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007351 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007352 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007353 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007354 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007355 if (is_privileged)
7356 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007357 msg.rpc_argp = &calldata->args;
7358 msg.rpc_resp = &calldata->res;
7359 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007360 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007361
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007362 return rpc_run_task(&task_setup_data);
7363}
7364
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007365static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007366{
7367 struct rpc_task *task;
7368 int ret = 0;
7369
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007370 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7371 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007372 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007373 if (IS_ERR(task))
7374 ret = PTR_ERR(task);
7375 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007376 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007377 dprintk("<-- %s status=%d\n", __func__, ret);
7378 return ret;
7379}
7380
7381static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7382{
7383 struct rpc_task *task;
7384 int ret;
7385
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007386 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007387 if (IS_ERR(task)) {
7388 ret = PTR_ERR(task);
7389 goto out;
7390 }
7391 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007392 if (!ret) {
7393 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7394
7395 if (task->tk_status == 0)
7396 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007397 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007398 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007399 rpc_put_task(task);
7400out:
7401 dprintk("<-- %s status=%d\n", __func__, ret);
7402 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007403}
7404
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007405struct nfs4_reclaim_complete_data {
7406 struct nfs_client *clp;
7407 struct nfs41_reclaim_complete_args arg;
7408 struct nfs41_reclaim_complete_res res;
7409};
7410
7411static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7412{
7413 struct nfs4_reclaim_complete_data *calldata = data;
7414
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007415 nfs41_setup_sequence(calldata->clp->cl_session,
7416 &calldata->arg.seq_args,
7417 &calldata->res.seq_res,
7418 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007419}
7420
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007421static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7422{
7423 switch(task->tk_status) {
7424 case 0:
7425 case -NFS4ERR_COMPLETE_ALREADY:
7426 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7427 break;
7428 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007429 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007430 /* fall through */
7431 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007432 return -EAGAIN;
7433 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007434 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007435 }
7436 return 0;
7437}
7438
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007439static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7440{
7441 struct nfs4_reclaim_complete_data *calldata = data;
7442 struct nfs_client *clp = calldata->clp;
7443 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7444
7445 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007446 if (!nfs41_sequence_done(task, res))
7447 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007448
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007449 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007450 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7451 rpc_restart_call_prepare(task);
7452 return;
7453 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007454 dprintk("<-- %s\n", __func__);
7455}
7456
7457static void nfs4_free_reclaim_complete_data(void *data)
7458{
7459 struct nfs4_reclaim_complete_data *calldata = data;
7460
7461 kfree(calldata);
7462}
7463
7464static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7465 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7466 .rpc_call_done = nfs4_reclaim_complete_done,
7467 .rpc_release = nfs4_free_reclaim_complete_data,
7468};
7469
7470/*
7471 * Issue a global reclaim complete.
7472 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007473static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7474 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007475{
7476 struct nfs4_reclaim_complete_data *calldata;
7477 struct rpc_task *task;
7478 struct rpc_message msg = {
7479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007480 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007481 };
7482 struct rpc_task_setup task_setup_data = {
7483 .rpc_client = clp->cl_rpcclient,
7484 .rpc_message = &msg,
7485 .callback_ops = &nfs4_reclaim_complete_call_ops,
7486 .flags = RPC_TASK_ASYNC,
7487 };
7488 int status = -ENOMEM;
7489
7490 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007491 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007492 if (calldata == NULL)
7493 goto out;
7494 calldata->clp = clp;
7495 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007496
Chuck Levera9c92d62013-08-09 12:48:18 -04007497 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007498 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007499 msg.rpc_argp = &calldata->arg;
7500 msg.rpc_resp = &calldata->res;
7501 task_setup_data.callback_data = calldata;
7502 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007503 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007504 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007505 goto out;
7506 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007507 status = nfs4_wait_for_completion_rpc_task(task);
7508 if (status == 0)
7509 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007510 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007511 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007512out:
7513 dprintk("<-- %s status=%d\n", __func__, status);
7514 return status;
7515}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007516
7517static void
7518nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7519{
7520 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007521 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007522 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007523
7524 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007525 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7526 * right now covering the LAYOUTGET we are about to send.
7527 * However, that is not so catastrophic, and there seems
7528 * to be no way to prevent it completely.
7529 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007530 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007531 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007532 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007533 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7534 NFS_I(lgp->args.inode)->layout,
7535 lgp->args.ctx->state)) {
7536 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007537 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007538}
7539
7540static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7541{
7542 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007543 struct inode *inode = lgp->args.inode;
7544 struct nfs_server *server = NFS_SERVER(inode);
7545 struct pnfs_layout_hdr *lo;
7546 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007547 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007548
Boaz Harroshed7e5422014-01-22 20:34:54 +02007549 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007550
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007551 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007552 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007553
7554 switch (task->tk_status) {
7555 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007556 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007557 /*
7558 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7559 * (or clients) writing to the same RAID stripe
7560 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007561 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007562 /*
7563 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7564 * existing layout before getting a new one).
7565 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007566 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007567 timeo = rpc_get_timeout(task->tk_client);
7568 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007569 now = jiffies;
7570 if (time_after(giveup, now)) {
7571 unsigned long delay;
7572
7573 /* Delay for:
7574 * - Not less then NFS4_POLL_RETRY_MIN.
7575 * - One last time a jiffie before we give up
7576 * - exponential backoff (time_now minus start_attempt)
7577 */
7578 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7579 min((giveup - now - 1),
7580 now - lgp->args.timestamp));
7581
7582 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7583 __func__, delay);
7584 rpc_delay(task, delay);
7585 task->tk_status = 0;
7586 rpc_restart_call_prepare(task);
7587 goto out; /* Do not call nfs4_async_handle_error() */
7588 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007589 break;
7590 case -NFS4ERR_EXPIRED:
7591 case -NFS4ERR_BAD_STATEID:
7592 spin_lock(&inode->i_lock);
7593 lo = NFS_I(inode)->layout;
7594 if (!lo || list_empty(&lo->plh_segs)) {
7595 spin_unlock(&inode->i_lock);
7596 /* If the open stateid was bad, then recover it. */
7597 state = lgp->args.ctx->state;
7598 } else {
7599 LIST_HEAD(head);
7600
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007601 /*
7602 * Mark the bad layout state as invalid, then retry
7603 * with the current stateid.
7604 */
Trond Myklebustee314c22012-10-01 17:25:48 -07007605 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7606 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007607 pnfs_free_lseg_list(&head);
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007608
7609 task->tk_status = 0;
7610 rpc_restart_call_prepare(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007611 }
7612 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007613 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebustee314c22012-10-01 17:25:48 -07007614 rpc_restart_call_prepare(task);
7615out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007616 dprintk("<-- %s\n", __func__);
7617}
7618
Idan Kedar85541162012-08-02 11:47:10 +03007619static size_t max_response_pages(struct nfs_server *server)
7620{
7621 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7622 return nfs_page_array_len(0, max_resp_sz);
7623}
7624
7625static void nfs4_free_pages(struct page **pages, size_t size)
7626{
7627 int i;
7628
7629 if (!pages)
7630 return;
7631
7632 for (i = 0; i < size; i++) {
7633 if (!pages[i])
7634 break;
7635 __free_page(pages[i]);
7636 }
7637 kfree(pages);
7638}
7639
7640static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7641{
7642 struct page **pages;
7643 int i;
7644
7645 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7646 if (!pages) {
7647 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7648 return NULL;
7649 }
7650
7651 for (i = 0; i < size; i++) {
7652 pages[i] = alloc_page(gfp_flags);
7653 if (!pages[i]) {
7654 dprintk("%s: failed to allocate page\n", __func__);
7655 nfs4_free_pages(pages, size);
7656 return NULL;
7657 }
7658 }
7659
7660 return pages;
7661}
7662
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007663static void nfs4_layoutget_release(void *calldata)
7664{
7665 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007666 struct inode *inode = lgp->args.inode;
7667 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007668 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007669
7670 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007671 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007672 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007673 put_nfs_open_context(lgp->args.ctx);
7674 kfree(calldata);
7675 dprintk("<-- %s\n", __func__);
7676}
7677
7678static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7679 .rpc_call_prepare = nfs4_layoutget_prepare,
7680 .rpc_call_done = nfs4_layoutget_done,
7681 .rpc_release = nfs4_layoutget_release,
7682};
7683
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007684struct pnfs_layout_segment *
7685nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007686{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007687 struct inode *inode = lgp->args.inode;
7688 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007689 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007690 struct rpc_task *task;
7691 struct rpc_message msg = {
7692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7693 .rpc_argp = &lgp->args,
7694 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007695 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007696 };
7697 struct rpc_task_setup task_setup_data = {
7698 .rpc_client = server->client,
7699 .rpc_message = &msg,
7700 .callback_ops = &nfs4_layoutget_call_ops,
7701 .callback_data = lgp,
7702 .flags = RPC_TASK_ASYNC,
7703 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007704 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007705 int status = 0;
7706
7707 dprintk("--> %s\n", __func__);
7708
Idan Kedar85541162012-08-02 11:47:10 +03007709 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7710 if (!lgp->args.layout.pages) {
7711 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007712 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007713 }
7714 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007715 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007716
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007717 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007718 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007719 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007720
7721 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7722 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7723
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007724 task = rpc_run_task(&task_setup_data);
7725 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007726 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007727 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007728 if (status == 0)
7729 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007730 trace_nfs4_layoutget(lgp->args.ctx,
7731 &lgp->args.range,
7732 &lgp->res.range,
7733 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007734 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7735 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007736 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007737 rpc_put_task(task);
7738 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007739 if (status)
7740 return ERR_PTR(status);
7741 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007742}
7743
Benny Halevycbe82602011-05-22 19:52:37 +03007744static void
7745nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7746{
7747 struct nfs4_layoutreturn *lrp = calldata;
7748
7749 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007750 nfs41_setup_sequence(lrp->clp->cl_session,
7751 &lrp->args.seq_args,
7752 &lrp->res.seq_res,
7753 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007754}
7755
7756static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7757{
7758 struct nfs4_layoutreturn *lrp = calldata;
7759 struct nfs_server *server;
7760
7761 dprintk("--> %s\n", __func__);
7762
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007763 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007764 return;
7765
7766 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007767 switch (task->tk_status) {
7768 default:
7769 task->tk_status = 0;
7770 case 0:
7771 break;
7772 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007773 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007774 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007775 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007776 return;
7777 }
Benny Halevycbe82602011-05-22 19:52:37 +03007778 dprintk("<-- %s\n", __func__);
7779}
7780
7781static void nfs4_layoutreturn_release(void *calldata)
7782{
7783 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007784 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007785
7786 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007787 spin_lock(&lo->plh_inode->i_lock);
7788 if (lrp->res.lrs_present)
7789 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7790 lo->plh_block_lgets--;
7791 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007792 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007793 kfree(calldata);
7794 dprintk("<-- %s\n", __func__);
7795}
7796
7797static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7798 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7799 .rpc_call_done = nfs4_layoutreturn_done,
7800 .rpc_release = nfs4_layoutreturn_release,
7801};
7802
7803int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7804{
7805 struct rpc_task *task;
7806 struct rpc_message msg = {
7807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7808 .rpc_argp = &lrp->args,
7809 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007810 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007811 };
7812 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007813 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007814 .rpc_message = &msg,
7815 .callback_ops = &nfs4_layoutreturn_call_ops,
7816 .callback_data = lrp,
7817 };
7818 int status;
7819
7820 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007821 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007822 task = rpc_run_task(&task_setup_data);
7823 if (IS_ERR(task))
7824 return PTR_ERR(task);
7825 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007826 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007827 dprintk("<-- %s status=%d\n", __func__, status);
7828 rpc_put_task(task);
7829 return status;
7830}
7831
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007832static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007833_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7834 struct pnfs_device *pdev,
7835 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007836{
7837 struct nfs4_getdeviceinfo_args args = {
7838 .pdev = pdev,
7839 };
7840 struct nfs4_getdeviceinfo_res res = {
7841 .pdev = pdev,
7842 };
7843 struct rpc_message msg = {
7844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7845 .rpc_argp = &args,
7846 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007847 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007848 };
7849 int status;
7850
7851 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007852 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007853 dprintk("<-- %s status=%d\n", __func__, status);
7854
7855 return status;
7856}
7857
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007858int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7859 struct pnfs_device *pdev,
7860 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007861{
7862 struct nfs4_exception exception = { };
7863 int err;
7864
7865 do {
7866 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007867 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007868 &exception);
7869 } while (exception.retry);
7870 return err;
7871}
7872EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7873
Andy Adamson863a3c62011-03-23 13:27:54 +00007874static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7875{
7876 struct nfs4_layoutcommit_data *data = calldata;
7877 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007878 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007879
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007880 nfs41_setup_sequence(session,
7881 &data->args.seq_args,
7882 &data->res.seq_res,
7883 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007884}
7885
7886static void
7887nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7888{
7889 struct nfs4_layoutcommit_data *data = calldata;
7890 struct nfs_server *server = NFS_SERVER(data->args.inode);
7891
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007892 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007893 return;
7894
7895 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007896 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7897 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7898 case -NFS4ERR_BADLAYOUT: /* no layout */
7899 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007900 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007901 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007902 break;
7903 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10007904 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007905 rpc_restart_call_prepare(task);
7906 return;
7907 }
7908 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007909}
7910
7911static void nfs4_layoutcommit_release(void *calldata)
7912{
7913 struct nfs4_layoutcommit_data *data = calldata;
7914
Andy Adamsondb29c082011-07-30 20:52:38 -04007915 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05007916 nfs_post_op_update_inode_force_wcc(data->args.inode,
7917 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00007918 put_rpccred(data->cred);
7919 kfree(data);
7920}
7921
7922static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7923 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7924 .rpc_call_done = nfs4_layoutcommit_done,
7925 .rpc_release = nfs4_layoutcommit_release,
7926};
7927
7928int
Andy Adamsonef311532011-03-12 02:58:10 -05007929nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007930{
7931 struct rpc_message msg = {
7932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7933 .rpc_argp = &data->args,
7934 .rpc_resp = &data->res,
7935 .rpc_cred = data->cred,
7936 };
7937 struct rpc_task_setup task_setup_data = {
7938 .task = &data->task,
7939 .rpc_client = NFS_CLIENT(data->args.inode),
7940 .rpc_message = &msg,
7941 .callback_ops = &nfs4_layoutcommit_ops,
7942 .callback_data = data,
7943 .flags = RPC_TASK_ASYNC,
7944 };
7945 struct rpc_task *task;
7946 int status = 0;
7947
7948 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7949 "lbw: %llu inode %lu\n",
7950 data->task.tk_pid, sync,
7951 data->args.lastbytewritten,
7952 data->args.inode->i_ino);
7953
Chuck Levera9c92d62013-08-09 12:48:18 -04007954 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007955 task = rpc_run_task(&task_setup_data);
7956 if (IS_ERR(task))
7957 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007958 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007959 goto out;
7960 status = nfs4_wait_for_completion_rpc_task(task);
7961 if (status != 0)
7962 goto out;
7963 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007964 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007965out:
7966 dprintk("%s: status %d\n", __func__, status);
7967 rpc_put_task(task);
7968 return status;
7969}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007970
Andy Adamson97431202013-08-08 10:57:56 -04007971/**
7972 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7973 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7974 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007975static int
7976_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007977 struct nfs_fsinfo *info,
7978 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007979{
7980 struct nfs41_secinfo_no_name_args args = {
7981 .style = SECINFO_STYLE_CURRENT_FH,
7982 };
7983 struct nfs4_secinfo_res res = {
7984 .flavors = flavors,
7985 };
7986 struct rpc_message msg = {
7987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7988 .rpc_argp = &args,
7989 .rpc_resp = &res,
7990 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007991 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007992 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007993 int status;
7994
7995 if (use_integrity) {
7996 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007997 cred = nfs4_get_clid_cred(server->nfs_client);
7998 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007999 }
8000
8001 dprintk("--> %s\n", __func__);
8002 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8003 &res.seq_res, 0);
8004 dprintk("<-- %s status=%d\n", __func__, status);
8005
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008006 if (cred)
8007 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008008
8009 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008010}
8011
8012static int
8013nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8014 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8015{
8016 struct nfs4_exception exception = { };
8017 int err;
8018 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008019 /* first try using integrity protection */
8020 err = -NFS4ERR_WRONGSEC;
8021
8022 /* try to use integrity protection with machine cred */
8023 if (_nfs4_is_integrity_protected(server->nfs_client))
8024 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8025 flavors, true);
8026
8027 /*
8028 * if unable to use integrity protection, or SECINFO with
8029 * integrity protection returns NFS4ERR_WRONGSEC (which is
8030 * disallowed by spec, but exists in deployed servers) use
8031 * the current filesystem's rpc_client and the user cred.
8032 */
8033 if (err == -NFS4ERR_WRONGSEC)
8034 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8035 flavors, false);
8036
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008037 switch (err) {
8038 case 0:
8039 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008040 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008041 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008042 default:
8043 err = nfs4_handle_exception(server, err, &exception);
8044 }
8045 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008046out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008047 return err;
8048}
8049
8050static int
8051nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8052 struct nfs_fsinfo *info)
8053{
8054 int err;
8055 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008056 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008057 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008058 struct nfs4_secinfo4 *secinfo;
8059 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008060
8061 page = alloc_page(GFP_KERNEL);
8062 if (!page) {
8063 err = -ENOMEM;
8064 goto out;
8065 }
8066
8067 flavors = page_address(page);
8068 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8069
8070 /*
8071 * Fall back on "guess and check" method if
8072 * the server doesn't support SECINFO_NO_NAME
8073 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008074 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008075 err = nfs4_find_root_sec(server, fhandle, info);
8076 goto out_freepage;
8077 }
8078 if (err)
8079 goto out_freepage;
8080
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008081 for (i = 0; i < flavors->num_flavors; i++) {
8082 secinfo = &flavors->flavors[i];
8083
8084 switch (secinfo->flavor) {
8085 case RPC_AUTH_NULL:
8086 case RPC_AUTH_UNIX:
8087 case RPC_AUTH_GSS:
8088 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8089 &secinfo->flavor_info);
8090 break;
8091 default:
8092 flavor = RPC_AUTH_MAXFLAVOR;
8093 break;
8094 }
8095
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008096 if (!nfs_auth_info_match(&server->auth_info, flavor))
8097 flavor = RPC_AUTH_MAXFLAVOR;
8098
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008099 if (flavor != RPC_AUTH_MAXFLAVOR) {
8100 err = nfs4_lookup_root_sec(server, fhandle,
8101 info, flavor);
8102 if (!err)
8103 break;
8104 }
8105 }
8106
8107 if (flavor == RPC_AUTH_MAXFLAVOR)
8108 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008109
8110out_freepage:
8111 put_page(page);
8112 if (err == -EACCES)
8113 return -EPERM;
8114out:
8115 return err;
8116}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008117
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008118static int _nfs41_test_stateid(struct nfs_server *server,
8119 nfs4_stateid *stateid,
8120 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008121{
8122 int status;
8123 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008124 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008125 };
8126 struct nfs41_test_stateid_res res;
8127 struct rpc_message msg = {
8128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8129 .rpc_argp = &args,
8130 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008131 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008132 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008133 struct rpc_clnt *rpc_client = server->client;
8134
8135 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8136 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008137
Chuck Lever38527b12012-07-11 16:30:23 -04008138 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008139 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008140 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008141 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008142 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008143 if (status != NFS_OK) {
8144 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008145 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008146 }
8147 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008148 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008149}
8150
Chuck Lever38527b12012-07-11 16:30:23 -04008151/**
8152 * nfs41_test_stateid - perform a TEST_STATEID operation
8153 *
8154 * @server: server / transport on which to perform the operation
8155 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008156 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008157 *
8158 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8159 * Otherwise a negative NFS4ERR value is returned if the operation
8160 * failed or the state ID is not currently valid.
8161 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008162static int nfs41_test_stateid(struct nfs_server *server,
8163 nfs4_stateid *stateid,
8164 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008165{
8166 struct nfs4_exception exception = { };
8167 int err;
8168 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008169 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008170 if (err != -NFS4ERR_DELAY)
8171 break;
8172 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008173 } while (exception.retry);
8174 return err;
8175}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008176
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008177struct nfs_free_stateid_data {
8178 struct nfs_server *server;
8179 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008180 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008181};
8182
8183static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8184{
8185 struct nfs_free_stateid_data *data = calldata;
8186 nfs41_setup_sequence(nfs4_get_session(data->server),
8187 &data->args.seq_args,
8188 &data->res.seq_res,
8189 task);
8190}
8191
8192static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8193{
8194 struct nfs_free_stateid_data *data = calldata;
8195
8196 nfs41_sequence_done(task, &data->res.seq_res);
8197
8198 switch (task->tk_status) {
8199 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008200 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008201 rpc_restart_call_prepare(task);
8202 }
8203}
8204
8205static void nfs41_free_stateid_release(void *calldata)
8206{
8207 kfree(calldata);
8208}
8209
Trond Myklebust17f26b12013-08-21 15:48:42 -04008210static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008211 .rpc_call_prepare = nfs41_free_stateid_prepare,
8212 .rpc_call_done = nfs41_free_stateid_done,
8213 .rpc_release = nfs41_free_stateid_release,
8214};
8215
8216static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8217 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008218 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008219 bool privileged)
8220{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008221 struct rpc_message msg = {
8222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008223 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008224 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008225 struct rpc_task_setup task_setup = {
8226 .rpc_client = server->client,
8227 .rpc_message = &msg,
8228 .callback_ops = &nfs41_free_stateid_ops,
8229 .flags = RPC_TASK_ASYNC,
8230 };
8231 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008232
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008233 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8234 &task_setup.rpc_client, &msg);
8235
Chuck Lever38527b12012-07-11 16:30:23 -04008236 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008237 data = kmalloc(sizeof(*data), GFP_NOFS);
8238 if (!data)
8239 return ERR_PTR(-ENOMEM);
8240 data->server = server;
8241 nfs4_stateid_copy(&data->args.stateid, stateid);
8242
8243 task_setup.callback_data = data;
8244
8245 msg.rpc_argp = &data->args;
8246 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008247 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008248 if (privileged)
8249 nfs4_set_sequence_privileged(&data->args.seq_args);
8250
8251 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008252}
8253
Chuck Lever38527b12012-07-11 16:30:23 -04008254/**
8255 * nfs41_free_stateid - perform a FREE_STATEID operation
8256 *
8257 * @server: server / transport on which to perform the operation
8258 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008259 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008260 *
8261 * Returns NFS_OK if the server freed "stateid". Otherwise a
8262 * negative NFS4ERR value is returned.
8263 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008264static int nfs41_free_stateid(struct nfs_server *server,
8265 nfs4_stateid *stateid,
8266 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008267{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008268 struct rpc_task *task;
8269 int ret;
8270
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008271 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008272 if (IS_ERR(task))
8273 return PTR_ERR(task);
8274 ret = rpc_wait_for_completion_task(task);
8275 if (!ret)
8276 ret = task->tk_status;
8277 rpc_put_task(task);
8278 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008279}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008280
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008281static void
8282nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008283{
8284 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008285 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008286
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008287 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008288 nfs4_free_lock_state(server, lsp);
8289 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008290 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008291 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008292}
8293
Trond Myklebust36281ca2012-03-04 18:13:56 -05008294static bool nfs41_match_stateid(const nfs4_stateid *s1,
8295 const nfs4_stateid *s2)
8296{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008297 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008298 return false;
8299
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008300 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008301 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008302 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008303 return true;
8304
8305 return false;
8306}
8307
Andy Adamson557134a2009-04-01 09:21:53 -04008308#endif /* CONFIG_NFS_V4_1 */
8309
Trond Myklebust36281ca2012-03-04 18:13:56 -05008310static bool nfs4_match_stateid(const nfs4_stateid *s1,
8311 const nfs4_stateid *s2)
8312{
Trond Myklebustf597c532012-03-04 18:13:56 -05008313 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008314}
8315
8316
Trond Myklebust17280172012-03-11 13:11:00 -04008317static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008318 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008319 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320 .recover_open = nfs4_open_reclaim,
8321 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008322 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008323 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324};
8325
Andy Adamson591d71c2009-04-01 09:22:47 -04008326#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008327static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008328 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8329 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8330 .recover_open = nfs4_open_reclaim,
8331 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008332 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008333 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008334 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008335};
8336#endif /* CONFIG_NFS_V4_1 */
8337
Trond Myklebust17280172012-03-11 13:11:00 -04008338static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008339 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008340 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341 .recover_open = nfs4_open_expired,
8342 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008343 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344};
8345
Andy Adamson591d71c2009-04-01 09:22:47 -04008346#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008347static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008348 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8349 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008350 .recover_open = nfs41_open_expired,
8351 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008352 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008353};
8354#endif /* CONFIG_NFS_V4_1 */
8355
Trond Myklebust17280172012-03-11 13:11:00 -04008356static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008357 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008358 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008359 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008360};
8361
8362#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008363static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008364 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008365 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008366 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008367};
8368#endif
8369
Chuck Leverec011fe2013-10-17 14:12:39 -04008370static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008371 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008372 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008373};
8374
8375#if defined(CONFIG_NFS_V4_1)
8376static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008377 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008378 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008379};
8380#endif /* CONFIG_NFS_V4_1 */
8381
Trond Myklebust97dc1352010-06-16 09:52:26 -04008382static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8383 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008384 .init_caps = NFS_CAP_READDIRPLUS
8385 | NFS_CAP_ATOMIC_OPEN
8386 | NFS_CAP_CHANGE_ATTR
8387 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008388 .init_client = nfs40_init_client,
8389 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008390 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008391 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008392 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008393 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008394 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8395 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8396 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008397 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008398};
8399
8400#if defined(CONFIG_NFS_V4_1)
8401static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8402 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008403 .init_caps = NFS_CAP_READDIRPLUS
8404 | NFS_CAP_ATOMIC_OPEN
8405 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008406 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008407 | NFS_CAP_STATEID_NFSV41
8408 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008409 .init_client = nfs41_init_client,
8410 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008411 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008412 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008413 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008414 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008415 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8416 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8417 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008418 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008419};
8420#endif
8421
Steve Dickson42c2c422013-05-22 12:50:38 -04008422#if defined(CONFIG_NFS_V4_2)
8423static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8424 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008425 .init_caps = NFS_CAP_READDIRPLUS
8426 | NFS_CAP_ATOMIC_OPEN
8427 | NFS_CAP_CHANGE_ATTR
8428 | NFS_CAP_POSIX_LOCK
8429 | NFS_CAP_STATEID_NFSV41
8430 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008431 .init_client = nfs41_init_client,
8432 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008433 .match_stateid = nfs41_match_stateid,
8434 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008435 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008436 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008437 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8438 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8439 .state_renewal_ops = &nfs41_state_renewal_ops,
8440};
8441#endif
8442
Trond Myklebust97dc1352010-06-16 09:52:26 -04008443const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8444 [0] = &nfs_v4_0_minor_ops,
8445#if defined(CONFIG_NFS_V4_1)
8446 [1] = &nfs_v4_1_minor_ops,
8447#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008448#if defined(CONFIG_NFS_V4_2)
8449 [2] = &nfs_v4_2_minor_ops,
8450#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008451};
8452
Trond Myklebust17f26b12013-08-21 15:48:42 -04008453static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008454 .create = nfs_create,
8455 .lookup = nfs_lookup,
8456 .atomic_open = nfs_atomic_open,
8457 .link = nfs_link,
8458 .unlink = nfs_unlink,
8459 .symlink = nfs_symlink,
8460 .mkdir = nfs_mkdir,
8461 .rmdir = nfs_rmdir,
8462 .mknod = nfs_mknod,
8463 .rename = nfs_rename,
8464 .permission = nfs_permission,
8465 .getattr = nfs_getattr,
8466 .setattr = nfs_setattr,
8467 .getxattr = generic_getxattr,
8468 .setxattr = generic_setxattr,
8469 .listxattr = generic_listxattr,
8470 .removexattr = generic_removexattr,
8471};
8472
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008473static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008474 .permission = nfs_permission,
8475 .getattr = nfs_getattr,
8476 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008477 .getxattr = generic_getxattr,
8478 .setxattr = generic_setxattr,
8479 .listxattr = generic_listxattr,
8480 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008481};
8482
David Howells509de812006-08-22 20:06:11 -04008483const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484 .version = 4, /* protocol version */
8485 .dentry_ops = &nfs4_dentry_operations,
8486 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008487 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008488 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008490 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008491 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 .getattr = nfs4_proc_getattr,
8493 .setattr = nfs4_proc_setattr,
8494 .lookup = nfs4_proc_lookup,
8495 .access = nfs4_proc_access,
8496 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497 .create = nfs4_proc_create,
8498 .remove = nfs4_proc_remove,
8499 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008500 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008502 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008503 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008504 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505 .link = nfs4_proc_link,
8506 .symlink = nfs4_proc_symlink,
8507 .mkdir = nfs4_proc_mkdir,
8508 .rmdir = nfs4_proc_remove,
8509 .readdir = nfs4_proc_readdir,
8510 .mknod = nfs4_proc_mknod,
8511 .statfs = nfs4_proc_statfs,
8512 .fsinfo = nfs4_proc_fsinfo,
8513 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008514 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008516 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008518 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008520 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008522 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008523 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008525 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008526 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008527 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008528 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008529 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008530 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008531 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008532 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008533 .create_server = nfs4_create_server,
8534 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535};
8536
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008537static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8538 .prefix = XATTR_NAME_NFSV4_ACL,
8539 .list = nfs4_xattr_list_nfs4_acl,
8540 .get = nfs4_xattr_get_nfs4_acl,
8541 .set = nfs4_xattr_set_nfs4_acl,
8542};
8543
8544const struct xattr_handler *nfs4_xattr_handlers[] = {
8545 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008546#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8547 &nfs4_xattr_nfs4_label_handler,
8548#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008549 NULL
8550};
8551
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552/*
8553 * Local variables:
8554 * c-basic-offset: 8
8555 * End:
8556 */