blob: 405bd95c1f588739a511fb1566ed47f1513e696f [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
Bryan Schumaker7c513052011-03-24 17:12:24 +0000888int nfs4_call_sync(struct rpc_clnt *clnt,
889 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000890 struct rpc_message *msg,
891 struct nfs4_sequence_args *args,
892 struct nfs4_sequence_res *res,
893 int cache_reply)
894{
Chuck Levera9c92d62013-08-09 12:48:18 -0400895 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400896 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000897}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
900{
901 struct nfs_inode *nfsi = NFS_I(dir);
902
903 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400904 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700905 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700907 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000908 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 spin_unlock(&dir->i_lock);
910}
911
912struct nfs4_opendata {
913 struct kref kref;
914 struct nfs_openargs o_arg;
915 struct nfs_openres o_res;
916 struct nfs_open_confirmargs c_arg;
917 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500918 struct nfs4_string owner_name;
919 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400921 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400923 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct nfs4_state_owner *owner;
925 struct nfs4_state *state;
926 struct iattr attrs;
927 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400928 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400929 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400930 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400931 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int cancelled;
933};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400934
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400935static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
936 int err, struct nfs4_exception *exception)
937{
938 if (err != -EINVAL)
939 return false;
940 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
941 return false;
942 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
943 exception->retry = 1;
944 return true;
945}
946
947static enum open_claim_type4
948nfs4_map_atomic_open_claim(struct nfs_server *server,
949 enum open_claim_type4 claim)
950{
951 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
952 return claim;
953 switch (claim) {
954 default:
955 return claim;
956 case NFS4_OPEN_CLAIM_FH:
957 return NFS4_OPEN_CLAIM_NULL;
958 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
959 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
960 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
961 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
962 }
963}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100966{
967 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400968 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969 p->o_res.seqid = p->o_arg.seqid;
970 p->c_res.seqid = p->c_arg.seqid;
971 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400972 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100973 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500974 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100975}
976
Al Viro82a2c1b2011-06-22 18:30:55 -0400977static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100978 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400979 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400980 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400981 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400982 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100983{
Al Viro82a2c1b2011-06-22 18:30:55 -0400984 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100985 struct inode *dir = parent->d_inode;
986 struct nfs_server *server = NFS_SERVER(dir);
987 struct nfs4_opendata *p;
988
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400989 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100990 if (p == NULL)
991 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400992
993 p->f_label = nfs4_label_alloc(server, gfp_mask);
994 if (IS_ERR(p->f_label))
995 goto err_free_p;
996
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400997 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100998 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400999 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001000 nfs_sb_active(dentry->d_sb);
1001 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001002 p->dir = parent;
1003 p->owner = sp;
1004 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 p->o_arg.open_flags = flags;
1006 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001007 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1008 * will return permission denied for all bits until close */
1009 if (!(flags & O_EXCL)) {
1010 /* ask server to check for all possible rights as results
1011 * are cached */
1012 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1013 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001014 }
David Howells7539bba2006-08-22 20:06:09 -04001015 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001016 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1017 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001018 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001019 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001020 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001021 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001022 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001023 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1024 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001025 case NFS4_OPEN_CLAIM_NULL:
1026 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1027 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1028 p->o_arg.fh = NFS_FH(dir);
1029 break;
1030 case NFS4_OPEN_CLAIM_PREVIOUS:
1031 case NFS4_OPEN_CLAIM_FH:
1032 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1033 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1034 p->o_arg.fh = NFS_FH(dentry->d_inode);
1035 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001036 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001037 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001038
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001039 p->o_arg.u.attrs = &p->attrs;
1040 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001041
1042 verf[0] = jiffies;
1043 verf[1] = current->pid;
1044 memcpy(p->o_arg.u.verifier.data, verf,
1045 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001046 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001047 p->c_arg.fh = &p->o_res.fh;
1048 p->c_arg.stateid = &p->o_res.stateid;
1049 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001050 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001051 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001053
1054err_free_label:
1055 nfs4_label_free(p->f_label);
1056err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001057 kfree(p);
1058err:
1059 dput(parent);
1060 return NULL;
1061}
1062
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001063static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001064{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001065 struct nfs4_opendata *p = container_of(kref,
1066 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001067 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001068
1069 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001070 if (p->state != NULL)
1071 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001072 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001073
1074 nfs4_label_free(p->f_label);
1075
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001076 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001077 dput(p->dentry);
1078 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001079 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001080 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001081 kfree(p);
1082}
1083
1084static void nfs4_opendata_put(struct nfs4_opendata *p)
1085{
1086 if (p != NULL)
1087 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001088}
1089
Trond Myklebust06f814a2006-01-03 09:55:07 +01001090static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1091{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001092 int ret;
1093
Trond Myklebust06f814a2006-01-03 09:55:07 +01001094 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001095 return ret;
1096}
1097
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001098static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099{
1100 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001101
Trond Myklebust536e43d2012-01-17 22:04:26 -05001102 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103 goto out;
1104 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001105 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001106 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1107 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001108 break;
1109 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001110 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1111 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001112 break;
1113 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001114 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1115 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001116 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001117out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001118 return ret;
1119}
1120
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001121static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001123 if (delegation == NULL)
1124 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001126 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001127 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001128 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001129 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1130 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001131 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001132 return 1;
1133}
1134
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001136{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001137 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001138 case FMODE_WRITE:
1139 state->n_wronly++;
1140 break;
1141 case FMODE_READ:
1142 state->n_rdonly++;
1143 break;
1144 case FMODE_READ|FMODE_WRITE:
1145 state->n_rdwr++;
1146 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001147 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001148}
1149
Trond Myklebust4f14c192014-02-12 19:15:06 -05001150static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001151{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001152 struct nfs_client *clp = state->owner->so_server->nfs_client;
1153 bool need_recover = false;
1154
1155 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1156 need_recover = true;
1157 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1158 need_recover = true;
1159 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1160 need_recover = true;
1161 if (need_recover)
1162 nfs4_state_mark_reclaim_nograce(clp, state);
1163}
1164
Trond Myklebuste999e802014-02-10 18:20:47 -05001165static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1166 nfs4_stateid *stateid)
1167{
1168 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1169 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001170 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1171 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001172 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001173 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001174 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1175 return true;
1176 return false;
1177}
1178
Trond Myklebust226056c2014-02-11 10:41:07 -05001179static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1180 nfs4_stateid *stateid, fmode_t fmode)
1181{
1182 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1183 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1184 case FMODE_WRITE:
1185 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1186 break;
1187 case FMODE_READ:
1188 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1189 break;
1190 case 0:
1191 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1192 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1193 clear_bit(NFS_OPEN_STATE, &state->flags);
1194 }
1195 if (stateid == NULL)
1196 return;
1197 if (!nfs_need_update_open_stateid(state, stateid))
1198 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001199 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001200 nfs4_stateid_copy(&state->stateid, stateid);
1201 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001202}
1203
1204static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1205{
1206 write_seqlock(&state->seqlock);
1207 nfs_clear_open_stateid_locked(state, stateid, fmode);
1208 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001209 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1210 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001211}
1212
Trond Myklebust003707c2007-07-05 18:07:55 -04001213static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1214{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001216 case FMODE_READ:
1217 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1218 break;
1219 case FMODE_WRITE:
1220 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1221 break;
1222 case FMODE_READ|FMODE_WRITE:
1223 set_bit(NFS_O_RDWR_STATE, &state->flags);
1224 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001225 if (!nfs_need_update_open_stateid(state, stateid))
1226 return;
1227 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1228 nfs4_stateid_copy(&state->stateid, stateid);
1229 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001230}
1231
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001232static 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 -07001233{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001234 /*
1235 * Protect the call to nfs4_state_set_mode_locked and
1236 * serialise the stateid update
1237 */
1238 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001239 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001240 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001241 set_bit(NFS_DELEGATED_STATE, &state->flags);
1242 }
1243 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001244 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001245 write_sequnlock(&state->seqlock);
1246 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001248 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001251static 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 -05001252{
1253 struct nfs_inode *nfsi = NFS_I(state->inode);
1254 struct nfs_delegation *deleg_cur;
1255 int ret = 0;
1256
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001257 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001258
1259 rcu_read_lock();
1260 deleg_cur = rcu_dereference(nfsi->delegation);
1261 if (deleg_cur == NULL)
1262 goto no_delegation;
1263
1264 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001265 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001266 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001267 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001268 goto no_delegation_unlock;
1269
1270 if (delegation == NULL)
1271 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001272 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001273 goto no_delegation_unlock;
1274
Trond Myklebustb7391f42008-12-23 15:21:52 -05001275 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001276 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001277 ret = 1;
1278no_delegation_unlock:
1279 spin_unlock(&deleg_cur->lock);
1280no_delegation:
1281 rcu_read_unlock();
1282
1283 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001284 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001285 ret = 1;
1286 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001287 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1288 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001289
1290 return ret;
1291}
1292
1293
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001294static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001295{
1296 struct nfs_delegation *delegation;
1297
1298 rcu_read_lock();
1299 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001300 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001301 rcu_read_unlock();
1302 return;
1303 }
1304 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001305 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001306}
1307
Trond Myklebust6ee41262007-07-08 14:11:36 -04001308static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001309{
1310 struct nfs4_state *state = opendata->state;
1311 struct nfs_inode *nfsi = NFS_I(state->inode);
1312 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001313 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001314 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001315 nfs4_stateid stateid;
1316 int ret = -EAGAIN;
1317
Trond Myklebustaac00a82007-07-05 19:02:21 -04001318 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001319 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001320 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001321 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001322 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001323 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001324 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001325 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001326 rcu_read_lock();
1327 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001328 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001329 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001330 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001331 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001332 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001333 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001334 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001335 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001336 if (!opendata->is_recover) {
1337 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1338 if (ret != 0)
1339 goto out;
1340 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001341 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001342
1343 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001345 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001346 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001347out:
1348 return ERR_PTR(ret);
1349out_return_state:
1350 atomic_inc(&state->count);
1351 return state;
1352}
1353
Andy Adamsone23008e2012-10-02 21:07:32 -04001354static void
1355nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1356{
1357 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1358 struct nfs_delegation *delegation;
1359 int delegation_flags = 0;
1360
1361 rcu_read_lock();
1362 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1363 if (delegation)
1364 delegation_flags = delegation->flags;
1365 rcu_read_unlock();
1366 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1367 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1368 "returning a delegation for "
1369 "OPEN(CLAIM_DELEGATE_CUR)\n",
1370 clp->cl_hostname);
1371 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1372 nfs_inode_set_delegation(state->inode,
1373 data->owner->so_cred,
1374 &data->o_res);
1375 else
1376 nfs_inode_reclaim_delegation(state->inode,
1377 data->owner->so_cred,
1378 &data->o_res);
1379}
1380
1381/*
1382 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1383 * and update the nfs4_state.
1384 */
1385static struct nfs4_state *
1386_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1387{
1388 struct inode *inode = data->state->inode;
1389 struct nfs4_state *state = data->state;
1390 int ret;
1391
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001392 if (!data->rpc_done) {
1393 if (data->rpc_status) {
1394 ret = data->rpc_status;
1395 goto err;
1396 }
1397 /* cached opens have already been processed */
1398 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001399 }
1400
Andy Adamsone23008e2012-10-02 21:07:32 -04001401 ret = nfs_refresh_inode(inode, &data->f_attr);
1402 if (ret)
1403 goto err;
1404
1405 if (data->o_res.delegation_type != 0)
1406 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001407update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001408 update_open_stateid(state, &data->o_res.stateid, NULL,
1409 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001410 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001411
1412 return state;
1413err:
1414 return ERR_PTR(ret);
1415
1416}
1417
1418static struct nfs4_state *
1419_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001420{
1421 struct inode *inode;
1422 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001423 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001424
Trond Myklebustaac00a82007-07-05 19:02:21 -04001425 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001426 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001427 goto out;
1428 }
1429
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001430 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001432 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001433 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001434 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001435 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001436 goto err;
1437 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001438 state = nfs4_get_open_state(inode, data->owner);
1439 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001440 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001441 if (data->o_res.delegation_type != 0)
1442 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001443 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001444 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001446out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001447 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001449err_put_inode:
1450 iput(inode);
1451err:
1452 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001453}
1454
Andy Adamsone23008e2012-10-02 21:07:32 -04001455static struct nfs4_state *
1456nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1457{
1458 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1459 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1460 return _nfs4_opendata_to_nfs4_state(data);
1461}
1462
Trond Myklebust864472e2006-01-03 09:55:15 +01001463static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1464{
1465 struct nfs_inode *nfsi = NFS_I(state->inode);
1466 struct nfs_open_context *ctx;
1467
1468 spin_lock(&state->inode->i_lock);
1469 list_for_each_entry(ctx, &nfsi->open_files, list) {
1470 if (ctx->state != state)
1471 continue;
1472 get_nfs_open_context(ctx);
1473 spin_unlock(&state->inode->i_lock);
1474 return ctx;
1475 }
1476 spin_unlock(&state->inode->i_lock);
1477 return ERR_PTR(-ENOENT);
1478}
1479
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001480static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1481 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001482{
1483 struct nfs4_opendata *opendata;
1484
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001485 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001486 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001487 if (opendata == NULL)
1488 return ERR_PTR(-ENOMEM);
1489 opendata->state = state;
1490 atomic_inc(&state->count);
1491 return opendata;
1492}
1493
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001494static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001495{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001496 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001497 int ret;
1498
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001499 opendata->o_arg.open_flags = 0;
1500 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001501 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1502 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1503 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001504 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001505 if (ret != 0)
1506 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001507 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001508 if (IS_ERR(newstate))
1509 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001510 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001511 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001512 return 0;
1513}
1514
1515static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1516{
Trond Myklebust864472e2006-01-03 09:55:15 +01001517 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001518 int ret;
1519
Trond Myklebust4f14c192014-02-12 19:15:06 -05001520 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1521 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1522 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1523 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001524 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001525 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001526 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001527 smp_rmb();
1528 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001529 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001530 if (ret != 0)
1531 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001532 if (newstate != state)
1533 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001534 }
1535 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001536 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001537 if (ret != 0)
1538 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001539 if (newstate != state)
1540 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001541 }
1542 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001543 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001544 if (ret != 0)
1545 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001546 if (newstate != state)
1547 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001548 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001549 /*
1550 * We may have performed cached opens for all three recoveries.
1551 * Check if we need to update the current stateid.
1552 */
1553 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001554 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001555 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001556 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001557 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001558 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001559 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001560 return 0;
1561}
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563/*
1564 * OPEN_RECLAIM:
1565 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001567static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001569 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001570 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001571 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int status;
1573
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001574 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1575 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001576 if (IS_ERR(opendata))
1577 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001578 rcu_read_lock();
1579 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001580 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001581 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001582 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001583 opendata->o_arg.u.delegation_type = delegation_type;
1584 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001585 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return status;
1587}
1588
Trond Myklebust539cd032007-06-05 11:46:42 -04001589static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 struct nfs_server *server = NFS_SERVER(state->inode);
1592 struct nfs4_exception exception = { };
1593 int err;
1594 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001595 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001596 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001597 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1598 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001599 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001600 break;
1601 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 } while (exception.retry);
1603 return err;
1604}
1605
Trond Myklebust864472e2006-01-03 09:55:15 +01001606static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1607{
1608 struct nfs_open_context *ctx;
1609 int ret;
1610
1611 ctx = nfs4_state_find_open_context(state);
1612 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001613 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001614 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001615 put_nfs_open_context(ctx);
1616 return ret;
1617}
1618
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001619static 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 -07001620{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001621 switch (err) {
1622 default:
1623 printk(KERN_ERR "NFS: %s: unhandled error "
1624 "%d.\n", __func__, err);
1625 case 0:
1626 case -ENOENT:
1627 case -ESTALE:
1628 break;
1629 case -NFS4ERR_BADSESSION:
1630 case -NFS4ERR_BADSLOT:
1631 case -NFS4ERR_BAD_HIGH_SLOT:
1632 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1633 case -NFS4ERR_DEADSESSION:
1634 set_bit(NFS_DELEGATED_STATE, &state->flags);
1635 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1636 return -EAGAIN;
1637 case -NFS4ERR_STALE_CLIENTID:
1638 case -NFS4ERR_STALE_STATEID:
1639 set_bit(NFS_DELEGATED_STATE, &state->flags);
1640 case -NFS4ERR_EXPIRED:
1641 /* Don't recall a delegation if it was lost */
1642 nfs4_schedule_lease_recovery(server->nfs_client);
1643 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001644 case -NFS4ERR_MOVED:
1645 nfs4_schedule_migration_recovery(server);
1646 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001647 case -NFS4ERR_LEASE_MOVED:
1648 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1649 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001650 case -NFS4ERR_DELEG_REVOKED:
1651 case -NFS4ERR_ADMIN_REVOKED:
1652 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001653 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001654 nfs_inode_find_state_and_recover(state->inode,
1655 stateid);
1656 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001657 return 0;
1658 case -NFS4ERR_DELAY:
1659 case -NFS4ERR_GRACE:
1660 set_bit(NFS_DELEGATED_STATE, &state->flags);
1661 ssleep(1);
1662 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001663 case -ENOMEM:
1664 case -NFS4ERR_DENIED:
1665 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1666 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return err;
1669}
1670
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001671int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1672{
1673 struct nfs_server *server = NFS_SERVER(state->inode);
1674 struct nfs4_opendata *opendata;
1675 int err;
1676
1677 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1678 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1679 if (IS_ERR(opendata))
1680 return PTR_ERR(opendata);
1681 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1682 err = nfs4_open_recover(opendata, state);
1683 nfs4_opendata_put(opendata);
1684 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1685}
1686
Chuck Leverbe05c862013-08-09 12:49:47 -04001687static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1688{
1689 struct nfs4_opendata *data = calldata;
1690
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001691 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1692 &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001693}
1694
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001695static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1696{
1697 struct nfs4_opendata *data = calldata;
1698
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001699 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001700
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001701 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001702 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001703 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001704 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001705 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001706 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001707 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001708}
1709
1710static void nfs4_open_confirm_release(void *calldata)
1711{
1712 struct nfs4_opendata *data = calldata;
1713 struct nfs4_state *state = NULL;
1714
1715 /* If this request hasn't been cancelled, do nothing */
1716 if (data->cancelled == 0)
1717 goto out_free;
1718 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001719 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001720 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001721 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001722 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001723 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001724out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001725 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001726}
1727
1728static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001729 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001730 .rpc_call_done = nfs4_open_confirm_done,
1731 .rpc_release = nfs4_open_confirm_release,
1732};
1733
1734/*
1735 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1736 */
1737static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1738{
1739 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1740 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001741 struct rpc_message msg = {
1742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1743 .rpc_argp = &data->c_arg,
1744 .rpc_resp = &data->c_res,
1745 .rpc_cred = data->owner->so_cred,
1746 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001747 struct rpc_task_setup task_setup_data = {
1748 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001749 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001750 .callback_ops = &nfs4_open_confirm_ops,
1751 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001752 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001753 .flags = RPC_TASK_ASYNC,
1754 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 int status;
1756
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001757 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001758 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001759 data->rpc_done = 0;
1760 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001761 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001762 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001763 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001764 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001765 status = nfs4_wait_for_completion_rpc_task(task);
1766 if (status != 0) {
1767 data->cancelled = 1;
1768 smp_wmb();
1769 } else
1770 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001771 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return status;
1773}
1774
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001775static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001777 struct nfs4_opendata *data = calldata;
1778 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001779 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001780
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001782 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001783 /*
1784 * Check if we still need to send an OPEN call, or if we can use
1785 * a delegation instead.
1786 */
1787 if (data->state != NULL) {
1788 struct nfs_delegation *delegation;
1789
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001790 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001791 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001792 rcu_read_lock();
1793 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001794 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001795 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001796 can_open_delegated(delegation, data->o_arg.fmode))
1797 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001798 rcu_read_unlock();
1799 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001800 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001801 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001802 switch (data->o_arg.claim) {
1803 case NFS4_OPEN_CLAIM_PREVIOUS:
1804 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1805 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001806 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001807 case NFS4_OPEN_CLAIM_FH:
1808 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001809 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1810 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001811 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001812 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001813 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001814 &data->o_res.seq_res,
1815 task) != 0)
1816 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001817
1818 /* Set the create mode (note dependency on the session type) */
1819 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1820 if (data->o_arg.open_flags & O_EXCL) {
1821 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1822 if (nfs4_has_persistent_session(clp))
1823 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1824 else if (clp->cl_mvops->minor_version > 0)
1825 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1826 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001827 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001828unlock_no_action:
1829 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001830out_no_action:
1831 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001832out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001833 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001834}
1835
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001836static void nfs4_open_done(struct rpc_task *task, void *calldata)
1837{
1838 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001840 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001841
Trond Myklebust14516c32010-07-31 14:29:06 -04001842 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1843 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001844
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001846 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1847 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001848 case S_IFREG:
1849 break;
1850 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001851 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001852 break;
1853 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001854 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001855 break;
1856 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001857 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001858 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001859 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001860 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001861 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1862 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001863 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001864 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001865}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001866
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001867static void nfs4_open_release(void *calldata)
1868{
1869 struct nfs4_opendata *data = calldata;
1870 struct nfs4_state *state = NULL;
1871
1872 /* If this request hasn't been cancelled, do nothing */
1873 if (data->cancelled == 0)
1874 goto out_free;
1875 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001876 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001877 goto out_free;
1878 /* In case we need an open_confirm, no cleanup! */
1879 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1880 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001881 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001882 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001883 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001884out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001885 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001886}
1887
1888static const struct rpc_call_ops nfs4_open_ops = {
1889 .rpc_call_prepare = nfs4_open_prepare,
1890 .rpc_call_done = nfs4_open_done,
1891 .rpc_release = nfs4_open_release,
1892};
1893
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001894static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001895{
1896 struct inode *dir = data->dir->d_inode;
1897 struct nfs_server *server = NFS_SERVER(dir);
1898 struct nfs_openargs *o_arg = &data->o_arg;
1899 struct nfs_openres *o_res = &data->o_res;
1900 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001901 struct rpc_message msg = {
1902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1903 .rpc_argp = o_arg,
1904 .rpc_resp = o_res,
1905 .rpc_cred = data->owner->so_cred,
1906 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001907 struct rpc_task_setup task_setup_data = {
1908 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001909 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001910 .callback_ops = &nfs4_open_ops,
1911 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001912 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001913 .flags = RPC_TASK_ASYNC,
1914 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001915 int status;
1916
Chuck Levera9c92d62013-08-09 12:48:18 -04001917 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001918 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001919 data->rpc_done = 0;
1920 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001921 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001922 data->is_recover = 0;
1923 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001924 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001925 data->is_recover = 1;
1926 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001927 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001928 if (IS_ERR(task))
1929 return PTR_ERR(task);
1930 status = nfs4_wait_for_completion_rpc_task(task);
1931 if (status != 0) {
1932 data->cancelled = 1;
1933 smp_wmb();
1934 } else
1935 status = data->rpc_status;
1936 rpc_put_task(task);
1937
1938 return status;
1939}
1940
1941static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1942{
1943 struct inode *dir = data->dir->d_inode;
1944 struct nfs_openres *o_res = &data->o_res;
1945 int status;
1946
1947 status = nfs4_run_open_task(data, 1);
1948 if (status != 0 || !data->rpc_done)
1949 return status;
1950
Trond Myklebust6926afd2012-01-07 13:22:46 -05001951 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1952
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001953 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1954 status = _nfs4_proc_open_confirm(data);
1955 if (status != 0)
1956 return status;
1957 }
1958
1959 return status;
1960}
1961
Trond Myklebustf3792d62014-07-10 08:54:32 -04001962/*
1963 * Additional permission checks in order to distinguish between an
1964 * open for read, and an open for execute. This works around the
1965 * fact that NFSv4 OPEN treats read and execute permissions as being
1966 * the same.
1967 * Note that in the non-execute case, we want to turn off permission
1968 * checking if we just created a new file (POSIX open() semantics).
1969 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001970static int nfs4_opendata_access(struct rpc_cred *cred,
1971 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001972 struct nfs4_state *state, fmode_t fmode,
1973 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001974{
1975 struct nfs_access_entry cache;
1976 u32 mask;
1977
1978 /* access call failed or for some reason the server doesn't
1979 * support any access modes -- defer access call until later */
1980 if (opendata->o_res.access_supported == 0)
1981 return 0;
1982
1983 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04001984 /*
1985 * Use openflags to check for exec, because fmode won't
1986 * always have FMODE_EXEC set when file open for exec.
1987 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001988 if (openflags & __FMODE_EXEC) {
1989 /* ONLY check for exec rights */
1990 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04001991 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001992 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001993
1994 cache.cred = cred;
1995 cache.jiffies = jiffies;
1996 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1997 nfs_access_add_cache(state->inode, &cache);
1998
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001999 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002000 return 0;
2001
2002 /* even though OPEN succeeded, access is denied. Close the file */
2003 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002004 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002005}
2006
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002007/*
2008 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2009 */
2010static int _nfs4_proc_open(struct nfs4_opendata *data)
2011{
2012 struct inode *dir = data->dir->d_inode;
2013 struct nfs_server *server = NFS_SERVER(dir);
2014 struct nfs_openargs *o_arg = &data->o_arg;
2015 struct nfs_openres *o_res = &data->o_res;
2016 int status;
2017
2018 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002019 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002020 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002021 if (status != 0) {
2022 if (status == -NFS4ERR_BADNAME &&
2023 !(o_arg->open_flags & O_CREAT))
2024 return -ENOENT;
2025 return status;
2026 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002027
Trond Myklebust6926afd2012-01-07 13:22:46 -05002028 nfs_fattr_map_and_free_names(server, &data->f_attr);
2029
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002030 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002031 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002032 if (o_arg->open_flags & O_EXCL)
2033 data->file_created = 1;
2034 else if (o_res->cinfo.before != o_res->cinfo.after)
2035 data->file_created = 1;
2036 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002037 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2038 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002040 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002042 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002045 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Andy Adamsond83217c2011-03-01 01:34:17 +00002049static int nfs4_recover_expired_lease(struct nfs_server *server)
2050{
2051 return nfs4_client_recover_expired_lease(server->nfs_client);
2052}
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054/*
2055 * OPEN_EXPIRED:
2056 * reclaim state on the server after a network partition.
2057 * Assumes caller holds the appropriate lock
2058 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002059static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002061 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002062 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002064 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002065 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002066 if (IS_ERR(opendata))
2067 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002068 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002069 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002070 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002071 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002072 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002075static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002076{
Trond Myklebust539cd032007-06-05 11:46:42 -04002077 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002078 struct nfs4_exception exception = { };
2079 int err;
2080
2081 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002082 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002083 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002084 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2085 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002086 switch (err) {
2087 default:
2088 goto out;
2089 case -NFS4ERR_GRACE:
2090 case -NFS4ERR_DELAY:
2091 nfs4_handle_exception(server, err, &exception);
2092 err = 0;
2093 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002094 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002095out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002096 return err;
2097}
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002102 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Trond Myklebust864472e2006-01-03 09:55:15 +01002104 ctx = nfs4_state_find_open_context(state);
2105 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002106 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002107 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002108 put_nfs_open_context(ctx);
2109 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002112#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002113static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002114{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002115 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002116 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002117 struct nfs_delegation *delegation;
2118 struct rpc_cred *cred = NULL;
2119 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002120
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002121 /* If a state reset has been done, test_stateid is unneeded */
2122 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2123 return;
2124
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002125 /* Get the delegation credential for use by test/free_stateid */
2126 rcu_read_lock();
2127 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2128 if (delegation != NULL &&
2129 nfs4_stateid_match(&delegation->stateid, stateid)) {
2130 cred = get_rpccred(delegation->cred);
2131 rcu_read_unlock();
2132 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002133 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002134 } else
2135 rcu_read_unlock();
2136
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002137 if (status != NFS_OK) {
2138 /* Free the stateid unless the server explicitly
2139 * informs us the stateid is unrecognized. */
2140 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002141 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002142 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002143
Bryan Schumaker69388672012-09-26 15:25:52 -04002144 write_seqlock(&state->seqlock);
2145 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2146 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002147 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2148 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002149
2150 if (cred != NULL)
2151 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002152}
2153
2154/**
2155 * nfs41_check_open_stateid - possibly free an open stateid
2156 *
2157 * @state: NFSv4 state for an inode
2158 *
2159 * Returns NFS_OK if recovery for this stateid is now finished.
2160 * Otherwise a negative NFS4ERR value is returned.
2161 */
2162static int nfs41_check_open_stateid(struct nfs4_state *state)
2163{
2164 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002165 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002166 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002167 int status;
2168
2169 /* If a state reset has been done, test_stateid is unneeded */
2170 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2171 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2172 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2173 return -NFS4ERR_BAD_STATEID;
2174
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002175 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002176 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002177 if (status != NFS_OK) {
2178 /* Free the stateid unless the server explicitly
2179 * informs us the stateid is unrecognized. */
2180 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002181 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002182
2183 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2184 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2185 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002186 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002187 }
2188 return status;
2189}
2190
2191static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2192{
Chuck Levereb64cf92012-07-11 16:30:05 -04002193 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002194
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002195 nfs41_clear_delegation_stateid(state);
2196 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002197 if (status != NFS_OK)
2198 status = nfs4_open_expired(sp, state);
2199 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002200}
2201#endif
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002204 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2205 * fields corresponding to attributes that were used to store the verifier.
2206 * Make sure we clobber those fields in the later setattr call
2207 */
2208static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2209{
2210 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2211 !(sattr->ia_valid & ATTR_ATIME_SET))
2212 sattr->ia_valid |= ATTR_ATIME;
2213
2214 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2215 !(sattr->ia_valid & ATTR_MTIME_SET))
2216 sattr->ia_valid |= ATTR_MTIME;
2217}
2218
Trond Myklebustc21443c2013-02-07 14:26:21 -05002219static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2220 fmode_t fmode,
2221 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002222 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002223{
2224 struct nfs4_state_owner *sp = opendata->owner;
2225 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002226 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002227 struct nfs4_state *state;
2228 unsigned int seq;
2229 int ret;
2230
2231 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2232
2233 ret = _nfs4_proc_open(opendata);
NeilBrown4fa2c542014-07-21 13:28:28 +10002234 if (ret != 0) {
2235 if (ret == -ENOENT) {
NeilBrownf39c0102014-09-11 16:19:37 +10002236 dentry = opendata->dentry;
2237 if (dentry->d_inode)
2238 d_delete(dentry);
2239 else if (d_unhashed(dentry))
2240 d_add(dentry, NULL);
2241
2242 nfs_set_verifier(dentry,
NeilBrown4fa2c542014-07-21 13:28:28 +10002243 nfs_save_change_attribute(opendata->dir->d_inode));
2244 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002245 goto out;
NeilBrown4fa2c542014-07-21 13:28:28 +10002246 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002247
2248 state = nfs4_opendata_to_nfs4_state(opendata);
2249 ret = PTR_ERR(state);
2250 if (IS_ERR(state))
2251 goto out;
2252 if (server->caps & NFS_CAP_POSIX_LOCK)
2253 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2254
Trond Myklebust275bb302013-05-29 13:11:28 -04002255 dentry = opendata->dentry;
2256 if (dentry->d_inode == NULL) {
2257 /* FIXME: Is this d_drop() ever needed? */
2258 d_drop(dentry);
2259 dentry = d_add_unique(dentry, igrab(state->inode));
2260 if (dentry == NULL) {
2261 dentry = opendata->dentry;
2262 } else if (dentry != ctx->dentry) {
2263 dput(ctx->dentry);
2264 ctx->dentry = dget(dentry);
2265 }
2266 nfs_set_verifier(dentry,
2267 nfs_save_change_attribute(opendata->dir->d_inode));
2268 }
2269
Trond Myklebustc21443c2013-02-07 14:26:21 -05002270 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2271 if (ret != 0)
2272 goto out;
2273
Trond Myklebust3efb9722013-05-29 13:17:04 -04002274 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002275 if (dentry->d_inode == state->inode) {
2276 nfs_inode_attach_open_context(ctx);
2277 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2278 nfs4_schedule_stateid_recovery(server, state);
2279 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002280out:
2281 return ret;
2282}
2283
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002284/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002285 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 */
Andy Adamson82be4172012-05-23 05:02:35 -04002287static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002288 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002289 int flags,
2290 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002291 struct nfs4_label *label,
2292 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 struct nfs4_state_owner *sp;
2295 struct nfs4_state *state = NULL;
2296 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002297 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002298 struct dentry *dentry = ctx->dentry;
2299 struct rpc_cred *cred = ctx->cred;
2300 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2301 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002302 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002303 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002304 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002308 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2309 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2311 goto out_err;
2312 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002313 status = nfs4_recover_expired_lease(server);
2314 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002315 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002316 if (dentry->d_inode != NULL)
2317 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002318 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002319 if (dentry->d_inode)
2320 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002321 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002322 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002323 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002324 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
David Quigley14c43f72013-05-22 12:50:43 -04002326 if (label) {
2327 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2328 if (IS_ERR(olabel)) {
2329 status = PTR_ERR(olabel);
2330 goto err_opendata_put;
2331 }
2332 }
2333
Trond Myklebuste911b812014-03-26 13:24:37 -07002334 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2335 if (!opendata->f_attr.mdsthreshold) {
2336 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2337 if (!opendata->f_attr.mdsthreshold)
2338 goto err_free_label;
2339 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002340 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002341 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002342 if (dentry->d_inode != NULL)
2343 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002344
Trond Myklebust3efb9722013-05-29 13:17:04 -04002345 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002346 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002347 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002348 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002349
Trond Myklebust549b19c2013-04-16 18:42:34 -04002350 if ((opendata->o_arg.open_flags & O_EXCL) &&
2351 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002352 nfs4_exclusive_attrset(opendata, sattr);
2353
2354 nfs_fattr_init(opendata->o_res.f_attr);
2355 status = nfs4_do_setattr(state->inode, cred,
2356 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002357 state, label, olabel);
2358 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002359 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002360 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002361 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002362 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002363 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002364 if (opendata->file_created)
2365 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002366
Trond Myklebuste911b812014-03-26 13:24:37 -07002367 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002368 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002369 opendata->f_attr.mdsthreshold = NULL;
2370 }
Andy Adamson82be4172012-05-23 05:02:35 -04002371
David Quigley14c43f72013-05-22 12:50:43 -04002372 nfs4_label_free(olabel);
2373
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002374 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002377err_free_label:
2378 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002379err_opendata_put:
2380 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002381err_put_state_owner:
2382 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return status;
2385}
2386
2387
Andy Adamson82be4172012-05-23 05:02:35 -04002388static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002389 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002390 int flags,
2391 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002392 struct nfs4_label *label,
2393 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002395 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 struct nfs4_exception exception = { };
2397 struct nfs4_state *res;
2398 int status;
2399
2400 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002401 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002402 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002403 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (status == 0)
2405 break;
2406 /* NOTE: BAD_SEQID means the server and client disagree about the
2407 * book-keeping w.r.t. state-changing operations
2408 * (OPEN/CLOSE/LOCK/LOCKU...)
2409 * It is actually a sign of a bug on the client or on the server.
2410 *
2411 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002412 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 * have unhashed the old state_owner for us, and that we can
2414 * therefore safely retry using a new one. We should still warn
2415 * the user though...
2416 */
2417 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002418 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002419 " returned a bad sequence-id error!\n",
2420 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 exception.retry = 1;
2422 continue;
2423 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002424 /*
2425 * BAD_STATEID on OPEN means that the server cancelled our
2426 * state before it received the OPEN_CONFIRM.
2427 * Recover by retrying the request as per the discussion
2428 * on Page 181 of RFC3530.
2429 */
2430 if (status == -NFS4ERR_BAD_STATEID) {
2431 exception.retry = 1;
2432 continue;
2433 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002434 if (status == -EAGAIN) {
2435 /* We must have found a delegation */
2436 exception.retry = 1;
2437 continue;
2438 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002439 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2440 continue;
2441 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 status, &exception));
2443 } while (exception.retry);
2444 return res;
2445}
2446
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002447static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2448 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002449 struct nfs4_state *state, struct nfs4_label *ilabel,
2450 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002452 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002454 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 .iap = sattr,
2456 .server = server,
2457 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002458 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 };
2460 struct nfs_setattrres res = {
2461 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002462 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 .server = server,
2464 };
2465 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2467 .rpc_argp = &arg,
2468 .rpc_resp = &res,
2469 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002471 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002472 fmode_t fmode;
2473 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002474 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
David Quigleyaa9c2662013-05-22 12:50:44 -04002476 arg.bitmask = nfs4_bitmask(server, ilabel);
2477 if (ilabel)
2478 arg.bitmask = nfs4_bitmask(server, olabel);
2479
Trond Myklebust0e574af2005-10-27 22:12:38 -04002480 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Trond Myklebustee3ae842013-04-29 10:35:36 -04002482 /* Servers should only apply open mode checks for file size changes */
2483 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2484 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2485
2486 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2487 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002488 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002489 struct nfs_lockowner lockowner = {
2490 .l_owner = current->files,
2491 .l_pid = current->tgid,
2492 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002493 if (!nfs4_valid_open_stateid(state))
2494 return -EBADF;
2495 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2496 &lockowner) == -EIO)
2497 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002498 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002499 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Bryan Schumaker7c513052011-03-24 17:12:24 +00002501 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002502 if (status == 0 && state != NULL)
2503 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002504 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002507static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2508 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002509 struct nfs4_state *state, struct nfs4_label *ilabel,
2510 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002512 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002513 struct nfs4_exception exception = {
2514 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002515 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002516 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 int err;
2518 do {
David Quigley1775fd32013-05-22 12:50:42 -04002519 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002520 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002521 switch (err) {
2522 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002523 if (!(sattr->ia_valid & ATTR_SIZE)) {
2524 pr_warn_once("NFSv4: server %s is incorrectly "
2525 "applying open mode checks to "
2526 "a SETATTR that is not "
2527 "changing file size.\n",
2528 server->nfs_client->cl_hostname);
2529 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002530 if (state && !(state->state & FMODE_WRITE)) {
2531 err = -EBADF;
2532 if (sattr->ia_valid & ATTR_OPEN)
2533 err = -EACCES;
2534 goto out;
2535 }
2536 }
2537 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002539out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return err;
2541}
2542
2543struct nfs4_closedata {
2544 struct inode *inode;
2545 struct nfs4_state *state;
2546 struct nfs_closeargs arg;
2547 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002548 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002549 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002550 bool roc;
2551 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552};
2553
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002554static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002555{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002556 struct nfs4_closedata *calldata = data;
2557 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002558 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002559
Fred Isamanf7e89172011-01-06 11:36:32 +00002560 if (calldata->roc)
2561 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002562 nfs4_put_open_state(calldata->state);
2563 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002564 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002565 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002566 kfree(calldata);
2567}
2568
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002569static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002571 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002574 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Chuck Levera3ca5652012-03-01 17:00:40 -05002576 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002577 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2578 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002579 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 /* hmm. we are done with the inode, and in the process of freeing
2581 * the state_owner. we keep this around to process errors
2582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 switch (task->tk_status) {
2584 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002585 res_stateid = &calldata->res.stateid;
2586 if (calldata->arg.fmode == 0 && calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002587 pnfs_roc_set_barrier(state->inode,
2588 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002589 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002590 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002591 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002593 case -NFS4ERR_OLD_STATEID:
2594 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002596 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002599 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002600 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002601 goto out_release;
2602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
Trond Myklebust412f6c42014-08-25 22:09:08 -04002604 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002605out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002606 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002607 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002608 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002611static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002613 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002614 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002615 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002616 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002617 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002618
Chuck Levera3ca5652012-03-01 17:00:40 -05002619 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002620 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002621 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002622
Trond Myklebust88069f72009-12-08 08:33:16 -05002623 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002624 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002625 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2626 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2627 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04002628 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002629 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002630 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002631 if (state->n_rdonly == 0)
2632 call_close |= is_rdonly;
2633 else if (is_rdonly)
2634 calldata->arg.fmode |= FMODE_READ;
2635 if (state->n_wronly == 0)
2636 call_close |= is_wronly;
2637 else if (is_wronly)
2638 calldata->arg.fmode |= FMODE_WRITE;
2639 } else if (is_rdwr)
2640 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2641
2642 if (calldata->arg.fmode == 0)
2643 call_close |= is_rdwr;
2644
Trond Myklebust5d422302013-03-14 16:57:48 -04002645 if (!nfs4_valid_open_stateid(state))
2646 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002647 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002648
2649 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002650 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002651 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002652 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002653
Fred Isamanf7e89172011-01-06 11:36:32 +00002654 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002655 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002656 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002657 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2658 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002659 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002660 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002661 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002662
Trond Myklebust516a6af2005-10-27 22:12:41 -04002663 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002664 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002665 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002666 &calldata->arg.seq_args,
2667 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002668 task) != 0)
2669 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002670 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002671 return;
2672out_no_action:
2673 task->tk_action = NULL;
2674out_wait:
2675 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002678static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002679 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002680 .rpc_call_done = nfs4_close_done,
2681 .rpc_release = nfs4_free_closedata,
2682};
2683
Peng Taofe08c542014-07-03 13:05:01 +08002684static bool nfs4_state_has_opener(struct nfs4_state *state)
2685{
2686 /* first check existing openers */
2687 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 &&
2688 state->n_rdonly != 0)
2689 return true;
2690
2691 if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 &&
2692 state->n_wronly != 0)
2693 return true;
2694
2695 if (test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 &&
2696 state->n_rdwr != 0)
2697 return true;
2698
2699 return false;
2700}
2701
2702static bool nfs4_roc(struct inode *inode)
2703{
2704 struct nfs_inode *nfsi = NFS_I(inode);
2705 struct nfs_open_context *ctx;
2706 struct nfs4_state *state;
2707
2708 spin_lock(&inode->i_lock);
2709 list_for_each_entry(ctx, &nfsi->open_files, list) {
2710 state = ctx->state;
2711 if (state == NULL)
2712 continue;
2713 if (nfs4_state_has_opener(state)) {
2714 spin_unlock(&inode->i_lock);
2715 return false;
2716 }
2717 }
2718 spin_unlock(&inode->i_lock);
2719
2720 if (nfs4_check_delegation(inode, FMODE_READ))
2721 return false;
2722
2723 return pnfs_roc(inode);
2724}
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726/*
2727 * It is possible for data to be read/written from a mem-mapped file
2728 * after the sys_close call (which hits the vfs layer as a flush).
2729 * This means that we can't safely call nfsv4 close on a file until
2730 * the inode is cleared. This in turn means that we are not good
2731 * NFSv4 citizens - we do not indicate to the server to update the file's
2732 * share state even when we are done with one of the three share
2733 * stateid's in the inode.
2734 *
2735 * NOTE: Caller must be holding the sp->so_owner semaphore!
2736 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002737int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002739 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002741 struct nfs4_state_owner *sp = state->owner;
2742 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002743 struct rpc_message msg = {
2744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2745 .rpc_cred = state->owner->so_cred,
2746 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002747 struct rpc_task_setup task_setup_data = {
2748 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002749 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002750 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002751 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002752 .flags = RPC_TASK_ASYNC,
2753 };
Trond Myklebust95121352005-10-18 14:20:12 -07002754 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002756 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2757 &task_setup_data.rpc_client, &msg);
2758
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002759 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002761 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002762 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002763 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002765 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002766 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002768 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002769 if (calldata->arg.seqid == NULL)
2770 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002771 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002772 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002773 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002774 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002775 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002776 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002777 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002778
Trond Myklebust1174dd12010-12-21 10:52:24 -05002779 msg.rpc_argp = &calldata->arg;
2780 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002781 task_setup_data.callback_data = calldata;
2782 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002783 if (IS_ERR(task))
2784 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002785 status = 0;
2786 if (wait)
2787 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002788 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002789 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002790out_free_calldata:
2791 kfree(calldata);
2792out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002793 nfs4_put_open_state(state);
2794 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002795 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
Trond Myklebust2b484292010-09-17 10:56:51 -04002798static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002799nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2800 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002803 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2804
2805 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Trond Myklebust565277f2007-10-15 18:17:53 -04002807 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002808 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002809
2810 nfs4_label_release_security(label);
2811
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002812 if (IS_ERR(state))
2813 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002814 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815}
2816
Trond Myklebust1185a552009-12-03 15:54:02 -05002817static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002818{
2819 if (ctx->state == NULL)
2820 return;
2821 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002822 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002823 else
Al Viro643168c2011-06-22 18:20:23 -04002824 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002825}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Trond Myklebustb944dba2013-11-04 15:20:20 -05002827#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2828#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002829#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2832{
Benny Halevy43652ad2009-04-01 09:21:54 -04002833 struct nfs4_server_caps_arg args = {
2834 .fhandle = fhandle,
2835 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 struct nfs4_server_caps_res res = {};
2837 struct rpc_message msg = {
2838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002839 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 .rpc_resp = &res,
2841 };
2842 int status;
2843
Bryan Schumaker7c513052011-03-24 17:12:24 +00002844 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002846 /* Sanity check the server answers */
2847 switch (server->nfs_client->cl_minorversion) {
2848 case 0:
2849 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2850 res.attr_bitmask[2] = 0;
2851 break;
2852 case 1:
2853 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2854 break;
2855 case 2:
2856 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002859 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2860 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2861 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2862 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002863 NFS_CAP_CTIME|NFS_CAP_MTIME|
2864 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002865 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2866 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 server->caps |= NFS_CAP_ACLS;
2868 if (res.has_links != 0)
2869 server->caps |= NFS_CAP_HARDLINKS;
2870 if (res.has_symlinks != 0)
2871 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002872 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2873 server->caps |= NFS_CAP_FILEID;
2874 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2875 server->caps |= NFS_CAP_MODE;
2876 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2877 server->caps |= NFS_CAP_NLINK;
2878 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2879 server->caps |= NFS_CAP_OWNER;
2880 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2881 server->caps |= NFS_CAP_OWNER_GROUP;
2882 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2883 server->caps |= NFS_CAP_ATIME;
2884 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2885 server->caps |= NFS_CAP_CTIME;
2886 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2887 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002888#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2889 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2890 server->caps |= NFS_CAP_SECURITY_LABEL;
2891#endif
2892 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2893 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002894 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002895
Trond Myklebusta65318b2009-03-11 14:10:28 -04002896 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2897 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2898 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002899 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002901 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return status;
2905}
2906
Trond Myklebust55a97592006-06-09 09:34:19 -04002907int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
2909 struct nfs4_exception exception = { };
2910 int err;
2911 do {
2912 err = nfs4_handle_exception(server,
2913 _nfs4_server_capabilities(server, fhandle),
2914 &exception);
2915 } while (exception.retry);
2916 return err;
2917}
2918
2919static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2920 struct nfs_fsinfo *info)
2921{
David Quigleyaa9c2662013-05-22 12:50:44 -04002922 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002924 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 };
2926 struct nfs4_lookup_res res = {
2927 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002928 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 .fh = fhandle,
2930 };
2931 struct rpc_message msg = {
2932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2933 .rpc_argp = &args,
2934 .rpc_resp = &res,
2935 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002936
David Quigleyaa9c2662013-05-22 12:50:44 -04002937 bitmask[0] = nfs4_fattr_bitmap[0];
2938 bitmask[1] = nfs4_fattr_bitmap[1];
2939 /*
2940 * Process the label in the upcoming getfattr
2941 */
2942 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2943
Trond Myklebust0e574af2005-10-27 22:12:38 -04002944 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002945 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
2948static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2949 struct nfs_fsinfo *info)
2950{
2951 struct nfs4_exception exception = { };
2952 int err;
2953 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002954 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002955 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002956 switch (err) {
2957 case 0:
2958 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002959 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002960 default:
2961 err = nfs4_handle_exception(server, err, &exception);
2962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002964out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 return err;
2966}
2967
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002968static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2969 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2970{
Trond Myklebustc2190662013-08-26 19:23:04 -04002971 struct rpc_auth_create_args auth_args = {
2972 .pseudoflavor = flavor,
2973 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002974 struct rpc_auth *auth;
2975 int ret;
2976
Trond Myklebustc2190662013-08-26 19:23:04 -04002977 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002978 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002979 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002980 goto out;
2981 }
2982 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002983out:
2984 return ret;
2985}
2986
Chuck Lever9a744ba2013-03-16 15:56:02 -04002987/*
2988 * Retry pseudoroot lookup with various security flavors. We do this when:
2989 *
2990 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2991 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2992 *
2993 * Returns zero on success, or a negative NFS4ERR value, or a
2994 * negative errno value.
2995 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002996static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002997 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002999 /* Per 3530bis 15.33.5 */
3000 static const rpc_authflavor_t flav_array[] = {
3001 RPC_AUTH_GSS_KRB5P,
3002 RPC_AUTH_GSS_KRB5I,
3003 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003004 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003005 RPC_AUTH_NULL,
3006 };
3007 int status = -EPERM;
3008 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003010 if (server->auth_info.flavor_len > 0) {
3011 /* try each flavor specified by user */
3012 for (i = 0; i < server->auth_info.flavor_len; i++) {
3013 status = nfs4_lookup_root_sec(server, fhandle, info,
3014 server->auth_info.flavors[i]);
3015 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3016 continue;
3017 break;
3018 }
3019 } else {
3020 /* no flavors specified by user, try default list */
3021 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3022 status = nfs4_lookup_root_sec(server, fhandle, info,
3023 flav_array[i]);
3024 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3025 continue;
3026 break;
3027 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003028 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003029
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003030 /*
3031 * -EACCESS could mean that the user doesn't have correct permissions
3032 * to access the mount. It could also mean that we tried to mount
3033 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3034 * existing mount programs don't handle -EACCES very well so it should
3035 * be mapped to -EPERM instead.
3036 */
3037 if (status == -EACCES)
3038 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003039 return status;
3040}
3041
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003042static int nfs4_do_find_root_sec(struct nfs_server *server,
3043 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3044{
3045 int mv = server->nfs_client->cl_minorversion;
3046 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3047}
3048
3049/**
3050 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3051 * @server: initialized nfs_server handle
3052 * @fhandle: we fill in the pseudo-fs root file handle
3053 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003054 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003055 *
3056 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003057 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003058int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003059 struct nfs_fsinfo *info,
3060 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003061{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003062 int status;
3063
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003064 switch (auth_probe) {
3065 case false:
3066 status = nfs4_lookup_root(server, fhandle, info);
3067 if (status != -NFS4ERR_WRONGSEC)
3068 break;
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003069 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003070 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003071 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (status == 0)
3074 status = nfs4_server_capabilities(server, fhandle);
3075 if (status == 0)
3076 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003077
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003078 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Bryan Schumakerbae36242012-05-10 15:07:31 -04003081static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3082 struct nfs_fsinfo *info)
3083{
3084 int error;
3085 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003086 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003087
3088 error = nfs4_server_capabilities(server, mntfh);
3089 if (error < 0) {
3090 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3091 return error;
3092 }
3093
David Quigley14c43f72013-05-22 12:50:43 -04003094 label = nfs4_label_alloc(server, GFP_KERNEL);
3095 if (IS_ERR(label))
3096 return PTR_ERR(label);
3097
David Quigley1775fd32013-05-22 12:50:42 -04003098 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003099 if (error < 0) {
3100 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003101 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003102 }
3103
3104 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3105 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3106 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3107
David Quigley14c43f72013-05-22 12:50:43 -04003108err_free_label:
3109 nfs4_label_free(label);
3110
Bryan Schumakerbae36242012-05-10 15:07:31 -04003111 return error;
3112}
3113
Manoj Naik6b97fd32006-06-09 09:34:29 -04003114/*
3115 * Get locations and (maybe) other attributes of a referral.
3116 * Note that we'll actually follow the referral later when
3117 * we detect fsid mismatch in inode revalidation
3118 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003119static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3120 const struct qstr *name, struct nfs_fattr *fattr,
3121 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003122{
3123 int status = -ENOMEM;
3124 struct page *page = NULL;
3125 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003126
3127 page = alloc_page(GFP_KERNEL);
3128 if (page == NULL)
3129 goto out;
3130 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3131 if (locations == NULL)
3132 goto out;
3133
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003134 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003135 if (status != 0)
3136 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003137
3138 /*
3139 * If the fsid didn't change, this is a migration event, not a
3140 * referral. Cause us to drop into the exception handler, which
3141 * will kick off migration recovery.
3142 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003143 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003144 dprintk("%s: server did not return a different fsid for"
3145 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003146 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003147 goto out;
3148 }
Andy Adamson533eb462011-06-13 18:25:56 -04003149 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3150 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003151
Andy Adamson533eb462011-06-13 18:25:56 -04003152 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003153 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003154 memset(fhandle, 0, sizeof(struct nfs_fh));
3155out:
3156 if (page)
3157 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003158 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003159 return status;
3160}
3161
David Quigley1775fd32013-05-22 12:50:42 -04003162static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3163 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
3165 struct nfs4_getattr_arg args = {
3166 .fh = fhandle,
3167 .bitmask = server->attr_bitmask,
3168 };
3169 struct nfs4_getattr_res res = {
3170 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003171 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 .server = server,
3173 };
3174 struct rpc_message msg = {
3175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3176 .rpc_argp = &args,
3177 .rpc_resp = &res,
3178 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003179
3180 args.bitmask = nfs4_bitmask(server, label);
3181
Trond Myklebust0e574af2005-10-27 22:12:38 -04003182 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003183 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
David Quigley1775fd32013-05-22 12:50:42 -04003186static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3187 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
3189 struct nfs4_exception exception = { };
3190 int err;
3191 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003192 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3193 trace_nfs4_getattr(server, fhandle, fattr, err);
3194 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 &exception);
3196 } while (exception.retry);
3197 return err;
3198}
3199
3200/*
3201 * The file is not closed if it is opened due to the a request to change
3202 * the size of the file. The open call will not be needed once the
3203 * VFS layer lookup-intents are implemented.
3204 *
3205 * Close is called when the inode is destroyed.
3206 * If we haven't opened the file for O_WRONLY, we
3207 * need to in the size_change case to obtain a stateid.
3208 *
3209 * Got race?
3210 * Because OPEN is always done by name in nfsv4, it is
3211 * possible that we opened a different file by the same
3212 * name. We can recognize this race condition, but we
3213 * can't do anything about it besides returning an error.
3214 *
3215 * This will be fixed with VFS changes (lookup-intent).
3216 */
3217static int
3218nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3219 struct iattr *sattr)
3220{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003221 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003222 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003223 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003224 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 int status;
3226
Peng Tao88ac8152014-09-12 11:04:10 +08003227 if (pnfs_ld_layoutret_on_setattr(inode) &&
3228 sattr->ia_valid & ATTR_SIZE &&
3229 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003230 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003231
Trond Myklebust0e574af2005-10-27 22:12:38 -04003232 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Andy Adamson26699402012-05-30 16:12:24 -04003234 /* Deal with open(O_TRUNC) */
3235 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003236 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003237
3238 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003239 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003240 return 0;
3241
Trond Myklebustd5308382005-11-04 15:33:38 -05003242 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003243 if (sattr->ia_valid & ATTR_FILE) {
3244 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003245
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003246 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003247 if (ctx) {
3248 cred = ctx->cred;
3249 state = ctx->state;
3250 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003251 }
3252
David Quigley14c43f72013-05-22 12:50:43 -04003253 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3254 if (IS_ERR(label))
3255 return PTR_ERR(label);
3256
3257 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003258 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003259 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003260 nfs_setsecurity(inode, fattr, label);
3261 }
David Quigley14c43f72013-05-22 12:50:43 -04003262 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return status;
3264}
3265
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003266static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3267 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003268 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003269{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003270 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003271 int status;
3272 struct nfs4_lookup_arg args = {
3273 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003274 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003275 .name = name,
3276 };
3277 struct nfs4_lookup_res res = {
3278 .server = server,
3279 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003280 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003281 .fh = fhandle,
3282 };
3283 struct rpc_message msg = {
3284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3285 .rpc_argp = &args,
3286 .rpc_resp = &res,
3287 };
3288
David Quigleyaa9c2662013-05-22 12:50:44 -04003289 args.bitmask = nfs4_bitmask(server, label);
3290
David Howells2b3de442006-08-22 20:06:09 -04003291 nfs_fattr_init(fattr);
3292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003294 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 dprintk("NFS reply lookup: %d\n", status);
3296 return status;
3297}
3298
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003299static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003300{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003301 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003302 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003303 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3304 fattr->nlink = 2;
3305}
3306
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003307static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3308 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003309 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003310{
3311 struct nfs4_exception exception = { };
3312 struct rpc_clnt *client = *clnt;
3313 int err;
3314 do {
David Quigley1775fd32013-05-22 12:50:42 -04003315 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003316 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003317 switch (err) {
3318 case -NFS4ERR_BADNAME:
3319 err = -ENOENT;
3320 goto out;
3321 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003322 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003323 goto out;
3324 case -NFS4ERR_WRONGSEC:
3325 err = -EPERM;
3326 if (client != *clnt)
3327 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003328 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003329 if (IS_ERR(client))
3330 return PTR_ERR(client);
3331
3332 exception.retry = 1;
3333 break;
3334 default:
3335 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3336 }
3337 } while (exception.retry);
3338
3339out:
3340 if (err == 0)
3341 *clnt = client;
3342 else if (client != *clnt)
3343 rpc_shutdown_client(client);
3344
3345 return err;
3346}
3347
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003348static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003349 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3350 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003352 int status;
3353 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003354
David Quigley1775fd32013-05-22 12:50:42 -04003355 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003356 if (client != NFS_CLIENT(dir)) {
3357 rpc_shutdown_client(client);
3358 nfs_fixup_secinfo_attributes(fattr);
3359 }
3360 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003363struct rpc_clnt *
3364nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3365 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3366{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003367 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003368 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003369
David Quigley1775fd32013-05-22 12:50:42 -04003370 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003371 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003372 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003373 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003374}
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3377{
Trond Myklebust76b32992007-08-10 17:45:11 -04003378 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 struct nfs4_accessargs args = {
3380 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003381 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003383 struct nfs4_accessres res = {
3384 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003385 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 struct rpc_message msg = {
3387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3388 .rpc_argp = &args,
3389 .rpc_resp = &res,
3390 .rpc_cred = entry->cred,
3391 };
3392 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003393 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
3395 /*
3396 * Determine which access bits we want to ask for...
3397 */
3398 if (mode & MAY_READ)
3399 args.access |= NFS4_ACCESS_READ;
3400 if (S_ISDIR(inode->i_mode)) {
3401 if (mode & MAY_WRITE)
3402 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3403 if (mode & MAY_EXEC)
3404 args.access |= NFS4_ACCESS_LOOKUP;
3405 } else {
3406 if (mode & MAY_WRITE)
3407 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3408 if (mode & MAY_EXEC)
3409 args.access |= NFS4_ACCESS_EXECUTE;
3410 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003411
3412 res.fattr = nfs_alloc_fattr();
3413 if (res.fattr == NULL)
3414 return -ENOMEM;
3415
Bryan Schumaker7c513052011-03-24 17:12:24 +00003416 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003418 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003419 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003421 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return status;
3423}
3424
3425static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3426{
3427 struct nfs4_exception exception = { };
3428 int err;
3429 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003430 err = _nfs4_proc_access(inode, entry);
3431 trace_nfs4_access(inode, err);
3432 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 &exception);
3434 } while (exception.retry);
3435 return err;
3436}
3437
3438/*
3439 * TODO: For the time being, we don't try to get any attributes
3440 * along with any of the zero-copy operations READ, READDIR,
3441 * READLINK, WRITE.
3442 *
3443 * In the case of the first three, we want to put the GETATTR
3444 * after the read-type operation -- this is because it is hard
3445 * to predict the length of a GETATTR response in v4, and thus
3446 * align the READ data correctly. This means that the GETATTR
3447 * may end up partially falling into the page cache, and we should
3448 * shift it into the 'tail' of the xdr_buf before processing.
3449 * To do this efficiently, we need to know the total length
3450 * of data received, which doesn't seem to be available outside
3451 * of the RPC layer.
3452 *
3453 * In the case of WRITE, we also want to put the GETATTR after
3454 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003455 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 *
3457 * Both of these changes to the XDR layer would in fact be quite
3458 * minor, but I decided to leave them for a subsequent patch.
3459 */
3460static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3461 unsigned int pgbase, unsigned int pglen)
3462{
3463 struct nfs4_readlink args = {
3464 .fh = NFS_FH(inode),
3465 .pgbase = pgbase,
3466 .pglen = pglen,
3467 .pages = &page,
3468 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003469 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 struct rpc_message msg = {
3471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3472 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003473 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 };
3475
Bryan Schumaker7c513052011-03-24 17:12:24 +00003476 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 -07003477}
3478
3479static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3480 unsigned int pgbase, unsigned int pglen)
3481{
3482 struct nfs4_exception exception = { };
3483 int err;
3484 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003485 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3486 trace_nfs4_readlink(inode, err);
3487 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 &exception);
3489 } while (exception.retry);
3490 return err;
3491}
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003494 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496static int
3497nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003498 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
David Quigleyaa9c2662013-05-22 12:50:44 -04003500 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003501 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003503 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 int status = 0;
3505
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003506 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3507 if (IS_ERR(ctx))
3508 return PTR_ERR(ctx);
3509
David Quigleyaa9c2662013-05-22 12:50:44 -04003510 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3511
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003512 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003513 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 if (IS_ERR(state)) {
3515 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003516 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003519 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003520 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 return status;
3522}
3523
3524static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3525{
Trond Myklebust16e42952005-10-27 22:12:44 -04003526 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003527 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003529 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003531 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003532 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003533 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3536 .rpc_argp = &args,
3537 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003539 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003540
Bryan Schumaker7c513052011-03-24 17:12:24 +00003541 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003542 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003543 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 return status;
3545}
3546
3547static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3548{
3549 struct nfs4_exception exception = { };
3550 int err;
3551 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003552 err = _nfs4_proc_remove(dir, name);
3553 trace_nfs4_remove(dir, name, err);
3554 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 &exception);
3556 } while (exception.retry);
3557 return err;
3558}
3559
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003560static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003562 struct nfs_server *server = NFS_SERVER(dir);
3563 struct nfs_removeargs *args = msg->rpc_argp;
3564 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003566 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003568 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003569
3570 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003573static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3574{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003575 nfs4_setup_sequence(NFS_SERVER(data->dir),
3576 &data->args.seq_args,
3577 &data->res.seq_res,
3578 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003581static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003583 struct nfs_unlinkdata *data = task->tk_calldata;
3584 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003585
Trond Myklebust14516c32010-07-31 14:29:06 -04003586 if (!nfs4_sequence_done(task, &res->seq_res))
3587 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003588 if (nfs4_async_handle_error(task, res->server, NULL,
3589 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003590 return 0;
3591 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003592 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Jeff Laytond3d41522010-09-17 17:31:57 -04003595static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3596{
3597 struct nfs_server *server = NFS_SERVER(dir);
3598 struct nfs_renameargs *arg = msg->rpc_argp;
3599 struct nfs_renameres *res = msg->rpc_resp;
3600
3601 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003602 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003603 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003604}
3605
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003606static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3607{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003608 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3609 &data->args.seq_args,
3610 &data->res.seq_res,
3611 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003612}
3613
3614static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3615 struct inode *new_dir)
3616{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003617 struct nfs_renamedata *data = task->tk_calldata;
3618 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003619
3620 if (!nfs4_sequence_done(task, &res->seq_res))
3621 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003622 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003623 return 0;
3624
3625 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003626 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003627 return 1;
3628}
3629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3631{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003632 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 struct nfs4_link_arg arg = {
3634 .fh = NFS_FH(inode),
3635 .dir_fh = NFS_FH(dir),
3636 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003637 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003639 struct nfs4_link_res res = {
3640 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003641 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003642 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 struct rpc_message msg = {
3644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3645 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003646 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003648 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Trond Myklebust136f2622010-04-16 16:22:49 -04003650 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003651 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003652 goto out;
3653
David Quigley14c43f72013-05-22 12:50:43 -04003654 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3655 if (IS_ERR(res.label)) {
3656 status = PTR_ERR(res.label);
3657 goto out;
3658 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003659 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003660
Bryan Schumaker7c513052011-03-24 17:12:24 +00003661 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003662 if (!status) {
3663 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003664 status = nfs_post_op_update_inode(inode, res.fattr);
3665 if (!status)
3666 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003667 }
David Quigley14c43f72013-05-22 12:50:43 -04003668
3669
3670 nfs4_label_free(res.label);
3671
Trond Myklebust136f2622010-04-16 16:22:49 -04003672out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003673 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return status;
3675}
3676
3677static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3678{
3679 struct nfs4_exception exception = { };
3680 int err;
3681 do {
3682 err = nfs4_handle_exception(NFS_SERVER(inode),
3683 _nfs4_proc_link(inode, dir, name),
3684 &exception);
3685 } while (exception.retry);
3686 return err;
3687}
3688
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003689struct nfs4_createdata {
3690 struct rpc_message msg;
3691 struct nfs4_create_arg arg;
3692 struct nfs4_create_res res;
3693 struct nfs_fh fh;
3694 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003695 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003696};
3697
3698static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3699 struct qstr *name, struct iattr *sattr, u32 ftype)
3700{
3701 struct nfs4_createdata *data;
3702
3703 data = kzalloc(sizeof(*data), GFP_KERNEL);
3704 if (data != NULL) {
3705 struct nfs_server *server = NFS_SERVER(dir);
3706
David Quigley14c43f72013-05-22 12:50:43 -04003707 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3708 if (IS_ERR(data->label))
3709 goto out_free;
3710
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003711 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3712 data->msg.rpc_argp = &data->arg;
3713 data->msg.rpc_resp = &data->res;
3714 data->arg.dir_fh = NFS_FH(dir);
3715 data->arg.server = server;
3716 data->arg.name = name;
3717 data->arg.attrs = sattr;
3718 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003719 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003720 data->res.server = server;
3721 data->res.fh = &data->fh;
3722 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003723 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003724 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003725 }
3726 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003727out_free:
3728 kfree(data);
3729 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003730}
3731
3732static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3733{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003734 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003735 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003736 if (status == 0) {
3737 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003738 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003739 }
3740 return status;
3741}
3742
3743static void nfs4_free_createdata(struct nfs4_createdata *data)
3744{
David Quigley14c43f72013-05-22 12:50:43 -04003745 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003746 kfree(data);
3747}
3748
Chuck Lever4f390c12006-08-22 20:06:22 -04003749static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003750 struct page *page, unsigned int len, struct iattr *sattr,
3751 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003753 struct nfs4_createdata *data;
3754 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Chuck Lever94a6d752006-08-22 20:06:23 -04003756 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003757 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003758
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003759 status = -ENOMEM;
3760 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3761 if (data == NULL)
3762 goto out;
3763
3764 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3765 data->arg.u.symlink.pages = &page;
3766 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003767 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003769 status = nfs4_do_create(dir, dentry, data);
3770
3771 nfs4_free_createdata(data);
3772out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 return status;
3774}
3775
Chuck Lever4f390c12006-08-22 20:06:22 -04003776static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003777 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
3779 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003780 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003782
3783 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003786 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3787 trace_nfs4_symlink(dir, &dentry->d_name, err);
3788 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 &exception);
3790 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003791
3792 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 return err;
3794}
3795
3796static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003797 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003799 struct nfs4_createdata *data;
3800 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003802 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3803 if (data == NULL)
3804 goto out;
3805
David Quigley1775fd32013-05-22 12:50:42 -04003806 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003807 status = nfs4_do_create(dir, dentry, data);
3808
3809 nfs4_free_createdata(data);
3810out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return status;
3812}
3813
3814static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3815 struct iattr *sattr)
3816{
3817 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003818 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003820
David Quigleyaa9c2662013-05-22 12:50:44 -04003821 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3822
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003823 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003825 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3826 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3827 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 &exception);
3829 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003830 nfs4_label_release_security(label);
3831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return err;
3833}
3834
3835static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003836 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
3838 struct inode *dir = dentry->d_inode;
3839 struct nfs4_readdir_arg args = {
3840 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003841 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 .pgbase = 0,
3843 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003844 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003845 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 };
3847 struct nfs4_readdir_res res;
3848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3850 .rpc_argp = &args,
3851 .rpc_resp = &res,
3852 .rpc_cred = cred,
3853 };
3854 int status;
3855
Al Viro6de14722013-09-16 10:53:17 -04003856 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3857 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003858 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003859 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003861 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 -05003862 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003863 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003864 status += args.pgbase;
3865 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003866
3867 nfs_invalidate_atime(dir);
3868
Harvey Harrison3110ff82008-05-02 13:42:44 -07003869 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return status;
3871}
3872
3873static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003874 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
3876 struct nfs4_exception exception = { };
3877 int err;
3878 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003879 err = _nfs4_proc_readdir(dentry, cred, cookie,
3880 pages, count, plus);
3881 trace_nfs4_readdir(dentry->d_inode, err);
3882 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 &exception);
3884 } while (exception.retry);
3885 return err;
3886}
3887
3888static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003889 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003891 struct nfs4_createdata *data;
3892 int mode = sattr->ia_mode;
3893 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003895 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3896 if (data == NULL)
3897 goto out;
3898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003900 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003902 data->arg.ftype = NF4BLK;
3903 data->arg.u.device.specdata1 = MAJOR(rdev);
3904 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 }
3906 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003907 data->arg.ftype = NF4CHR;
3908 data->arg.u.device.specdata1 = MAJOR(rdev);
3909 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003910 } else if (!S_ISSOCK(mode)) {
3911 status = -EINVAL;
3912 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 }
David Quigley1775fd32013-05-22 12:50:42 -04003914
David Quigleyaa9c2662013-05-22 12:50:44 -04003915 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003916 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003917out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003918 nfs4_free_createdata(data);
3919out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 return status;
3921}
3922
3923static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3924 struct iattr *sattr, dev_t rdev)
3925{
3926 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003927 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003929
David Quigleyaa9c2662013-05-22 12:50:44 -04003930 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3931
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003932 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003934 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3935 trace_nfs4_mknod(dir, &dentry->d_name, err);
3936 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 &exception);
3938 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003939
3940 nfs4_label_release_security(label);
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 return err;
3943}
3944
3945static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3946 struct nfs_fsstat *fsstat)
3947{
3948 struct nfs4_statfs_arg args = {
3949 .fh = fhandle,
3950 .bitmask = server->attr_bitmask,
3951 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003952 struct nfs4_statfs_res res = {
3953 .fsstat = fsstat,
3954 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 struct rpc_message msg = {
3956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3957 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003958 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 };
3960
Trond Myklebust0e574af2005-10-27 22:12:38 -04003961 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003962 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963}
3964
3965static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3966{
3967 struct nfs4_exception exception = { };
3968 int err;
3969 do {
3970 err = nfs4_handle_exception(server,
3971 _nfs4_proc_statfs(server, fhandle, fsstat),
3972 &exception);
3973 } while (exception.retry);
3974 return err;
3975}
3976
3977static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3978 struct nfs_fsinfo *fsinfo)
3979{
3980 struct nfs4_fsinfo_arg args = {
3981 .fh = fhandle,
3982 .bitmask = server->attr_bitmask,
3983 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003984 struct nfs4_fsinfo_res res = {
3985 .fsinfo = fsinfo,
3986 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 struct rpc_message msg = {
3988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3989 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003990 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 };
3992
Bryan Schumaker7c513052011-03-24 17:12:24 +00003993 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
3995
3996static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3997{
3998 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003999 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 int err;
4001
4002 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004003 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004004 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004005 if (err == 0) {
4006 struct nfs_client *clp = server->nfs_client;
4007
4008 spin_lock(&clp->cl_lock);
4009 clp->cl_lease_time = fsinfo->lease_time * HZ;
4010 clp->cl_last_renewal = now;
4011 spin_unlock(&clp->cl_lock);
4012 break;
4013 }
4014 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 } while (exception.retry);
4016 return err;
4017}
4018
4019static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4020{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004021 int error;
4022
Trond Myklebust0e574af2005-10-27 22:12:38 -04004023 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004024 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004025 if (error == 0) {
4026 /* block layout checks this! */
4027 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004028 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004029 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004030
4031 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032}
4033
4034static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4035 struct nfs_pathconf *pathconf)
4036{
4037 struct nfs4_pathconf_arg args = {
4038 .fh = fhandle,
4039 .bitmask = server->attr_bitmask,
4040 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004041 struct nfs4_pathconf_res res = {
4042 .pathconf = pathconf,
4043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 struct rpc_message msg = {
4045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4046 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004047 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 };
4049
4050 /* None of the pathconf attributes are mandatory to implement */
4051 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4052 memset(pathconf, 0, sizeof(*pathconf));
4053 return 0;
4054 }
4055
Trond Myklebust0e574af2005-10-27 22:12:38 -04004056 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004057 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
4060static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4061 struct nfs_pathconf *pathconf)
4062{
4063 struct nfs4_exception exception = { };
4064 int err;
4065
4066 do {
4067 err = nfs4_handle_exception(server,
4068 _nfs4_proc_pathconf(server, fhandle, pathconf),
4069 &exception);
4070 } while (exception.retry);
4071 return err;
4072}
4073
Trond Myklebust5521abf2013-03-16 20:54:34 -04004074int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004075 const struct nfs_open_context *ctx,
4076 const struct nfs_lock_context *l_ctx,
4077 fmode_t fmode)
4078{
4079 const struct nfs_lockowner *lockowner = NULL;
4080
4081 if (l_ctx != NULL)
4082 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004083 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004084}
4085EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4086
Trond Myklebust5521abf2013-03-16 20:54:34 -04004087static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4088 const struct nfs_open_context *ctx,
4089 const struct nfs_lock_context *l_ctx,
4090 fmode_t fmode)
4091{
4092 nfs4_stateid current_stateid;
4093
Trond Myklebuste1253be2014-03-05 08:44:23 -05004094 /* If the current stateid represents a lost lock, then exit */
4095 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4096 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004097 return nfs4_stateid_match(stateid, &current_stateid);
4098}
4099
4100static bool nfs4_error_stateid_expired(int err)
4101{
4102 switch (err) {
4103 case -NFS4ERR_DELEG_REVOKED:
4104 case -NFS4ERR_ADMIN_REVOKED:
4105 case -NFS4ERR_BAD_STATEID:
4106 case -NFS4ERR_STALE_STATEID:
4107 case -NFS4ERR_OLD_STATEID:
4108 case -NFS4ERR_OPENMODE:
4109 case -NFS4ERR_EXPIRED:
4110 return true;
4111 }
4112 return false;
4113}
4114
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004115void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004116{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004117 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004118}
4119
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004120static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004122 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004124 trace_nfs4_read(hdr, task->tk_status);
4125 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004126 hdr->args.context->state,
4127 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004128 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004129 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004131
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004132 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004134 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
4137
Trond Myklebust5521abf2013-03-16 20:54:34 -04004138static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004139 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004140{
4141
4142 if (!nfs4_error_stateid_expired(task->tk_status) ||
4143 nfs4_stateid_is_current(&args->stateid,
4144 args->context,
4145 args->lock_context,
4146 FMODE_READ))
4147 return false;
4148 rpc_restart_call_prepare(task);
4149 return true;
4150}
4151
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004152static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004153{
4154
4155 dprintk("--> %s\n", __func__);
4156
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004157 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004158 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004159 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004160 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004161 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4162 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004163}
4164
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004165static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4166 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004168 hdr->timestamp = jiffies;
4169 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004170 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004171 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172}
4173
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004174static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4175 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004176{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004177 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4178 &hdr->args.seq_args,
4179 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004180 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004181 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004182 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4183 hdr->args.lock_context,
4184 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004185 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004186 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004187 return -EIO;
4188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189}
4190
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004191static int nfs4_write_done_cb(struct rpc_task *task,
4192 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004194 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004195
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004196 trace_nfs4_write(hdr, task->tk_status);
4197 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004198 hdr->args.context->state,
4199 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004200 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004201 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004203 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004204 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4205 nfs_post_op_update_inode_force_wcc(inode, &hdr->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004206 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209
Trond Myklebust5521abf2013-03-16 20:54:34 -04004210static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004211 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004212{
4213
4214 if (!nfs4_error_stateid_expired(task->tk_status) ||
4215 nfs4_stateid_is_current(&args->stateid,
4216 args->context,
4217 args->lock_context,
4218 FMODE_WRITE))
4219 return false;
4220 rpc_restart_call_prepare(task);
4221 return true;
4222}
4223
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004224static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004225{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004226 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004227 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004228 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004229 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004230 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4231 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004232}
4233
Trond Myklebust5a37f852012-04-28 14:55:16 -04004234static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004235bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004236{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004237 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004238 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004239 return false;
4240 /* Otherwise, request attributes if and only if we don't hold
4241 * a delegation
4242 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004243 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004244}
Fred Isamana69aef12011-03-03 15:13:47 +00004245
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004246static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4247 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004249 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004250
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004251 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4252 hdr->args.bitmask = NULL;
4253 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004254 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004255 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004256
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004257 if (!hdr->pgio_done_cb)
4258 hdr->pgio_done_cb = nfs4_write_done_cb;
4259 hdr->res.server = server;
4260 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004262 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004263 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264}
4265
Fred Isaman0b7c0152012-04-20 14:47:39 -04004266static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4267{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004268 nfs4_setup_sequence(NFS_SERVER(data->inode),
4269 &data->args.seq_args,
4270 &data->res.seq_res,
4271 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272}
4273
Fred Isaman0b7c0152012-04-20 14:47:39 -04004274static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004277
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004278 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004279 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4280 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004281 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004282 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285}
4286
Fred Isaman0b7c0152012-04-20 14:47:39 -04004287static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004288{
4289 if (!nfs4_sequence_done(task, &data->res.seq_res))
4290 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004291 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004292}
4293
Fred Isaman0b7c0152012-04-20 14:47:39 -04004294static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004296 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004297
Fred Isaman0b7c0152012-04-20 14:47:39 -04004298 if (data->commit_done_cb == NULL)
4299 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004300 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004301 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004302 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004305struct nfs4_renewdata {
4306 struct nfs_client *client;
4307 unsigned long timestamp;
4308};
4309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310/*
4311 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4312 * standalone procedure for queueing an asynchronous RENEW.
4313 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004314static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004315{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004316 struct nfs4_renewdata *data = calldata;
4317 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004318
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004319 if (atomic_read(&clp->cl_count) > 1)
4320 nfs4_schedule_state_renewal(clp);
4321 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004322 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004323}
4324
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004325static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004327 struct nfs4_renewdata *data = calldata;
4328 struct nfs_client *clp = data->client;
4329 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004331 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004332 switch (task->tk_status) {
4333 case 0:
4334 break;
4335 case -NFS4ERR_LEASE_MOVED:
4336 nfs4_schedule_lease_moved_recovery(clp);
4337 break;
4338 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004339 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004340 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4341 return;
4342 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004343 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004344 return;
4345 }
4346 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004348 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349}
4350
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004351static const struct rpc_call_ops nfs4_renew_ops = {
4352 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004353 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004354};
4355
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004356static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357{
4358 struct rpc_message msg = {
4359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4360 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004361 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004363 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004365 if (renew_flags == 0)
4366 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004367 if (!atomic_inc_not_zero(&clp->cl_count))
4368 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004369 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004370 if (data == NULL)
4371 return -ENOMEM;
4372 data->client = clp;
4373 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004374 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004375 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376}
4377
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004378static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379{
4380 struct rpc_message msg = {
4381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4382 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004383 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 };
4385 unsigned long now = jiffies;
4386 int status;
4387
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004388 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 if (status < 0)
4390 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004391 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 return 0;
4393}
4394
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004395static inline int nfs4_server_supports_acls(struct nfs_server *server)
4396{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004397 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004398}
4399
Trond Myklebust21f498c2012-08-24 10:59:25 -04004400/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4401 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004402 * the stack.
4403 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004404#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004405
Neil Hormane9e3d722011-03-04 19:26:03 -05004406static int buf_to_pages_noslab(const void *buf, size_t buflen,
4407 struct page **pages, unsigned int *pgbase)
4408{
4409 struct page *newpage, **spages;
4410 int rc = 0;
4411 size_t len;
4412 spages = pages;
4413
4414 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004415 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004416 newpage = alloc_page(GFP_KERNEL);
4417
4418 if (newpage == NULL)
4419 goto unwind;
4420 memcpy(page_address(newpage), buf, len);
4421 buf += len;
4422 buflen -= len;
4423 *pages++ = newpage;
4424 rc++;
4425 } while (buflen != 0);
4426
4427 return rc;
4428
4429unwind:
4430 for(; rc > 0; rc--)
4431 __free_page(spages[rc-1]);
4432 return -ENOMEM;
4433}
4434
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004435struct nfs4_cached_acl {
4436 int cached;
4437 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004438 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004439};
4440
4441static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004442{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004443 struct nfs_inode *nfsi = NFS_I(inode);
4444
4445 spin_lock(&inode->i_lock);
4446 kfree(nfsi->nfs4_acl);
4447 nfsi->nfs4_acl = acl;
4448 spin_unlock(&inode->i_lock);
4449}
4450
4451static void nfs4_zap_acl_attr(struct inode *inode)
4452{
4453 nfs4_set_cached_acl(inode, NULL);
4454}
4455
4456static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4457{
4458 struct nfs_inode *nfsi = NFS_I(inode);
4459 struct nfs4_cached_acl *acl;
4460 int ret = -ENOENT;
4461
4462 spin_lock(&inode->i_lock);
4463 acl = nfsi->nfs4_acl;
4464 if (acl == NULL)
4465 goto out;
4466 if (buf == NULL) /* user is just asking for length */
4467 goto out_len;
4468 if (acl->cached == 0)
4469 goto out;
4470 ret = -ERANGE; /* see getxattr(2) man page */
4471 if (acl->len > buflen)
4472 goto out;
4473 memcpy(buf, acl->data, acl->len);
4474out_len:
4475 ret = acl->len;
4476out:
4477 spin_unlock(&inode->i_lock);
4478 return ret;
4479}
4480
Sachin Prabhu5794d212012-04-17 14:36:40 +01004481static 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 +00004482{
4483 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004484 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004485
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004486 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004487 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004488 if (acl == NULL)
4489 goto out;
4490 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004491 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004492 } else {
4493 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4494 if (acl == NULL)
4495 goto out;
4496 acl->cached = 0;
4497 }
4498 acl->len = acl_len;
4499out:
4500 nfs4_set_cached_acl(inode, acl);
4501}
4502
Andy Adamsonbf118a32011-12-07 11:55:27 -05004503/*
4504 * The getxattr API returns the required buffer length when called with a
4505 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4506 * the required buf. On a NULL buf, we send a page of data to the server
4507 * guessing that the ACL request can be serviced by a page. If so, we cache
4508 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4509 * the cache. If not so, we throw away the page, and cache the required
4510 * length. The next getxattr call will then produce another round trip to
4511 * the server, this time with the input buf of the required size.
4512 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004513static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004514{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004515 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004516 struct nfs_getaclargs args = {
4517 .fh = NFS_FH(inode),
4518 .acl_pages = pages,
4519 .acl_len = buflen,
4520 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004521 struct nfs_getaclres res = {
4522 .acl_len = buflen,
4523 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004524 struct rpc_message msg = {
4525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4526 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004527 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004528 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004529 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4530 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004531
Andy Adamsonbf118a32011-12-07 11:55:27 -05004532 /* As long as we're doing a round trip to the server anyway,
4533 * let's be prepared for a page of acl data. */
4534 if (npages == 0)
4535 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004536 if (npages > ARRAY_SIZE(pages))
4537 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004538
Andy Adamsonbf118a32011-12-07 11:55:27 -05004539 for (i = 0; i < npages; i++) {
4540 pages[i] = alloc_page(GFP_KERNEL);
4541 if (!pages[i])
4542 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004543 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004544
4545 /* for decoding across pages */
4546 res.acl_scratch = alloc_page(GFP_KERNEL);
4547 if (!res.acl_scratch)
4548 goto out_free;
4549
Andy Adamsonbf118a32011-12-07 11:55:27 -05004550 args.acl_len = npages * PAGE_SIZE;
4551 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004552
Peng Taode040be2012-01-10 22:42:47 +08004553 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004554 __func__, buf, buflen, npages, args.acl_len);
4555 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4556 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004557 if (ret)
4558 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004559
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004560 /* Handle the case where the passed-in buffer is too short */
4561 if (res.acl_flags & NFS4_ACL_TRUNC) {
4562 /* Did the user only issue a request for the acl length? */
4563 if (buf == NULL)
4564 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004565 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004566 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004567 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004568 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004569 if (buf) {
4570 if (res.acl_len > buflen) {
4571 ret = -ERANGE;
4572 goto out_free;
4573 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004574 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004575 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004576out_ok:
4577 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004578out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004579 for (i = 0; i < npages; i++)
4580 if (pages[i])
4581 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004582 if (res.acl_scratch)
4583 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004584 return ret;
4585}
4586
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004587static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4588{
4589 struct nfs4_exception exception = { };
4590 ssize_t ret;
4591 do {
4592 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004593 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004594 if (ret >= 0)
4595 break;
4596 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4597 } while (exception.retry);
4598 return ret;
4599}
4600
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004601static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4602{
4603 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004604 int ret;
4605
4606 if (!nfs4_server_supports_acls(server))
4607 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004608 ret = nfs_revalidate_inode(server, inode);
4609 if (ret < 0)
4610 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004611 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4612 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004613 ret = nfs4_read_cached_acl(inode, buf, buflen);
4614 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004615 /* -ENOENT is returned if there is no ACL or if there is an ACL
4616 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004617 return ret;
4618 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004619}
4620
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004621static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004622{
4623 struct nfs_server *server = NFS_SERVER(inode);
4624 struct page *pages[NFS4ACL_MAXPAGES];
4625 struct nfs_setaclargs arg = {
4626 .fh = NFS_FH(inode),
4627 .acl_pages = pages,
4628 .acl_len = buflen,
4629 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004630 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004631 struct rpc_message msg = {
4632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4633 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004634 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004635 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004636 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004637 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004638
4639 if (!nfs4_server_supports_acls(server))
4640 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004641 if (npages > ARRAY_SIZE(pages))
4642 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004643 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4644 if (i < 0)
4645 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004646 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004647 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004648
4649 /*
4650 * Free each page after tx, so the only ref left is
4651 * held by the network stack
4652 */
4653 for (; i > 0; i--)
4654 put_page(pages[i-1]);
4655
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004656 /*
4657 * Acl update can result in inode attribute update.
4658 * so mark the attribute cache invalid.
4659 */
4660 spin_lock(&inode->i_lock);
4661 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4662 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004663 nfs_access_zap_cache(inode);
4664 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004665 return ret;
4666}
4667
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004668static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4669{
4670 struct nfs4_exception exception = { };
4671 int err;
4672 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004673 err = __nfs4_proc_set_acl(inode, buf, buflen);
4674 trace_nfs4_set_acl(inode, err);
4675 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004676 &exception);
4677 } while (exception.retry);
4678 return err;
4679}
4680
David Quigleyaa9c2662013-05-22 12:50:44 -04004681#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4682static int _nfs4_get_security_label(struct inode *inode, void *buf,
4683 size_t buflen)
4684{
4685 struct nfs_server *server = NFS_SERVER(inode);
4686 struct nfs_fattr fattr;
4687 struct nfs4_label label = {0, 0, buflen, buf};
4688
4689 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004690 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004691 .fh = NFS_FH(inode),
4692 .bitmask = bitmask,
4693 };
4694 struct nfs4_getattr_res res = {
4695 .fattr = &fattr,
4696 .label = &label,
4697 .server = server,
4698 };
4699 struct rpc_message msg = {
4700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004701 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004702 .rpc_resp = &res,
4703 };
4704 int ret;
4705
4706 nfs_fattr_init(&fattr);
4707
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004708 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004709 if (ret)
4710 return ret;
4711 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4712 return -ENOENT;
4713 if (buflen < label.len)
4714 return -ERANGE;
4715 return 0;
4716}
4717
4718static int nfs4_get_security_label(struct inode *inode, void *buf,
4719 size_t buflen)
4720{
4721 struct nfs4_exception exception = { };
4722 int err;
4723
4724 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4725 return -EOPNOTSUPP;
4726
4727 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004728 err = _nfs4_get_security_label(inode, buf, buflen);
4729 trace_nfs4_get_security_label(inode, err);
4730 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004731 &exception);
4732 } while (exception.retry);
4733 return err;
4734}
4735
4736static int _nfs4_do_set_security_label(struct inode *inode,
4737 struct nfs4_label *ilabel,
4738 struct nfs_fattr *fattr,
4739 struct nfs4_label *olabel)
4740{
4741
4742 struct iattr sattr = {0};
4743 struct nfs_server *server = NFS_SERVER(inode);
4744 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004745 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004746 .fh = NFS_FH(inode),
4747 .iap = &sattr,
4748 .server = server,
4749 .bitmask = bitmask,
4750 .label = ilabel,
4751 };
4752 struct nfs_setattrres res = {
4753 .fattr = fattr,
4754 .label = olabel,
4755 .server = server,
4756 };
4757 struct rpc_message msg = {
4758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004759 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004760 .rpc_resp = &res,
4761 };
4762 int status;
4763
Jeff Layton12207f62013-11-01 10:49:32 -04004764 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004765
Jeff Layton12207f62013-11-01 10:49:32 -04004766 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004767 if (status)
4768 dprintk("%s failed: %d\n", __func__, status);
4769
4770 return status;
4771}
4772
4773static int nfs4_do_set_security_label(struct inode *inode,
4774 struct nfs4_label *ilabel,
4775 struct nfs_fattr *fattr,
4776 struct nfs4_label *olabel)
4777{
4778 struct nfs4_exception exception = { };
4779 int err;
4780
4781 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004782 err = _nfs4_do_set_security_label(inode, ilabel,
4783 fattr, olabel);
4784 trace_nfs4_set_security_label(inode, err);
4785 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004786 &exception);
4787 } while (exception.retry);
4788 return err;
4789}
4790
4791static int
4792nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4793{
4794 struct nfs4_label ilabel, *olabel = NULL;
4795 struct nfs_fattr fattr;
4796 struct rpc_cred *cred;
4797 struct inode *inode = dentry->d_inode;
4798 int status;
4799
4800 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4801 return -EOPNOTSUPP;
4802
4803 nfs_fattr_init(&fattr);
4804
4805 ilabel.pi = 0;
4806 ilabel.lfs = 0;
4807 ilabel.label = (char *)buf;
4808 ilabel.len = buflen;
4809
4810 cred = rpc_lookup_cred();
4811 if (IS_ERR(cred))
4812 return PTR_ERR(cred);
4813
4814 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4815 if (IS_ERR(olabel)) {
4816 status = -PTR_ERR(olabel);
4817 goto out;
4818 }
4819
4820 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4821 if (status == 0)
4822 nfs_setsecurity(inode, &fattr, olabel);
4823
4824 nfs4_label_free(olabel);
4825out:
4826 put_rpccred(cred);
4827 return status;
4828}
4829#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4830
4831
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832static int
NeilBrown8478eaa2014-09-18 16:09:27 +10004833nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server,
4834 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004836 struct nfs_client *clp = server->nfs_client;
4837
4838 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 return 0;
4840 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004841 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004842 case -NFS4ERR_ADMIN_REVOKED:
4843 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004844 if (state == NULL)
4845 break;
4846 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004847 case -NFS4ERR_OPENMODE:
4848 if (state == NULL)
4849 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004850 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004851 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004852 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004853 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004854 if (state != NULL) {
4855 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004856 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004859 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004860 nfs4_schedule_lease_recovery(clp);
4861 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004862 case -NFS4ERR_MOVED:
4863 if (nfs4_schedule_migration_recovery(server) < 0)
4864 goto recovery_failed;
4865 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004866 case -NFS4ERR_LEASE_MOVED:
4867 nfs4_schedule_lease_moved_recovery(clp);
4868 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004869#if defined(CONFIG_NFS_V4_1)
4870 case -NFS4ERR_BADSESSION:
4871 case -NFS4ERR_BADSLOT:
4872 case -NFS4ERR_BAD_HIGH_SLOT:
4873 case -NFS4ERR_DEADSESSION:
4874 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4875 case -NFS4ERR_SEQ_FALSE_RETRY:
4876 case -NFS4ERR_SEQ_MISORDERED:
4877 dprintk("%s ERROR %d, Reset session\n", __func__,
4878 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004879 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004880 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004881#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004883 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10004884 rpc_delay(task, nfs4_update_delay(timeout));
4885 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05004886 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004888 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004890 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 }
4892 task->tk_status = nfs4_map_errors(task->tk_status);
4893 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004894recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004895 task->tk_status = -EIO;
4896 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004897wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004898 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004899 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4900 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004901 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4902 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004903restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004904 task->tk_status = 0;
4905 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906}
4907
Chuck Leverf0920752012-05-21 22:45:41 -04004908static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4909 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004910{
4911 __be32 verf[2];
4912
Chuck Lever2c820d92012-05-21 22:45:33 -04004913 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4914 /* An impossible timestamp guarantees this value
4915 * will never match a generated boot time. */
4916 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004917 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004918 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004919 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004920 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4921 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004922 }
Chuck Levercd937102012-03-02 17:14:31 -05004923 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4924}
4925
Chuck Levere984a552012-09-14 17:24:21 -04004926static unsigned int
4927nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4928 char *buf, size_t len)
4929{
4930 unsigned int result;
4931
4932 rcu_read_lock();
4933 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4934 clp->cl_ipaddr,
4935 rpc_peeraddr2str(clp->cl_rpcclient,
4936 RPC_DISPLAY_ADDR),
4937 rpc_peeraddr2str(clp->cl_rpcclient,
4938 RPC_DISPLAY_PROTO));
4939 rcu_read_unlock();
4940 return result;
4941}
4942
4943static unsigned int
4944nfs4_init_uniform_client_string(const struct nfs_client *clp,
4945 char *buf, size_t len)
4946{
Trond Myklebust55b59292013-07-24 16:36:35 -04004947 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004948
4949 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004950 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4951 clp->rpc_ops->version,
4952 clp->cl_minorversion,
4953 nfs4_client_id_uniquifier,
4954 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004955 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4956 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004957 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004958}
4959
Chuck Lever706cb8d2014-03-12 12:51:47 -04004960/*
4961 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4962 * services. Advertise one based on the address family of the
4963 * clientaddr.
4964 */
4965static unsigned int
4966nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4967{
4968 if (strchr(clp->cl_ipaddr, ':') != NULL)
4969 return scnprintf(buf, len, "tcp6");
4970 else
4971 return scnprintf(buf, len, "tcp");
4972}
4973
Jeff Laytonf11b2a12014-06-21 20:52:17 -04004974static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
4975{
4976 struct nfs4_setclientid *sc = calldata;
4977
4978 if (task->tk_status == 0)
4979 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
4980}
4981
4982static const struct rpc_call_ops nfs4_setclientid_ops = {
4983 .rpc_call_done = nfs4_setclientid_done,
4984};
4985
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004986/**
4987 * nfs4_proc_setclientid - Negotiate client ID
4988 * @clp: state data structure
4989 * @program: RPC program for NFSv4 callback service
4990 * @port: IP port number for NFS4 callback service
4991 * @cred: RPC credential to use for this call
4992 * @res: where to place the result
4993 *
4994 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4995 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004996int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4997 unsigned short port, struct rpc_cred *cred,
4998 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999{
5000 nfs4_verifier sc_verifier;
5001 struct nfs4_setclientid setclientid = {
5002 .sc_verifier = &sc_verifier,
5003 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00005004 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 };
5006 struct rpc_message msg = {
5007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5008 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005009 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005010 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005012 struct rpc_task *task;
5013 struct rpc_task_setup task_setup_data = {
5014 .rpc_client = clp->cl_rpcclient,
5015 .rpc_message = &msg,
5016 .callback_ops = &nfs4_setclientid_ops,
5017 .callback_data = &setclientid,
5018 .flags = RPC_TASK_TIMEOUT,
5019 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005020 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Chuck Leverde734832012-07-11 16:30:50 -04005022 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005023 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005024 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5025 setclientid.sc_name_len =
5026 nfs4_init_uniform_client_string(clp,
5027 setclientid.sc_name,
5028 sizeof(setclientid.sc_name));
5029 else
5030 setclientid.sc_name_len =
5031 nfs4_init_nonuniform_client_string(clp,
5032 setclientid.sc_name,
5033 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04005034 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005035 setclientid.sc_netid_len =
5036 nfs4_init_callback_netid(clp,
5037 setclientid.sc_netid,
5038 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005039 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005040 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 clp->cl_ipaddr, port >> 8, port & 255);
5042
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005043 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
5044 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5045 setclientid.sc_name_len, setclientid.sc_name);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005046 task = rpc_run_task(&task_setup_data);
5047 if (IS_ERR(task)) {
5048 status = PTR_ERR(task);
5049 goto out;
5050 }
5051 status = task->tk_status;
5052 if (setclientid.sc_cred) {
5053 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5054 put_rpccred(setclientid.sc_cred);
5055 }
5056 rpc_put_task(task);
5057out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005058 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005059 dprintk("NFS reply setclientid: %d\n", status);
5060 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061}
5062
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005063/**
5064 * nfs4_proc_setclientid_confirm - Confirm client ID
5065 * @clp: state data structure
5066 * @res: result of a previous SETCLIENTID
5067 * @cred: RPC credential to use for this call
5068 *
5069 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5070 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005071int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005072 struct nfs4_setclientid_res *arg,
5073 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 struct rpc_message msg = {
5076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005077 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005078 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 int status;
5081
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005082 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5083 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5084 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005085 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005086 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005087 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 return status;
5089}
5090
Trond Myklebustfe650402006-01-03 09:55:18 +01005091struct nfs4_delegreturndata {
5092 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005093 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005094 struct nfs_fh fh;
5095 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005096 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005097 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005098 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005099 struct inode *inode;
5100 bool roc;
5101 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005102};
5103
Trond Myklebustfe650402006-01-03 09:55:18 +01005104static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5105{
5106 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005107
Trond Myklebust14516c32010-07-31 14:29:06 -04005108 if (!nfs4_sequence_done(task, &data->res.seq_res))
5109 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005110
Trond Myklebustca8acf82013-08-13 10:36:56 -04005111 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005112 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005113 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005114 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005115 case -NFS4ERR_ADMIN_REVOKED:
5116 case -NFS4ERR_DELEG_REVOKED:
5117 case -NFS4ERR_BAD_STATEID:
5118 case -NFS4ERR_OLD_STATEID:
5119 case -NFS4ERR_STALE_STATEID:
5120 case -NFS4ERR_EXPIRED:
5121 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005122 if (data->roc)
5123 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005124 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005125 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005126 if (nfs4_async_handle_error(task, data->res.server,
5127 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005128 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005129 return;
5130 }
5131 }
5132 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005133}
5134
5135static void nfs4_delegreturn_release(void *calldata)
5136{
Peng Tao039b7562014-07-03 13:05:02 +08005137 struct nfs4_delegreturndata *data = calldata;
5138
5139 if (data->roc)
5140 pnfs_roc_release(data->inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005141 kfree(calldata);
5142}
5143
Andy Adamson938e1012009-04-01 09:22:28 -04005144static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5145{
5146 struct nfs4_delegreturndata *d_data;
5147
5148 d_data = (struct nfs4_delegreturndata *)data;
5149
Peng Tao039b7562014-07-03 13:05:02 +08005150 if (d_data->roc &&
5151 pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task))
5152 return;
5153
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005154 nfs4_setup_sequence(d_data->res.server,
5155 &d_data->args.seq_args,
5156 &d_data->res.seq_res,
5157 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005158}
Andy Adamson938e1012009-04-01 09:22:28 -04005159
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005160static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005161 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005162 .rpc_call_done = nfs4_delegreturn_done,
5163 .rpc_release = nfs4_delegreturn_release,
5164};
5165
Trond Myklebuste6f81072008-01-24 18:14:34 -05005166static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005167{
5168 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005169 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005170 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005171 struct rpc_message msg = {
5172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5173 .rpc_cred = cred,
5174 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005175 struct rpc_task_setup task_setup_data = {
5176 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005177 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005178 .callback_ops = &nfs4_delegreturn_ops,
5179 .flags = RPC_TASK_ASYNC,
5180 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005181 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005182
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005183 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005184 if (data == NULL)
5185 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005186 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005187 data->args.fhandle = &data->fh;
5188 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005189 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005190 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005191 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005192 data->res.fattr = &data->fattr;
5193 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005194 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005195 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005196 data->rpc_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005197 data->inode = inode;
5198 data->roc = list_empty(&NFS_I(inode)->open_files) ?
5199 pnfs_roc(inode) : false;
Trond Myklebustfe650402006-01-03 09:55:18 +01005200
Trond Myklebustc970aa82007-07-14 15:39:59 -04005201 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005202 msg.rpc_argp = &data->args;
5203 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005204 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005205 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005206 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005207 if (!issync)
5208 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005209 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005210 if (status != 0)
5211 goto out;
5212 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005213 if (status == 0)
5214 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5215 else
5216 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005217out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005218 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005219 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220}
5221
Trond Myklebuste6f81072008-01-24 18:14:34 -05005222int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223{
5224 struct nfs_server *server = NFS_SERVER(inode);
5225 struct nfs4_exception exception = { };
5226 int err;
5227 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005228 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005229 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 switch (err) {
5231 case -NFS4ERR_STALE_STATEID:
5232 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 case 0:
5234 return 0;
5235 }
5236 err = nfs4_handle_exception(server, err, &exception);
5237 } while (exception.retry);
5238 return err;
5239}
5240
5241#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5242#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5243
5244/*
5245 * sleep, with exponential backoff, and retry the LOCK operation.
5246 */
5247static unsigned long
5248nfs4_set_lock_task_retry(unsigned long timeout)
5249{
Colin Cross416ad3c2013-05-06 23:50:06 +00005250 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 timeout <<= 1;
5252 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5253 return NFS4_LOCK_MAXTIMEOUT;
5254 return timeout;
5255}
5256
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5258{
5259 struct inode *inode = state->inode;
5260 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005261 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005262 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005264 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005266 struct nfs_lockt_res res = {
5267 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 };
5269 struct rpc_message msg = {
5270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5271 .rpc_argp = &arg,
5272 .rpc_resp = &res,
5273 .rpc_cred = state->owner->so_cred,
5274 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 struct nfs4_lock_state *lsp;
5276 int status;
5277
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005278 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005279 status = nfs4_set_lock_state(state, request);
5280 if (status != 0)
5281 goto out;
5282 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005283 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005284 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005285 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005286 switch (status) {
5287 case 0:
5288 request->fl_type = F_UNLCK;
5289 break;
5290 case -NFS4ERR_DENIED:
5291 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005293 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005294 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005295out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 return status;
5297}
5298
5299static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5300{
5301 struct nfs4_exception exception = { };
5302 int err;
5303
5304 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005305 err = _nfs4_proc_getlk(state, cmd, request);
5306 trace_nfs4_get_lock(request, state, cmd, err);
5307 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 &exception);
5309 } while (exception.retry);
5310 return err;
5311}
5312
5313static int do_vfs_lock(struct file *file, struct file_lock *fl)
5314{
5315 int res = 0;
5316 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5317 case FL_POSIX:
5318 res = posix_lock_file_wait(file, fl);
5319 break;
5320 case FL_FLOCK:
5321 res = flock_lock_file_wait(file, fl);
5322 break;
5323 default:
5324 BUG();
5325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 return res;
5327}
5328
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005329struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005330 struct nfs_locku_args arg;
5331 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005332 struct nfs4_lock_state *lsp;
5333 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005334 struct file_lock fl;
5335 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005336 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005337};
5338
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005339static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5340 struct nfs_open_context *ctx,
5341 struct nfs4_lock_state *lsp,
5342 struct nfs_seqid *seqid)
5343{
5344 struct nfs4_unlockdata *p;
5345 struct inode *inode = lsp->ls_state->inode;
5346
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005347 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005348 if (p == NULL)
5349 return NULL;
5350 p->arg.fh = NFS_FH(inode);
5351 p->arg.fl = &p->fl;
5352 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005353 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005354 p->arg.stateid = &lsp->ls_stateid;
5355 p->lsp = lsp;
5356 atomic_inc(&lsp->ls_count);
5357 /* Ensure we don't close file until we're done freeing locks! */
5358 p->ctx = get_nfs_open_context(ctx);
5359 memcpy(&p->fl, fl, sizeof(p->fl));
5360 p->server = NFS_SERVER(inode);
5361 return p;
5362}
5363
Trond Myklebust06f814a2006-01-03 09:55:07 +01005364static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005365{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005366 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005367 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005368 nfs4_put_lock_state(calldata->lsp);
5369 put_nfs_open_context(calldata->ctx);
5370 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005371}
5372
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005373static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005374{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005375 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005376
Trond Myklebust14516c32010-07-31 14:29:06 -04005377 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5378 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005379 switch (task->tk_status) {
5380 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005381 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5382 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005383 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005384 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005385 case -NFS4ERR_BAD_STATEID:
5386 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005387 case -NFS4ERR_STALE_STATEID:
5388 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005389 break;
5390 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005391 if (nfs4_async_handle_error(task, calldata->server,
5392 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005393 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005394 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005395 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005396}
5397
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005398static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005399{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005400 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005402 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005403 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005404 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005405 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005406 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005407 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005408 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005409 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005410 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005411 &calldata->res.seq_res,
5412 task) != 0)
5413 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005414 return;
5415out_no_action:
5416 task->tk_action = NULL;
5417out_wait:
5418 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419}
5420
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005421static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005422 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005423 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005424 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005425};
5426
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005427static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5428 struct nfs_open_context *ctx,
5429 struct nfs4_lock_state *lsp,
5430 struct nfs_seqid *seqid)
5431{
5432 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005433 struct rpc_message msg = {
5434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5435 .rpc_cred = ctx->cred,
5436 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005437 struct rpc_task_setup task_setup_data = {
5438 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005439 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005440 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005441 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005442 .flags = RPC_TASK_ASYNC,
5443 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005444
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005445 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5446 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5447
Frank Filz137d6ac2007-07-09 15:32:29 -07005448 /* Ensure this is an unlock - when canceling a lock, the
5449 * canceled lock is passed in, and it won't be an unlock.
5450 */
5451 fl->fl_type = F_UNLCK;
5452
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005453 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5454 if (data == NULL) {
5455 nfs_free_seqid(seqid);
5456 return ERR_PTR(-ENOMEM);
5457 }
5458
Chuck Levera9c92d62013-08-09 12:48:18 -04005459 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005460 msg.rpc_argp = &data->arg;
5461 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005462 task_setup_data.callback_data = data;
5463 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005464}
5465
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5467{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005468 struct inode *inode = state->inode;
5469 struct nfs4_state_owner *sp = state->owner;
5470 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005471 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005472 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005473 struct rpc_task *task;
5474 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005475 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
Trond Myklebust9b073572006-06-29 16:38:34 -04005477 status = nfs4_set_lock_state(state, request);
5478 /* Unlock _before_ we do the RPC call */
5479 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005480 /* Exclude nfs_delegation_claim_locks() */
5481 mutex_lock(&sp->so_delegreturn_mutex);
5482 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005483 down_read(&nfsi->rwsem);
5484 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5485 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005486 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005487 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005488 }
5489 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005490 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005491 if (status != 0)
5492 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005493 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005494 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005495 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5496 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005497 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005498 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005499 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005500 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005501 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005502 status = PTR_ERR(task);
5503 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005504 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005505 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005506 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005507out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005508 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005509 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005510 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511}
5512
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005513struct nfs4_lockdata {
5514 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005515 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005516 struct nfs4_lock_state *lsp;
5517 struct nfs_open_context *ctx;
5518 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005519 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005520 int rpc_status;
5521 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005522 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005523};
5524
5525static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005526 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5527 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005528{
5529 struct nfs4_lockdata *p;
5530 struct inode *inode = lsp->ls_state->inode;
5531 struct nfs_server *server = NFS_SERVER(inode);
5532
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005533 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005534 if (p == NULL)
5535 return NULL;
5536
5537 p->arg.fh = NFS_FH(inode);
5538 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005539 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005540 if (p->arg.open_seqid == NULL)
5541 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005542 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005543 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005544 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005545 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005546 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005547 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005548 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005549 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005550 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005551 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005552 atomic_inc(&lsp->ls_count);
5553 p->ctx = get_nfs_open_context(ctx);
5554 memcpy(&p->fl, fl, sizeof(p->fl));
5555 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005556out_free_seqid:
5557 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005558out_free:
5559 kfree(p);
5560 return NULL;
5561}
5562
5563static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5564{
5565 struct nfs4_lockdata *data = calldata;
5566 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
Harvey Harrison3110ff82008-05-02 13:42:44 -07005568 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005569 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005570 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005571 /* Do we need to do an open_to_lock_owner? */
5572 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005573 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005574 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005575 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005576 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005577 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005578 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005579 } else
5580 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005581 if (!nfs4_valid_open_stateid(state)) {
5582 data->rpc_status = -EBADF;
5583 task->tk_action = NULL;
5584 goto out_release_open_seqid;
5585 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005586 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005587 if (nfs4_setup_sequence(data->server,
5588 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005589 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005590 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005591 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005592out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005593 nfs_release_seqid(data->arg.open_seqid);
5594out_release_lock_seqid:
5595 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005596out_wait:
5597 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005598 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005599}
5600
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005601static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5602{
5603 struct nfs4_lockdata *data = calldata;
5604
Harvey Harrison3110ff82008-05-02 13:42:44 -07005605 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005606
Trond Myklebust14516c32010-07-31 14:29:06 -04005607 if (!nfs4_sequence_done(task, &data->res.seq_res))
5608 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005609
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005610 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005611 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005612 if (data->rpc_status == 0)
5613 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5614 else
5615 goto out;
5616 }
5617 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005618 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005619 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005620 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005621 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005622out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005623 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005624}
5625
5626static void nfs4_lock_release(void *calldata)
5627{
5628 struct nfs4_lockdata *data = calldata;
5629
Harvey Harrison3110ff82008-05-02 13:42:44 -07005630 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005631 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005632 if (data->cancelled != 0) {
5633 struct rpc_task *task;
5634 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5635 data->arg.lock_seqid);
5636 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005637 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005638 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005639 } else
5640 nfs_free_seqid(data->arg.lock_seqid);
5641 nfs4_put_lock_state(data->lsp);
5642 put_nfs_open_context(data->ctx);
5643 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005644 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005645}
5646
5647static const struct rpc_call_ops nfs4_lock_ops = {
5648 .rpc_call_prepare = nfs4_lock_prepare,
5649 .rpc_call_done = nfs4_lock_done,
5650 .rpc_release = nfs4_lock_release,
5651};
5652
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005653static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5654{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005655 switch (error) {
5656 case -NFS4ERR_ADMIN_REVOKED:
5657 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005658 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005659 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005660 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005661 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005662 break;
5663 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005664 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005665 case -NFS4ERR_EXPIRED:
5666 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005667 };
5668}
5669
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005670static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005671{
5672 struct nfs4_lockdata *data;
5673 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005674 struct rpc_message msg = {
5675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5676 .rpc_cred = state->owner->so_cred,
5677 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005678 struct rpc_task_setup task_setup_data = {
5679 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005680 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005681 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005682 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005683 .flags = RPC_TASK_ASYNC,
5684 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005685 int ret;
5686
Harvey Harrison3110ff82008-05-02 13:42:44 -07005687 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005688 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005689 fl->fl_u.nfs4_fl.owner,
5690 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005691 if (data == NULL)
5692 return -ENOMEM;
5693 if (IS_SETLKW(cmd))
5694 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005695 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005696 msg.rpc_argp = &data->arg;
5697 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005698 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005699 if (recovery_type > NFS_LOCK_NEW) {
5700 if (recovery_type == NFS_LOCK_RECLAIM)
5701 data->arg.reclaim = NFS_LOCK_RECLAIM;
5702 nfs4_set_sequence_privileged(&data->arg.seq_args);
5703 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005704 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005705 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005706 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005707 ret = nfs4_wait_for_completion_rpc_task(task);
5708 if (ret == 0) {
5709 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005710 if (ret)
5711 nfs4_handle_setlk_error(data->server, data->lsp,
5712 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005713 } else
5714 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005715 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005716 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005717 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718}
5719
5720static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5721{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005722 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005723 struct nfs4_exception exception = {
5724 .inode = state->inode,
5725 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005726 int err;
5727
5728 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005729 /* Cache the lock if possible... */
5730 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5731 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005732 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005733 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005734 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005735 break;
5736 nfs4_handle_exception(server, err, &exception);
5737 } while (exception.retry);
5738 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739}
5740
5741static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5742{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005743 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005744 struct nfs4_exception exception = {
5745 .inode = state->inode,
5746 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005747 int err;
5748
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005749 err = nfs4_set_lock_state(state, request);
5750 if (err != 0)
5751 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005752 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005753 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5754 return 0;
5755 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005756 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005757 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5758 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005759 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005760 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005761 switch (err) {
5762 default:
5763 goto out;
5764 case -NFS4ERR_GRACE:
5765 case -NFS4ERR_DELAY:
5766 nfs4_handle_exception(server, err, &exception);
5767 err = 0;
5768 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005769 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005770out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772}
5773
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005774#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005775/**
5776 * nfs41_check_expired_locks - possibly free a lock stateid
5777 *
5778 * @state: NFSv4 state for an inode
5779 *
5780 * Returns NFS_OK if recovery for this stateid is now finished.
5781 * Otherwise a negative NFS4ERR value is returned.
5782 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005783static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005784{
Chuck Levereb64cf92012-07-11 16:30:05 -04005785 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005786 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005787 struct nfs_server *server = NFS_SERVER(state->inode);
5788
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005789 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005790 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005791 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5792
5793 status = nfs41_test_stateid(server,
5794 &lsp->ls_stateid,
5795 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005796 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005797 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005798 /* Free the stateid unless the server
5799 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005800 if (status != -NFS4ERR_BAD_STATEID)
5801 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005802 &lsp->ls_stateid,
5803 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005804 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005805 ret = status;
5806 }
5807 }
5808 };
5809
5810 return ret;
5811}
5812
5813static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5814{
5815 int status = NFS_OK;
5816
5817 if (test_bit(LK_STATE_IN_USE, &state->flags))
5818 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005819 if (status != NFS_OK)
5820 status = nfs4_lock_expired(state, request);
5821 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005822}
5823#endif
5824
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5826{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005827 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005828 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005829 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005830 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005831 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005833 if ((fl_flags & FL_POSIX) &&
5834 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5835 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005836 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005837 status = nfs4_set_lock_state(state, request);
5838 if (status != 0)
5839 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005840 request->fl_flags |= FL_ACCESS;
5841 status = do_vfs_lock(request->fl_file, request);
5842 if (status < 0)
5843 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005844 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005845 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005846 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005847 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005848 request->fl_flags = fl_flags & ~FL_SLEEP;
5849 status = do_vfs_lock(request->fl_file, request);
5850 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005851 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005852 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5853 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005854 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005855 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005856 goto out;
5857 down_read(&nfsi->rwsem);
5858 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5859 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005860 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005861 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005862 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005863 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005864 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005865 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5866 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005867out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005868 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005869out:
5870 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 return status;
5872}
5873
5874static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5875{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005876 struct nfs4_exception exception = {
5877 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005878 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005879 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 int err;
5881
5882 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005883 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005884 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005885 if (err == -NFS4ERR_DENIED)
5886 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005888 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 } while (exception.retry);
5890 return err;
5891}
5892
5893static int
5894nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5895{
5896 struct nfs_open_context *ctx;
5897 struct nfs4_state *state;
5898 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5899 int status;
5900
5901 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005902 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 state = ctx->state;
5904
5905 if (request->fl_start < 0 || request->fl_end < 0)
5906 return -EINVAL;
5907
Trond Myklebustd9531262009-07-21 19:22:38 -04005908 if (IS_GETLK(cmd)) {
5909 if (state != NULL)
5910 return nfs4_proc_getlk(state, F_GETLK, request);
5911 return 0;
5912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913
5914 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5915 return -EINVAL;
5916
Trond Myklebustd9531262009-07-21 19:22:38 -04005917 if (request->fl_type == F_UNLCK) {
5918 if (state != NULL)
5919 return nfs4_proc_unlck(state, cmd, request);
5920 return 0;
5921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
Trond Myklebustd9531262009-07-21 19:22:38 -04005923 if (state == NULL)
5924 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005925 /*
5926 * Don't rely on the VFS having checked the file open mode,
5927 * since it won't do this for flock() locks.
5928 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005929 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005930 case F_RDLCK:
5931 if (!(filp->f_mode & FMODE_READ))
5932 return -EBADF;
5933 break;
5934 case F_WRLCK:
5935 if (!(filp->f_mode & FMODE_WRITE))
5936 return -EBADF;
5937 }
5938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 do {
5940 status = nfs4_proc_setlk(state, cmd, request);
5941 if ((status != -EAGAIN) || IS_SETLK(cmd))
5942 break;
5943 timeout = nfs4_set_lock_task_retry(timeout);
5944 status = -ERESTARTSYS;
5945 if (signalled())
5946 break;
5947 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 return status;
5949}
5950
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005951int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005952{
5953 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005954 int err;
5955
5956 err = nfs4_set_lock_state(state, fl);
5957 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005958 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005959 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005960 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005961}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005962
Trond Myklebustcf470c32012-03-07 13:49:12 -05005963struct nfs_release_lockowner_data {
5964 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005965 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005966 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005967 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04005968 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005969};
5970
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005971static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5972{
5973 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08005974 struct nfs_server *server = data->server;
5975 nfs40_setup_sequence(server, &data->args.seq_args,
5976 &data->res.seq_res, task);
5977 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04005978 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005979}
5980
5981static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5982{
5983 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04005984 struct nfs_server *server = data->server;
5985
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005986 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04005987
5988 switch (task->tk_status) {
5989 case 0:
5990 renew_lease(server, data->timestamp);
5991 break;
5992 case -NFS4ERR_STALE_CLIENTID:
5993 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08005994 nfs4_schedule_lease_recovery(server->nfs_client);
5995 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04005996 case -NFS4ERR_LEASE_MOVED:
5997 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10005998 if (nfs4_async_handle_error(task, server,
5999 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006000 rpc_restart_call_prepare(task);
6001 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006002}
6003
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006004static void nfs4_release_lockowner_release(void *calldata)
6005{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006006 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006007 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006008 kfree(calldata);
6009}
6010
Trond Myklebust17280172012-03-11 13:11:00 -04006011static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006012 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6013 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006014 .rpc_release = nfs4_release_lockowner_release,
6015};
6016
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006017static void
6018nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006019{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006020 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006021 struct rpc_message msg = {
6022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6023 };
6024
6025 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006026 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006027
Trond Myklebustcf470c32012-03-07 13:49:12 -05006028 data = kmalloc(sizeof(*data), GFP_NOFS);
6029 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006030 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006031 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006032 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006033 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6034 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6035 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006036
Trond Myklebustcf470c32012-03-07 13:49:12 -05006037 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006038 msg.rpc_resp = &data->res;
6039 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006040 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006041}
6042
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006043#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6044
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006045static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6046 const void *buf, size_t buflen,
6047 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006048{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006049 if (strcmp(key, "") != 0)
6050 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006051
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006052 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006053}
6054
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006055static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6056 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006057{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006058 if (strcmp(key, "") != 0)
6059 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006060
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006061 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006062}
6063
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006064static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6065 size_t list_len, const char *name,
6066 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006067{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006068 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006069
J. Bruce Fields096455a2006-03-20 23:23:42 -05006070 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
6071 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006072
6073 if (list && len <= list_len)
6074 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006075 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006076}
6077
David Quigleyc9bccef2013-05-22 12:50:45 -04006078#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6079static inline int nfs4_server_supports_labels(struct nfs_server *server)
6080{
6081 return server->caps & NFS_CAP_SECURITY_LABEL;
6082}
6083
6084static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6085 const void *buf, size_t buflen,
6086 int flags, int type)
6087{
6088 if (security_ismaclabel(key))
6089 return nfs4_set_security_label(dentry, buf, buflen);
6090
6091 return -EOPNOTSUPP;
6092}
6093
6094static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6095 void *buf, size_t buflen, int type)
6096{
6097 if (security_ismaclabel(key))
6098 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
6099 return -EOPNOTSUPP;
6100}
6101
6102static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6103 size_t list_len, const char *name,
6104 size_t name_len, int type)
6105{
6106 size_t len = 0;
6107
6108 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
6109 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
6110 if (list && len <= list_len)
6111 security_inode_listsecurity(dentry->d_inode, list, len);
6112 }
6113 return len;
6114}
6115
6116static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6117 .prefix = XATTR_SECURITY_PREFIX,
6118 .list = nfs4_xattr_list_nfs4_label,
6119 .get = nfs4_xattr_get_nfs4_label,
6120 .set = nfs4_xattr_set_nfs4_label,
6121};
6122#endif
6123
6124
Andy Adamson533eb462011-06-13 18:25:56 -04006125/*
6126 * nfs_fhget will use either the mounted_on_fileid or the fileid
6127 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006128static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6129{
Andy Adamson533eb462011-06-13 18:25:56 -04006130 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6131 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6132 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006133 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006134 return;
6135
6136 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006137 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006138 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6139 fattr->nlink = 2;
6140}
6141
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006142static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6143 const struct qstr *name,
6144 struct nfs4_fs_locations *fs_locations,
6145 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006146{
6147 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006148 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006149 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006150 };
6151 struct nfs4_fs_locations_arg args = {
6152 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006153 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006154 .page = page,
6155 .bitmask = bitmask,
6156 };
Benny Halevy22958462009-04-01 09:22:02 -04006157 struct nfs4_fs_locations_res res = {
6158 .fs_locations = fs_locations,
6159 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006160 struct rpc_message msg = {
6161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6162 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006163 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006164 };
6165 int status;
6166
Harvey Harrison3110ff82008-05-02 13:42:44 -07006167 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006168
6169 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6170 * is not supported */
6171 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6172 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6173 else
6174 bitmask[0] |= FATTR4_WORD0_FILEID;
6175
Trond Myklebustc228fd32007-01-13 02:28:11 -05006176 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006177 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006178 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006179 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006180 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006181 return status;
6182}
6183
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006184int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6185 const struct qstr *name,
6186 struct nfs4_fs_locations *fs_locations,
6187 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006188{
6189 struct nfs4_exception exception = { };
6190 int err;
6191 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006192 err = _nfs4_proc_fs_locations(client, dir, name,
6193 fs_locations, page);
6194 trace_nfs4_get_fs_locations(dir, name, err);
6195 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006196 &exception);
6197 } while (exception.retry);
6198 return err;
6199}
6200
Chuck Leverb03d7352013-10-17 14:12:50 -04006201/*
6202 * This operation also signals the server that this client is
6203 * performing migration recovery. The server can stop returning
6204 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6205 * appended to this compound to identify the client ID which is
6206 * performing recovery.
6207 */
6208static int _nfs40_proc_get_locations(struct inode *inode,
6209 struct nfs4_fs_locations *locations,
6210 struct page *page, struct rpc_cred *cred)
6211{
6212 struct nfs_server *server = NFS_SERVER(inode);
6213 struct rpc_clnt *clnt = server->client;
6214 u32 bitmask[2] = {
6215 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6216 };
6217 struct nfs4_fs_locations_arg args = {
6218 .clientid = server->nfs_client->cl_clientid,
6219 .fh = NFS_FH(inode),
6220 .page = page,
6221 .bitmask = bitmask,
6222 .migration = 1, /* skip LOOKUP */
6223 .renew = 1, /* append RENEW */
6224 };
6225 struct nfs4_fs_locations_res res = {
6226 .fs_locations = locations,
6227 .migration = 1,
6228 .renew = 1,
6229 };
6230 struct rpc_message msg = {
6231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6232 .rpc_argp = &args,
6233 .rpc_resp = &res,
6234 .rpc_cred = cred,
6235 };
6236 unsigned long now = jiffies;
6237 int status;
6238
6239 nfs_fattr_init(&locations->fattr);
6240 locations->server = server;
6241 locations->nlocations = 0;
6242
6243 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6244 nfs4_set_sequence_privileged(&args.seq_args);
6245 status = nfs4_call_sync_sequence(clnt, server, &msg,
6246 &args.seq_args, &res.seq_res);
6247 if (status)
6248 return status;
6249
6250 renew_lease(server, now);
6251 return 0;
6252}
6253
6254#ifdef CONFIG_NFS_V4_1
6255
6256/*
6257 * This operation also signals the server that this client is
6258 * performing migration recovery. The server can stop asserting
6259 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6260 * performing this operation is identified in the SEQUENCE
6261 * operation in this compound.
6262 *
6263 * When the client supports GETATTR(fs_locations_info), it can
6264 * be plumbed in here.
6265 */
6266static int _nfs41_proc_get_locations(struct inode *inode,
6267 struct nfs4_fs_locations *locations,
6268 struct page *page, struct rpc_cred *cred)
6269{
6270 struct nfs_server *server = NFS_SERVER(inode);
6271 struct rpc_clnt *clnt = server->client;
6272 u32 bitmask[2] = {
6273 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6274 };
6275 struct nfs4_fs_locations_arg args = {
6276 .fh = NFS_FH(inode),
6277 .page = page,
6278 .bitmask = bitmask,
6279 .migration = 1, /* skip LOOKUP */
6280 };
6281 struct nfs4_fs_locations_res res = {
6282 .fs_locations = locations,
6283 .migration = 1,
6284 };
6285 struct rpc_message msg = {
6286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6287 .rpc_argp = &args,
6288 .rpc_resp = &res,
6289 .rpc_cred = cred,
6290 };
6291 int status;
6292
6293 nfs_fattr_init(&locations->fattr);
6294 locations->server = server;
6295 locations->nlocations = 0;
6296
6297 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6298 nfs4_set_sequence_privileged(&args.seq_args);
6299 status = nfs4_call_sync_sequence(clnt, server, &msg,
6300 &args.seq_args, &res.seq_res);
6301 if (status == NFS4_OK &&
6302 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6303 status = -NFS4ERR_LEASE_MOVED;
6304 return status;
6305}
6306
6307#endif /* CONFIG_NFS_V4_1 */
6308
6309/**
6310 * nfs4_proc_get_locations - discover locations for a migrated FSID
6311 * @inode: inode on FSID that is migrating
6312 * @locations: result of query
6313 * @page: buffer
6314 * @cred: credential to use for this operation
6315 *
6316 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6317 * operation failed, or a negative errno if a local error occurred.
6318 *
6319 * On success, "locations" is filled in, but if the server has
6320 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6321 * asserted.
6322 *
6323 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6324 * from this client that require migration recovery.
6325 */
6326int nfs4_proc_get_locations(struct inode *inode,
6327 struct nfs4_fs_locations *locations,
6328 struct page *page, struct rpc_cred *cred)
6329{
6330 struct nfs_server *server = NFS_SERVER(inode);
6331 struct nfs_client *clp = server->nfs_client;
6332 const struct nfs4_mig_recovery_ops *ops =
6333 clp->cl_mvops->mig_recovery_ops;
6334 struct nfs4_exception exception = { };
6335 int status;
6336
6337 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6338 (unsigned long long)server->fsid.major,
6339 (unsigned long long)server->fsid.minor,
6340 clp->cl_hostname);
6341 nfs_display_fhandle(NFS_FH(inode), __func__);
6342
6343 do {
6344 status = ops->get_locations(inode, locations, page, cred);
6345 if (status != -NFS4ERR_DELAY)
6346 break;
6347 nfs4_handle_exception(server, status, &exception);
6348 } while (exception.retry);
6349 return status;
6350}
6351
Chuck Lever44c99932013-10-17 14:13:30 -04006352/*
6353 * This operation also signals the server that this client is
6354 * performing "lease moved" recovery. The server can stop
6355 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6356 * is appended to this compound to identify the client ID which is
6357 * performing recovery.
6358 */
6359static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6360{
6361 struct nfs_server *server = NFS_SERVER(inode);
6362 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6363 struct rpc_clnt *clnt = server->client;
6364 struct nfs4_fsid_present_arg args = {
6365 .fh = NFS_FH(inode),
6366 .clientid = clp->cl_clientid,
6367 .renew = 1, /* append RENEW */
6368 };
6369 struct nfs4_fsid_present_res res = {
6370 .renew = 1,
6371 };
6372 struct rpc_message msg = {
6373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6374 .rpc_argp = &args,
6375 .rpc_resp = &res,
6376 .rpc_cred = cred,
6377 };
6378 unsigned long now = jiffies;
6379 int status;
6380
6381 res.fh = nfs_alloc_fhandle();
6382 if (res.fh == NULL)
6383 return -ENOMEM;
6384
6385 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6386 nfs4_set_sequence_privileged(&args.seq_args);
6387 status = nfs4_call_sync_sequence(clnt, server, &msg,
6388 &args.seq_args, &res.seq_res);
6389 nfs_free_fhandle(res.fh);
6390 if (status)
6391 return status;
6392
6393 do_renew_lease(clp, now);
6394 return 0;
6395}
6396
6397#ifdef CONFIG_NFS_V4_1
6398
6399/*
6400 * This operation also signals the server that this client is
6401 * performing "lease moved" recovery. The server can stop asserting
6402 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6403 * this operation is identified in the SEQUENCE operation in this
6404 * compound.
6405 */
6406static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6407{
6408 struct nfs_server *server = NFS_SERVER(inode);
6409 struct rpc_clnt *clnt = server->client;
6410 struct nfs4_fsid_present_arg args = {
6411 .fh = NFS_FH(inode),
6412 };
6413 struct nfs4_fsid_present_res res = {
6414 };
6415 struct rpc_message msg = {
6416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6417 .rpc_argp = &args,
6418 .rpc_resp = &res,
6419 .rpc_cred = cred,
6420 };
6421 int status;
6422
6423 res.fh = nfs_alloc_fhandle();
6424 if (res.fh == NULL)
6425 return -ENOMEM;
6426
6427 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6428 nfs4_set_sequence_privileged(&args.seq_args);
6429 status = nfs4_call_sync_sequence(clnt, server, &msg,
6430 &args.seq_args, &res.seq_res);
6431 nfs_free_fhandle(res.fh);
6432 if (status == NFS4_OK &&
6433 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6434 status = -NFS4ERR_LEASE_MOVED;
6435 return status;
6436}
6437
6438#endif /* CONFIG_NFS_V4_1 */
6439
6440/**
6441 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6442 * @inode: inode on FSID to check
6443 * @cred: credential to use for this operation
6444 *
6445 * Server indicates whether the FSID is present, moved, or not
6446 * recognized. This operation is necessary to clear a LEASE_MOVED
6447 * condition for this client ID.
6448 *
6449 * Returns NFS4_OK if the FSID is present on this server,
6450 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6451 * NFS4ERR code if some error occurred on the server, or a
6452 * negative errno if a local failure occurred.
6453 */
6454int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6455{
6456 struct nfs_server *server = NFS_SERVER(inode);
6457 struct nfs_client *clp = server->nfs_client;
6458 const struct nfs4_mig_recovery_ops *ops =
6459 clp->cl_mvops->mig_recovery_ops;
6460 struct nfs4_exception exception = { };
6461 int status;
6462
6463 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6464 (unsigned long long)server->fsid.major,
6465 (unsigned long long)server->fsid.minor,
6466 clp->cl_hostname);
6467 nfs_display_fhandle(NFS_FH(inode), __func__);
6468
6469 do {
6470 status = ops->fsid_present(inode, cred);
6471 if (status != -NFS4ERR_DELAY)
6472 break;
6473 nfs4_handle_exception(server, status, &exception);
6474 } while (exception.retry);
6475 return status;
6476}
6477
Andy Adamson5ec16a82013-08-08 10:57:55 -04006478/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006479 * If 'use_integrity' is true and the state managment nfs_client
6480 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6481 * and the machine credential as per RFC3530bis and RFC5661 Security
6482 * Considerations sections. Otherwise, just use the user cred with the
6483 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006484 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006485static 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 +00006486{
6487 int status;
6488 struct nfs4_secinfo_arg args = {
6489 .dir_fh = NFS_FH(dir),
6490 .name = name,
6491 };
6492 struct nfs4_secinfo_res res = {
6493 .flavors = flavors,
6494 };
6495 struct rpc_message msg = {
6496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6497 .rpc_argp = &args,
6498 .rpc_resp = &res,
6499 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006500 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006501 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006502
6503 if (use_integrity) {
6504 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006505 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6506 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006507 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006508
6509 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006510
6511 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6512 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6513
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006514 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6515 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006516 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006517
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006518 if (cred)
6519 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006520
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006521 return status;
6522}
6523
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006524int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6525 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006526{
6527 struct nfs4_exception exception = { };
6528 int err;
6529 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006530 err = -NFS4ERR_WRONGSEC;
6531
6532 /* try to use integrity protection with machine cred */
6533 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6534 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6535
6536 /*
6537 * if unable to use integrity protection, or SECINFO with
6538 * integrity protection returns NFS4ERR_WRONGSEC (which is
6539 * disallowed by spec, but exists in deployed servers) use
6540 * the current filesystem's rpc_client and the user cred.
6541 */
6542 if (err == -NFS4ERR_WRONGSEC)
6543 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6544
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006545 trace_nfs4_secinfo(dir, name, err);
6546 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006547 &exception);
6548 } while (exception.retry);
6549 return err;
6550}
6551
Andy Adamson557134a2009-04-01 09:21:53 -04006552#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006553/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006554 * Check the exchange flags returned by the server for invalid flags, having
6555 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6556 * DS flags set.
6557 */
6558static int nfs4_check_cl_exchange_flags(u32 flags)
6559{
6560 if (flags & ~EXCHGID4_FLAG_MASK_R)
6561 goto out_inval;
6562 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6563 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6564 goto out_inval;
6565 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6566 goto out_inval;
6567 return NFS_OK;
6568out_inval:
6569 return -NFS4ERR_INVAL;
6570}
6571
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006572static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006573nfs41_same_server_scope(struct nfs41_server_scope *a,
6574 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006575{
6576 if (a->server_scope_sz == b->server_scope_sz &&
6577 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6578 return true;
6579
6580 return false;
6581}
6582
Andy Adamson357f54d2010-12-14 10:11:57 -05006583/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006584 * nfs4_proc_bind_conn_to_session()
6585 *
6586 * The 4.1 client currently uses the same TCP connection for the
6587 * fore and backchannel.
6588 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006589int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006590{
6591 int status;
6592 struct nfs41_bind_conn_to_session_res res;
6593 struct rpc_message msg = {
6594 .rpc_proc =
6595 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6596 .rpc_argp = clp,
6597 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006598 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006599 };
6600
6601 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006602
6603 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6604 if (unlikely(res.session == NULL)) {
6605 status = -ENOMEM;
6606 goto out;
6607 }
6608
6609 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006610 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006611 if (status == 0) {
6612 if (memcmp(res.session->sess_id.data,
6613 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6614 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6615 status = -EIO;
6616 goto out_session;
6617 }
6618 if (res.dir != NFS4_CDFS4_BOTH) {
6619 dprintk("NFS: %s: Unexpected direction from server\n",
6620 __func__);
6621 status = -EIO;
6622 goto out_session;
6623 }
6624 if (res.use_conn_in_rdma_mode) {
6625 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6626 __func__);
6627 status = -EIO;
6628 goto out_session;
6629 }
6630 }
6631out_session:
6632 kfree(res.session);
6633out:
6634 dprintk("<-- %s status= %d\n", __func__, status);
6635 return status;
6636}
6637
6638/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006639 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6640 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006641 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006642static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6643 .how = SP4_MACH_CRED,
6644 .enforce.u.words = {
6645 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6646 1 << (OP_EXCHANGE_ID - 32) |
6647 1 << (OP_CREATE_SESSION - 32) |
6648 1 << (OP_DESTROY_SESSION - 32) |
6649 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006650 },
6651 .allow.u.words = {
6652 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006653 1 << (OP_LOCKU) |
6654 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006655 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006656 1 << (OP_SECINFO_NO_NAME - 32) |
6657 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006658 1 << (OP_FREE_STATEID - 32) |
6659 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006660 }
6661};
6662
6663/*
6664 * Select the state protection mode for client `clp' given the server results
6665 * from exchange_id in `sp'.
6666 *
6667 * Returns 0 on success, negative errno otherwise.
6668 */
6669static int nfs4_sp4_select_mode(struct nfs_client *clp,
6670 struct nfs41_state_protection *sp)
6671{
6672 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6673 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6674 1 << (OP_EXCHANGE_ID - 32) |
6675 1 << (OP_CREATE_SESSION - 32) |
6676 1 << (OP_DESTROY_SESSION - 32) |
6677 1 << (OP_DESTROY_CLIENTID - 32)
6678 };
6679 unsigned int i;
6680
6681 if (sp->how == SP4_MACH_CRED) {
6682 /* Print state protect result */
6683 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6684 for (i = 0; i <= LAST_NFS4_OP; i++) {
6685 if (test_bit(i, sp->enforce.u.longs))
6686 dfprintk(MOUNT, " enforce op %d\n", i);
6687 if (test_bit(i, sp->allow.u.longs))
6688 dfprintk(MOUNT, " allow op %d\n", i);
6689 }
6690
6691 /* make sure nothing is on enforce list that isn't supported */
6692 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6693 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6694 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6695 return -EINVAL;
6696 }
6697 }
6698
6699 /*
6700 * Minimal mode - state operations are allowed to use machine
6701 * credential. Note this already happens by default, so the
6702 * client doesn't have to do anything more than the negotiation.
6703 *
6704 * NOTE: we don't care if EXCHANGE_ID is in the list -
6705 * we're already using the machine cred for exchange_id
6706 * and will never use a different cred.
6707 */
6708 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6709 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6710 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6711 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6712 dfprintk(MOUNT, "sp4_mach_cred:\n");
6713 dfprintk(MOUNT, " minimal mode enabled\n");
6714 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6715 } else {
6716 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6717 return -EINVAL;
6718 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006719
6720 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6721 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6722 dfprintk(MOUNT, " cleanup mode enabled\n");
6723 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6724 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006725
6726 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6727 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6728 dfprintk(MOUNT, " secinfo mode enabled\n");
6729 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6730 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006731
6732 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6733 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6734 dfprintk(MOUNT, " stateid mode enabled\n");
6735 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6736 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006737
6738 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6739 dfprintk(MOUNT, " write mode enabled\n");
6740 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6741 }
6742
6743 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6744 dfprintk(MOUNT, " commit mode enabled\n");
6745 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6746 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006747 }
6748
6749 return 0;
6750}
6751
6752/*
6753 * _nfs4_proc_exchange_id()
6754 *
6755 * Wrapper for EXCHANGE_ID operation.
6756 */
6757static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6758 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006759{
6760 nfs4_verifier verifier;
6761 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006762 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006763 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006764#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006765 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006766 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6767 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6768#else
6769 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6770 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6771#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006772 };
6773 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006774 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006775 };
6776 int status;
6777 struct rpc_message msg = {
6778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6779 .rpc_argp = &args,
6780 .rpc_resp = &res,
6781 .rpc_cred = cred,
6782 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006783
Chuck Leverf0920752012-05-21 22:45:41 -04006784 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006785 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6786 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006787 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6788 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6789 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006790
Chuck Leveracdeb692012-05-21 22:46:16 -04006791 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006792 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006793 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006794 status = -ENOMEM;
6795 goto out;
6796 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006797
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006798 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006799 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006800 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006801 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006802 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006803 }
6804
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006805 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006806 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006807 status = -ENOMEM;
6808 goto out_server_scope;
6809 }
6810
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006811 switch (sp4_how) {
6812 case SP4_NONE:
6813 args.state_protect.how = SP4_NONE;
6814 break;
6815
6816 case SP4_MACH_CRED:
6817 args.state_protect = nfs4_sp4_mach_cred_request;
6818 break;
6819
6820 default:
6821 /* unsupported! */
6822 WARN_ON_ONCE(1);
6823 status = -EINVAL;
6824 goto out_server_scope;
6825 }
6826
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006827 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006828 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006829 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006830 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006831
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006832 if (status == 0)
6833 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6834
Chuck Lever177313f2012-05-21 22:44:58 -04006835 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006836 clp->cl_clientid = res.clientid;
6837 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6838 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6839 clp->cl_seqid = res.seqid;
6840
Chuck Leveracdeb692012-05-21 22:46:16 -04006841 kfree(clp->cl_serverowner);
6842 clp->cl_serverowner = res.server_owner;
6843 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006844
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006845 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006846 kfree(clp->cl_implid);
6847 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006848
Chuck Lever177313f2012-05-21 22:44:58 -04006849 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006850 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006851 res.server_scope)) {
6852 dprintk("%s: server_scope mismatch detected\n",
6853 __func__);
6854 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006855 kfree(clp->cl_serverscope);
6856 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006857 }
6858
Chuck Lever177313f2012-05-21 22:44:58 -04006859 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006860 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006861 goto out;
6862 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006863 } else
6864 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006865
Chuck Leveracdeb692012-05-21 22:46:16 -04006866out_server_owner:
6867 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006868out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006869 kfree(res.server_scope);
6870out:
Chuck Lever177313f2012-05-21 22:44:58 -04006871 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006872 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006873 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006874 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006875 clp->cl_implid->date.seconds,
6876 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006877 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006878 return status;
6879}
6880
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006881/*
6882 * nfs4_proc_exchange_id()
6883 *
6884 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6885 *
6886 * Since the clientid has expired, all compounds using sessions
6887 * associated with the stale clientid will be returning
6888 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6889 * be in some phase of session reset.
6890 *
6891 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6892 */
6893int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6894{
6895 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6896 int status;
6897
6898 /* try SP4_MACH_CRED if krb5i/p */
6899 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6900 authflavor == RPC_AUTH_GSS_KRB5P) {
6901 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6902 if (!status)
6903 return 0;
6904 }
6905
6906 /* try SP4_NONE */
6907 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6908}
6909
Trond Myklebust66245532012-05-25 17:18:09 -04006910static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6911 struct rpc_cred *cred)
6912{
6913 struct rpc_message msg = {
6914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6915 .rpc_argp = clp,
6916 .rpc_cred = cred,
6917 };
6918 int status;
6919
6920 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006921 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006922 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006923 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006924 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6925 return status;
6926}
6927
6928static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6929 struct rpc_cred *cred)
6930{
6931 unsigned int loop;
6932 int ret;
6933
6934 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6935 ret = _nfs4_proc_destroy_clientid(clp, cred);
6936 switch (ret) {
6937 case -NFS4ERR_DELAY:
6938 case -NFS4ERR_CLIENTID_BUSY:
6939 ssleep(1);
6940 break;
6941 default:
6942 return ret;
6943 }
6944 }
6945 return 0;
6946}
6947
6948int nfs4_destroy_clientid(struct nfs_client *clp)
6949{
6950 struct rpc_cred *cred;
6951 int ret = 0;
6952
6953 if (clp->cl_mvops->minor_version < 1)
6954 goto out;
6955 if (clp->cl_exchange_flags == 0)
6956 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006957 if (clp->cl_preserve_clid)
6958 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006959 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006960 ret = nfs4_proc_destroy_clientid(clp, cred);
6961 if (cred)
6962 put_rpccred(cred);
6963 switch (ret) {
6964 case 0:
6965 case -NFS4ERR_STALE_CLIENTID:
6966 clp->cl_exchange_flags = 0;
6967 }
6968out:
6969 return ret;
6970}
6971
Andy Adamson2050f0c2009-04-01 09:22:30 -04006972struct nfs4_get_lease_time_data {
6973 struct nfs4_get_lease_time_args *args;
6974 struct nfs4_get_lease_time_res *res;
6975 struct nfs_client *clp;
6976};
6977
6978static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6979 void *calldata)
6980{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006981 struct nfs4_get_lease_time_data *data =
6982 (struct nfs4_get_lease_time_data *)calldata;
6983
6984 dprintk("--> %s\n", __func__);
6985 /* just setup sequence, do not trigger session recovery
6986 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006987 nfs41_setup_sequence(data->clp->cl_session,
6988 &data->args->la_seq_args,
6989 &data->res->lr_seq_res,
6990 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006991 dprintk("<-- %s\n", __func__);
6992}
6993
6994/*
6995 * Called from nfs4_state_manager thread for session setup, so don't recover
6996 * from sequence operation or clientid errors.
6997 */
6998static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6999{
7000 struct nfs4_get_lease_time_data *data =
7001 (struct nfs4_get_lease_time_data *)calldata;
7002
7003 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007004 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7005 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007006 switch (task->tk_status) {
7007 case -NFS4ERR_DELAY:
7008 case -NFS4ERR_GRACE:
7009 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7010 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7011 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007012 /* fall through */
7013 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007014 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007015 return;
7016 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007017 dprintk("<-- %s\n", __func__);
7018}
7019
Trond Myklebust17280172012-03-11 13:11:00 -04007020static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007021 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7022 .rpc_call_done = nfs4_get_lease_time_done,
7023};
7024
7025int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7026{
7027 struct rpc_task *task;
7028 struct nfs4_get_lease_time_args args;
7029 struct nfs4_get_lease_time_res res = {
7030 .lr_fsinfo = fsinfo,
7031 };
7032 struct nfs4_get_lease_time_data data = {
7033 .args = &args,
7034 .res = &res,
7035 .clp = clp,
7036 };
7037 struct rpc_message msg = {
7038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7039 .rpc_argp = &args,
7040 .rpc_resp = &res,
7041 };
7042 struct rpc_task_setup task_setup = {
7043 .rpc_client = clp->cl_rpcclient,
7044 .rpc_message = &msg,
7045 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007046 .callback_data = &data,
7047 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007048 };
7049 int status;
7050
Chuck Levera9c92d62013-08-09 12:48:18 -04007051 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007052 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007053 dprintk("--> %s\n", __func__);
7054 task = rpc_run_task(&task_setup);
7055
7056 if (IS_ERR(task))
7057 status = PTR_ERR(task);
7058 else {
7059 status = task->tk_status;
7060 rpc_put_task(task);
7061 }
7062 dprintk("<-- %s return %d\n", __func__, status);
7063
7064 return status;
7065}
7066
Andy Adamsonfc931582009-04-01 09:22:31 -04007067/*
7068 * Initialize the values to be used by the client in CREATE_SESSION
7069 * If nfs4_init_session set the fore channel request and response sizes,
7070 * use them.
7071 *
7072 * Set the back channel max_resp_sz_cached to zero to force the client to
7073 * always set csa_cachethis to FALSE because the current implementation
7074 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7075 */
7076static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7077{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007078 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007079
Andy Adamson18aad3d2013-06-26 12:21:49 -04007080 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7081 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7082
Andy Adamsonfc931582009-04-01 09:22:31 -04007083 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007084 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7085 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007086 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007087 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007088
7089 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007090 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007091 __func__,
7092 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007093 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007094
7095 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007096 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7097 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7098 args->bc_attrs.max_resp_sz_cached = 0;
7099 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7100 args->bc_attrs.max_reqs = 1;
7101
7102 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7103 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7104 __func__,
7105 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7106 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7107 args->bc_attrs.max_reqs);
7108}
7109
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007110static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04007111{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007112 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7113 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
7114
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007115 if (rcvd->max_resp_sz > sent->max_resp_sz)
7116 return -EINVAL;
7117 /*
7118 * Our requested max_ops is the minimum we need; we're not
7119 * prepared to break up compounds into smaller pieces than that.
7120 * So, no point even trying to continue if the server won't
7121 * cooperate:
7122 */
7123 if (rcvd->max_ops < sent->max_ops)
7124 return -EINVAL;
7125 if (rcvd->max_reqs == 0)
7126 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007127 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7128 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007129 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007130}
7131
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007132static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7133{
7134 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7135 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007136
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007137 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7138 return -EINVAL;
7139 if (rcvd->max_resp_sz < sent->max_resp_sz)
7140 return -EINVAL;
7141 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7142 return -EINVAL;
7143 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007144 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007145 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007146 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007147 return -EINVAL;
7148 return 0;
7149}
Andy Adamson8d353012009-04-01 09:22:32 -04007150
Andy Adamson8d353012009-04-01 09:22:32 -04007151static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7152 struct nfs4_session *session)
7153{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007154 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007155
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007156 ret = nfs4_verify_fore_channel_attrs(args, session);
7157 if (ret)
7158 return ret;
7159 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04007160}
7161
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007162static int _nfs4_proc_create_session(struct nfs_client *clp,
7163 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007164{
7165 struct nfs4_session *session = clp->cl_session;
7166 struct nfs41_create_session_args args = {
7167 .client = clp,
7168 .cb_program = NFS4_CALLBACK,
7169 };
7170 struct nfs41_create_session_res res = {
7171 .client = clp,
7172 };
7173 struct rpc_message msg = {
7174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7175 .rpc_argp = &args,
7176 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007177 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007178 };
7179 int status;
7180
7181 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007182 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007183
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007184 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007185 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007186
Trond Myklebust43095d32012-11-20 11:13:12 -05007187 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007188 /* Verify the session's negotiated channel_attrs values */
7189 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04007190 /* Increment the clientid slot sequence id */
7191 clp->cl_seqid++;
7192 }
7193
7194 return status;
7195}
7196
7197/*
7198 * Issues a CREATE_SESSION operation to the server.
7199 * It is the responsibility of the caller to verify the session is
7200 * expired before calling this routine.
7201 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007202int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007203{
7204 int status;
7205 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007206 struct nfs4_session *session = clp->cl_session;
7207
7208 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7209
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007210 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007211 if (status)
7212 goto out;
7213
Andy Adamsonaacd5532011-11-09 13:58:21 -05007214 /* Init or reset the session slot tables */
7215 status = nfs4_setup_session_slot_tables(session);
7216 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007217 if (status)
7218 goto out;
7219
7220 ptr = (unsigned *)&session->sess_id.data[0];
7221 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7222 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007223out:
7224 dprintk("<-- %s\n", __func__);
7225 return status;
7226}
7227
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007228/*
7229 * Issue the over-the-wire RPC DESTROY_SESSION.
7230 * The caller must serialize access to this routine.
7231 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007232int nfs4_proc_destroy_session(struct nfs4_session *session,
7233 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007234{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007235 struct rpc_message msg = {
7236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7237 .rpc_argp = session,
7238 .rpc_cred = cred,
7239 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007240 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007241
7242 dprintk("--> nfs4_proc_destroy_session\n");
7243
7244 /* session is still being setup */
7245 if (session->clp->cl_cons_state != NFS_CS_READY)
7246 return status;
7247
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007248 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007249 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007250
7251 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007252 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007253 "Session has been destroyed regardless...\n", status);
7254
7255 dprintk("<-- nfs4_proc_destroy_session\n");
7256 return status;
7257}
7258
Trond Myklebust7b38c362012-05-23 13:23:31 -04007259/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007260 * Renew the cl_session lease.
7261 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007262struct nfs4_sequence_data {
7263 struct nfs_client *clp;
7264 struct nfs4_sequence_args args;
7265 struct nfs4_sequence_res res;
7266};
7267
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007268static void nfs41_sequence_release(void *data)
7269{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007270 struct nfs4_sequence_data *calldata = data;
7271 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007272
Alexandros Batsakis71358402010-02-05 03:45:05 -08007273 if (atomic_read(&clp->cl_count) > 1)
7274 nfs4_schedule_state_renewal(clp);
7275 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007276 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007277}
7278
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007279static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7280{
7281 switch(task->tk_status) {
7282 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007283 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7284 return -EAGAIN;
7285 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007286 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007287 }
7288 return 0;
7289}
7290
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007291static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007292{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007293 struct nfs4_sequence_data *calldata = data;
7294 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007295
Trond Myklebust14516c32010-07-31 14:29:06 -04007296 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7297 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007298
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007299 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007300 if (task->tk_status < 0) {
7301 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007302 if (atomic_read(&clp->cl_count) == 1)
7303 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007304
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007305 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7306 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007307 return;
7308 }
7309 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007310 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007311out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007312 dprintk("<-- %s\n", __func__);
7313}
7314
7315static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7316{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007317 struct nfs4_sequence_data *calldata = data;
7318 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007319 struct nfs4_sequence_args *args;
7320 struct nfs4_sequence_res *res;
7321
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007322 args = task->tk_msg.rpc_argp;
7323 res = task->tk_msg.rpc_resp;
7324
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007325 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007326}
7327
7328static const struct rpc_call_ops nfs41_sequence_ops = {
7329 .rpc_call_done = nfs41_sequence_call_done,
7330 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007331 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007332};
7333
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007334static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7335 struct rpc_cred *cred,
7336 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007337{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007338 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007339 struct rpc_message msg = {
7340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7341 .rpc_cred = cred,
7342 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007343 struct rpc_task_setup task_setup_data = {
7344 .rpc_client = clp->cl_rpcclient,
7345 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007346 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007347 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007348 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007349
Alexandros Batsakis71358402010-02-05 03:45:05 -08007350 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007351 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007352 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007353 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007354 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007355 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007356 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007357 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007358 if (is_privileged)
7359 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007360 msg.rpc_argp = &calldata->args;
7361 msg.rpc_resp = &calldata->res;
7362 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007363 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007364
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007365 return rpc_run_task(&task_setup_data);
7366}
7367
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007368static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007369{
7370 struct rpc_task *task;
7371 int ret = 0;
7372
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007373 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007374 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007375 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007376 if (IS_ERR(task))
7377 ret = PTR_ERR(task);
7378 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007379 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007380 dprintk("<-- %s status=%d\n", __func__, ret);
7381 return ret;
7382}
7383
7384static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7385{
7386 struct rpc_task *task;
7387 int ret;
7388
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007389 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007390 if (IS_ERR(task)) {
7391 ret = PTR_ERR(task);
7392 goto out;
7393 }
7394 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007395 if (!ret) {
7396 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7397
7398 if (task->tk_status == 0)
7399 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007400 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007401 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007402 rpc_put_task(task);
7403out:
7404 dprintk("<-- %s status=%d\n", __func__, ret);
7405 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007406}
7407
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007408struct nfs4_reclaim_complete_data {
7409 struct nfs_client *clp;
7410 struct nfs41_reclaim_complete_args arg;
7411 struct nfs41_reclaim_complete_res res;
7412};
7413
7414static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7415{
7416 struct nfs4_reclaim_complete_data *calldata = data;
7417
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007418 nfs41_setup_sequence(calldata->clp->cl_session,
7419 &calldata->arg.seq_args,
7420 &calldata->res.seq_res,
7421 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007422}
7423
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007424static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7425{
7426 switch(task->tk_status) {
7427 case 0:
7428 case -NFS4ERR_COMPLETE_ALREADY:
7429 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7430 break;
7431 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007432 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007433 /* fall through */
7434 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007435 return -EAGAIN;
7436 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007437 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007438 }
7439 return 0;
7440}
7441
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007442static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7443{
7444 struct nfs4_reclaim_complete_data *calldata = data;
7445 struct nfs_client *clp = calldata->clp;
7446 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7447
7448 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007449 if (!nfs41_sequence_done(task, res))
7450 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007451
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007452 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007453 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7454 rpc_restart_call_prepare(task);
7455 return;
7456 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007457 dprintk("<-- %s\n", __func__);
7458}
7459
7460static void nfs4_free_reclaim_complete_data(void *data)
7461{
7462 struct nfs4_reclaim_complete_data *calldata = data;
7463
7464 kfree(calldata);
7465}
7466
7467static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7468 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7469 .rpc_call_done = nfs4_reclaim_complete_done,
7470 .rpc_release = nfs4_free_reclaim_complete_data,
7471};
7472
7473/*
7474 * Issue a global reclaim complete.
7475 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007476static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7477 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007478{
7479 struct nfs4_reclaim_complete_data *calldata;
7480 struct rpc_task *task;
7481 struct rpc_message msg = {
7482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007483 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007484 };
7485 struct rpc_task_setup task_setup_data = {
7486 .rpc_client = clp->cl_rpcclient,
7487 .rpc_message = &msg,
7488 .callback_ops = &nfs4_reclaim_complete_call_ops,
7489 .flags = RPC_TASK_ASYNC,
7490 };
7491 int status = -ENOMEM;
7492
7493 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007494 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007495 if (calldata == NULL)
7496 goto out;
7497 calldata->clp = clp;
7498 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007499
Chuck Levera9c92d62013-08-09 12:48:18 -04007500 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007501 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007502 msg.rpc_argp = &calldata->arg;
7503 msg.rpc_resp = &calldata->res;
7504 task_setup_data.callback_data = calldata;
7505 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007506 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007507 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007508 goto out;
7509 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007510 status = nfs4_wait_for_completion_rpc_task(task);
7511 if (status == 0)
7512 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007513 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007514 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007515out:
7516 dprintk("<-- %s status=%d\n", __func__, status);
7517 return status;
7518}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007519
7520static void
7521nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7522{
7523 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007524 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007525 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007526
7527 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007528 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7529 * right now covering the LAYOUTGET we are about to send.
7530 * However, that is not so catastrophic, and there seems
7531 * to be no way to prevent it completely.
7532 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007533 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007534 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007535 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007536 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7537 NFS_I(lgp->args.inode)->layout,
7538 lgp->args.ctx->state)) {
7539 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007540 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007541}
7542
7543static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7544{
7545 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007546 struct inode *inode = lgp->args.inode;
7547 struct nfs_server *server = NFS_SERVER(inode);
7548 struct pnfs_layout_hdr *lo;
7549 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007550 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007551
Boaz Harroshed7e5422014-01-22 20:34:54 +02007552 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007553
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007554 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007555 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007556
7557 switch (task->tk_status) {
7558 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007559 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007560 /*
7561 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7562 * (or clients) writing to the same RAID stripe
7563 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007564 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007565 /*
7566 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7567 * existing layout before getting a new one).
7568 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007569 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007570 timeo = rpc_get_timeout(task->tk_client);
7571 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007572 now = jiffies;
7573 if (time_after(giveup, now)) {
7574 unsigned long delay;
7575
7576 /* Delay for:
7577 * - Not less then NFS4_POLL_RETRY_MIN.
7578 * - One last time a jiffie before we give up
7579 * - exponential backoff (time_now minus start_attempt)
7580 */
7581 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7582 min((giveup - now - 1),
7583 now - lgp->args.timestamp));
7584
7585 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7586 __func__, delay);
7587 rpc_delay(task, delay);
7588 task->tk_status = 0;
7589 rpc_restart_call_prepare(task);
7590 goto out; /* Do not call nfs4_async_handle_error() */
7591 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007592 break;
7593 case -NFS4ERR_EXPIRED:
7594 case -NFS4ERR_BAD_STATEID:
7595 spin_lock(&inode->i_lock);
7596 lo = NFS_I(inode)->layout;
7597 if (!lo || list_empty(&lo->plh_segs)) {
7598 spin_unlock(&inode->i_lock);
7599 /* If the open stateid was bad, then recover it. */
7600 state = lgp->args.ctx->state;
7601 } else {
7602 LIST_HEAD(head);
7603
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007604 /*
7605 * Mark the bad layout state as invalid, then retry
7606 * with the current stateid.
7607 */
Trond Myklebustee314c22012-10-01 17:25:48 -07007608 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7609 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007610 pnfs_free_lseg_list(&head);
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007611
7612 task->tk_status = 0;
7613 rpc_restart_call_prepare(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007614 }
7615 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007616 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebustee314c22012-10-01 17:25:48 -07007617 rpc_restart_call_prepare(task);
7618out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007619 dprintk("<-- %s\n", __func__);
7620}
7621
Idan Kedar85541162012-08-02 11:47:10 +03007622static size_t max_response_pages(struct nfs_server *server)
7623{
7624 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7625 return nfs_page_array_len(0, max_resp_sz);
7626}
7627
7628static void nfs4_free_pages(struct page **pages, size_t size)
7629{
7630 int i;
7631
7632 if (!pages)
7633 return;
7634
7635 for (i = 0; i < size; i++) {
7636 if (!pages[i])
7637 break;
7638 __free_page(pages[i]);
7639 }
7640 kfree(pages);
7641}
7642
7643static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7644{
7645 struct page **pages;
7646 int i;
7647
7648 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7649 if (!pages) {
7650 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7651 return NULL;
7652 }
7653
7654 for (i = 0; i < size; i++) {
7655 pages[i] = alloc_page(gfp_flags);
7656 if (!pages[i]) {
7657 dprintk("%s: failed to allocate page\n", __func__);
7658 nfs4_free_pages(pages, size);
7659 return NULL;
7660 }
7661 }
7662
7663 return pages;
7664}
7665
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007666static void nfs4_layoutget_release(void *calldata)
7667{
7668 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007669 struct inode *inode = lgp->args.inode;
7670 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007671 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007672
7673 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007674 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007675 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007676 put_nfs_open_context(lgp->args.ctx);
7677 kfree(calldata);
7678 dprintk("<-- %s\n", __func__);
7679}
7680
7681static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7682 .rpc_call_prepare = nfs4_layoutget_prepare,
7683 .rpc_call_done = nfs4_layoutget_done,
7684 .rpc_release = nfs4_layoutget_release,
7685};
7686
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007687struct pnfs_layout_segment *
7688nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007689{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007690 struct inode *inode = lgp->args.inode;
7691 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007692 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007693 struct rpc_task *task;
7694 struct rpc_message msg = {
7695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7696 .rpc_argp = &lgp->args,
7697 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007698 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007699 };
7700 struct rpc_task_setup task_setup_data = {
7701 .rpc_client = server->client,
7702 .rpc_message = &msg,
7703 .callback_ops = &nfs4_layoutget_call_ops,
7704 .callback_data = lgp,
7705 .flags = RPC_TASK_ASYNC,
7706 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007707 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007708 int status = 0;
7709
7710 dprintk("--> %s\n", __func__);
7711
Idan Kedar85541162012-08-02 11:47:10 +03007712 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7713 if (!lgp->args.layout.pages) {
7714 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007715 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007716 }
7717 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007718 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007719
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007720 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007721 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007722 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007723
7724 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7725 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7726
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007727 task = rpc_run_task(&task_setup_data);
7728 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007729 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007730 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007731 if (status == 0)
7732 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007733 trace_nfs4_layoutget(lgp->args.ctx,
7734 &lgp->args.range,
7735 &lgp->res.range,
7736 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007737 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7738 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007739 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007740 rpc_put_task(task);
7741 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007742 if (status)
7743 return ERR_PTR(status);
7744 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007745}
7746
Benny Halevycbe82602011-05-22 19:52:37 +03007747static void
7748nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7749{
7750 struct nfs4_layoutreturn *lrp = calldata;
7751
7752 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007753 nfs41_setup_sequence(lrp->clp->cl_session,
7754 &lrp->args.seq_args,
7755 &lrp->res.seq_res,
7756 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007757}
7758
7759static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7760{
7761 struct nfs4_layoutreturn *lrp = calldata;
7762 struct nfs_server *server;
7763
7764 dprintk("--> %s\n", __func__);
7765
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007766 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007767 return;
7768
7769 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007770 switch (task->tk_status) {
7771 default:
7772 task->tk_status = 0;
7773 case 0:
7774 break;
7775 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007776 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007777 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007778 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007779 return;
7780 }
Benny Halevycbe82602011-05-22 19:52:37 +03007781 dprintk("<-- %s\n", __func__);
7782}
7783
7784static void nfs4_layoutreturn_release(void *calldata)
7785{
7786 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007787 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007788
7789 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007790 spin_lock(&lo->plh_inode->i_lock);
7791 if (lrp->res.lrs_present)
7792 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7793 lo->plh_block_lgets--;
7794 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007795 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007796 kfree(calldata);
7797 dprintk("<-- %s\n", __func__);
7798}
7799
7800static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7801 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7802 .rpc_call_done = nfs4_layoutreturn_done,
7803 .rpc_release = nfs4_layoutreturn_release,
7804};
7805
7806int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7807{
7808 struct rpc_task *task;
7809 struct rpc_message msg = {
7810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7811 .rpc_argp = &lrp->args,
7812 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007813 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007814 };
7815 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007816 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007817 .rpc_message = &msg,
7818 .callback_ops = &nfs4_layoutreturn_call_ops,
7819 .callback_data = lrp,
7820 };
7821 int status;
7822
7823 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007824 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007825 task = rpc_run_task(&task_setup_data);
7826 if (IS_ERR(task))
7827 return PTR_ERR(task);
7828 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007829 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007830 dprintk("<-- %s status=%d\n", __func__, status);
7831 rpc_put_task(task);
7832 return status;
7833}
7834
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007835static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007836_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7837 struct pnfs_device *pdev,
7838 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007839{
7840 struct nfs4_getdeviceinfo_args args = {
7841 .pdev = pdev,
7842 };
7843 struct nfs4_getdeviceinfo_res res = {
7844 .pdev = pdev,
7845 };
7846 struct rpc_message msg = {
7847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7848 .rpc_argp = &args,
7849 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007850 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007851 };
7852 int status;
7853
7854 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007855 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007856 dprintk("<-- %s status=%d\n", __func__, status);
7857
7858 return status;
7859}
7860
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007861int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7862 struct pnfs_device *pdev,
7863 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007864{
7865 struct nfs4_exception exception = { };
7866 int err;
7867
7868 do {
7869 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007870 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007871 &exception);
7872 } while (exception.retry);
7873 return err;
7874}
7875EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7876
Andy Adamson863a3c62011-03-23 13:27:54 +00007877static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7878{
7879 struct nfs4_layoutcommit_data *data = calldata;
7880 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007881 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007882
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007883 nfs41_setup_sequence(session,
7884 &data->args.seq_args,
7885 &data->res.seq_res,
7886 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007887}
7888
7889static void
7890nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7891{
7892 struct nfs4_layoutcommit_data *data = calldata;
7893 struct nfs_server *server = NFS_SERVER(data->args.inode);
7894
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007895 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007896 return;
7897
7898 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007899 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7900 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7901 case -NFS4ERR_BADLAYOUT: /* no layout */
7902 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007903 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007904 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007905 break;
7906 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10007907 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007908 rpc_restart_call_prepare(task);
7909 return;
7910 }
7911 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007912}
7913
7914static void nfs4_layoutcommit_release(void *calldata)
7915{
7916 struct nfs4_layoutcommit_data *data = calldata;
7917
Andy Adamsondb29c082011-07-30 20:52:38 -04007918 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05007919 nfs_post_op_update_inode_force_wcc(data->args.inode,
7920 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00007921 put_rpccred(data->cred);
7922 kfree(data);
7923}
7924
7925static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7926 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7927 .rpc_call_done = nfs4_layoutcommit_done,
7928 .rpc_release = nfs4_layoutcommit_release,
7929};
7930
7931int
Andy Adamsonef311532011-03-12 02:58:10 -05007932nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007933{
7934 struct rpc_message msg = {
7935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7936 .rpc_argp = &data->args,
7937 .rpc_resp = &data->res,
7938 .rpc_cred = data->cred,
7939 };
7940 struct rpc_task_setup task_setup_data = {
7941 .task = &data->task,
7942 .rpc_client = NFS_CLIENT(data->args.inode),
7943 .rpc_message = &msg,
7944 .callback_ops = &nfs4_layoutcommit_ops,
7945 .callback_data = data,
7946 .flags = RPC_TASK_ASYNC,
7947 };
7948 struct rpc_task *task;
7949 int status = 0;
7950
7951 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7952 "lbw: %llu inode %lu\n",
7953 data->task.tk_pid, sync,
7954 data->args.lastbytewritten,
7955 data->args.inode->i_ino);
7956
Chuck Levera9c92d62013-08-09 12:48:18 -04007957 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007958 task = rpc_run_task(&task_setup_data);
7959 if (IS_ERR(task))
7960 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007961 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007962 goto out;
7963 status = nfs4_wait_for_completion_rpc_task(task);
7964 if (status != 0)
7965 goto out;
7966 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007967 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007968out:
7969 dprintk("%s: status %d\n", __func__, status);
7970 rpc_put_task(task);
7971 return status;
7972}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007973
Andy Adamson97431202013-08-08 10:57:56 -04007974/**
7975 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7976 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7977 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007978static int
7979_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007980 struct nfs_fsinfo *info,
7981 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007982{
7983 struct nfs41_secinfo_no_name_args args = {
7984 .style = SECINFO_STYLE_CURRENT_FH,
7985 };
7986 struct nfs4_secinfo_res res = {
7987 .flavors = flavors,
7988 };
7989 struct rpc_message msg = {
7990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7991 .rpc_argp = &args,
7992 .rpc_resp = &res,
7993 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007994 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007995 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007996 int status;
7997
7998 if (use_integrity) {
7999 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008000 cred = nfs4_get_clid_cred(server->nfs_client);
8001 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008002 }
8003
8004 dprintk("--> %s\n", __func__);
8005 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8006 &res.seq_res, 0);
8007 dprintk("<-- %s status=%d\n", __func__, status);
8008
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008009 if (cred)
8010 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008011
8012 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008013}
8014
8015static int
8016nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8017 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8018{
8019 struct nfs4_exception exception = { };
8020 int err;
8021 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008022 /* first try using integrity protection */
8023 err = -NFS4ERR_WRONGSEC;
8024
8025 /* try to use integrity protection with machine cred */
8026 if (_nfs4_is_integrity_protected(server->nfs_client))
8027 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8028 flavors, true);
8029
8030 /*
8031 * if unable to use integrity protection, or SECINFO with
8032 * integrity protection returns NFS4ERR_WRONGSEC (which is
8033 * disallowed by spec, but exists in deployed servers) use
8034 * the current filesystem's rpc_client and the user cred.
8035 */
8036 if (err == -NFS4ERR_WRONGSEC)
8037 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8038 flavors, false);
8039
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008040 switch (err) {
8041 case 0:
8042 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008043 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008044 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008045 default:
8046 err = nfs4_handle_exception(server, err, &exception);
8047 }
8048 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008049out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008050 return err;
8051}
8052
8053static int
8054nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8055 struct nfs_fsinfo *info)
8056{
8057 int err;
8058 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008059 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008060 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008061 struct nfs4_secinfo4 *secinfo;
8062 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008063
8064 page = alloc_page(GFP_KERNEL);
8065 if (!page) {
8066 err = -ENOMEM;
8067 goto out;
8068 }
8069
8070 flavors = page_address(page);
8071 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8072
8073 /*
8074 * Fall back on "guess and check" method if
8075 * the server doesn't support SECINFO_NO_NAME
8076 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008077 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008078 err = nfs4_find_root_sec(server, fhandle, info);
8079 goto out_freepage;
8080 }
8081 if (err)
8082 goto out_freepage;
8083
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008084 for (i = 0; i < flavors->num_flavors; i++) {
8085 secinfo = &flavors->flavors[i];
8086
8087 switch (secinfo->flavor) {
8088 case RPC_AUTH_NULL:
8089 case RPC_AUTH_UNIX:
8090 case RPC_AUTH_GSS:
8091 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8092 &secinfo->flavor_info);
8093 break;
8094 default:
8095 flavor = RPC_AUTH_MAXFLAVOR;
8096 break;
8097 }
8098
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008099 if (!nfs_auth_info_match(&server->auth_info, flavor))
8100 flavor = RPC_AUTH_MAXFLAVOR;
8101
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008102 if (flavor != RPC_AUTH_MAXFLAVOR) {
8103 err = nfs4_lookup_root_sec(server, fhandle,
8104 info, flavor);
8105 if (!err)
8106 break;
8107 }
8108 }
8109
8110 if (flavor == RPC_AUTH_MAXFLAVOR)
8111 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008112
8113out_freepage:
8114 put_page(page);
8115 if (err == -EACCES)
8116 return -EPERM;
8117out:
8118 return err;
8119}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008120
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008121static int _nfs41_test_stateid(struct nfs_server *server,
8122 nfs4_stateid *stateid,
8123 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008124{
8125 int status;
8126 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008127 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008128 };
8129 struct nfs41_test_stateid_res res;
8130 struct rpc_message msg = {
8131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8132 .rpc_argp = &args,
8133 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008134 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008135 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008136 struct rpc_clnt *rpc_client = server->client;
8137
8138 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8139 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008140
Chuck Lever38527b12012-07-11 16:30:23 -04008141 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008142 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008143 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008144 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008145 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008146 if (status != NFS_OK) {
8147 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008148 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008149 }
8150 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008151 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008152}
8153
Chuck Lever38527b12012-07-11 16:30:23 -04008154/**
8155 * nfs41_test_stateid - perform a TEST_STATEID operation
8156 *
8157 * @server: server / transport on which to perform the operation
8158 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008159 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008160 *
8161 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8162 * Otherwise a negative NFS4ERR value is returned if the operation
8163 * failed or the state ID is not currently valid.
8164 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008165static int nfs41_test_stateid(struct nfs_server *server,
8166 nfs4_stateid *stateid,
8167 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008168{
8169 struct nfs4_exception exception = { };
8170 int err;
8171 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008172 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008173 if (err != -NFS4ERR_DELAY)
8174 break;
8175 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008176 } while (exception.retry);
8177 return err;
8178}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008179
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008180struct nfs_free_stateid_data {
8181 struct nfs_server *server;
8182 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008183 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008184};
8185
8186static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8187{
8188 struct nfs_free_stateid_data *data = calldata;
8189 nfs41_setup_sequence(nfs4_get_session(data->server),
8190 &data->args.seq_args,
8191 &data->res.seq_res,
8192 task);
8193}
8194
8195static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8196{
8197 struct nfs_free_stateid_data *data = calldata;
8198
8199 nfs41_sequence_done(task, &data->res.seq_res);
8200
8201 switch (task->tk_status) {
8202 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008203 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008204 rpc_restart_call_prepare(task);
8205 }
8206}
8207
8208static void nfs41_free_stateid_release(void *calldata)
8209{
8210 kfree(calldata);
8211}
8212
Trond Myklebust17f26b12013-08-21 15:48:42 -04008213static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008214 .rpc_call_prepare = nfs41_free_stateid_prepare,
8215 .rpc_call_done = nfs41_free_stateid_done,
8216 .rpc_release = nfs41_free_stateid_release,
8217};
8218
8219static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8220 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008221 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008222 bool privileged)
8223{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008224 struct rpc_message msg = {
8225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008226 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008227 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008228 struct rpc_task_setup task_setup = {
8229 .rpc_client = server->client,
8230 .rpc_message = &msg,
8231 .callback_ops = &nfs41_free_stateid_ops,
8232 .flags = RPC_TASK_ASYNC,
8233 };
8234 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008235
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008236 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8237 &task_setup.rpc_client, &msg);
8238
Chuck Lever38527b12012-07-11 16:30:23 -04008239 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008240 data = kmalloc(sizeof(*data), GFP_NOFS);
8241 if (!data)
8242 return ERR_PTR(-ENOMEM);
8243 data->server = server;
8244 nfs4_stateid_copy(&data->args.stateid, stateid);
8245
8246 task_setup.callback_data = data;
8247
8248 msg.rpc_argp = &data->args;
8249 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008250 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008251 if (privileged)
8252 nfs4_set_sequence_privileged(&data->args.seq_args);
8253
8254 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008255}
8256
Chuck Lever38527b12012-07-11 16:30:23 -04008257/**
8258 * nfs41_free_stateid - perform a FREE_STATEID operation
8259 *
8260 * @server: server / transport on which to perform the operation
8261 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008262 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008263 *
8264 * Returns NFS_OK if the server freed "stateid". Otherwise a
8265 * negative NFS4ERR value is returned.
8266 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008267static int nfs41_free_stateid(struct nfs_server *server,
8268 nfs4_stateid *stateid,
8269 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008270{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008271 struct rpc_task *task;
8272 int ret;
8273
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008274 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008275 if (IS_ERR(task))
8276 return PTR_ERR(task);
8277 ret = rpc_wait_for_completion_task(task);
8278 if (!ret)
8279 ret = task->tk_status;
8280 rpc_put_task(task);
8281 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008282}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008283
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008284static void
8285nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008286{
8287 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008288 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008289
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008290 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008291 nfs4_free_lock_state(server, lsp);
8292 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008293 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008294 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008295}
8296
Trond Myklebust36281ca2012-03-04 18:13:56 -05008297static bool nfs41_match_stateid(const nfs4_stateid *s1,
8298 const nfs4_stateid *s2)
8299{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008300 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008301 return false;
8302
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008303 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008304 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008305 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008306 return true;
8307
8308 return false;
8309}
8310
Andy Adamson557134a2009-04-01 09:21:53 -04008311#endif /* CONFIG_NFS_V4_1 */
8312
Trond Myklebust36281ca2012-03-04 18:13:56 -05008313static bool nfs4_match_stateid(const nfs4_stateid *s1,
8314 const nfs4_stateid *s2)
8315{
Trond Myklebustf597c532012-03-04 18:13:56 -05008316 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008317}
8318
8319
Trond Myklebust17280172012-03-11 13:11:00 -04008320static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008321 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008322 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323 .recover_open = nfs4_open_reclaim,
8324 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008325 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008326 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327};
8328
Andy Adamson591d71c2009-04-01 09:22:47 -04008329#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008330static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008331 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8332 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8333 .recover_open = nfs4_open_reclaim,
8334 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008335 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008336 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008337 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008338};
8339#endif /* CONFIG_NFS_V4_1 */
8340
Trond Myklebust17280172012-03-11 13:11:00 -04008341static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008342 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008343 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344 .recover_open = nfs4_open_expired,
8345 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008346 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347};
8348
Andy Adamson591d71c2009-04-01 09:22:47 -04008349#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008350static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008351 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8352 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008353 .recover_open = nfs41_open_expired,
8354 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008355 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008356};
8357#endif /* CONFIG_NFS_V4_1 */
8358
Trond Myklebust17280172012-03-11 13:11:00 -04008359static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008360 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008361 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008362 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008363};
8364
8365#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008366static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008367 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008368 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008369 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008370};
8371#endif
8372
Chuck Leverec011fe2013-10-17 14:12:39 -04008373static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008374 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008375 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008376};
8377
8378#if defined(CONFIG_NFS_V4_1)
8379static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008380 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008381 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008382};
8383#endif /* CONFIG_NFS_V4_1 */
8384
Trond Myklebust97dc1352010-06-16 09:52:26 -04008385static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8386 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008387 .init_caps = NFS_CAP_READDIRPLUS
8388 | NFS_CAP_ATOMIC_OPEN
8389 | NFS_CAP_CHANGE_ATTR
8390 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008391 .init_client = nfs40_init_client,
8392 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008393 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008394 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008395 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008396 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008397 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8398 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8399 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008400 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008401};
8402
8403#if defined(CONFIG_NFS_V4_1)
8404static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8405 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008406 .init_caps = NFS_CAP_READDIRPLUS
8407 | NFS_CAP_ATOMIC_OPEN
8408 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008409 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008410 | NFS_CAP_STATEID_NFSV41
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -04008411 | NFS_CAP_ATOMIC_OPEN_V1
8412 | NFS_CAP_SEEK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008413 .init_client = nfs41_init_client,
8414 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008415 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008416 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008417 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008418 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008419 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8420 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8421 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008422 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008423};
8424#endif
8425
Steve Dickson42c2c422013-05-22 12:50:38 -04008426#if defined(CONFIG_NFS_V4_2)
8427static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8428 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008429 .init_caps = NFS_CAP_READDIRPLUS
8430 | NFS_CAP_ATOMIC_OPEN
8431 | NFS_CAP_CHANGE_ATTR
8432 | NFS_CAP_POSIX_LOCK
8433 | NFS_CAP_STATEID_NFSV41
8434 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008435 .init_client = nfs41_init_client,
8436 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008437 .match_stateid = nfs41_match_stateid,
8438 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008439 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008440 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008441 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8442 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8443 .state_renewal_ops = &nfs41_state_renewal_ops,
8444};
8445#endif
8446
Trond Myklebust97dc1352010-06-16 09:52:26 -04008447const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8448 [0] = &nfs_v4_0_minor_ops,
8449#if defined(CONFIG_NFS_V4_1)
8450 [1] = &nfs_v4_1_minor_ops,
8451#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008452#if defined(CONFIG_NFS_V4_2)
8453 [2] = &nfs_v4_2_minor_ops,
8454#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008455};
8456
Trond Myklebust17f26b12013-08-21 15:48:42 -04008457static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008458 .create = nfs_create,
8459 .lookup = nfs_lookup,
8460 .atomic_open = nfs_atomic_open,
8461 .link = nfs_link,
8462 .unlink = nfs_unlink,
8463 .symlink = nfs_symlink,
8464 .mkdir = nfs_mkdir,
8465 .rmdir = nfs_rmdir,
8466 .mknod = nfs_mknod,
8467 .rename = nfs_rename,
8468 .permission = nfs_permission,
8469 .getattr = nfs_getattr,
8470 .setattr = nfs_setattr,
8471 .getxattr = generic_getxattr,
8472 .setxattr = generic_setxattr,
8473 .listxattr = generic_listxattr,
8474 .removexattr = generic_removexattr,
8475};
8476
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008477static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008478 .permission = nfs_permission,
8479 .getattr = nfs_getattr,
8480 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008481 .getxattr = generic_getxattr,
8482 .setxattr = generic_setxattr,
8483 .listxattr = generic_listxattr,
8484 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008485};
8486
David Howells509de812006-08-22 20:06:11 -04008487const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008488 .version = 4, /* protocol version */
8489 .dentry_ops = &nfs4_dentry_operations,
8490 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008491 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008492 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008494 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008495 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496 .getattr = nfs4_proc_getattr,
8497 .setattr = nfs4_proc_setattr,
8498 .lookup = nfs4_proc_lookup,
8499 .access = nfs4_proc_access,
8500 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 .create = nfs4_proc_create,
8502 .remove = nfs4_proc_remove,
8503 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008504 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008506 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008507 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008508 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 .link = nfs4_proc_link,
8510 .symlink = nfs4_proc_symlink,
8511 .mkdir = nfs4_proc_mkdir,
8512 .rmdir = nfs4_proc_remove,
8513 .readdir = nfs4_proc_readdir,
8514 .mknod = nfs4_proc_mknod,
8515 .statfs = nfs4_proc_statfs,
8516 .fsinfo = nfs4_proc_fsinfo,
8517 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008518 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008520 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008522 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008524 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008526 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008527 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008529 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008530 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008531 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008532 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008533 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008534 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008535 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008536 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008537 .create_server = nfs4_create_server,
8538 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539};
8540
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008541static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8542 .prefix = XATTR_NAME_NFSV4_ACL,
8543 .list = nfs4_xattr_list_nfs4_acl,
8544 .get = nfs4_xattr_get_nfs4_acl,
8545 .set = nfs4_xattr_set_nfs4_acl,
8546};
8547
8548const struct xattr_handler *nfs4_xattr_handlers[] = {
8549 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008550#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8551 &nfs4_xattr_nfs4_label_handler,
8552#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008553 NULL
8554};
8555
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556/*
8557 * Local variables:
8558 * c-basic-offset: 8
8559 * End:
8560 */