blob: 2564e1c89f567e31db339aafdd0a30083cfe584f [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 *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
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
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400155 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000156 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800157 case -NFS4ERR_BADOWNER:
158 case -NFS4ERR_BADNAME:
159 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400160 case -NFS4ERR_SHARE_DENIED:
161 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400162 case -NFS4ERR_MINOR_VERS_MISMATCH:
163 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400164 case -NFS4ERR_ACCESS:
165 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400166 case -NFS4ERR_FILE_OPEN:
167 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700170 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/*
177 * This is our standard bitmap for GETATTR requests.
178 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400179const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 FATTR4_WORD0_TYPE
181 | FATTR4_WORD0_CHANGE
182 | FATTR4_WORD0_SIZE
183 | FATTR4_WORD0_FSID
184 | FATTR4_WORD0_FILEID,
185 FATTR4_WORD1_MODE
186 | FATTR4_WORD1_NUMLINKS
187 | FATTR4_WORD1_OWNER
188 | FATTR4_WORD1_OWNER_GROUP
189 | FATTR4_WORD1_RAWDEV
190 | FATTR4_WORD1_SPACE_USED
191 | FATTR4_WORD1_TIME_ACCESS
192 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400193 | FATTR4_WORD1_TIME_MODIFY,
194#ifdef CONFIG_NFS_V4_SECURITY_LABEL
195 FATTR4_WORD2_SECURITY_LABEL
196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Trond Myklebust1549210f2012-06-05 09:16:47 -0400199static const u32 nfs4_pnfs_open_bitmap[3] = {
200 FATTR4_WORD0_TYPE
201 | FATTR4_WORD0_CHANGE
202 | FATTR4_WORD0_SIZE
203 | FATTR4_WORD0_FSID
204 | FATTR4_WORD0_FILEID,
205 FATTR4_WORD1_MODE
206 | FATTR4_WORD1_NUMLINKS
207 | FATTR4_WORD1_OWNER
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
213 | FATTR4_WORD1_TIME_MODIFY,
214 FATTR4_WORD2_MDSTHRESHOLD
215};
216
Andy Adamsone23008e2012-10-02 21:07:32 -0400217static const u32 nfs4_open_noattr_bitmap[3] = {
218 FATTR4_WORD0_TYPE
219 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_FILEID,
221};
222
David Quigleya09df2c2013-05-22 12:50:41 -0400223const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 FATTR4_WORD0_FILES_AVAIL
225 | FATTR4_WORD0_FILES_FREE
226 | FATTR4_WORD0_FILES_TOTAL,
227 FATTR4_WORD1_SPACE_AVAIL
228 | FATTR4_WORD1_SPACE_FREE
229 | FATTR4_WORD1_SPACE_TOTAL
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_MAXLINK
234 | FATTR4_WORD0_MAXNAME,
235 0
236};
237
Fred Isamandae100c2011-07-30 20:52:37 -0400238const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 | FATTR4_WORD0_MAXREAD
240 | FATTR4_WORD0_MAXWRITE
241 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700242 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400243 | FATTR4_WORD1_FS_LAYOUT_TYPES,
244 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
David Quigleya09df2c2013-05-22 12:50:41 -0400247const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400248 FATTR4_WORD0_TYPE
249 | FATTR4_WORD0_CHANGE
250 | FATTR4_WORD0_SIZE
251 | FATTR4_WORD0_FSID
252 | FATTR4_WORD0_FILEID
253 | FATTR4_WORD0_FS_LOCATIONS,
254 FATTR4_WORD1_MODE
255 | FATTR4_WORD1_NUMLINKS
256 | FATTR4_WORD1_OWNER
257 | FATTR4_WORD1_OWNER_GROUP
258 | FATTR4_WORD1_RAWDEV
259 | FATTR4_WORD1_SPACE_USED
260 | FATTR4_WORD1_TIME_ACCESS
261 | FATTR4_WORD1_TIME_METADATA
262 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400263 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400264};
265
Al Virobc4785c2006-10-19 23:28:51 -0700266static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct nfs4_readdir_arg *readdir)
268{
Al Viro0dbb4c62006-10-19 23:28:49 -0700269 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000272 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
274 return;
275 }
276
277 readdir->cookie = 0;
278 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
279 if (cookie == 2)
280 return;
281
282 /*
283 * NFSv4 servers do not return entries for '.' and '..'
284 * Therefore, we fake these entries here. We let '.'
285 * have cookie 0 and '..' have cookie 1. Note that
286 * when talking to the server, we always send cookie 0
287 * instead of 1 or 2.
288 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800289 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (cookie == 0) {
292 *p++ = xdr_one; /* next */
293 *p++ = xdr_zero; /* cookie, first word */
294 *p++ = xdr_one; /* cookie, second word */
295 *p++ = xdr_one; /* entry len */
296 memcpy(p, ".\0\0\0", 4); /* entry */
297 p++;
298 *p++ = xdr_one; /* bitmap length */
299 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
300 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400301 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_two; /* cookie, second word */
307 *p++ = xdr_two; /* entry len */
308 memcpy(p, "..\0\0", 4); /* entry */
309 p++;
310 *p++ = xdr_one; /* bitmap length */
311 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
312 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400313 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 readdir->pgbase = (char *)p - (char *)start;
316 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800317 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Trond Myklebust65de8722008-12-23 15:21:44 -0500320static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
321{
322 int res = 0;
323
324 might_sleep();
325
326 if (*timeout <= 0)
327 *timeout = NFS4_POLL_RETRY_MIN;
328 if (*timeout > NFS4_POLL_RETRY_MAX)
329 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000330 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500331 if (fatal_signal_pending(current))
332 res = -ERESTARTSYS;
333 *timeout <<= 1;
334 return res;
335}
336
337/* This is the error handling routine for processes that are allowed
338 * to sleep.
339 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800340static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500341{
342 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500343 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500344 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 int ret = errorcode;
346
347 exception->retry = 0;
348 switch(errorcode) {
349 case 0:
350 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400352 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400353 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500354 exception->retry = 1;
355 return 0;
356 }
357 if (state == NULL)
358 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400359 ret = nfs4_schedule_stateid_recovery(server, state);
360 if (ret < 0)
361 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500362 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500363 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500364 case -NFS4ERR_ADMIN_REVOKED:
365 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400366 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
367 nfs_remove_bad_delegation(inode);
368 exception->retry = 1;
369 break;
370 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500371 if (state == NULL)
372 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400373 ret = nfs4_schedule_stateid_recovery(server, state);
374 if (ret < 0)
375 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500376 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400377 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400378 if (state != NULL) {
379 ret = nfs4_schedule_stateid_recovery(server, state);
380 if (ret < 0)
381 break;
382 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500384 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500385 nfs4_schedule_lease_recovery(clp);
386 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400387 case -NFS4ERR_MOVED:
388 ret = nfs4_schedule_migration_recovery(server);
389 if (ret < 0)
390 break;
391 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400392 case -NFS4ERR_LEASE_MOVED:
393 nfs4_schedule_lease_moved_recovery(clp);
394 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500395#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400396 case -NFS4ERR_BADSESSION:
397 case -NFS4ERR_BADSLOT:
398 case -NFS4ERR_BAD_HIGH_SLOT:
399 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
400 case -NFS4ERR_DEADSESSION:
401 case -NFS4ERR_SEQ_FALSE_RETRY:
402 case -NFS4ERR_SEQ_MISORDERED:
403 dprintk("%s ERROR: %d Reset session\n", __func__,
404 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400405 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400406 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500407#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500408 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500409 if (exception->timeout > HZ) {
410 /* We have retried a decent amount, time to
411 * fail
412 */
413 ret = -EBUSY;
414 break;
415 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500416 case -NFS4ERR_GRACE:
417 case -NFS4ERR_DELAY:
418 ret = nfs4_delay(server->client, &exception->timeout);
419 if (ret != 0)
420 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400421 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500422 case -NFS4ERR_OLD_STATEID:
423 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800424 break;
425 case -NFS4ERR_BADOWNER:
426 /* The following works around a Linux server bug! */
427 case -NFS4ERR_BADNAME:
428 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
429 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
430 exception->retry = 1;
431 printk(KERN_WARNING "NFS: v4 server %s "
432 "does not accept raw "
433 "uid/gids. "
434 "Reenabling the idmapper.\n",
435 server->nfs_client->cl_hostname);
436 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 }
438 /* We failed to handle the error */
439 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500440wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500441 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400442 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
443 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500444 if (ret == 0)
445 exception->retry = 1;
446 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500447}
448
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400449/*
450 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
451 * or 'false' otherwise.
452 */
453static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
454{
455 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
456
457 if (flavor == RPC_AUTH_GSS_KRB5I ||
458 flavor == RPC_AUTH_GSS_KRB5P)
459 return true;
460
461 return false;
462}
Trond Myklebust65de8722008-12-23 15:21:44 -0500463
Trond Myklebust452e9352010-07-31 14:29:06 -0400464static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 spin_lock(&clp->cl_lock);
467 if (time_before(clp->cl_last_renewal,timestamp))
468 clp->cl_last_renewal = timestamp;
469 spin_unlock(&clp->cl_lock);
470}
471
Trond Myklebust452e9352010-07-31 14:29:06 -0400472static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
473{
474 do_renew_lease(server->nfs_client, timestamp);
475}
476
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400477struct nfs4_call_sync_data {
478 const struct nfs_server *seq_server;
479 struct nfs4_sequence_args *seq_args;
480 struct nfs4_sequence_res *seq_res;
481};
482
Chuck Levera9c92d62013-08-09 12:48:18 -0400483static void nfs4_init_sequence(struct nfs4_sequence_args *args,
484 struct nfs4_sequence_res *res, int cache_reply)
485{
486 args->sa_slot = NULL;
487 args->sa_cache_this = cache_reply;
488 args->sa_privileged = 0;
489
490 res->sr_slot = NULL;
491}
492
493static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
494{
495 args->sa_privileged = 1;
496}
497
Chuck Lever3bd23842013-08-09 12:49:19 -0400498static int nfs40_setup_sequence(const struct nfs_server *server,
499 struct nfs4_sequence_args *args,
500 struct nfs4_sequence_res *res,
501 struct rpc_task *task)
502{
503 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
504 struct nfs4_slot *slot;
505
506 /* slot already allocated? */
507 if (res->sr_slot != NULL)
508 goto out_start;
509
510 spin_lock(&tbl->slot_tbl_lock);
511 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
512 goto out_sleep;
513
514 slot = nfs4_alloc_slot(tbl);
515 if (IS_ERR(slot)) {
516 if (slot == ERR_PTR(-ENOMEM))
517 task->tk_timeout = HZ >> 2;
518 goto out_sleep;
519 }
520 spin_unlock(&tbl->slot_tbl_lock);
521
522 args->sa_slot = slot;
523 res->sr_slot = slot;
524
525out_start:
526 rpc_call_start(task);
527 return 0;
528
529out_sleep:
530 if (args->sa_privileged)
531 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
532 NULL, RPC_PRIORITY_PRIVILEGED);
533 else
534 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
535 spin_unlock(&tbl->slot_tbl_lock);
536 return -EAGAIN;
537}
538
539static int nfs40_sequence_done(struct rpc_task *task,
540 struct nfs4_sequence_res *res)
541{
542 struct nfs4_slot *slot = res->sr_slot;
543 struct nfs4_slot_table *tbl;
544
545 if (!RPC_WAS_SENT(task))
546 goto out;
547
548 tbl = slot->table;
549 spin_lock(&tbl->slot_tbl_lock);
550 if (!nfs41_wake_and_assign_slot(tbl, slot))
551 nfs4_free_slot(tbl, slot);
552 spin_unlock(&tbl->slot_tbl_lock);
553
554 res->sr_slot = NULL;
555out:
556 return 1;
557}
558
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400559#if defined(CONFIG_NFS_V4_1)
560
Trond Myklebustd185a332010-06-16 09:52:25 -0400561static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400562{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500563 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400564 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500565 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400566
Benny Halevydfb4f3092010-09-24 09:17:01 -0400567 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400568 /* just wake up the next guy waiting since
569 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500570 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500571 return;
Andy Adamson13615872009-04-01 09:22:17 -0400572 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500573 tbl = res->sr_slot->table;
574 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500575
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500576 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500577 /* Be nice to the server: try to ensure that the last transmitted
578 * value for highest_user_slotid <= target_highest_slotid
579 */
580 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
581 send_new_highest_used_slotid = true;
582
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500583 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
584 send_new_highest_used_slotid = false;
585 goto out_unlock;
586 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500587 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500588
589 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
590 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500591out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500592 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400593 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500594 if (send_new_highest_used_slotid)
595 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400596}
597
Trond Myklebust14516c32010-07-31 14:29:06 -0400598static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400599{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500600 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500601 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400602 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500603 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500604 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400605
Bryan Schumaker468f8612011-04-18 15:57:32 -0400606 /* don't increment the sequence number if the task wasn't sent */
607 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400608 goto out;
609
Trond Myklebust933602e2012-11-16 12:12:38 -0500610 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500611 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500612
Trond Myklebustac20d162012-12-15 15:36:07 -0500613 if (slot->interrupted) {
614 slot->interrupted = 0;
615 interrupted = true;
616 }
617
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400618 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500619 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400620 switch (res->sr_status) {
621 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400622 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500623 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500624 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500625 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500626 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500627 if (res->sr_status_flags != 0)
628 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500629 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400630 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500631 case 1:
632 /*
633 * sr_status remains 1 if an RPC level error occurred.
634 * The server may or may not have processed the sequence
635 * operation..
636 * Mark the slot as having hosted an interrupted RPC call.
637 */
638 slot->interrupted = 1;
639 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400640 case -NFS4ERR_DELAY:
641 /* The server detected a resend of the RPC call and
642 * returned NFS4ERR_DELAY as per Section 2.10.6.2
643 * of RFC5661.
644 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500645 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400646 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500647 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500648 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400649 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500650 case -NFS4ERR_BADSLOT:
651 /*
652 * The slot id we used was probably retired. Try again
653 * using a different slot id.
654 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500655 goto retry_nowait;
656 case -NFS4ERR_SEQ_MISORDERED:
657 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500658 * Was the last operation on this sequence interrupted?
659 * If so, retry after bumping the sequence number.
660 */
661 if (interrupted) {
662 ++slot->seq_nr;
663 goto retry_nowait;
664 }
665 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500666 * Could this slot have been previously retired?
667 * If so, then the server may be expecting seq_nr = 1!
668 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500669 if (slot->seq_nr != 1) {
670 slot->seq_nr = 1;
671 goto retry_nowait;
672 }
673 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500674 case -NFS4ERR_SEQ_FALSE_RETRY:
675 ++slot->seq_nr;
676 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400677 default:
678 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500679 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400680 }
681out:
682 /* The session may be reset by one of the error handlers. */
683 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400684 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500685 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500686retry_nowait:
687 if (rpc_restart_call_prepare(task)) {
688 task->tk_status = 0;
689 ret = 0;
690 }
691 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400692out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400693 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400694 goto out;
695 rpc_delay(task, NFS4_POLL_RETRY_MAX);
696 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400697}
698
Trond Myklebust14516c32010-07-31 14:29:06 -0400699static int nfs4_sequence_done(struct rpc_task *task,
700 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400701{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500702 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400703 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400704 if (!res->sr_slot->table->session)
705 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400706 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400707}
708
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000709int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400710 struct nfs4_sequence_args *args,
711 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400712 struct rpc_task *task)
713{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400714 struct nfs4_slot *slot;
715 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400716
717 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400718 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400719 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400720 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400721
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400722 tbl = &session->fc_slot_table;
723
Trond Myklebust69d206b2012-11-22 13:21:02 -0500724 task->tk_timeout = 0;
725
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400726 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400727 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400728 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500729 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500730 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400731 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400732 }
733
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500734 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500735 if (IS_ERR(slot)) {
736 /* If out of memory, try again in 1/4 second */
737 if (slot == ERR_PTR(-ENOMEM))
738 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400739 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400740 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400741 }
742 spin_unlock(&tbl->slot_tbl_lock);
743
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500744 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400745
Chuck Levere8d92382013-08-09 12:47:51 -0400746 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500747 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400748
Benny Halevydfb4f3092010-09-24 09:17:01 -0400749 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500750 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400751 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400752 /*
753 * sr_status is only set in decode_sequence, and so will remain
754 * set to 1 if an rpc level failure occurs.
755 */
756 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400757 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400758out_success:
759 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400760 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400761out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400762 /* Privileged tasks are queued with top priority */
763 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400764 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
765 NULL, RPC_PRIORITY_PRIVILEGED);
766 else
767 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400768 spin_unlock(&tbl->slot_tbl_lock);
769 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400770}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000771EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400772
Chuck Lever5a580e02013-08-09 12:48:09 -0400773static int nfs4_setup_sequence(const struct nfs_server *server,
774 struct nfs4_sequence_args *args,
775 struct nfs4_sequence_res *res,
776 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400777{
Trond Myklebust035168a2010-06-16 09:52:26 -0400778 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400779 int ret = 0;
780
Chuck Lever3bd23842013-08-09 12:49:19 -0400781 if (!session)
782 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400783
Chuck Levere8d92382013-08-09 12:47:51 -0400784 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400785 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400786 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400787
Trond Myklebust9d12b212012-01-17 22:04:25 -0500788 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400789
Andy Adamsonce5039c2009-04-01 09:22:13 -0400790 dprintk("<-- %s status=%d\n", __func__, ret);
791 return ret;
792}
793
Andy Adamsonce5039c2009-04-01 09:22:13 -0400794static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
795{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400796 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400797 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400798
Trond Myklebust035168a2010-06-16 09:52:26 -0400799 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
800
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400801 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400802}
803
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
805{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400806 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400807
Trond Myklebust14516c32010-07-31 14:29:06 -0400808 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400809}
810
Trond Myklebust17280172012-03-11 13:11:00 -0400811static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400812 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400813 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400814};
815
Chuck Lever3bd23842013-08-09 12:49:19 -0400816#else /* !CONFIG_NFS_V4_1 */
817
Chuck Lever5a580e02013-08-09 12:48:09 -0400818static int nfs4_setup_sequence(const struct nfs_server *server,
819 struct nfs4_sequence_args *args,
820 struct nfs4_sequence_res *res,
821 struct rpc_task *task)
822{
Chuck Lever3bd23842013-08-09 12:49:19 -0400823 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400824}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400825
Trond Myklebust14516c32010-07-31 14:29:06 -0400826static int nfs4_sequence_done(struct rpc_task *task,
827 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400828{
Chuck Lever3bd23842013-08-09 12:49:19 -0400829 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400830}
Chuck Lever3bd23842013-08-09 12:49:19 -0400831
832#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400833
Chuck Lever9915ea72013-08-09 12:48:27 -0400834static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
835{
836 struct nfs4_call_sync_data *data = calldata;
837 nfs4_setup_sequence(data->seq_server,
838 data->seq_args, data->seq_res, task);
839}
840
841static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
842{
843 struct nfs4_call_sync_data *data = calldata;
844 nfs4_sequence_done(task, data->seq_res);
845}
846
847static const struct rpc_call_ops nfs40_call_sync_ops = {
848 .rpc_call_prepare = nfs40_call_sync_prepare,
849 .rpc_call_done = nfs40_call_sync_done,
850};
851
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400852static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400853 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 struct nfs4_sequence_args *args,
856 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400857{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858 int ret;
859 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400860 struct nfs_client *clp = server->nfs_client;
861 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100862 .seq_server = server,
863 .seq_args = args,
864 .seq_res = res,
865 };
866 struct rpc_task_setup task_setup = {
867 .rpc_client = clnt,
868 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400869 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 .callback_data = &data
871 };
872
873 task = rpc_run_task(&task_setup);
874 if (IS_ERR(task))
875 ret = PTR_ERR(task);
876 else {
877 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400878 rpc_put_task(task);
879 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100880 return ret;
881}
882
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400883static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000884int nfs4_call_sync(struct rpc_clnt *clnt,
885 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000886 struct rpc_message *msg,
887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
889 int cache_reply)
890{
Chuck Levera9c92d62013-08-09 12:48:18 -0400891 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400892 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000893}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
896{
897 struct nfs_inode *nfsi = NFS_I(dir);
898
899 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400900 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700901 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700903 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000904 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_unlock(&dir->i_lock);
906}
907
908struct nfs4_opendata {
909 struct kref kref;
910 struct nfs_openargs o_arg;
911 struct nfs_openres o_res;
912 struct nfs_open_confirmargs c_arg;
913 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500914 struct nfs4_string owner_name;
915 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400917 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400919 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct nfs4_state_owner *owner;
921 struct nfs4_state *state;
922 struct iattr attrs;
923 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400924 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400925 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400926 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400927 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int cancelled;
929};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400930
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400931static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
932 int err, struct nfs4_exception *exception)
933{
934 if (err != -EINVAL)
935 return false;
936 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
937 return false;
938 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
939 exception->retry = 1;
940 return true;
941}
942
943static enum open_claim_type4
944nfs4_map_atomic_open_claim(struct nfs_server *server,
945 enum open_claim_type4 claim)
946{
947 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
948 return claim;
949 switch (claim) {
950 default:
951 return claim;
952 case NFS4_OPEN_CLAIM_FH:
953 return NFS4_OPEN_CLAIM_NULL;
954 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
955 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
956 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
957 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
958 }
959}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100962{
963 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400964 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100965 p->o_res.seqid = p->o_arg.seqid;
966 p->c_res.seqid = p->c_arg.seqid;
967 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400968 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500970 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971}
972
Al Viro82a2c1b2011-06-22 18:30:55 -0400973static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100974 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400975 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400976 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400977 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400978 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100979{
Al Viro82a2c1b2011-06-22 18:30:55 -0400980 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100981 struct inode *dir = parent->d_inode;
982 struct nfs_server *server = NFS_SERVER(dir);
983 struct nfs4_opendata *p;
984
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400985 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986 if (p == NULL)
987 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400988
989 p->f_label = nfs4_label_alloc(server, gfp_mask);
990 if (IS_ERR(p->f_label))
991 goto err_free_p;
992
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400993 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100994 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400995 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400996 nfs_sb_active(dentry->d_sb);
997 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100998 p->dir = parent;
999 p->owner = sp;
1000 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 p->o_arg.open_flags = flags;
1002 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001003 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1004 * will return permission denied for all bits until close */
1005 if (!(flags & O_EXCL)) {
1006 /* ask server to check for all possible rights as results
1007 * are cached */
1008 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1009 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001010 }
David Howells7539bba2006-08-22 20:06:09 -04001011 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001012 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1013 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001014 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001015 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001016 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001017 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001018 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001019 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1020 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001021 case NFS4_OPEN_CLAIM_NULL:
1022 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1023 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1024 p->o_arg.fh = NFS_FH(dir);
1025 break;
1026 case NFS4_OPEN_CLAIM_PREVIOUS:
1027 case NFS4_OPEN_CLAIM_FH:
1028 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1029 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1030 p->o_arg.fh = NFS_FH(dentry->d_inode);
1031 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001032 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001033 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001034
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001035 p->o_arg.u.attrs = &p->attrs;
1036 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001037
1038 verf[0] = jiffies;
1039 verf[1] = current->pid;
1040 memcpy(p->o_arg.u.verifier.data, verf,
1041 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001042 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001043 p->c_arg.fh = &p->o_res.fh;
1044 p->c_arg.stateid = &p->o_res.stateid;
1045 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001046 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001047 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001049
1050err_free_label:
1051 nfs4_label_free(p->f_label);
1052err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001053 kfree(p);
1054err:
1055 dput(parent);
1056 return NULL;
1057}
1058
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001059static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001060{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001061 struct nfs4_opendata *p = container_of(kref,
1062 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001063 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001064
1065 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001066 if (p->state != NULL)
1067 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001068 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001069
1070 nfs4_label_free(p->f_label);
1071
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001072 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001073 dput(p->dentry);
1074 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001075 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001076 kfree(p);
1077}
1078
1079static void nfs4_opendata_put(struct nfs4_opendata *p)
1080{
1081 if (p != NULL)
1082 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001083}
1084
Trond Myklebust06f814a2006-01-03 09:55:07 +01001085static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1086{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001087 int ret;
1088
Trond Myklebust06f814a2006-01-03 09:55:07 +01001089 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001090 return ret;
1091}
1092
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001093static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001094{
1095 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096
Trond Myklebust536e43d2012-01-17 22:04:26 -05001097 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001098 goto out;
1099 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001101 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1102 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 break;
1104 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001105 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1106 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001107 break;
1108 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001109 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1110 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001111 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001112out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001113 return ret;
1114}
1115
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001116static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001118 if (delegation == NULL)
1119 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001122 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001123 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001124 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1125 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001126 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 return 1;
1128}
1129
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001130static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001131{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001132 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001133 case FMODE_WRITE:
1134 state->n_wronly++;
1135 break;
1136 case FMODE_READ:
1137 state->n_rdonly++;
1138 break;
1139 case FMODE_READ|FMODE_WRITE:
1140 state->n_rdwr++;
1141 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001142 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001143}
1144
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001145static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001146{
1147 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001148 nfs4_stateid_copy(&state->stateid, stateid);
1149 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001150 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001152 case FMODE_READ:
1153 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1154 break;
1155 case FMODE_WRITE:
1156 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1157 break;
1158 case FMODE_READ|FMODE_WRITE:
1159 set_bit(NFS_O_RDWR_STATE, &state->flags);
1160 }
1161}
1162
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001163static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001164{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001165 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001166 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001167 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001168}
1169
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001170static 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 -07001171{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001172 /*
1173 * Protect the call to nfs4_state_set_mode_locked and
1174 * serialise the stateid update
1175 */
1176 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001177 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001178 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001179 set_bit(NFS_DELEGATED_STATE, &state->flags);
1180 }
1181 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001182 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001183 write_sequnlock(&state->seqlock);
1184 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001185 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001186 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001189static 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 -05001190{
1191 struct nfs_inode *nfsi = NFS_I(state->inode);
1192 struct nfs_delegation *deleg_cur;
1193 int ret = 0;
1194
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001195 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001196
1197 rcu_read_lock();
1198 deleg_cur = rcu_dereference(nfsi->delegation);
1199 if (deleg_cur == NULL)
1200 goto no_delegation;
1201
1202 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001203 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001204 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001205 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001206 goto no_delegation_unlock;
1207
1208 if (delegation == NULL)
1209 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001210 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001211 goto no_delegation_unlock;
1212
Trond Myklebustb7391f42008-12-23 15:21:52 -05001213 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001214 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001215 ret = 1;
1216no_delegation_unlock:
1217 spin_unlock(&deleg_cur->lock);
1218no_delegation:
1219 rcu_read_unlock();
1220
1221 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001222 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001223 ret = 1;
1224 }
1225
1226 return ret;
1227}
1228
1229
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001230static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001231{
1232 struct nfs_delegation *delegation;
1233
1234 rcu_read_lock();
1235 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001236 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001237 rcu_read_unlock();
1238 return;
1239 }
1240 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001241 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001242}
1243
Trond Myklebust6ee41262007-07-08 14:11:36 -04001244static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001245{
1246 struct nfs4_state *state = opendata->state;
1247 struct nfs_inode *nfsi = NFS_I(state->inode);
1248 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001249 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001250 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001251 nfs4_stateid stateid;
1252 int ret = -EAGAIN;
1253
Trond Myklebustaac00a82007-07-05 19:02:21 -04001254 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001255 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001256 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001257 if (can_open_cached(state, fmode, open_mode)) {
1258 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001259 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001260 goto out_return_state;
1261 }
1262 spin_unlock(&state->owner->so_lock);
1263 }
Trond Myklebust34310432008-12-23 15:21:38 -05001264 rcu_read_lock();
1265 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001266 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001267 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001268 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001269 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001270 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001271 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001272 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001273 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001274 if (!opendata->is_recover) {
1275 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1276 if (ret != 0)
1277 goto out;
1278 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001279 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001280
1281 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001283 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001284 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001285out:
1286 return ERR_PTR(ret);
1287out_return_state:
1288 atomic_inc(&state->count);
1289 return state;
1290}
1291
Andy Adamsone23008e2012-10-02 21:07:32 -04001292static void
1293nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1294{
1295 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1296 struct nfs_delegation *delegation;
1297 int delegation_flags = 0;
1298
1299 rcu_read_lock();
1300 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1301 if (delegation)
1302 delegation_flags = delegation->flags;
1303 rcu_read_unlock();
1304 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1305 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1306 "returning a delegation for "
1307 "OPEN(CLAIM_DELEGATE_CUR)\n",
1308 clp->cl_hostname);
1309 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1310 nfs_inode_set_delegation(state->inode,
1311 data->owner->so_cred,
1312 &data->o_res);
1313 else
1314 nfs_inode_reclaim_delegation(state->inode,
1315 data->owner->so_cred,
1316 &data->o_res);
1317}
1318
1319/*
1320 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1321 * and update the nfs4_state.
1322 */
1323static struct nfs4_state *
1324_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1325{
1326 struct inode *inode = data->state->inode;
1327 struct nfs4_state *state = data->state;
1328 int ret;
1329
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001330 if (!data->rpc_done) {
1331 if (data->rpc_status) {
1332 ret = data->rpc_status;
1333 goto err;
1334 }
1335 /* cached opens have already been processed */
1336 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001337 }
1338
Andy Adamsone23008e2012-10-02 21:07:32 -04001339 ret = nfs_refresh_inode(inode, &data->f_attr);
1340 if (ret)
1341 goto err;
1342
1343 if (data->o_res.delegation_type != 0)
1344 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001345update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001346 update_open_stateid(state, &data->o_res.stateid, NULL,
1347 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001348 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001349
1350 return state;
1351err:
1352 return ERR_PTR(ret);
1353
1354}
1355
1356static struct nfs4_state *
1357_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001358{
1359 struct inode *inode;
1360 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001361 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001362
Trond Myklebustaac00a82007-07-05 19:02:21 -04001363 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001364 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001365 goto out;
1366 }
1367
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001368 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001370 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001371 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001372 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001373 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001374 goto err;
1375 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376 state = nfs4_get_open_state(inode, data->owner);
1377 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001378 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001379 if (data->o_res.delegation_type != 0)
1380 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001381 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001382 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001383 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001384out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001385 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001386 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001387err_put_inode:
1388 iput(inode);
1389err:
1390 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001391}
1392
Andy Adamsone23008e2012-10-02 21:07:32 -04001393static struct nfs4_state *
1394nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1395{
1396 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1397 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1398 return _nfs4_opendata_to_nfs4_state(data);
1399}
1400
Trond Myklebust864472e2006-01-03 09:55:15 +01001401static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1402{
1403 struct nfs_inode *nfsi = NFS_I(state->inode);
1404 struct nfs_open_context *ctx;
1405
1406 spin_lock(&state->inode->i_lock);
1407 list_for_each_entry(ctx, &nfsi->open_files, list) {
1408 if (ctx->state != state)
1409 continue;
1410 get_nfs_open_context(ctx);
1411 spin_unlock(&state->inode->i_lock);
1412 return ctx;
1413 }
1414 spin_unlock(&state->inode->i_lock);
1415 return ERR_PTR(-ENOENT);
1416}
1417
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001418static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1419 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001420{
1421 struct nfs4_opendata *opendata;
1422
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001423 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001424 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001425 if (opendata == NULL)
1426 return ERR_PTR(-ENOMEM);
1427 opendata->state = state;
1428 atomic_inc(&state->count);
1429 return opendata;
1430}
1431
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001432static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001433{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001434 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001435 int ret;
1436
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001437 opendata->o_arg.open_flags = 0;
1438 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001439 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1440 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1441 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001442 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001443 if (ret != 0)
1444 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001445 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001446 if (IS_ERR(newstate))
1447 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001448 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001449 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001450 return 0;
1451}
1452
1453static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1454{
Trond Myklebust864472e2006-01-03 09:55:15 +01001455 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001456 int ret;
1457
1458 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001459 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001460 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001461 smp_rmb();
1462 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001463 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001464 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001465 if (ret != 0)
1466 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001467 if (newstate != state)
1468 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001469 }
1470 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001471 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001472 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001473 if (ret != 0)
1474 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001475 if (newstate != state)
1476 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001477 }
1478 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001479 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001480 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001481 if (ret != 0)
1482 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001483 if (newstate != state)
1484 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001485 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001486 /*
1487 * We may have performed cached opens for all three recoveries.
1488 * Check if we need to update the current stateid.
1489 */
1490 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001491 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001492 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001493 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001494 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001495 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001496 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001497 return 0;
1498}
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500/*
1501 * OPEN_RECLAIM:
1502 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001504static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001506 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001507 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001508 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 int status;
1510
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001511 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1512 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001513 if (IS_ERR(opendata))
1514 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001515 rcu_read_lock();
1516 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001517 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001518 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001519 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001520 opendata->o_arg.u.delegation_type = delegation_type;
1521 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001522 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return status;
1524}
1525
Trond Myklebust539cd032007-06-05 11:46:42 -04001526static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 struct nfs_server *server = NFS_SERVER(state->inode);
1529 struct nfs4_exception exception = { };
1530 int err;
1531 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001532 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001533 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001534 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1535 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001536 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001537 break;
1538 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 } while (exception.retry);
1540 return err;
1541}
1542
Trond Myklebust864472e2006-01-03 09:55:15 +01001543static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1544{
1545 struct nfs_open_context *ctx;
1546 int ret;
1547
1548 ctx = nfs4_state_find_open_context(state);
1549 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001550 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001551 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001552 put_nfs_open_context(ctx);
1553 return ret;
1554}
1555
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001556static 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 -07001557{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001558 switch (err) {
1559 default:
1560 printk(KERN_ERR "NFS: %s: unhandled error "
1561 "%d.\n", __func__, err);
1562 case 0:
1563 case -ENOENT:
1564 case -ESTALE:
1565 break;
1566 case -NFS4ERR_BADSESSION:
1567 case -NFS4ERR_BADSLOT:
1568 case -NFS4ERR_BAD_HIGH_SLOT:
1569 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1570 case -NFS4ERR_DEADSESSION:
1571 set_bit(NFS_DELEGATED_STATE, &state->flags);
1572 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1573 return -EAGAIN;
1574 case -NFS4ERR_STALE_CLIENTID:
1575 case -NFS4ERR_STALE_STATEID:
1576 set_bit(NFS_DELEGATED_STATE, &state->flags);
1577 case -NFS4ERR_EXPIRED:
1578 /* Don't recall a delegation if it was lost */
1579 nfs4_schedule_lease_recovery(server->nfs_client);
1580 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001581 case -NFS4ERR_MOVED:
1582 nfs4_schedule_migration_recovery(server);
1583 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001584 case -NFS4ERR_LEASE_MOVED:
1585 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1586 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001587 case -NFS4ERR_DELEG_REVOKED:
1588 case -NFS4ERR_ADMIN_REVOKED:
1589 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001590 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001591 nfs_inode_find_state_and_recover(state->inode,
1592 stateid);
1593 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001594 return 0;
1595 case -NFS4ERR_DELAY:
1596 case -NFS4ERR_GRACE:
1597 set_bit(NFS_DELEGATED_STATE, &state->flags);
1598 ssleep(1);
1599 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001600 case -ENOMEM:
1601 case -NFS4ERR_DENIED:
1602 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1603 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return err;
1606}
1607
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001608int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1609{
1610 struct nfs_server *server = NFS_SERVER(state->inode);
1611 struct nfs4_opendata *opendata;
1612 int err;
1613
1614 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1615 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1616 if (IS_ERR(opendata))
1617 return PTR_ERR(opendata);
1618 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1619 err = nfs4_open_recover(opendata, state);
1620 nfs4_opendata_put(opendata);
1621 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1622}
1623
Chuck Leverbe05c862013-08-09 12:49:47 -04001624static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1625{
1626 struct nfs4_opendata *data = calldata;
1627
1628 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1629 &data->o_res.seq_res, task);
1630}
1631
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001632static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1633{
1634 struct nfs4_opendata *data = calldata;
1635
Chuck Leverbe05c862013-08-09 12:49:47 -04001636 nfs40_sequence_done(task, &data->o_res.seq_res);
1637
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001638 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001639 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001640 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001641 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001642 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001643 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001644 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001645}
1646
1647static void nfs4_open_confirm_release(void *calldata)
1648{
1649 struct nfs4_opendata *data = calldata;
1650 struct nfs4_state *state = NULL;
1651
1652 /* If this request hasn't been cancelled, do nothing */
1653 if (data->cancelled == 0)
1654 goto out_free;
1655 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001656 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001657 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001658 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001659 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001660 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001661out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001662 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001663}
1664
1665static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001666 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001667 .rpc_call_done = nfs4_open_confirm_done,
1668 .rpc_release = nfs4_open_confirm_release,
1669};
1670
1671/*
1672 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1673 */
1674static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1675{
1676 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1677 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001678 struct rpc_message msg = {
1679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1680 .rpc_argp = &data->c_arg,
1681 .rpc_resp = &data->c_res,
1682 .rpc_cred = data->owner->so_cred,
1683 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001684 struct rpc_task_setup task_setup_data = {
1685 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001686 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001687 .callback_ops = &nfs4_open_confirm_ops,
1688 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001689 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001690 .flags = RPC_TASK_ASYNC,
1691 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 int status;
1693
Chuck Leverbe05c862013-08-09 12:49:47 -04001694 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001695 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001696 data->rpc_done = 0;
1697 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001698 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001699 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001700 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001701 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001702 status = nfs4_wait_for_completion_rpc_task(task);
1703 if (status != 0) {
1704 data->cancelled = 1;
1705 smp_wmb();
1706 } else
1707 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001708 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return status;
1710}
1711
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001712static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001714 struct nfs4_opendata *data = calldata;
1715 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001716 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001717
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001718 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001719 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001720 /*
1721 * Check if we still need to send an OPEN call, or if we can use
1722 * a delegation instead.
1723 */
1724 if (data->state != NULL) {
1725 struct nfs_delegation *delegation;
1726
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001727 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001728 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001729 rcu_read_lock();
1730 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001731 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001732 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001733 can_open_delegated(delegation, data->o_arg.fmode))
1734 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001735 rcu_read_unlock();
1736 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001737 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001738 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001739 switch (data->o_arg.claim) {
1740 case NFS4_OPEN_CLAIM_PREVIOUS:
1741 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1742 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001743 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001744 case NFS4_OPEN_CLAIM_FH:
1745 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001746 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1747 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001748 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001749 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001750 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001751 &data->o_res.seq_res,
1752 task) != 0)
1753 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001754
1755 /* Set the create mode (note dependency on the session type) */
1756 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1757 if (data->o_arg.open_flags & O_EXCL) {
1758 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1759 if (nfs4_has_persistent_session(clp))
1760 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1761 else if (clp->cl_mvops->minor_version > 0)
1762 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1763 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001764 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001765unlock_no_action:
1766 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001767out_no_action:
1768 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001769out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001770 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001771}
1772
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773static void nfs4_open_done(struct rpc_task *task, void *calldata)
1774{
1775 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001777 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001778
Trond Myklebust14516c32010-07-31 14:29:06 -04001779 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1780 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001781
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001782 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001783 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1784 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001785 case S_IFREG:
1786 break;
1787 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001788 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001789 break;
1790 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001791 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001792 break;
1793 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001794 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001795 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001796 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001797 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001798 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1799 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001800 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001801 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001802}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001803
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001804static void nfs4_open_release(void *calldata)
1805{
1806 struct nfs4_opendata *data = calldata;
1807 struct nfs4_state *state = NULL;
1808
1809 /* If this request hasn't been cancelled, do nothing */
1810 if (data->cancelled == 0)
1811 goto out_free;
1812 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001813 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001814 goto out_free;
1815 /* In case we need an open_confirm, no cleanup! */
1816 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1817 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001818 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001819 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001820 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001821out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001822 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001823}
1824
1825static const struct rpc_call_ops nfs4_open_ops = {
1826 .rpc_call_prepare = nfs4_open_prepare,
1827 .rpc_call_done = nfs4_open_done,
1828 .rpc_release = nfs4_open_release,
1829};
1830
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001831static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001832{
1833 struct inode *dir = data->dir->d_inode;
1834 struct nfs_server *server = NFS_SERVER(dir);
1835 struct nfs_openargs *o_arg = &data->o_arg;
1836 struct nfs_openres *o_res = &data->o_res;
1837 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001838 struct rpc_message msg = {
1839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1840 .rpc_argp = o_arg,
1841 .rpc_resp = o_res,
1842 .rpc_cred = data->owner->so_cred,
1843 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001844 struct rpc_task_setup task_setup_data = {
1845 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001846 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001847 .callback_ops = &nfs4_open_ops,
1848 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001849 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001850 .flags = RPC_TASK_ASYNC,
1851 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001852 int status;
1853
Chuck Levera9c92d62013-08-09 12:48:18 -04001854 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001855 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001856 data->rpc_done = 0;
1857 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001858 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001859 data->is_recover = 0;
1860 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001861 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001862 data->is_recover = 1;
1863 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001864 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001865 if (IS_ERR(task))
1866 return PTR_ERR(task);
1867 status = nfs4_wait_for_completion_rpc_task(task);
1868 if (status != 0) {
1869 data->cancelled = 1;
1870 smp_wmb();
1871 } else
1872 status = data->rpc_status;
1873 rpc_put_task(task);
1874
1875 return status;
1876}
1877
1878static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1879{
1880 struct inode *dir = data->dir->d_inode;
1881 struct nfs_openres *o_res = &data->o_res;
1882 int status;
1883
1884 status = nfs4_run_open_task(data, 1);
1885 if (status != 0 || !data->rpc_done)
1886 return status;
1887
Trond Myklebust6926afd2012-01-07 13:22:46 -05001888 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1889
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001890 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1891 status = _nfs4_proc_open_confirm(data);
1892 if (status != 0)
1893 return status;
1894 }
1895
1896 return status;
1897}
1898
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001899static int nfs4_opendata_access(struct rpc_cred *cred,
1900 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001901 struct nfs4_state *state, fmode_t fmode,
1902 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001903{
1904 struct nfs_access_entry cache;
1905 u32 mask;
1906
1907 /* access call failed or for some reason the server doesn't
1908 * support any access modes -- defer access call until later */
1909 if (opendata->o_res.access_supported == 0)
1910 return 0;
1911
1912 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001913 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001914 * write mode bits, but POSIX allows the creating process to write.
1915 * use openflags to check for exec, because fmode won't
1916 * always have FMODE_EXEC set when file open for exec. */
1917 if (openflags & __FMODE_EXEC) {
1918 /* ONLY check for exec rights */
1919 mask = MAY_EXEC;
1920 } else if (fmode & FMODE_READ)
1921 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001922
1923 cache.cred = cred;
1924 cache.jiffies = jiffies;
1925 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1926 nfs_access_add_cache(state->inode, &cache);
1927
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001928 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001929 return 0;
1930
1931 /* even though OPEN succeeded, access is denied. Close the file */
1932 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001933 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001934}
1935
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001936/*
1937 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1938 */
1939static int _nfs4_proc_open(struct nfs4_opendata *data)
1940{
1941 struct inode *dir = data->dir->d_inode;
1942 struct nfs_server *server = NFS_SERVER(dir);
1943 struct nfs_openargs *o_arg = &data->o_arg;
1944 struct nfs_openres *o_res = &data->o_res;
1945 int status;
1946
1947 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001948 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001949 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001950 if (status != 0) {
1951 if (status == -NFS4ERR_BADNAME &&
1952 !(o_arg->open_flags & O_CREAT))
1953 return -ENOENT;
1954 return status;
1955 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001956
Trond Myklebust6926afd2012-01-07 13:22:46 -05001957 nfs_fattr_map_and_free_names(server, &data->f_attr);
1958
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001959 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001960 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001961 if (o_arg->open_flags & O_EXCL)
1962 data->file_created = 1;
1963 else if (o_res->cinfo.before != o_res->cinfo.after)
1964 data->file_created = 1;
1965 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001966 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1967 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001969 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001971 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001974 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Andy Adamsond83217c2011-03-01 01:34:17 +00001978static int nfs4_recover_expired_lease(struct nfs_server *server)
1979{
1980 return nfs4_client_recover_expired_lease(server->nfs_client);
1981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983/*
1984 * OPEN_EXPIRED:
1985 * reclaim state on the server after a network partition.
1986 * Assumes caller holds the appropriate lock
1987 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001988static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001990 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001991 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001993 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001994 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001995 if (IS_ERR(opendata))
1996 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001997 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001998 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001999 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002000 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002001 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002004static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002005{
Trond Myklebust539cd032007-06-05 11:46:42 -04002006 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002007 struct nfs4_exception exception = { };
2008 int err;
2009
2010 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002011 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002012 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002013 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2014 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002015 switch (err) {
2016 default:
2017 goto out;
2018 case -NFS4ERR_GRACE:
2019 case -NFS4ERR_DELAY:
2020 nfs4_handle_exception(server, err, &exception);
2021 err = 0;
2022 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002023 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002024out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002025 return err;
2026}
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2029{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002031 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Trond Myklebust864472e2006-01-03 09:55:15 +01002033 ctx = nfs4_state_find_open_context(state);
2034 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002035 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002036 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002037 put_nfs_open_context(ctx);
2038 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002041#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002042static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002043{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002044 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002045 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002046 struct nfs_delegation *delegation;
2047 struct rpc_cred *cred = NULL;
2048 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002049
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002050 /* If a state reset has been done, test_stateid is unneeded */
2051 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2052 return;
2053
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002054 /* Get the delegation credential for use by test/free_stateid */
2055 rcu_read_lock();
2056 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2057 if (delegation != NULL &&
2058 nfs4_stateid_match(&delegation->stateid, stateid)) {
2059 cred = get_rpccred(delegation->cred);
2060 rcu_read_unlock();
2061 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002062 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002063 } else
2064 rcu_read_unlock();
2065
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002066 if (status != NFS_OK) {
2067 /* Free the stateid unless the server explicitly
2068 * informs us the stateid is unrecognized. */
2069 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002070 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002071 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002072
Bryan Schumaker69388672012-09-26 15:25:52 -04002073 write_seqlock(&state->seqlock);
2074 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2075 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002076 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2077 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002078
2079 if (cred != NULL)
2080 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002081}
2082
2083/**
2084 * nfs41_check_open_stateid - possibly free an open stateid
2085 *
2086 * @state: NFSv4 state for an inode
2087 *
2088 * Returns NFS_OK if recovery for this stateid is now finished.
2089 * Otherwise a negative NFS4ERR value is returned.
2090 */
2091static int nfs41_check_open_stateid(struct nfs4_state *state)
2092{
2093 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002094 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002095 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002096 int status;
2097
2098 /* If a state reset has been done, test_stateid is unneeded */
2099 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2100 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2101 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2102 return -NFS4ERR_BAD_STATEID;
2103
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002104 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002105 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002106 if (status != NFS_OK) {
2107 /* Free the stateid unless the server explicitly
2108 * informs us the stateid is unrecognized. */
2109 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002110 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002111
2112 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2113 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2114 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002115 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002116 }
2117 return status;
2118}
2119
2120static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2121{
Chuck Levereb64cf92012-07-11 16:30:05 -04002122 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002123
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002124 nfs41_clear_delegation_stateid(state);
2125 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002126 if (status != NFS_OK)
2127 status = nfs4_open_expired(sp, state);
2128 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002129}
2130#endif
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002133 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2134 * fields corresponding to attributes that were used to store the verifier.
2135 * Make sure we clobber those fields in the later setattr call
2136 */
2137static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2138{
2139 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2140 !(sattr->ia_valid & ATTR_ATIME_SET))
2141 sattr->ia_valid |= ATTR_ATIME;
2142
2143 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2144 !(sattr->ia_valid & ATTR_MTIME_SET))
2145 sattr->ia_valid |= ATTR_MTIME;
2146}
2147
Trond Myklebustc21443c2013-02-07 14:26:21 -05002148static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2149 fmode_t fmode,
2150 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002151 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002152{
2153 struct nfs4_state_owner *sp = opendata->owner;
2154 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002155 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002156 struct nfs4_state *state;
2157 unsigned int seq;
2158 int ret;
2159
2160 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2161
2162 ret = _nfs4_proc_open(opendata);
2163 if (ret != 0)
2164 goto out;
2165
2166 state = nfs4_opendata_to_nfs4_state(opendata);
2167 ret = PTR_ERR(state);
2168 if (IS_ERR(state))
2169 goto out;
2170 if (server->caps & NFS_CAP_POSIX_LOCK)
2171 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2172
Trond Myklebust275bb302013-05-29 13:11:28 -04002173 dentry = opendata->dentry;
2174 if (dentry->d_inode == NULL) {
2175 /* FIXME: Is this d_drop() ever needed? */
2176 d_drop(dentry);
2177 dentry = d_add_unique(dentry, igrab(state->inode));
2178 if (dentry == NULL) {
2179 dentry = opendata->dentry;
2180 } else if (dentry != ctx->dentry) {
2181 dput(ctx->dentry);
2182 ctx->dentry = dget(dentry);
2183 }
2184 nfs_set_verifier(dentry,
2185 nfs_save_change_attribute(opendata->dir->d_inode));
2186 }
2187
Trond Myklebustc21443c2013-02-07 14:26:21 -05002188 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2189 if (ret != 0)
2190 goto out;
2191
Trond Myklebust3efb9722013-05-29 13:17:04 -04002192 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002193 if (dentry->d_inode == state->inode) {
2194 nfs_inode_attach_open_context(ctx);
2195 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2196 nfs4_schedule_stateid_recovery(server, state);
2197 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002198out:
2199 return ret;
2200}
2201
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002202/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002203 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 */
Andy Adamson82be4172012-05-23 05:02:35 -04002205static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002206 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002207 int flags,
2208 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002209 struct nfs4_label *label,
2210 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 struct nfs4_state_owner *sp;
2213 struct nfs4_state *state = NULL;
2214 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002215 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002216 struct dentry *dentry = ctx->dentry;
2217 struct rpc_cred *cred = ctx->cred;
2218 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2219 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002220 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002221 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002222 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002226 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2227 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2229 goto out_err;
2230 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002231 status = nfs4_recover_expired_lease(server);
2232 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002233 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002234 if (dentry->d_inode != NULL)
2235 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002236 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002237 if (dentry->d_inode)
2238 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002239 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002240 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002241 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002242 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
David Quigley14c43f72013-05-22 12:50:43 -04002244 if (label) {
2245 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2246 if (IS_ERR(olabel)) {
2247 status = PTR_ERR(olabel);
2248 goto err_opendata_put;
2249 }
2250 }
2251
Andy Adamson82be4172012-05-23 05:02:35 -04002252 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2253 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2254 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002255 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002256 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002257 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002258 if (dentry->d_inode != NULL)
2259 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002260
Trond Myklebust3efb9722013-05-29 13:17:04 -04002261 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002262 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002263 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002264 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002265
Trond Myklebust549b19c2013-04-16 18:42:34 -04002266 if ((opendata->o_arg.open_flags & O_EXCL) &&
2267 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002268 nfs4_exclusive_attrset(opendata, sattr);
2269
2270 nfs_fattr_init(opendata->o_res.f_attr);
2271 status = nfs4_do_setattr(state->inode, cred,
2272 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002273 state, label, olabel);
2274 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002275 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002276 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002277 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002278 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002279 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002280 if (opendata->file_created)
2281 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002282
2283 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2284 *ctx_th = opendata->f_attr.mdsthreshold;
2285 else
2286 kfree(opendata->f_attr.mdsthreshold);
2287 opendata->f_attr.mdsthreshold = NULL;
2288
David Quigley14c43f72013-05-22 12:50:43 -04002289 nfs4_label_free(olabel);
2290
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002291 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002294err_free_label:
2295 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002296err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002297 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002298 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002299err_put_state_owner:
2300 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return status;
2303}
2304
2305
Andy Adamson82be4172012-05-23 05:02:35 -04002306static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002307 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002308 int flags,
2309 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002310 struct nfs4_label *label,
2311 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002313 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 struct nfs4_exception exception = { };
2315 struct nfs4_state *res;
2316 int status;
2317
2318 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002319 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002320 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002321 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (status == 0)
2323 break;
2324 /* NOTE: BAD_SEQID means the server and client disagree about the
2325 * book-keeping w.r.t. state-changing operations
2326 * (OPEN/CLOSE/LOCK/LOCKU...)
2327 * It is actually a sign of a bug on the client or on the server.
2328 *
2329 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002330 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 * have unhashed the old state_owner for us, and that we can
2332 * therefore safely retry using a new one. We should still warn
2333 * the user though...
2334 */
2335 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002336 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002337 " returned a bad sequence-id error!\n",
2338 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 exception.retry = 1;
2340 continue;
2341 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002342 /*
2343 * BAD_STATEID on OPEN means that the server cancelled our
2344 * state before it received the OPEN_CONFIRM.
2345 * Recover by retrying the request as per the discussion
2346 * on Page 181 of RFC3530.
2347 */
2348 if (status == -NFS4ERR_BAD_STATEID) {
2349 exception.retry = 1;
2350 continue;
2351 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002352 if (status == -EAGAIN) {
2353 /* We must have found a delegation */
2354 exception.retry = 1;
2355 continue;
2356 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002357 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2358 continue;
2359 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 status, &exception));
2361 } while (exception.retry);
2362 return res;
2363}
2364
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002365static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2366 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002367 struct nfs4_state *state, struct nfs4_label *ilabel,
2368 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002370 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002372 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 .iap = sattr,
2374 .server = server,
2375 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002376 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 };
2378 struct nfs_setattrres res = {
2379 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002380 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 .server = server,
2382 };
2383 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2385 .rpc_argp = &arg,
2386 .rpc_resp = &res,
2387 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002389 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002390 fmode_t fmode;
2391 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002392 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
David Quigleyaa9c2662013-05-22 12:50:44 -04002394 arg.bitmask = nfs4_bitmask(server, ilabel);
2395 if (ilabel)
2396 arg.bitmask = nfs4_bitmask(server, olabel);
2397
Trond Myklebust0e574af2005-10-27 22:12:38 -04002398 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Trond Myklebustee3ae842013-04-29 10:35:36 -04002400 /* Servers should only apply open mode checks for file size changes */
2401 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2402 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2403
2404 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2405 /* Use that stateid */
2406 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002407 struct nfs_lockowner lockowner = {
2408 .l_owner = current->files,
2409 .l_pid = current->tgid,
2410 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002411 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002412 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002413 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002414 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Bryan Schumaker7c513052011-03-24 17:12:24 +00002416 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002417 if (status == 0 && state != NULL)
2418 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002419 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002422static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2423 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002424 struct nfs4_state *state, struct nfs4_label *ilabel,
2425 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002427 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002428 struct nfs4_exception exception = {
2429 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002430 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002431 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 int err;
2433 do {
David Quigley1775fd32013-05-22 12:50:42 -04002434 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002435 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002436 switch (err) {
2437 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002438 if (!(sattr->ia_valid & ATTR_SIZE)) {
2439 pr_warn_once("NFSv4: server %s is incorrectly "
2440 "applying open mode checks to "
2441 "a SETATTR that is not "
2442 "changing file size.\n",
2443 server->nfs_client->cl_hostname);
2444 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002445 if (state && !(state->state & FMODE_WRITE)) {
2446 err = -EBADF;
2447 if (sattr->ia_valid & ATTR_OPEN)
2448 err = -EACCES;
2449 goto out;
2450 }
2451 }
2452 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002454out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return err;
2456}
2457
2458struct nfs4_closedata {
2459 struct inode *inode;
2460 struct nfs4_state *state;
2461 struct nfs_closeargs arg;
2462 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002463 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002464 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002465 bool roc;
2466 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467};
2468
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002469static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002470{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002471 struct nfs4_closedata *calldata = data;
2472 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002473 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002474
Fred Isamanf7e89172011-01-06 11:36:32 +00002475 if (calldata->roc)
2476 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002477 nfs4_put_open_state(calldata->state);
2478 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002479 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002480 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002481 kfree(calldata);
2482}
2483
Trond Myklebust88069f72009-12-08 08:33:16 -05002484static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2485 fmode_t fmode)
2486{
2487 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002488 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002489 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2490 case FMODE_WRITE:
2491 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2492 break;
2493 case FMODE_READ:
2494 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2495 break;
2496 case 0:
2497 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2498 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2499 clear_bit(NFS_OPEN_STATE, &state->flags);
2500 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002501 spin_unlock(&state->owner->so_lock);
2502}
2503
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002504static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002506 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 struct nfs_server *server = NFS_SERVER(calldata->inode);
2509
Chuck Levera3ca5652012-03-01 17:00:40 -05002510 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002511 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2512 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002513 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 /* hmm. we are done with the inode, and in the process of freeing
2515 * the state_owner. we keep this around to process errors
2516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 switch (task->tk_status) {
2518 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002519 if (calldata->roc)
2520 pnfs_roc_set_barrier(state->inode,
2521 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002522 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002523 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002524 nfs4_close_clear_stateid_flags(state,
2525 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 break;
2527 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002528 case -NFS4ERR_OLD_STATEID:
2529 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002531 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002534 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2535 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002537 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002538 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002539 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002542static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002544 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002545 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002546 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002547 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002548
Chuck Levera3ca5652012-03-01 17:00:40 -05002549 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002550 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002551 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002552
Trond Myklebust88069f72009-12-08 08:33:16 -05002553 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2554 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002555 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002556 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002557 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002558 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002559 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2560 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2561 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002562 }
2563 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002564 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2565 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2566 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002567 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002568 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002569 if (!nfs4_valid_open_stateid(state))
2570 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002571 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002572
2573 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002574 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002575 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002576 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002577
Fred Isamanf7e89172011-01-06 11:36:32 +00002578 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002579 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002580 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002581 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2582 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002583 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002584 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002585 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002586
Trond Myklebust516a6af2005-10-27 22:12:41 -04002587 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002588 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002589 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002590 &calldata->arg.seq_args,
2591 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002592 task) != 0)
2593 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002594 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002595 return;
2596out_no_action:
2597 task->tk_action = NULL;
2598out_wait:
2599 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002602static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002603 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002604 .rpc_call_done = nfs4_close_done,
2605 .rpc_release = nfs4_free_closedata,
2606};
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608/*
2609 * It is possible for data to be read/written from a mem-mapped file
2610 * after the sys_close call (which hits the vfs layer as a flush).
2611 * This means that we can't safely call nfsv4 close on a file until
2612 * the inode is cleared. This in turn means that we are not good
2613 * NFSv4 citizens - we do not indicate to the server to update the file's
2614 * share state even when we are done with one of the three share
2615 * stateid's in the inode.
2616 *
2617 * NOTE: Caller must be holding the sp->so_owner semaphore!
2618 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002619int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002621 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002623 struct nfs4_state_owner *sp = state->owner;
2624 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002625 struct rpc_message msg = {
2626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2627 .rpc_cred = state->owner->so_cred,
2628 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002629 struct rpc_task_setup task_setup_data = {
2630 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002631 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002632 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002633 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002634 .flags = RPC_TASK_ASYNC,
2635 };
Trond Myklebust95121352005-10-18 14:20:12 -07002636 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002638 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2639 &task_setup_data.rpc_client, &msg);
2640
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002641 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002643 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002644 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002645 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002647 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002648 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002650 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002651 if (calldata->arg.seqid == NULL)
2652 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002653 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002654 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002655 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002656 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002657 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002658 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002659 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002660
Trond Myklebust1174dd12010-12-21 10:52:24 -05002661 msg.rpc_argp = &calldata->arg;
2662 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002663 task_setup_data.callback_data = calldata;
2664 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002665 if (IS_ERR(task))
2666 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002667 status = 0;
2668 if (wait)
2669 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002670 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002671 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002672out_free_calldata:
2673 kfree(calldata);
2674out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002675 nfs4_put_open_state(state);
2676 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002677 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
2679
Trond Myklebust2b484292010-09-17 10:56:51 -04002680static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002681nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2682 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002685 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2686
2687 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Trond Myklebust565277f2007-10-15 18:17:53 -04002689 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002690 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002691
2692 nfs4_label_release_security(label);
2693
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002694 if (IS_ERR(state))
2695 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002696 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
Trond Myklebust1185a552009-12-03 15:54:02 -05002699static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002700{
2701 if (ctx->state == NULL)
2702 return;
2703 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002704 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002705 else
Al Viro643168c2011-06-22 18:20:23 -04002706 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2710{
Benny Halevy43652ad2009-04-01 09:21:54 -04002711 struct nfs4_server_caps_arg args = {
2712 .fhandle = fhandle,
2713 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 struct nfs4_server_caps_res res = {};
2715 struct rpc_message msg = {
2716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002717 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 .rpc_resp = &res,
2719 };
2720 int status;
2721
Bryan Schumaker7c513052011-03-24 17:12:24 +00002722 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (status == 0) {
2724 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002725 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2726 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2727 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2728 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2729 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2731 server->caps |= NFS_CAP_ACLS;
2732 if (res.has_links != 0)
2733 server->caps |= NFS_CAP_HARDLINKS;
2734 if (res.has_symlinks != 0)
2735 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002736 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2737 server->caps |= NFS_CAP_FILEID;
2738 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2739 server->caps |= NFS_CAP_MODE;
2740 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2741 server->caps |= NFS_CAP_NLINK;
2742 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2743 server->caps |= NFS_CAP_OWNER;
2744 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2745 server->caps |= NFS_CAP_OWNER_GROUP;
2746 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2747 server->caps |= NFS_CAP_ATIME;
2748 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2749 server->caps |= NFS_CAP_CTIME;
2750 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2751 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002752#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2753 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2754 server->caps |= NFS_CAP_SECURITY_LABEL;
2755#endif
2756 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2757 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002758
David Quigleyaa9c2662013-05-22 12:50:44 -04002759 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2760 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2761 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2762 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002763 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2764 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2765 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002767 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return status;
2771}
2772
Trond Myklebust55a97592006-06-09 09:34:19 -04002773int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 struct nfs4_exception exception = { };
2776 int err;
2777 do {
2778 err = nfs4_handle_exception(server,
2779 _nfs4_server_capabilities(server, fhandle),
2780 &exception);
2781 } while (exception.retry);
2782 return err;
2783}
2784
2785static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2786 struct nfs_fsinfo *info)
2787{
David Quigleyaa9c2662013-05-22 12:50:44 -04002788 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002790 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 };
2792 struct nfs4_lookup_res res = {
2793 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002794 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 .fh = fhandle,
2796 };
2797 struct rpc_message msg = {
2798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2799 .rpc_argp = &args,
2800 .rpc_resp = &res,
2801 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002802
David Quigleyaa9c2662013-05-22 12:50:44 -04002803 bitmask[0] = nfs4_fattr_bitmap[0];
2804 bitmask[1] = nfs4_fattr_bitmap[1];
2805 /*
2806 * Process the label in the upcoming getfattr
2807 */
2808 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2809
Trond Myklebust0e574af2005-10-27 22:12:38 -04002810 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002811 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
2814static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2815 struct nfs_fsinfo *info)
2816{
2817 struct nfs4_exception exception = { };
2818 int err;
2819 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002820 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002821 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002822 switch (err) {
2823 case 0:
2824 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002825 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002826 default:
2827 err = nfs4_handle_exception(server, err, &exception);
2828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002830out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return err;
2832}
2833
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002834static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2835 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2836{
Trond Myklebustc2190662013-08-26 19:23:04 -04002837 struct rpc_auth_create_args auth_args = {
2838 .pseudoflavor = flavor,
2839 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002840 struct rpc_auth *auth;
2841 int ret;
2842
Trond Myklebustc2190662013-08-26 19:23:04 -04002843 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002844 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002845 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002846 goto out;
2847 }
2848 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002849out:
2850 return ret;
2851}
2852
Chuck Lever9a744ba2013-03-16 15:56:02 -04002853/*
2854 * Retry pseudoroot lookup with various security flavors. We do this when:
2855 *
2856 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2857 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2858 *
2859 * Returns zero on success, or a negative NFS4ERR value, or a
2860 * negative errno value.
2861 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002862static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002863 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002865 /* Per 3530bis 15.33.5 */
2866 static const rpc_authflavor_t flav_array[] = {
2867 RPC_AUTH_GSS_KRB5P,
2868 RPC_AUTH_GSS_KRB5I,
2869 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002870 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002871 RPC_AUTH_NULL,
2872 };
2873 int status = -EPERM;
2874 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Chuck Lever9a744ba2013-03-16 15:56:02 -04002876 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002877 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002878 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002879 continue;
2880 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002881 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002882
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002883 /*
2884 * -EACCESS could mean that the user doesn't have correct permissions
2885 * to access the mount. It could also mean that we tried to mount
2886 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2887 * existing mount programs don't handle -EACCES very well so it should
2888 * be mapped to -EPERM instead.
2889 */
2890 if (status == -EACCES)
2891 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002892 return status;
2893}
2894
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002895static int nfs4_do_find_root_sec(struct nfs_server *server,
2896 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2897{
2898 int mv = server->nfs_client->cl_minorversion;
2899 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2900}
2901
2902/**
2903 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2904 * @server: initialized nfs_server handle
2905 * @fhandle: we fill in the pseudo-fs root file handle
2906 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002907 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002908 *
2909 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002910 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002911int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002912 struct nfs_fsinfo *info,
2913 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002914{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002915 int status;
2916
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002917 switch (auth_probe) {
2918 case false:
2919 status = nfs4_lookup_root(server, fhandle, info);
2920 if (status != -NFS4ERR_WRONGSEC)
2921 break;
2922 /* Did user force a 'sec=' mount option? */
2923 if (server->flags & NFS_MOUNT_SECFLAVOUR)
2924 break;
2925 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002926 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002927 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002928
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if (status == 0)
2930 status = nfs4_server_capabilities(server, fhandle);
2931 if (status == 0)
2932 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002933
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002934 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935}
2936
Bryan Schumakerbae36242012-05-10 15:07:31 -04002937static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2938 struct nfs_fsinfo *info)
2939{
2940 int error;
2941 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002942 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002943
2944 error = nfs4_server_capabilities(server, mntfh);
2945 if (error < 0) {
2946 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2947 return error;
2948 }
2949
David Quigley14c43f72013-05-22 12:50:43 -04002950 label = nfs4_label_alloc(server, GFP_KERNEL);
2951 if (IS_ERR(label))
2952 return PTR_ERR(label);
2953
David Quigley1775fd32013-05-22 12:50:42 -04002954 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002955 if (error < 0) {
2956 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002957 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002958 }
2959
2960 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2961 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2962 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2963
David Quigley14c43f72013-05-22 12:50:43 -04002964err_free_label:
2965 nfs4_label_free(label);
2966
Bryan Schumakerbae36242012-05-10 15:07:31 -04002967 return error;
2968}
2969
Manoj Naik6b97fd32006-06-09 09:34:29 -04002970/*
2971 * Get locations and (maybe) other attributes of a referral.
2972 * Note that we'll actually follow the referral later when
2973 * we detect fsid mismatch in inode revalidation
2974 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002975static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2976 const struct qstr *name, struct nfs_fattr *fattr,
2977 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002978{
2979 int status = -ENOMEM;
2980 struct page *page = NULL;
2981 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002982
2983 page = alloc_page(GFP_KERNEL);
2984 if (page == NULL)
2985 goto out;
2986 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2987 if (locations == NULL)
2988 goto out;
2989
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002990 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002991 if (status != 0)
2992 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04002993
2994 /*
2995 * If the fsid didn't change, this is a migration event, not a
2996 * referral. Cause us to drop into the exception handler, which
2997 * will kick off migration recovery.
2998 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002999 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003000 dprintk("%s: server did not return a different fsid for"
3001 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003002 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003003 goto out;
3004 }
Andy Adamson533eb462011-06-13 18:25:56 -04003005 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3006 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003007
Andy Adamson533eb462011-06-13 18:25:56 -04003008 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003009 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003010 memset(fhandle, 0, sizeof(struct nfs_fh));
3011out:
3012 if (page)
3013 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003014 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003015 return status;
3016}
3017
David Quigley1775fd32013-05-22 12:50:42 -04003018static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3019 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
3021 struct nfs4_getattr_arg args = {
3022 .fh = fhandle,
3023 .bitmask = server->attr_bitmask,
3024 };
3025 struct nfs4_getattr_res res = {
3026 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003027 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 .server = server,
3029 };
3030 struct rpc_message msg = {
3031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3032 .rpc_argp = &args,
3033 .rpc_resp = &res,
3034 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003035
3036 args.bitmask = nfs4_bitmask(server, label);
3037
Trond Myklebust0e574af2005-10-27 22:12:38 -04003038 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003039 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
David Quigley1775fd32013-05-22 12:50:42 -04003042static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3043 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
3045 struct nfs4_exception exception = { };
3046 int err;
3047 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003048 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3049 trace_nfs4_getattr(server, fhandle, fattr, err);
3050 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 &exception);
3052 } while (exception.retry);
3053 return err;
3054}
3055
3056/*
3057 * The file is not closed if it is opened due to the a request to change
3058 * the size of the file. The open call will not be needed once the
3059 * VFS layer lookup-intents are implemented.
3060 *
3061 * Close is called when the inode is destroyed.
3062 * If we haven't opened the file for O_WRONLY, we
3063 * need to in the size_change case to obtain a stateid.
3064 *
3065 * Got race?
3066 * Because OPEN is always done by name in nfsv4, it is
3067 * possible that we opened a different file by the same
3068 * name. We can recognize this race condition, but we
3069 * can't do anything about it besides returning an error.
3070 *
3071 * This will be fixed with VFS changes (lookup-intent).
3072 */
3073static int
3074nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3075 struct iattr *sattr)
3076{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003077 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003078 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003079 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003080 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 int status;
3082
Benny Halevy8a1636c2010-07-14 15:43:57 -04003083 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003084 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003085
Trond Myklebust0e574af2005-10-27 22:12:38 -04003086 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Andy Adamson26699402012-05-30 16:12:24 -04003088 /* Deal with open(O_TRUNC) */
3089 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003090 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003091
3092 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003093 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003094 return 0;
3095
Trond Myklebustd5308382005-11-04 15:33:38 -05003096 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003097 if (sattr->ia_valid & ATTR_FILE) {
3098 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003099
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003100 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003101 if (ctx) {
3102 cred = ctx->cred;
3103 state = ctx->state;
3104 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003105 }
3106
David Quigley14c43f72013-05-22 12:50:43 -04003107 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3108 if (IS_ERR(label))
3109 return PTR_ERR(label);
3110
3111 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003112 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003113 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003114 nfs_setsecurity(inode, fattr, label);
3115 }
David Quigley14c43f72013-05-22 12:50:43 -04003116 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return status;
3118}
3119
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003120static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3121 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003122 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003123{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003124 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003125 int status;
3126 struct nfs4_lookup_arg args = {
3127 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003128 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003129 .name = name,
3130 };
3131 struct nfs4_lookup_res res = {
3132 .server = server,
3133 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003134 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003135 .fh = fhandle,
3136 };
3137 struct rpc_message msg = {
3138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3139 .rpc_argp = &args,
3140 .rpc_resp = &res,
3141 };
3142
David Quigleyaa9c2662013-05-22 12:50:44 -04003143 args.bitmask = nfs4_bitmask(server, label);
3144
David Howells2b3de442006-08-22 20:06:09 -04003145 nfs_fattr_init(fattr);
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003148 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 dprintk("NFS reply lookup: %d\n", status);
3150 return status;
3151}
3152
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003153static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003154{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003155 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003156 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003157 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3158 fattr->nlink = 2;
3159}
3160
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003161static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3162 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003163 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003164{
3165 struct nfs4_exception exception = { };
3166 struct rpc_clnt *client = *clnt;
3167 int err;
3168 do {
David Quigley1775fd32013-05-22 12:50:42 -04003169 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003170 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003171 switch (err) {
3172 case -NFS4ERR_BADNAME:
3173 err = -ENOENT;
3174 goto out;
3175 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003176 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003177 goto out;
3178 case -NFS4ERR_WRONGSEC:
3179 err = -EPERM;
3180 if (client != *clnt)
3181 goto out;
Trond Myklebust41d058c2013-09-07 17:49:05 -04003182 /* No security negotiation if the user specified 'sec=' */
3183 if (NFS_SERVER(dir)->flags & NFS_MOUNT_SECFLAVOUR)
3184 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003185 client = nfs4_create_sec_client(client, dir, name);
3186 if (IS_ERR(client))
3187 return PTR_ERR(client);
3188
3189 exception.retry = 1;
3190 break;
3191 default:
3192 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3193 }
3194 } while (exception.retry);
3195
3196out:
3197 if (err == 0)
3198 *clnt = client;
3199 else if (client != *clnt)
3200 rpc_shutdown_client(client);
3201
3202 return err;
3203}
3204
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003205static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003206 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3207 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003209 int status;
3210 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003211
David Quigley1775fd32013-05-22 12:50:42 -04003212 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003213 if (client != NFS_CLIENT(dir)) {
3214 rpc_shutdown_client(client);
3215 nfs_fixup_secinfo_attributes(fattr);
3216 }
3217 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003220struct rpc_clnt *
3221nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3222 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3223{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003224 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003225 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003226
David Quigley1775fd32013-05-22 12:50:42 -04003227 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003228 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003229 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003230 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003231}
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3234{
Trond Myklebust76b32992007-08-10 17:45:11 -04003235 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 struct nfs4_accessargs args = {
3237 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003238 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003240 struct nfs4_accessres res = {
3241 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003242 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 struct rpc_message msg = {
3244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3245 .rpc_argp = &args,
3246 .rpc_resp = &res,
3247 .rpc_cred = entry->cred,
3248 };
3249 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003250 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252 /*
3253 * Determine which access bits we want to ask for...
3254 */
3255 if (mode & MAY_READ)
3256 args.access |= NFS4_ACCESS_READ;
3257 if (S_ISDIR(inode->i_mode)) {
3258 if (mode & MAY_WRITE)
3259 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3260 if (mode & MAY_EXEC)
3261 args.access |= NFS4_ACCESS_LOOKUP;
3262 } else {
3263 if (mode & MAY_WRITE)
3264 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3265 if (mode & MAY_EXEC)
3266 args.access |= NFS4_ACCESS_EXECUTE;
3267 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003268
3269 res.fattr = nfs_alloc_fattr();
3270 if (res.fattr == NULL)
3271 return -ENOMEM;
3272
Bryan Schumaker7c513052011-03-24 17:12:24 +00003273 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003275 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003276 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003278 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return status;
3280}
3281
3282static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3283{
3284 struct nfs4_exception exception = { };
3285 int err;
3286 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003287 err = _nfs4_proc_access(inode, entry);
3288 trace_nfs4_access(inode, err);
3289 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 &exception);
3291 } while (exception.retry);
3292 return err;
3293}
3294
3295/*
3296 * TODO: For the time being, we don't try to get any attributes
3297 * along with any of the zero-copy operations READ, READDIR,
3298 * READLINK, WRITE.
3299 *
3300 * In the case of the first three, we want to put the GETATTR
3301 * after the read-type operation -- this is because it is hard
3302 * to predict the length of a GETATTR response in v4, and thus
3303 * align the READ data correctly. This means that the GETATTR
3304 * may end up partially falling into the page cache, and we should
3305 * shift it into the 'tail' of the xdr_buf before processing.
3306 * To do this efficiently, we need to know the total length
3307 * of data received, which doesn't seem to be available outside
3308 * of the RPC layer.
3309 *
3310 * In the case of WRITE, we also want to put the GETATTR after
3311 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003312 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 *
3314 * Both of these changes to the XDR layer would in fact be quite
3315 * minor, but I decided to leave them for a subsequent patch.
3316 */
3317static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3318 unsigned int pgbase, unsigned int pglen)
3319{
3320 struct nfs4_readlink args = {
3321 .fh = NFS_FH(inode),
3322 .pgbase = pgbase,
3323 .pglen = pglen,
3324 .pages = &page,
3325 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003326 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 struct rpc_message msg = {
3328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3329 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003330 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 };
3332
Bryan Schumaker7c513052011-03-24 17:12:24 +00003333 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 -07003334}
3335
3336static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3337 unsigned int pgbase, unsigned int pglen)
3338{
3339 struct nfs4_exception exception = { };
3340 int err;
3341 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003342 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3343 trace_nfs4_readlink(inode, err);
3344 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 &exception);
3346 } while (exception.retry);
3347 return err;
3348}
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003351 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353static int
3354nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003355 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
David Quigleyaa9c2662013-05-22 12:50:44 -04003357 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003358 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003360 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 int status = 0;
3362
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003363 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3364 if (IS_ERR(ctx))
3365 return PTR_ERR(ctx);
3366
David Quigleyaa9c2662013-05-22 12:50:44 -04003367 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3368
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003369 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003370 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 if (IS_ERR(state)) {
3372 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003376 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003377 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return status;
3379}
3380
3381static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3382{
Trond Myklebust16e42952005-10-27 22:12:44 -04003383 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003384 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003386 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003388 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003389 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003390 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3393 .rpc_argp = &args,
3394 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003396 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003397
Bryan Schumaker7c513052011-03-24 17:12:24 +00003398 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003399 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003400 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return status;
3402}
3403
3404static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3405{
3406 struct nfs4_exception exception = { };
3407 int err;
3408 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003409 err = _nfs4_proc_remove(dir, name);
3410 trace_nfs4_remove(dir, name, err);
3411 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 &exception);
3413 } while (exception.retry);
3414 return err;
3415}
3416
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003417static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003419 struct nfs_server *server = NFS_SERVER(dir);
3420 struct nfs_removeargs *args = msg->rpc_argp;
3421 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003423 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003425 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003426
3427 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428}
3429
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003430static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3431{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003432 nfs4_setup_sequence(NFS_SERVER(data->dir),
3433 &data->args.seq_args,
3434 &data->res.seq_res,
3435 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436}
3437
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003438static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003440 struct nfs_unlinkdata *data = task->tk_calldata;
3441 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003442
Trond Myklebust14516c32010-07-31 14:29:06 -04003443 if (!nfs4_sequence_done(task, &res->seq_res))
3444 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003445 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003446 return 0;
3447 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003448 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449}
3450
Jeff Laytond3d41522010-09-17 17:31:57 -04003451static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3452{
3453 struct nfs_server *server = NFS_SERVER(dir);
3454 struct nfs_renameargs *arg = msg->rpc_argp;
3455 struct nfs_renameres *res = msg->rpc_resp;
3456
3457 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003458 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003459 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003460}
3461
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003462static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3463{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003464 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3465 &data->args.seq_args,
3466 &data->res.seq_res,
3467 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003468}
3469
3470static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3471 struct inode *new_dir)
3472{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003473 struct nfs_renamedata *data = task->tk_calldata;
3474 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003475
3476 if (!nfs4_sequence_done(task, &res->seq_res))
3477 return 0;
3478 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3479 return 0;
3480
3481 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003482 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003483 return 1;
3484}
3485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3487 struct inode *new_dir, struct qstr *new_name)
3488{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003489 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003490 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 .old_dir = NFS_FH(old_dir),
3492 .new_dir = NFS_FH(new_dir),
3493 .old_name = old_name,
3494 .new_name = new_name,
3495 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003496 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003497 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003498 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 struct rpc_message msg = {
3500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3501 .rpc_argp = &arg,
3502 .rpc_resp = &res,
3503 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003504 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003505
Bryan Schumaker7c513052011-03-24 17:12:24 +00003506 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 if (!status) {
3508 update_changeattr(old_dir, &res.old_cinfo);
3509 update_changeattr(new_dir, &res.new_cinfo);
3510 }
3511 return status;
3512}
3513
3514static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3515 struct inode *new_dir, struct qstr *new_name)
3516{
3517 struct nfs4_exception exception = { };
3518 int err;
3519 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003520 err = _nfs4_proc_rename(old_dir, old_name,
3521 new_dir, new_name);
3522 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3523 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 &exception);
3525 } while (exception.retry);
3526 return err;
3527}
3528
3529static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3530{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003531 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 struct nfs4_link_arg arg = {
3533 .fh = NFS_FH(inode),
3534 .dir_fh = NFS_FH(dir),
3535 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003536 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003538 struct nfs4_link_res res = {
3539 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003540 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003541 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 struct rpc_message msg = {
3543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3544 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003545 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003547 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Trond Myklebust136f2622010-04-16 16:22:49 -04003549 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003550 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003551 goto out;
3552
David Quigley14c43f72013-05-22 12:50:43 -04003553 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3554 if (IS_ERR(res.label)) {
3555 status = PTR_ERR(res.label);
3556 goto out;
3557 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003558 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003559
Bryan Schumaker7c513052011-03-24 17:12:24 +00003560 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003561 if (!status) {
3562 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003563 status = nfs_post_op_update_inode(inode, res.fattr);
3564 if (!status)
3565 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003566 }
David Quigley14c43f72013-05-22 12:50:43 -04003567
3568
3569 nfs4_label_free(res.label);
3570
Trond Myklebust136f2622010-04-16 16:22:49 -04003571out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003572 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 return status;
3574}
3575
3576static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3577{
3578 struct nfs4_exception exception = { };
3579 int err;
3580 do {
3581 err = nfs4_handle_exception(NFS_SERVER(inode),
3582 _nfs4_proc_link(inode, dir, name),
3583 &exception);
3584 } while (exception.retry);
3585 return err;
3586}
3587
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003588struct nfs4_createdata {
3589 struct rpc_message msg;
3590 struct nfs4_create_arg arg;
3591 struct nfs4_create_res res;
3592 struct nfs_fh fh;
3593 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003594 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003595};
3596
3597static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3598 struct qstr *name, struct iattr *sattr, u32 ftype)
3599{
3600 struct nfs4_createdata *data;
3601
3602 data = kzalloc(sizeof(*data), GFP_KERNEL);
3603 if (data != NULL) {
3604 struct nfs_server *server = NFS_SERVER(dir);
3605
David Quigley14c43f72013-05-22 12:50:43 -04003606 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3607 if (IS_ERR(data->label))
3608 goto out_free;
3609
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003610 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3611 data->msg.rpc_argp = &data->arg;
3612 data->msg.rpc_resp = &data->res;
3613 data->arg.dir_fh = NFS_FH(dir);
3614 data->arg.server = server;
3615 data->arg.name = name;
3616 data->arg.attrs = sattr;
3617 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003618 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003619 data->res.server = server;
3620 data->res.fh = &data->fh;
3621 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003622 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003623 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003624 }
3625 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003626out_free:
3627 kfree(data);
3628 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003629}
3630
3631static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3632{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003633 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003634 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003635 if (status == 0) {
3636 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003637 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003638 }
3639 return status;
3640}
3641
3642static void nfs4_free_createdata(struct nfs4_createdata *data)
3643{
David Quigley14c43f72013-05-22 12:50:43 -04003644 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003645 kfree(data);
3646}
3647
Chuck Lever4f390c12006-08-22 20:06:22 -04003648static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003649 struct page *page, unsigned int len, struct iattr *sattr,
3650 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003652 struct nfs4_createdata *data;
3653 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Chuck Lever94a6d752006-08-22 20:06:23 -04003655 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003656 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003657
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003658 status = -ENOMEM;
3659 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3660 if (data == NULL)
3661 goto out;
3662
3663 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3664 data->arg.u.symlink.pages = &page;
3665 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003666 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003668 status = nfs4_do_create(dir, dentry, data);
3669
3670 nfs4_free_createdata(data);
3671out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 return status;
3673}
3674
Chuck Lever4f390c12006-08-22 20:06:22 -04003675static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003676 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
3678 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003679 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003681
3682 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003685 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3686 trace_nfs4_symlink(dir, &dentry->d_name, err);
3687 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 &exception);
3689 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003690
3691 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return err;
3693}
3694
3695static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003696 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003698 struct nfs4_createdata *data;
3699 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003701 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3702 if (data == NULL)
3703 goto out;
3704
David Quigley1775fd32013-05-22 12:50:42 -04003705 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003706 status = nfs4_do_create(dir, dentry, data);
3707
3708 nfs4_free_createdata(data);
3709out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return status;
3711}
3712
3713static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3714 struct iattr *sattr)
3715{
3716 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003717 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003719
David Quigleyaa9c2662013-05-22 12:50:44 -04003720 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3721
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003722 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003724 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3725 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3726 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 &exception);
3728 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003729 nfs4_label_release_security(label);
3730
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 return err;
3732}
3733
3734static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003735 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
3737 struct inode *dir = dentry->d_inode;
3738 struct nfs4_readdir_arg args = {
3739 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003740 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 .pgbase = 0,
3742 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003743 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003744 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 };
3746 struct nfs4_readdir_res res;
3747 struct rpc_message msg = {
3748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3749 .rpc_argp = &args,
3750 .rpc_resp = &res,
3751 .rpc_cred = cred,
3752 };
3753 int status;
3754
Harvey Harrison3110ff82008-05-02 13:42:44 -07003755 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003756 dentry->d_parent->d_name.name,
3757 dentry->d_name.name,
3758 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003759 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003761 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 -05003762 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003763 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003764 status += args.pgbase;
3765 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003766
3767 nfs_invalidate_atime(dir);
3768
Harvey Harrison3110ff82008-05-02 13:42:44 -07003769 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 return status;
3771}
3772
3773static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003774 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
3776 struct nfs4_exception exception = { };
3777 int err;
3778 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003779 err = _nfs4_proc_readdir(dentry, cred, cookie,
3780 pages, count, plus);
3781 trace_nfs4_readdir(dentry->d_inode, err);
3782 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 &exception);
3784 } while (exception.retry);
3785 return err;
3786}
3787
3788static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003789 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003791 struct nfs4_createdata *data;
3792 int mode = sattr->ia_mode;
3793 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003795 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3796 if (data == NULL)
3797 goto out;
3798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003800 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003802 data->arg.ftype = NF4BLK;
3803 data->arg.u.device.specdata1 = MAJOR(rdev);
3804 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
3806 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003807 data->arg.ftype = NF4CHR;
3808 data->arg.u.device.specdata1 = MAJOR(rdev);
3809 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003810 } else if (!S_ISSOCK(mode)) {
3811 status = -EINVAL;
3812 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 }
David Quigley1775fd32013-05-22 12:50:42 -04003814
David Quigleyaa9c2662013-05-22 12:50:44 -04003815 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003816 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003817out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003818 nfs4_free_createdata(data);
3819out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return status;
3821}
3822
3823static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3824 struct iattr *sattr, dev_t rdev)
3825{
3826 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003827 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003829
David Quigleyaa9c2662013-05-22 12:50:44 -04003830 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3831
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003832 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003834 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3835 trace_nfs4_mknod(dir, &dentry->d_name, err);
3836 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 &exception);
3838 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003839
3840 nfs4_label_release_security(label);
3841
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 return err;
3843}
3844
3845static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3846 struct nfs_fsstat *fsstat)
3847{
3848 struct nfs4_statfs_arg args = {
3849 .fh = fhandle,
3850 .bitmask = server->attr_bitmask,
3851 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003852 struct nfs4_statfs_res res = {
3853 .fsstat = fsstat,
3854 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 struct rpc_message msg = {
3856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3857 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003858 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 };
3860
Trond Myklebust0e574af2005-10-27 22:12:38 -04003861 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003862 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863}
3864
3865static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3866{
3867 struct nfs4_exception exception = { };
3868 int err;
3869 do {
3870 err = nfs4_handle_exception(server,
3871 _nfs4_proc_statfs(server, fhandle, fsstat),
3872 &exception);
3873 } while (exception.retry);
3874 return err;
3875}
3876
3877static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3878 struct nfs_fsinfo *fsinfo)
3879{
3880 struct nfs4_fsinfo_arg args = {
3881 .fh = fhandle,
3882 .bitmask = server->attr_bitmask,
3883 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003884 struct nfs4_fsinfo_res res = {
3885 .fsinfo = fsinfo,
3886 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 struct rpc_message msg = {
3888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3889 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003890 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 };
3892
Bryan Schumaker7c513052011-03-24 17:12:24 +00003893 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894}
3895
3896static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3897{
3898 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003899 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 int err;
3901
3902 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003903 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003904 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003905 if (err == 0) {
3906 struct nfs_client *clp = server->nfs_client;
3907
3908 spin_lock(&clp->cl_lock);
3909 clp->cl_lease_time = fsinfo->lease_time * HZ;
3910 clp->cl_last_renewal = now;
3911 spin_unlock(&clp->cl_lock);
3912 break;
3913 }
3914 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 } while (exception.retry);
3916 return err;
3917}
3918
3919static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3920{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003921 int error;
3922
Trond Myklebust0e574af2005-10-27 22:12:38 -04003923 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003924 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003925 if (error == 0) {
3926 /* block layout checks this! */
3927 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003928 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003929 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003930
3931 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932}
3933
3934static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3935 struct nfs_pathconf *pathconf)
3936{
3937 struct nfs4_pathconf_arg args = {
3938 .fh = fhandle,
3939 .bitmask = server->attr_bitmask,
3940 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003941 struct nfs4_pathconf_res res = {
3942 .pathconf = pathconf,
3943 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 struct rpc_message msg = {
3945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3946 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003947 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 };
3949
3950 /* None of the pathconf attributes are mandatory to implement */
3951 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3952 memset(pathconf, 0, sizeof(*pathconf));
3953 return 0;
3954 }
3955
Trond Myklebust0e574af2005-10-27 22:12:38 -04003956 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003957 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
3960static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3961 struct nfs_pathconf *pathconf)
3962{
3963 struct nfs4_exception exception = { };
3964 int err;
3965
3966 do {
3967 err = nfs4_handle_exception(server,
3968 _nfs4_proc_pathconf(server, fhandle, pathconf),
3969 &exception);
3970 } while (exception.retry);
3971 return err;
3972}
3973
Trond Myklebust5521abf2013-03-16 20:54:34 -04003974int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003975 const struct nfs_open_context *ctx,
3976 const struct nfs_lock_context *l_ctx,
3977 fmode_t fmode)
3978{
3979 const struct nfs_lockowner *lockowner = NULL;
3980
3981 if (l_ctx != NULL)
3982 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003983 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003984}
3985EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3986
Trond Myklebust5521abf2013-03-16 20:54:34 -04003987static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3988 const struct nfs_open_context *ctx,
3989 const struct nfs_lock_context *l_ctx,
3990 fmode_t fmode)
3991{
3992 nfs4_stateid current_stateid;
3993
3994 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3995 return false;
3996 return nfs4_stateid_match(stateid, &current_stateid);
3997}
3998
3999static bool nfs4_error_stateid_expired(int err)
4000{
4001 switch (err) {
4002 case -NFS4ERR_DELEG_REVOKED:
4003 case -NFS4ERR_ADMIN_REVOKED:
4004 case -NFS4ERR_BAD_STATEID:
4005 case -NFS4ERR_STALE_STATEID:
4006 case -NFS4ERR_OLD_STATEID:
4007 case -NFS4ERR_OPENMODE:
4008 case -NFS4ERR_EXPIRED:
4009 return true;
4010 }
4011 return false;
4012}
4013
Benny Halevyd20581a2011-05-22 19:52:03 +03004014void __nfs4_read_done_cb(struct nfs_read_data *data)
4015{
Fred Isamancd841602012-04-20 14:47:44 -04004016 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004017}
4018
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004019static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
Fred Isamancd841602012-04-20 14:47:44 -04004021 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004023 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004024 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004025 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004026 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004028
Benny Halevyd20581a2011-05-22 19:52:03 +03004029 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004031 renew_lease(server, data->timestamp);
4032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
Trond Myklebust5521abf2013-03-16 20:54:34 -04004035static bool nfs4_read_stateid_changed(struct rpc_task *task,
4036 struct nfs_readargs *args)
4037{
4038
4039 if (!nfs4_error_stateid_expired(task->tk_status) ||
4040 nfs4_stateid_is_current(&args->stateid,
4041 args->context,
4042 args->lock_context,
4043 FMODE_READ))
4044 return false;
4045 rpc_restart_call_prepare(task);
4046 return true;
4047}
4048
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004049static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4050{
4051
4052 dprintk("--> %s\n", __func__);
4053
4054 if (!nfs4_sequence_done(task, &data->res.seq_res))
4055 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004056 if (nfs4_read_stateid_changed(task, &data->args))
4057 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004058 return data->read_done_cb ? data->read_done_cb(task, data) :
4059 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004060}
4061
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004062static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004065 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004066 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004067 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068}
4069
NeilBrownef1820f2013-09-04 17:04:49 +10004070static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004071{
Trond Myklebust9b206142013-03-17 15:52:00 -04004072 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004073 &data->args.seq_args,
4074 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004075 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004076 return 0;
4077 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4078 data->args.lock_context, FMODE_READ) == -EIO)
4079 return -EIO;
4080 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4081 return -EIO;
4082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083}
4084
Fred Isamanb029bc92011-03-03 15:13:42 +00004085static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086{
Fred Isamancd841602012-04-20 14:47:44 -04004087 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004089 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004090 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004091 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004092 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004094 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004096 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004097 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099}
4100
Trond Myklebust5521abf2013-03-16 20:54:34 -04004101static bool nfs4_write_stateid_changed(struct rpc_task *task,
4102 struct nfs_writeargs *args)
4103{
4104
4105 if (!nfs4_error_stateid_expired(task->tk_status) ||
4106 nfs4_stateid_is_current(&args->stateid,
4107 args->context,
4108 args->lock_context,
4109 FMODE_WRITE))
4110 return false;
4111 rpc_restart_call_prepare(task);
4112 return true;
4113}
4114
Fred Isamanb029bc92011-03-03 15:13:42 +00004115static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4116{
4117 if (!nfs4_sequence_done(task, &data->res.seq_res))
4118 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004119 if (nfs4_write_stateid_changed(task, &data->args))
4120 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004121 return data->write_done_cb ? data->write_done_cb(task, data) :
4122 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004123}
4124
Trond Myklebust5a37f852012-04-28 14:55:16 -04004125static
4126bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004127{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004128 const struct nfs_pgio_header *hdr = data->header;
4129
4130 /* Don't request attributes for pNFS or O_DIRECT writes */
4131 if (data->ds_clp != NULL || hdr->dreq != NULL)
4132 return false;
4133 /* Otherwise, request attributes if and only if we don't hold
4134 * a delegation
4135 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004136 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004137}
Fred Isamana69aef12011-03-03 15:13:47 +00004138
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004139static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140{
Fred Isamancd841602012-04-20 14:47:44 -04004141 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004142
Trond Myklebust5a37f852012-04-28 14:55:16 -04004143 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004144 data->args.bitmask = NULL;
4145 data->res.fattr = NULL;
4146 } else
4147 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004148
Fred Isamanb029bc92011-03-03 15:13:42 +00004149 if (!data->write_done_cb)
4150 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004151 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 data->timestamp = jiffies;
4153
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004154 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004155 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156}
4157
NeilBrownef1820f2013-09-04 17:04:49 +10004158static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004159{
Trond Myklebust9b206142013-03-17 15:52:00 -04004160 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004161 &data->args.seq_args,
4162 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004163 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004164 return 0;
4165 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4166 data->args.lock_context, FMODE_WRITE) == -EIO)
4167 return -EIO;
4168 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4169 return -EIO;
4170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171}
4172
Fred Isaman0b7c0152012-04-20 14:47:39 -04004173static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4174{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004175 nfs4_setup_sequence(NFS_SERVER(data->inode),
4176 &data->args.seq_args,
4177 &data->res.seq_res,
4178 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179}
4180
Fred Isaman0b7c0152012-04-20 14:47:39 -04004181static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004184
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004185 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004186 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004187 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004188 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
4192
Fred Isaman0b7c0152012-04-20 14:47:39 -04004193static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004194{
4195 if (!nfs4_sequence_done(task, &data->res.seq_res))
4196 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004197 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004198}
4199
Fred Isaman0b7c0152012-04-20 14:47:39 -04004200static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004202 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004203
Fred Isaman0b7c0152012-04-20 14:47:39 -04004204 if (data->commit_done_cb == NULL)
4205 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004206 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004207 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004208 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209}
4210
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004211struct nfs4_renewdata {
4212 struct nfs_client *client;
4213 unsigned long timestamp;
4214};
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216/*
4217 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4218 * standalone procedure for queueing an asynchronous RENEW.
4219 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004220static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004221{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004222 struct nfs4_renewdata *data = calldata;
4223 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004224
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004225 if (atomic_read(&clp->cl_count) > 1)
4226 nfs4_schedule_state_renewal(clp);
4227 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004228 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004229}
4230
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004231static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004233 struct nfs4_renewdata *data = calldata;
4234 struct nfs_client *clp = data->client;
4235 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004237 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004239 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004240 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4241 return;
4242 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004243 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004244 return;
4245 }
4246 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004248 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004251static const struct rpc_call_ops nfs4_renew_ops = {
4252 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004253 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004254};
4255
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004256static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257{
4258 struct rpc_message msg = {
4259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4260 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004261 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004263 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004265 if (renew_flags == 0)
4266 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004267 if (!atomic_inc_not_zero(&clp->cl_count))
4268 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004269 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004270 if (data == NULL)
4271 return -ENOMEM;
4272 data->client = clp;
4273 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004274 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004275 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276}
4277
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004278static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
4280 struct rpc_message msg = {
4281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4282 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004283 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 };
4285 unsigned long now = jiffies;
4286 int status;
4287
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004288 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 if (status < 0)
4290 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004291 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 return 0;
4293}
4294
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004295static inline int nfs4_server_supports_acls(struct nfs_server *server)
4296{
4297 return (server->caps & NFS_CAP_ACLS)
4298 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4299 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4300}
4301
Trond Myklebust21f498c2012-08-24 10:59:25 -04004302/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4303 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004304 * the stack.
4305 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004306#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004307
Neil Hormane9e3d722011-03-04 19:26:03 -05004308static int buf_to_pages_noslab(const void *buf, size_t buflen,
4309 struct page **pages, unsigned int *pgbase)
4310{
4311 struct page *newpage, **spages;
4312 int rc = 0;
4313 size_t len;
4314 spages = pages;
4315
4316 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004317 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004318 newpage = alloc_page(GFP_KERNEL);
4319
4320 if (newpage == NULL)
4321 goto unwind;
4322 memcpy(page_address(newpage), buf, len);
4323 buf += len;
4324 buflen -= len;
4325 *pages++ = newpage;
4326 rc++;
4327 } while (buflen != 0);
4328
4329 return rc;
4330
4331unwind:
4332 for(; rc > 0; rc--)
4333 __free_page(spages[rc-1]);
4334 return -ENOMEM;
4335}
4336
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004337struct nfs4_cached_acl {
4338 int cached;
4339 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004340 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004341};
4342
4343static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004344{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004345 struct nfs_inode *nfsi = NFS_I(inode);
4346
4347 spin_lock(&inode->i_lock);
4348 kfree(nfsi->nfs4_acl);
4349 nfsi->nfs4_acl = acl;
4350 spin_unlock(&inode->i_lock);
4351}
4352
4353static void nfs4_zap_acl_attr(struct inode *inode)
4354{
4355 nfs4_set_cached_acl(inode, NULL);
4356}
4357
4358static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4359{
4360 struct nfs_inode *nfsi = NFS_I(inode);
4361 struct nfs4_cached_acl *acl;
4362 int ret = -ENOENT;
4363
4364 spin_lock(&inode->i_lock);
4365 acl = nfsi->nfs4_acl;
4366 if (acl == NULL)
4367 goto out;
4368 if (buf == NULL) /* user is just asking for length */
4369 goto out_len;
4370 if (acl->cached == 0)
4371 goto out;
4372 ret = -ERANGE; /* see getxattr(2) man page */
4373 if (acl->len > buflen)
4374 goto out;
4375 memcpy(buf, acl->data, acl->len);
4376out_len:
4377 ret = acl->len;
4378out:
4379 spin_unlock(&inode->i_lock);
4380 return ret;
4381}
4382
Sachin Prabhu5794d212012-04-17 14:36:40 +01004383static 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 +00004384{
4385 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004386 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004387
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004388 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004389 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004390 if (acl == NULL)
4391 goto out;
4392 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004393 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004394 } else {
4395 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4396 if (acl == NULL)
4397 goto out;
4398 acl->cached = 0;
4399 }
4400 acl->len = acl_len;
4401out:
4402 nfs4_set_cached_acl(inode, acl);
4403}
4404
Andy Adamsonbf118a32011-12-07 11:55:27 -05004405/*
4406 * The getxattr API returns the required buffer length when called with a
4407 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4408 * the required buf. On a NULL buf, we send a page of data to the server
4409 * guessing that the ACL request can be serviced by a page. If so, we cache
4410 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4411 * the cache. If not so, we throw away the page, and cache the required
4412 * length. The next getxattr call will then produce another round trip to
4413 * the server, this time with the input buf of the required size.
4414 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004415static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004416{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004417 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004418 struct nfs_getaclargs args = {
4419 .fh = NFS_FH(inode),
4420 .acl_pages = pages,
4421 .acl_len = buflen,
4422 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004423 struct nfs_getaclres res = {
4424 .acl_len = buflen,
4425 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004426 struct rpc_message msg = {
4427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4428 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004429 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004430 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004431 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4432 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004433
Andy Adamsonbf118a32011-12-07 11:55:27 -05004434 /* As long as we're doing a round trip to the server anyway,
4435 * let's be prepared for a page of acl data. */
4436 if (npages == 0)
4437 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004438 if (npages > ARRAY_SIZE(pages))
4439 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004440
Andy Adamsonbf118a32011-12-07 11:55:27 -05004441 for (i = 0; i < npages; i++) {
4442 pages[i] = alloc_page(GFP_KERNEL);
4443 if (!pages[i])
4444 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004445 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004446
4447 /* for decoding across pages */
4448 res.acl_scratch = alloc_page(GFP_KERNEL);
4449 if (!res.acl_scratch)
4450 goto out_free;
4451
Andy Adamsonbf118a32011-12-07 11:55:27 -05004452 args.acl_len = npages * PAGE_SIZE;
4453 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004454
Peng Taode040be2012-01-10 22:42:47 +08004455 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004456 __func__, buf, buflen, npages, args.acl_len);
4457 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4458 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004459 if (ret)
4460 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004461
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004462 /* Handle the case where the passed-in buffer is too short */
4463 if (res.acl_flags & NFS4_ACL_TRUNC) {
4464 /* Did the user only issue a request for the acl length? */
4465 if (buf == NULL)
4466 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004467 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004468 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004469 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004470 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004471 if (buf) {
4472 if (res.acl_len > buflen) {
4473 ret = -ERANGE;
4474 goto out_free;
4475 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004476 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004477 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004478out_ok:
4479 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004480out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004481 for (i = 0; i < npages; i++)
4482 if (pages[i])
4483 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004484 if (res.acl_scratch)
4485 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004486 return ret;
4487}
4488
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004489static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4490{
4491 struct nfs4_exception exception = { };
4492 ssize_t ret;
4493 do {
4494 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004495 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004496 if (ret >= 0)
4497 break;
4498 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4499 } while (exception.retry);
4500 return ret;
4501}
4502
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004503static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4504{
4505 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004506 int ret;
4507
4508 if (!nfs4_server_supports_acls(server))
4509 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004510 ret = nfs_revalidate_inode(server, inode);
4511 if (ret < 0)
4512 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004513 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4514 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004515 ret = nfs4_read_cached_acl(inode, buf, buflen);
4516 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004517 /* -ENOENT is returned if there is no ACL or if there is an ACL
4518 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004519 return ret;
4520 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004521}
4522
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004523static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004524{
4525 struct nfs_server *server = NFS_SERVER(inode);
4526 struct page *pages[NFS4ACL_MAXPAGES];
4527 struct nfs_setaclargs arg = {
4528 .fh = NFS_FH(inode),
4529 .acl_pages = pages,
4530 .acl_len = buflen,
4531 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004532 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004533 struct rpc_message msg = {
4534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4535 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004536 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004537 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004538 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004539 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004540
4541 if (!nfs4_server_supports_acls(server))
4542 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004543 if (npages > ARRAY_SIZE(pages))
4544 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004545 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4546 if (i < 0)
4547 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004548 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004549 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004550
4551 /*
4552 * Free each page after tx, so the only ref left is
4553 * held by the network stack
4554 */
4555 for (; i > 0; i--)
4556 put_page(pages[i-1]);
4557
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004558 /*
4559 * Acl update can result in inode attribute update.
4560 * so mark the attribute cache invalid.
4561 */
4562 spin_lock(&inode->i_lock);
4563 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4564 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004565 nfs_access_zap_cache(inode);
4566 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004567 return ret;
4568}
4569
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004570static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4571{
4572 struct nfs4_exception exception = { };
4573 int err;
4574 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004575 err = __nfs4_proc_set_acl(inode, buf, buflen);
4576 trace_nfs4_set_acl(inode, err);
4577 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004578 &exception);
4579 } while (exception.retry);
4580 return err;
4581}
4582
David Quigleyaa9c2662013-05-22 12:50:44 -04004583#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4584static int _nfs4_get_security_label(struct inode *inode, void *buf,
4585 size_t buflen)
4586{
4587 struct nfs_server *server = NFS_SERVER(inode);
4588 struct nfs_fattr fattr;
4589 struct nfs4_label label = {0, 0, buflen, buf};
4590
4591 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4592 struct nfs4_getattr_arg args = {
4593 .fh = NFS_FH(inode),
4594 .bitmask = bitmask,
4595 };
4596 struct nfs4_getattr_res res = {
4597 .fattr = &fattr,
4598 .label = &label,
4599 .server = server,
4600 };
4601 struct rpc_message msg = {
4602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4603 .rpc_argp = &args,
4604 .rpc_resp = &res,
4605 };
4606 int ret;
4607
4608 nfs_fattr_init(&fattr);
4609
4610 ret = rpc_call_sync(server->client, &msg, 0);
4611 if (ret)
4612 return ret;
4613 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4614 return -ENOENT;
4615 if (buflen < label.len)
4616 return -ERANGE;
4617 return 0;
4618}
4619
4620static int nfs4_get_security_label(struct inode *inode, void *buf,
4621 size_t buflen)
4622{
4623 struct nfs4_exception exception = { };
4624 int err;
4625
4626 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4627 return -EOPNOTSUPP;
4628
4629 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004630 err = _nfs4_get_security_label(inode, buf, buflen);
4631 trace_nfs4_get_security_label(inode, err);
4632 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004633 &exception);
4634 } while (exception.retry);
4635 return err;
4636}
4637
4638static int _nfs4_do_set_security_label(struct inode *inode,
4639 struct nfs4_label *ilabel,
4640 struct nfs_fattr *fattr,
4641 struct nfs4_label *olabel)
4642{
4643
4644 struct iattr sattr = {0};
4645 struct nfs_server *server = NFS_SERVER(inode);
4646 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4647 struct nfs_setattrargs args = {
4648 .fh = NFS_FH(inode),
4649 .iap = &sattr,
4650 .server = server,
4651 .bitmask = bitmask,
4652 .label = ilabel,
4653 };
4654 struct nfs_setattrres res = {
4655 .fattr = fattr,
4656 .label = olabel,
4657 .server = server,
4658 };
4659 struct rpc_message msg = {
4660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4661 .rpc_argp = &args,
4662 .rpc_resp = &res,
4663 };
4664 int status;
4665
4666 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4667
4668 status = rpc_call_sync(server->client, &msg, 0);
4669 if (status)
4670 dprintk("%s failed: %d\n", __func__, status);
4671
4672 return status;
4673}
4674
4675static int nfs4_do_set_security_label(struct inode *inode,
4676 struct nfs4_label *ilabel,
4677 struct nfs_fattr *fattr,
4678 struct nfs4_label *olabel)
4679{
4680 struct nfs4_exception exception = { };
4681 int err;
4682
4683 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004684 err = _nfs4_do_set_security_label(inode, ilabel,
4685 fattr, olabel);
4686 trace_nfs4_set_security_label(inode, err);
4687 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004688 &exception);
4689 } while (exception.retry);
4690 return err;
4691}
4692
4693static int
4694nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4695{
4696 struct nfs4_label ilabel, *olabel = NULL;
4697 struct nfs_fattr fattr;
4698 struct rpc_cred *cred;
4699 struct inode *inode = dentry->d_inode;
4700 int status;
4701
4702 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4703 return -EOPNOTSUPP;
4704
4705 nfs_fattr_init(&fattr);
4706
4707 ilabel.pi = 0;
4708 ilabel.lfs = 0;
4709 ilabel.label = (char *)buf;
4710 ilabel.len = buflen;
4711
4712 cred = rpc_lookup_cred();
4713 if (IS_ERR(cred))
4714 return PTR_ERR(cred);
4715
4716 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4717 if (IS_ERR(olabel)) {
4718 status = -PTR_ERR(olabel);
4719 goto out;
4720 }
4721
4722 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4723 if (status == 0)
4724 nfs_setsecurity(inode, &fattr, olabel);
4725
4726 nfs4_label_free(olabel);
4727out:
4728 put_rpccred(cred);
4729 return status;
4730}
4731#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4732
4733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004735nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004737 struct nfs_client *clp = server->nfs_client;
4738
4739 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 return 0;
4741 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004742 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004743 case -NFS4ERR_ADMIN_REVOKED:
4744 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004745 if (state == NULL)
4746 break;
4747 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004748 case -NFS4ERR_OPENMODE:
4749 if (state == NULL)
4750 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004751 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004752 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004753 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004754 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004755 if (state != NULL) {
4756 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004757 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004760 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004761 nfs4_schedule_lease_recovery(clp);
4762 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004763 case -NFS4ERR_MOVED:
4764 if (nfs4_schedule_migration_recovery(server) < 0)
4765 goto recovery_failed;
4766 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004767 case -NFS4ERR_LEASE_MOVED:
4768 nfs4_schedule_lease_moved_recovery(clp);
4769 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004770#if defined(CONFIG_NFS_V4_1)
4771 case -NFS4ERR_BADSESSION:
4772 case -NFS4ERR_BADSLOT:
4773 case -NFS4ERR_BAD_HIGH_SLOT:
4774 case -NFS4ERR_DEADSESSION:
4775 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4776 case -NFS4ERR_SEQ_FALSE_RETRY:
4777 case -NFS4ERR_SEQ_MISORDERED:
4778 dprintk("%s ERROR %d, Reset session\n", __func__,
4779 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004780 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Chuck Leverf1478c12013-10-17 14:13:07 -04004781 goto restart_call;
Andy Adamson4745e312009-04-01 09:22:42 -04004782#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004784 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004785 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004787 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004789 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 }
4791 task->tk_status = nfs4_map_errors(task->tk_status);
4792 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004793recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004794 task->tk_status = -EIO;
4795 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004796wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004797 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004798 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4799 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004800 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4801 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004802restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004803 task->tk_status = 0;
4804 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
Chuck Leverf0920752012-05-21 22:45:41 -04004807static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4808 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004809{
4810 __be32 verf[2];
4811
Chuck Lever2c820d92012-05-21 22:45:33 -04004812 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4813 /* An impossible timestamp guarantees this value
4814 * will never match a generated boot time. */
4815 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004816 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004817 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004818 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004819 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4820 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004821 }
Chuck Levercd937102012-03-02 17:14:31 -05004822 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4823}
4824
Chuck Levere984a552012-09-14 17:24:21 -04004825static unsigned int
4826nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4827 char *buf, size_t len)
4828{
4829 unsigned int result;
4830
4831 rcu_read_lock();
4832 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4833 clp->cl_ipaddr,
4834 rpc_peeraddr2str(clp->cl_rpcclient,
4835 RPC_DISPLAY_ADDR),
4836 rpc_peeraddr2str(clp->cl_rpcclient,
4837 RPC_DISPLAY_PROTO));
4838 rcu_read_unlock();
4839 return result;
4840}
4841
4842static unsigned int
4843nfs4_init_uniform_client_string(const struct nfs_client *clp,
4844 char *buf, size_t len)
4845{
Trond Myklebust55b59292013-07-24 16:36:35 -04004846 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004847
4848 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004849 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4850 clp->rpc_ops->version,
4851 clp->cl_minorversion,
4852 nfs4_client_id_uniquifier,
4853 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004854 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4855 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004856 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004857}
4858
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004859/**
4860 * nfs4_proc_setclientid - Negotiate client ID
4861 * @clp: state data structure
4862 * @program: RPC program for NFSv4 callback service
4863 * @port: IP port number for NFS4 callback service
4864 * @cred: RPC credential to use for this call
4865 * @res: where to place the result
4866 *
4867 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4868 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004869int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4870 unsigned short port, struct rpc_cred *cred,
4871 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
4873 nfs4_verifier sc_verifier;
4874 struct nfs4_setclientid setclientid = {
4875 .sc_verifier = &sc_verifier,
4876 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004877 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 };
4879 struct rpc_message msg = {
4880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4881 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004882 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004883 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004885 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Chuck Leverde734832012-07-11 16:30:50 -04004887 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004888 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004889 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4890 setclientid.sc_name_len =
4891 nfs4_init_uniform_client_string(clp,
4892 setclientid.sc_name,
4893 sizeof(setclientid.sc_name));
4894 else
4895 setclientid.sc_name_len =
4896 nfs4_init_nonuniform_client_string(clp,
4897 setclientid.sc_name,
4898 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004899 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004900 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004901 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004902 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004903 rpc_peeraddr2str(clp->cl_rpcclient,
4904 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004905 rcu_read_unlock();
4906 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004907 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 clp->cl_ipaddr, port >> 8, port & 255);
4909
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004910 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4911 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4912 setclientid.sc_name_len, setclientid.sc_name);
4913 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004914 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004915 dprintk("NFS reply setclientid: %d\n", status);
4916 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917}
4918
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004919/**
4920 * nfs4_proc_setclientid_confirm - Confirm client ID
4921 * @clp: state data structure
4922 * @res: result of a previous SETCLIENTID
4923 * @cred: RPC credential to use for this call
4924 *
4925 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4926 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004927int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004928 struct nfs4_setclientid_res *arg,
4929 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 struct rpc_message msg = {
4932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004933 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004934 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 int status;
4937
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004938 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4939 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4940 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004941 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004942 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004943 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 return status;
4945}
4946
Trond Myklebustfe650402006-01-03 09:55:18 +01004947struct nfs4_delegreturndata {
4948 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004949 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004950 struct nfs_fh fh;
4951 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004952 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004953 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004954 int rpc_status;
4955};
4956
Trond Myklebustfe650402006-01-03 09:55:18 +01004957static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4958{
4959 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004960
Trond Myklebust14516c32010-07-31 14:29:06 -04004961 if (!nfs4_sequence_done(task, &data->res.seq_res))
4962 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004963
Trond Myklebustca8acf82013-08-13 10:36:56 -04004964 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004965 switch (task->tk_status) {
4966 case -NFS4ERR_STALE_STATEID:
4967 case -NFS4ERR_EXPIRED:
4968 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004969 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004970 break;
4971 default:
4972 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4973 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004974 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004975 return;
4976 }
4977 }
4978 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004979}
4980
4981static void nfs4_delegreturn_release(void *calldata)
4982{
Trond Myklebustfe650402006-01-03 09:55:18 +01004983 kfree(calldata);
4984}
4985
Andy Adamson938e1012009-04-01 09:22:28 -04004986static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4987{
4988 struct nfs4_delegreturndata *d_data;
4989
4990 d_data = (struct nfs4_delegreturndata *)data;
4991
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004992 nfs4_setup_sequence(d_data->res.server,
4993 &d_data->args.seq_args,
4994 &d_data->res.seq_res,
4995 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004996}
Andy Adamson938e1012009-04-01 09:22:28 -04004997
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004998static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004999 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005000 .rpc_call_done = nfs4_delegreturn_done,
5001 .rpc_release = nfs4_delegreturn_release,
5002};
5003
Trond Myklebuste6f81072008-01-24 18:14:34 -05005004static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005005{
5006 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005007 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005008 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005009 struct rpc_message msg = {
5010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5011 .rpc_cred = cred,
5012 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005013 struct rpc_task_setup task_setup_data = {
5014 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005015 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005016 .callback_ops = &nfs4_delegreturn_ops,
5017 .flags = RPC_TASK_ASYNC,
5018 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005019 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005020
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005021 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005022 if (data == NULL)
5023 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005024 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005025 data->args.fhandle = &data->fh;
5026 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005027 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005028 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005029 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005030 data->res.fattr = &data->fattr;
5031 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005032 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005033 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005034 data->rpc_status = 0;
5035
Trond Myklebustc970aa82007-07-14 15:39:59 -04005036 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005037 msg.rpc_argp = &data->args;
5038 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005039 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005040 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005041 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005042 if (!issync)
5043 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005044 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005045 if (status != 0)
5046 goto out;
5047 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005048 if (status == 0)
5049 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5050 else
5051 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005052out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005053 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005054 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055}
5056
Trond Myklebuste6f81072008-01-24 18:14:34 -05005057int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
5059 struct nfs_server *server = NFS_SERVER(inode);
5060 struct nfs4_exception exception = { };
5061 int err;
5062 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005063 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005064 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 switch (err) {
5066 case -NFS4ERR_STALE_STATEID:
5067 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 case 0:
5069 return 0;
5070 }
5071 err = nfs4_handle_exception(server, err, &exception);
5072 } while (exception.retry);
5073 return err;
5074}
5075
5076#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5077#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5078
5079/*
5080 * sleep, with exponential backoff, and retry the LOCK operation.
5081 */
5082static unsigned long
5083nfs4_set_lock_task_retry(unsigned long timeout)
5084{
Colin Cross416ad3c2013-05-06 23:50:06 +00005085 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 timeout <<= 1;
5087 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5088 return NFS4_LOCK_MAXTIMEOUT;
5089 return timeout;
5090}
5091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5093{
5094 struct inode *inode = state->inode;
5095 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005096 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005097 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005099 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005101 struct nfs_lockt_res res = {
5102 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 };
5104 struct rpc_message msg = {
5105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5106 .rpc_argp = &arg,
5107 .rpc_resp = &res,
5108 .rpc_cred = state->owner->so_cred,
5109 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 struct nfs4_lock_state *lsp;
5111 int status;
5112
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005113 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005114 status = nfs4_set_lock_state(state, request);
5115 if (status != 0)
5116 goto out;
5117 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005118 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005119 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005120 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005121 switch (status) {
5122 case 0:
5123 request->fl_type = F_UNLCK;
5124 break;
5125 case -NFS4ERR_DENIED:
5126 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005128 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005129 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005130out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 return status;
5132}
5133
5134static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5135{
5136 struct nfs4_exception exception = { };
5137 int err;
5138
5139 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005140 err = _nfs4_proc_getlk(state, cmd, request);
5141 trace_nfs4_get_lock(request, state, cmd, err);
5142 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 &exception);
5144 } while (exception.retry);
5145 return err;
5146}
5147
5148static int do_vfs_lock(struct file *file, struct file_lock *fl)
5149{
5150 int res = 0;
5151 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5152 case FL_POSIX:
5153 res = posix_lock_file_wait(file, fl);
5154 break;
5155 case FL_FLOCK:
5156 res = flock_lock_file_wait(file, fl);
5157 break;
5158 default:
5159 BUG();
5160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 return res;
5162}
5163
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005164struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005165 struct nfs_locku_args arg;
5166 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005167 struct nfs4_lock_state *lsp;
5168 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005169 struct file_lock fl;
5170 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005171 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005172};
5173
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005174static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5175 struct nfs_open_context *ctx,
5176 struct nfs4_lock_state *lsp,
5177 struct nfs_seqid *seqid)
5178{
5179 struct nfs4_unlockdata *p;
5180 struct inode *inode = lsp->ls_state->inode;
5181
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005182 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005183 if (p == NULL)
5184 return NULL;
5185 p->arg.fh = NFS_FH(inode);
5186 p->arg.fl = &p->fl;
5187 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005188 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005189 p->arg.stateid = &lsp->ls_stateid;
5190 p->lsp = lsp;
5191 atomic_inc(&lsp->ls_count);
5192 /* Ensure we don't close file until we're done freeing locks! */
5193 p->ctx = get_nfs_open_context(ctx);
5194 memcpy(&p->fl, fl, sizeof(p->fl));
5195 p->server = NFS_SERVER(inode);
5196 return p;
5197}
5198
Trond Myklebust06f814a2006-01-03 09:55:07 +01005199static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005200{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005201 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005202 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005203 nfs4_put_lock_state(calldata->lsp);
5204 put_nfs_open_context(calldata->ctx);
5205 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005206}
5207
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005208static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005209{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005210 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005211
Trond Myklebust14516c32010-07-31 14:29:06 -04005212 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5213 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005214 switch (task->tk_status) {
5215 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005216 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5217 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005218 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005219 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005220 case -NFS4ERR_BAD_STATEID:
5221 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005222 case -NFS4ERR_STALE_STATEID:
5223 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005224 break;
5225 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005226 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005227 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005228 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005229 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005230}
5231
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005232static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005233{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005234 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005236 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005237 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005238 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005239 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005240 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005241 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005242 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005243 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005244 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005245 &calldata->res.seq_res,
5246 task) != 0)
5247 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005248 return;
5249out_no_action:
5250 task->tk_action = NULL;
5251out_wait:
5252 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253}
5254
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005255static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005256 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005257 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005258 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005259};
5260
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005261static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5262 struct nfs_open_context *ctx,
5263 struct nfs4_lock_state *lsp,
5264 struct nfs_seqid *seqid)
5265{
5266 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005267 struct rpc_message msg = {
5268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5269 .rpc_cred = ctx->cred,
5270 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005271 struct rpc_task_setup task_setup_data = {
5272 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005273 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005274 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005275 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005276 .flags = RPC_TASK_ASYNC,
5277 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005278
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005279 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5280 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5281
Frank Filz137d6ac2007-07-09 15:32:29 -07005282 /* Ensure this is an unlock - when canceling a lock, the
5283 * canceled lock is passed in, and it won't be an unlock.
5284 */
5285 fl->fl_type = F_UNLCK;
5286
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005287 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5288 if (data == NULL) {
5289 nfs_free_seqid(seqid);
5290 return ERR_PTR(-ENOMEM);
5291 }
5292
Chuck Levera9c92d62013-08-09 12:48:18 -04005293 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005294 msg.rpc_argp = &data->arg;
5295 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005296 task_setup_data.callback_data = data;
5297 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005298}
5299
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5301{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005302 struct inode *inode = state->inode;
5303 struct nfs4_state_owner *sp = state->owner;
5304 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005305 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005306 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005307 struct rpc_task *task;
5308 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005309 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Trond Myklebust9b073572006-06-29 16:38:34 -04005311 status = nfs4_set_lock_state(state, request);
5312 /* Unlock _before_ we do the RPC call */
5313 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005314 /* Exclude nfs_delegation_claim_locks() */
5315 mutex_lock(&sp->so_delegreturn_mutex);
5316 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005317 down_read(&nfsi->rwsem);
5318 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5319 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005320 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005321 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005322 }
5323 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005324 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005325 if (status != 0)
5326 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005327 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005328 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005329 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5330 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005331 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005332 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005333 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005334 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005335 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005336 status = PTR_ERR(task);
5337 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005338 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005339 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005340 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005341out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005342 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005343 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005344 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345}
5346
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005347struct nfs4_lockdata {
5348 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005349 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005350 struct nfs4_lock_state *lsp;
5351 struct nfs_open_context *ctx;
5352 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005353 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005354 int rpc_status;
5355 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005356 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005357};
5358
5359static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005360 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5361 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005362{
5363 struct nfs4_lockdata *p;
5364 struct inode *inode = lsp->ls_state->inode;
5365 struct nfs_server *server = NFS_SERVER(inode);
5366
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005367 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005368 if (p == NULL)
5369 return NULL;
5370
5371 p->arg.fh = NFS_FH(inode);
5372 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005373 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005374 if (p->arg.open_seqid == NULL)
5375 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005376 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005377 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005378 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005379 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005380 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005381 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005382 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005383 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005384 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005385 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005386 atomic_inc(&lsp->ls_count);
5387 p->ctx = get_nfs_open_context(ctx);
5388 memcpy(&p->fl, fl, sizeof(p->fl));
5389 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005390out_free_seqid:
5391 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005392out_free:
5393 kfree(p);
5394 return NULL;
5395}
5396
5397static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5398{
5399 struct nfs4_lockdata *data = calldata;
5400 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Harvey Harrison3110ff82008-05-02 13:42:44 -07005402 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005403 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005404 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005405 /* Do we need to do an open_to_lock_owner? */
5406 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005407 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005408 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005409 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005410 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005411 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005412 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005413 } else
5414 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005415 if (!nfs4_valid_open_stateid(state)) {
5416 data->rpc_status = -EBADF;
5417 task->tk_action = NULL;
5418 goto out_release_open_seqid;
5419 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005420 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005421 if (nfs4_setup_sequence(data->server,
5422 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005423 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005424 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005425 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005426out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005427 nfs_release_seqid(data->arg.open_seqid);
5428out_release_lock_seqid:
5429 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005430out_wait:
5431 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005432 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005433}
5434
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005435static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5436{
5437 struct nfs4_lockdata *data = calldata;
5438
Harvey Harrison3110ff82008-05-02 13:42:44 -07005439 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005440
Trond Myklebust14516c32010-07-31 14:29:06 -04005441 if (!nfs4_sequence_done(task, &data->res.seq_res))
5442 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005443
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005444 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005445 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005446 if (data->rpc_status == 0)
5447 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5448 else
5449 goto out;
5450 }
5451 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005452 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005453 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005454 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005455 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005456out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005457 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005458}
5459
5460static void nfs4_lock_release(void *calldata)
5461{
5462 struct nfs4_lockdata *data = calldata;
5463
Harvey Harrison3110ff82008-05-02 13:42:44 -07005464 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005465 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005466 if (data->cancelled != 0) {
5467 struct rpc_task *task;
5468 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5469 data->arg.lock_seqid);
5470 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005471 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005472 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005473 } else
5474 nfs_free_seqid(data->arg.lock_seqid);
5475 nfs4_put_lock_state(data->lsp);
5476 put_nfs_open_context(data->ctx);
5477 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005478 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005479}
5480
5481static const struct rpc_call_ops nfs4_lock_ops = {
5482 .rpc_call_prepare = nfs4_lock_prepare,
5483 .rpc_call_done = nfs4_lock_done,
5484 .rpc_release = nfs4_lock_release,
5485};
5486
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005487static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5488{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005489 switch (error) {
5490 case -NFS4ERR_ADMIN_REVOKED:
5491 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005492 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005493 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005494 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005495 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005496 break;
5497 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005498 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005499 case -NFS4ERR_EXPIRED:
5500 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005501 };
5502}
5503
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005504static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005505{
5506 struct nfs4_lockdata *data;
5507 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005508 struct rpc_message msg = {
5509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5510 .rpc_cred = state->owner->so_cred,
5511 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005512 struct rpc_task_setup task_setup_data = {
5513 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005514 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005515 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005516 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005517 .flags = RPC_TASK_ASYNC,
5518 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005519 int ret;
5520
Harvey Harrison3110ff82008-05-02 13:42:44 -07005521 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005522 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005523 fl->fl_u.nfs4_fl.owner,
5524 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005525 if (data == NULL)
5526 return -ENOMEM;
5527 if (IS_SETLKW(cmd))
5528 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005529 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005530 msg.rpc_argp = &data->arg;
5531 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005532 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005533 if (recovery_type > NFS_LOCK_NEW) {
5534 if (recovery_type == NFS_LOCK_RECLAIM)
5535 data->arg.reclaim = NFS_LOCK_RECLAIM;
5536 nfs4_set_sequence_privileged(&data->arg.seq_args);
5537 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005538 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005539 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005540 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005541 ret = nfs4_wait_for_completion_rpc_task(task);
5542 if (ret == 0) {
5543 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005544 if (ret)
5545 nfs4_handle_setlk_error(data->server, data->lsp,
5546 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005547 } else
5548 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005549 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005550 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552}
5553
5554static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5555{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005556 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005557 struct nfs4_exception exception = {
5558 .inode = state->inode,
5559 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005560 int err;
5561
5562 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005563 /* Cache the lock if possible... */
5564 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5565 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005566 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005567 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005568 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005569 break;
5570 nfs4_handle_exception(server, err, &exception);
5571 } while (exception.retry);
5572 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573}
5574
5575static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5576{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005577 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005578 struct nfs4_exception exception = {
5579 .inode = state->inode,
5580 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005581 int err;
5582
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005583 err = nfs4_set_lock_state(state, request);
5584 if (err != 0)
5585 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005586 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005587 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5588 return 0;
5589 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005590 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005591 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5592 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005593 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005594 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005595 switch (err) {
5596 default:
5597 goto out;
5598 case -NFS4ERR_GRACE:
5599 case -NFS4ERR_DELAY:
5600 nfs4_handle_exception(server, err, &exception);
5601 err = 0;
5602 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005603 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005604out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606}
5607
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005608#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005609/**
5610 * nfs41_check_expired_locks - possibly free a lock stateid
5611 *
5612 * @state: NFSv4 state for an inode
5613 *
5614 * Returns NFS_OK if recovery for this stateid is now finished.
5615 * Otherwise a negative NFS4ERR value is returned.
5616 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005617static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005618{
Chuck Levereb64cf92012-07-11 16:30:05 -04005619 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005620 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005621 struct nfs_server *server = NFS_SERVER(state->inode);
5622
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005623 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005624 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005625 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5626
5627 status = nfs41_test_stateid(server,
5628 &lsp->ls_stateid,
5629 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005630 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005631 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005632 /* Free the stateid unless the server
5633 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005634 if (status != -NFS4ERR_BAD_STATEID)
5635 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005636 &lsp->ls_stateid,
5637 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005638 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005639 ret = status;
5640 }
5641 }
5642 };
5643
5644 return ret;
5645}
5646
5647static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5648{
5649 int status = NFS_OK;
5650
5651 if (test_bit(LK_STATE_IN_USE, &state->flags))
5652 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005653 if (status != NFS_OK)
5654 status = nfs4_lock_expired(state, request);
5655 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005656}
5657#endif
5658
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5660{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005661 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005662 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005663 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005664 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005665 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005667 if ((fl_flags & FL_POSIX) &&
5668 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5669 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005670 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005671 status = nfs4_set_lock_state(state, request);
5672 if (status != 0)
5673 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005674 request->fl_flags |= FL_ACCESS;
5675 status = do_vfs_lock(request->fl_file, request);
5676 if (status < 0)
5677 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005678 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005679 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005680 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005681 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005682 request->fl_flags = fl_flags & ~FL_SLEEP;
5683 status = do_vfs_lock(request->fl_file, request);
5684 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005685 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005686 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5687 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005688 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005689 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005690 goto out;
5691 down_read(&nfsi->rwsem);
5692 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5693 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005694 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005695 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005696 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005697 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005698 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005699 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5700 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005701out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005702 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005703out:
5704 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 return status;
5706}
5707
5708static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5709{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005710 struct nfs4_exception exception = {
5711 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005712 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005713 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 int err;
5715
5716 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005717 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005718 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005719 if (err == -NFS4ERR_DENIED)
5720 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005722 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 } while (exception.retry);
5724 return err;
5725}
5726
5727static int
5728nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5729{
5730 struct nfs_open_context *ctx;
5731 struct nfs4_state *state;
5732 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5733 int status;
5734
5735 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005736 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 state = ctx->state;
5738
5739 if (request->fl_start < 0 || request->fl_end < 0)
5740 return -EINVAL;
5741
Trond Myklebustd9531262009-07-21 19:22:38 -04005742 if (IS_GETLK(cmd)) {
5743 if (state != NULL)
5744 return nfs4_proc_getlk(state, F_GETLK, request);
5745 return 0;
5746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
5748 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5749 return -EINVAL;
5750
Trond Myklebustd9531262009-07-21 19:22:38 -04005751 if (request->fl_type == F_UNLCK) {
5752 if (state != NULL)
5753 return nfs4_proc_unlck(state, cmd, request);
5754 return 0;
5755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
Trond Myklebustd9531262009-07-21 19:22:38 -04005757 if (state == NULL)
5758 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005759 /*
5760 * Don't rely on the VFS having checked the file open mode,
5761 * since it won't do this for flock() locks.
5762 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005763 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005764 case F_RDLCK:
5765 if (!(filp->f_mode & FMODE_READ))
5766 return -EBADF;
5767 break;
5768 case F_WRLCK:
5769 if (!(filp->f_mode & FMODE_WRITE))
5770 return -EBADF;
5771 }
5772
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 do {
5774 status = nfs4_proc_setlk(state, cmd, request);
5775 if ((status != -EAGAIN) || IS_SETLK(cmd))
5776 break;
5777 timeout = nfs4_set_lock_task_retry(timeout);
5778 status = -ERESTARTSYS;
5779 if (signalled())
5780 break;
5781 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 return status;
5783}
5784
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005785int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005786{
5787 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005788 int err;
5789
5790 err = nfs4_set_lock_state(state, fl);
5791 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005792 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005793 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005794 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005795}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005796
Trond Myklebustcf470c32012-03-07 13:49:12 -05005797struct nfs_release_lockowner_data {
5798 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005799 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005800 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005801 struct nfs4_sequence_args seq_args;
5802 struct nfs4_sequence_res seq_res;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005803};
5804
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005805static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5806{
5807 struct nfs_release_lockowner_data *data = calldata;
5808 nfs40_setup_sequence(data->server,
5809 &data->seq_args, &data->seq_res, task);
5810}
5811
5812static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5813{
5814 struct nfs_release_lockowner_data *data = calldata;
5815 nfs40_sequence_done(task, &data->seq_res);
5816}
5817
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005818static void nfs4_release_lockowner_release(void *calldata)
5819{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005820 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005821 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005822 kfree(calldata);
5823}
5824
Trond Myklebust17280172012-03-11 13:11:00 -04005825static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005826 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5827 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005828 .rpc_release = nfs4_release_lockowner_release,
5829};
5830
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005831static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005832{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005833 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005834 struct rpc_message msg = {
5835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5836 };
5837
5838 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005839 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005840
Trond Myklebustcf470c32012-03-07 13:49:12 -05005841 data = kmalloc(sizeof(*data), GFP_NOFS);
5842 if (!data)
5843 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005844 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005845 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005846 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005847 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5848 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5849 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005850
Trond Myklebustcf470c32012-03-07 13:49:12 -05005851 msg.rpc_argp = &data->args;
5852 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5853 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005854}
5855
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005856#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5857
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005858static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5859 const void *buf, size_t buflen,
5860 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005861{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005862 if (strcmp(key, "") != 0)
5863 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005864
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005865 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005866}
5867
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005868static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5869 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005870{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005871 if (strcmp(key, "") != 0)
5872 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005873
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005874 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005875}
5876
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005877static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5878 size_t list_len, const char *name,
5879 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005880{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005881 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005882
J. Bruce Fields096455a2006-03-20 23:23:42 -05005883 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5884 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005885
5886 if (list && len <= list_len)
5887 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005888 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005889}
5890
David Quigleyc9bccef2013-05-22 12:50:45 -04005891#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5892static inline int nfs4_server_supports_labels(struct nfs_server *server)
5893{
5894 return server->caps & NFS_CAP_SECURITY_LABEL;
5895}
5896
5897static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5898 const void *buf, size_t buflen,
5899 int flags, int type)
5900{
5901 if (security_ismaclabel(key))
5902 return nfs4_set_security_label(dentry, buf, buflen);
5903
5904 return -EOPNOTSUPP;
5905}
5906
5907static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5908 void *buf, size_t buflen, int type)
5909{
5910 if (security_ismaclabel(key))
5911 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5912 return -EOPNOTSUPP;
5913}
5914
5915static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5916 size_t list_len, const char *name,
5917 size_t name_len, int type)
5918{
5919 size_t len = 0;
5920
5921 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5922 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5923 if (list && len <= list_len)
5924 security_inode_listsecurity(dentry->d_inode, list, len);
5925 }
5926 return len;
5927}
5928
5929static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5930 .prefix = XATTR_SECURITY_PREFIX,
5931 .list = nfs4_xattr_list_nfs4_label,
5932 .get = nfs4_xattr_get_nfs4_label,
5933 .set = nfs4_xattr_set_nfs4_label,
5934};
5935#endif
5936
5937
Andy Adamson533eb462011-06-13 18:25:56 -04005938/*
5939 * nfs_fhget will use either the mounted_on_fileid or the fileid
5940 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005941static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5942{
Andy Adamson533eb462011-06-13 18:25:56 -04005943 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5944 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5945 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005946 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005947 return;
5948
5949 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005950 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005951 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5952 fattr->nlink = 2;
5953}
5954
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005955static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5956 const struct qstr *name,
5957 struct nfs4_fs_locations *fs_locations,
5958 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005959{
5960 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005961 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005962 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005963 };
5964 struct nfs4_fs_locations_arg args = {
5965 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005966 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005967 .page = page,
5968 .bitmask = bitmask,
5969 };
Benny Halevy22958462009-04-01 09:22:02 -04005970 struct nfs4_fs_locations_res res = {
5971 .fs_locations = fs_locations,
5972 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005973 struct rpc_message msg = {
5974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5975 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005976 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005977 };
5978 int status;
5979
Harvey Harrison3110ff82008-05-02 13:42:44 -07005980 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005981
5982 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5983 * is not supported */
5984 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5985 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5986 else
5987 bitmask[0] |= FATTR4_WORD0_FILEID;
5988
Trond Myklebustc228fd32007-01-13 02:28:11 -05005989 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005990 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005991 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005992 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005993 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005994 return status;
5995}
5996
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005997int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5998 const struct qstr *name,
5999 struct nfs4_fs_locations *fs_locations,
6000 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006001{
6002 struct nfs4_exception exception = { };
6003 int err;
6004 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006005 err = _nfs4_proc_fs_locations(client, dir, name,
6006 fs_locations, page);
6007 trace_nfs4_get_fs_locations(dir, name, err);
6008 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006009 &exception);
6010 } while (exception.retry);
6011 return err;
6012}
6013
Chuck Leverb03d7352013-10-17 14:12:50 -04006014/*
6015 * This operation also signals the server that this client is
6016 * performing migration recovery. The server can stop returning
6017 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6018 * appended to this compound to identify the client ID which is
6019 * performing recovery.
6020 */
6021static int _nfs40_proc_get_locations(struct inode *inode,
6022 struct nfs4_fs_locations *locations,
6023 struct page *page, struct rpc_cred *cred)
6024{
6025 struct nfs_server *server = NFS_SERVER(inode);
6026 struct rpc_clnt *clnt = server->client;
6027 u32 bitmask[2] = {
6028 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6029 };
6030 struct nfs4_fs_locations_arg args = {
6031 .clientid = server->nfs_client->cl_clientid,
6032 .fh = NFS_FH(inode),
6033 .page = page,
6034 .bitmask = bitmask,
6035 .migration = 1, /* skip LOOKUP */
6036 .renew = 1, /* append RENEW */
6037 };
6038 struct nfs4_fs_locations_res res = {
6039 .fs_locations = locations,
6040 .migration = 1,
6041 .renew = 1,
6042 };
6043 struct rpc_message msg = {
6044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6045 .rpc_argp = &args,
6046 .rpc_resp = &res,
6047 .rpc_cred = cred,
6048 };
6049 unsigned long now = jiffies;
6050 int status;
6051
6052 nfs_fattr_init(&locations->fattr);
6053 locations->server = server;
6054 locations->nlocations = 0;
6055
6056 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6057 nfs4_set_sequence_privileged(&args.seq_args);
6058 status = nfs4_call_sync_sequence(clnt, server, &msg,
6059 &args.seq_args, &res.seq_res);
6060 if (status)
6061 return status;
6062
6063 renew_lease(server, now);
6064 return 0;
6065}
6066
6067#ifdef CONFIG_NFS_V4_1
6068
6069/*
6070 * This operation also signals the server that this client is
6071 * performing migration recovery. The server can stop asserting
6072 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6073 * performing this operation is identified in the SEQUENCE
6074 * operation in this compound.
6075 *
6076 * When the client supports GETATTR(fs_locations_info), it can
6077 * be plumbed in here.
6078 */
6079static int _nfs41_proc_get_locations(struct inode *inode,
6080 struct nfs4_fs_locations *locations,
6081 struct page *page, struct rpc_cred *cred)
6082{
6083 struct nfs_server *server = NFS_SERVER(inode);
6084 struct rpc_clnt *clnt = server->client;
6085 u32 bitmask[2] = {
6086 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6087 };
6088 struct nfs4_fs_locations_arg args = {
6089 .fh = NFS_FH(inode),
6090 .page = page,
6091 .bitmask = bitmask,
6092 .migration = 1, /* skip LOOKUP */
6093 };
6094 struct nfs4_fs_locations_res res = {
6095 .fs_locations = locations,
6096 .migration = 1,
6097 };
6098 struct rpc_message msg = {
6099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6100 .rpc_argp = &args,
6101 .rpc_resp = &res,
6102 .rpc_cred = cred,
6103 };
6104 int status;
6105
6106 nfs_fattr_init(&locations->fattr);
6107 locations->server = server;
6108 locations->nlocations = 0;
6109
6110 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6111 nfs4_set_sequence_privileged(&args.seq_args);
6112 status = nfs4_call_sync_sequence(clnt, server, &msg,
6113 &args.seq_args, &res.seq_res);
6114 if (status == NFS4_OK &&
6115 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6116 status = -NFS4ERR_LEASE_MOVED;
6117 return status;
6118}
6119
6120#endif /* CONFIG_NFS_V4_1 */
6121
6122/**
6123 * nfs4_proc_get_locations - discover locations for a migrated FSID
6124 * @inode: inode on FSID that is migrating
6125 * @locations: result of query
6126 * @page: buffer
6127 * @cred: credential to use for this operation
6128 *
6129 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6130 * operation failed, or a negative errno if a local error occurred.
6131 *
6132 * On success, "locations" is filled in, but if the server has
6133 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6134 * asserted.
6135 *
6136 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6137 * from this client that require migration recovery.
6138 */
6139int nfs4_proc_get_locations(struct inode *inode,
6140 struct nfs4_fs_locations *locations,
6141 struct page *page, struct rpc_cred *cred)
6142{
6143 struct nfs_server *server = NFS_SERVER(inode);
6144 struct nfs_client *clp = server->nfs_client;
6145 const struct nfs4_mig_recovery_ops *ops =
6146 clp->cl_mvops->mig_recovery_ops;
6147 struct nfs4_exception exception = { };
6148 int status;
6149
6150 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6151 (unsigned long long)server->fsid.major,
6152 (unsigned long long)server->fsid.minor,
6153 clp->cl_hostname);
6154 nfs_display_fhandle(NFS_FH(inode), __func__);
6155
6156 do {
6157 status = ops->get_locations(inode, locations, page, cred);
6158 if (status != -NFS4ERR_DELAY)
6159 break;
6160 nfs4_handle_exception(server, status, &exception);
6161 } while (exception.retry);
6162 return status;
6163}
6164
Chuck Lever44c99932013-10-17 14:13:30 -04006165/*
6166 * This operation also signals the server that this client is
6167 * performing "lease moved" recovery. The server can stop
6168 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6169 * is appended to this compound to identify the client ID which is
6170 * performing recovery.
6171 */
6172static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6173{
6174 struct nfs_server *server = NFS_SERVER(inode);
6175 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6176 struct rpc_clnt *clnt = server->client;
6177 struct nfs4_fsid_present_arg args = {
6178 .fh = NFS_FH(inode),
6179 .clientid = clp->cl_clientid,
6180 .renew = 1, /* append RENEW */
6181 };
6182 struct nfs4_fsid_present_res res = {
6183 .renew = 1,
6184 };
6185 struct rpc_message msg = {
6186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6187 .rpc_argp = &args,
6188 .rpc_resp = &res,
6189 .rpc_cred = cred,
6190 };
6191 unsigned long now = jiffies;
6192 int status;
6193
6194 res.fh = nfs_alloc_fhandle();
6195 if (res.fh == NULL)
6196 return -ENOMEM;
6197
6198 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6199 nfs4_set_sequence_privileged(&args.seq_args);
6200 status = nfs4_call_sync_sequence(clnt, server, &msg,
6201 &args.seq_args, &res.seq_res);
6202 nfs_free_fhandle(res.fh);
6203 if (status)
6204 return status;
6205
6206 do_renew_lease(clp, now);
6207 return 0;
6208}
6209
6210#ifdef CONFIG_NFS_V4_1
6211
6212/*
6213 * This operation also signals the server that this client is
6214 * performing "lease moved" recovery. The server can stop asserting
6215 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6216 * this operation is identified in the SEQUENCE operation in this
6217 * compound.
6218 */
6219static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6220{
6221 struct nfs_server *server = NFS_SERVER(inode);
6222 struct rpc_clnt *clnt = server->client;
6223 struct nfs4_fsid_present_arg args = {
6224 .fh = NFS_FH(inode),
6225 };
6226 struct nfs4_fsid_present_res res = {
6227 };
6228 struct rpc_message msg = {
6229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6230 .rpc_argp = &args,
6231 .rpc_resp = &res,
6232 .rpc_cred = cred,
6233 };
6234 int status;
6235
6236 res.fh = nfs_alloc_fhandle();
6237 if (res.fh == NULL)
6238 return -ENOMEM;
6239
6240 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6241 nfs4_set_sequence_privileged(&args.seq_args);
6242 status = nfs4_call_sync_sequence(clnt, server, &msg,
6243 &args.seq_args, &res.seq_res);
6244 nfs_free_fhandle(res.fh);
6245 if (status == NFS4_OK &&
6246 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6247 status = -NFS4ERR_LEASE_MOVED;
6248 return status;
6249}
6250
6251#endif /* CONFIG_NFS_V4_1 */
6252
6253/**
6254 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6255 * @inode: inode on FSID to check
6256 * @cred: credential to use for this operation
6257 *
6258 * Server indicates whether the FSID is present, moved, or not
6259 * recognized. This operation is necessary to clear a LEASE_MOVED
6260 * condition for this client ID.
6261 *
6262 * Returns NFS4_OK if the FSID is present on this server,
6263 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6264 * NFS4ERR code if some error occurred on the server, or a
6265 * negative errno if a local failure occurred.
6266 */
6267int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6268{
6269 struct nfs_server *server = NFS_SERVER(inode);
6270 struct nfs_client *clp = server->nfs_client;
6271 const struct nfs4_mig_recovery_ops *ops =
6272 clp->cl_mvops->mig_recovery_ops;
6273 struct nfs4_exception exception = { };
6274 int status;
6275
6276 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6277 (unsigned long long)server->fsid.major,
6278 (unsigned long long)server->fsid.minor,
6279 clp->cl_hostname);
6280 nfs_display_fhandle(NFS_FH(inode), __func__);
6281
6282 do {
6283 status = ops->fsid_present(inode, cred);
6284 if (status != -NFS4ERR_DELAY)
6285 break;
6286 nfs4_handle_exception(server, status, &exception);
6287 } while (exception.retry);
6288 return status;
6289}
6290
Andy Adamson5ec16a82013-08-08 10:57:55 -04006291/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006292 * If 'use_integrity' is true and the state managment nfs_client
6293 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6294 * and the machine credential as per RFC3530bis and RFC5661 Security
6295 * Considerations sections. Otherwise, just use the user cred with the
6296 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006297 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006298static 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 +00006299{
6300 int status;
6301 struct nfs4_secinfo_arg args = {
6302 .dir_fh = NFS_FH(dir),
6303 .name = name,
6304 };
6305 struct nfs4_secinfo_res res = {
6306 .flavors = flavors,
6307 };
6308 struct rpc_message msg = {
6309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6310 .rpc_argp = &args,
6311 .rpc_resp = &res,
6312 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006313 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006314 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006315
6316 if (use_integrity) {
6317 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006318 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6319 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006320 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006321
6322 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006323
6324 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6325 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6326
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006327 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6328 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006329 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006330
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006331 if (cred)
6332 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006333
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006334 return status;
6335}
6336
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006337int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6338 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006339{
6340 struct nfs4_exception exception = { };
6341 int err;
6342 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006343 err = -NFS4ERR_WRONGSEC;
6344
6345 /* try to use integrity protection with machine cred */
6346 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6347 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6348
6349 /*
6350 * if unable to use integrity protection, or SECINFO with
6351 * integrity protection returns NFS4ERR_WRONGSEC (which is
6352 * disallowed by spec, but exists in deployed servers) use
6353 * the current filesystem's rpc_client and the user cred.
6354 */
6355 if (err == -NFS4ERR_WRONGSEC)
6356 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6357
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006358 trace_nfs4_secinfo(dir, name, err);
6359 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006360 &exception);
6361 } while (exception.retry);
6362 return err;
6363}
6364
Andy Adamson557134a2009-04-01 09:21:53 -04006365#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006366/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006367 * Check the exchange flags returned by the server for invalid flags, having
6368 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6369 * DS flags set.
6370 */
6371static int nfs4_check_cl_exchange_flags(u32 flags)
6372{
6373 if (flags & ~EXCHGID4_FLAG_MASK_R)
6374 goto out_inval;
6375 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6376 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6377 goto out_inval;
6378 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6379 goto out_inval;
6380 return NFS_OK;
6381out_inval:
6382 return -NFS4ERR_INVAL;
6383}
6384
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006385static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006386nfs41_same_server_scope(struct nfs41_server_scope *a,
6387 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006388{
6389 if (a->server_scope_sz == b->server_scope_sz &&
6390 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6391 return true;
6392
6393 return false;
6394}
6395
Andy Adamson357f54d2010-12-14 10:11:57 -05006396/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006397 * nfs4_proc_bind_conn_to_session()
6398 *
6399 * The 4.1 client currently uses the same TCP connection for the
6400 * fore and backchannel.
6401 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006402int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006403{
6404 int status;
6405 struct nfs41_bind_conn_to_session_res res;
6406 struct rpc_message msg = {
6407 .rpc_proc =
6408 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6409 .rpc_argp = clp,
6410 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006411 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006412 };
6413
6414 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006415
6416 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6417 if (unlikely(res.session == NULL)) {
6418 status = -ENOMEM;
6419 goto out;
6420 }
6421
6422 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006423 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006424 if (status == 0) {
6425 if (memcmp(res.session->sess_id.data,
6426 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6427 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6428 status = -EIO;
6429 goto out_session;
6430 }
6431 if (res.dir != NFS4_CDFS4_BOTH) {
6432 dprintk("NFS: %s: Unexpected direction from server\n",
6433 __func__);
6434 status = -EIO;
6435 goto out_session;
6436 }
6437 if (res.use_conn_in_rdma_mode) {
6438 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6439 __func__);
6440 status = -EIO;
6441 goto out_session;
6442 }
6443 }
6444out_session:
6445 kfree(res.session);
6446out:
6447 dprintk("<-- %s status= %d\n", __func__, status);
6448 return status;
6449}
6450
6451/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006452 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6453 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006454 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006455static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6456 .how = SP4_MACH_CRED,
6457 .enforce.u.words = {
6458 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6459 1 << (OP_EXCHANGE_ID - 32) |
6460 1 << (OP_CREATE_SESSION - 32) |
6461 1 << (OP_DESTROY_SESSION - 32) |
6462 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006463 },
6464 .allow.u.words = {
6465 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006466 1 << (OP_LOCKU) |
6467 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006468 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006469 1 << (OP_SECINFO_NO_NAME - 32) |
6470 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006471 1 << (OP_FREE_STATEID - 32) |
6472 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006473 }
6474};
6475
6476/*
6477 * Select the state protection mode for client `clp' given the server results
6478 * from exchange_id in `sp'.
6479 *
6480 * Returns 0 on success, negative errno otherwise.
6481 */
6482static int nfs4_sp4_select_mode(struct nfs_client *clp,
6483 struct nfs41_state_protection *sp)
6484{
6485 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6486 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6487 1 << (OP_EXCHANGE_ID - 32) |
6488 1 << (OP_CREATE_SESSION - 32) |
6489 1 << (OP_DESTROY_SESSION - 32) |
6490 1 << (OP_DESTROY_CLIENTID - 32)
6491 };
6492 unsigned int i;
6493
6494 if (sp->how == SP4_MACH_CRED) {
6495 /* Print state protect result */
6496 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6497 for (i = 0; i <= LAST_NFS4_OP; i++) {
6498 if (test_bit(i, sp->enforce.u.longs))
6499 dfprintk(MOUNT, " enforce op %d\n", i);
6500 if (test_bit(i, sp->allow.u.longs))
6501 dfprintk(MOUNT, " allow op %d\n", i);
6502 }
6503
6504 /* make sure nothing is on enforce list that isn't supported */
6505 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6506 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6507 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6508 return -EINVAL;
6509 }
6510 }
6511
6512 /*
6513 * Minimal mode - state operations are allowed to use machine
6514 * credential. Note this already happens by default, so the
6515 * client doesn't have to do anything more than the negotiation.
6516 *
6517 * NOTE: we don't care if EXCHANGE_ID is in the list -
6518 * we're already using the machine cred for exchange_id
6519 * and will never use a different cred.
6520 */
6521 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6522 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6523 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6524 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6525 dfprintk(MOUNT, "sp4_mach_cred:\n");
6526 dfprintk(MOUNT, " minimal mode enabled\n");
6527 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6528 } else {
6529 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6530 return -EINVAL;
6531 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006532
6533 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6534 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6535 dfprintk(MOUNT, " cleanup mode enabled\n");
6536 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6537 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006538
6539 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6540 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6541 dfprintk(MOUNT, " secinfo mode enabled\n");
6542 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6543 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006544
6545 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6546 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6547 dfprintk(MOUNT, " stateid mode enabled\n");
6548 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6549 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006550
6551 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6552 dfprintk(MOUNT, " write mode enabled\n");
6553 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6554 }
6555
6556 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6557 dfprintk(MOUNT, " commit mode enabled\n");
6558 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6559 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006560 }
6561
6562 return 0;
6563}
6564
6565/*
6566 * _nfs4_proc_exchange_id()
6567 *
6568 * Wrapper for EXCHANGE_ID operation.
6569 */
6570static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6571 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006572{
6573 nfs4_verifier verifier;
6574 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006575 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006576 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006577 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6578 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006579 };
6580 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006581 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006582 };
6583 int status;
6584 struct rpc_message msg = {
6585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6586 .rpc_argp = &args,
6587 .rpc_resp = &res,
6588 .rpc_cred = cred,
6589 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006590
Chuck Leverf0920752012-05-21 22:45:41 -04006591 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006592 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6593 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006594 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6595 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6596 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006597
Chuck Leveracdeb692012-05-21 22:46:16 -04006598 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006599 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006600 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006601 status = -ENOMEM;
6602 goto out;
6603 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006604
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006605 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006606 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006607 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006608 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006609 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006610 }
6611
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006612 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006613 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006614 status = -ENOMEM;
6615 goto out_server_scope;
6616 }
6617
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006618 switch (sp4_how) {
6619 case SP4_NONE:
6620 args.state_protect.how = SP4_NONE;
6621 break;
6622
6623 case SP4_MACH_CRED:
6624 args.state_protect = nfs4_sp4_mach_cred_request;
6625 break;
6626
6627 default:
6628 /* unsupported! */
6629 WARN_ON_ONCE(1);
6630 status = -EINVAL;
6631 goto out_server_scope;
6632 }
6633
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006634 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006635 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006636 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006637 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006638
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006639 if (status == 0)
6640 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6641
Chuck Lever177313f2012-05-21 22:44:58 -04006642 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006643 clp->cl_clientid = res.clientid;
6644 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6645 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6646 clp->cl_seqid = res.seqid;
6647
Chuck Leveracdeb692012-05-21 22:46:16 -04006648 kfree(clp->cl_serverowner);
6649 clp->cl_serverowner = res.server_owner;
6650 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006651
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006652 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006653 kfree(clp->cl_implid);
6654 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006655
Chuck Lever177313f2012-05-21 22:44:58 -04006656 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006657 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006658 res.server_scope)) {
6659 dprintk("%s: server_scope mismatch detected\n",
6660 __func__);
6661 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006662 kfree(clp->cl_serverscope);
6663 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006664 }
6665
Chuck Lever177313f2012-05-21 22:44:58 -04006666 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006667 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006668 goto out;
6669 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006670 } else
6671 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006672
Chuck Leveracdeb692012-05-21 22:46:16 -04006673out_server_owner:
6674 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006675out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006676 kfree(res.server_scope);
6677out:
Chuck Lever177313f2012-05-21 22:44:58 -04006678 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006679 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006680 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006681 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006682 clp->cl_implid->date.seconds,
6683 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006684 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006685 return status;
6686}
6687
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006688/*
6689 * nfs4_proc_exchange_id()
6690 *
6691 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6692 *
6693 * Since the clientid has expired, all compounds using sessions
6694 * associated with the stale clientid will be returning
6695 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6696 * be in some phase of session reset.
6697 *
6698 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6699 */
6700int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6701{
6702 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6703 int status;
6704
6705 /* try SP4_MACH_CRED if krb5i/p */
6706 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6707 authflavor == RPC_AUTH_GSS_KRB5P) {
6708 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6709 if (!status)
6710 return 0;
6711 }
6712
6713 /* try SP4_NONE */
6714 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6715}
6716
Trond Myklebust66245532012-05-25 17:18:09 -04006717static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6718 struct rpc_cred *cred)
6719{
6720 struct rpc_message msg = {
6721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6722 .rpc_argp = clp,
6723 .rpc_cred = cred,
6724 };
6725 int status;
6726
6727 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006728 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006729 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006730 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006731 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6732 return status;
6733}
6734
6735static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6736 struct rpc_cred *cred)
6737{
6738 unsigned int loop;
6739 int ret;
6740
6741 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6742 ret = _nfs4_proc_destroy_clientid(clp, cred);
6743 switch (ret) {
6744 case -NFS4ERR_DELAY:
6745 case -NFS4ERR_CLIENTID_BUSY:
6746 ssleep(1);
6747 break;
6748 default:
6749 return ret;
6750 }
6751 }
6752 return 0;
6753}
6754
6755int nfs4_destroy_clientid(struct nfs_client *clp)
6756{
6757 struct rpc_cred *cred;
6758 int ret = 0;
6759
6760 if (clp->cl_mvops->minor_version < 1)
6761 goto out;
6762 if (clp->cl_exchange_flags == 0)
6763 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006764 if (clp->cl_preserve_clid)
6765 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006766 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006767 ret = nfs4_proc_destroy_clientid(clp, cred);
6768 if (cred)
6769 put_rpccred(cred);
6770 switch (ret) {
6771 case 0:
6772 case -NFS4ERR_STALE_CLIENTID:
6773 clp->cl_exchange_flags = 0;
6774 }
6775out:
6776 return ret;
6777}
6778
Andy Adamson2050f0c2009-04-01 09:22:30 -04006779struct nfs4_get_lease_time_data {
6780 struct nfs4_get_lease_time_args *args;
6781 struct nfs4_get_lease_time_res *res;
6782 struct nfs_client *clp;
6783};
6784
6785static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6786 void *calldata)
6787{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006788 struct nfs4_get_lease_time_data *data =
6789 (struct nfs4_get_lease_time_data *)calldata;
6790
6791 dprintk("--> %s\n", __func__);
6792 /* just setup sequence, do not trigger session recovery
6793 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006794 nfs41_setup_sequence(data->clp->cl_session,
6795 &data->args->la_seq_args,
6796 &data->res->lr_seq_res,
6797 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006798 dprintk("<-- %s\n", __func__);
6799}
6800
6801/*
6802 * Called from nfs4_state_manager thread for session setup, so don't recover
6803 * from sequence operation or clientid errors.
6804 */
6805static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6806{
6807 struct nfs4_get_lease_time_data *data =
6808 (struct nfs4_get_lease_time_data *)calldata;
6809
6810 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006811 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6812 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006813 switch (task->tk_status) {
6814 case -NFS4ERR_DELAY:
6815 case -NFS4ERR_GRACE:
6816 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6817 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6818 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006819 /* fall through */
6820 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006821 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006822 return;
6823 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006824 dprintk("<-- %s\n", __func__);
6825}
6826
Trond Myklebust17280172012-03-11 13:11:00 -04006827static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006828 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6829 .rpc_call_done = nfs4_get_lease_time_done,
6830};
6831
6832int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6833{
6834 struct rpc_task *task;
6835 struct nfs4_get_lease_time_args args;
6836 struct nfs4_get_lease_time_res res = {
6837 .lr_fsinfo = fsinfo,
6838 };
6839 struct nfs4_get_lease_time_data data = {
6840 .args = &args,
6841 .res = &res,
6842 .clp = clp,
6843 };
6844 struct rpc_message msg = {
6845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6846 .rpc_argp = &args,
6847 .rpc_resp = &res,
6848 };
6849 struct rpc_task_setup task_setup = {
6850 .rpc_client = clp->cl_rpcclient,
6851 .rpc_message = &msg,
6852 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006853 .callback_data = &data,
6854 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006855 };
6856 int status;
6857
Chuck Levera9c92d62013-08-09 12:48:18 -04006858 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006859 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006860 dprintk("--> %s\n", __func__);
6861 task = rpc_run_task(&task_setup);
6862
6863 if (IS_ERR(task))
6864 status = PTR_ERR(task);
6865 else {
6866 status = task->tk_status;
6867 rpc_put_task(task);
6868 }
6869 dprintk("<-- %s return %d\n", __func__, status);
6870
6871 return status;
6872}
6873
Andy Adamsonfc931582009-04-01 09:22:31 -04006874/*
6875 * Initialize the values to be used by the client in CREATE_SESSION
6876 * If nfs4_init_session set the fore channel request and response sizes,
6877 * use them.
6878 *
6879 * Set the back channel max_resp_sz_cached to zero to force the client to
6880 * always set csa_cachethis to FALSE because the current implementation
6881 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6882 */
6883static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6884{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006885 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006886
Andy Adamson18aad3d2013-06-26 12:21:49 -04006887 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6888 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6889
Andy Adamsonfc931582009-04-01 09:22:31 -04006890 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006891 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6892 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006893 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006894 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006895
6896 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006897 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006898 __func__,
6899 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006900 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006901
6902 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006903 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6904 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6905 args->bc_attrs.max_resp_sz_cached = 0;
6906 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6907 args->bc_attrs.max_reqs = 1;
6908
6909 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6910 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6911 __func__,
6912 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6913 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6914 args->bc_attrs.max_reqs);
6915}
6916
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006917static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006918{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006919 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6920 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6921
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006922 if (rcvd->max_resp_sz > sent->max_resp_sz)
6923 return -EINVAL;
6924 /*
6925 * Our requested max_ops is the minimum we need; we're not
6926 * prepared to break up compounds into smaller pieces than that.
6927 * So, no point even trying to continue if the server won't
6928 * cooperate:
6929 */
6930 if (rcvd->max_ops < sent->max_ops)
6931 return -EINVAL;
6932 if (rcvd->max_reqs == 0)
6933 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006934 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6935 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006936 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006937}
6938
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006939static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6940{
6941 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6942 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006943
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006944 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6945 return -EINVAL;
6946 if (rcvd->max_resp_sz < sent->max_resp_sz)
6947 return -EINVAL;
6948 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6949 return -EINVAL;
6950 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006951 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006952 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006953 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006954 return -EINVAL;
6955 return 0;
6956}
Andy Adamson8d353012009-04-01 09:22:32 -04006957
Andy Adamson8d353012009-04-01 09:22:32 -04006958static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6959 struct nfs4_session *session)
6960{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006961 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006962
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006963 ret = nfs4_verify_fore_channel_attrs(args, session);
6964 if (ret)
6965 return ret;
6966 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006967}
6968
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006969static int _nfs4_proc_create_session(struct nfs_client *clp,
6970 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006971{
6972 struct nfs4_session *session = clp->cl_session;
6973 struct nfs41_create_session_args args = {
6974 .client = clp,
6975 .cb_program = NFS4_CALLBACK,
6976 };
6977 struct nfs41_create_session_res res = {
6978 .client = clp,
6979 };
6980 struct rpc_message msg = {
6981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6982 .rpc_argp = &args,
6983 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006984 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006985 };
6986 int status;
6987
6988 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006989 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006990
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006991 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006992 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006993
Trond Myklebust43095d32012-11-20 11:13:12 -05006994 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006995 /* Verify the session's negotiated channel_attrs values */
6996 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006997 /* Increment the clientid slot sequence id */
6998 clp->cl_seqid++;
6999 }
7000
7001 return status;
7002}
7003
7004/*
7005 * Issues a CREATE_SESSION operation to the server.
7006 * It is the responsibility of the caller to verify the session is
7007 * expired before calling this routine.
7008 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007009int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007010{
7011 int status;
7012 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007013 struct nfs4_session *session = clp->cl_session;
7014
7015 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7016
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007017 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007018 if (status)
7019 goto out;
7020
Andy Adamsonaacd5532011-11-09 13:58:21 -05007021 /* Init or reset the session slot tables */
7022 status = nfs4_setup_session_slot_tables(session);
7023 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007024 if (status)
7025 goto out;
7026
7027 ptr = (unsigned *)&session->sess_id.data[0];
7028 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7029 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007030out:
7031 dprintk("<-- %s\n", __func__);
7032 return status;
7033}
7034
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007035/*
7036 * Issue the over-the-wire RPC DESTROY_SESSION.
7037 * The caller must serialize access to this routine.
7038 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007039int nfs4_proc_destroy_session(struct nfs4_session *session,
7040 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007041{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007042 struct rpc_message msg = {
7043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7044 .rpc_argp = session,
7045 .rpc_cred = cred,
7046 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007047 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007048
7049 dprintk("--> nfs4_proc_destroy_session\n");
7050
7051 /* session is still being setup */
7052 if (session->clp->cl_cons_state != NFS_CS_READY)
7053 return status;
7054
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007055 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007056 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007057
7058 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007059 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007060 "Session has been destroyed regardless...\n", status);
7061
7062 dprintk("<-- nfs4_proc_destroy_session\n");
7063 return status;
7064}
7065
Trond Myklebust7b38c362012-05-23 13:23:31 -04007066/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007067 * Renew the cl_session lease.
7068 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007069struct nfs4_sequence_data {
7070 struct nfs_client *clp;
7071 struct nfs4_sequence_args args;
7072 struct nfs4_sequence_res res;
7073};
7074
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007075static void nfs41_sequence_release(void *data)
7076{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007077 struct nfs4_sequence_data *calldata = data;
7078 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007079
Alexandros Batsakis71358402010-02-05 03:45:05 -08007080 if (atomic_read(&clp->cl_count) > 1)
7081 nfs4_schedule_state_renewal(clp);
7082 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007083 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007084}
7085
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007086static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7087{
7088 switch(task->tk_status) {
7089 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007090 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7091 return -EAGAIN;
7092 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007093 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007094 }
7095 return 0;
7096}
7097
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007098static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007099{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007100 struct nfs4_sequence_data *calldata = data;
7101 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007102
Trond Myklebust14516c32010-07-31 14:29:06 -04007103 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7104 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007105
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007106 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007107 if (task->tk_status < 0) {
7108 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007109 if (atomic_read(&clp->cl_count) == 1)
7110 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007111
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007112 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7113 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007114 return;
7115 }
7116 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007117 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007118out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007119 dprintk("<-- %s\n", __func__);
7120}
7121
7122static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7123{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007124 struct nfs4_sequence_data *calldata = data;
7125 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007126 struct nfs4_sequence_args *args;
7127 struct nfs4_sequence_res *res;
7128
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007129 args = task->tk_msg.rpc_argp;
7130 res = task->tk_msg.rpc_resp;
7131
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007132 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007133}
7134
7135static const struct rpc_call_ops nfs41_sequence_ops = {
7136 .rpc_call_done = nfs41_sequence_call_done,
7137 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007138 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007139};
7140
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007141static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7142 struct rpc_cred *cred,
7143 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007144{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007145 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007146 struct rpc_message msg = {
7147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7148 .rpc_cred = cred,
7149 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007150 struct rpc_task_setup task_setup_data = {
7151 .rpc_client = clp->cl_rpcclient,
7152 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007153 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007154 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007155 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007156
Alexandros Batsakis71358402010-02-05 03:45:05 -08007157 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007158 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007159 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007160 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007161 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007162 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007163 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007164 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007165 if (is_privileged)
7166 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007167 msg.rpc_argp = &calldata->args;
7168 msg.rpc_resp = &calldata->res;
7169 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007170 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007171
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007172 return rpc_run_task(&task_setup_data);
7173}
7174
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007175static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007176{
7177 struct rpc_task *task;
7178 int ret = 0;
7179
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007180 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7181 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007182 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007183 if (IS_ERR(task))
7184 ret = PTR_ERR(task);
7185 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007186 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007187 dprintk("<-- %s status=%d\n", __func__, ret);
7188 return ret;
7189}
7190
7191static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7192{
7193 struct rpc_task *task;
7194 int ret;
7195
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007196 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007197 if (IS_ERR(task)) {
7198 ret = PTR_ERR(task);
7199 goto out;
7200 }
7201 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007202 if (!ret) {
7203 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7204
7205 if (task->tk_status == 0)
7206 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007207 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007208 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007209 rpc_put_task(task);
7210out:
7211 dprintk("<-- %s status=%d\n", __func__, ret);
7212 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007213}
7214
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007215struct nfs4_reclaim_complete_data {
7216 struct nfs_client *clp;
7217 struct nfs41_reclaim_complete_args arg;
7218 struct nfs41_reclaim_complete_res res;
7219};
7220
7221static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7222{
7223 struct nfs4_reclaim_complete_data *calldata = data;
7224
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007225 nfs41_setup_sequence(calldata->clp->cl_session,
7226 &calldata->arg.seq_args,
7227 &calldata->res.seq_res,
7228 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007229}
7230
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007231static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7232{
7233 switch(task->tk_status) {
7234 case 0:
7235 case -NFS4ERR_COMPLETE_ALREADY:
7236 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7237 break;
7238 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007239 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007240 /* fall through */
7241 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007242 return -EAGAIN;
7243 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007244 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007245 }
7246 return 0;
7247}
7248
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007249static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7250{
7251 struct nfs4_reclaim_complete_data *calldata = data;
7252 struct nfs_client *clp = calldata->clp;
7253 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7254
7255 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007256 if (!nfs41_sequence_done(task, res))
7257 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007258
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007259 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007260 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7261 rpc_restart_call_prepare(task);
7262 return;
7263 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007264 dprintk("<-- %s\n", __func__);
7265}
7266
7267static void nfs4_free_reclaim_complete_data(void *data)
7268{
7269 struct nfs4_reclaim_complete_data *calldata = data;
7270
7271 kfree(calldata);
7272}
7273
7274static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7275 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7276 .rpc_call_done = nfs4_reclaim_complete_done,
7277 .rpc_release = nfs4_free_reclaim_complete_data,
7278};
7279
7280/*
7281 * Issue a global reclaim complete.
7282 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007283static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7284 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007285{
7286 struct nfs4_reclaim_complete_data *calldata;
7287 struct rpc_task *task;
7288 struct rpc_message msg = {
7289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007290 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007291 };
7292 struct rpc_task_setup task_setup_data = {
7293 .rpc_client = clp->cl_rpcclient,
7294 .rpc_message = &msg,
7295 .callback_ops = &nfs4_reclaim_complete_call_ops,
7296 .flags = RPC_TASK_ASYNC,
7297 };
7298 int status = -ENOMEM;
7299
7300 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007301 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007302 if (calldata == NULL)
7303 goto out;
7304 calldata->clp = clp;
7305 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007306
Chuck Levera9c92d62013-08-09 12:48:18 -04007307 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007308 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007309 msg.rpc_argp = &calldata->arg;
7310 msg.rpc_resp = &calldata->res;
7311 task_setup_data.callback_data = calldata;
7312 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007313 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007314 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007315 goto out;
7316 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007317 status = nfs4_wait_for_completion_rpc_task(task);
7318 if (status == 0)
7319 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007320 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007321 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007322out:
7323 dprintk("<-- %s status=%d\n", __func__, status);
7324 return status;
7325}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007326
7327static void
7328nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7329{
7330 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007331 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007332 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007333
7334 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007335 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7336 * right now covering the LAYOUTGET we are about to send.
7337 * However, that is not so catastrophic, and there seems
7338 * to be no way to prevent it completely.
7339 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007340 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007341 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007342 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007343 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7344 NFS_I(lgp->args.inode)->layout,
7345 lgp->args.ctx->state)) {
7346 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007347 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007348}
7349
7350static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7351{
7352 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007353 struct inode *inode = lgp->args.inode;
7354 struct nfs_server *server = NFS_SERVER(inode);
7355 struct pnfs_layout_hdr *lo;
7356 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007357 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007358
7359 dprintk("--> %s\n", __func__);
7360
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007361 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007362 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007363
7364 switch (task->tk_status) {
7365 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007366 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007367 case -NFS4ERR_LAYOUTTRYLATER:
7368 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007369 timeo = rpc_get_timeout(task->tk_client);
7370 giveup = lgp->args.timestamp + timeo;
7371 if (time_after(giveup, jiffies))
7372 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07007373 break;
7374 case -NFS4ERR_EXPIRED:
7375 case -NFS4ERR_BAD_STATEID:
7376 spin_lock(&inode->i_lock);
7377 lo = NFS_I(inode)->layout;
7378 if (!lo || list_empty(&lo->plh_segs)) {
7379 spin_unlock(&inode->i_lock);
7380 /* If the open stateid was bad, then recover it. */
7381 state = lgp->args.ctx->state;
7382 } else {
7383 LIST_HEAD(head);
7384
7385 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7386 spin_unlock(&inode->i_lock);
7387 /* Mark the bad layout state as invalid, then
7388 * retry using the open stateid. */
7389 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007390 }
7391 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007392 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7393 rpc_restart_call_prepare(task);
7394out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007395 dprintk("<-- %s\n", __func__);
7396}
7397
Idan Kedar85541162012-08-02 11:47:10 +03007398static size_t max_response_pages(struct nfs_server *server)
7399{
7400 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7401 return nfs_page_array_len(0, max_resp_sz);
7402}
7403
7404static void nfs4_free_pages(struct page **pages, size_t size)
7405{
7406 int i;
7407
7408 if (!pages)
7409 return;
7410
7411 for (i = 0; i < size; i++) {
7412 if (!pages[i])
7413 break;
7414 __free_page(pages[i]);
7415 }
7416 kfree(pages);
7417}
7418
7419static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7420{
7421 struct page **pages;
7422 int i;
7423
7424 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7425 if (!pages) {
7426 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7427 return NULL;
7428 }
7429
7430 for (i = 0; i < size; i++) {
7431 pages[i] = alloc_page(gfp_flags);
7432 if (!pages[i]) {
7433 dprintk("%s: failed to allocate page\n", __func__);
7434 nfs4_free_pages(pages, size);
7435 return NULL;
7436 }
7437 }
7438
7439 return pages;
7440}
7441
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007442static void nfs4_layoutget_release(void *calldata)
7443{
7444 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007445 struct inode *inode = lgp->args.inode;
7446 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007447 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007448
7449 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007450 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007451 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007452 put_nfs_open_context(lgp->args.ctx);
7453 kfree(calldata);
7454 dprintk("<-- %s\n", __func__);
7455}
7456
7457static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7458 .rpc_call_prepare = nfs4_layoutget_prepare,
7459 .rpc_call_done = nfs4_layoutget_done,
7460 .rpc_release = nfs4_layoutget_release,
7461};
7462
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007463struct pnfs_layout_segment *
7464nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007465{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007466 struct inode *inode = lgp->args.inode;
7467 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007468 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007469 struct rpc_task *task;
7470 struct rpc_message msg = {
7471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7472 .rpc_argp = &lgp->args,
7473 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007474 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007475 };
7476 struct rpc_task_setup task_setup_data = {
7477 .rpc_client = server->client,
7478 .rpc_message = &msg,
7479 .callback_ops = &nfs4_layoutget_call_ops,
7480 .callback_data = lgp,
7481 .flags = RPC_TASK_ASYNC,
7482 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007483 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007484 int status = 0;
7485
7486 dprintk("--> %s\n", __func__);
7487
Idan Kedar85541162012-08-02 11:47:10 +03007488 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7489 if (!lgp->args.layout.pages) {
7490 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007491 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007492 }
7493 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007494 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007495
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007496 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007497 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007498 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007499
7500 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7501 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7502
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007503 task = rpc_run_task(&task_setup_data);
7504 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007505 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007506 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007507 if (status == 0)
7508 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007509 trace_nfs4_layoutget(lgp->args.ctx,
7510 &lgp->args.range,
7511 &lgp->res.range,
7512 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007513 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7514 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007515 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007516 rpc_put_task(task);
7517 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007518 if (status)
7519 return ERR_PTR(status);
7520 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007521}
7522
Benny Halevycbe82602011-05-22 19:52:37 +03007523static void
7524nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7525{
7526 struct nfs4_layoutreturn *lrp = calldata;
7527
7528 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007529 nfs41_setup_sequence(lrp->clp->cl_session,
7530 &lrp->args.seq_args,
7531 &lrp->res.seq_res,
7532 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007533}
7534
7535static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7536{
7537 struct nfs4_layoutreturn *lrp = calldata;
7538 struct nfs_server *server;
7539
7540 dprintk("--> %s\n", __func__);
7541
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007542 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007543 return;
7544
7545 server = NFS_SERVER(lrp->args.inode);
7546 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007547 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007548 return;
7549 }
Benny Halevycbe82602011-05-22 19:52:37 +03007550 dprintk("<-- %s\n", __func__);
7551}
7552
7553static void nfs4_layoutreturn_release(void *calldata)
7554{
7555 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007556 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007557
7558 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007559 spin_lock(&lo->plh_inode->i_lock);
7560 if (lrp->res.lrs_present)
7561 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7562 lo->plh_block_lgets--;
7563 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007564 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007565 kfree(calldata);
7566 dprintk("<-- %s\n", __func__);
7567}
7568
7569static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7570 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7571 .rpc_call_done = nfs4_layoutreturn_done,
7572 .rpc_release = nfs4_layoutreturn_release,
7573};
7574
7575int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7576{
7577 struct rpc_task *task;
7578 struct rpc_message msg = {
7579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7580 .rpc_argp = &lrp->args,
7581 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007582 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007583 };
7584 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007585 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007586 .rpc_message = &msg,
7587 .callback_ops = &nfs4_layoutreturn_call_ops,
7588 .callback_data = lrp,
7589 };
7590 int status;
7591
7592 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007593 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007594 task = rpc_run_task(&task_setup_data);
7595 if (IS_ERR(task))
7596 return PTR_ERR(task);
7597 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007598 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007599 dprintk("<-- %s status=%d\n", __func__, status);
7600 rpc_put_task(task);
7601 return status;
7602}
7603
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007604/*
7605 * Retrieve the list of Data Server devices from the MDS.
7606 */
7607static int _nfs4_getdevicelist(struct nfs_server *server,
7608 const struct nfs_fh *fh,
7609 struct pnfs_devicelist *devlist)
7610{
7611 struct nfs4_getdevicelist_args args = {
7612 .fh = fh,
7613 .layoutclass = server->pnfs_curr_ld->id,
7614 };
7615 struct nfs4_getdevicelist_res res = {
7616 .devlist = devlist,
7617 };
7618 struct rpc_message msg = {
7619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7620 .rpc_argp = &args,
7621 .rpc_resp = &res,
7622 };
7623 int status;
7624
7625 dprintk("--> %s\n", __func__);
7626 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7627 &res.seq_res, 0);
7628 dprintk("<-- %s status=%d\n", __func__, status);
7629 return status;
7630}
7631
7632int nfs4_proc_getdevicelist(struct nfs_server *server,
7633 const struct nfs_fh *fh,
7634 struct pnfs_devicelist *devlist)
7635{
7636 struct nfs4_exception exception = { };
7637 int err;
7638
7639 do {
7640 err = nfs4_handle_exception(server,
7641 _nfs4_getdevicelist(server, fh, devlist),
7642 &exception);
7643 } while (exception.retry);
7644
7645 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7646 err, devlist->num_devs);
7647
7648 return err;
7649}
7650EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7651
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007652static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007653_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7654 struct pnfs_device *pdev,
7655 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007656{
7657 struct nfs4_getdeviceinfo_args args = {
7658 .pdev = pdev,
7659 };
7660 struct nfs4_getdeviceinfo_res res = {
7661 .pdev = pdev,
7662 };
7663 struct rpc_message msg = {
7664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7665 .rpc_argp = &args,
7666 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007667 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007668 };
7669 int status;
7670
7671 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007672 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007673 dprintk("<-- %s status=%d\n", __func__, status);
7674
7675 return status;
7676}
7677
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007678int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7679 struct pnfs_device *pdev,
7680 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007681{
7682 struct nfs4_exception exception = { };
7683 int err;
7684
7685 do {
7686 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007687 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007688 &exception);
7689 } while (exception.retry);
7690 return err;
7691}
7692EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7693
Andy Adamson863a3c62011-03-23 13:27:54 +00007694static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7695{
7696 struct nfs4_layoutcommit_data *data = calldata;
7697 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007698 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007699
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007700 nfs41_setup_sequence(session,
7701 &data->args.seq_args,
7702 &data->res.seq_res,
7703 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007704}
7705
7706static void
7707nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7708{
7709 struct nfs4_layoutcommit_data *data = calldata;
7710 struct nfs_server *server = NFS_SERVER(data->args.inode);
7711
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007712 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007713 return;
7714
7715 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007716 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7717 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7718 case -NFS4ERR_BADLAYOUT: /* no layout */
7719 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007720 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007721 break;
7722 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007723 nfs_post_op_update_inode_force_wcc(data->args.inode,
7724 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007725 break;
7726 default:
7727 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7728 rpc_restart_call_prepare(task);
7729 return;
7730 }
7731 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007732}
7733
7734static void nfs4_layoutcommit_release(void *calldata)
7735{
7736 struct nfs4_layoutcommit_data *data = calldata;
7737
Andy Adamsondb29c082011-07-30 20:52:38 -04007738 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007739 put_rpccred(data->cred);
7740 kfree(data);
7741}
7742
7743static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7744 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7745 .rpc_call_done = nfs4_layoutcommit_done,
7746 .rpc_release = nfs4_layoutcommit_release,
7747};
7748
7749int
Andy Adamsonef311532011-03-12 02:58:10 -05007750nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007751{
7752 struct rpc_message msg = {
7753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7754 .rpc_argp = &data->args,
7755 .rpc_resp = &data->res,
7756 .rpc_cred = data->cred,
7757 };
7758 struct rpc_task_setup task_setup_data = {
7759 .task = &data->task,
7760 .rpc_client = NFS_CLIENT(data->args.inode),
7761 .rpc_message = &msg,
7762 .callback_ops = &nfs4_layoutcommit_ops,
7763 .callback_data = data,
7764 .flags = RPC_TASK_ASYNC,
7765 };
7766 struct rpc_task *task;
7767 int status = 0;
7768
7769 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7770 "lbw: %llu inode %lu\n",
7771 data->task.tk_pid, sync,
7772 data->args.lastbytewritten,
7773 data->args.inode->i_ino);
7774
Chuck Levera9c92d62013-08-09 12:48:18 -04007775 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007776 task = rpc_run_task(&task_setup_data);
7777 if (IS_ERR(task))
7778 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007779 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007780 goto out;
7781 status = nfs4_wait_for_completion_rpc_task(task);
7782 if (status != 0)
7783 goto out;
7784 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007785 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007786out:
7787 dprintk("%s: status %d\n", __func__, status);
7788 rpc_put_task(task);
7789 return status;
7790}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007791
Andy Adamson97431202013-08-08 10:57:56 -04007792/**
7793 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7794 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7795 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007796static int
7797_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007798 struct nfs_fsinfo *info,
7799 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007800{
7801 struct nfs41_secinfo_no_name_args args = {
7802 .style = SECINFO_STYLE_CURRENT_FH,
7803 };
7804 struct nfs4_secinfo_res res = {
7805 .flavors = flavors,
7806 };
7807 struct rpc_message msg = {
7808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7809 .rpc_argp = &args,
7810 .rpc_resp = &res,
7811 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007812 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007813 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007814 int status;
7815
7816 if (use_integrity) {
7817 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007818 cred = nfs4_get_clid_cred(server->nfs_client);
7819 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007820 }
7821
7822 dprintk("--> %s\n", __func__);
7823 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7824 &res.seq_res, 0);
7825 dprintk("<-- %s status=%d\n", __func__, status);
7826
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007827 if (cred)
7828 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007829
7830 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007831}
7832
7833static int
7834nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7835 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7836{
7837 struct nfs4_exception exception = { };
7838 int err;
7839 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007840 /* first try using integrity protection */
7841 err = -NFS4ERR_WRONGSEC;
7842
7843 /* try to use integrity protection with machine cred */
7844 if (_nfs4_is_integrity_protected(server->nfs_client))
7845 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7846 flavors, true);
7847
7848 /*
7849 * if unable to use integrity protection, or SECINFO with
7850 * integrity protection returns NFS4ERR_WRONGSEC (which is
7851 * disallowed by spec, but exists in deployed servers) use
7852 * the current filesystem's rpc_client and the user cred.
7853 */
7854 if (err == -NFS4ERR_WRONGSEC)
7855 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7856 flavors, false);
7857
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007858 switch (err) {
7859 case 0:
7860 case -NFS4ERR_WRONGSEC:
7861 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007862 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007863 default:
7864 err = nfs4_handle_exception(server, err, &exception);
7865 }
7866 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007867out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007868 return err;
7869}
7870
7871static int
7872nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7873 struct nfs_fsinfo *info)
7874{
7875 int err;
7876 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007877 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007878 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007879 struct nfs4_secinfo4 *secinfo;
7880 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007881
7882 page = alloc_page(GFP_KERNEL);
7883 if (!page) {
7884 err = -ENOMEM;
7885 goto out;
7886 }
7887
7888 flavors = page_address(page);
7889 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7890
7891 /*
7892 * Fall back on "guess and check" method if
7893 * the server doesn't support SECINFO_NO_NAME
7894 */
7895 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7896 err = nfs4_find_root_sec(server, fhandle, info);
7897 goto out_freepage;
7898 }
7899 if (err)
7900 goto out_freepage;
7901
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007902 for (i = 0; i < flavors->num_flavors; i++) {
7903 secinfo = &flavors->flavors[i];
7904
7905 switch (secinfo->flavor) {
7906 case RPC_AUTH_NULL:
7907 case RPC_AUTH_UNIX:
7908 case RPC_AUTH_GSS:
7909 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7910 &secinfo->flavor_info);
7911 break;
7912 default:
7913 flavor = RPC_AUTH_MAXFLAVOR;
7914 break;
7915 }
7916
7917 if (flavor != RPC_AUTH_MAXFLAVOR) {
7918 err = nfs4_lookup_root_sec(server, fhandle,
7919 info, flavor);
7920 if (!err)
7921 break;
7922 }
7923 }
7924
7925 if (flavor == RPC_AUTH_MAXFLAVOR)
7926 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007927
7928out_freepage:
7929 put_page(page);
7930 if (err == -EACCES)
7931 return -EPERM;
7932out:
7933 return err;
7934}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007935
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007936static int _nfs41_test_stateid(struct nfs_server *server,
7937 nfs4_stateid *stateid,
7938 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007939{
7940 int status;
7941 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007942 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007943 };
7944 struct nfs41_test_stateid_res res;
7945 struct rpc_message msg = {
7946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7947 .rpc_argp = &args,
7948 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007949 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007950 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007951 struct rpc_clnt *rpc_client = server->client;
7952
7953 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7954 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007955
Chuck Lever38527b12012-07-11 16:30:23 -04007956 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007957 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007958 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007959 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007960 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007961 if (status != NFS_OK) {
7962 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007963 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007964 }
7965 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007966 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007967}
7968
Chuck Lever38527b12012-07-11 16:30:23 -04007969/**
7970 * nfs41_test_stateid - perform a TEST_STATEID operation
7971 *
7972 * @server: server / transport on which to perform the operation
7973 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007974 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007975 *
7976 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7977 * Otherwise a negative NFS4ERR value is returned if the operation
7978 * failed or the state ID is not currently valid.
7979 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007980static int nfs41_test_stateid(struct nfs_server *server,
7981 nfs4_stateid *stateid,
7982 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007983{
7984 struct nfs4_exception exception = { };
7985 int err;
7986 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007987 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007988 if (err != -NFS4ERR_DELAY)
7989 break;
7990 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007991 } while (exception.retry);
7992 return err;
7993}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007994
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007995struct nfs_free_stateid_data {
7996 struct nfs_server *server;
7997 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007998 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007999};
8000
8001static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8002{
8003 struct nfs_free_stateid_data *data = calldata;
8004 nfs41_setup_sequence(nfs4_get_session(data->server),
8005 &data->args.seq_args,
8006 &data->res.seq_res,
8007 task);
8008}
8009
8010static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8011{
8012 struct nfs_free_stateid_data *data = calldata;
8013
8014 nfs41_sequence_done(task, &data->res.seq_res);
8015
8016 switch (task->tk_status) {
8017 case -NFS4ERR_DELAY:
8018 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8019 rpc_restart_call_prepare(task);
8020 }
8021}
8022
8023static void nfs41_free_stateid_release(void *calldata)
8024{
8025 kfree(calldata);
8026}
8027
Trond Myklebust17f26b12013-08-21 15:48:42 -04008028static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008029 .rpc_call_prepare = nfs41_free_stateid_prepare,
8030 .rpc_call_done = nfs41_free_stateid_done,
8031 .rpc_release = nfs41_free_stateid_release,
8032};
8033
8034static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8035 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008036 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008037 bool privileged)
8038{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008039 struct rpc_message msg = {
8040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008041 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008042 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008043 struct rpc_task_setup task_setup = {
8044 .rpc_client = server->client,
8045 .rpc_message = &msg,
8046 .callback_ops = &nfs41_free_stateid_ops,
8047 .flags = RPC_TASK_ASYNC,
8048 };
8049 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008050
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008051 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8052 &task_setup.rpc_client, &msg);
8053
Chuck Lever38527b12012-07-11 16:30:23 -04008054 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008055 data = kmalloc(sizeof(*data), GFP_NOFS);
8056 if (!data)
8057 return ERR_PTR(-ENOMEM);
8058 data->server = server;
8059 nfs4_stateid_copy(&data->args.stateid, stateid);
8060
8061 task_setup.callback_data = data;
8062
8063 msg.rpc_argp = &data->args;
8064 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008065 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008066 if (privileged)
8067 nfs4_set_sequence_privileged(&data->args.seq_args);
8068
8069 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008070}
8071
Chuck Lever38527b12012-07-11 16:30:23 -04008072/**
8073 * nfs41_free_stateid - perform a FREE_STATEID operation
8074 *
8075 * @server: server / transport on which to perform the operation
8076 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008077 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008078 *
8079 * Returns NFS_OK if the server freed "stateid". Otherwise a
8080 * negative NFS4ERR value is returned.
8081 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008082static int nfs41_free_stateid(struct nfs_server *server,
8083 nfs4_stateid *stateid,
8084 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008085{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008086 struct rpc_task *task;
8087 int ret;
8088
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008089 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008090 if (IS_ERR(task))
8091 return PTR_ERR(task);
8092 ret = rpc_wait_for_completion_task(task);
8093 if (!ret)
8094 ret = task->tk_status;
8095 rpc_put_task(task);
8096 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008097}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008098
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008099static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8100{
8101 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008102 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008103
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008104 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008105 nfs4_free_lock_state(server, lsp);
8106 if (IS_ERR(task))
8107 return PTR_ERR(task);
8108 rpc_put_task(task);
8109 return 0;
8110}
8111
Trond Myklebust36281ca2012-03-04 18:13:56 -05008112static bool nfs41_match_stateid(const nfs4_stateid *s1,
8113 const nfs4_stateid *s2)
8114{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008115 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008116 return false;
8117
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008118 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008119 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008120 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008121 return true;
8122
8123 return false;
8124}
8125
Andy Adamson557134a2009-04-01 09:21:53 -04008126#endif /* CONFIG_NFS_V4_1 */
8127
Trond Myklebust36281ca2012-03-04 18:13:56 -05008128static bool nfs4_match_stateid(const nfs4_stateid *s1,
8129 const nfs4_stateid *s2)
8130{
Trond Myklebustf597c532012-03-04 18:13:56 -05008131 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008132}
8133
8134
Trond Myklebust17280172012-03-11 13:11:00 -04008135static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008136 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008137 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138 .recover_open = nfs4_open_reclaim,
8139 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008140 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008141 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142};
8143
Andy Adamson591d71c2009-04-01 09:22:47 -04008144#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008145static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008146 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8147 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8148 .recover_open = nfs4_open_reclaim,
8149 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008150 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008151 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008152 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008153};
8154#endif /* CONFIG_NFS_V4_1 */
8155
Trond Myklebust17280172012-03-11 13:11:00 -04008156static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008157 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008158 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159 .recover_open = nfs4_open_expired,
8160 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008161 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162};
8163
Andy Adamson591d71c2009-04-01 09:22:47 -04008164#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008165static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008166 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8167 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008168 .recover_open = nfs41_open_expired,
8169 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008170 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008171};
8172#endif /* CONFIG_NFS_V4_1 */
8173
Trond Myklebust17280172012-03-11 13:11:00 -04008174static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008175 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008176 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008177 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008178};
8179
8180#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008181static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008182 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008183 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008184 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008185};
8186#endif
8187
Chuck Leverec011fe2013-10-17 14:12:39 -04008188static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008189 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008190 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008191};
8192
8193#if defined(CONFIG_NFS_V4_1)
8194static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008195 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008196 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008197};
8198#endif /* CONFIG_NFS_V4_1 */
8199
Trond Myklebust97dc1352010-06-16 09:52:26 -04008200static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8201 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008202 .init_caps = NFS_CAP_READDIRPLUS
8203 | NFS_CAP_ATOMIC_OPEN
8204 | NFS_CAP_CHANGE_ATTR
8205 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008206 .init_client = nfs40_init_client,
8207 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008208 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008209 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008210 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008211 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008212 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8213 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8214 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008215 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008216};
8217
8218#if defined(CONFIG_NFS_V4_1)
8219static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8220 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008221 .init_caps = NFS_CAP_READDIRPLUS
8222 | NFS_CAP_ATOMIC_OPEN
8223 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008224 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008225 | NFS_CAP_STATEID_NFSV41
8226 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008227 .init_client = nfs41_init_client,
8228 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008229 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008230 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008231 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008232 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008233 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8234 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8235 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008236 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008237};
8238#endif
8239
Steve Dickson42c2c422013-05-22 12:50:38 -04008240#if defined(CONFIG_NFS_V4_2)
8241static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8242 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008243 .init_caps = NFS_CAP_READDIRPLUS
8244 | NFS_CAP_ATOMIC_OPEN
8245 | NFS_CAP_CHANGE_ATTR
8246 | NFS_CAP_POSIX_LOCK
8247 | NFS_CAP_STATEID_NFSV41
8248 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008249 .init_client = nfs41_init_client,
8250 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008251 .match_stateid = nfs41_match_stateid,
8252 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008253 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008254 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008255 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8256 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8257 .state_renewal_ops = &nfs41_state_renewal_ops,
8258};
8259#endif
8260
Trond Myklebust97dc1352010-06-16 09:52:26 -04008261const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8262 [0] = &nfs_v4_0_minor_ops,
8263#if defined(CONFIG_NFS_V4_1)
8264 [1] = &nfs_v4_1_minor_ops,
8265#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008266#if defined(CONFIG_NFS_V4_2)
8267 [2] = &nfs_v4_2_minor_ops,
8268#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008269};
8270
Trond Myklebust17f26b12013-08-21 15:48:42 -04008271static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008272 .create = nfs_create,
8273 .lookup = nfs_lookup,
8274 .atomic_open = nfs_atomic_open,
8275 .link = nfs_link,
8276 .unlink = nfs_unlink,
8277 .symlink = nfs_symlink,
8278 .mkdir = nfs_mkdir,
8279 .rmdir = nfs_rmdir,
8280 .mknod = nfs_mknod,
8281 .rename = nfs_rename,
8282 .permission = nfs_permission,
8283 .getattr = nfs_getattr,
8284 .setattr = nfs_setattr,
8285 .getxattr = generic_getxattr,
8286 .setxattr = generic_setxattr,
8287 .listxattr = generic_listxattr,
8288 .removexattr = generic_removexattr,
8289};
8290
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008291static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008292 .permission = nfs_permission,
8293 .getattr = nfs_getattr,
8294 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008295 .getxattr = generic_getxattr,
8296 .setxattr = generic_setxattr,
8297 .listxattr = generic_listxattr,
8298 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008299};
8300
David Howells509de812006-08-22 20:06:11 -04008301const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302 .version = 4, /* protocol version */
8303 .dentry_ops = &nfs4_dentry_operations,
8304 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008305 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008306 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008308 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008309 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310 .getattr = nfs4_proc_getattr,
8311 .setattr = nfs4_proc_setattr,
8312 .lookup = nfs4_proc_lookup,
8313 .access = nfs4_proc_access,
8314 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315 .create = nfs4_proc_create,
8316 .remove = nfs4_proc_remove,
8317 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008318 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 .unlink_done = nfs4_proc_unlink_done,
8320 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008321 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008322 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008323 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 .link = nfs4_proc_link,
8325 .symlink = nfs4_proc_symlink,
8326 .mkdir = nfs4_proc_mkdir,
8327 .rmdir = nfs4_proc_remove,
8328 .readdir = nfs4_proc_readdir,
8329 .mknod = nfs4_proc_mknod,
8330 .statfs = nfs4_proc_statfs,
8331 .fsinfo = nfs4_proc_fsinfo,
8332 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008333 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008334 .decode_dirent = nfs4_decode_dirent,
8335 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008336 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008337 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008338 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008340 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008341 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008342 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008344 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008345 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008347 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008348 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008349 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008350 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008351 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008352 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008353 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008354 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008355 .create_server = nfs4_create_server,
8356 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357};
8358
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008359static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8360 .prefix = XATTR_NAME_NFSV4_ACL,
8361 .list = nfs4_xattr_list_nfs4_acl,
8362 .get = nfs4_xattr_get_nfs4_acl,
8363 .set = nfs4_xattr_set_nfs4_acl,
8364};
8365
8366const struct xattr_handler *nfs4_xattr_handlers[] = {
8367 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008368#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8369 &nfs4_xattr_nfs4_label_handler,
8370#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008371 NULL
8372};
8373
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374/*
8375 * Local variables:
8376 * c-basic-offset: 8
8377 * End:
8378 */